diff --git a/.travis.yml b/.travis.yml index 0a1e6717..742501c4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ +dist: xenial language: node_js node_js: - "10" @@ -9,12 +10,15 @@ cache: cargo: true yarn: true directories: + - binaryen/bin/ - node_modules compiler: - clang before_install: - - curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain=nightly -y + - curl -sSf https://sh.rustup.rs | sh -s -- --default-toolchain=nightly -y - curl -o- -L https://yarnpkg.com/install.sh | bash + # - rm -rf binaryen + # - mkdir -p binaryen/bin && curl -o- -L https://github.com/WebAssembly/binaryen/releases/download/version_73/binaryen-version_73-x86-linux.tar.gz | tar -xz -C binaryen/bin --strip-components=1 - export PATH=$HOME/.yarn/bin:$HOME/.cargo/bin:$(yarn global bin):$PATH install: - yarn diff --git a/CHANGELOG.md b/CHANGELOG.md index 20ee5893..c5287554 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,14 @@ +# 0.3.1 + +- Add wasm-dalek-ed25519 +- Add keccak256 +- Try secp256k1 (bloating code, not used atm) + +# 0.2.1 + +- Add basic crypto functions, creation of wasm-crypto package +- blake2, bip39, sha2, xxhash + # 0.1.1 - Initial release diff --git a/package.json b/package.json index 4e5c1e67..d7e322b0 100644 --- a/package.json +++ b/package.json @@ -15,13 +15,13 @@ "scripts": { "build": "./scripts/build.sh", "check": "tslint --project . && tsc --noEmit --pretty", - "clean": "polkadot-dev-clean-build", + "clean": "./scripts/clean.sh", "postinstall": "polkadot-dev-yarn-only", "test": "echo \"Tests included in build\"" }, "devDependencies": { "@babel/core": "^7.4.0", "@polkadot/dev": "^0.28.1", - "@polkadot/util": "^0.39.0-beta.2" + "@polkadot/util": "^0.40.0-beta.1" } } diff --git a/packages/wasm-crypto/Cargo.lock b/packages/wasm-crypto/Cargo.lock index 91861e9a..69bb5a53 100644 --- a/packages/wasm-crypto/Cargo.lock +++ b/packages/wasm-crypto/Cargo.lock @@ -106,6 +106,11 @@ name = "constant_time_eq" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "crunchy" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "crypto-mac" version = "0.7.0" @@ -496,6 +501,14 @@ dependencies = [ "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tiny-keccak" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "twox-hash" version = "1.1.2" @@ -539,6 +552,7 @@ dependencies = [ "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "tiny-bip39 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "twox-hash 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "wasm-bindgen 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)", "wee_alloc 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -639,6 +653,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "11d43355396e872eefb45ce6342e4374ed7bc2b3a502d1b28e36d6e23c05d1f4" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e" +"checksum crunchy 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a2f4a431c5c9f662e1200b7c7f02c34e91361150e382089a8f2dec3ba680cbda" "checksum crypto-mac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4434400df11d95d556bac068ddfedd482915eb18fe8bea89bc80b6e4b1c179e5" "checksum digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05f47366984d3ad862010e22c7ce81a7dbcaebbdfb37241a620f8b6596ee135c" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" @@ -687,6 +702,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)" = "1825685f977249735d510a242a6727b46efe914bb67e38d30c071b1b72b1d5c2" "checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015" "checksum tiny-bip39 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f5388a470627f97a01a6e13389ced797a42b1611f9de7e0f6ca705675ac55297" +"checksum tiny-keccak 1.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e9175261fbdb60781fcd388a4d6cc7e14764a2b629a7ad94abb439aed223a44f" "checksum twox-hash 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "555cd4909480122bbbf21e34faac4cb08a171f324775670447ed116726c474af" "checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" diff --git a/packages/wasm-crypto/Cargo.toml b/packages/wasm-crypto/Cargo.toml index cc9f9784..21d821e2 100644 --- a/packages/wasm-crypto/Cargo.toml +++ b/packages/wasm-crypto/Cargo.toml @@ -3,9 +3,12 @@ [package] authors = ["Jaco Greeff "] +description = "WASM bindings to Rust crypto libaries." edition = "2018" +license = "Apache-2.0" name = "wasm" publish = false +repository = "https://github.com/polkadot-js/wasm/" version = "0.0.0" [lib] @@ -15,9 +18,11 @@ crate-type = ["cdylib", "rlib"] blake2-rfc = "0.2.18" byteorder = "1.3.1" hmac = "0.7.0" +# libsecp256k1 = "0.2.2" pbkdf2 = { version = "0.3.0", default-features = false } sha2 = "0.8.0" tiny-bip39 = { version = "0.6.1", default-features = false } +tiny-keccak = "1.4.2" twox-hash = "1.1.2" wasm-bindgen = "0.2" wee_alloc = "0.4.3" diff --git a/packages/wasm-crypto/src/js/index.d.ts b/packages/wasm-crypto/src/js/index.d.ts index 9eb970a7..8581735a 100644 --- a/packages/wasm-crypto/src/js/index.d.ts +++ b/packages/wasm-crypto/src/js/index.d.ts @@ -7,7 +7,10 @@ export function bip39ToEntropy (phrase: string): Uint8Array; export function bip39ToMiniSecret (phrase: string, password: string): Uint8Array; export function bip39Validate (phrase: string): boolean; export function blake2b (data: Uint8Array, key: Uint8Array, byteSize: number): Uint8Array; +export function keccak256 (data: Uint8Array): Uint8Array; export function pbkdf2 (data: Uint8Array, salt: Uint8Array, rounds: number): Uint8Array; +// export function secp256k1IsRecoverable (message: Uint8Array, signature: Uint8Array): number; +// export function secp256k1Recover (message: Uint8Array, signature: Uint8Array): Uint8Array; export function sha512 (data: Uint8Array): Uint8Array; export function twox (data: Uint8Array, rounds: number): Uint8Array; diff --git a/packages/wasm-crypto/src/js/index.js b/packages/wasm-crypto/src/js/index.js index 09a42647..f586d73f 100644 --- a/packages/wasm-crypto/src/js/index.js +++ b/packages/wasm-crypto/src/js/index.js @@ -9,7 +9,10 @@ module.exports.bip39ToEntropy = stubbed.ext_bip39_to_entropy; module.exports.bip39ToMiniSecret = stubbed.ext_bip39_to_mini_secret; module.exports.bip39Validate = stubbed.ext_bip39_validate module.exports.blake2b = stubbed.ext_blake2b; +module.exports.keccak256 = stubbed.ext_keccak256; module.exports.pbkdf2 = stubbed.ext_pbkdf2; +// module.exports.secp256k1IsRecoverable = stubbed.ext_secp256k1_is_recoverable; +// module.exports.secp256k1Recover = stubbed.ext_secp256k1_recover; module.exports.sha512 = stubbed.ext_sha512; module.exports.twox = stubbed.ext_twox; diff --git a/packages/wasm-crypto/src/lib.rs b/packages/wasm-crypto/src/lib.rs index 3602611f..e62b1bed 100644 --- a/packages/wasm-crypto/src/lib.rs +++ b/packages/wasm-crypto/src/lib.rs @@ -2,22 +2,15 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -extern crate bip39; -extern crate blake2_rfc; -extern crate hmac; -extern crate pbkdf2; -extern crate sha2; -extern crate twox_hash; -extern crate wasm_bindgen; -extern crate wee_alloc; - use bip39::{Mnemonic, MnemonicType, Language}; use blake2_rfc::blake2b::blake2b; use byteorder::{ByteOrder, LittleEndian}; use hmac::Hmac; use pbkdf2::pbkdf2; +// use secp256k1; use sha2::{Digest, Sha512}; use std::hash::Hasher; +use tiny_keccak::keccak256; use twox_hash::XxHash; use wasm_bindgen::prelude::*; @@ -98,7 +91,7 @@ pub fn ext_bip39_validate(phrase: &str) -> bool { /// * key: Key to add to the hashing (normally empty) /// * size: Size in bytes of the resulting output /// -/// Returns a vecor with the hash result +/// Returns a vector with the hash result #[wasm_bindgen] pub fn ext_blake2b(data: &[u8], key: &[u8], size: u32) -> Vec { // we cast to usize here - due to the WASM, we'd rather have u32 inputs @@ -107,6 +100,17 @@ pub fn ext_blake2b(data: &[u8], key: &[u8], size: u32) -> Vec { .to_vec() } +/// Create a keccak256 hash for the specified input +/// +// * data: Arbitrary data to be hashed +/// +/// Returns the hash as a vector +#[wasm_bindgen] +pub fn ext_keccak256(data: &[u8]) -> Vec { + keccak256(data) + .to_vec() +} + /// pbkdf2 hash from an input, salt for the number of specified rounds /// /// * data: Arbitrary data to be hashed @@ -124,6 +128,58 @@ pub fn ext_pbkdf2(data: &[u8], salt: &[u8], rounds: u32) -> Vec { result.to_vec() } +// /// Checks for the recoverability of a signature from message +// /// +// /// * message: The message +// /// * signature: The signature +// /// +// /// Returns 0 on success, or a specific error code +// #[wasm_bindgen] +// pub fn ext_secp256k1_is_recoverable(message: &[u8], signature: &[u8]) -> u32 { +// let rs = match secp256k1::Signature::parse_slice(&signature[0..64]) { +// Ok(rs) => rs, +// _ => return 1 +// }; +// let v = match secp256k1::RecoveryId::parse(if signature[64] > 26 { signature[64] - 27 } else { signature[64] } as u8) { +// Ok(v) => v, +// _ => return 2 +// }; +// let mut msg = [0u8; 32]; + +// msg.copy_from_slice(&message); + +// match secp256k1::recover(&secp256k1::Message::parse(&msg), &rs, &v) { +// Ok(_) => 0, +// _ => 3 +// } +// } + +// /// Recovers the secp256k1 signature from the input +// /// +// /// * message: The message +// /// * signature: The signature +// /// +// /// Returns a vector with the recovered public key +// #[wasm_bindgen] +// pub fn ext_secp256k1_recover(message: &[u8], signature: &[u8]) -> Vec { +// let rs = match secp256k1::Signature::parse_slice(&signature[0..64]) { +// Ok(rs) => rs, +// _ => panic!("Unable to recover r,s") +// }; +// let v = match secp256k1::RecoveryId::parse(if signature[64] > 26 { signature[64] - 27 } else { signature[64] } as u8) { +// Ok(v) => v, +// _ => panic!("Unable to recover v") +// }; +// let mut msg = [0u8; 32]; + +// msg.copy_from_slice(&message); + +// match secp256k1::recover(&secp256k1::Message::parse(&msg), &rs, &v) { +// Ok(pk) => return pk.serialize()[1..65].to_vec(), +// _ => panic!("Unable to recover publickey") +// } +// } + /// sha512 hash for the specified input /// /// * data: Arbitrary data to be hashed @@ -163,12 +219,31 @@ pub mod tests { use hex_literal::{hex, hex_impl}; use super::*; + // // Constructs the message that Ethereum RPC's `personal_sign` and `eth_sign` would sign. + // fn ethereum_signable_message(data: &[u8]) -> Vec { + // let prefix = b"Pay DOTs to the Polkadot account:"; + // let mut l = prefix.len() + data.len(); + // let mut rev = Vec::new(); + // let mut v = b"\x19Ethereum Signed Message:\n".to_vec(); + + // while l > 0 { + // rev.push(b'0' + (l % 10) as u8); + // l /= 10; + // } + + // v.extend(rev.into_iter().rev()); + // v.extend_from_slice(&prefix[..]); + // v.extend_from_slice(data); + // v + // } + #[test] fn can_bip39_entropy() { let phrase = "legal winner thank year wave sausage worth useful legal winner thank yellow"; let entropy = hex!("7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f"); + let result = ext_bip39_to_entropy(phrase); - assert_eq!(ext_bip39_to_entropy(phrase), entropy); + assert_eq!(result, entropy); } #[test] @@ -176,19 +251,26 @@ pub mod tests { let phrase = "legal winner thank year wave sausage worth useful legal winner thank yellow"; let password = "Substrate"; let mini = hex!("4313249608fe8ac10fd5886c92c4579007272cb77c21551ee5b8d60b78041685"); + let result = ext_bip39_to_mini_secret(phrase, password); - assert_eq!(ext_bip39_to_mini_secret(phrase, password)[..], mini[..]); + assert_eq!(result[..], mini[..]); } #[test] fn can_bip39_generate() { - assert!(ext_bip39_validate(&ext_bip39_generate(12))); + let phrase = ext_bip39_generate(12); + let is_valid = ext_bip39_validate(&phrase); + + assert!(is_valid); } #[test] fn can_bip39_validate() { - assert_eq!(ext_bip39_validate("seed sock milk update focus rotate barely fade car face mechanic mercy"), true); - assert_eq!(ext_bip39_validate("wine photo extra cushion basket dwarf humor cloud truck job boat submit"), false); + let is_valid = ext_bip39_validate("seed sock milk update focus rotate barely fade car face mechanic mercy"); + let is_invalid = ext_bip39_validate("wine photo extra cushion basket dwarf humor cloud truck job boat submit"); + + assert_eq!(is_valid, true); + assert_eq!(is_invalid, false); } #[test] @@ -196,9 +278,20 @@ pub mod tests { let data = b"abc"; let expected_32 = hex!("bddd813c634239723171ef3fee98579b94964e3bb1cb3e427262c8c068d52319"); let expected_64 = hex!("ba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923"); + let hash_32 = ext_blake2b(data, &[], 32); + let hash_64 = ext_blake2b(data, &[], 64); - assert_eq!(ext_blake2b(data, &[], 32)[..], expected_32[..]); - assert_eq!(ext_blake2b(data, &[], 64)[..], expected_64[..]); + assert_eq!(hash_32[..], expected_32[..]); + assert_eq!(hash_64[..], expected_64[..]); + } + + #[test] + fn can_keccak256() { + let data = b"test value"; + let expected = hex!("2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e"); + let hash = ext_keccak256(data); + + assert_eq!(hash[..], expected[..]); } #[test] @@ -206,16 +299,29 @@ pub mod tests { let salt = b"this is a salt"; let data = b"hello world"; let expected = hex!("5fcbe04f05300a3ecc5c35d18ea0b78f3f6853d2ae5f3fca374f69a7d1f78b5def5c60dae1a568026c7492511e0c53521e8bb6e03a650e1263265fee92722270"); + let hash = ext_pbkdf2(data, salt, 2048); - assert_eq!(ext_pbkdf2(data, salt, 2048)[..], expected[..]); + assert_eq!(hash[..], expected[..]); } + // #[test] + // fn can_secp256k1_real_eth_sig() { + // let sig = hex!("7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff1c"); + // let pk = hex!("DF67EC7EAe23D2459694685257b6FC59d1BAA1FE"); + // let data = [42, 0, 0, 0, 0, 0, 0, 0]; + // let msg = ext_keccak256(ðereum_signable_message(&data)); + // let result = ext_keccak256(&ext_secp256k1_recover(&msg[..], &sig[..])); + + // assert_eq!(result[12..], pk[..]); + // } + #[test] fn can_sha512() { let data = b"hello world"; let expected = hex!("309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f"); + let hash = ext_sha512(data); - assert_eq!(ext_sha512(data)[..], expected[..]); + assert_eq!(hash[..], expected[..]); } #[test] @@ -223,8 +329,10 @@ pub mod tests { let data = b"abc"; let expected_64 = hex!("990977adf52cbc44"); let expected_256 = hex!("990977adf52cbc440889329981caa9bef7da5770b2b8a05303b75d95360dd62b"); + let hash_64 = ext_twox(data, 1); + let hash_256 = ext_twox(data, 4); - assert_eq!(ext_twox(data, 1)[..], expected_64[..]); - assert_eq!(ext_twox(data, 4)[..], expected_256[..]); + assert_eq!(hash_64[..], expected_64[..]); + assert_eq!(hash_256[..], expected_256[..]); } } diff --git a/packages/wasm-crypto/test/all.js b/packages/wasm-crypto/test/all.js index f549a560..eadd3ce9 100644 --- a/packages/wasm-crypto/test/all.js +++ b/packages/wasm-crypto/test/all.js @@ -6,10 +6,6 @@ const { assert, stringToU8a, u8aToHex } = require('@polkadot/util'); const crypto = require('../build/index'); -async function beforeAll () { - return crypto.waitReady(); -} - function bip39Generate () { const RESULT = crypto.bip39Generate(21); @@ -43,51 +39,56 @@ function bip39ToMiniSecret () { } function blake2bHash () { - const DATA = stringToU8a('abc'); - const EXPECTED = '0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923'; - - const hash = u8aToHex(crypto.blake2b(DATA, new Uint8Array(), 64)); + const hash = u8aToHex(crypto.blake2b(stringToU8a('abc'), new Uint8Array(), 64)); console.log('\tRES', hash); - assert(hash === EXPECTED, 'ERROR: blake2bHash does not match'); + assert(hash === '0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923', 'ERROR: blake2bHash does not match'); +} + +function keccak256 () { + const hash = u8aToHex(crypto.keccak256(stringToU8a('test value'))); + + console.log('\tRES', hash); + + assert(hash === '0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e', 'ERROR: keccak256 does not match'); } function pbkdf2Hash () { - const SALT = stringToU8a('this is a salt'); - const DATA = stringToU8a('hello world'); - const EXPECTED = '0x5fcbe04f05300a3ecc5c35d18ea0b78f3f6853d2ae5f3fca374f69a7d1f78b5def5c60dae1a568026c7492511e0c53521e8bb6e03a650e1263265fee92722270'; - - const hash = u8aToHex(crypto.pbkdf2(DATA, SALT, 2048)); + const hash = u8aToHex(crypto.pbkdf2(stringToU8a('hello world'), stringToU8a('this is a salt'), 2048)); console.log('\tRES', hash); - assert(hash === EXPECTED, 'ERROR: pbkdf2Hash does not match'); + assert(hash === '0x5fcbe04f05300a3ecc5c35d18ea0b78f3f6853d2ae5f3fca374f69a7d1f78b5def5c60dae1a568026c7492511e0c53521e8bb6e03a650e1263265fee92722270', 'ERROR: pbkdf2Hash does not match'); } -function sha512Hash () { - const DATA = stringToU8a('hello world'); - const EXPECTED = '0x309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f'; +// function secp256k1Recover () { +// const pk = u8aToHex(crypto.keccak256(crypto.secp256k1Recover( +// hexToU8a('0x2bbfa926ee947dfac384d981f8913b4e0992e52d7b6711e0ec3d9f8b0f752648'), +// hexToU8a('0x7505f2880114da51b3f5d535f8687953c0ab9af4ab81e592eaebebf53b728d2b6dfd9b5bcd70fee412b1f31360e7c2774009305cb84fc50c1d0ff8034dfa5fff1c') +// ))); - const hash = u8aToHex(crypto.sha512(DATA)); +// console.log('\tPUB', pk); + +// assert(pk === '0x91d366769d44a7c7690428bfdf67ec7eae23d2459694685257b6fc59d1baa1fe', 'ERROR: secp256k1Recover does not match'); +// } + +function sha512Hash () { + const hash = u8aToHex(crypto.sha512(stringToU8a('hello world'))); console.log('\tRES', hash); - assert(hash === EXPECTED, 'ERROR: sha512Hash does not match'); + assert(hash === '0x309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f', 'ERROR: sha512Hash does not match'); } function twoxHash () { - const DATA = stringToU8a('abc'); - const EXPECTED_64 = '0x990977adf52cbc44'; - const EXPECTED_256 = '0x990977adf52cbc440889329981caa9bef7da5770b2b8a05303b75d95360dd62b'; - - const hash64 = u8aToHex(crypto.twox(DATA, 1)); - const hash256 = u8aToHex(crypto.twox(DATA, 4)); + const hash64 = u8aToHex(crypto.twox(stringToU8a('abc'), 1)); + const hash256 = u8aToHex(crypto.twox(stringToU8a('abc'), 4)); console.log('\t64 ', hash64); console.log('\t256', hash256); - assert(hash64 === EXPECTED_64 && hash256 === EXPECTED_256, 'ERROR: twoxHash does not match'); + assert(hash64 === '0x990977adf52cbc44' && hash256 === '0x990977adf52cbc440889329981caa9bef7da5770b2b8a05303b75d95360dd62b', 'ERROR: twoxHash does not match'); } const tests = { @@ -96,12 +97,18 @@ const tests = { bip39ToEntropy, bip39ToMiniSecret, blake2bHash, + keccak256, pbkdf2Hash, + // secp256k1Recover, sha512Hash, twoxHash }; -function runAll () { +module.exports.beforeAll = async function beforeAll () { + return crypto.waitReady(); +} + +module.exports.runAll = function runAll () { Object.keys(tests).forEach((name) => { console.time(name); console.log(); @@ -113,6 +120,4 @@ function runAll () { }); } -module.exports.beforeAll = beforeAll; -module.exports.runAll = runAll; module.exports.tests = tests; diff --git a/packages/wasm-dalek-ed25519/Cargo.lock b/packages/wasm-dalek-ed25519/Cargo.lock new file mode 100644 index 00000000..5cc5fd10 --- /dev/null +++ b/packages/wasm-dalek-ed25519/Cargo.lock @@ -0,0 +1,512 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "autocfg" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bitflags" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "block-buffer" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-padding 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "block-padding" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bumpalo" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "byte-tools" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "byteorder" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cc" +version = "1.0.31" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cfg-if" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "clear_on_drop" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cloudabi" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "curve25519-dalek" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "clear_on_drop 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "subtle 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "digest" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ed25519-dalek" +version = "1.0.0-pre.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "clear_on_drop 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "curve25519-dalek 1.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "failure" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "fake-simd" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "generic-array" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "hex-literal" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "hex-literal-impl 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-hack 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "hex-literal-impl" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro-hack 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "lazy_static" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.50" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "log" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "memory_units" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "opaque-debug" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "packed_simd" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro-hack" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro-hack-impl 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "proc-macro-hack-impl" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "proc-macro2" +version = "0.4.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "0.6.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", + "packed_simd 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_hc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_jitter 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_chacha" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rand_hc" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_isaac" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_jitter" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_os" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_pcg" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_xorshift" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "sha2" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-buffer 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "subtle" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "syn" +version = "0.15.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "typenum" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unreachable" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "wasm" +version = "0.0.0" +dependencies = [ + "ed25519-dalek 1.0.0-pre.1 (registry+https://github.com/rust-lang/crates.io-index)", + "hex-literal 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)", + "wee_alloc 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "wasm-bindgen-macro 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bumpalo 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-shared 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-macro-support 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-backend 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)", + "wasm-bindgen-shared 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.39" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "wee_alloc" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", + "memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6d640bee2da49f60a4068a7fae53acde8982514ab7bae8b8cea9e88cbcfd799" +"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" +"checksum block-buffer 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49665c62e0e700857531fa5d3763e91b539ff1abeebd56808d378b495870d60d" +"checksum block-padding 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d75255892aeb580d3c566f213a2b6fdc1c66667839f45719ee1d30ebf2aea591" +"checksum bumpalo 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "21ef2109240a377370f55ea3ef0b486b46d7b5c0f7455ab0ec676d73f875d58a" +"checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" +"checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb" +"checksum cc 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)" = "c9ce8bb087aacff865633f0bd5aeaed910fe2fe55b55f4739527f2e023a2e53d" +"checksum cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "11d43355396e872eefb45ce6342e4374ed7bc2b3a502d1b28e36d6e23c05d1f4" +"checksum clear_on_drop 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "97276801e127ffb46b66ce23f35cc96bd454fa311294bced4bbace7baa8b1d17" +"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" +"checksum curve25519-dalek 1.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e1f8a6fc0376eb52dc18af94915cc04dfdf8353746c0e8c550ae683a0815e5c1" +"checksum digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05f47366984d3ad862010e22c7ce81a7dbcaebbdfb37241a620f8b6596ee135c" +"checksum ed25519-dalek 1.0.0-pre.1 (registry+https://github.com/rust-lang/crates.io-index)" = "81956bcf7ef761fb4e1d88de3fa181358a0d26cbcb9755b587a08f9119824b86" +"checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" +"checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" +"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" +"checksum generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0f28c2f5bfb5960175af447a2da7c18900693738343dc896ffbcabd9839592" +"checksum hex-literal 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ddc2928beef125e519d69ae1baa8c37ea2e0d3848545217f6db0179c5eb1d639" +"checksum hex-literal-impl 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "520870c3213943eb8d7803e80180d12a6c7ceb4ae74602544529d1643dc4ddda" +"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" +"checksum libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)" = "aab692d7759f5cd8c859e169db98ae5b52c924add2af5fbbca11d12fefb567c1" +"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" +"checksum memory_units 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" +"checksum opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "93f5bb2e8e8dec81642920ccff6b61f1eb94fa3020c5a325c9851ff604152409" +"checksum packed_simd 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a85ea9fc0d4ac0deb6fe7911d38786b32fc11119afd9e9d38b84ff691ce64220" +"checksum proc-macro-hack 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2c725b36c99df7af7bf9324e9c999b9e37d92c8f8caf106d82e1d7953218d2d8" +"checksum proc-macro-hack-impl 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2b753ad9ed99dd8efeaa7d2fb8453c8f6bc3e54b97966d35f1bc77ca6865254a" +"checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915" +"checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1" +"checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" +"checksum rand_chacha 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef" +"checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +"checksum rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0e7a549d590831370895ab7ba4ea0c1b6b011d106b5ff2da6eee112615e6dc0" +"checksum rand_hc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4" +"checksum rand_isaac 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08" +"checksum rand_jitter 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b9ea758282efe12823e0d952ddb269d2e1897227e464919a554f2a03ef1b832" +"checksum rand_os 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071" +"checksum rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44" +"checksum rand_xorshift 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c" +"checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +"checksum sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4d8bfd0e469f417657573d8451fb33d16cfe0989359b93baf3a1ffc639543d" +"checksum subtle 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "702662512f3ddeb74a64ce2fbbf3707ee1b6bb663d28bb054e0779bbc720d926" +"checksum syn 0.15.29 (registry+https://github.com/rust-lang/crates.io-index)" = "1825685f977249735d510a242a6727b46efe914bb67e38d30c071b1b72b1d5c2" +"checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" +"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" +"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" +"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum wasm-bindgen 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)" = "06e36714cc3947a9c80ddc9715edb319e445e5ab0d0b083debec0dd52558580e" +"checksum wasm-bindgen-backend 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)" = "6764493f6eeb3f8632ffd418bf7db6a7e7aca98417c115833e4663976cd60a5e" +"checksum wasm-bindgen-macro 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)" = "a473501c40a2a4da83ae7176fead61f10d003554b701071a704dd0667397027a" +"checksum wasm-bindgen-macro-support 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)" = "72e5a249738c6315f65ce5527632e2a3a067cdf1765422d8ac1572dcee14f819" +"checksum wasm-bindgen-shared 0.2.39 (registry+https://github.com/rust-lang/crates.io-index)" = "70f57643dac7decc3b53f0e4ffd027e225b79281648842eb46015e58a2400870" +"checksum wee_alloc 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e841fed992c6c70cf2af5dbcffb0d988b1a7ffc5177ae4fb714ed14944b39d0a" +"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/packages/wasm-dalek-ed25519/Cargo.toml b/packages/wasm-dalek-ed25519/Cargo.toml new file mode 100644 index 00000000..b0a77bae --- /dev/null +++ b/packages/wasm-dalek-ed25519/Cargo.toml @@ -0,0 +1,27 @@ +# name & version is only used internally, doesn't need to be adjusted. In the case of +# name keep it consistent at "wasm" since the scripts (for re-use purposes) expect that + +[package] +authors = ["Jaco Greeff ", "kianenigma "] +description = "WASM bindings to w3f/schnorrkel." +edition = "2018" +license = "Apache-2.0" +name = "wasm" +publish = false +repository = "https://github.com/polkadot-js/wasm/" +version = "0.0.0" + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +ed25519-dalek = { version = "1.0.0-pre.1", features = ["nightly"] } +wasm-bindgen = "0.2" +wee_alloc = "0.4.3" + +[dev-dependencies] +hex-literal = "0.1.4" +rand = { version = "0.6.5", features = ["wasm-bindgen"] } + +[profile.release] +lto = true diff --git a/packages/wasm-dalek-ed25519/LICENSE b/packages/wasm-dalek-ed25519/LICENSE new file mode 100644 index 00000000..0d381b2e --- /dev/null +++ b/packages/wasm-dalek-ed25519/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/packages/wasm-dalek-ed25519/README.md b/packages/wasm-dalek-ed25519/README.md new file mode 100644 index 00000000..68e63f26 --- /dev/null +++ b/packages/wasm-dalek-ed25519/README.md @@ -0,0 +1 @@ +# @polkadot/wasm-dalek-ed25519 diff --git a/packages/wasm-dalek-ed25519/adjust.sh b/packages/wasm-dalek-ed25519/adjust.sh new file mode 100755 index 00000000..b87002ab --- /dev/null +++ b/packages/wasm-dalek-ed25519/adjust.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +set -e + +# shortcuts for files +echo "*** Adjusting output" +SRC=build/wasm.js + +# We don't want inline requires +sed -i -e 's/var wasm;/const crypto = require('\''crypto'\''); let wasm; const requires = { crypto };/g' $SRC +sed -i -e 's/return addHeapObject(require(varg0));/return addHeapObject(requires[varg0]);/g' $SRC + +# this creates issues in both the browser and RN (@polkadot/util has a polyfill) +sed -i -e 's/const TextDecoder = require('\''util'\'')\.TextDecoder;/const { u8aToString } = require('\''@polkadot\/util'\'');/g' $SRC +sed -i -e 's/let cachedTextDecoder = new /\/\/ let cachedTextDecoder = new /g' $SRC +sed -i -e 's/cachedTextDecoder\.decode/u8aToString/g' $SRC diff --git a/packages/wasm-dalek-ed25519/package.json b/packages/wasm-dalek-ed25519/package.json new file mode 100644 index 00000000..213b96fa --- /dev/null +++ b/packages/wasm-dalek-ed25519/package.json @@ -0,0 +1,21 @@ +{ + "name": "@polkadot/wasm-dalek-ed25519", + "version": "0.2.1", + "author": "Jaco Greeff ", + "files": [ + "index.d.ts", + "index.js", + "wasm.d.ts", + "wasm.js", + "wasm_bg.js", + "wasm_wasm.js" + ], + "main": "index.js", + "types": "index.d.ts", + "peerDependencies": { + "@polkadot/util": "*" + }, + "devDependencies": { + "jest": "*" + } +} diff --git a/packages/wasm-dalek-ed25519/src/js/index.d.ts b/packages/wasm-dalek-ed25519/src/js/index.d.ts new file mode 100644 index 00000000..3e153c10 --- /dev/null +++ b/packages/wasm-dalek-ed25519/src/js/index.d.ts @@ -0,0 +1,10 @@ +// Copyright 2019 @polkadot/wasm-schnorrkel authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +export function keypairFromSeed (seed: Uint8Array): Uint8Array; +export function sign (publicKey: Uint8Array, secretKey: Uint8Array, message: Uint8Array): Uint8Array; +export function verify (signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array): boolean; + +export function isReady (): boolean; +export function waitReady (): Promise; diff --git a/packages/wasm-dalek-ed25519/src/js/index.js b/packages/wasm-dalek-ed25519/src/js/index.js new file mode 100644 index 00000000..f0215ece --- /dev/null +++ b/packages/wasm-dalek-ed25519/src/js/index.js @@ -0,0 +1,12 @@ +// Copyright 2019 @polkadot/wasm-schnorrkel authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +const stubbed = require('./wasm'); + +module.exports.keypairFromSeed = stubbed.ext_ed_from_seed; +module.exports.sign = stubbed.ext_ed_sign; +module.exports.verify = stubbed.ext_ed_verify; + +module.exports.isReady = stubbed.isReady; +module.exports.waitReady = stubbed.waitReady; diff --git a/packages/wasm-dalek-ed25519/src/lib.rs b/packages/wasm-dalek-ed25519/src/lib.rs new file mode 100644 index 00000000..f0cfbb1c --- /dev/null +++ b/packages/wasm-dalek-ed25519/src/lib.rs @@ -0,0 +1,168 @@ +// Copyright 2019 @polkadot/wasm-dalek-ed25519 authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +use ed25519_dalek::{Keypair, PublicKey, SecretKey, Signature}; +use wasm_bindgen::prelude::*; + +// Use `wee_alloc` as the global allocator. +#[global_allocator] +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + +/// Keypair helper function. +fn create_from_pair(pair: &[u8]) -> Keypair { + match Keypair::from_bytes(pair) { + Ok(pair) => return pair, + Err(_) => panic!("Provided pair is invalid.") + } +} + +/// Keypair helper function +fn create_from_parts(public: &[u8], secret: &[u8]) -> Keypair { + let mut pair = vec![]; + + pair.extend_from_slice(secret); + pair.extend_from_slice(public); + + create_from_pair(&pair) +} + +/// Keypair helper function. +fn create_from_seed(seed: &[u8]) -> Keypair { + let secret = SecretKey::from_bytes(seed).unwrap(); + let public: PublicKey = (&secret).into(); + + create_from_parts(public.as_bytes(), seed) +} + +/// PublicKey helper +fn create_public(public: &[u8]) -> PublicKey { + match PublicKey::from_bytes(public) { + Ok(public) => return public, + Err(_) => panic!("Provided public key is invalid.") + } +} + +/// Generate a key pair. +/// +/// * seed: UIntArray with 32 element +/// +/// returned vector is the concatenation of first the private key (64 bytes) +/// followed by the public key (32) bytes. +#[wasm_bindgen] +pub fn ext_ed_from_seed(seed: &[u8]) -> Vec { + create_from_seed(seed) + .to_bytes() + .to_vec() +} + +/// Sign a message +/// +/// The combination of both public and private key must be provided. +/// This is effectively equivalent to a keypair. +/// +/// * public: UIntArray with 32 element +/// * private: UIntArray with 64 element +/// * message: Arbitrary length UIntArray +/// +/// * returned vector is the signature consisting of 64 bytes. +#[wasm_bindgen] +pub fn ext_ed_sign(public: &[u8], secret: &[u8], message: &[u8]) -> Vec { + create_from_parts(public, secret) + .sign(message) + .to_bytes() + .to_vec() +} + +/// Verify a message and its corresponding against a public key; +/// +/// * signature: UIntArray with 64 element +/// * message: Arbitrary length UIntArray +/// * pubkey: UIntArray with 32 element +#[wasm_bindgen] +pub fn ext_ed_verify(signature: &[u8], message: &[u8], public: &[u8]) -> bool { + let signature = match Signature::from_bytes(signature) { + Ok(signature) => signature, + Err(_) => return false + }; + + create_public(public) + .verify(message, &signature) + .is_ok() +} + +#[cfg(test)] +pub mod tests { + extern crate rand; + + use hex_literal::{hex, hex_impl}; + use super::*; + use ed25519_dalek::{SIGNATURE_LENGTH, KEYPAIR_LENGTH, SECRET_KEY_LENGTH}; + + fn generate_random_seed() -> Vec { + (0..32).map(|_| rand::random::() ).collect() + } + + #[test] + fn can_create_keypair() { + let seed = generate_random_seed(); + let keypair = ext_ed_from_seed(seed.as_slice()); + + assert!(keypair.len() == KEYPAIR_LENGTH); + } + + #[test] + fn creates_pair_from_known() { + let seed = b"12345678901234567890123456789012"; + let expected = hex!("2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee"); + let keypair = ext_ed_from_seed(seed); + let public = &keypair[SECRET_KEY_LENGTH..KEYPAIR_LENGTH]; + + assert_eq!(public, expected); + } + + #[test] + fn can_sign_message() { + let seed = generate_random_seed(); + let keypair = ext_ed_from_seed(seed.as_slice()); + let private = &keypair[0..SECRET_KEY_LENGTH]; + let public = &keypair[SECRET_KEY_LENGTH..KEYPAIR_LENGTH]; + let message = b"this is a message"; + let signature = ext_ed_sign(public, private, message); + + assert!(signature.len() == SIGNATURE_LENGTH); + } + + #[test] + fn can_verify_message() { + let seed = generate_random_seed(); + let keypair = ext_ed_from_seed(seed.as_slice()); + let private = &keypair[0..SECRET_KEY_LENGTH]; + let public = &keypair[SECRET_KEY_LENGTH..KEYPAIR_LENGTH]; + let message = b"this is a message"; + let signature = ext_ed_sign(public, private, message); + let is_valid = ext_ed_verify(&signature[..], message, public); + + assert!(is_valid); + } + + #[test] + fn can_verify_known() { + let public = hex!("2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee"); + let message = b"this is a message"; + let signature = hex!("90588f3f512496f2dd40571d162e8182860081c74e2085316e7c4396918f07da412ee029978e4dd714057fe973bd9e7d645148bf7b66680d67c93227cde95202"); + let is_valid = ext_ed_verify(&signature, message, &public); + + assert!(is_valid); + } + + #[test] + fn can_verify_known_wrong() { + let public = hex!("2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee"); + let message = b"this is a message"; + let signature = &[0u8; 64]; + let is_valid = ext_ed_verify(signature, message, &public); + + assert_eq!(is_valid, false); + } +} diff --git a/packages/wasm-dalek-ed25519/test/all.js b/packages/wasm-dalek-ed25519/test/all.js new file mode 100644 index 00000000..d2efbc54 --- /dev/null +++ b/packages/wasm-dalek-ed25519/test/all.js @@ -0,0 +1,79 @@ +// Copyright 2019 @polkadot/wasm-schnorrkel 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 crypto = require('crypto'); +const { assert, hexToU8a, stringToU8a, u8aToHex } = require('@polkadot/util'); +const dalek = require('../build/index'); + +function extractKeys (pair) { + return [pair, pair.slice(32), pair.slice(0, 32)]; +} + +function randomPair () { + return extractKeys(dalek.keypairFromSeed(crypto.randomBytes(32))); +} + +function pairFromSeed () { + const [pair, pk, sk] = extractKeys(dalek.keypairFromSeed(stringToU8a('12345678901234567890123456789012'))); + + console.log('\tSEC', u8aToHex(sk)); + console.log('\tPUB', u8aToHex(pk)); + + assert(u8aToHex(pair) === '0x31323334353637383930313233343536373839303132333435363738393031322f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee', 'ERROR: pairFromSeed() does not match'); +} + +function signAndVerify () { + const [, pk, sk] = randomPair(); + const signature = dalek.sign(pk, sk, stringToU8a('this is a message')); + const isValid = dalek.verify(signature, stringToU8a('this is a message'), pk); + + console.log('\tSIG', u8aToHex(signature)); + console.log('\tRES', isValid); + + assert(isValid, 'ERROR: Unable to verify signature'); +} + +function verifyExisting () { + const isValid = dalek.verify(hexToU8a('0x90588f3f512496f2dd40571d162e8182860081c74e2085316e7c4396918f07da412ee029978e4dd714057fe973bd9e7d645148bf7b66680d67c93227cde95202'), stringToU8a('this is a message'), hexToU8a('0x2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee')); + + console.log('\tRES', isValid); + + assert(isValid, 'ERROR: Unable to verify signature'); +} + +function benchmark () { + const MESSAGE = stringToU8a('this is a message'); + + for (let i = 0; i < 256; i++) { + const [, pk, sk] = randomPair(); + + assert(dalek.verify(dalek.sign(pk, sk, MESSAGE), MESSAGE, pk), 'ERROR: Unable to verify signature'); + } +} + +const tests = { + pairFromSeed, + signAndVerify, + verifyExisting, + benchmark +}; + +module.exports.beforeAll = async function beforeAll () { + return dalek.waitReady(); +} + +module.exports.runAll = function runAll () { + Object.keys(tests).forEach((name) => { + console.time(name); + console.log(); + console.log(name); + + tests[name](); + + console.timeEnd(name); + }); +} + +module.exports.tests = tests; diff --git a/packages/wasm-dalek-ed25519/test/asm.js b/packages/wasm-dalek-ed25519/test/asm.js new file mode 100644 index 00000000..5cd58cd5 --- /dev/null +++ b/packages/wasm-dalek-ed25519/test/asm.js @@ -0,0 +1,9 @@ +// Copyright 2019 @polkadot/wasm-schnorrkel 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('./wasm'); diff --git a/packages/wasm-dalek-ed25519/test/jest.spec.js b/packages/wasm-dalek-ed25519/test/jest.spec.js new file mode 100644 index 00000000..5cd3e1d4 --- /dev/null +++ b/packages/wasm-dalek-ed25519/test/jest.spec.js @@ -0,0 +1,16 @@ +// Copyright 2019 @polkadot/wasm-schnorrkel 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 + +require('../../wasm-crypto/build/crypto-polyfill'); + +const { beforeAll, tests } = require('./all.js'); + +describe('schnorrkel-js', () => { + beforeEach(beforeAll); + + Object.keys(tests).forEach((name) => { + it(name, tests[name]); + }); +}); diff --git a/packages/wasm-dalek-ed25519/test/wasm.js b/packages/wasm-dalek-ed25519/test/wasm.js new file mode 100644 index 00000000..265bbecf --- /dev/null +++ b/packages/wasm-dalek-ed25519/test/wasm.js @@ -0,0 +1,11 @@ +// Copyright 2019 @polkadot/wasm-schnorrkel 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 { beforeAll, runAll } = require('./all'); + +(async () => { + await beforeAll(); + runAll(); +})(); diff --git a/packages/wasm-schnorrkel/Cargo.toml b/packages/wasm-schnorrkel/Cargo.toml index 66d82f33..9c8d2b37 100644 --- a/packages/wasm-schnorrkel/Cargo.toml +++ b/packages/wasm-schnorrkel/Cargo.toml @@ -3,9 +3,12 @@ [package] authors = ["Jaco Greeff ", "kianenigma "] +description = "WASM bindings to w3f/schnorrkel." edition = "2018" +license = "Apache-2.0" name = "wasm" publish = false +repository = "https://github.com/polkadot-js/wasm/" version = "0.0.0" [lib] diff --git a/packages/wasm-schnorrkel/adjust.sh b/packages/wasm-schnorrkel/adjust.sh index 2320448a..b87002ab 100755 --- a/packages/wasm-schnorrkel/adjust.sh +++ b/packages/wasm-schnorrkel/adjust.sh @@ -2,9 +2,6 @@ set -e -echo "*** Building package" -../../scripts/build-package.sh - # shortcuts for files echo "*** Adjusting output" SRC=build/wasm.js @@ -17,6 +14,3 @@ sed -i -e 's/return addHeapObject(require(varg0));/return addHeapObject(requires sed -i -e 's/const TextDecoder = require('\''util'\'')\.TextDecoder;/const { u8aToString } = require('\''@polkadot\/util'\'');/g' $SRC sed -i -e 's/let cachedTextDecoder = new /\/\/ let cachedTextDecoder = new /g' $SRC sed -i -e 's/cachedTextDecoder\.decode/u8aToString/g' $SRC - -echo "*** Testing package" -../../scripts/test-package.sh diff --git a/packages/wasm-schnorrkel/package.json b/packages/wasm-schnorrkel/package.json index 2a82588d..f94aae81 100644 --- a/packages/wasm-schnorrkel/package.json +++ b/packages/wasm-schnorrkel/package.json @@ -6,7 +6,6 @@ "kianenigma " ], "files": [ - "crypto-polyfill.js", "index.d.ts", "index.js", "wasm.d.ts", diff --git a/packages/wasm-schnorrkel/src/js/crypto-polyfill.js b/packages/wasm-schnorrkel/src/js/crypto-polyfill.js deleted file mode 100644 index f0f609d5..00000000 --- a/packages/wasm-schnorrkel/src/js/crypto-polyfill.js +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2019 @polkadot/wasm-schnorrkel authors & contributors -// This software may be modified and distributed under the terms -// of the Apache-2.0 license. See the LICENSE file for details. - -const crypto = require('crypto'); - -if (!global.crypto) { - global.crypto = {}; -} - -if (!global.crypto.getRandomValues) { - global.crypto.getRandomValues = function (arr) { - const buffer = crypto.randomBytes(arr.length); - - return buffer.reduce((arr, value, index) => { - arr[index] = value; - - return arr; - }, arr); - } -} diff --git a/packages/wasm-schnorrkel/src/js/index.js b/packages/wasm-schnorrkel/src/js/index.js index bb66c612..abd12442 100644 --- a/packages/wasm-schnorrkel/src/js/index.js +++ b/packages/wasm-schnorrkel/src/js/index.js @@ -4,12 +4,12 @@ const stubbed = require('./wasm'); -module.exports.deriveKeypairHard = stubbed.derive_keypair_hard; -module.exports.deriveKeypairSoft = stubbed.derive_keypair_soft; -module.exports.derivePublicSoft = stubbed.derive_public_soft; -module.exports.keypairFromSeed = stubbed.keypair_from_seed; -module.exports.sign = stubbed.sign; -module.exports.verify = stubbed.verify; +module.exports.deriveKeypairHard = stubbed.ext_sr_derive_keypair_hard; +module.exports.deriveKeypairSoft = stubbed.ext_sr_derive_keypair_soft; +module.exports.derivePublicSoft = stubbed.ext_sr_derive_public_soft; +module.exports.keypairFromSeed = stubbed.ext_sr_from_seed; +module.exports.sign = stubbed.ext_sr_sign; +module.exports.verify = stubbed.ext_sr_verify; module.exports.isReady = stubbed.isReady; module.exports.waitReady = stubbed.waitReady; diff --git a/packages/wasm-schnorrkel/src/lib.rs b/packages/wasm-schnorrkel/src/lib.rs index 0c0fa0f0..ec3eb833 100644 --- a/packages/wasm-schnorrkel/src/lib.rs +++ b/packages/wasm-schnorrkel/src/lib.rs @@ -6,10 +6,6 @@ // which was adpated from the initial https://github.com/paritytech/schnorrkel-js/ // forked at commit eff430ddc3090f56317c80654208b8298ef7ab3f -extern crate wasm_bindgen; -extern crate wee_alloc; -extern crate schnorrkel; - use schnorrkel::{ Keypair, MiniSecretKey, PublicKey, SecretKey, Signature, derive::{Derivation, ChainCode, CHAIN_CODE_LENGTH}, @@ -71,7 +67,7 @@ fn create_secret(secret: &[u8]) -> SecretKey { /// /// returned vector the derived keypair as a array of 96 bytes #[wasm_bindgen] -pub fn derive_keypair_hard(pair: &[u8], cc: &[u8]) -> Vec { +pub fn ext_sr_derive_keypair_hard(pair: &[u8], cc: &[u8]) -> Vec { create_from_pair(pair).secret .hard_derive_mini_secret_key(Some(create_cc(cc)), &[]).0 .expand_to_keypair() @@ -86,7 +82,7 @@ pub fn derive_keypair_hard(pair: &[u8], cc: &[u8]) -> Vec { /// /// returned vector the derived keypair as a array of 96 bytes #[wasm_bindgen] -pub fn derive_keypair_soft(pair: &[u8], cc: &[u8]) -> Vec { +pub fn ext_sr_derive_keypair_soft(pair: &[u8], cc: &[u8]) -> Vec { create_from_pair(pair) .derived_key_simple(create_cc(cc), &[]).0 .to_bytes() @@ -100,7 +96,7 @@ pub fn derive_keypair_soft(pair: &[u8], cc: &[u8]) -> Vec { /// /// returned vector is the derived publicKey as a array of 32 bytes #[wasm_bindgen] -pub fn derive_public_soft(public: &[u8], cc: &[u8]) -> Vec { +pub fn ext_sr_derive_public_soft(public: &[u8], cc: &[u8]) -> Vec { create_public(public) .derived_key_simple(create_cc(cc), &[]).0 .to_bytes().to_vec() @@ -113,7 +109,7 @@ pub fn derive_public_soft(public: &[u8], cc: &[u8]) -> Vec { /// returned vector is the concatenation of first the private key (64 bytes) /// followed by the public key (32) bytes. #[wasm_bindgen] -pub fn keypair_from_seed(seed: &[u8]) -> Vec { +pub fn ext_sr_from_seed(seed: &[u8]) -> Vec { create_from_seed(seed) .to_bytes() .to_vec() @@ -130,7 +126,7 @@ pub fn keypair_from_seed(seed: &[u8]) -> Vec { /// /// * returned vector is the signature consisting of 64 bytes. #[wasm_bindgen] -pub fn sign(public: &[u8], secret: &[u8], message: &[u8]) -> Vec { +pub fn ext_sr_sign(public: &[u8], secret: &[u8], message: &[u8]) -> Vec { create_secret(secret) .sign_simple(SIGNING_CTX, message, &create_public(public)) .to_bytes() @@ -143,7 +139,7 @@ pub fn sign(public: &[u8], secret: &[u8], message: &[u8]) -> Vec { /// * message: Arbitrary length UIntArray /// * pubkey: UIntArray with 32 element #[wasm_bindgen] -pub fn verify(signature: &[u8], message: &[u8], public: &[u8]) -> bool { +pub fn ext_sr_verify(signature: &[u8], message: &[u8], public: &[u8]) -> bool { let signature = match Signature::from_bytes(signature) { Ok(signature) => signature, Err(_) => return false @@ -169,7 +165,7 @@ pub mod tests { #[test] fn can_create_keypair() { let seed = generate_random_seed(); - let keypair = keypair_from_seed(seed.as_slice()); + let keypair = ext_sr_from_seed(seed.as_slice()); assert!(keypair.len() == KEYPAIR_LENGTH); } @@ -178,7 +174,7 @@ pub mod tests { fn creates_pair_from_known() { let seed = hex!("fac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"); let expected = hex!("46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a"); - let keypair = keypair_from_seed(&seed); + let keypair = ext_sr_from_seed(&seed); let public = &keypair[SECRET_KEY_LENGTH..KEYPAIR_LENGTH]; assert_eq!(public, expected); @@ -187,11 +183,11 @@ pub mod tests { #[test] fn can_sign_message() { let seed = generate_random_seed(); - let keypair = keypair_from_seed(seed.as_slice()); + let keypair = ext_sr_from_seed(seed.as_slice()); let private = &keypair[0..SECRET_KEY_LENGTH]; let public = &keypair[SECRET_KEY_LENGTH..KEYPAIR_LENGTH]; let message = b"this is a message"; - let signature = sign(public, private, message); + let signature = ext_sr_sign(public, private, message); assert!(signature.len() == SIGNATURE_LENGTH); } @@ -199,13 +195,14 @@ pub mod tests { #[test] fn can_verify_message() { let seed = generate_random_seed(); - let keypair = keypair_from_seed(seed.as_slice()); + let keypair = ext_sr_from_seed(seed.as_slice()); let private = &keypair[0..SECRET_KEY_LENGTH]; let public = &keypair[SECRET_KEY_LENGTH..KEYPAIR_LENGTH]; let message = b"this is a message"; - let signature = sign(public, private, message); + let signature = ext_sr_sign(public, private, message); + let is_valid = ext_sr_verify(&signature[..], message, public); - assert!(verify(&signature[..], message, public)); + assert!(is_valid); } #[test] @@ -213,8 +210,8 @@ pub mod tests { let cc = hex!("0c666f6f00000000000000000000000000000000000000000000000000000000"); // foo let seed = hex!("fac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"); let expected = hex!("40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a"); - let keypair = keypair_from_seed(&seed); - let derived = derive_keypair_soft(&keypair, &cc); + let keypair = ext_sr_from_seed(&seed); + let derived = ext_sr_derive_keypair_soft(&keypair, &cc); let public = &derived[SECRET_KEY_LENGTH..KEYPAIR_LENGTH]; assert_eq!(public, expected); @@ -225,8 +222,9 @@ pub mod tests { let cc = hex!("0c666f6f00000000000000000000000000000000000000000000000000000000"); // foo let public = hex!("46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a"); let expected = hex!("40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a"); + let derived = ext_sr_derive_public_soft(&public, &cc); - assert_eq!(derive_public_soft(&public, &cc), expected); + assert_eq!(derived, expected); } #[test] @@ -234,8 +232,8 @@ pub mod tests { let cc = hex!("14416c6963650000000000000000000000000000000000000000000000000000"); // Alice let seed = hex!("fac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e"); let expected = hex!("d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"); - let keypair = keypair_from_seed(&seed); - let derived = derive_keypair_hard(&keypair, &cc); + let keypair = ext_sr_from_seed(&seed); + let derived = ext_sr_derive_keypair_hard(&keypair, &cc); let public = &derived[SECRET_KEY_LENGTH..KEYPAIR_LENGTH]; assert_eq!(public, expected); diff --git a/packages/wasm-schnorrkel/test/all.js b/packages/wasm-schnorrkel/test/all.js index 19556289..428c34cd 100644 --- a/packages/wasm-schnorrkel/test/all.js +++ b/packages/wasm-schnorrkel/test/all.js @@ -15,32 +15,22 @@ function randomPair () { return extractKeys(schnorrkel.keypairFromSeed(crypto.randomBytes(32))); } -async function beforeAll () { - return schnorrkel.waitReady(); -} - function pairFromSeed () { - const SEED = stringToU8a('12345678901234567890123456789012'); - const PAIR = hexToU8a('0xf0106660c3dda23f16daa9ac5b811b963077f5bc0af89f85804f0de8e424f050f98d66f39442506ff947fd911f18c7a7a5da639a63e8d3b4e233f74143d951c1741c08a06f41c596608f6774259bd9043304adfa5d3eea62760bd9be97634d63'); - - const pair = schnorrkel.keypairFromSeed(SEED); + const pair = schnorrkel.keypairFromSeed(stringToU8a('12345678901234567890123456789012')); console.log('\tSEC', u8aToHex(pair.slice(0, 64))); console.log('\tPUB', u8aToHex(pair.slice(64))); - assert(u8aToHex(pair) === u8aToHex(PAIR), 'ERROR: pairFromSeed() does not match'); + assert(u8aToHex(pair) === '0xf0106660c3dda23f16daa9ac5b811b963077f5bc0af89f85804f0de8e424f050f98d66f39442506ff947fd911f18c7a7a5da639a63e8d3b4e233f74143d951c1741c08a06f41c596608f6774259bd9043304adfa5d3eea62760bd9be97634d63', 'ERROR: pairFromSeed() does not match'); } function devFromSeed () { - const SEED = hexToU8a('0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e'); - const PAIR = hexToU8a('0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'); - - const pair = schnorrkel.keypairFromSeed(SEED); + const pair = schnorrkel.keypairFromSeed(hexToU8a('0xfac7959dbfe72f052e5a0c3c8d6530f202b02fd8f9f5ca3580ec8deb7797479e')); console.log('\tSEC', u8aToHex(pair.slice(0, 64))); console.log('\tPUB', u8aToHex(pair.slice(64))); - assert(u8aToHex(pair) === u8aToHex(PAIR), 'ERROR: devFromSeed() does not match'); + assert(u8aToHex(pair) === '0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a', 'ERROR: devFromSeed() does not match'); } function verifyExisting () { @@ -57,11 +47,9 @@ function verifyExisting () { } function signAndVerify () { - const MESSAGE = stringToU8a('this is a message'); - const [, pk, sk] = randomPair(); - const signature = schnorrkel.sign(pk, sk, MESSAGE); - const isValid = schnorrkel.verify(signature, MESSAGE, pk); + const signature = schnorrkel.sign(pk, sk, stringToU8a('this is a message')); + const isValid = schnorrkel.verify(signature, stringToU8a('this is a message'), pk); console.log('\tSIG', u8aToHex(signature)); console.log('\tRES', isValid); @@ -70,62 +58,47 @@ function signAndVerify () { } function deriveHard () { - const CC = hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000'); const [pair] = randomPair(); - const derived = schnorrkel.deriveKeypairHard(pair, CC); + const derived = schnorrkel.deriveKeypairHard(pair, hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000')); console.log('\tSEC', u8aToHex(derived.slice(0, 64))); console.log('\tPUB', u8aToHex(derived.slice(64))); } function deriveHardKnown () { - const CC = hexToU8a('0x14416c6963650000000000000000000000000000000000000000000000000000'); - const PAIR = hexToU8a('0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'); - const PUBLIC = '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d'; - - const derived = schnorrkel.deriveKeypairHard(PAIR, CC); + const derived = schnorrkel.deriveKeypairHard(hexToU8a('0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x14416c6963650000000000000000000000000000000000000000000000000000')); const publicKey = u8aToHex(derived.slice(64)); console.log('\tSEC', u8aToHex(derived.slice(0, 64))); console.log('\tPUB', publicKey); - assert(publicKey === PUBLIC, 'Unmatched resulting public keys'); + assert(publicKey === '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', 'Unmatched resulting public keys'); } function deriveSoft () { - const CC = hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000'); - const [pair] = randomPair(); - const derived = schnorrkel.deriveKeypairSoft(pair, CC); + const derived = schnorrkel.deriveKeypairSoft(pair, hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000')); console.log('\tSEC', u8aToHex(derived.slice(0, 64))); console.log('\tPUB', u8aToHex(derived.slice(64))); } function deriveSoftKnown () { - const CC = hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000'); - const PUBLIC = '0x40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a'; - const PAIR = hexToU8a('0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'); - - const derived = schnorrkel.deriveKeypairSoft(PAIR, CC); + const derived = schnorrkel.deriveKeypairSoft(hexToU8a('0x28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca3446ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000')); const publicKey = u8aToHex(derived.slice(64)); console.log('\tSEC', u8aToHex(derived.slice(0, 64))); console.log('\tPUB', publicKey); - assert(publicKey === PUBLIC, 'Unmatched resulting public keys'); + assert(publicKey === '0x40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a', 'Unmatched resulting public keys'); } function deriveSoftPubkey () { - const CC = hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000'); - const PUBLIC = hexToU8a('0x46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'); - const DERIVED = '0x40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a'; - - const derived = u8aToHex(schnorrkel.derivePublicSoft(PUBLIC, CC)); + const derived = u8aToHex(schnorrkel.derivePublicSoft( hexToU8a('0x46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a'), hexToU8a('0x0c666f6f00000000000000000000000000000000000000000000000000000000'))); console.log('\tPUB', derived); - assert(derived === DERIVED, 'Unmatched resulting public keys'); + assert(derived === '0x40b9675df90efa6069ff623b0fdfcf706cd47ca7452a5056c7ad58194d23440a', 'Unmatched resulting public keys'); } function benchmark () { @@ -151,7 +124,11 @@ const tests = { benchmark }; -function runAll () { +module.exports.beforeAll = async function beforeAll () { + return schnorrkel.waitReady(); +} + +module.exports.runAll = function runAll () { Object.keys(tests).forEach((name) => { console.time(name); console.log(); @@ -163,6 +140,4 @@ function runAll () { }); } -module.exports.beforeAll = beforeAll; -module.exports.runAll = runAll; module.exports.tests = tests; diff --git a/packages/wasm-schnorrkel/test/jest.spec.js b/packages/wasm-schnorrkel/test/jest.spec.js index bbadd9b4..5cd3e1d4 100644 --- a/packages/wasm-schnorrkel/test/jest.spec.js +++ b/packages/wasm-schnorrkel/test/jest.spec.js @@ -3,7 +3,7 @@ // of the Apache-2.0 license. See the LICENSE file for details. // @ts-check -require('../build/crypto-polyfill'); +require('../../wasm-crypto/build/crypto-polyfill'); const { beforeAll, tests } = require('./all.js'); diff --git a/scripts/clean.sh b/scripts/clean.sh new file mode 100755 index 00000000..2ba267ed --- /dev/null +++ b/scripts/clean.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +set -e + +./scripts/install-build-deps.sh + +echo "*** Cleaning packages" +cd packages + +PACKAGES=( $(ls -1d *) ) + +for PKG in "${PACKAGES[@]}"; do + if [ -f "$PKG/package.json" ]; then + echo "*** Cleaning $PKG" + cd $PKG + + rm -rf build + cargo clean + + cd .. + fi +done + +cd .. diff --git a/scripts/test-package.sh b/scripts/test-package.sh index 3c275a88..a338fd9d 100755 --- a/scripts/test-package.sh +++ b/scripts/test-package.sh @@ -5,7 +5,7 @@ set -e echo "*** Testing package" echo "*** Testing Rust" -cargo test -- --nocapture +cargo test --release echo "*** Testing WASM" node ./test/wasm.js diff --git a/yarn.lock b/yarn.lock index 3a9d03a3..cc9b1aaf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1579,10 +1579,10 @@ typescript "^3.3.4000" vuepress "^1.0.0-alpha.44" -"@polkadot/util@^0.39.0-beta.2": - version "0.39.0-beta.2" - resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.39.0-beta.2.tgz#feaffe90f076ab3d29f35d40ad98ad2befb2109b" - integrity sha512-G3iM+cgAL6OfxSGa2bjL1Nx6/WCm1AXREXGK0mipf1EEz3bbkF5/yF6JZvmMgtroxRlFQtnUW+T5mYkbKK8dcQ== +"@polkadot/util@^0.40.0-beta.1": + version "0.40.0-beta.1" + resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.40.0-beta.1.tgz#3b439cace02b07f7e32abd81b6c958335459c229" + integrity sha512-oJhWnQGfYo9B4B1hqrTexrmUg7WEfym35F8816u9bZLFA3T7JChGS1enbPkzs5CgOyZmFugb2DeRwnlC84KH7w== dependencies: "@babel/runtime" "^7.4.0" "@types/bn.js" "^4.11.4"