1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11 | }) : function(o, v) {
|
12 | o["default"] = v;
|
13 | });
|
14 | var __importStar = (this && this.__importStar) || function (mod) {
|
15 | if (mod && mod.__esModule) return mod;
|
16 | var result = {};
|
17 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18 | __setModuleDefault(result, mod);
|
19 | return result;
|
20 | };
|
21 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
22 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
23 | };
|
24 | Object.defineProperty(exports, "__esModule", { value: true });
|
25 | exports.isZeroAddress = exports.zeroAddress = exports.importPublic = exports.privateToAddress = exports.privateToPublic = exports.publicToAddress = exports.pubToAddress = exports.isValidPublic = exports.isValidPrivate = exports.generateAddress2 = exports.generateAddress = exports.isValidChecksumAddress = exports.toChecksumAddress = exports.isValidAddress = exports.Account = void 0;
|
26 | const assert_1 = __importDefault(require("assert"));
|
27 | const bn_js_1 = __importDefault(require("bn.js"));
|
28 | const rlp = __importStar(require("rlp"));
|
29 | const secp256k1_1 = require("ethereum-cryptography/secp256k1");
|
30 | const internal_1 = require("./internal");
|
31 | const constants_1 = require("./constants");
|
32 | const bytes_1 = require("./bytes");
|
33 | const hash_1 = require("./hash");
|
34 | const helpers_1 = require("./helpers");
|
35 | const types_1 = require("./types");
|
36 | class Account {
|
37 | |
38 |
|
39 |
|
40 |
|
41 | constructor(nonce = new bn_js_1.default(0), balance = new bn_js_1.default(0), stateRoot = constants_1.KECCAK256_RLP, codeHash = constants_1.KECCAK256_NULL) {
|
42 | this.nonce = nonce;
|
43 | this.balance = balance;
|
44 | this.stateRoot = stateRoot;
|
45 | this.codeHash = codeHash;
|
46 | this._validate();
|
47 | }
|
48 | static fromAccountData(accountData) {
|
49 | const { nonce, balance, stateRoot, codeHash } = accountData;
|
50 | return new Account(nonce ? new bn_js_1.default((0, bytes_1.toBuffer)(nonce)) : undefined, balance ? new bn_js_1.default((0, bytes_1.toBuffer)(balance)) : undefined, stateRoot ? (0, bytes_1.toBuffer)(stateRoot) : undefined, codeHash ? (0, bytes_1.toBuffer)(codeHash) : undefined);
|
51 | }
|
52 | static fromRlpSerializedAccount(serialized) {
|
53 | const values = rlp.decode(serialized);
|
54 | if (!Array.isArray(values)) {
|
55 | throw new Error('Invalid serialized account input. Must be array');
|
56 | }
|
57 | return this.fromValuesArray(values);
|
58 | }
|
59 | static fromValuesArray(values) {
|
60 | const [nonce, balance, stateRoot, codeHash] = values;
|
61 | return new Account(new bn_js_1.default(nonce), new bn_js_1.default(balance), stateRoot, codeHash);
|
62 | }
|
63 | _validate() {
|
64 | if (this.nonce.lt(new bn_js_1.default(0))) {
|
65 | throw new Error('nonce must be greater than zero');
|
66 | }
|
67 | if (this.balance.lt(new bn_js_1.default(0))) {
|
68 | throw new Error('balance must be greater than zero');
|
69 | }
|
70 | if (this.stateRoot.length !== 32) {
|
71 | throw new Error('stateRoot must have a length of 32');
|
72 | }
|
73 | if (this.codeHash.length !== 32) {
|
74 | throw new Error('codeHash must have a length of 32');
|
75 | }
|
76 | }
|
77 | |
78 |
|
79 |
|
80 | raw() {
|
81 | return [
|
82 | (0, types_1.bnToUnpaddedBuffer)(this.nonce),
|
83 | (0, types_1.bnToUnpaddedBuffer)(this.balance),
|
84 | this.stateRoot,
|
85 | this.codeHash,
|
86 | ];
|
87 | }
|
88 | |
89 |
|
90 |
|
91 | serialize() {
|
92 | return rlp.encode(this.raw());
|
93 | }
|
94 | |
95 |
|
96 |
|
97 | isContract() {
|
98 | return !this.codeHash.equals(constants_1.KECCAK256_NULL);
|
99 | }
|
100 | |
101 |
|
102 |
|
103 |
|
104 |
|
105 | isEmpty() {
|
106 | return this.balance.isZero() && this.nonce.isZero() && this.codeHash.equals(constants_1.KECCAK256_NULL);
|
107 | }
|
108 | }
|
109 | exports.Account = Account;
|
110 |
|
111 |
|
112 |
|
113 | const isValidAddress = function (hexAddress) {
|
114 | try {
|
115 | (0, helpers_1.assertIsString)(hexAddress);
|
116 | }
|
117 | catch (e) {
|
118 | return false;
|
119 | }
|
120 | return /^0x[0-9a-fA-F]{40}$/.test(hexAddress);
|
121 | };
|
122 | exports.isValidAddress = isValidAddress;
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | const toChecksumAddress = function (hexAddress, eip1191ChainId) {
|
136 | (0, helpers_1.assertIsHexString)(hexAddress);
|
137 | const address = (0, internal_1.stripHexPrefix)(hexAddress).toLowerCase();
|
138 | let prefix = '';
|
139 | if (eip1191ChainId) {
|
140 | const chainId = (0, types_1.toType)(eip1191ChainId, types_1.TypeOutput.BN);
|
141 | prefix = chainId.toString() + '0x';
|
142 | }
|
143 | const hash = (0, hash_1.keccakFromString)(prefix + address).toString('hex');
|
144 | let ret = '0x';
|
145 | for (let i = 0; i < address.length; i++) {
|
146 | if (parseInt(hash[i], 16) >= 8) {
|
147 | ret += address[i].toUpperCase();
|
148 | }
|
149 | else {
|
150 | ret += address[i];
|
151 | }
|
152 | }
|
153 | return ret;
|
154 | };
|
155 | exports.toChecksumAddress = toChecksumAddress;
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | const isValidChecksumAddress = function (hexAddress, eip1191ChainId) {
|
162 | return (0, exports.isValidAddress)(hexAddress) && (0, exports.toChecksumAddress)(hexAddress, eip1191ChainId) === hexAddress;
|
163 | };
|
164 | exports.isValidChecksumAddress = isValidChecksumAddress;
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | const generateAddress = function (from, nonce) {
|
171 | (0, helpers_1.assertIsBuffer)(from);
|
172 | (0, helpers_1.assertIsBuffer)(nonce);
|
173 | const nonceBN = new bn_js_1.default(nonce);
|
174 | if (nonceBN.isZero()) {
|
175 |
|
176 |
|
177 | return (0, hash_1.rlphash)([from, null]).slice(-20);
|
178 | }
|
179 |
|
180 | return (0, hash_1.rlphash)([from, Buffer.from(nonceBN.toArray())]).slice(-20);
|
181 | };
|
182 | exports.generateAddress = generateAddress;
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | const generateAddress2 = function (from, salt, initCode) {
|
190 | (0, helpers_1.assertIsBuffer)(from);
|
191 | (0, helpers_1.assertIsBuffer)(salt);
|
192 | (0, helpers_1.assertIsBuffer)(initCode);
|
193 | (0, assert_1.default)(from.length === 20);
|
194 | (0, assert_1.default)(salt.length === 32);
|
195 | const address = (0, hash_1.keccak256)(Buffer.concat([Buffer.from('ff', 'hex'), from, salt, (0, hash_1.keccak256)(initCode)]));
|
196 | return address.slice(-20);
|
197 | };
|
198 | exports.generateAddress2 = generateAddress2;
|
199 |
|
200 |
|
201 |
|
202 | const isValidPrivate = function (privateKey) {
|
203 | return (0, secp256k1_1.privateKeyVerify)(privateKey);
|
204 | };
|
205 | exports.isValidPrivate = isValidPrivate;
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | const isValidPublic = function (publicKey, sanitize = false) {
|
213 | (0, helpers_1.assertIsBuffer)(publicKey);
|
214 | if (publicKey.length === 64) {
|
215 |
|
216 | return (0, secp256k1_1.publicKeyVerify)(Buffer.concat([Buffer.from([4]), publicKey]));
|
217 | }
|
218 | if (!sanitize) {
|
219 | return false;
|
220 | }
|
221 | return (0, secp256k1_1.publicKeyVerify)(publicKey);
|
222 | };
|
223 | exports.isValidPublic = isValidPublic;
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | const pubToAddress = function (pubKey, sanitize = false) {
|
231 | (0, helpers_1.assertIsBuffer)(pubKey);
|
232 | if (sanitize && pubKey.length !== 64) {
|
233 | pubKey = Buffer.from((0, secp256k1_1.publicKeyConvert)(pubKey, false).slice(1));
|
234 | }
|
235 | (0, assert_1.default)(pubKey.length === 64);
|
236 |
|
237 | return (0, hash_1.keccak)(pubKey).slice(-20);
|
238 | };
|
239 | exports.pubToAddress = pubToAddress;
|
240 | exports.publicToAddress = exports.pubToAddress;
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | const privateToPublic = function (privateKey) {
|
246 | (0, helpers_1.assertIsBuffer)(privateKey);
|
247 |
|
248 | return Buffer.from((0, secp256k1_1.publicKeyCreate)(privateKey, false)).slice(1);
|
249 | };
|
250 | exports.privateToPublic = privateToPublic;
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | const privateToAddress = function (privateKey) {
|
256 | return (0, exports.publicToAddress)((0, exports.privateToPublic)(privateKey));
|
257 | };
|
258 | exports.privateToAddress = privateToAddress;
|
259 |
|
260 |
|
261 |
|
262 | const importPublic = function (publicKey) {
|
263 | (0, helpers_1.assertIsBuffer)(publicKey);
|
264 | if (publicKey.length !== 64) {
|
265 | publicKey = Buffer.from((0, secp256k1_1.publicKeyConvert)(publicKey, false).slice(1));
|
266 | }
|
267 | return publicKey;
|
268 | };
|
269 | exports.importPublic = importPublic;
|
270 |
|
271 |
|
272 |
|
273 | const zeroAddress = function () {
|
274 | const addressLength = 20;
|
275 | const addr = (0, bytes_1.zeros)(addressLength);
|
276 | return (0, bytes_1.bufferToHex)(addr);
|
277 | };
|
278 | exports.zeroAddress = zeroAddress;
|
279 |
|
280 |
|
281 |
|
282 | const isZeroAddress = function (hexAddress) {
|
283 | try {
|
284 | (0, helpers_1.assertIsString)(hexAddress);
|
285 | }
|
286 | catch (e) {
|
287 | return false;
|
288 | }
|
289 | const zeroAddr = (0, exports.zeroAddress)();
|
290 | return zeroAddr === hexAddress;
|
291 | };
|
292 | exports.isZeroAddress = isZeroAddress;
|
293 |
|
\ | No newline at end of file |