-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.mjs
More file actions
94 lines (80 loc) · 3.07 KB
/
Copy pathtest.mjs
File metadata and controls
94 lines (80 loc) · 3.07 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
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const { selector, hash, generateSignatures, reverseLookupSync, parseSignature, isValidABIType } = require('./dist/abi4.cjs');
let pass = 0, fail = 0;
function assert(cond, msg) {
if (cond) { pass++; }
else { fail++; console.error(' FAIL:', msg); }
}
console.log('Testing abi4...\n');
// SHA-512/256 test vectors
assert(selector('add(uint64,uint64)uint128') === '8aa3b61f', 'add(uint64,uint64)uint128 => 8aa3b61f');
assert(typeof hash('hello') === 'string' && hash('hello').length === 64, 'hash() returns 64-char hex string');
assert(hash('') !== hash('a'), 'different inputs produce different hashes');
// hash() rejects non-ASCII
let threw = false;
try { hash('héllo'); } catch (e) { threw = true; }
assert(threw, 'hash() throws on non-ASCII input');
// parseSignature
const p = parseSignature('transfer(address,uint64)void');
assert(p.name === 'transfer', 'parseSignature name');
assert(p.args === 'address,uint64', 'parseSignature args');
assert(p.ret === 'void', 'parseSignature ret');
const p2 = parseSignature('foo((uint64,address))bool');
assert(p2.name === 'foo', 'nested tuple parse name');
assert(p2.args === '(uint64,address)', 'nested tuple parse args');
assert(p2.ret === 'bool', 'nested tuple parse ret');
// isValidABIType
assert(isValidABIType('uint64') === true, 'uint64 valid');
assert(isValidABIType('bool') === true, 'bool valid');
assert(isValidABIType('(uint64,address)') === true, 'tuple valid');
assert(isValidABIType('uint64[]') === true, 'array valid');
assert(isValidABIType('') === false, 'empty invalid');
assert(isValidABIType('FOO') === false, 'FOO invalid');
// generateSignatures
const results = generateSignatures({
names: ['add', 'sub'],
argTypes: ['uint64'],
returnTypes: ['void', 'uint64'],
minArgs: 0,
maxArgs: 1
});
assert(results.length === 8, `generateSignatures count: expected 8 got ${results.length}`);
const sel = selector('add()void');
assert(results.some(r => r.selector === sel && r.signature === 'add()void'), 'add()void present');
assert(results.some(r => r.name === 'sub'), 'sub present');
// Collision detection
const colResults = generateSignatures({
names: ['a', 'b'],
argTypes: [],
returnTypes: ['void'],
minArgs: 0,
maxArgs: 0
});
assert(colResults.length === 2, 'collision test count');
// reverseLookupSync
const matches = reverseLookupSync({
target: '8aa3b61f',
names: ['add'],
argTypes: ['uint64'],
returnTypes: ['uint128'],
minArgs: 2,
maxArgs: 2
});
assert(matches.length === 1, `reverseLookup found 1 match, got ${matches.length}`);
assert(matches[0].name === 'add', 'reverse match name');
assert(matches[0].args === 'uint64,uint64', 'reverse match args');
// reverseLookupSync with dictionary
const dictMatches = reverseLookupSync({
target: '8aa3b61f',
names: [],
dictionary: ['add'],
argTypes: ['uint64'],
returnTypes: ['uint128'],
minArgs: 2,
maxArgs: 2,
maxWords: 1
});
assert(dictMatches.length === 1, `reverseLookup dict found match`);
console.log(`\n${pass} passed, ${fail} failed`);
process.exit(fail > 0 ? 1 : 0);