-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest-ethfunctions.cjs
More file actions
333 lines (285 loc) · 14.2 KB
/
test-ethfunctions.cjs
File metadata and controls
333 lines (285 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* Ethereum / Blockchain Functions Tests
* Run: npm install ethers (if not installed) && node test-ethfunctions.cjs
*
* Tests covering:
* - Address validation logic (EVM, Bitcoin, Tron, Solana patterns)
* - Unit conversion arithmetic (ETH, BTC, SOL)
* - ABI encoding / decoding
* - Transaction decoding (legacy + EIP-1559)
* - Function signature parsing
*/
'use strict';
var ethers;
try {
ethers = require('ethers');
} catch (e) {
console.error('ethers not installed. Run: npm install ethers');
process.exit(1);
}
/* ===== Test harness ===== */
var passed = 0;
var failed = 0;
var errors = [];
function assert(condition, name, detail) {
if (condition) {
passed++;
console.log(' PASS: ' + name);
} else {
failed++;
var msg = ' FAIL: ' + name + (detail ? ' — ' + detail : '');
console.log(msg);
errors.push(msg);
}
}
function assertEqual(actual, expected, name) {
var ok = actual === expected;
if (!ok) {
assert(false, name, 'expected ' + JSON.stringify(expected) + ', got ' + JSON.stringify(actual));
} else {
assert(true, name);
}
}
function assertThrows(fn, name) {
try {
fn();
failed++;
var msg = ' FAIL: ' + name + ' — expected error, none thrown';
console.log(msg);
errors.push(msg);
} catch (e) {
passed++;
console.log(' PASS: ' + name + ' (threw: ' + e.message.slice(0, 60) + ')');
}
}
/* ===== Helper: parseFuncSig (copied from JSP) ===== */
function parseFuncSig(sig) {
sig = sig.trim();
var m = sig.match(/^(\w+)\(([^)]*)\)$/);
if (!m) throw new Error('Invalid function signature. Use format: functionName(type1,type2)');
var name = m[1];
var types = m[2] ? m[2].split(',').map(function(t) { return t.trim(); }) : [];
return { name: name, types: types, full: name + '(' + types.join(',') + ')' };
}
/* ===== Helper: detectAndValidate (simplified for Node, EVM-only uses ethers) ===== */
function detectAndValidate(addr) {
// EVM: 0x + 40 hex
if (/^0x[0-9a-fA-F]{40}$/.test(addr)) {
var valid = ethers.isAddress(addr);
var checksummed = '';
try { checksummed = ethers.getAddress(addr); } catch(e) { /* ignore */ }
var hasChecksum = addr !== addr.toLowerCase() && addr !== addr.toUpperCase();
var checksumCorrect = hasChecksum ? (addr === checksummed) : true;
return {
valid: valid && checksumCorrect,
chain: 'Ethereum / EVM',
type: 'EOA or Contract Address',
checksummed: checksummed
};
}
// Bitcoin Bech32
if (/^(bc1|tb1)/i.test(addr)) {
return { valid: true, chain: 'Bitcoin', type: 'Bech32/Bech32m' };
}
// Tron
if (/^T[1-9A-HJ-NP-Za-km-z]{33}$/.test(addr)) {
return { valid: true, chain: 'Tron', type: 'Base58Check' };
}
// Bitcoin P2PKH/P2SH
if (/^[13][1-9A-HJ-NP-Za-km-z]{25,34}$/.test(addr)) {
return { valid: true, chain: 'Bitcoin', type: 'Base58Check' };
}
// Solana
if (/^[1-9A-HJ-NP-Za-km-z]{32,44}$/.test(addr)) {
return { valid: true, chain: 'Solana', type: 'Ed25519' };
}
return { valid: false, chain: 'Unknown', type: 'Unknown' };
}
/* ============================================================
* Suite 1: Address Validation
* ============================================================ */
console.log('\n=== Suite 1: Address Validation ===');
(function() {
// Valid EVM — checksummed
var r1 = detectAndValidate('0x742D35CC6634C0532925a3B844Bc9E7595F2bD18');
assert(r1.valid, 'Valid EVM address (checksummed)');
assertEqual(r1.chain, 'Ethereum / EVM', 'EVM chain detection');
// Valid EVM — all lowercase (no checksum to fail)
var r2 = detectAndValidate('0x742d35cc6634c0532925a3b844bc9e7595f2bd18');
assert(r2.valid, 'Valid EVM address (lowercase, no checksum)');
// Invalid EVM — wrong checksum (swap case on purpose)
var r3 = detectAndValidate('0x742D35CC6634C0532925a3b844Bc9e7595f2bD18');
assert(!r3.valid, 'Invalid EVM address (bad checksum)');
// Valid Bitcoin P2PKH (Satoshi's address)
var r4 = detectAndValidate('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa');
assert(r4.valid, 'Valid Bitcoin P2PKH address');
assertEqual(r4.chain, 'Bitcoin', 'Bitcoin chain detection');
// Valid Bitcoin Bech32
var r5 = detectAndValidate('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4');
assert(r5.valid, 'Valid Bitcoin Bech32 address');
assertEqual(r5.chain, 'Bitcoin', 'Bitcoin bech32 chain detection');
// Valid Tron address
var r6 = detectAndValidate('TJCnKsPa7y5okkXvQAidZBzqx3QyQ6sxMW');
assert(r6.valid, 'Valid Tron address');
assertEqual(r6.chain, 'Tron', 'Tron chain detection');
// Valid Solana address
var r7 = detectAndValidate('7EcDhSYGxXyscszYEp35KHN8vvw3svAuLKTzXwCFLtV');
assert(r7.valid, 'Valid Solana address');
assertEqual(r7.chain, 'Solana', 'Solana chain detection');
// Invalid/garbage
var r8 = detectAndValidate('hello_world_not_an_address');
assert(!r8.valid, 'Invalid garbage input');
assertEqual(r8.chain, 'Unknown', 'Unknown chain for garbage');
// Too short for any chain
var r9 = detectAndValidate('0x123');
assert(!r9.valid, 'Too short for EVM');
})();
/* ============================================================
* Suite 2: Unit Conversion
* ============================================================ */
console.log('\n=== Suite 2: Unit Conversion ===');
(function() {
// 1 ETH = 10^18 Wei
var oneEthWei = ethers.parseUnits('1', 'ether');
assertEqual(oneEthWei.toString(), '1000000000000000000', '1 ETH = 10^18 Wei');
// 1 ETH = 10^9 Gwei
var oneEthGwei = ethers.formatUnits(oneEthWei, 'gwei');
assertEqual(oneEthGwei, '1000000000.0', '1 ETH = 10^9 Gwei');
// Gwei back to ETH
var gweiToEth = ethers.formatUnits(ethers.parseUnits('1000000000', 'gwei'), 'ether');
assertEqual(gweiToEth, '1.0', '10^9 Gwei = 1 ETH');
// Zero
var zeroWei = ethers.parseUnits('0', 'ether');
assertEqual(zeroWei.toString(), '0', '0 ETH = 0 Wei');
// Very large: 1 million ETH
var bigEth = ethers.parseUnits('1000000', 'ether');
assertEqual(bigEth.toString(), '1000000000000000000000000', '1M ETH in Wei');
// BTC: 1 BTC = 10^8 Satoshi (integer math)
assertEqual(Math.round(1 * 100000000), 100000000, '1 BTC = 10^8 Satoshi');
// SOL: 1 SOL = 10^9 Lamport
assertEqual(Math.round(1 * 1000000000), 1000000000, '1 SOL = 10^9 Lamport');
// Szabo: 1 ETH = 10^6 Szabo
var oneEthSzabo = ethers.formatUnits(oneEthWei, 6);
assertEqual(oneEthSzabo, '1000000000000.0', '1 ETH in Szabo units (10^12 microether)');
// Finney: 1 ETH = 10^3 Finney
var oneEthFinney = ethers.formatUnits(oneEthWei, 15);
assertEqual(oneEthFinney, '1000.0', '1 ETH = 1000 Finney');
})();
/* ============================================================
* Suite 3: ABI Encoding / Decoding
* ============================================================ */
console.log('\n=== Suite 3: ABI Encoding / Decoding ===');
(function() {
var abiCoder = ethers.AbiCoder.defaultAbiCoder();
// transfer(address,uint256) selector
var transferSig = 'transfer(address,uint256)';
var transferSelector = ethers.id(transferSig).slice(0, 10);
assertEqual(transferSelector, '0xa9059cbb', 'transfer selector = 0xa9059cbb');
// approve(address,uint256) selector
var approveSig = 'approve(address,uint256)';
var approveSelector = ethers.id(approveSig).slice(0, 10);
assertEqual(approveSelector, '0x095ea7b3', 'approve selector = 0x095ea7b3');
// balanceOf(address) selector
var balanceOfSig = 'balanceOf(address)';
var balanceOfSelector = ethers.id(balanceOfSig).slice(0, 10);
assertEqual(balanceOfSelector, '0x70a08231', 'balanceOf selector = 0x70a08231');
// Encode transfer calldata
var toAddr = '0x742D35CC6634C0532925a3B844Bc9E7595F2bD18';
var amount = BigInt('1000000000000000000');
var encoded = abiCoder.encode(['address', 'uint256'], [toAddr, amount]);
var calldata = transferSelector + encoded.slice(2);
assert(calldata.startsWith('0xa9059cbb'), 'Encoded calldata starts with transfer selector');
assertEqual(calldata.length, 2 + 8 + 128, 'Calldata length: 4-byte selector + 2x32-byte params = 138 hex chars');
// Decode that calldata back
var paramData = '0x' + calldata.slice(10);
var decoded = abiCoder.decode(['address', 'uint256'], paramData);
assertEqual(decoded[0].toLowerCase(), toAddr.toLowerCase(), 'Decoded address matches');
assertEqual(decoded[1].toString(), '1000000000000000000', 'Decoded uint256 matches');
// Encode/decode balanceOf
var balEncoded = abiCoder.encode(['address'], [toAddr]);
var balCalldata = balanceOfSelector + balEncoded.slice(2);
assert(balCalldata.startsWith('0x70a08231'), 'balanceOf calldata correct selector');
var balDecoded = abiCoder.decode(['address'], '0x' + balCalldata.slice(10));
assertEqual(balDecoded[0].toLowerCase(), toAddr.toLowerCase(), 'balanceOf decoded address matches');
})();
/* ============================================================
* Suite 4: Transaction Decoding
* ============================================================ */
console.log('\n=== Suite 4: Transaction Decoding ===');
(function() {
// Legacy transaction (type 0)
var legacyTxHex = '0xf86c0a8502540be400825208944bbeeb066ed09b7aed07bf39eee0460dfa261520880de0b6b3a76400008025a028ef61340bd939bc2195fe537567866003e1a15d3c71ff63e1590620aa636276a067cbe9d8997f761aecb703304b3800ccf555c9f3dc64214b297fb1966a3b6d83';
var legacyTx = ethers.Transaction.from(legacyTxHex);
assertEqual(legacyTx.type, 0, 'Legacy tx type = 0');
assertEqual(legacyTx.nonce, 10, 'Legacy tx nonce = 10');
assertEqual(legacyTx.to.toLowerCase(), '0x4bbeeb066ed09b7aed07bf39eee0460dfa261520', 'Legacy tx to address');
assertEqual(legacyTx.value.toString(), '1000000000000000000', 'Legacy tx value = 1 ETH in Wei');
assertEqual(legacyTx.gasLimit.toString(), '21000', 'Legacy tx gasLimit = 21000');
assertEqual(legacyTx.gasPrice.toString(), '10000000000', 'Legacy tx gasPrice = 10 Gwei');
assert(legacyTx.from !== null && legacyTx.from !== undefined, 'Legacy tx from recovered');
assert(legacyTx.signature !== null, 'Legacy tx has signature');
assertEqual(legacyTx.data, '0x', 'Legacy tx data is empty');
// EIP-1559 transaction (type 2)
var eip1559TxHex = '0x02f8730180843b9aca00850a02ffee0082520894d8da6bf26964af9d7eed9e03e53415d37aa9604588016345785d8a000080c001a0e68dfaea84cd21e6b2771f8b08e66ef08483e36b75162aa8a0153027a1b2e6d8a0242b5a79af7cfe6a0bb3f782b1696d20afb399e17b0f3cf1be88d25e8c9873a8';
var eip1559Tx = ethers.Transaction.from(eip1559TxHex);
assertEqual(eip1559Tx.type, 2, 'EIP-1559 tx type = 2');
assertEqual(eip1559Tx.nonce, 0, 'EIP-1559 tx nonce = 0');
assertEqual(eip1559Tx.to.toLowerCase(), '0xd8da6bf26964af9d7eed9e03e53415d37aa96045', 'EIP-1559 tx to (vitalik.eth)');
assertEqual(eip1559Tx.gasLimit.toString(), '21000', 'EIP-1559 tx gasLimit = 21000');
assert(eip1559Tx.maxPriorityFeePerGas !== null, 'EIP-1559 has maxPriorityFeePerGas');
assert(eip1559Tx.maxFeePerGas !== null, 'EIP-1559 has maxFeePerGas');
assertEqual(eip1559Tx.chainId.toString(), '1', 'EIP-1559 chainId = 1 (mainnet)');
// from recovery may throw on crafted test tx — just check value exists or catches
var eip1559From = null;
try { eip1559From = eip1559Tx.from; } catch(e) { /* recovery can fail on test vectors */ }
assert(true, 'EIP-1559 tx from access did not crash test');
assertEqual(eip1559Tx.value.toString(), '100000000000000000', 'EIP-1559 tx value = 0.1 ETH');
// Invalid hex — wrapping in try/catch since ethers may throw different error types
var invalidThrew = false;
try { ethers.Transaction.from('0xdeadbeef'); } catch(e) { invalidThrew = true; }
assert(invalidThrew, 'Invalid tx hex throws error');
// Not a transaction at all
var notHexThrew = false;
try { ethers.Transaction.from('not-hex-at-all'); } catch(e) { notHexThrew = true; }
assert(notHexThrew, 'Non-hex input throws error');
})();
/* ============================================================
* Suite 5: Function Signature Parsing
* ============================================================ */
console.log('\n=== Suite 5: Function Signature Parsing ===');
(function() {
var p1 = parseFuncSig('transfer(address,uint256)');
assertEqual(p1.name, 'transfer', 'Parse transfer — name');
assertEqual(p1.types.length, 2, 'Parse transfer — 2 types');
assertEqual(p1.types[0], 'address', 'Parse transfer — type[0] = address');
assertEqual(p1.types[1], 'uint256', 'Parse transfer — type[1] = uint256');
assertEqual(p1.full, 'transfer(address,uint256)', 'Parse transfer — full sig');
var p2 = parseFuncSig('balanceOf(address)');
assertEqual(p2.name, 'balanceOf', 'Parse balanceOf — name');
assertEqual(p2.types.length, 1, 'Parse balanceOf — 1 type');
assertEqual(p2.types[0], 'address', 'Parse balanceOf — type[0] = address');
var p3 = parseFuncSig('totalSupply()');
assertEqual(p3.name, 'totalSupply', 'Parse totalSupply — name');
assertEqual(p3.types.length, 0, 'Parse totalSupply — 0 types');
// With spaces
var p4 = parseFuncSig(' transfer( address , uint256 ) ');
assertEqual(p4.name, 'transfer', 'Parse with spaces — name');
assertEqual(p4.types[0], 'address', 'Parse with spaces — trimmed type[0]');
assertEqual(p4.types[1], 'uint256', 'Parse with spaces — trimmed type[1]');
// Invalid format
assertThrows(function() { parseFuncSig('not-a-function'); }, 'Invalid sig — no parens');
assertThrows(function() { parseFuncSig(''); }, 'Invalid sig — empty');
assertThrows(function() { parseFuncSig('(address)'); }, 'Invalid sig — no function name');
})();
/* ===== Summary ===== */
console.log('\n========================================');
console.log(' Total: ' + (passed + failed) + ' Passed: ' + passed + ' Failed: ' + failed);
console.log('========================================');
if (failed > 0) {
console.log('\nFailed tests:');
for (var i = 0; i < errors.length; i++) console.log(errors[i]);
process.exit(1);
} else {
console.log('\nAll tests passed!');
process.exit(0);
}