mainnet-js 3.1.7 → 4.0.0-next.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (267) hide show
  1. package/dist/module/cache/IndexedDbCache.d.ts +1 -1
  2. package/dist/module/cache/IndexedDbCache.d.ts.map +1 -1
  3. package/dist/module/cache/MemoryCache.d.ts +1 -1
  4. package/dist/module/cache/MemoryCache.d.ts.map +1 -1
  5. package/dist/module/cache/WebStorageCache.d.ts +1 -1
  6. package/dist/module/cache/WebStorageCache.d.ts.map +1 -1
  7. package/dist/module/cache/walletCache.d.ts +16 -6
  8. package/dist/module/cache/walletCache.d.ts.map +1 -1
  9. package/dist/module/cache/walletCache.js +92 -34
  10. package/dist/module/cache/walletCache.js.map +1 -1
  11. package/dist/module/cli.js +1 -2
  12. package/dist/module/cli.js.map +1 -1
  13. package/dist/module/db/index.d.ts +1 -1
  14. package/dist/module/db/index.d.ts.map +1 -1
  15. package/dist/module/db/index.js +1 -1
  16. package/dist/module/db/index.js.map +1 -1
  17. package/dist/module/enum.d.ts +1 -1
  18. package/dist/module/enum.d.ts.map +1 -1
  19. package/dist/module/history/getHistory.d.ts +1 -1
  20. package/dist/module/history/getHistory.d.ts.map +1 -1
  21. package/dist/module/history/getHistory.js +3 -3
  22. package/dist/module/history/getHistory.js.map +1 -1
  23. package/dist/module/index.d.ts +25 -22
  24. package/dist/module/index.d.ts.map +1 -1
  25. package/dist/module/index.js +33 -26
  26. package/dist/module/index.js.map +1 -1
  27. package/dist/module/interface.d.ts +12 -2
  28. package/dist/module/interface.d.ts.map +1 -1
  29. package/dist/module/interface.js.map +1 -1
  30. package/dist/module/libauth.d.ts +1 -1
  31. package/dist/module/libauth.d.ts.map +1 -1
  32. package/dist/module/libauth.js +1 -1
  33. package/dist/module/libauth.js.map +1 -1
  34. package/dist/module/mine/mine.d.ts +2 -7
  35. package/dist/module/mine/mine.d.ts.map +1 -1
  36. package/dist/module/mine/mine.js +6 -27
  37. package/dist/module/mine/mine.js.map +1 -1
  38. package/dist/module/network/Connection.d.ts +1 -12
  39. package/dist/module/network/Connection.d.ts.map +1 -1
  40. package/dist/module/network/Connection.js +24 -33
  41. package/dist/module/network/Connection.js.map +1 -1
  42. package/dist/module/network/ElectrumNetworkProvider.d.ts +16 -17
  43. package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
  44. package/dist/module/network/ElectrumNetworkProvider.js +91 -93
  45. package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
  46. package/dist/module/network/MockNetworkProvider.d.ts +28 -0
  47. package/dist/module/network/MockNetworkProvider.d.ts.map +1 -0
  48. package/dist/module/network/MockNetworkProvider.js +74 -0
  49. package/dist/module/network/MockNetworkProvider.js.map +1 -0
  50. package/dist/module/network/NetworkProvider.d.ts +9 -2
  51. package/dist/module/network/NetworkProvider.d.ts.map +1 -1
  52. package/dist/module/network/configuration.d.ts +2 -4
  53. package/dist/module/network/configuration.d.ts.map +1 -1
  54. package/dist/module/network/configuration.js +25 -50
  55. package/dist/module/network/configuration.js.map +1 -1
  56. package/dist/module/network/constant.d.ts +7 -7
  57. package/dist/module/network/constant.d.ts.map +1 -1
  58. package/dist/module/network/constant.js +21 -24
  59. package/dist/module/network/constant.js.map +1 -1
  60. package/dist/module/network/default.d.ts +5 -3
  61. package/dist/module/network/default.d.ts.map +1 -1
  62. package/dist/module/network/default.js +29 -51
  63. package/dist/module/network/default.js.map +1 -1
  64. package/dist/module/network/index.d.ts +4 -4
  65. package/dist/module/network/index.d.ts.map +1 -1
  66. package/dist/module/network/index.js +2 -2
  67. package/dist/module/network/index.js.map +1 -1
  68. package/dist/module/network/interface.d.ts +0 -6
  69. package/dist/module/network/interface.d.ts.map +1 -1
  70. package/dist/module/rate/ExchangeRate.d.ts +1 -1
  71. package/dist/module/rate/ExchangeRate.d.ts.map +1 -1
  72. package/dist/module/rate/ExchangeRate.js +13 -7
  73. package/dist/module/rate/ExchangeRate.js.map +1 -1
  74. package/dist/module/transaction/Wif.d.ts +3 -3
  75. package/dist/module/transaction/Wif.d.ts.map +1 -1
  76. package/dist/module/transaction/Wif.js +4 -4
  77. package/dist/module/transaction/Wif.js.map +1 -1
  78. package/dist/module/transaction/allocateFee.d.ts +1 -1
  79. package/dist/module/transaction/allocateFee.d.ts.map +1 -1
  80. package/dist/module/transaction/allocateFee.js +2 -2
  81. package/dist/module/transaction/allocateFee.js.map +1 -1
  82. package/dist/module/util/deriveCashaddr.js.map +1 -1
  83. package/dist/module/util/deriveNetwork.js +1 -1
  84. package/dist/module/util/deriveNetwork.js.map +1 -1
  85. package/dist/module/util/getAddrsByXpubKey.js +1 -1
  86. package/dist/module/util/getAddrsByXpubKey.js.map +1 -1
  87. package/dist/module/util/getXPubKey.js +1 -1
  88. package/dist/module/util/getXPubKey.js.map +1 -1
  89. package/dist/module/util/hd.d.ts +2 -0
  90. package/dist/module/util/hd.d.ts.map +1 -1
  91. package/dist/module/util/hd.js +41 -0
  92. package/dist/module/util/hd.js.map +1 -1
  93. package/dist/module/util/index.d.ts +7 -9
  94. package/dist/module/util/index.d.ts.map +1 -1
  95. package/dist/module/util/index.js +7 -9
  96. package/dist/module/util/index.js.map +1 -1
  97. package/dist/module/util/transaction.js +1 -1
  98. package/dist/module/util/transaction.js.map +1 -1
  99. package/dist/module/wallet/Base.d.ts +13 -9
  100. package/dist/module/wallet/Base.d.ts.map +1 -1
  101. package/dist/module/wallet/Base.js +54 -58
  102. package/dist/module/wallet/Base.js.map +1 -1
  103. package/dist/module/wallet/HDWallet.d.ts.map +1 -1
  104. package/dist/module/wallet/HDWallet.js +19 -38
  105. package/dist/module/wallet/HDWallet.js.map +1 -1
  106. package/dist/module/wallet/Util.d.ts +4 -22
  107. package/dist/module/wallet/Util.d.ts.map +1 -1
  108. package/dist/module/wallet/Util.js +75 -102
  109. package/dist/module/wallet/Util.js.map +1 -1
  110. package/dist/module/wallet/Watch.d.ts +24 -5
  111. package/dist/module/wallet/Watch.d.ts.map +1 -1
  112. package/dist/module/wallet/Watch.js +141 -17
  113. package/dist/module/wallet/Watch.js.map +1 -1
  114. package/dist/module/wallet/Wif.d.ts +2 -6
  115. package/dist/module/wallet/Wif.d.ts.map +1 -1
  116. package/dist/module/wallet/Wif.js +3 -69
  117. package/dist/module/wallet/Wif.js.map +1 -1
  118. package/dist/module/wallet/createWallet.d.ts +1 -1
  119. package/dist/module/wallet/createWallet.d.ts.map +1 -1
  120. package/dist/module/wallet/createWallet.js +2 -1
  121. package/dist/module/wallet/createWallet.js.map +1 -1
  122. package/dist/module/wallet/interface.d.ts +3 -2
  123. package/dist/module/wallet/interface.d.ts.map +1 -1
  124. package/dist/module/wallet/model.d.ts +2 -1
  125. package/dist/module/wallet/model.d.ts.map +1 -1
  126. package/dist/module/wallet/model.js +1 -1
  127. package/dist/module/wallet/model.js.map +1 -1
  128. package/package.json +17 -29
  129. package/dist/index.html +0 -9
  130. package/dist/mainnet-3.1.7.js +0 -2066
  131. package/dist/module/network/util.d.ts +0 -3
  132. package/dist/module/network/util.d.ts.map +0 -1
  133. package/dist/module/network/util.js +0 -27
  134. package/dist/module/network/util.js.map +0 -1
  135. package/dist/module/test/expect.d.ts +0 -12
  136. package/dist/module/test/expect.d.ts.map +0 -1
  137. package/dist/module/test/expect.js +0 -47
  138. package/dist/module/test/expect.js.map +0 -1
  139. package/dist/module/test/fetch.d.ts +0 -3
  140. package/dist/module/test/fetch.d.ts.map +0 -1
  141. package/dist/module/test/fetch.js +0 -32
  142. package/dist/module/test/fetch.js.map +0 -1
  143. package/dist/module/util/randomBytes.d.ts +0 -2
  144. package/dist/module/util/randomBytes.d.ts.map +0 -1
  145. package/dist/module/util/randomBytes.js +0 -13
  146. package/dist/module/util/randomBytes.js.map +0 -1
  147. package/dist/tsconfig.tsbuildinfo +0 -1
  148. package/src/cache/IndexedDbCache.test.ts +0 -15
  149. package/src/cache/IndexedDbCache.ts +0 -172
  150. package/src/cache/MemoryCache.test.ts +0 -15
  151. package/src/cache/MemoryCache.ts +0 -32
  152. package/src/cache/WebStorageCache.test.ts +0 -15
  153. package/src/cache/WebStorageCache.ts +0 -38
  154. package/src/cache/index.ts +0 -2
  155. package/src/cache/interface.ts +0 -9
  156. package/src/cache/walletCache.ts +0 -254
  157. package/src/chain.ts +0 -3
  158. package/src/cli.ts +0 -32
  159. package/src/config.ts +0 -23
  160. package/src/constant.ts +0 -27
  161. package/src/db/ExchangeRateProvider.ts +0 -28
  162. package/src/db/StorageProvider.ts +0 -64
  163. package/src/db/index.ts +0 -2
  164. package/src/db/interface.ts +0 -11
  165. package/src/enum.ts +0 -34
  166. package/src/history/getHistory.test.ts +0 -290
  167. package/src/history/getHistory.ts +0 -411
  168. package/src/history/interface.ts +0 -24
  169. package/src/index.ts +0 -48
  170. package/src/interface.ts +0 -72
  171. package/src/libauth.ts +0 -11
  172. package/src/message/index.ts +0 -2
  173. package/src/message/interface.ts +0 -40
  174. package/src/message/signed.test.ts +0 -309
  175. package/src/message/signed.ts +0 -201
  176. package/src/mine/index.ts +0 -1
  177. package/src/mine/mine.test.ts +0 -10
  178. package/src/mine/mine.ts +0 -42
  179. package/src/network/Connection.test.ts +0 -51
  180. package/src/network/Connection.ts +0 -73
  181. package/src/network/ElectrumNetworkProvider.ts +0 -657
  182. package/src/network/NetworkProvider.ts +0 -180
  183. package/src/network/Rpc.test.ts +0 -130
  184. package/src/network/configuration.test.ts +0 -59
  185. package/src/network/configuration.ts +0 -72
  186. package/src/network/constant.ts +0 -43
  187. package/src/network/default.ts +0 -120
  188. package/src/network/electrum.test.ts +0 -28
  189. package/src/network/getRelayFeeCache.test.ts +0 -15
  190. package/src/network/getRelayFeeCache.ts +0 -23
  191. package/src/network/index.ts +0 -14
  192. package/src/network/interface.ts +0 -80
  193. package/src/network/util.test.ts +0 -24
  194. package/src/network/util.ts +0 -30
  195. package/src/rate/ExchangeRate.test.headless.js +0 -35
  196. package/src/rate/ExchangeRate.test.ts +0 -51
  197. package/src/rate/ExchangeRate.ts +0 -142
  198. package/src/test/expect.ts +0 -59
  199. package/src/test/fetch.ts +0 -39
  200. package/src/test/json.test.ts +0 -13
  201. package/src/transaction/Wif.ts +0 -680
  202. package/src/transaction/allocateFee.test.ts +0 -298
  203. package/src/transaction/allocateFee.ts +0 -149
  204. package/src/util/amountInSatoshi.test.ts +0 -27
  205. package/src/util/amountInSatoshi.ts +0 -33
  206. package/src/util/asSendRequestObject.ts +0 -81
  207. package/src/util/base64.test.ts +0 -39
  208. package/src/util/base64.ts +0 -12
  209. package/src/util/browserNotSupported.ts +0 -7
  210. package/src/util/checkForEmptySeed.ts +0 -9
  211. package/src/util/checkUtxos.ts +0 -29
  212. package/src/util/checkWifNetwork.ts +0 -24
  213. package/src/util/convert.test.ts +0 -46
  214. package/src/util/convert.ts +0 -50
  215. package/src/util/delay.ts +0 -3
  216. package/src/util/deriveCashaddr.test.ts +0 -164
  217. package/src/util/deriveCashaddr.ts +0 -143
  218. package/src/util/deriveLockscript.ts +0 -16
  219. package/src/util/deriveNetwork.ts +0 -19
  220. package/src/util/derivePublicKeyHash.test.ts +0 -55
  221. package/src/util/derivePublicKeyHash.ts +0 -64
  222. package/src/util/floor.test.ts +0 -21
  223. package/src/util/floor.ts +0 -4
  224. package/src/util/getAddrsByXpubKey.test.ts +0 -115
  225. package/src/util/getAddrsByXpubKey.ts +0 -86
  226. package/src/util/getRuntimePlatform.test.headless.js +0 -40
  227. package/src/util/getRuntimePlatform.test.ts +0 -5
  228. package/src/util/getRuntimePlatform.ts +0 -31
  229. package/src/util/getUsdRate.ts +0 -5
  230. package/src/util/getXPubKey.ts +0 -39
  231. package/src/util/hash160.test.ts +0 -18
  232. package/src/util/hash160.ts +0 -12
  233. package/src/util/hd.ts +0 -16
  234. package/src/util/header.test.ts +0 -34
  235. package/src/util/header.ts +0 -26
  236. package/src/util/index.ts +0 -33
  237. package/src/util/randomBytes.ts +0 -13
  238. package/src/util/randomInt.test.ts +0 -15
  239. package/src/util/randomInt.ts +0 -4
  240. package/src/util/sanitizeAddress.ts +0 -10
  241. package/src/util/sanitizeUnit.ts +0 -11
  242. package/src/util/satoshiToAmount.test.ts +0 -6
  243. package/src/util/satoshiToAmount.ts +0 -33
  244. package/src/util/sumSendRequestAmounts.ts +0 -34
  245. package/src/util/sumUtxoValue.ts +0 -27
  246. package/src/util/transaction.ts +0 -10
  247. package/src/wallet/Base.ts +0 -1563
  248. package/src/wallet/Cashtokens.test.headless.js +0 -730
  249. package/src/wallet/Cashtokens.test.ts +0 -1411
  250. package/src/wallet/HDWallet.test.ts +0 -1086
  251. package/src/wallet/HDWallet.ts +0 -992
  252. package/src/wallet/Util.test.ts +0 -134
  253. package/src/wallet/Util.ts +0 -191
  254. package/src/wallet/WalletCache.test.ts +0 -45
  255. package/src/wallet/Watch.ts +0 -441
  256. package/src/wallet/Wif.bip39.test.ts +0 -48
  257. package/src/wallet/Wif.test.ts +0 -1189
  258. package/src/wallet/Wif.ts +0 -687
  259. package/src/wallet/Wif.watchOnly.test.ts +0 -58
  260. package/src/wallet/createWallet.ts +0 -238
  261. package/src/wallet/enum.ts +0 -18
  262. package/src/wallet/interface.ts +0 -102
  263. package/src/wallet/model.test.ts +0 -24
  264. package/src/wallet/model.ts +0 -352
  265. package/tsconfig.browser.json +0 -11
  266. package/tsconfig.json +0 -33
  267. package/webpack.config.cjs +0 -132
