Comparing version 0.2.3 to 0.2.5
/// <reference types="node" /> | ||
export declare type Pbkdf2SyncFunction = (password: string | Buffer, salt: string | Buffer, iterations: number, keylen: number, digest: string) => Buffer; | ||
export declare type Pbkdf2Function = (password: string | Buffer, salt: string | Buffer, iterations: number, keylen: number, digest: string, callback: (err: Error | null, derivedKey: Buffer | null) => void) => void; | ||
export default class Mnemonic { | ||
static pbkdf2Sync: Pbkdf2SyncFunction; | ||
static pbkdf2: Pbkdf2Function; | ||
private _entropy; | ||
@@ -13,2 +17,3 @@ private _words; | ||
toSeed(passphrase?: string): Buffer; | ||
toSeedAsync(passphrase?: string): Promise<Buffer>; | ||
} |
@@ -78,6 +78,20 @@ 'use strict'; | ||
const salt = `mnemonic${passphrase}`; | ||
return _crypto2.default.pbkdf2Sync(this.phrase.normalize('NFKD'), salt.normalize('NFKD'), 2048, 64, 'sha512'); | ||
return Mnemonic.pbkdf2Sync(this.phrase.normalize('NFKD'), salt.normalize('NFKD'), 2048, 64, 'sha512'); | ||
} | ||
toSeedAsync(passphrase = '') { | ||
const salt = `mnemonic${passphrase}`; | ||
return new Promise((resolve, reject) => { | ||
Mnemonic.pbkdf2(this.phrase.normalize('NFKD'), salt.normalize('NFKD'), 2048, 64, 'sha512', (err, key) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(key); | ||
}); | ||
}); | ||
} | ||
} | ||
exports.default = Mnemonic; | ||
Mnemonic.pbkdf2Sync = _crypto2.default.pbkdf2Sync; | ||
Mnemonic.pbkdf2 = _crypto2.default.pbkdf2; | ||
function uint11ToBitArray(n) { | ||
@@ -84,0 +98,0 @@ return [Math.min(n & 1024, 1), Math.min(n & 512, 1), Math.min(n & 256, 1), Math.min(n & 128, 1), Math.min(n & 64, 1), Math.min(n & 32, 1), Math.min(n & 16, 1), Math.min(n & 8, 1), Math.min(n & 4, 1), Math.min(n & 2, 1), Math.min(n & 1, 1)]; |
@@ -9,2 +9,27 @@ 'use strict'; | ||
var __awaiter = undefined && undefined.__awaiter || function (thisArg, _arguments, P, generator) { | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { | ||
try { | ||
step(generator.next(value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function rejected(value) { | ||
try { | ||
step(generator["throw"](value)); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
} | ||
function step(result) { | ||
result.done ? resolve(result.value) : new P(function (resolve) { | ||
resolve(result.value); | ||
}).then(fulfilled, rejected); | ||
} | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
describe('generate', () => { | ||
@@ -74,9 +99,22 @@ test('generates mnemonic phrase from entropy', () => { | ||
const mnemonic = _2.default.parse(phrase); | ||
if (mnemonic) { | ||
expect(mnemonic.toSeed().toString('hex')).toBe(seed); | ||
} else { | ||
expect(mnemonic).not.toBe(null); | ||
} | ||
expect(mnemonic.toSeed().toString('hex')).toBe(seed); | ||
}); | ||
}); | ||
}); | ||
describe('toSeedAsync', () => { | ||
test('returns a binary seed derived from a mnemonic phrase', () => __awaiter(undefined, void 0, void 0, function* () { | ||
const testCases = { | ||
'essay wasp gain consider media wage wave sick bachelor knock observe undo': 'b7df235f1e8addda6befbdf66f4df613474e8ff6041c7826e4df7fa68aa8c244a1d687eda050f97fc20fc2fcd8c09e19ef21d6c14f523639b033e9fc4e6375a6', | ||
'rival admit primary wing salt round prevent town measure void belt almost': '267d6fe81adc779fd2e875d62739cc67690af67025bca8fc6b1f5f3228fb312a1a9b10dedbb3cffc62730438f5afc8725dac6ee11fcd319b98611863226a2957', | ||
'express woman recall bike quit chicken cloud empty file sword tobacco rib': 'c35ecec5b1986cd3a1407bc3c829610eb5fc9497e59f31c151a5ce422c7ff7e68bdf3343343605a53db8e7376a932a74b3e08296c0b51476f3d288b750089d9d' | ||
}; | ||
const keys = Object.keys(testCases); | ||
for (let i = 0; i < keys.length; i++) { | ||
const phrase = keys[i]; | ||
const seed = testCases[phrase]; | ||
const mnemonic = _2.default.parse(phrase); | ||
const derivedSeed = yield mnemonic.toSeedAsync(); | ||
expect(derivedSeed.toString('hex')).toBe(seed); | ||
} | ||
})); | ||
}); |
{ | ||
"name": "wallet.ts", | ||
"version": "0.2.3", | ||
"version": "0.2.5", | ||
"description": "Utilities for cryptocurrency wallets, written in TypeScript", | ||
@@ -5,0 +5,0 @@ "keywords": ["crypto", "cryptocurrency", "bip32", "bip39", "bip44", "hdkey", "mnemonic", "bitcoin", "ethereum", "litecoin", "eip55", "erc20", "wallet", "typescript"], |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
195722
1247
32