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
@@ -1,441 +0,0 @@
1
- import {
2
- binToHex,
3
- CashAddressNetworkPrefix,
4
- decodeCashAddress,
5
- secp256k1,
6
- encodeCashAddress,
7
- assertSuccess,
8
- hash160,
9
- decodeCashAddressFormatWithoutPrefix,
10
- } from "@bitauth/libauth";
11
- import {
12
- networkPrefixMap,
13
- NetworkType,
14
- prefixFromNetworkMap,
15
- UnitEnum,
16
- } from "../enum.js";
17
- import { BaseWallet } from "./Base.js";
18
- import { WalletTypeEnum } from "./enum.js";
19
- import { DUST_UTXO_THRESHOLD } from "../constant.js";
20
- import { TxI, Utxo } from "../interface.js";
21
- import { derivePrefix } from "../util/derivePublicKeyHash.js";
22
- import { SignedMessage } from "../message/signed.js";
23
- import { VerifyMessageResponseI } from "../message/interface.js";
24
- import { TransactionHistoryItem } from "../history/interface.js";
25
- import { getHistory } from "../history/getHistory.js";
26
- import { WalletInfoI } from "./interface.js";
27
- import { toCashaddr, toTokenaddr } from "../util/deriveCashaddr.js";
28
-
29
- export interface WatchWalletOptions {
30
- name?: string;
31
- publicKey?: Uint8Array;
32
- publicKeyCompressed?: Uint8Array;
33
- publicKeyHash?: Uint8Array;
34
- address?: string;
35
- }
36
-
37
- /**
38
- * Class to manage a mainnet watch wallet.
39
- */
40
- export class WatchWallet extends BaseWallet {
41
- readonly publicKeyCompressed?: Uint8Array;
42
- readonly publicKey?: Uint8Array;
43
- readonly publicKeyHash!: Uint8Array;
44
- readonly cashaddr!: string;
45
- readonly tokenaddr!: string;
46
-
47
- static networkPrefix = CashAddressNetworkPrefix.mainnet;
48
- static walletType = WalletTypeEnum.Watch;
49
-
50
- constructor(
51
- name = "",
52
- network = NetworkType.Mainnet,
53
- walletType = WalletTypeEnum.Watch
54
- ) {
55
- super(network);
56
-
57
- this.name = name;
58
- // @ts-ignore
59
- this.walletType = walletType;
60
- }
61
-
62
- /// Initialize the watch-only wallet given the options - publicKey variations
63
- /// This internal method is called by the various static constructors
64
- protected async initialize({
65
- name = "",
66
- publicKey = undefined,
67
- publicKeyCompressed = undefined,
68
- publicKeyHash = undefined,
69
- address = undefined,
70
- }: WatchWalletOptions = {}): Promise<this> {
71
- if (publicKey?.length) {
72
- // @ts-ignore
73
- this.publicKey = publicKey;
74
-
75
- if (!publicKeyCompressed) {
76
- publicKeyCompressed = assertSuccess(
77
- secp256k1.compressPublicKey(publicKey)
78
- );
79
- }
80
- }
81
-
82
- if (publicKeyCompressed?.length) {
83
- // @ts-ignore
84
- this.publicKeyCompressed = publicKeyCompressed;
85
-
86
- if (!publicKey) {
87
- publicKey = assertSuccess(
88
- secp256k1.uncompressPublicKey(publicKeyCompressed)
89
- );
90
- // @ts-ignore
91
- this.publicKey = publicKey;
92
- }
93
-
94
- publicKeyHash = hash160(publicKeyCompressed);
95
- }
96
-
97
- if (publicKeyHash?.length) {
98
- // @ts-ignore
99
- this.publicKeyHash = publicKeyHash;
100
-
101
- address = encodeCashAddress({
102
- prefix: prefixFromNetworkMap[this.network],
103
- type: "p2pkh",
104
- payload: publicKeyHash,
105
- }).address;
106
- }
107
-
108
- if (address?.length) {
109
- // derive prefix if not provided
110
- if (!address.includes(":")) {
111
- const decoded = assertSuccess(
112
- decodeCashAddressFormatWithoutPrefix(address)
113
- );
114
- address = `${decoded.prefix}:${address}`;
115
- }
116
-
117
- const decoded = assertSuccess(decodeCashAddress(address));
118
-
119
- if (networkPrefixMap[decoded.prefix] !== this.network) {
120
- throw Error(
121
- `a ${decoded.prefix} address cannot be watched from a ${this.network} Wallet`
122
- );
123
- }
124
-
125
- if (!this.publicKeyHash?.length) {
126
- if (decoded.type.includes("p2pkh")) {
127
- // @ts-ignore
128
- this.publicKeyHash = decoded.payload;
129
- }
130
- }
131
-
132
- // @ts-ignore
133
- this.cashaddr = toCashaddr(address);
134
- // @ts-ignore
135
- this.tokenaddr = toTokenaddr(address);
136
- }
137
-
138
- if (!this.cashaddr?.length || !this.tokenaddr?.length) {
139
- throw Error("Could not initialize wallet - insufficient parameters");
140
- }
141
-
142
- this.name = name;
143
-
144
- return this;
145
- }
146
-
147
- /**
148
- * getDepositAddress - get a wallet deposit address
149
- *
150
- * a high-level function,
151
- *
152
- * @see {@link https://rest-unstable.mainnet.cash/api-docs/#/wallet/depositAddress|/wallet/deposit_address} for REST endpoint
153
- *
154
- * @returns The deposit address as a string
155
- */
156
- public getDepositAddress(): string {
157
- return this.cashaddr;
158
- }
159
-
160
- /**
161
- * getChangeAddress - get a wallet change address
162
- *
163
- * a high-level function,
164
- *
165
- * @see {@link https://rest-unstable.mainnet.cash/api-docs/#/wallet/changeAddress|/wallet/change_address} for REST endpoint
166
- *
167
- * @returns The change address as a string
168
- */
169
- public getChangeAddress(): string {
170
- return this.cashaddr;
171
- }
172
-
173
- /**
174
- * getTokenDepositAddress - get a cashtoken aware wallet deposit address
175
- *
176
- * @returns The cashtoken aware deposit address as a string
177
- */
178
- public getTokenDepositAddress(): string {
179
- return this.tokenaddr;
180
- }
181
-
182
- // returns the public key hash for an address
183
- public getPublicKeyHash(hex = false): string | Uint8Array {
184
- return hex ? binToHex(this.publicKeyHash) : this.publicKeyHash;
185
- }
186
-
187
- // returns the public key hash for an address
188
- public getPublicKey(hex = false): string | Uint8Array {
189
- if (this.publicKey) {
190
- return hex ? binToHex(this.publicKey) : this.publicKey;
191
- } else {
192
- throw Error(
193
- "The public key for this wallet is not known, perhaps the wallet was created to watch the *hash* of a public key? i.e. a cashaddress."
194
- );
195
- }
196
- }
197
-
198
- // returns the public key hash for an address
199
- public getPublicKeyCompressed(hex = false): string | Uint8Array {
200
- if (this.publicKeyCompressed) {
201
- return hex
202
- ? binToHex(this.publicKeyCompressed)
203
- : this.publicKeyCompressed;
204
- } else {
205
- throw Error(
206
- "The compressed public key for this wallet is not known, perhaps the wallet was created to watch the *hash* of a public key? i.e. a cashaddress."
207
- );
208
- }
209
- }
210
-
211
- // Return wallet info
212
- public getInfo(): WalletInfoI {
213
- return {
214
- cashaddr: this.cashaddr,
215
- tokenaddr: this.tokenaddr,
216
- isTestnet: this.isTestnet,
217
- name: this.name,
218
- network: this.network as any,
219
- publicKeyHash: binToHex(this.publicKeyHash),
220
- walletId: this.toString(),
221
- walletDbEntry: this.toDbString(),
222
- };
223
- }
224
-
225
- /**
226
- * utxos Get unspent outputs for the wallet
227
- *
228
- */
229
- public async getUtxos(): Promise<Utxo[]> {
230
- const bchUtxos: Utxo[] = await this.provider.getUtxos(this.cashaddr);
231
- return this._slpSemiAware
232
- ? bchUtxos.filter((u) => u.satoshis > DUST_UTXO_THRESHOLD)
233
- : bchUtxos;
234
- }
235
-
236
- // Gets balance by summing value in all utxos in sats
237
- // Balance includes DUST utxos which could be slp tokens and also cashtokens with BCH amounts
238
- public async getBalance(): Promise<bigint> {
239
- return this.provider.getBalance(this.cashaddr);
240
- }
241
-
242
- // gets transaction history of this wallet
243
- public async getRawHistory(
244
- fromHeight: number = 0,
245
- toHeight: number = -1
246
- ): Promise<TxI[]> {
247
- return await this.provider.getHistory(this.cashaddr, fromHeight, toHeight);
248
- }
249
-
250
- /**
251
- * getHistory gets transaction history of this wallet with most data decoded and ready to present to user
252
- * @note balance calculations are valid only if querying to the blockchain tip (`toHeight` === -1, `count` === -1)
253
- * @note this method is heavy on network calls, if invoked in browser use of cache is advised, @see `Config.UseLocalStorageCache`
254
- * @note this method tries to recreate the history tab view of Electron Cash wallet, however, it may not be 100% accurate if the tnransaction value changes are the same in the same block (ordering)
255
- *
256
- * @param unit optional, BCH or currency unit to present balance and balance changes. If unit is currency like USD or EUR, balances will be subject to possible rounding errors. Default 0
257
- * @param fromHeight optional, if set, history will be limited. Default 0
258
- * @param toHeight optional, if set, history will be limited. Default -1, meaning that all history items will be returned, including mempool
259
- * @param start optional, if set, the result set will be paginated with offset `start`
260
- * @param count optional, if set, the result set will be paginated with `count`. Default -1, meaning that all history items will be returned
261
- *
262
- * @returns an array of transaction history items, with input values and addresses encoded in cashaddress format. @see `TransactionHistoryItem` type
263
- */
264
- public async getHistory({
265
- unit = "sat",
266
- fromHeight = 0,
267
- toHeight = -1,
268
- start = 0,
269
- count = -1,
270
- }: {
271
- unit?: UnitEnum;
272
- fromHeight?: number;
273
- toHeight?: number;
274
- start?: number;
275
- count?: number;
276
- } = {}): Promise<TransactionHistoryItem[]> {
277
- return getHistory({
278
- addresses: [this.cashaddr],
279
- provider: this.provider,
280
- unit,
281
- fromHeight,
282
- toHeight,
283
- start,
284
- count,
285
- });
286
- }
287
-
288
- protected fromId(walletId: string): Promise<this> {
289
- const [walletType, networkGiven, arg1, arg2]: string[] =
290
- walletId.split(":");
291
-
292
- if (walletType !== WalletTypeEnum.Watch) {
293
- throw Error(
294
- `fromId called on a ${walletType} wallet, expected a ${WalletTypeEnum.Watch} wallet`
295
- );
296
- }
297
-
298
- if (this.network != networkGiven) {
299
- throw Error(`Network prefix ${networkGiven} to a ${this.network} wallet`);
300
- }
301
-
302
- if (arg2) {
303
- return this.watchOnly(`${arg1}:${arg2}`);
304
- }
305
-
306
- return this.watchOnly(arg1);
307
- }
308
-
309
- // Initialize a watch only wallet from a cash addr
310
- protected async watchOnly(address: string): Promise<this> {
311
- // @ts-ignore
312
- this.walletType = WalletTypeEnum.Watch;
313
-
314
- return this.initialize({ address, name: this.name });
315
- }
316
-
317
- /**
318
- * watchOnly - create a watch-only wallet
319
- *
320
- * such kind of wallet does not have a private key and is unable to spend any funds
321
- * however it still allows to use many utility functions such as getting and watching balance, etc.
322
- *
323
- * @param address cashaddress, token aware cashaddress of a wallet
324
- *
325
- * @returns instantiated wallet
326
- */
327
- public static async watchOnly<T extends typeof WatchWallet>(
328
- this: T,
329
- address: string
330
- ) {
331
- if (!address?.length)
332
- throw Error("address is required to create a watch-only wallet");
333
-
334
- return new this().watchOnly(address) as InstanceType<T>;
335
- }
336
-
337
- /**
338
- * fromCashaddr - create a watch-only wallet in the network derived from the address
339
- *
340
- * such kind of wallet does not have a private key and is unable to spend any funds
341
- * however it still allows to use many utility functions such as getting and watching balance, etc.
342
- *
343
- * @param address cashaddress of a wallet
344
- *
345
- * @returns instantiated wallet
346
- */
347
- public static async fromCashaddr<T extends typeof WatchWallet>(
348
- this: T,
349
- address: string
350
- ): Promise<InstanceType<T>> {
351
- if (!address?.length)
352
- throw Error("address is required to create a watch-only wallet");
353
-
354
- const prefix = derivePrefix(address);
355
- const networkType = networkPrefixMap[prefix] as NetworkType;
356
- return new this("", networkType).watchOnly(address) as InstanceType<T>;
357
- }
358
-
359
- /**
360
- * fromTokenaddr - create a watch-only wallet in the network derived from the address
361
- *
362
- * such kind of wallet does not have a private key and is unable to spend any funds
363
- * however it still allows to use many utility functions such as getting and watching balance, etc.
364
- *
365
- * @param address token aware cashaddress of a wallet
366
- *
367
- * @returns instantiated wallet
368
- */
369
- public static async fromTokenaddr<T extends typeof WatchWallet>(
370
- this: T,
371
- address: string
372
- ): Promise<InstanceType<T>> {
373
- if (!address?.length)
374
- throw Error("address is required to create a watch-only wallet");
375
-
376
- const prefix = derivePrefix(address);
377
- const networkType = networkPrefixMap[prefix] as NetworkType;
378
- return new this("", networkType).watchOnly(address) as InstanceType<T>;
379
- }
380
-
381
- /**
382
- * fromPublicKey - create a watch-only wallet in the network derived from the compressed or uncompressed public key
383
- *
384
- * such kind of wallet does not have a private key and is unable to spend any funds
385
- * however it still allows to use many utility functions such as getting and watching balance, etc.
386
- *
387
- * @param publicKey compressed or uncompressed public key
388
- *
389
- * @returns instantiated wallet
390
- */
391
- public static async fromPublicKey<T extends typeof WatchWallet>(
392
- this: T,
393
- publicKey: Uint8Array
394
- ): Promise<InstanceType<T>> {
395
- this.walletType = WalletTypeEnum.Watch;
396
-
397
- return new this(
398
- "",
399
- networkPrefixMap[this.networkPrefix] as NetworkType
400
- ).initialize({
401
- publicKey: publicKey.length === 33 ? undefined : publicKey,
402
- publicKeyCompressed: publicKey.length === 33 ? publicKey : undefined,
403
- }) as InstanceType<T>;
404
- }
405
-
406
- // Convenience wrapper to verify interface
407
- public verify(
408
- message: string,
409
- sig: string,
410
- address?: string,
411
- publicKey?: Uint8Array
412
- ): VerifyMessageResponseI {
413
- if (!address) {
414
- address = this.cashaddr;
415
- }
416
-
417
- return super.verify(message, sig, address, publicKey);
418
- }
419
- }
420
-
421
- /**
422
- * Class to manage a testnet watch wallet.
423
- */
424
- export class TestNetWatchWallet extends WatchWallet {
425
- static networkPrefix = CashAddressNetworkPrefix.testnet;
426
- static walletType = WalletTypeEnum.Watch;
427
- constructor(name = "", network = NetworkType.Testnet) {
428
- super(name, network);
429
- }
430
- }
431
-
432
- /**
433
- * Class to manage a regtest watch wallet.
434
- */
435
- export class RegTestWatchWallet extends WatchWallet {
436
- static networkPrefix = CashAddressNetworkPrefix.regtest;
437
- static walletType = WalletTypeEnum.Watch;
438
- constructor(name = "", network = NetworkType.Regtest) {
439
- super(name, network);
440
- }
441
- }
@@ -1,48 +0,0 @@
1
- import { deriveSeedFromBip39Mnemonic, hexToBin } from "@bitauth/libauth";
2
- import { RegTestWallet } from "./Wif";
3
-
4
- describe(`Test bip39 edge cases`, () => {
5
- test("Should match match the abandon seed", async () => {
6
- let abandonSeed =
7
- "9d7aab1883c82345264e144366d69edacb85be1b311bd97487e241c1a7f6f870dbab428e30f584ea75a608d2d5c50af5199ca79dba02108d85a4272e55f43449";
8
- let w = deriveSeedFromBip39Mnemonic(new Array(12).join("abandon "));
9
- let wSeed = new Uint8Array(w.buffer);
10
- expect(wSeed).toStrictEqual(hexToBin(abandonSeed));
11
- });
12
-
13
- test("Should catch a blank seed", async () => {
14
- try {
15
- let w = RegTestWallet.fromSeed("");
16
- let cashaddr = (await w).cashaddr;
17
- expect(cashaddr).toContain(
18
- "bchreg:qrvcdmgpk73zyfd8pmdl9wnuld36zh9n4g974kwcsl"
19
- );
20
- } catch (e: any) {
21
- expect(e.message).toBe(`refusing to create wallet from empty mnemonic`);
22
- }
23
- });
24
-
25
- test("Should catch an invalid seed", async () => {
26
- try {
27
- let w = RegTestWallet.fromSeed(
28
- "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon"
29
- );
30
- let cashaddr = (await w).cashaddr;
31
- expect(cashaddr).toContain(
32
- "bchreg:qrvcdmgpk73zyfd8pmdl9wnuld36zh9n4g974kwcsl"
33
- );
34
- } catch (e: any) {
35
- expect(e.message).toBe(`Invalid mnemonic, must be 12 or 24 words`);
36
- }
37
- });
38
-
39
- test("Should match the blank seed", async () => {
40
- let w = RegTestWallet.fromSeed(
41
- "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
42
- );
43
- let cashaddr = (await w).cashaddr;
44
- expect(cashaddr).toContain(
45
- "bchreg:qrvcdmgpk73zyfd8pmdl9wnuld36zh9n4g974kwcsl"
46
- );
47
- });
48
- });