package/src/wallet/Wif.ts DELETED
@@ -1,687 +0,0 @@
1
- //#region Imports
2
- import {
3
- assertSuccess,
4
- binToHex,
5
- CashAddressNetworkPrefix,
6
- decodePrivateKeyWif,
7
- deriveHdPath,
8
- deriveHdPrivateNodeFromSeed,
9
- deriveHdPublicNode,
10
- deriveSeedFromBip39Mnemonic,
11
- encodeHdPublicKey,
12
- encodePrivateKeyWif,
13
- generateBip39Mnemonic,
14
- generatePrivateKey,
15
- HdKeyNetwork,
16
- hexToBin,
17
- secp256k1,
18
- } from "@bitauth/libauth";
19
-
20
- import { NetworkType } from "../enum.js";
21
-
22
- import { WalletTypeEnum } from "./enum.js";
23
- import { MnemonicI, SendRequestOptionsI, WalletInfoI } from "./interface.js";
24
-
25
- import {
26
- OpReturnData,
27
- SendRequest,
28
- SendRequestArray,
29
- SendResponse,
30
- SourceOutput,
31
- TokenSendRequest,
32
- XPubKey,
33
- } from "./model.js";
34
-
35
- import { signUnsignedTransaction } from "../transaction/Wif.js";
36
-
37
- import { DERIVATION_PATHS } from "../constant.js";
38
- import {
39
- SignedMessage,
40
- SignedMessageI,
41
- SignedMessageResponseI,
42
- } from "../message/index.js";
43
- import ElectrumNetworkProvider from "../network/ElectrumNetworkProvider.js";
44
- import { checkForEmptySeed } from "../util/checkForEmptySeed.js";
45
- import { checkWifNetwork } from "../util/checkWifNetwork.js";
46
- import { generateRandomBytes } from "../util/randomBytes.js";
47
-
48
- import { Config } from "../config.js";
49
- import { WatchWallet } from "./Watch.js";
50
- //#endregion Imports
51
-
52
- export interface WalletOptions {
53
- name?: string;
54
- mnemonic?: string;
55
- derivationPath?: string;
56
- privateKey?: Uint8Array;
57
- privateKeyWif?: string;
58
- publicKey?: Uint8Array;
59
- publicKeyCompressed?: Uint8Array;
60
- publicKeyHash?: Uint8Array;
61
- address?: string;
62
- }
63
-
64
- /**
65
- * Class to manage a bitcoin cash wallet.
66
- */
67
- export class Wallet extends WatchWallet {
68
- declare readonly provider: ElectrumNetworkProvider;
69
-
70
- readonly derivationPath: string = Config.DefaultParentDerivationPath + "/0/0";
71
- readonly parentDerivationPath: string = Config.DefaultParentDerivationPath;
72
- readonly mnemonic!: string;
73
- readonly parentXPubKey!: string;
74
- readonly privateKey!: Uint8Array;
75
- readonly privateKeyWif!: string;
76
-
77
- declare name: string;
78
-
79
- //#region Constructors and Statics
80
- constructor(
81
- name = "",
82
- network = NetworkType.Mainnet,
83
- walletType = WalletTypeEnum.Seed
84
- ) {
85
- super(name, network);
86
-
87
- this.name = name;
88
- // @ts-ignore
89
- this.walletType = walletType;
90
- }
91
-
92
- /// Initialize the wallet given the options mnemonic, privateKey or publicKey variations
93
- /// If none provided, a new random mnemonic will be generated
94
- /// If mnemonic or private key provided, the wallet will be able to sign transactions
95
- /// Otherwise, the wallet will be watch-only
96
- /// This internal method is called by the various static constructors
97
- protected async initialize({
98
- name = "",
99
- mnemonic = undefined,
100
- derivationPath = undefined,
101
- privateKey = undefined,
102
- privateKeyWif = undefined,
103
- publicKey = undefined,
104
- publicKeyCompressed = undefined,
105
- publicKeyHash = undefined,
106
- address = undefined,
107
- }: WalletOptions = {}) {
108
- // seed wallet
109
- if (this.walletType === WalletTypeEnum.Seed && !mnemonic) {
110
- mnemonic = generateBip39Mnemonic();
111
- }
112
-
113
- if (mnemonic?.length) {
114
- mnemonic = mnemonic.trim().toLowerCase();
115
- if (![12, 24].includes(mnemonic.split(" ").length)) {
116
- throw Error("Invalid mnemonic, must be 12 or 24 words");
117
- }
118
-
119
- if (derivationPath) {
120
- // @ts-ignore
121
- this.derivationPath = derivationPath;
122
-
123
- // If the derivation path is for the first account child, set the parent derivation path
124
- const path = derivationPath.split("/");
125
- if (path.slice(-2).join("/") == "0/0") {
126
- // @ts-ignore
127
- this.parentDerivationPath = path.slice(0, -2).join("/");
128
- }
129
- } else {
130
- derivationPath = Config.DefaultParentDerivationPath + "/0/0";
131
- // @ts-ignore
132
- this.parentDerivationPath = Config.DefaultParentDerivationPath;
133
- }
134
-
135
- // @ts-ignore
136
- this.mnemonic = mnemonic;
137
- // @ts-ignore
138
- this.derivationPath = derivationPath;
139
-
140
- const seed = deriveSeedFromBip39Mnemonic(this.mnemonic);
141
- checkForEmptySeed(seed);
142
-
143
- const rootNode = deriveHdPrivateNodeFromSeed(seed, {
144
- assumeValidity: true,
145
- throwErrors: true,
146
- });
147
- const parentNode = deriveHdPath(rootNode, this.parentDerivationPath);
148
-
149
- // @ts-ignore
150
- this.parentXPubKey = assertSuccess(
151
- encodeHdPublicKey({
152
- node: deriveHdPublicNode(parentNode),
153
- network: this.network === NetworkType.Mainnet ? "mainnet" : "testnet",
154
- })
155
- ).hdPublicKey;
156
-
157
- const childNode = deriveHdPath(rootNode, this.derivationPath);
158
- privateKey = childNode.privateKey;
159
- }
160
-
161
- // privkey wallet
162
- if (this.walletType === WalletTypeEnum.PrivateKey && !privateKey) {
163
- // @ts-ignore
164
- this.privateKey = generatePrivateKey(
165
- () => generateRandomBytes(32) as Uint8Array
166
- );
167
- }
168
-
169
- if (privateKey?.length) {
170
- // @ts-ignore
171
- this.privateKey = privateKey;
172
-
173
- privateKeyWif = encodePrivateKeyWif(
174
- privateKey,
175
- this.network === NetworkType.Regtest
176
- ? NetworkType.Testnet
177
- : this.network
178
- );
179
- }
180
-
181
- // wif wallet
182
- if (this.walletType === WalletTypeEnum.Wif && !privateKeyWif) {
183
- // @ts-ignore
184
- this.privateKey = generatePrivateKey(
185
- () => generateRandomBytes(32) as Uint8Array
186
- );
187
-
188
- privateKeyWif = encodePrivateKeyWif(
189
- this.privateKey,
190
- this.network === NetworkType.Regtest
191
- ? NetworkType.Testnet
192
- : this.network
193
- );
194
- }
195
-
196
- if (privateKeyWif?.length) {
197
- checkWifNetwork(privateKeyWif, this.network);
198
-
199
- // @ts-ignore
200
- this.privateKeyWif = privateKeyWif;
201
-
202
- if (!this.privateKey) {
203
- // @ts-ignore
204
- this.privateKey = assertSuccess(
205
- decodePrivateKeyWif(privateKeyWif)
206
- ).privateKey;
207
- }
208
-
209
- publicKey = assertSuccess(
210
- secp256k1.derivePublicKeyUncompressed(this.privateKey)
211
- );
212
- publicKeyCompressed = assertSuccess(
213
- secp256k1.compressPublicKey(publicKey!)
214
- );
215
- }
216
-
217
- // rest cases are for watch wallets
218
- await super.initialize({
219
- name,
220
- publicKey,
221
- publicKeyCompressed,
222
- publicKeyHash,
223
- address,
224
- });
225
-
226
- if (this.privateKey) {
227
- // @ts-ignore
228
- this.walletCache = new Map<string, { privateKey: Uint8Array }>().set(
229
- this.cashaddr,
230
- { privateKey: this.privateKey }
231
- );
232
- }
233
-
234
- return this;
235
- }
236
- //#endregion Constructors and Statics
237
-
238
- //#region Accessors
239
- // Get mnemonic and derivation path for wallet
240
- public getSeed(): MnemonicI {
241
- if (!this.mnemonic) {
242
- throw Error("Wallet mnemonic seed phrase not set");
243
- }
244
- if (!this.derivationPath) {
245
- throw Error("Wallet derivation path not set");
246
- }
247
- return {
248
- seed: this.mnemonic,
249
- derivationPath: this.derivationPath,
250
- parentDerivationPath: this.parentDerivationPath,
251
- };
252
- }
253
-
254
- // Return wallet info
255
- public getInfo(): WalletInfoI {
256
- return {
257
- cashaddr: this.cashaddr,
258
- tokenaddr: this.tokenaddr,
259
- isTestnet: this.isTestnet,
260
- name: this.name,
261
- network: this.network as any,
262
- seed: this.mnemonic ? this.getSeed().seed : undefined,
263
- derivationPath: this.mnemonic ? this.getSeed().derivationPath : undefined,
264
- parentDerivationPath: this.mnemonic
265
- ? this.getSeed().parentDerivationPath
266
- : undefined,
267
- parentXPubKey: this.parentXPubKey ? this.parentXPubKey : undefined,
268
- publicKey: this.publicKey ? binToHex(this.publicKey) : undefined,
269
- publicKeyHash: binToHex(this.publicKeyHash),
270
- privateKey: this.privateKey ? binToHex(this.privateKey) : undefined,
271
- privateKeyWif: this.privateKeyWif,
272
- walletId: this.toString(),
273
- walletDbEntry: this.toDbString(),
274
- };
275
- }
276
-
277
- // Get common xpub paths from zerothChild privateKey
278
- public async deriveHdPaths(hdPaths: string[]): Promise<any[]> {
279
- if (!this.mnemonic)
280
- throw Error("refusing to create wallet from empty mnemonic");
281
- const seed = deriveSeedFromBip39Mnemonic(this.mnemonic);
282
- checkForEmptySeed(seed);
283
- const hdNode = deriveHdPrivateNodeFromSeed(seed, {
284
- assumeValidity: true, // TODO: we should switch to libauth's BIP39 implementation and set this to false
285
- throwErrors: true,
286
- });
287
-
288
- const result: any[] = [];
289
-
290
- for (const path of hdPaths) {
291
- if (path === "m") {
292
- throw Error(
293
- "Storing or sharing of parent public key may lead to loss of funds. Storing or sharing *root* parent public keys is strongly discouraged, although all parent keys have risk. See: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#implications"
294
- );
295
- }
296
- const childNode = deriveHdPath(hdNode, path);
297
- if (typeof childNode === "string") {
298
- throw Error(childNode);
299
- }
300
- const node = deriveHdPublicNode(childNode);
301
- if (typeof node === "string") {
302
- throw Error(node);
303
- }
304
- const xPubKey = encodeHdPublicKey(
305
- {
306
- network: this.network as HdKeyNetwork,
307
- node: node,
308
- },
309
- {
310
- throwErrors: true,
311
- }
312
- ).hdPublicKey;
313
- const key = new XPubKey({
314
- path: path,
315
- xPubKey: xPubKey,
316
- });
317
-
318
- result.push(await key.ready());
319
- }
320
- return await Promise.all(result).then((result) => {
321
- return result;
322
- });
323
- }
324
-
325
- public async getXPubKeys(paths?: string[]) {
326
- if (this.mnemonic) {
327
- if (paths) {
328
- let xPubKeys = await this.deriveHdPaths(paths);
329
- return [xPubKeys];
330
- } else {
331
- return await this.deriveHdPaths(DERIVATION_PATHS);
332
- }
333
- } else {
334
- throw Error("xpubkeys can only be derived from seed type wallets.");
335
- }
336
- }
337
- //#endregion
338
-
339
- //#region Statics
340
- /**
341
- * fromId - create a wallet from encoded walletId string
342
- *
343
- * @param walletId walletId options to steer the creation process
344
- *
345
- * @returns wallet instantiated accordingly to the walletId rules
346
- */
347
- public static async fromId<T extends typeof Wallet>(
348
- this: T,
349
- walletId: string
350
- ): Promise<InstanceType<T>> {
351
- return new this().fromId(walletId) as InstanceType<T>;
352
- }
353
-
354
- /**
355
- * fromPrivateKey - create a wallet using the private key supplied in hex or Uint8Array
356
- *
357
- * @param wif WIF encoded private key string
358
- *
359
- * @returns instantiated wallet
360
- */
361
- public static async fromPrivateKey<T extends typeof Wallet>(
362
- this: T,
363
- privateKey: string | Uint8Array
364
- ): Promise<InstanceType<T>> {
365
- return new this().fromPrivateKey(privateKey) as InstanceType<T>;
366
- }
367
-
368
- /**
369
- * fromWIF - create a wallet using the private key supplied in `Wallet Import Format`
370
- *
371
- * @param wif WIF encoded private key string
372
- *
373
- * @returns instantiated wallet
374
- */
375
- public static async fromWIF<T extends typeof Wallet>(
376
- this: T,
377
- wif: string
378
- ): Promise<InstanceType<T>> {
379
- return new this().fromWIF(wif) as InstanceType<T>;
380
- }
381
-
382
- /**
383
- * fromSeed - create a wallet using the seed phrase and derivation path
384
- *
385
- * unless specified the derivation path m/44'/0'/0'/0/0 will be used
386
- *
387
- * @param mnemonic BIP39 12 word seed phrase
388
- * @param derivationPath BIP44 HD wallet derivation path to get a single the private key from hierarchy
389
- *
390
- * @returns instantiated wallet
391
- */
392
- public static async fromSeed<T extends typeof Wallet>(
393
- this: T,
394
- mnemonic: string,
395
- derivationPath?: string
396
- ): Promise<InstanceType<T>> {
397
- return new this().fromSeed(mnemonic, derivationPath) as InstanceType<T>;
398
- }
399
-
400
- /**
401
- * newRandom - create a random wallet
402
- *
403
- * if `name` parameter is specified, the wallet will also be persisted to DB
404
- *
405
- * @param name user friendly wallet alias
406
- * @param dbName name under which the wallet will be stored in the database
407
- *
408
- * @returns instantiated wallet
409
- */
410
- public static async newRandom<T extends typeof Wallet>(
411
- this: T,
412
- name: string = "",
413
- dbName?: string
414
- ): Promise<InstanceType<T>> {
415
- return new this().newRandom(name, dbName) as InstanceType<T>;
416
- }
417
- //#endregion Constructors
418
-
419
- //#region Protected implementations
420
- protected fromId = async (walletId: string): Promise<this> => {
421
- const [walletType, networkGiven, arg1, arg2]: string[] =
422
- walletId.split(":");
423
-
424
- if (this.network !== networkGiven) {
425
- throw Error(`Network prefix ${networkGiven} to a ${this.network} wallet`);
426
- }
427
-
428
- // "wif:regtest:cNfsPtqN2bMRS7vH5qd8tR8GMvgXyL5BjnGAKgZ8DYEiCrCCQcP6"
429
- switch (walletType) {
430
- case WalletTypeEnum.PrivateKey:
431
- return this.fromPrivateKey(arg1);
432
-
433
- case WalletTypeEnum.Wif:
434
- return this.fromWIF(arg1);
435
-
436
- case WalletTypeEnum.Watch:
437
- if (arg2) {
438
- // watch:testnet:bchtest:qq1234567
439
- return this.watchOnly(`${arg1}:${arg2}`);
440
- }
441
- // watch:testnet:qq1234567
442
- return this.watchOnly(`${arg1}`);
443
-
444
- case WalletTypeEnum.Named:
445
- if (arg2) {
446
- // named:testnet:wallet_1:my_database
447
- return this.named(arg1, arg2);
448
- } else {
449
- // named:testnet:wallet_1
450
- return this.named(arg1);
451
- }
452
-
453
- case WalletTypeEnum.Seed:
454
- if (arg2) {
455
- // seed:testnet:table later ... stove kitten pluck:m/44'/0'/0'/0/0
456
- return this.fromSeed(arg1, arg2);
457
- }
458
- // seed:testnet:table later ... stove kitten pluck
459
- return this.fromSeed(arg1);
460
-
461
- default:
462
- throw Error(`Unknown wallet type '${walletType}'`);
463
- }
464
- };
465
-
466
- // Initialize wallet from a mnemonic phrase
467
- protected async fromSeed(
468
- mnemonic: string,
469
- derivationPath?: string
470
- ): Promise<this> {
471
- if (!mnemonic.length) {
472
- throw Error("refusing to create wallet from empty mnemonic");
473
- }
474
-
475
- // @ts-ignore
476
- this.walletType = WalletTypeEnum.Seed;
477
-
478
- await this.initialize({ mnemonic, derivationPath });
479
-
480
- return this;
481
- }
482
-
483
- // Initialize wallet from private key in hex or Uint8Array
484
- protected async fromPrivateKey(
485
- privateKey: string | Uint8Array
486
- ): Promise<this> {
487
- if (typeof privateKey === "string") {
488
- privateKey = hexToBin(privateKey);
489
- }
490
-
491
- // @ts-ignore
492
- this.walletType = WalletTypeEnum.PrivateKey;
493
-
494
- await this.initialize({ privateKey });
495
-
496
- return this;
497
- }
498
-
499
- // Initialize wallet from Wallet Import Format
500
- protected async fromWIF(privateKeyWif: string): Promise<this> {
501
- // @ts-ignore
502
- this.walletType = WalletTypeEnum.Wif;
503
-
504
- await this.initialize({ privateKeyWif });
505
-
506
- return this;
507
- }
508
-
509
- /**
510
- * newRandom (internal) if the wallet is named, get or create it; otherwise create a random
511
- * unnamed wallet
512
- * @param {string} name name of the wallet
513
- * @param {string} dbName database name the wallet is stored in
514
- */
515
- protected async newRandom(name: string, dbName?: string): Promise<this> {
516
- dbName = dbName ? dbName : this.networkPrefix;
517
- if (name.length > 0) {
518
- return this.named(name, dbName);
519
- } else {
520
- return this.initialize();
521
- }
522
- }
523
- //#endregion Protected Implementations
524
-
525
- //#region Serialization
526
- // Returns the serialized wallet as a string
527
- // If storing in a database, set asNamed to false to store secrets
528
- // In all other cases, the a named wallet is deserialized from the database
529
- // by the name key
530
- public toString() {
531
- if (this.name) {
532
- return `named:${this.network}:${this.name}`;
533
- } else if (this.walletType == WalletTypeEnum.PrivateKey) {
534
- return `${this.walletType}:${this.network}:${binToHex(this.privateKey)}`;
535
- } else if (this.walletType == WalletTypeEnum.Seed) {
536
- return `${this.walletType}:${this.network}:${this.mnemonic}:${this.derivationPath}`;
537
- } else if (this.walletType === WalletTypeEnum.Wif) {
538
- return `${this.walletType}:${this.network}:${this.privateKeyWif}`;
539
- } else if (this.walletType == WalletTypeEnum.Watch) {
540
- return super.toString();
541
- }
542
-
543
- throw Error("toString unsupported wallet type");
544
- }
545
-
546
- /**
547
- * toDbString - store the serialized version of the wallet in the database, not just the name
548
- *
549
- * @throws {Error} if called on BaseWallet
550
- */
551
- public toDbString() {
552
- if (this.walletType == WalletTypeEnum.Seed) {
553
- return `${this.walletType}:${this.network}:${this.mnemonic}:${this.derivationPath}`;
554
- } else if (this.walletType == WalletTypeEnum.PrivateKey) {
555
- return `${this.walletType}:${this.network}:${binToHex(this.privateKey)}`;
556
- } else if (this.walletType === WalletTypeEnum.Wif) {
557
- return `${this.walletType}:${this.network}:${this.privateKeyWif}`;
558
- } else if (this.walletType == WalletTypeEnum.Watch) {
559
- return super.toDbString();
560
- }
561
-
562
- throw Error("toDbString unsupported wallet type");
563
- }
564
- //#endregion Serialization
565
-
566
- //#region Funds
567
- /**
568
- * sendMax Send all available funds to a destination cash address
569
- *
570
- * @param {string} cashaddr destination cash address
571
- * @param {SendRequestOptionsI} options Options of the send requests
572
- *
573
- * @returns (depending on the options parameter) the transaction id, new address balance and a link to the transaction on the blockchain explorer
574
- */
575
- public async sendMax(
576
- cashaddr: string,
577
- options?: SendRequestOptionsI
578
- ): Promise<SendResponse> {
579
- return this.sendMaxRaw(cashaddr, options, this.privateKey);
580
- }
581
-
582
- /**
583
- * encodeTransaction Encode and sign a transaction given a list of sendRequests, options and estimate fees.
584
- * @param {SendRequest[]} sendRequests SendRequests
585
- * @param {boolean} discardChange=false
586
- * @param {SendRequestOptionsI} options Options of the send requests
587
- */
588
- public async encodeTransaction(
589
- requests:
590
- | SendRequest
591
- | TokenSendRequest
592
- | OpReturnData
593
- | Array<SendRequest | TokenSendRequest | OpReturnData>
594
- | SendRequestArray[],
595
- discardChange: boolean = false,
596
- options?: SendRequestOptionsI,
597
- privateKey?: Uint8Array
598
- ) {
599
- privateKey = privateKey ?? this.privateKey;
600
-
601
- if (!privateKey && options?.buildUnsigned !== true) {
602
- throw new Error(`Missing private key`);
603
- }
604
-
605
- return super.encodeTransaction(
606
- requests,
607
- discardChange,
608
- options,
609
- privateKey
610
- );
611
- }
612
-
613
- public async signUnsignedTransaction(
614
- transaction: Uint8Array | string,
615
- sourceOutputs: SourceOutput[]
616
- ): Promise<Uint8Array> {
617
- if (!this.privateKey) {
618
- throw Error("Can not sign a transaction with watch-only wallet.");
619
- }
620
-
621
- return signUnsignedTransaction(transaction, sourceOutputs, this.privateKey);
622
- }
623
- //#endregion Funds
624
-
625
- //#region Signing
626
- // Convenience wrapper to sign interface
627
- public sign(
628
- message: string,
629
- privateKey: Uint8Array | undefined = undefined
630
- ): SignedMessageResponseI {
631
- return super.sign(message, privateKey ?? this.privateKey);
632
- }
633
- }
634
-
635
- /**
636
- * Class to manage a testnet wallet.
637
- */
638
- export class TestNetWallet extends Wallet {
639
- static networkPrefix = CashAddressNetworkPrefix.testnet;
640
- static faucetServer = "https://rest-unstable.mainnet.cash";
641
- constructor(name = "") {
642
- super(name, NetworkType.Testnet);
643
- }
644
- }
645
-
646
- /**
647
- * Class to manage a regtest wallet.
648
- */
649
- export class RegTestWallet extends Wallet {
650
- static networkPrefix = CashAddressNetworkPrefix.regtest;
651
- constructor(name = "") {
652
- super(name, NetworkType.Regtest);
653
- }
654
- }
655
-
656
- /**
657
- * Class to manage a bitcoin cash wif wallet.
658
- */
659
- export class WifWallet extends Wallet {
660
- static networkPrefix = CashAddressNetworkPrefix.mainnet;
661
- static walletType = WalletTypeEnum.Wif;
662
- constructor(name = "") {
663
- super(name, NetworkType.Mainnet, WalletTypeEnum.Wif);
664
- }
665
- }
666
-
667
- /**
668
- * Class to manage a testnet wif wallet.
669
- */
670
- export class TestNetWifWallet extends Wallet {
671
- static networkPrefix = CashAddressNetworkPrefix.testnet;
672
- static walletType = WalletTypeEnum.Wif;
673
- constructor(name = "") {
674
- super(name, NetworkType.Testnet, WalletTypeEnum.Wif);
675
- }
676
- }
677
-
678
- /**
679
- * Class to manage a regtest wif wallet.
680
- */
681
- export class RegTestWifWallet extends Wallet {
682
- static networkPrefix = CashAddressNetworkPrefix.regtest;
683
- static walletType = WalletTypeEnum.Wif;
684
- constructor(name = "") {
685
- super(name, NetworkType.Regtest, WalletTypeEnum.Wif);
686
- }
687
- }