1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.mergeResolutions = exports.nftSelectors = exports.tokenSelectors = exports.intAsHexBytes = exports.decodeTxInfo = exports.maybeHexBuffer = exports.hexBuffer = exports.splitPath = exports.padHexString = exports.ERC1155_CLEAR_SIGNED_SELECTORS = exports.ERC721_CLEAR_SIGNED_SELECTORS = exports.ERC20_CLEAR_SIGNED_SELECTORS = void 0;
|
4 | const bignumber_js_1 = require("bignumber.js");
|
5 | const index_1 = require("@ledgerhq/evm-tools/selectors/index");
|
6 | Object.defineProperty(exports, "ERC20_CLEAR_SIGNED_SELECTORS", { enumerable: true, get: function () { return index_1.ERC20_CLEAR_SIGNED_SELECTORS; } });
|
7 | Object.defineProperty(exports, "ERC721_CLEAR_SIGNED_SELECTORS", { enumerable: true, get: function () { return index_1.ERC721_CLEAR_SIGNED_SELECTORS; } });
|
8 | Object.defineProperty(exports, "ERC1155_CLEAR_SIGNED_SELECTORS", { enumerable: true, get: function () { return index_1.ERC1155_CLEAR_SIGNED_SELECTORS; } });
|
9 | const rlp_1 = require("@ethersproject/rlp");
|
10 | const padHexString = (str) => {
|
11 | return str.length % 2 ? "0" + str : str;
|
12 | };
|
13 | exports.padHexString = padHexString;
|
14 | function splitPath(path) {
|
15 | const result = [];
|
16 | const components = path.split("/");
|
17 | components.forEach(element => {
|
18 | let number = parseInt(element, 10);
|
19 | if (isNaN(number)) {
|
20 | return;
|
21 | }
|
22 | if (element.length > 1 && element[element.length - 1] === "'") {
|
23 | number += 0x80000000;
|
24 | }
|
25 | result.push(number);
|
26 | });
|
27 | return result;
|
28 | }
|
29 | exports.splitPath = splitPath;
|
30 | function hexBuffer(str) {
|
31 | const strWithoutPrefix = str.startsWith("0x") ? str.slice(2) : str;
|
32 | return Buffer.from((0, exports.padHexString)(strWithoutPrefix), "hex");
|
33 | }
|
34 | exports.hexBuffer = hexBuffer;
|
35 | function maybeHexBuffer(str) {
|
36 | if (!str)
|
37 | return null;
|
38 | return hexBuffer(str);
|
39 | }
|
40 | exports.maybeHexBuffer = maybeHexBuffer;
|
41 | const decodeTxInfo = (rawTx) => {
|
42 | const VALID_TYPES = [1, 2];
|
43 | const txType = VALID_TYPES.includes(rawTx[0]) ? rawTx[0] : null;
|
44 | const rlpData = txType === null ? rawTx : rawTx.slice(1);
|
45 | const rlpTx = (0, rlp_1.decode)(rlpData).map(hex => Buffer.from(hex.slice(2), "hex"));
|
46 | let chainIdTruncated = 0;
|
47 | const rlpDecoded = (0, rlp_1.decode)(rlpData);
|
48 | let decodedTx;
|
49 | if (txType === 2) {
|
50 |
|
51 | decodedTx = {
|
52 | data: rlpDecoded[7],
|
53 | to: rlpDecoded[5],
|
54 | chainId: rlpTx[0],
|
55 | };
|
56 | }
|
57 | else if (txType === 1) {
|
58 |
|
59 | decodedTx = {
|
60 | data: rlpDecoded[6],
|
61 | to: rlpDecoded[4],
|
62 | chainId: rlpTx[0],
|
63 | };
|
64 | }
|
65 | else {
|
66 |
|
67 | decodedTx = {
|
68 | data: rlpDecoded[5],
|
69 | to: rlpDecoded[3],
|
70 |
|
71 | chainId: rlpTx.length > 6 ? rlpTx[6] : Buffer.from("0x01", "hex"),
|
72 | };
|
73 | }
|
74 | const chainIdSrc = decodedTx.chainId;
|
75 | let chainId = new bignumber_js_1.BigNumber(0);
|
76 | if (chainIdSrc) {
|
77 |
|
78 | chainId = new bignumber_js_1.BigNumber(chainIdSrc.toString("hex"), 16);
|
79 | const chainIdTruncatedBuf = Buffer.alloc(4);
|
80 | if (chainIdSrc.length > 4) {
|
81 | chainIdSrc.copy(chainIdTruncatedBuf);
|
82 | }
|
83 | else {
|
84 | chainIdSrc.copy(chainIdTruncatedBuf, 4 - chainIdSrc.length);
|
85 | }
|
86 | chainIdTruncated = chainIdTruncatedBuf.readUInt32BE(0);
|
87 | }
|
88 | let vrsOffset = 0;
|
89 | if (txType === null && rlpTx.length > 6) {
|
90 | const rlpVrs = Buffer.from((0, rlp_1.encode)(rlpTx.slice(-3)).slice(2), "hex");
|
91 | vrsOffset = rawTx.length - (rlpVrs.length - 1);
|
92 |
|
93 | if (rlpVrs[0] > 0xf7) {
|
94 |
|
95 | vrsOffset++;
|
96 |
|
97 | const sizeOfListLen = rlpVrs[0] - 0xf7;
|
98 |
|
99 | vrsOffset += sizeOfListLen - 1;
|
100 | }
|
101 | }
|
102 | return {
|
103 | decodedTx,
|
104 | txType,
|
105 | chainId,
|
106 | chainIdTruncated,
|
107 | vrsOffset,
|
108 | };
|
109 | };
|
110 | exports.decodeTxInfo = decodeTxInfo;
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | const intAsHexBytes = (int, bytes) => int.toString(16).padStart(2 * bytes, "0");
|
122 | exports.intAsHexBytes = intAsHexBytes;
|
123 | exports.tokenSelectors = Object.values(index_1.ERC20_CLEAR_SIGNED_SELECTORS);
|
124 | exports.nftSelectors = [
|
125 | ...Object.values(index_1.ERC721_CLEAR_SIGNED_SELECTORS),
|
126 | ...Object.values(index_1.ERC1155_CLEAR_SIGNED_SELECTORS),
|
127 | ];
|
128 | const mergeResolutions = (resolutionsArray) => {
|
129 | const mergedResolutions = {
|
130 | nfts: [],
|
131 | erc20Tokens: [],
|
132 | externalPlugin: [],
|
133 | plugin: [],
|
134 | domains: [],
|
135 | };
|
136 | for (const resolutions of resolutionsArray) {
|
137 | for (const key in resolutions) {
|
138 | mergedResolutions[key].push(...resolutions[key]);
|
139 | }
|
140 | }
|
141 | return mergedResolutions;
|
142 | };
|
143 | exports.mergeResolutions = mergeResolutions;
|
144 |
|
\ | No newline at end of file |