Add bip39ToSeed, add wasm-crypto (simple) usage (#10)

This commit is contained in:
Jaco Greeff
2019-03-23 17:51:39 +01:00
committed by GitHub
parent 51270660da
commit 44c0553cae
12 changed files with 77 additions and 12 deletions
+4
View File
@@ -1,3 +1,7 @@
# 0.4.1
- Add bip39ToSeed
# 0.3.1
- Add wasm-dalek-ed25519 for all used crypto there
+1 -1
View File
@@ -9,5 +9,5 @@
"packages": [
"packages/*"
],
"version": "0.3.1"
"version": "0.4.0"
}
+1 -1
View File
@@ -22,6 +22,6 @@
"devDependencies": {
"@babel/core": "^7.4.0",
"@polkadot/dev": "^0.28.1",
"@polkadot/util": "^0.39.1"
"@polkadot/util": "^0.40.1"
}
}
+28
View File
@@ -1,3 +1,31 @@
# @polkadot/wasm-crypto
Wrapper around crypto hashing functions
## Usage
Install the package (also requires `@polkadot/util` for `TextEncoder` polyfills - not included here as a dependency to keep the tree lean)
`yarn add @polkadot/wasm-crypto @polkadot/util`
Use it -
```js
const { u8aToHex } = require('@polkadot/util');
const { bio39Generate, bip39ToSeed, waitReady } = require('@polkadot/wasm-crypto');
async function main () {
// first wait until the WASM has been loaded (async init)
await waitReady();
// generate phrase
const phrase = bip39Generate();
// get ed25519 seed from phrase
const seed = bip39ToSeed(phrase);
// display
console.log('phrase:', phrase);
console.log('seed:', u8aToHex(seed));
}
```
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/wasm-crypto",
"version": "0.3.1",
"version": "0.4.0",
"author": "Jaco Greeff <jacogr@gmail.com>",
"files": [
"crypto-polyfill.js",
+1
View File
@@ -5,6 +5,7 @@
export function bip39Generate (words: 12 | 15 | 18 | 21 | 24): string;
export function bip39ToEntropy (phrase: string): Uint8Array;
export function bip39ToMiniSecret (phrase: string, password: string): Uint8Array;
export function bip39ToSeed (phrase: string): Uint8Array;
export function bip39Validate (phrase: string): boolean;
export function blake2b (data: Uint8Array, key: Uint8Array, byteSize: number): Uint8Array;
export function keccak256 (data: Uint8Array): Uint8Array;
+1
View File
@@ -7,6 +7,7 @@ const stubbed = require('./wasm');
module.exports.bip39Generate = stubbed.ext_bip39_generate;
module.exports.bip39ToEntropy = stubbed.ext_bip39_to_entropy;
module.exports.bip39ToMiniSecret = stubbed.ext_bip39_to_mini_secret;
module.exports.bip39ToSeed = stubbed.ext_bip39_to_seed;
module.exports.bip39Validate = stubbed.ext_bip39_validate
module.exports.blake2b = stubbed.ext_blake2b;
module.exports.keccak256 = stubbed.ext_keccak256;
+25 -3
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
use bip39::{Mnemonic, MnemonicType, Language};
use bip39::{Mnemonic, MnemonicType, Language, Seed};
use blake2_rfc::blake2b::blake2b;
use byteorder::{ByteOrder, LittleEndian};
use hmac::Hmac;
@@ -64,14 +64,27 @@ pub fn ext_bip39_to_entropy(phrase: &str) -> Vec<u8> {
#[wasm_bindgen]
pub fn ext_bip39_to_mini_secret(phrase: &str, password: &str) -> Vec<u8> {
let salt = format!("mnemonic{}", password);
let mnemonic = Mnemonic::from_phrase(phrase, Language::English);
let mnemonic = Mnemonic::from_phrase(phrase, Language::English).unwrap();
let mut result = [0u8; 64];
pbkdf2::<Hmac<Sha512>>(mnemonic.unwrap().entropy(), salt.as_bytes(), 2048, &mut result);
pbkdf2::<Hmac<Sha512>>(mnemonic.entropy(), salt.as_bytes(), 2048, &mut result);
result[..32].to_vec()
}
/// Creates a see from a bip-39 phrase
///
/// @phrase: mnemonic phrase
///
/// Returns a 32-byte seed
#[wasm_bindgen]
pub fn ext_bip39_to_seed(phrase: &str) -> Vec<u8> {
let mnemonic = Mnemonic::from_phrase(phrase, Language::English).unwrap();
Seed::new(&mnemonic, "")
.as_bytes()[..32]
.to_vec()
}
/// Validates a bip39 phrase
///
/// * phrase: mnemonic phrase
@@ -256,6 +269,15 @@ pub mod tests {
assert_eq!(result[..], mini[..]);
}
#[test]
fn can_bip39_seed() {
let phrase = "seed sock milk update focus rotate barely fade car face mechanic mercy";
let seed = hex!("3c121e20de068083b49c2315697fb59a2d9e8643c24e5ea7628132c58969a027");
let result = ext_bip39_to_seed(phrase);
assert_eq!(result[..], seed[..]);
}
#[test]
fn can_bip39_generate() {
let phrase = ext_bip39_generate(12);
+9
View File
@@ -38,6 +38,14 @@ function bip39ToMiniSecret () {
assert(RESULT === '0x4313249608fe8ac10fd5886c92c4579007272cb77c21551ee5b8d60b78041685', 'ERROR: Invalid bip39 mini secret');
}
function bip39ToSeed () {
const RESULT = u8aToHex(crypto.bip39ToSeed('seed sock milk update focus rotate barely fade car face mechanic mercy'));
console.log('\SEE', RESULT);
assert(RESULT === '0x3c121e20de068083b49c2315697fb59a2d9e8643c24e5ea7628132c58969a027', 'ERROR: Invalid bip39 mini secret');
}
function blake2bHash () {
const hash = u8aToHex(crypto.blake2b(stringToU8a('abc'), new Uint8Array(), 64));
@@ -96,6 +104,7 @@ const tests = {
bip39Validate,
bip39ToEntropy,
bip39ToMiniSecret,
bip39ToSeed,
blake2bHash,
keccak256,
pbkdf2Hash,
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/wasm-dalek-ed25519",
"version": "0.3.1",
"version": "0.4.0",
"author": "Jaco Greeff <jacogr@gmail.com>",
"files": [
"index.d.ts",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/wasm-schnorrkel",
"version": "0.3.1",
"version": "0.4.0",
"collaborators": [
"Jaco Greeff <jacogr@gmail.com>",
"kianenigma <Kian.peymani@gmail.com>"
+4 -4
View File
@@ -1579,10 +1579,10 @@
typescript "^3.3.4000"
vuepress "^1.0.0-alpha.44"
"@polkadot/util@^0.39.1":
version "0.39.1"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.39.1.tgz#553155a0610e879abf51210a4f99af9c1e3ce25b"
integrity sha512-Ks6uR6J8CBmbp/aSgM5OnfUR76tft2yTNMHNV/05BRzZN8BONG211REnNFlvy+/BU2igUNvTIhDo9GnHoR6Hhw==
"@polkadot/util@^0.40.1":
version "0.40.1"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.40.1.tgz#4a990d134ea394425e9b1ceb8a7f1fc0868a5a66"
integrity sha512-S9owTDNIB7sNFzon4lK/c9A+6O73XcoCPAIYA3TtNg494qA1X8exesenVJgVFzqht/Z3B/RAXLsEa8DSk9BycQ==
dependencies:
"@babel/runtime" "^7.4.0"
"@types/bn.js" "^4.11.4"