Browse Source

import pls work

master
peter 2 years ago
parent
commit
2b0eb02a1f
3 changed files with 14 additions and 23 deletions
  1. 12
    21
      hdkey.ts
  2. 1
    1
      package.json
  3. 1
    1
      tsconfig.json

+ 12
- 21
hdkey.ts View File

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

+ 1
- 1
package.json View File

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

+ 1
- 1
tsconfig.json View File

@@ -1,7 +1,7 @@
1 1
 {
2 2
     "compilerOptions": {
3 3
       "noImplicitAny": false,
4
-      "target": "esnext",
4
+      "target": "ESNext",
5 5
       "module": "commonjs",
6 6
       "declaration": true,
7 7
       "strict": true,

Loading…
Cancel
Save