diff --git a/packages/wasm-crypto/test/all/bip39.js b/packages/wasm-crypto/test/all/bip39.js index bad92ff7..4416712e 100644 --- a/packages/wasm-crypto/test/all/bip39.js +++ b/packages/wasm-crypto/test/all/bip39.js @@ -7,52 +7,50 @@ const { assert, u8aToHex } = require('@polkadot/util'); -module.exports = function (wasm) { - function bip39_generate () { - const RESULT = wasm.bip39Generate(21); +function bip39_generate (wasm) { + const RESULT = wasm.bip39Generate(21); - console.log('\tPHR', RESULT); + console.log('\tPHR', RESULT); - assert(RESULT.split(' ').length === 21, 'ERROR: Invalid bip39 Phase length'); - } + assert(RESULT.split(' ').length === 21, 'ERROR: Invalid bip39 Phase length'); +} - function bip39_validate () { - const RESULT = wasm.bip39Validate(wasm.bip39Generate(12)); +function bip39_validate (wasm) { + const RESULT = wasm.bip39Validate(wasm.bip39Generate(12)); - console.log('\tVAL', RESULT); + console.log('\tVAL', RESULT); - assert(RESULT, 'ERROR: Invalid bip39 validation'); - } + assert(RESULT, 'ERROR: Invalid bip39 validation'); +} - function bip39_toEntropy () { - const RESULT = u8aToHex(wasm.bip39ToEntropy('legal winner thank year wave sausage worth useful legal winner thank yellow')); +function bip39_toEntropy (wasm) { + const RESULT = u8aToHex(wasm.bip39ToEntropy('legal winner thank year wave sausage worth useful legal winner thank yellow')); - console.log('\tENT', RESULT); + console.log('\tENT', RESULT); - assert(RESULT === '0x7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f', 'ERROR: Invalid bip39 entropy'); - } + assert(RESULT === '0x7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f', 'ERROR: Invalid bip39 entropy'); +} - function bip39_toMiniSecret () { - const RESULT = u8aToHex(wasm.bip39ToMiniSecret('legal winner thank year wave sausage worth useful legal winner thank yellow', 'Substrate')); +function bip39_toMiniSecret (wasm) { + const RESULT = u8aToHex(wasm.bip39ToMiniSecret('legal winner thank year wave sausage worth useful legal winner thank yellow', 'Substrate')); - console.log('\tMIN', RESULT); + console.log('\tMIN', RESULT); - assert(RESULT === '0x4313249608fe8ac10fd5886c92c4579007272cb77c21551ee5b8d60b78041685', 'ERROR: Invalid bip39 mini secret'); - } + assert(RESULT === '0x4313249608fe8ac10fd5886c92c4579007272cb77c21551ee5b8d60b78041685', 'ERROR: Invalid bip39 mini secret'); +} - function bip39_toSeed () { - const RESULT = u8aToHex(wasm.bip39ToSeed('seed sock milk update focus rotate barely fade car face mechanic mercy', '')); +function bip39_toSeed (wasm) { + const RESULT = u8aToHex(wasm.bip39ToSeed('seed sock milk update focus rotate barely fade car face mechanic mercy', '')); - console.log('\tSEE', RESULT); + console.log('\tSEE', RESULT); - assert(RESULT === '0x3c121e20de068083b49c2315697fb59a2d9e8643c24e5ea7628132c58969a027', 'ERROR: Invalid bip39 mini secret'); - } + assert(RESULT === '0x3c121e20de068083b49c2315697fb59a2d9e8643c24e5ea7628132c58969a027', 'ERROR: Invalid bip39 mini secret'); +} - return { - bip39_generate, - bip39_validate, - bip39_toEntropy, - bip39_toMiniSecret, - bip39_toSeed - }; +module.exports = { + bip39_generate, + bip39_validate, + bip39_toEntropy, + bip39_toMiniSecret, + bip39_toSeed }; diff --git a/packages/wasm-crypto/test/all/ed25519.js b/packages/wasm-crypto/test/all/ed25519.js index 76d3af7c..e32a6c75 100644 --- a/packages/wasm-crypto/test/all/ed25519.js +++ b/packages/wasm-crypto/test/all/ed25519.js @@ -8,57 +8,55 @@ const crypto = require('crypto'); const { assert, hexToU8a, stringToU8a, u8aToHex } = require('@polkadot/util'); -module.exports = function (wasm) { - function extractKeys (pair) { - return [pair, pair.slice(32), pair.slice(0, 32)]; +function extractKeys (pair) { + return [pair, pair.slice(32), pair.slice(0, 32)]; +} + +function randomPair (wasm) { + return extractKeys(wasm.ed25519KeypairFromSeed(crypto.randomBytes(32))); +} + +function ed25519_pairFromSeed (wasm) { + const [pair, pk, sk] = extractKeys(wasm.ed25519KeypairFromSeed(stringToU8a('12345678901234567890123456789012'))); + + console.log('\tSEC', u8aToHex(sk)); + console.log('\tPUB', u8aToHex(pk)); + + assert(u8aToHex(pair) === '0x31323334353637383930313233343536373839303132333435363738393031322f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee', 'ERROR: pairFromSeed() does not match'); +} + +function ed25519_signAndVerify (wasm) { + const [, pk, sk] = randomPair(wasm); + const signature = wasm.ed25519Sign(pk, sk, stringToU8a('this is a message')); + const isValid = wasm.ed25519Verify(signature, stringToU8a('this is a message'), pk); + + console.log('\tSIG', u8aToHex(signature)); + console.log('\tRES', isValid); + + assert(isValid, 'ERROR: Unable to verify signature'); +} + +function ed25519_verifyExisting (wasm) { + const isValid = wasm.ed25519Verify(hexToU8a('0x90588f3f512496f2dd40571d162e8182860081c74e2085316e7c4396918f07da412ee029978e4dd714057fe973bd9e7d645148bf7b66680d67c93227cde95202'), stringToU8a('this is a message'), hexToU8a('0x2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee')); + + console.log('\tRES', isValid); + + assert(isValid, 'ERROR: Unable to verify signature'); +} + +function ed25519_benchmark (wasm) { + const MESSAGE = stringToU8a('this is a message'); + + for (let i = 0; i < 256; i++) { + const [, pk, sk] = randomPair(wasm); + + assert(wasm.ed25519Verify(wasm.ed25519Sign(pk, sk, MESSAGE), MESSAGE, pk), 'ERROR: Unable to verify signature'); } +} - function randomPair () { - return extractKeys(wasm.ed25519KeypairFromSeed(crypto.randomBytes(32))); - } - - function ed25519_pairFromSeed () { - const [pair, pk, sk] = extractKeys(wasm.ed25519KeypairFromSeed(stringToU8a('12345678901234567890123456789012'))); - - console.log('\tSEC', u8aToHex(sk)); - console.log('\tPUB', u8aToHex(pk)); - - assert(u8aToHex(pair) === '0x31323334353637383930313233343536373839303132333435363738393031322f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee', 'ERROR: pairFromSeed() does not match'); - } - - function ed25519_signAndVerify () { - const [, pk, sk] = randomPair(); - const signature = wasm.ed25519Sign(pk, sk, stringToU8a('this is a message')); - const isValid = wasm.ed25519Verify(signature, stringToU8a('this is a message'), pk); - - console.log('\tSIG', u8aToHex(signature)); - console.log('\tRES', isValid); - - assert(isValid, 'ERROR: Unable to verify signature'); - } - - function ed25519_verifyExisting () { - const isValid = wasm.ed25519Verify(hexToU8a('0x90588f3f512496f2dd40571d162e8182860081c74e2085316e7c4396918f07da412ee029978e4dd714057fe973bd9e7d645148bf7b66680d67c93227cde95202'), stringToU8a('this is a message'), hexToU8a('0x2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee')); - - console.log('\tRES', isValid); - - assert(isValid, 'ERROR: Unable to verify signature'); - } - - function ed25519_benchmark () { - const MESSAGE = stringToU8a('this is a message'); - - for (let i = 0; i < 256; i++) { - const [, pk, sk] = randomPair(); - - assert(wasm.ed25519Verify(wasm.ed25519Sign(pk, sk, MESSAGE), MESSAGE, pk), 'ERROR: Unable to verify signature'); - } - } - - return { - ed25519_pairFromSeed, - ed25519_signAndVerify, - ed25519_verifyExisting, - ed25519_benchmark - }; +module.exports = { + ed25519_pairFromSeed, + ed25519_signAndVerify, + ed25519_verifyExisting, + ed25519_benchmark }; diff --git a/packages/wasm-crypto/test/all/hashing.js b/packages/wasm-crypto/test/all/hashing.js index 9355fcfb..17e8f869 100644 --- a/packages/wasm-crypto/test/all/hashing.js +++ b/packages/wasm-crypto/test/all/hashing.js @@ -7,66 +7,64 @@ const { assert, stringToU8a, u8aToHex } = require('@polkadot/util'); -module.exports = function (wasm) { - function blake2bHash () { - const hash = u8aToHex(wasm.blake2b(stringToU8a('abc'), new Uint8Array(), 64)); +function blake2bHash (wasm) { + const hash = u8aToHex(wasm.blake2b(stringToU8a('abc'), new Uint8Array(), 64)); - console.log('\tRES', hash); + console.log('\tRES', hash); - assert(hash === '0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923', 'ERROR: blake2bHash does not match'); - } + assert(hash === '0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923', 'ERROR: blake2bHash does not match'); +} - function keccak256 () { - const hash = u8aToHex(wasm.keccak256(stringToU8a('test value'))); +function keccak256 (wasm) { + const hash = u8aToHex(wasm.keccak256(stringToU8a('test value'))); - console.log('\tRES', hash); + console.log('\tRES', hash); - assert(hash === '0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e', 'ERROR: keccak256 does not match'); - } + assert(hash === '0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e', 'ERROR: keccak256 does not match'); +} - function pbkdf2Hash () { - const hash = u8aToHex(wasm.pbkdf2(stringToU8a('hello world'), stringToU8a('this is a salt'), 2048)); +function pbkdf2Hash (wasm) { + const hash = u8aToHex(wasm.pbkdf2(stringToU8a('hello world'), stringToU8a('this is a salt'), 2048)); - console.log('\tRES', hash); + console.log('\tRES', hash); - assert(hash === '0x5fcbe04f05300a3ecc5c35d18ea0b78f3f6853d2ae5f3fca374f69a7d1f78b5def5c60dae1a568026c7492511e0c53521e8bb6e03a650e1263265fee92722270', 'ERROR: pbkdf2Hash does not match'); - } + assert(hash === '0x5fcbe04f05300a3ecc5c35d18ea0b78f3f6853d2ae5f3fca374f69a7d1f78b5def5c60dae1a568026c7492511e0c53521e8bb6e03a650e1263265fee92722270', 'ERROR: pbkdf2Hash does not match'); +} - // function secp256k1Recover () { - // const pk = u8aToHex(wasm.keccak256(wasm.secp256k1Recover( - // hexToU8a('0x2bbfa926ee947dfac384d981f8913b4e0992e52d7b6711e0ec3d9f8b0f752648'), - // hexToU8a('0x7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff1c') - // ))); +// function secp256k1Recover (wasm) { +// const pk = u8aToHex(wasm.keccak256(wasm.secp256k1Recover( +// hexToU8a('0x2bbfa926ee947dfac384d981f8913b4e0992e52d7b6711e0ec3d9f8b0f752648'), +// hexToU8a('0x7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff1c') +// ))); - // console.log('\tPUB', pk); +// console.log('\tPUB', pk); - // assert(pk === '0x91d366769d44a7c7690428bfdf67ec7eae23d2459694685257b6fc59d1baa1fe', 'ERROR: secp256k1Recover does not match'); - // } +// assert(pk === '0x91d366769d44a7c7690428bfdf67ec7eae23d2459694685257b6fc59d1baa1fe', 'ERROR: secp256k1Recover does not match'); +// } - function sha512Hash () { - const hash = u8aToHex(wasm.sha512(stringToU8a('hello world'))); +function sha512Hash (wasm) { + const hash = u8aToHex(wasm.sha512(stringToU8a('hello world'))); - console.log('\tRES', hash); + console.log('\tRES', hash); - assert(hash === '0x309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f', 'ERROR: sha512Hash does not match'); - } + assert(hash === '0x309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f', 'ERROR: sha512Hash does not match'); +} - function twoxHash () { - const hash64 = u8aToHex(wasm.twox(stringToU8a('abc'), 1)); - const hash256 = u8aToHex(wasm.twox(stringToU8a('abc'), 4)); +function twoxHash (wasm) { + const hash64 = u8aToHex(wasm.twox(stringToU8a('abc'), 1)); + const hash256 = u8aToHex(wasm.twox(stringToU8a('abc'), 4)); - console.log('\t64 ', hash64); - console.log('\t256', hash256); + console.log('\t64 ', hash64); + console.log('\t256', hash256); - assert(hash64 === '0x990977adf52cbc44' && hash256 === '0x990977adf52cbc440889329981caa9bef7da5770b2b8a05303b75d95360dd62b', 'ERROR: twoxHash does not match'); - } + assert(hash64 === '0x990977adf52cbc44' && hash256 === '0x990977adf52cbc440889329981caa9bef7da5770b2b8a05303b75d95360dd62b', 'ERROR: twoxHash does not match'); +} - return { - blake2bHash, - keccak256, - pbkdf2Hash, - // secp256k1Recover, - sha512Hash, - twoxHash - }; +module.exports = { + blake2bHash, + keccak256, + pbkdf2Hash, + // secp256k1Recover, + sha512Hash, + twoxHash }; diff --git a/packages/wasm-crypto/test/all/index.js b/packages/wasm-crypto/test/all/index.js index cedbcc36..c15c7d4b 100644 --- a/packages/wasm-crypto/test/all/index.js +++ b/packages/wasm-crypto/test/all/index.js @@ -4,48 +4,66 @@ // of the Apache-2.0 license. See the LICENSE file for details. // @ts-check +// @ts-ignore +const wasm = require('../../build/index'); const bip39 = require('./bip39'); const ed25519 = require('./ed25519'); const hashing = require('./hashing'); const sr25519 = require('./sr25519'); -module.exports = function (wasm) { - const tests = { - ...bip39(wasm), - ...ed25519(wasm), - ...hashing(wasm), - ...sr25519(wasm) - }; - - return { - beforeAll: async function beforeAll () { - return wasm.waitReady(); - }, - runAll: function runAll () { - const failed = []; - let count = 0; - - Object.keys(tests).forEach((name) => { - count++; - - try { - console.time(name); - console.log(); - console.log(name); - - tests[name](); - - console.timeEnd(name); - } catch (error) { - console.error(error); - failed.push(name); - } - }); - - if (failed.length) { - throw new Error(`Failed: ${failed.length} of ${count}: ${failed.concat(', ')}`); - } - }, - tests - }; +const tests = { + ...bip39, + ...ed25519, + ...hashing, + ...sr25519 +}; + +async function beforeAll () { + return wasm.waitReady(); +} + +function runAll () { + const failed = []; + let count = 0; + + Object.keys(tests).forEach((name) => { + count++; + + try { + console.time(name); + console.log(); + console.log(name); + + tests[name](wasm); + + console.timeEnd(name); + } catch (error) { + console.error(error); + failed.push(name); + } + }); + + if (failed.length) { + throw new Error(`Failed: ${failed.length} of ${count}: ${failed.concat(', ')}`); + } +} + +function runUnassisted () { + (async () => { + await beforeAll(); + + runAll(); + })().catch((error) => { + console.error(error); + + process.exit(-1); + }); +} + +module.exports = { + beforeAll, + runAll, + runUnassisted, + tests, + wasm }; diff --git a/packages/wasm-crypto/test/all/sr25519.js b/packages/wasm-crypto/test/all/sr25519.js index c099a777..f1bca0ec 100644 --- a/packages/wasm-crypto/test/all/sr25519.js +++ b/packages/wasm-crypto/test/all/sr25519.js @@ -8,120 +8,118 @@ const crypto = require('crypto'); const { assert, hexToU8a, stringToU8a, u8aToHex } = require('@polkadot/util'); -module.exports = function (wasm) { - function extractKeys (pair) { - return [pair, pair.slice(64), pair.slice(0, 64)]; +function extractKeys (pair) { + return [pair, pair.slice(64), pair.slice(0, 64)]; +} + +function randomPair (wasm) { + return extractKeys(wasm.sr25519KeypairFromSeed(crypto.randomBytes(32))); +} + +function sr25519_pairFromSeed (wasm) { + const pair = wasm.sr25519KeypairFromSeed(stringToU8a('12345678901234567890123456789012')); + + console.log('\tSEC', u8aToHex(pair.slice(0, 64))); + console.log('\tPUB', u8aToHex(pair.slice(64))); + + assert(u8aToHex(pair) === '0xf0106660c3dda23f16daa9ac5b811b963077f5bc0af89f85804f0de8e424f050f98d66f39442506ff947fd911f18c7a7a5da639a63e8d3b4e233f74143d951c1741c08a06f41c596608f6774259bd9043304adfa5d3eea62760bd9be97634d63', 'ERROR: pairFromSeed() does not match'); +} + +function sr25519_devFromSeed (wasm) { + const pair = wasm.sr25519KeypairFromSeed(hexToU8a('0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e')); + + console.log('\tSEC', u8aToHex(pair.slice(0, 64))); + console.log('\tPUB', u8aToHex(pair.slice(64))); + + assert(u8aToHex(pair) === '0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a', 'ERROR: devFromSeed() does not match'); +} + +function sr25519_verifyExisting (wasm) { + const PK = hexToU8a('0xb4bfa1f7a5166695eb75299fd1c4c03ea212871c342f2c5dfea0902b2c246918'); + const MESSAGE = stringToU8a('Verifying that I am the owner of 5G9hQLdsKQswNPgB499DeA5PkFBbgkLPJWkkS6FAM6xGQ8xD. Hash: 221455a3\n'); + const SIGNATURE = hexToU8a('0x5a9755f069939f45d96aaf125cf5ce7ba1db998686f87f2fb3cbdea922078741a73891ba265f70c31436e18a9acd14d189d73c12317ab6c313285cd938453202'); + + const isValid = wasm.sr25519Verify(SIGNATURE, MESSAGE, PK); + + console.log('\tRES', isValid); + + assert(isValid, 'ERROR: Unable to verify signature'); +} + +function sr25519_signAndVerify (wasm) { + const [, pk, sk] = randomPair(wasm); + const signature = wasm.sr25519Sign(pk, sk, stringToU8a('this is a message')); + const isValid = wasm.sr25519Verify(signature, stringToU8a('this is a message'), pk); + + console.log('\tSIG', u8aToHex(signature)); + console.log('\tRES', isValid); + + assert(isValid, 'ERROR: Unable to verify signature'); +} + +function sr25519_deriveHard (wasm) { + const [pair] = randomPair(wasm); + const derived = wasm.sr25519DeriveKeypairHard(pair, hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000')); + + console.log('\tSEC', u8aToHex(derived.slice(0, 64))); + console.log('\tPUB', u8aToHex(derived.slice(64))); +} + +function sr25519_deriveHardKnown (wasm) { + const derived = wasm.sr25519DeriveKeypairHard(hexToU8a('0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x14416c6963650000000000000000000000000000000000000000000000000000')); + const publicKey = u8aToHex(derived.slice(64)); + + console.log('\tSEC', u8aToHex(derived.slice(0, 64))); + console.log('\tPUB', publicKey); + + assert(publicKey === '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', 'Unmatched resulting public keys'); +} + +function sr25519_deriveSoft (wasm) { + const [pair] = randomPair(wasm); + const derived = wasm.sr25519DeriveKeypairSoft(pair, hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000')); + + console.log('\tSEC', u8aToHex(derived.slice(0, 64))); + console.log('\tPUB', u8aToHex(derived.slice(64))); +} + +function sr25519_deriveSoftKnown (wasm) { + const derived = wasm.sr25519DeriveKeypairSoft(hexToU8a('0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000')); + const publicKey = u8aToHex(derived.slice(64)); + + console.log('\tSEC', u8aToHex(derived.slice(0, 64))); + console.log('\tPUB', publicKey); + + assert(publicKey === '0x40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a', 'Unmatched resulting public keys'); +} + +function sr25519_deriveSoftPubkey (wasm) { + const derived = u8aToHex(wasm.sr25519DerivePublicSoft(hexToU8a('0x46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000'))); + + console.log('\tPUB', derived); + + assert(derived === '0x40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a', 'Unmatched resulting public keys'); +} + +function sr25519_benchmark (wasm) { + const MESSAGE = stringToU8a('this is a message'); + + for (let i = 0; i < 256; i++) { + const [, pk, sk] = randomPair(wasm); + + assert(wasm.sr25519Verify(wasm.sr25519Sign(pk, sk, MESSAGE), MESSAGE, pk), 'ERROR: Unable to verify signature'); } +} - function randomPair () { - return extractKeys(wasm.sr25519KeypairFromSeed(crypto.randomBytes(32))); - } - - function sr25519_pairFromSeed () { - const pair = wasm.sr25519KeypairFromSeed(stringToU8a('12345678901234567890123456789012')); - - console.log('\tSEC', u8aToHex(pair.slice(0, 64))); - console.log('\tPUB', u8aToHex(pair.slice(64))); - - assert(u8aToHex(pair) === '0xf0106660c3dda23f16daa9ac5b811b963077f5bc0af89f85804f0de8e424f050f98d66f39442506ff947fd911f18c7a7a5da639a63e8d3b4e233f74143d951c1741c08a06f41c596608f6774259bd9043304adfa5d3eea62760bd9be97634d63', 'ERROR: pairFromSeed() does not match'); - } - - function sr25519_devFromSeed () { - const pair = wasm.sr25519KeypairFromSeed(hexToU8a('0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e')); - - console.log('\tSEC', u8aToHex(pair.slice(0, 64))); - console.log('\tPUB', u8aToHex(pair.slice(64))); - - assert(u8aToHex(pair) === '0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a', 'ERROR: devFromSeed() does not match'); - } - - function sr25519_verifyExisting () { - const PK = hexToU8a('0xb4bfa1f7a5166695eb75299fd1c4c03ea212871c342f2c5dfea0902b2c246918'); - const MESSAGE = stringToU8a('Verifying that I am the owner of 5G9hQLdsKQswNPgB499DeA5PkFBbgkLPJWkkS6FAM6xGQ8xD. Hash: 221455a3\n'); - const SIGNATURE = hexToU8a('0x5a9755f069939f45d96aaf125cf5ce7ba1db998686f87f2fb3cbdea922078741a73891ba265f70c31436e18a9acd14d189d73c12317ab6c313285cd938453202'); - - const isValid = wasm.sr25519Verify(SIGNATURE, MESSAGE, PK); - - console.log('\tRES', isValid); - - assert(isValid, 'ERROR: Unable to verify signature'); - } - - function sr25519_signAndVerify () { - const [, pk, sk] = randomPair(); - const signature = wasm.sr25519Sign(pk, sk, stringToU8a('this is a message')); - const isValid = wasm.sr25519Verify(signature, stringToU8a('this is a message'), pk); - - console.log('\tSIG', u8aToHex(signature)); - console.log('\tRES', isValid); - - assert(isValid, 'ERROR: Unable to verify signature'); - } - - function sr25519_deriveHard () { - const [pair] = randomPair(); - const derived = wasm.sr25519DeriveKeypairHard(pair, hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000')); - - console.log('\tSEC', u8aToHex(derived.slice(0, 64))); - console.log('\tPUB', u8aToHex(derived.slice(64))); - } - - function sr25519_deriveHardKnown () { - const derived = wasm.sr25519DeriveKeypairHard(hexToU8a('0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x14416c6963650000000000000000000000000000000000000000000000000000')); - const publicKey = u8aToHex(derived.slice(64)); - - console.log('\tSEC', u8aToHex(derived.slice(0, 64))); - console.log('\tPUB', publicKey); - - assert(publicKey === '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', 'Unmatched resulting public keys'); - } - - function sr25519_deriveSoft () { - const [pair] = randomPair(); - const derived = wasm.sr25519DeriveKeypairSoft(pair, hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000')); - - console.log('\tSEC', u8aToHex(derived.slice(0, 64))); - console.log('\tPUB', u8aToHex(derived.slice(64))); - } - - function sr25519_deriveSoftKnown () { - const derived = wasm.sr25519DeriveKeypairSoft(hexToU8a('0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000')); - const publicKey = u8aToHex(derived.slice(64)); - - console.log('\tSEC', u8aToHex(derived.slice(0, 64))); - console.log('\tPUB', publicKey); - - assert(publicKey === '0x40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a', 'Unmatched resulting public keys'); - } - - function sr25519_deriveSoftPubkey () { - const derived = u8aToHex(wasm.sr25519DerivePublicSoft(hexToU8a('0x46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000'))); - - console.log('\tPUB', derived); - - assert(derived === '0x40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a', 'Unmatched resulting public keys'); - } - - function sr25519_benchmark () { - const MESSAGE = stringToU8a('this is a message'); - - for (let i = 0; i < 256; i++) { - const [, pk, sk] = randomPair(); - - assert(wasm.sr25519Verify(wasm.sr25519Sign(pk, sk, MESSAGE), MESSAGE, pk), 'ERROR: Unable to verify signature'); - } - } - - return { - sr25519_pairFromSeed, - sr25519_devFromSeed, - sr25519_deriveHard, - sr25519_deriveHardKnown, - sr25519_deriveSoft, - sr25519_deriveSoftKnown, - sr25519_deriveSoftPubkey, - sr25519_signAndVerify, - sr25519_verifyExisting, - sr25519_benchmark - }; +module.exports = { + sr25519_pairFromSeed, + sr25519_devFromSeed, + sr25519_deriveHard, + sr25519_deriveHardKnown, + sr25519_deriveSoft, + sr25519_deriveSoftKnown, + sr25519_deriveSoftPubkey, + sr25519_signAndVerify, + sr25519_verifyExisting, + sr25519_benchmark }; diff --git a/packages/wasm-crypto/test/asm.js b/packages/wasm-crypto/test/asm.js index be43b959..3bd9a4ff 100644 --- a/packages/wasm-crypto/test/asm.js +++ b/packages/wasm-crypto/test/asm.js @@ -13,16 +13,6 @@ require('override-require')( () => require('../build/wasm_asm') ); -// @ts-ignore -const wasm = require('../build/index'); -const { beforeAll, runAll } = require('./all')(wasm); +const { runUnassisted } = require('./all'); -(async () => { - await beforeAll(); - - runAll(); -})().catch((error) => { - console.error(error); - - process.exit(-1); -}); +runUnassisted(); diff --git a/packages/wasm-crypto/test/jest.spec.js b/packages/wasm-crypto/test/jest.spec.js index 88844628..ba6396a2 100644 --- a/packages/wasm-crypto/test/jest.spec.js +++ b/packages/wasm-crypto/test/jest.spec.js @@ -8,13 +8,12 @@ require('../build/crypto-polyfill'); // @ts-ignore -const wasm = require('../build/index'); -const { beforeAll, tests } = require('./all')(wasm); +const { beforeAll, tests, wasm } = require('./all'); describe('schnorrkel-js', () => { - beforeEach(beforeAll); + beforeEach(async () => beforeAll()); Object.keys(tests).forEach((name) => { - it(name, tests[name]); + it(name, () => tests[name](wasm)); }); }); diff --git a/packages/wasm-crypto/test/wasm.js b/packages/wasm-crypto/test/wasm.js index 3ddb6d1f..b4e020e6 100644 --- a/packages/wasm-crypto/test/wasm.js +++ b/packages/wasm-crypto/test/wasm.js @@ -4,16 +4,6 @@ // of the Apache-2.0 license. See the LICENSE file for details. // @ts-check -// @ts-ignore -const wasm = require('../build/index'); -const { beforeAll, runAll } = require('./all')(wasm); +const { runUnassisted } = require('./all'); -(async () => { - await beforeAll(); - - runAll(); -})().catch((error) => { - console.error(error); - - process.exit(-1); -}); +runUnassisted();