-
Notifications
You must be signed in to change notification settings - Fork 22
/
hdKey.ts
87 lines (71 loc) · 2.87 KB
/
hdKey.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const bip39 = require('bip39');
const hd = require('@sisi/ed25519-blake2b-hd-key');
import { checkParams } from '~@vite/vitejs-utils';
import { Hex } from './type';
export const ROOT_PATH = 'm/44\'/666666\'';
export function createMnemonics(strength: number = 256, wordlist: Array<string> = bip39.wordlists.EN): string {
const err = checkParams({ strength, wordlist }, [ 'strength', 'wordlist' ]);
if (err) {
throw new Error(err.message);
}
return bip39.generateMnemonic(strength, null, wordlist);
}
export function validateMnemonics(mnemonic: string, wordlist: Array<string> = bip39.wordlists.EN): Boolean {
return mnemonic && bip39.validateMnemonic(mnemonic, wordlist);
}
export function getEntropyFromMnemonics(mnemonic: string, wordlist: Array<string> = bip39.wordlists.EN): Hex {
const err = checkParams({ mnemonic, wordlist }, [ 'mnemonic', 'wordlist' ], [{
name: 'mnemonic',
func: _m => validateMnemonics(_m, wordlist)
}]);
if (err) {
throw new Error(err.message);
}
return bip39.mnemonicToEntropy(mnemonic, wordlist);
}
export function getSeedFromMnemonics(mnemonic: string, passphrase: string = '', wordlist: Array<string> = bip39.wordlists.EN): {
seed: Buffer;
seedHex: Hex;
} {
const err = checkParams({ mnemonic, wordlist }, [ 'mnemonic', 'wordlist' ], [{
name: 'mnemonic',
func: _m => validateMnemonics(_m, wordlist)
}]);
if (err) {
throw new Error(err.message);
}
const seed: Buffer = bip39.mnemonicToSeedSync(mnemonic, passphrase);
const seedHex: Hex = seed.toString('hex');
return { seed, seedHex };
}
export function createSeed(strength: number = 256, passphrase: string = '', wordlist: Array<string> = bip39.wordlists.EN): {
mnemonic: string;
seed: Buffer;
seedHex: Hex;
} {
const mnemonic = createMnemonics(strength, wordlist);
const { seed, seedHex } = getSeedFromMnemonics(mnemonic, passphrase, wordlist);
return { mnemonic, seed, seedHex };
}
export function getMnemonicsFromEntropy(entropy: Hex, wordlist: Array<string> = bip39.wordlists.EN): string {
const err = checkParams({ entropy }, ['entropy']);
if (err) {
throw new Error(err.message);
}
return bip39.entropyToMnemonic(entropy, wordlist);
}
export function getPath(index: number): string {
return `${ ROOT_PATH }/${ index }\'`;
}
export function deriveKeyPairByPath(seed: Hex, path: string): { privateKey: Hex; publicKey: Hex; } {
const { key } = hd.derivePath(path, seed);
const { privateKey, publicKey } = hd.getPublicKey(key);
return {
privateKey: Buffer.from(privateKey).toString('hex'),
publicKey: Buffer.from(publicKey).toString('hex')
};
}
export function deriveKeyPairByIndex(seed: Hex, index: number): { privateKey: Hex; publicKey: Hex; } {
const path = getPath(index);
return deriveKeyPairByPath(seed, path);
}