@@ -8,32 +8,39 @@ import {
8
8
} from '@scure/bip39'
9
9
import { bech32 } from 'bech32'
10
10
import { wordlist } from '@scure/bip39/wordlists/english'
11
- import { DERIVATION_PATH , PUBLIC_KEY_PREFIX , SECRET_KEY_PREFIX } from './constants'
11
+ import {
12
+ DERIVATION_PATH ,
13
+ PUBLIC_KEY_PREFIX ,
14
+ SECRET_KEY_PREFIX
15
+ } from './constants'
12
16
13
- type ExtendedKeys = {
14
- privateExtendedKey : string ,
15
- publicExtendedKey : string
16
- }
17
- type PrivateKey = { hex : string , bech32 : string }
18
- type PublicKey = { hex : string , bech32 : string }
19
- type Account = {
20
- privateKey : PrivateKey ,
21
- publicKey : PublicKey
22
- }
17
+ import {
18
+ ExtendedKeys ,
19
+ Key ,
20
+ Account ,
21
+ } from './types'
23
22
24
- export function accountFromSeedWords ( mnemonic : string , passphrase ?: string , accountIndex = 0 ) : Account {
23
+ export function accountFromSeedWords ( {
24
+ mnemonic,
25
+ passphrase,
26
+ accountIndex = 0
27
+ } : {
28
+ mnemonic : string ,
29
+ passphrase ?: string ,
30
+ accountIndex ?: number
31
+ } ) : Account {
25
32
const root = HDKey . fromMasterSeed ( mnemonicToSeedSync ( mnemonic , passphrase ) )
26
33
const seed = root . derive ( `${ DERIVATION_PATH } /${ accountIndex } '/0/0` )
27
34
const privateKeyHex = bytesToHex ( seed . privateKey ! )
28
35
const publicKeyHex = bytesToHex ( seed . publicKey ! . slice ( 1 ) )
29
36
if ( ! privateKeyHex && ! publicKeyHex ) {
30
37
throw new Error ( 'could not derive key pair' )
31
38
}
32
- const privateKeyBech32 = getBech32PrivateKey ( privateKeyHex )
33
- const publicKeyBech32 = getBech32PublicKey ( publicKeyHex )
39
+ const { bech32PrivateKey } = getBech32PrivateKey ( { privateKey : privateKeyHex } )
40
+ const { bech32PublicKey } = getBech32PublicKey ( { publicKey : publicKeyHex } )
34
41
return {
35
- privateKey : { hex : privateKeyHex , bech32 : privateKeyBech32 } ,
36
- publicKey : { hex : publicKeyHex , bech32 : publicKeyBech32 }
42
+ privateKey : { hex : privateKeyHex , bech32 : bech32PrivateKey } ,
43
+ publicKey : { hex : publicKeyHex , bech32 : bech32PublicKey }
37
44
}
38
45
}
39
46
@@ -43,28 +50,36 @@ export function accountFromRandomKey(): Account {
43
50
if ( ! privateKeyHex && ! publicKeyHex ) {
44
51
throw new Error ( 'could not derive key pair' )
45
52
}
46
- const privateKeyBech32 = getBech32PrivateKey ( privateKeyHex )
47
- const publicKeyBech32 = getBech32PublicKey ( publicKeyHex )
53
+ const { bech32PrivateKey } = getBech32PrivateKey ( { privateKey : privateKeyHex } )
54
+ const { bech32PublicKey } = getBech32PublicKey ( { publicKey : publicKeyHex } )
48
55
return {
49
- privateKey : { hex : privateKeyHex , bech32 : privateKeyBech32 } ,
50
- publicKey : { hex : publicKeyHex , bech32 : publicKeyBech32 }
56
+ privateKey : { hex : privateKeyHex , bech32 : bech32PrivateKey } ,
57
+ publicKey : { hex : publicKeyHex , bech32 : bech32PublicKey }
51
58
}
52
59
}
53
60
54
- export function getPublicKey ( privateKey : string ) : {
55
- publicKey : PublicKey
61
+ export function getPublicKey ( { privateKey } : { privateKey : string } ) : {
62
+ publicKey : Key
56
63
} {
57
64
const publicKeyHex = bytesToHex ( schnorr . getPublicKey ( privateKey ) )
58
65
if ( ! publicKeyHex ) {
59
66
throw new Error ( 'could not generate public key' )
60
67
}
61
- const publicKeyBech32 = getBech32PublicKey ( publicKeyHex )
68
+ const { bech32PublicKey } = getBech32PublicKey ( { publicKey : publicKeyHex } )
62
69
return {
63
- publicKey : { hex : publicKeyHex , bech32 : publicKeyBech32 }
70
+ publicKey : { hex : publicKeyHex , bech32 : bech32PublicKey }
64
71
}
65
72
}
66
73
67
- export function extendedKeysFromSeedWords ( mnemonic : string , passphrase ?: string , extendedAccountIndex = 0 ) : ExtendedKeys {
74
+ export function extendedKeysFromSeedWords ( {
75
+ mnemonic,
76
+ passphrase,
77
+ extendedAccountIndex = 0
78
+ } : {
79
+ mnemonic : string ,
80
+ passphrase ?: string ,
81
+ extendedAccountIndex ?: number
82
+ } ) : ExtendedKeys {
68
83
let root = HDKey . fromMasterSeed ( mnemonicToSeedSync ( mnemonic , passphrase ) )
69
84
let seed = root . derive ( `${ DERIVATION_PATH } /${ extendedAccountIndex } '` )
70
85
let privateExtendedKey = seed . privateExtendedKey
@@ -73,39 +88,49 @@ export function extendedKeysFromSeedWords(mnemonic: string, passphrase?: string,
73
88
return { privateExtendedKey, publicExtendedKey }
74
89
}
75
90
76
- export function accountFromExtendedKey ( base58key : string , accountIndex = 0 ) : {
77
- privateKey ?: PrivateKey ,
78
- publicKey : PublicKey
91
+ export function accountFromExtendedKey ( {
92
+ base58Key,
93
+ accountIndex = 0
94
+ } : {
95
+ base58Key : string ,
96
+ accountIndex ?: number
97
+ } ) : {
98
+ privateKey ?: Key ,
99
+ publicKey : Key
79
100
} {
80
- let extendedKey = HDKey . fromExtendedKey ( base58key )
81
- let version = base58key . slice ( 0 , 4 )
101
+ let extendedKey = HDKey . fromExtendedKey ( base58Key )
102
+ let version = base58Key . slice ( 0 , 4 )
82
103
let child = extendedKey . deriveChild ( 0 ) . deriveChild ( accountIndex )
83
104
let publicKeyHex = bytesToHex ( child . publicKey ! . slice ( 1 ) )
84
105
if ( ! publicKeyHex ) throw new Error ( 'could not derive public key' )
85
- let publicKeyBech32 = getBech32PublicKey ( publicKeyHex )
106
+ const { bech32PublicKey } = getBech32PublicKey ( { publicKey : publicKeyHex } )
86
107
if ( version === 'xprv' ) {
87
108
let privateKeyHex = bytesToHex ( child . privateKey ! )
88
109
if ( ! privateKeyHex ) throw new Error ( 'could not derive private key' )
89
- let privateKeyBech32 = getBech32PrivateKey ( privateKeyHex )
110
+ const { bech32PrivateKey } = getBech32PrivateKey ( { privateKey : privateKeyHex } )
90
111
return {
91
- privateKey : { hex : privateKeyHex , bech32 : privateKeyBech32 } ,
92
- publicKey : { hex : publicKeyHex , bech32 : publicKeyBech32 }
112
+ privateKey : { hex : privateKeyHex , bech32 : bech32PrivateKey } ,
113
+ publicKey : { hex : publicKeyHex , bech32 : bech32PublicKey }
93
114
}
94
115
}
95
- return { publicKey : { hex : publicKeyHex , bech32 : publicKeyBech32 } }
116
+ return { publicKey : { hex : publicKeyHex , bech32 : bech32PublicKey } }
96
117
}
97
118
98
119
function hexToBech32 ( key : string , prefix : string ) {
99
120
const words = bech32 . toWords ( hexToBytes ( key ) )
100
121
return bech32 . encode ( prefix , words )
101
122
}
102
123
103
- export function getBech32PrivateKey ( privateKey : string ) : string {
104
- return hexToBech32 ( privateKey , SECRET_KEY_PREFIX )
124
+ export function getBech32PrivateKey ( { privateKey } : { privateKey : string } ) : { bech32PrivateKey : string } {
125
+ return {
126
+ bech32PrivateKey : hexToBech32 ( privateKey , SECRET_KEY_PREFIX )
127
+ }
105
128
}
106
129
107
- export function getBech32PublicKey ( publicKey : string ) : string {
108
- return hexToBech32 ( publicKey , PUBLIC_KEY_PREFIX )
130
+ export function getBech32PublicKey ( { publicKey } : { publicKey : string } ) : { bech32PublicKey : string } {
131
+ return {
132
+ bech32PublicKey : hexToBech32 ( publicKey , PUBLIC_KEY_PREFIX )
133
+ }
109
134
}
110
135
111
136
export function generateSeedWords ( ) : { mnemonic : string } {
@@ -114,7 +139,7 @@ export function generateSeedWords(): { mnemonic: string } {
114
139
}
115
140
}
116
141
117
- export function validateWords ( mnemonic : string ) : { isMnemonicValid : boolean } {
142
+ export function validateWords ( { mnemonic } : { mnemonic : string } ) : { isMnemonicValid : boolean } {
118
143
return {
119
144
isMnemonicValid : validateMnemonic ( mnemonic , wordlist )
120
145
}
0 commit comments