Browse Source

import pls work

master
peter 2 years ago
parent
commit
66ca68a957
2 changed files with 110 additions and 105 deletions
  1. 109
    104
      hdkey.ts
  2. 1
    1
      package.json

+ 109
- 104
hdkey.ts View File

@@ -1,4 +1,4 @@
1
-const HDKey = require('hdkey');
1
+const hdKey = require('hdkey');
2 2
 const bitcoin = require('bitcoinjs-lib');
3 3
 const bip39 = require('bip39')
4 4
 const assert = require('bsert');
@@ -7,127 +7,132 @@ export
7 7
 type network = 'testnet' | 'mainnet'
8 8
 export
9 9
 type keyType =  "public" | "private"
10
-/**
11
- * Generate a BIP39 menemonic
12
- */
13
-export function  randomMenemonic():string{
14
-    return bip39.generateMnemonic()
15
-}
16
-
17
-/**
18
- * Generate a pseudo-random seed from BIP39 menemonic
19
- * @param menemonic 
20
- */
21
-export function  seedFromMenemonic(menemonic:string):string{
22
-    assert(bip39.validateMnemonic(menemonic))
23
-    return bip39.mnemonicToSeedSync(menemonic).toString('hex')
24
-}
25 10
 
26
-/**
27
- * Derive an extended keypair from seed
28
- * @param seed The seed-string to use for generating the HDKey
29
- * @param net Network to use ("testnet", "mainnet")
30
- * @param n n-th derived keypair default:0
31
- */
32
-export function  deriveAccount(seed:string, net:network = "mainnet", n:number = 0):{publicExtendedKey:string, privateExtendedKey:string}{
33
-    assert(n >= 0)
11
+class HDKey{
12
+    /**
13
+     * Generate a BIP39 menemonic
14
+     */
15
+    randomMenemonic():string{
16
+        return bip39.generateMnemonic()
17
+    }
34 18
 
35
-    let version
36
-    if(net === 'testnet'){
37
-        version = {private: 0x04358394, public: 0x043587CF} // HDKey does not support testnet keys out of the box
19
+    /**
20
+     * Generate a pseudo-random seed from BIP39 menemonic
21
+     * @param menemonic 
22
+     */
23
+    seedFromMenemonic(menemonic:string):string{
24
+        assert(bip39.validateMnemonic(menemonic))
25
+        return bip39.mnemonicToSeedSync(menemonic).toString('hex')
38 26
     }
39
-    let rootKey = HDKey.fromMasterSeed(seed, version)
40
-    let childKey = rootKey.derive('m/'+n+'\'')
41
-    childKey.network = version
42
-    return childKey
43
-}
44 27
 
45
-//doc https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
46
-//lib https://github.com/cryptocoinjs/hdkey
47
-//gen http://bip32.org/
28
+    /**
29
+     * Derive an extended keypair from seed
30
+     * @param seed The seed-string to use for generating the HDKey
31
+     * @param net Network to use ("testnet", "mainnet")
32
+     * @param n n-th derived keypair default:0
33
+     */
34
+    deriveAccount(seed:string, net:network = "mainnet", n:number = 0):{publicExtendedKey:string, privateExtendedKey:string}{
35
+        assert(n >= 0)
48 36
 
49
-// enter bip32  key of the following formats:
50
-// - extended public mainnet key  (xpub prefix) 
51
-// - extended private mainnet key (xprv prefix) (not reccomended)
52
-// - extended public testnet key  (tpub prefix) 
53
-// - extended private testnet key (tprv prefix) (not reccomended)
54
-/**
55
- * 
56
- * @param extendedKey Extended key to generate address from
57
- * @param networkName Network to use ("testnet", "mainnet")
58
- * @param n n-th derived address default:0
59
- */
60
-export function  generateAddress(extendedKey:string, n:number = 0):string{
61
-    assert(n >= 0)
62
-    let version
63
-    let t = checkHDKey(extendedKey)
64
-    if(!t){
65
-        throw new Error("Not an extended key: "+extendedKey)
66
-    }
67
-    if(t.net == "testnet"){
68
-        version = {private: 0x04358394, public: 0x043587CF} //HDKey does not support testnet keys out of the box
37
+        let version
38
+        if(net === 'testnet'){
39
+            version = {private: 0x04358394, public: 0x043587CF} // HDKey does not support testnet keys out of the box
40
+        }
41
+        let rootKey = hdKey.fromMasterSeed(seed, version)
42
+        let childKey = rootKey.derive('m/'+n+'\'')
43
+        childKey.network = version
44
+        return childKey
69 45
     }
70
-    const hdkey = HDKey.fromExtendedKey(extendedKey, version)
71
-    const childkey = hdkey.derive('m/0/'+n)
72
-    const address = bitcoin.payments.p2pkh({ pubkey: childkey._publicKey, network: bitcoin.networks[t.net] }).address!
73
-    return address
74
-}
75 46
 
47
+    //doc https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
48
+    //lib https://github.com/cryptocoinjs/hdkey
49
+    //gen http://bip32.org/
76 50
 
77
-export function  HDKeyFromExtendedKey(extendedKeyB58: string): any | false{
78
-    let hdkey
79
-    let version
80
-    let t = checkHDKey(extendedKeyB58)
81
-    if(!t){
82
-        return false
83
-    }
84
-    if(t.net === 'testnet'){
85
-        version = {private: 0x04358394, public: 0x043587CF} // HDKey does not support testnet keys out of the box
86
-    }
87
-    if(t.type === 'public'){
88
-        hdkey = HDKey.fromExtendedKey(extendedKeyB58, version)
89
-    }else{
90
-        hdkey = HDKey.fromExtendedKey(extendedKeyB58, version)
51
+    // enter bip32  key of the following formats:
52
+    // - extended public mainnet key  (xpub prefix) 
53
+    // - extended private mainnet key (xprv prefix) (not reccomended)
54
+    // - extended public testnet key  (tpub prefix) 
55
+    // - extended private testnet key (tprv prefix) (not reccomended)
56
+    /**
57
+     * 
58
+     * @param extendedKey Extended key to generate address from
59
+     * @param networkName Network to use ("testnet", "mainnet")
60
+     * @param n n-th derived address default:0
61
+     */
62
+    generateAddress(extendedKey:string, n:number = 0):string{
63
+        assert(n >= 0)
64
+        let version
65
+        let t = this.checkHDKey(extendedKey)
66
+        if(!t){
67
+            throw new Error("Not an extended key: "+extendedKey)
68
+        }
69
+        if(t.net == "testnet"){
70
+            version = {private: 0x04358394, public: 0x043587CF} //HDKey does not support testnet keys out of the box
71
+        }
72
+        const hdkey = hdKey.fromExtendedKey(extendedKey, version)
73
+        const childkey = hdkey.derive('m/0/'+n)
74
+        const address = bitcoin.payments.p2pkh({ pubkey: childkey._publicKey, network: bitcoin.networks[t.net] }).address!
75
+        return address
91 76
     }
92
-    return hdkey
93
-}
94 77
 
95
-export function  checkHDKey(key: string): {net: network, type: keyType} | false{
96
-    if(key.startsWith('x')){
97
-        if(key.startsWith('xpub')){
98
-            return {net:'mainnet', type: 'public'}
99
-        }else{
100
-            return {net:'mainnet', type: 'private'}
78
+
79
+    HDKeyFromExtendedKey(extendedKeyB58: string): any | false{
80
+        let hdkey
81
+        let version
82
+        let t = this.checkHDKey(extendedKeyB58)
83
+        if(!t){
84
+            return false
101 85
         }
102
-    }
103
-    if(key.startsWith('t')){
104
-        if(key.startsWith('tpub')){
105
-            return {net:'testnet', type: 'public'}
86
+        if(t.net === 'testnet'){
87
+            version = {private: 0x04358394, public: 0x043587CF} // HDKey does not support testnet keys out of the box
88
+        }
89
+        if(t.type === 'public'){
90
+            hdkey = hdkey.fromExtendedKey(extendedKeyB58, version)
106 91
         }else{
107
-            return {net:'testnet', type: 'private'}
92
+            hdkey = hdkey.fromExtendedKey(extendedKeyB58, version)
108 93
         }
94
+        return hdkey
109 95
     }
110
-    return false
111
-}
112 96
 
113
-export function  deriveAddress(extendedKey: string, n: number, derivationPrefix = 'm/0/'): string | false{
114
-    let t = checkHDKey(extendedKey)
115
-    let childkey = deriveKey(extendedKey, n, derivationPrefix)
116
-    if(!t || !childkey){
97
+    checkHDKey(key: string): {net: network, type: keyType} | false{
98
+        if(key.startsWith('x')){
99
+            if(key.startsWith('xpub')){
100
+                return {net:'mainnet', type: 'public'}
101
+            }else{
102
+                return {net:'mainnet', type: 'private'}
103
+            }
104
+        }
105
+        if(key.startsWith('t')){
106
+            if(key.startsWith('tpub')){
107
+                return {net:'testnet', type: 'public'}
108
+            }else{
109
+                return {net:'testnet', type: 'private'}
110
+            }
111
+        }
117 112
         return false
118 113
     }
119
-    return addressFromHDKey(childkey._publicKey, t.net)
120
-}
121 114
 
122
-export function  deriveKey(extendedKey: string, n: number, derivationPrefix = 'm/0/'): any | false{
123
-    let t = checkHDKey(extendedKey)
124
-    let hdkey = HDKeyFromExtendedKey(extendedKey)
125
-    if(!t || !hdkey){
126
-        return false
115
+    deriveAddress(extendedKey: string, n: number, derivationPrefix = 'm/0/'): string | false{
116
+        let t = this.checkHDKey(extendedKey)
117
+        let childkey = this.deriveKey(extendedKey, n, derivationPrefix)
118
+        if(!t || !childkey){
119
+            return false
120
+        }
121
+        return this.addressFromHDKey(childkey._publicKey, t.net)
122
+    }
123
+
124
+    deriveKey(extendedKey: string, n: number, derivationPrefix = 'm/0/'): any | false{
125
+        let t = this.checkHDKey(extendedKey)
126
+        let hdkey = this.HDKeyFromExtendedKey(extendedKey)
127
+        if(!t || !hdkey){
128
+            return false
129
+        }
130
+        return hdkey.derive(derivationPrefix+n)
131
+    }
132
+
133
+    addressFromHDKey(hdkey:string, net: network = "mainnet"){
134
+        return <string>bitcoin.payments.p2pkh({ pubkey: Buffer.from(hdkey), network: bitcoin.networks[net] }).address
127 135
     }
128
-    return hdkey.derive(derivationPrefix+n)
129 136
 }
130 137
 
131
-export function  addressFromHDKey(hdkey:string, net: network = "mainnet"){
132
-    return <string>bitcoin.payments.p2pkh({ pubkey: Buffer.from(hdkey), network: bitcoin.networks[net] }).address
133
-}
138
+export const hdkey = new HDKey()

+ 1
- 1
package.json View File

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "btc-hdkey",
3
-  "version": "0.0.13",
3
+  "version": "0.0.14",
4 4
   "description": "Hierarchical Deterministic Wallet support for BTC",
5 5
   "main": "hdkey.min.js",
6 6
   "types": "hdkey.d.ts",

Loading…
Cancel
Save