-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
77 lines (65 loc) · 3.1 KB
/
tests.js
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
// Require our modules
var ZNZ = require('./index.js');
function verifyPubkeyWithCatch(strPubkey = "") {
try {
return ZNZ.wallet.verifyPubkey(strPubkey);
} catch (e) {
return e.message;
}
}
async function tests() {
/* WALLET TESTS */
// Wallet generation
console.log("Test 1 --- Wallet Generation");
let nStartTime = Date.now();
const cWallet = await ZNZ.wallet.generateWallet();
console.log("Priv: " + cWallet.privkey);
console.log("Pub: " + cWallet.pubkey);
console.log("Test 1 --- End (took " + (Date.now() - nStartTime) + " ms)\n\n");
// Public Derivation
console.log("Test 2 --- Derive 10 Pubkeys from WIF Privkey");
nStartTime = Date.now();
for (let i=0; i<10; ++i) {
const nKeyStartTime = Date.now();
const strPubkey = ZNZ.wallet.pubFromPriv(cWallet.privkey);
console.log("Pub: " + strPubkey + ", took " + (Date.now() - nKeyStartTime) + " ms");
}
console.log("Test 2 --- End (took " + (Date.now() - nStartTime) + " ms)\n\n");
// Signature Creation
console.log('Test 3 --- Create a signature using our test wallet');
nStartTime = Date.now();
const cSig = await ZNZ.signer.sign('test', cWallet.privkey);
console.log('Sig: ' + cSig.toString('base64'));
console.log('Test 3 --- End (took ' + (Date.now() - nStartTime) + ' ms)\n\n');
// Signature Verification
console.log('Test 4 --- Verify the signature using only the sig, content and pubkey');
nStartTime = Date.now();
const fSigVerif = await ZNZ.signer.verify('test', cWallet.pubkey, cSig);
console.log('Verification: ' + (fSigVerif ? 'Success!' : 'Failed!'));
console.log('Test 4 --- End (took ' + (Date.now() - nStartTime) + ' ms)\n\n');
// Address verification
console.log('Test 5 --- Verify the address of our test wallet');
nStartTime = Date.now();
console.log('Verify Address: ' + (verifyPubkeyWithCatch(cWallet.pubkey) ? 'Valid!' : 'Invalid!'));
console.log('Test 5 --- End (took ' + (Date.now() - nStartTime) + ' ms)\n\n');
// Address verification with regression / integrity testing
console.log('Test 6 --- verifyPubkey() regression & integrity testing');
nStartTime = Date.now();
const arrTestAddresses = [
"sYbmHt8EP8YacjFGahjhqXT8GNeSiTjbRs", // VALID
"sYbmHt8EP8YacjFGahjhqXT8GNeSiTjbRs", // VALID
"sYbmHt8EP8YacjFGahjhqXT8GNeSiTjbRs", // VALID
"sAbmHt8EP8YacjFGahjhqXT8GNeSiTjbRs", // BAD
"sBbmHt8EP8YacjFGahjhqXT8GNeSiTjbRs", // BAD
"sY mHt8EP8YacjFGahjhqXT8GNeSiTjbRs", // BAD
"sYbmHt", // BAD
"i55j", // BAD
"sYbmHt8EP8YacjFGahjhqXT8GNeSiTjbR!", // BAD
"sYbmHt8EP8YacjFGahjhqXT8GNeSiTjbRiz", // BAD
"sYbmHt8EP8YacjFGahjhqXT8GNeSiTjbRizz", // BAD
"sYbmHt8EP8YacjFGahjhqXT8GNeSiTjbRz" // BAD
];
arrTestAddresses.forEach(strAddr => console.log('Verify %s: %s', strAddr.padEnd(36), verifyPubkeyWithCatch(strAddr)));
console.log('Test 6 --- End (took ' + (Date.now() - nStartTime) + ' ms)\n\n');
}
tests();