-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnemonics_to_keys.ts
41 lines (31 loc) · 1.66 KB
/
mnemonics_to_keys.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
import * as amino_1 from "@cosmjs/amino"
import * as crypto_1 from "@cosmjs/crypto"
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing"
const defaultOptions = {
bip39Password: "",
hdPath: amino_1.makeCosmoshubPath(0),
};
// Return the Private / Public keys pair by taking the mnemonic
export const convertMnemonicToKeyPair = async (mnemonic: string): Promise<{privkey: Uint8Array, pubkey: Uint8Array}> => {
const mnemonicChecked = new crypto_1.EnglishMnemonic(mnemonic);
const seed = await crypto_1.Bip39.mnemonicToSeed(mnemonicChecked, defaultOptions.bip39Password);
const { privkey } = crypto_1.Slip10.derivePath(crypto_1.Slip10Curve.Secp256k1, seed, defaultOptions.hdPath);
const { pubkey } = await crypto_1.Secp256k1.makeKeypair(privkey);
return {
privkey: privkey,
pubkey: crypto_1.Secp256k1.compressPubkey(pubkey),
};
}
// This function allows us to check that the result from convertMnemonicToKeyPair is correct.
// With the same mnemonic, it should return the same public key
export const convertMnemonicToPublicKey = async (mnemonic: string): Promise<Uint8Array> => {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic)
const account = (await wallet.getAccounts())[0]
return account.pubkey
}
export const convertMnemonicToPrivateKey = async (mnemonic: string): Promise<Uint8Array> => {
const mnemonicChecked = new crypto_1.EnglishMnemonic(mnemonic);
const seed = await crypto_1.Bip39.mnemonicToSeed(mnemonicChecked, defaultOptions.bip39Password);
const { privkey } = crypto_1.Slip10.derivePath(crypto_1.Slip10Curve.Secp256k1, seed, defaultOptions.hdPath);
return privkey
}