UNPKG

5.57 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.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;
4const bignumber_js_1 = require("bignumber.js");
5const index_1 = require("@ledgerhq/evm-tools/selectors/index");
6Object.defineProperty(exports, "ERC20_CLEAR_SIGNED_SELECTORS", { enumerable: true, get: function () { return index_1.ERC20_CLEAR_SIGNED_SELECTORS; } });
7Object.defineProperty(exports, "ERC721_CLEAR_SIGNED_SELECTORS", { enumerable: true, get: function () { return index_1.ERC721_CLEAR_SIGNED_SELECTORS; } });
8Object.defineProperty(exports, "ERC1155_CLEAR_SIGNED_SELECTORS", { enumerable: true, get: function () { return index_1.ERC1155_CLEAR_SIGNED_SELECTORS; } });
9const rlp_1 = require("@ethersproject/rlp");
10const padHexString = (str) => {
11 return str.length % 2 ? "0" + str : str;
12};
13exports.padHexString = padHexString;
14function 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; // FIXME shouldn't it throws instead?
21 }
22 if (element.length > 1 && element[element.length - 1] === "'") {
23 number += 0x80000000;
24 }
25 result.push(number);
26 });
27 return result;
28}
29exports.splitPath = splitPath;
30function hexBuffer(str) {
31 const strWithoutPrefix = str.startsWith("0x") ? str.slice(2) : str;
32 return Buffer.from((0, exports.padHexString)(strWithoutPrefix), "hex");
33}
34exports.hexBuffer = hexBuffer;
35function maybeHexBuffer(str) {
36 if (!str)
37 return null;
38 return hexBuffer(str);
39}
40exports.maybeHexBuffer = maybeHexBuffer;
41const 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 // EIP1559
51 decodedTx = {
52 data: rlpDecoded[7],
53 to: rlpDecoded[5],
54 chainId: rlpTx[0],
55 };
56 }
57 else if (txType === 1) {
58 // EIP2930
59 decodedTx = {
60 data: rlpDecoded[6],
61 to: rlpDecoded[4],
62 chainId: rlpTx[0],
63 };
64 }
65 else {
66 // Legacy tx
67 decodedTx = {
68 data: rlpDecoded[5],
69 to: rlpDecoded[3],
70 // Default to 1 for non EIP 155 txs
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 // Using BigNumber because chainID could be any uint256.
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 // First byte > 0xf7 means the length of the list length doesn't fit in a single byte.
93 if (rlpVrs[0] > 0xf7) {
94 // Increment vrsOffset to account for that extra byte.
95 vrsOffset++;
96 // Compute size of the list length.
97 const sizeOfListLen = rlpVrs[0] - 0xf7;
98 // Increase rlpOffset by the size of the list length.
99 vrsOffset += sizeOfListLen - 1;
100 }
101 }
102 return {
103 decodedTx,
104 txType,
105 chainId,
106 chainIdTruncated,
107 vrsOffset,
108 };
109};
110exports.decodeTxInfo = decodeTxInfo;
111/**
112 * @ignore for the README
113 *
114 * Helper to convert an integer as a hexadecimal string with the right amount of digits
115 * to respect the number of bytes given as parameter
116 *
117 * @param int Integer
118 * @param bytes Number of bytes it should be represented as (1 byte = 2 caraters)
119 * @returns The given integer as an hexa string padded with the right number of 0
120 */
121const intAsHexBytes = (int, bytes) => int.toString(16).padStart(2 * bytes, "0");
122exports.intAsHexBytes = intAsHexBytes;
123exports.tokenSelectors = Object.values(index_1.ERC20_CLEAR_SIGNED_SELECTORS);
124exports.nftSelectors = [
125 ...Object.values(index_1.ERC721_CLEAR_SIGNED_SELECTORS),
126 ...Object.values(index_1.ERC1155_CLEAR_SIGNED_SELECTORS),
127];
128const 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};
143exports.mergeResolutions = mergeResolutions;
144//# sourceMappingURL=utils.js.map
\No newline at end of file