Compare commits

...
17 Commits
Author SHA1 Message Date
Travis CI 49968708d3 [CI Skip] 0.14.1 2019-09-09 10:37:39 +00:00
Jaco Greeff 340ccd18d2 0.14 (#35) 2019-09-09 12:23:16 +02:00
Travis CI 173fca3a81 [CI Skip] 0.14.0-beta.4 2019-09-05 09:40:23 +00:00
Jaco Greeff 24af3cf2ce Bump deps (#34) 2019-09-05 11:25:57 +02:00
Travis CI c1186a3743 [CI Skip] 0.14.0-beta.3 2019-08-30 10:26:03 +00:00
Jaco Greeff bd18b42439 typescript-eslint 2 (#33)
* typescript-eslint 2

* Blind eslint fixes
2019-08-30 12:11:20 +02:00
Travis CI d047be77b3 [CI Skip] 0.14.0-beta.2 2019-08-29 13:58:42 +00:00
Jaco Greeff e467308ac1 Bump deps (#32) 2019-08-29 15:43:41 +02:00
Travis CI 4dbe2214a9 [CI Skip] 0.14.0-beta.1 2019-08-27 09:55:10 +00:00
Jaco Greeff 2844c15911 Bump dev (#31) 2019-08-27 11:41:06 +02:00
Jaco Greeff 419630da5f Maintainability indicator 2019-08-12 09:09:48 +02:00
Jaco Greeff 0e9f04fd33 Cleanup README 2019-08-11 08:06:41 +02:00
Jaco Greeff 2ecb1d209f Update README.md 2019-08-10 15:15:02 +02:00
Jaco Greeff d282dbd54c README badges 2019-08-10 14:23:28 +02:00
Travis CI e5680e8823 [CI Skip] 0.14.0-beta.0 2019-07-31 12:29:22 +00:00
Jaco Greeff c1c27d7a7d Test cleanups, exports params (#30) 2019-07-31 14:10:52 +02:00
Jaco Greeff 44c62a7b3d Update README.md 2019-07-20 12:37:38 +02:00
17 changed files with 2076 additions and 2202 deletions
+11 -1
View File
@@ -1 +1,11 @@
module.exports = require('@polkadot/dev/config/eslint');
const base = require('@polkadot/dev/config/eslint');
module.exports = {
...base,
parserOptions: {
...base.parserOptions,
project: [
'./tsconfig.eslint.json'
]
}
};
+4
View File
@@ -1,3 +1,7 @@
# 0.14.1
- No functionality changes at all, everything done is "under the hood" to give the codebase better maintainability
# 0.13.1
- Added an asm.js fallback which is active for React Native via the `"react-native"` entry in `package.json`. Ensure that you run the build step with the `NODE_OPTIONS=--max_old_space_size=8192` flags, since the asm.js bundle is large. (For RN the full command would therefore be `NODE_OPTIONS=--max_old_space_size=8192 npm start`)
+5 -6
View File
@@ -1,9 +1,8 @@
[![polkadotjs](https://img.shields.io/badge/polkadot-js-orange.svg?style=flat-square)](https://polkadot.js.org)
![license](https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square)
[![style](https://img.shields.io/badge/code%20style-semistandard-lightgrey.svg?style=flat-square)](https://github.com/Flet/semistandard)
[![travis](https://img.shields.io/travis/polkadot-js/wasm.svg?style=flat-square)](https://travis-ci.com/polkadot-js/wasm)
[![maintainability](https://img.shields.io/codeclimate/maintainability/polkadot-js/wasm.svg?style=flat-square)](https://codeclimate.com/github/polkadot-js/wasm/maintainability)
[![greenkeeper](https://img.shields.io/badge/greenkeeper-enabled-brightgreen.svg?style=flat-square)](https://greenkeeper.io/)
[![polkadotjs](https://img.shields.io/badge/polkadot-js-orange?style=flat-square)](https://polkadot.js.org)
![license](https://img.shields.io/badge/License-Apache%202.0-blue?logo=apache&style=flat-square)
[![npm](https://img.shields.io/npm/v/@polkadot/wasm-crypto?logo=npm&style=flat-square)](https://www.npmjs.com/package/@polkadot/wasm-crypto)
[![beta](https://img.shields.io/npm/v/@polkadot/wasm-crypto/beta?label=beta&logo=npm&&style=flat-square)](https://www.npmjs.com/package/@polkadot/wasm-crypto)
[![maintainability](https://img.shields.io/codeclimate/maintainability-percentage/polkadot-js/wasm?logo=code-climate&style=flat-square)](https://codeclimate.com/github/polkadot-js/wasm/maintainability)
# @polkadot/wasm
+1 -1
View File
@@ -9,5 +9,5 @@
"packages": [
"packages/*"
],
"version": "0.13.1"
"version": "0.14.1"
}
+5 -5
View File
@@ -1,5 +1,5 @@
{
"version": "0.13.1",
"version": "0.14.1",
"private": true,
"engines": {
"yarn": "^1.10.1"
@@ -9,7 +9,7 @@
],
"resolutions": {
"babel-core": "^7.0.0-bridge.0",
"typescript": "^3.5.3"
"typescript": "^3.6.2"
},
"scripts": {
"build": "./scripts/build.sh",
@@ -20,9 +20,9 @@
"test": "echo \"Tests included in build\""
},
"devDependencies": {
"@babel/core": "^7.5.4",
"@polkadot/dev": "^0.30.0-beta.23",
"@polkadot/util": "^0.94.0-beta.19",
"@babel/core": "^7.5.5",
"@polkadot/dev": "^0.31.0-beta.7",
"@polkadot/util": "^1.2.0-beta.7",
"override-require": "^1.1.1"
}
}
+1 -1
View File
@@ -19,7 +19,7 @@ async function main () {
await waitReady();
// generate phrase
const phrase = bip39Generate();
const phrase = bip39Generate(12);
// get ed25519 seed from phrase
const seed = bip39ToSeed(phrase, '');
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/wasm-crypto",
"version": "0.13.1",
"version": "0.14.1",
"author": "Jaco Greeff <jacogr@gmail.com>",
"files": [
"crypto-polyfill.js",
+31 -33
View File
@@ -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
};
+49 -51
View File
@@ -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
};
+42 -44
View File
@@ -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
};
+56 -40
View File
@@ -2,50 +2,66 @@
// Copyright 2019 @polkadot/wasm-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
// @ts-check
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
};
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
};
+112 -114
View File
@@ -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
};
+2 -15
View File
@@ -2,27 +2,14 @@
// Copyright 2019 @polkadot/wasm-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
// @ts-check
// @ts-ignore
global.WebAssembly = null;
require('override-require')(
(request) => request === './wasm_asm_stub',
// @ts-ignore
() => 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();
+5 -7
View File
@@ -2,19 +2,17 @@
// Copyright 2019 @polkadot/wasm-crypto authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
// @ts-check
// @ts-ignore
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 () => {
await beforeAll();
});
Object.keys(tests).forEach((name) => {
it(name, tests[name]);
it(name, () => tests[name](wasm));
});
});
+2 -12
View File
@@ -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();
+11
View File
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"exclude": [
"build",
"packages/*/build"
],
"compilerOptions": {
"allowJs": true,
"checkJs": true
}
}
+1738 -1871
View File
File diff suppressed because it is too large Load Diff