|
@@ -8,7 +8,7 @@ export type keyType = "public" | "private"
|
8
|
8
|
/**
|
9
|
9
|
* Generate a BIP39 menemonic
|
10
|
10
|
*/
|
11
|
|
-function randomMenemonic():string{
|
|
11
|
+export function randomMenemonic():string{
|
12
|
12
|
return bip39.generateMnemonic()
|
13
|
13
|
}
|
14
|
14
|
|
|
@@ -16,7 +16,7 @@ function randomMenemonic():string{
|
16
|
16
|
* Generate a pseudo-random seed from BIP39 menemonic
|
17
|
17
|
* @param menemonic
|
18
|
18
|
*/
|
19
|
|
-function seedFromMenemonic(menemonic:string):string{
|
|
19
|
+export function seedFromMenemonic(menemonic:string):string{
|
20
|
20
|
assert(bip39.validateMnemonic(menemonic))
|
21
|
21
|
return bip39.mnemonicToSeedSync(menemonic).toString('hex')
|
22
|
22
|
}
|
|
@@ -27,7 +27,7 @@ function seedFromMenemonic(menemonic:string):string{
|
27
|
27
|
* @param net Network to use ("testnet", "mainnet")
|
28
|
28
|
* @param n n-th derived keypair default:0
|
29
|
29
|
*/
|
30
|
|
-function deriveAccount(seed:string, net:network = "mainnet", n:number = 0):{publicExtendedKey:string, privateExtendedKey:string}{
|
|
30
|
+export function deriveAccount(seed:string, net:network = "mainnet", n:number = 0):{publicExtendedKey:string, privateExtendedKey:string}{
|
31
|
31
|
assert(n >= 0)
|
32
|
32
|
|
33
|
33
|
let version
|
|
@@ -55,7 +55,7 @@ function deriveAccount(seed:string, net:network = "mainnet", n:number = 0):{publ
|
55
|
55
|
* @param networkName Network to use ("testnet", "mainnet")
|
56
|
56
|
* @param n n-th derived address default:0
|
57
|
57
|
*/
|
58
|
|
-function generateAddress(extendedKey:string, n:number = 0):string{
|
|
58
|
+export function generateAddress(extendedKey:string, n:number = 0):string{
|
59
|
59
|
assert(n >= 0)
|
60
|
60
|
let version
|
61
|
61
|
let t = checkHDKey(extendedKey)
|
|
@@ -72,7 +72,7 @@ function generateAddress(extendedKey:string, n:number = 0):string{
|
72
|
72
|
}
|
73
|
73
|
|
74
|
74
|
|
75
|
|
-function HDKeyFromExtendedKey(extendedKeyB58: string): any | false{
|
|
75
|
+export function HDKeyFromExtendedKey(extendedKeyB58: string): any | false{
|
76
|
76
|
let hdkey
|
77
|
77
|
let version
|
78
|
78
|
let t = checkHDKey(extendedKeyB58)
|
|
@@ -90,7 +90,7 @@ function HDKeyFromExtendedKey(extendedKeyB58: string): any | false{
|
90
|
90
|
return hdkey
|
91
|
91
|
}
|
92
|
92
|
|
93
|
|
-function checkHDKey(key: string): {net: network, type: keyType} | false{
|
|
93
|
+export function checkHDKey(key: string): {net: network, type: keyType} | false{
|
94
|
94
|
if(key.startsWith('x')){
|
95
|
95
|
if(key.startsWith('xpub')){
|
96
|
96
|
return {net:'mainnet', type: 'public'}
|
|
@@ -108,7 +108,7 @@ function checkHDKey(key: string): {net: network, type: keyType} | false{
|
108
|
108
|
return false
|
109
|
109
|
}
|
110
|
110
|
|
111
|
|
-function deriveAddress(extendedKey: string, n: number, derivationPrefix = 'm/0/'): string | false{
|
|
111
|
+export function deriveAddress(extendedKey: string, n: number, derivationPrefix = 'm/0/'): string | false{
|
112
|
112
|
let t = checkHDKey(extendedKey)
|
113
|
113
|
let childkey = deriveKey(extendedKey, n, derivationPrefix)
|
114
|
114
|
if(!t || !childkey){
|
|
@@ -117,7 +117,7 @@ function deriveAddress(extendedKey: string, n: number, derivationPrefix = 'm/0/'
|
117
|
117
|
return addressFromHDKey(childkey._publicKey, t.net)
|
118
|
118
|
}
|
119
|
119
|
|
120
|
|
-function deriveKey(extendedKey: string, n: number, derivationPrefix = 'm/0/'): any | false{
|
|
120
|
+export function deriveKey(extendedKey: string, n: number, derivationPrefix = 'm/0/'): any | false{
|
121
|
121
|
let t = checkHDKey(extendedKey)
|
122
|
122
|
let hdkey = HDKeyFromExtendedKey(extendedKey)
|
123
|
123
|
if(!t || !hdkey){
|
|
@@ -126,7 +126,7 @@ function deriveKey(extendedKey: string, n: number, derivationPrefix = 'm/0/'): a
|
126
|
126
|
return hdkey.derive(derivationPrefix+n)
|
127
|
127
|
}
|
128
|
128
|
|
129
|
|
-function addressFromHDKey(hdkey:string, net: network = "mainnet"){
|
|
129
|
+export function addressFromHDKey(hdkey:string, net: network = "mainnet"){
|
130
|
130
|
return <string>bitcoin.payments.p2pkh({ pubkey: Buffer.from(hdkey), network: bitcoin.networks[net] }).address
|
131
|
131
|
}
|
132
|
132
|
|