Browse Source

restrwucture

master
peter 2 years ago
parent
commit
86c29893d9
2 changed files with 73 additions and 26 deletions
  1. 72
    25
      index.ts
  2. 1
    1
      package-lock.json

+ 72
- 25
index.ts View File

@@ -5,7 +5,7 @@ const bip39 = require('bip39')
5 5
 const assert = require('bsert');
6 6
 
7 7
 export type network = 'testnet' | 'mainnet'
8
-
8
+export type keyType =  "public" | "private"
9 9
 /**
10 10
  * Generate a BIP39 menemonic
11 11
  */
@@ -32,29 +32,15 @@ export function deriveAccount(seed:string, net:network = "mainnet", n:number = 0
32 32
     assert(n >= 0)
33 33
 
34 34
     let version
35
-    if(net == "testnet"){
36
-        version = {private: 0x04358394, public: 0x043587CF} //HDKey does not support testnet keys out of the box
35
+    if(net === 'testnet'){
36
+        version = {private: 0x04358394, public: 0x043587CF} // HDKey does not support testnet keys out of the box
37 37
     }
38
-    var rootKey = HDKey.fromMasterSeed(seed, version)
39
-    let childKey = rootKey.derive("m/"+n+"'")
38
+    let rootKey = HDKey.fromMasterSeed(seed, version)
39
+    let childKey = rootKey.derive('m/'+n+'\'')
40
+    childKey.network = version
40 41
     return childKey
41 42
 }
42 43
 
43
-function checkBTCHDKey(key:string, nw:network){
44
-    switch(nw){
45
-        case "mainnet":
46
-            assert(key.startsWith('x'))
47
-            if(key.startsWith('xprv'))
48
-                console.log("WARNING Use of extended private keys as generator is discouraged")
49
-        break
50
-        case "testnet":
51
-            assert(key.startsWith('t'))
52
-            if(key.startsWith('tprv'))
53
-                console.log("WARNING Use of extended private keys as generator is discouraged")
54
-        break
55
-    }
56
-}
57
-
58 44
 //doc https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
59 45
 //lib https://github.com/cryptocoinjs/hdkey
60 46
 //gen http://bip32.org/
@@ -70,16 +56,77 @@ function checkBTCHDKey(key:string, nw:network){
70 56
  * @param networkName Network to use ("testnet", "mainnet")
71 57
  * @param n n-th derived address default:0
72 58
  */
73
-export function generateAddress(extendedKey:string, networkName:network = "mainnet", n:number = 0):string{
59
+export function generateAddress(extendedKey:string, n:number = 0):string{
74 60
     assert(n >= 0)
75 61
     let version
76
-    checkBTCHDKey(extendedKey, networkName)
77
-    if(networkName == "testnet"){
62
+    let t = checkHDKey(extendedKey)
63
+    if(!t){
64
+        throw new Error("Not an extended key: "+extendedKey)
65
+    }
66
+    if(t.net == "testnet"){
78 67
         version = {private: 0x04358394, public: 0x043587CF} //HDKey does not support testnet keys out of the box
79 68
     }
80 69
     const hdkey = HDKey.fromExtendedKey(extendedKey, version)
81 70
     const childkey = hdkey.derive('m/0/'+n)
82
-    const address = bitcoin.payments.p2pkh({ pubkey: childkey._publicKey, network: bitcoin.networks[networkName] }).address!
71
+    const address = bitcoin.payments.p2pkh({ pubkey: childkey._publicKey, network: bitcoin.networks[t.net] }).address!
83 72
     return address
84
-    
73
+}
74
+
75
+
76
+export function HDKeyFromExtendedKey(extendedKeyB58: string): any | false{
77
+    let hdkey
78
+    let version
79
+    let t = checkHDKey(extendedKeyB58)
80
+    if(!t){
81
+        return false
82
+    }
83
+    if(t.net === 'testnet'){
84
+        version = {private: 0x04358394, public: 0x043587CF} // HDKey does not support testnet keys out of the box
85
+    }
86
+    if(t.type === 'public'){
87
+        hdkey = HDKey.fromExtendedKey(extendedKeyB58, version)
88
+    }else{
89
+        hdkey = HDKey.fromExtendedKey(extendedKeyB58, version)
90
+    }
91
+    return hdkey
92
+}
93
+
94
+export function checkHDKey(key: string): {net: network, type: keyType} | false{
95
+    if(key.startsWith('x')){
96
+        if(key.startsWith('xpub')){
97
+            return {net:'mainnet', type: 'public'}
98
+        }else{
99
+            return {net:'mainnet', type: 'private'}
100
+        }
101
+    }
102
+    if(key.startsWith('t')){
103
+        if(key.startsWith('tpub')){
104
+            return {net:'testnet', type: 'public'}
105
+        }else{
106
+            return {net:'testnet', type: 'private'}
107
+        }
108
+    }
109
+    return false
110
+}
111
+
112
+export function deriveAddress(extendedKey: string, n: number, derivationPrefix = 'm/0/'): string | false{
113
+    let t = checkHDKey(extendedKey)
114
+    let childkey = deriveKey(extendedKey, n, derivationPrefix)
115
+    if(!t || !childkey){
116
+        return false
117
+    }
118
+    return addressFromHDKey(childkey._publicKey, t.net)
119
+}
120
+
121
+export function deriveKey(extendedKey: string, n: number, derivationPrefix = 'm/0/'): any | false{
122
+    let t = checkHDKey(extendedKey)
123
+    let hdkey = HDKeyFromExtendedKey(extendedKey)
124
+    if(!t || !hdkey){
125
+        return false
126
+    }
127
+    return hdkey.derive(derivationPrefix+n)
128
+}
129
+
130
+export function addressFromHDKey(hdkey:string, net: network = "mainnet"){
131
+    return <string>bitcoin.payments.p2pkh({ pubkey: Buffer.from(hdkey), network: bitcoin.networks[net] }).address
85 132
 }

+ 1
- 1
package-lock.json View File

@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "btc-hdkey",
3
-  "version": "1.0.0",
3
+  "version": "0.0.1",
4 4
   "lockfileVersion": 1,
5 5
   "requires": true,
6 6
   "dependencies": {

Loading…
Cancel
Save