@@ -17,7 +17,7 @@ export function privateKeyFromSeedWords(
17
17
} : { mnemonic : string , passphrase ?: string }
18
18
) : { privateKey : string } {
19
19
const root = HDKey . fromMasterSeed ( mnemonicToSeedSync ( mnemonic , passphrase ) )
20
- const { privateKey } = root . derive ( DERIVATION_PATH )
20
+ const { privateKey } = root . derive ( ` ${ DERIVATION_PATH } /0'/0/0` )
21
21
if ( ! privateKey ) {
22
22
throw new Error ( 'could not derive private key' )
23
23
}
@@ -55,6 +55,40 @@ export function getBech32PublicKey({ publicKey }: { publicKey: string }): { bech
55
55
}
56
56
}
57
57
58
+ export function extendedPairFromSeedWords ( mnemonic : string , passphrase ?: string , extendedAccountIndex = 0 ) : {
59
+ privateExtendedKey : string ,
60
+ publicExtendedKey : string
61
+ } {
62
+ let root = HDKey . fromMasterSeed ( mnemonicToSeedSync ( mnemonic , passphrase ) )
63
+ let seed = root . derive ( `${ DERIVATION_PATH } /${ extendedAccountIndex } '` )
64
+ let privateExtendedKey = seed . privateExtendedKey
65
+ let publicExtendedKey = seed . publicExtendedKey
66
+ if ( ! privateExtendedKey ) throw new Error ( 'could not derive private extended key' )
67
+ return { privateExtendedKey, publicExtendedKey }
68
+ }
69
+
70
+ export function accountFromExtendedKey ( base58key : string , accountIndex = 0 ) : {
71
+ privateKey ?: { hex : string , bech32 : string } ,
72
+ publicKey : { hex : string , bech32 : string }
73
+ } {
74
+ let extendedKey = HDKey . fromExtendedKey ( base58key )
75
+ let version = base58key . slice ( 0 , 4 )
76
+ let child = extendedKey . deriveChild ( 0 ) . deriveChild ( accountIndex )
77
+ let publicKeyHex = bytesToHex ( child . publicKey ! . slice ( 1 ) )
78
+ if ( ! publicKeyHex ) throw new Error ( 'could not derive public key' )
79
+ let publicKeyBech32 = hexToBech32 ( publicKeyHex , 'npub' )
80
+ if ( version === 'xprv' ) {
81
+ let privateKeyHex = bytesToHex ( child . privateKey ! )
82
+ if ( ! privateKeyHex ) throw new Error ( 'could not derive private key' )
83
+ let privateKeyBech32 = hexToBech32 ( privateKeyHex , 'nsec' )
84
+ return {
85
+ privateKey : { hex : privateKeyHex , bech32 : privateKeyBech32 } ,
86
+ publicKey : { hex : publicKeyHex , bech32 : publicKeyBech32 }
87
+ }
88
+ }
89
+ return { publicKey : { hex : publicKeyHex , bech32 : publicKeyBech32 } }
90
+ }
91
+
58
92
export function generateSeedWords ( ) : { mnemonic : string } {
59
93
return {
60
94
mnemonic : generateMnemonic ( wordlist )
0 commit comments