mainnet-js 2.7.34 → 3.0.0-next.1

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 (195) hide show
  1. package/dist/index.html +1 -1
  2. package/dist/{mainnet-2.7.34.js → mainnet-3.0.0-next.1.js} +728 -708
  3. package/dist/module/cache/MemoryCache.d.ts +0 -1
  4. package/dist/module/cache/MemoryCache.d.ts.map +1 -1
  5. package/dist/module/cache/MemoryCache.js +5 -7
  6. package/dist/module/cache/MemoryCache.js.map +1 -1
  7. package/dist/module/cache/index.d.ts +1 -0
  8. package/dist/module/cache/index.d.ts.map +1 -1
  9. package/dist/module/cache/index.js +1 -0
  10. package/dist/module/cache/index.js.map +1 -1
  11. package/dist/module/cache/walletCache.d.ts +45 -0
  12. package/dist/module/cache/walletCache.d.ts.map +1 -0
  13. package/dist/module/cache/walletCache.js +140 -0
  14. package/dist/module/cache/walletCache.js.map +1 -0
  15. package/dist/module/chain.d.ts +1 -1
  16. package/dist/module/chain.js +1 -1
  17. package/dist/module/chain.js.map +1 -1
  18. package/dist/module/constant.d.ts +1 -1
  19. package/dist/module/constant.d.ts.map +1 -1
  20. package/dist/module/constant.js +1 -1
  21. package/dist/module/constant.js.map +1 -1
  22. package/dist/module/enum.d.ts +1 -7
  23. package/dist/module/enum.d.ts.map +1 -1
  24. package/dist/module/enum.js +0 -6
  25. package/dist/module/enum.js.map +1 -1
  26. package/dist/module/history/{electrumTransformer.d.ts → getHistory.d.ts} +3 -3
  27. package/dist/module/history/getHistory.d.ts.map +1 -0
  28. package/dist/module/history/{electrumTransformer.js → getHistory.js} +17 -14
  29. package/dist/module/history/getHistory.js.map +1 -0
  30. package/dist/module/index.d.ts +3 -1
  31. package/dist/module/index.d.ts.map +1 -1
  32. package/dist/module/index.js +3 -1
  33. package/dist/module/index.js.map +1 -1
  34. package/dist/module/interface.d.ts +7 -2
  35. package/dist/module/interface.d.ts.map +1 -1
  36. package/dist/module/interface.js.map +1 -1
  37. package/dist/module/message/interface.d.ts +2 -2
  38. package/dist/module/message/interface.d.ts.map +1 -1
  39. package/dist/module/message/interface.js +0 -3
  40. package/dist/module/message/interface.js.map +1 -1
  41. package/dist/module/message/signed.d.ts +5 -5
  42. package/dist/module/message/signed.d.ts.map +1 -1
  43. package/dist/module/message/signed.js +8 -8
  44. package/dist/module/message/signed.js.map +1 -1
  45. package/dist/module/network/ElectrumNetworkProvider.d.ts +2 -2
  46. package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
  47. package/dist/module/network/ElectrumNetworkProvider.js +3 -2
  48. package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
  49. package/dist/module/network/NetworkProvider.d.ts +2 -2
  50. package/dist/module/network/NetworkProvider.d.ts.map +1 -1
  51. package/dist/module/network/constant.js +4 -4
  52. package/dist/module/network/constant.js.map +1 -1
  53. package/dist/module/network/getRelayFeeCache.js +2 -2
  54. package/dist/module/network/getRelayFeeCache.js.map +1 -1
  55. package/dist/module/rate/ExchangeRate.d.ts +2 -1
  56. package/dist/module/rate/ExchangeRate.d.ts.map +1 -1
  57. package/dist/module/rate/ExchangeRate.js +4 -1
  58. package/dist/module/rate/ExchangeRate.js.map +1 -1
  59. package/dist/module/transaction/Wif.d.ts +25 -23
  60. package/dist/module/transaction/Wif.d.ts.map +1 -1
  61. package/dist/module/transaction/Wif.js +26 -25
  62. package/dist/module/transaction/Wif.js.map +1 -1
  63. package/dist/module/transaction/allocateFee.d.ts +3 -3
  64. package/dist/module/transaction/allocateFee.d.ts.map +1 -1
  65. package/dist/module/transaction/allocateFee.js +5 -6
  66. package/dist/module/transaction/allocateFee.js.map +1 -1
  67. package/dist/module/util/amountInSatoshi.d.ts +1 -1
  68. package/dist/module/util/amountInSatoshi.d.ts.map +1 -1
  69. package/dist/module/util/amountInSatoshi.js +3 -9
  70. package/dist/module/util/amountInSatoshi.js.map +1 -1
  71. package/dist/module/util/asSendRequestObject.d.ts.map +1 -1
  72. package/dist/module/util/asSendRequestObject.js +10 -7
  73. package/dist/module/util/asSendRequestObject.js.map +1 -1
  74. package/dist/module/util/checkUtxos.d.ts +2 -2
  75. package/dist/module/util/checkUtxos.d.ts.map +1 -1
  76. package/dist/module/util/checkUtxos.js +11 -12
  77. package/dist/module/util/checkUtxos.js.map +1 -1
  78. package/dist/module/util/convert.d.ts +3 -0
  79. package/dist/module/util/convert.d.ts.map +1 -1
  80. package/dist/module/util/convert.js +12 -0
  81. package/dist/module/util/convert.js.map +1 -1
  82. package/dist/module/util/deriveCashaddr.d.ts.map +1 -1
  83. package/dist/module/util/deriveCashaddr.js +6 -0
  84. package/dist/module/util/deriveCashaddr.js.map +1 -1
  85. package/dist/module/util/deriveNetwork.js +1 -1
  86. package/dist/module/util/deriveNetwork.js.map +1 -1
  87. package/dist/module/util/hd.d.ts +3 -0
  88. package/dist/module/util/hd.d.ts.map +1 -0
  89. package/dist/module/util/hd.js +11 -0
  90. package/dist/module/util/hd.js.map +1 -0
  91. package/dist/module/util/index.d.ts +3 -3
  92. package/dist/module/util/index.d.ts.map +1 -1
  93. package/dist/module/util/index.js +3 -3
  94. package/dist/module/util/index.js.map +1 -1
  95. package/dist/module/util/satoshiToAmount.d.ts +1 -1
  96. package/dist/module/util/satoshiToAmount.d.ts.map +1 -1
  97. package/dist/module/util/satoshiToAmount.js +3 -9
  98. package/dist/module/util/satoshiToAmount.js.map +1 -1
  99. package/dist/module/util/sumSendRequestAmounts.d.ts.map +1 -1
  100. package/dist/module/util/sumSendRequestAmounts.js +3 -4
  101. package/dist/module/util/sumSendRequestAmounts.js.map +1 -1
  102. package/dist/module/util/sumUtxoValue.d.ts +3 -3
  103. package/dist/module/util/sumUtxoValue.d.ts.map +1 -1
  104. package/dist/module/util/sumUtxoValue.js +2 -2
  105. package/dist/module/util/sumUtxoValue.js.map +1 -1
  106. package/dist/module/wallet/Base.d.ts +45 -103
  107. package/dist/module/wallet/Base.d.ts.map +1 -1
  108. package/dist/module/wallet/Base.js +99 -298
  109. package/dist/module/wallet/Base.js.map +1 -1
  110. package/dist/module/wallet/HDWallet.d.ts +164 -0
  111. package/dist/module/wallet/HDWallet.d.ts.map +1 -0
  112. package/dist/module/wallet/HDWallet.js +486 -0
  113. package/dist/module/wallet/HDWallet.js.map +1 -0
  114. package/dist/module/wallet/Util.js +1 -1
  115. package/dist/module/wallet/Util.js.map +1 -1
  116. package/dist/module/wallet/Watch.d.ts +151 -0
  117. package/dist/module/wallet/Watch.d.ts.map +1 -0
  118. package/dist/module/wallet/Watch.js +307 -0
  119. package/dist/module/wallet/Watch.js.map +1 -0
  120. package/dist/module/wallet/Wif.d.ts +23 -29
  121. package/dist/module/wallet/Wif.d.ts.map +1 -1
  122. package/dist/module/wallet/Wif.js +204 -267
  123. package/dist/module/wallet/Wif.js.map +1 -1
  124. package/dist/module/wallet/createWallet.d.ts +7 -1
  125. package/dist/module/wallet/createWallet.d.ts.map +1 -1
  126. package/dist/module/wallet/createWallet.js +26 -17
  127. package/dist/module/wallet/createWallet.js.map +1 -1
  128. package/dist/module/wallet/interface.d.ts +5 -6
  129. package/dist/module/wallet/interface.d.ts.map +1 -1
  130. package/dist/module/wallet/model.d.ts +15 -19
  131. package/dist/module/wallet/model.d.ts.map +1 -1
  132. package/dist/module/wallet/model.js +5 -25
  133. package/dist/module/wallet/model.js.map +1 -1
  134. package/dist/tsconfig.tsbuildinfo +1 -1
  135. package/package.json +1 -1
  136. package/src/cache/MemoryCache.ts +5 -5
  137. package/src/cache/index.ts +1 -0
  138. package/src/cache/walletCache.ts +239 -0
  139. package/src/chain.ts +1 -1
  140. package/src/constant.ts +1 -1
  141. package/src/enum.ts +0 -6
  142. package/src/history/{electrumTransformer.test.ts → getHistory.test.ts} +26 -53
  143. package/src/history/{electrumTransformer.ts → getHistory.ts} +31 -15
  144. package/src/index.ts +3 -1
  145. package/src/interface.ts +8 -2
  146. package/src/message/interface.ts +2 -28
  147. package/src/message/signed.test.ts +36 -48
  148. package/src/message/signed.ts +9 -12
  149. package/src/network/Connection.test.ts +3 -3
  150. package/src/network/ElectrumNetworkProvider.ts +5 -5
  151. package/src/network/NetworkProvider.ts +2 -2
  152. package/src/network/Rpc.test.ts +3 -4
  153. package/src/network/constant.ts +4 -4
  154. package/src/network/getRelayFeeCache.ts +2 -2
  155. package/src/rate/ExchangeRate.test.ts +2 -44
  156. package/src/rate/ExchangeRate.ts +5 -2
  157. package/src/transaction/Wif.ts +59 -52
  158. package/src/transaction/allocateFee.test.ts +110 -131
  159. package/src/transaction/allocateFee.ts +14 -15
  160. package/src/util/amountInSatoshi.test.ts +1 -9
  161. package/src/util/amountInSatoshi.ts +6 -10
  162. package/src/util/asSendRequestObject.ts +12 -7
  163. package/src/util/checkUtxos.ts +21 -26
  164. package/src/util/convert.ts +18 -0
  165. package/src/util/deriveCashaddr.ts +8 -0
  166. package/src/util/deriveNetwork.ts +1 -1
  167. package/src/util/derivePublicKeyHash.test.ts +0 -13
  168. package/src/util/hd.ts +16 -0
  169. package/src/util/index.ts +3 -7
  170. package/src/util/satoshiToAmount.test.ts +1 -1
  171. package/src/util/satoshiToAmount.ts +4 -10
  172. package/src/util/sumSendRequestAmounts.ts +3 -4
  173. package/src/util/sumUtxoValue.ts +7 -7
  174. package/src/wallet/Base.ts +147 -420
  175. package/src/wallet/Cashtokens.test.headless.js +11 -11
  176. package/src/wallet/Cashtokens.test.ts +36 -37
  177. package/src/wallet/HDWallet.test.ts +515 -0
  178. package/src/wallet/HDWallet.ts +764 -0
  179. package/src/wallet/Util.ts +1 -1
  180. package/src/wallet/Watch.ts +447 -0
  181. package/src/wallet/Wif.bip39.test.ts +1 -1
  182. package/src/wallet/Wif.test.ts +108 -133
  183. package/src/wallet/Wif.ts +258 -283
  184. package/src/wallet/createWallet.ts +28 -18
  185. package/src/wallet/interface.ts +5 -6
  186. package/src/wallet/model.test.ts +4 -7
  187. package/src/wallet/model.ts +19 -51
  188. package/dist/module/history/electrumTransformer.d.ts.map +0 -1
  189. package/dist/module/history/electrumTransformer.js.map +0 -1
  190. package/dist/module/util/balanceObjectFromSatoshi.d.ts +0 -8
  191. package/dist/module/util/balanceObjectFromSatoshi.d.ts.map +0 -1
  192. package/dist/module/util/balanceObjectFromSatoshi.js +0 -35
  193. package/dist/module/util/balanceObjectFromSatoshi.js.map +0 -1
  194. package/src/util/balanceObjectFromSatoshi.test.ts +0 -58
  195. package/src/util/balanceObjectFromSatoshi.ts +0 -52
@@ -142,7 +142,7 @@ export class Util {
142
142
  ],
143
143
  hex: binToHex(output.lockingBytecode),
144
144
  } as ElectrumRawTransactionVoutScriptPubKey,
145
- value: Number(output.valueSatoshis) / bchParam.subUnits,
145
+ value: Number(output.valueSatoshis) / Number(bchParam.subUnits),
146
146
  };
147
147
  }
148
148
  );
@@ -0,0 +1,447 @@
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
+ return this.getAddressUtxos(this.cashaddr);
231
+ }
232
+
233
+ public async getAddressUtxos(address?: string): Promise<Utxo[]> {
234
+ if (!address) {
235
+ address = this.cashaddr;
236
+ }
237
+
238
+ if (this._slpSemiAware) {
239
+ const bchUtxos: Utxo[] = await this.provider.getUtxos(address);
240
+ return bchUtxos.filter(
241
+ (bchutxo) => bchutxo.satoshis > DUST_UTXO_THRESHOLD
242
+ );
243
+ } else {
244
+ return this.provider.getUtxos(address);
245
+ }
246
+ }
247
+
248
+ // gets transaction history of this wallet
249
+ public async getRawHistory(
250
+ fromHeight: number = 0,
251
+ toHeight: number = -1
252
+ ): Promise<TxI[]> {
253
+ return await this.provider.getHistory(this.cashaddr, fromHeight, toHeight);
254
+ }
255
+
256
+ /**
257
+ * getHistory gets transaction history of this wallet with most data decoded and ready to present to user
258
+ * @note balance calculations are valid only if querying to the blockchain tip (`toHeight` === -1, `count` === -1)
259
+ * @note this method is heavy on network calls, if invoked in browser use of cache is advised, @see `Config.UseLocalStorageCache`
260
+ * @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)
261
+ *
262
+ * @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
263
+ * @param fromHeight optional, if set, history will be limited. Default 0
264
+ * @param toHeight optional, if set, history will be limited. Default -1, meaning that all history items will be returned, including mempool
265
+ * @param start optional, if set, the result set will be paginated with offset `start`
266
+ * @param count optional, if set, the result set will be paginated with `count`. Default -1, meaning that all history items will be returned
267
+ *
268
+ * @returns an array of transaction history items, with input values and addresses encoded in cashaddress format. @see `TransactionHistoryItem` type
269
+ */
270
+ public async getHistory({
271
+ unit = "sat",
272
+ fromHeight = 0,
273
+ toHeight = -1,
274
+ start = 0,
275
+ count = -1,
276
+ }: {
277
+ unit?: UnitEnum;
278
+ fromHeight?: number;
279
+ toHeight?: number;
280
+ start?: number;
281
+ count?: number;
282
+ } = {}): Promise<TransactionHistoryItem[]> {
283
+ return getHistory({
284
+ addresses: [this.cashaddr],
285
+ provider: this.provider,
286
+ unit,
287
+ fromHeight,
288
+ toHeight,
289
+ start,
290
+ count,
291
+ });
292
+ }
293
+
294
+ protected fromId(walletId: string): Promise<this> {
295
+ const [walletType, networkGiven, arg1, arg2]: string[] =
296
+ walletId.split(":");
297
+
298
+ if (walletType !== WalletTypeEnum.Watch) {
299
+ throw Error(
300
+ `fromId called on a ${walletType} wallet, expected a ${WalletTypeEnum.Watch} wallet`
301
+ );
302
+ }
303
+
304
+ if (this.network != networkGiven) {
305
+ throw Error(`Network prefix ${networkGiven} to a ${this.network} wallet`);
306
+ }
307
+
308
+ if (arg2) {
309
+ return this.watchOnly(`${arg1}:${arg2}`);
310
+ }
311
+
312
+ return this.watchOnly(arg1);
313
+ }
314
+
315
+ // Initialize a watch only wallet from a cash addr
316
+ protected async watchOnly(address: string): Promise<this> {
317
+ // @ts-ignore
318
+ this.walletType = WalletTypeEnum.Watch;
319
+
320
+ return this.initialize({ address, name: this.name });
321
+ }
322
+
323
+ /**
324
+ * watchOnly - create a watch-only wallet
325
+ *
326
+ * such kind of wallet does not have a private key and is unable to spend any funds
327
+ * however it still allows to use many utility functions such as getting and watching balance, etc.
328
+ *
329
+ * @param address cashaddress, token aware cashaddress of a wallet
330
+ *
331
+ * @returns instantiated wallet
332
+ */
333
+ public static async watchOnly<T extends typeof WatchWallet>(
334
+ this: T,
335
+ address: string
336
+ ) {
337
+ if (!address?.length)
338
+ throw Error("address is required to create a watch-only wallet");
339
+
340
+ return new this().watchOnly(address) as InstanceType<T>;
341
+ }
342
+
343
+ /**
344
+ * fromCashaddr - create a watch-only wallet in the network derived from the address
345
+ *
346
+ * such kind of wallet does not have a private key and is unable to spend any funds
347
+ * however it still allows to use many utility functions such as getting and watching balance, etc.
348
+ *
349
+ * @param address cashaddress of a wallet
350
+ *
351
+ * @returns instantiated wallet
352
+ */
353
+ public static async fromCashaddr<T extends typeof WatchWallet>(
354
+ this: T,
355
+ address: string
356
+ ): Promise<InstanceType<T>> {
357
+ if (!address?.length)
358
+ throw Error("address is required to create a watch-only wallet");
359
+
360
+ const prefix = derivePrefix(address);
361
+ const networkType = networkPrefixMap[prefix] as NetworkType;
362
+ return new this("", networkType).watchOnly(address) as InstanceType<T>;
363
+ }
364
+
365
+ /**
366
+ * fromTokenaddr - create a watch-only wallet in the network derived from the address
367
+ *
368
+ * such kind of wallet does not have a private key and is unable to spend any funds
369
+ * however it still allows to use many utility functions such as getting and watching balance, etc.
370
+ *
371
+ * @param address token aware cashaddress of a wallet
372
+ *
373
+ * @returns instantiated wallet
374
+ */
375
+ public static async fromTokenaddr<T extends typeof WatchWallet>(
376
+ this: T,
377
+ address: string
378
+ ): Promise<InstanceType<T>> {
379
+ if (!address?.length)
380
+ throw Error("address is required to create a watch-only wallet");
381
+
382
+ const prefix = derivePrefix(address);
383
+ const networkType = networkPrefixMap[prefix] as NetworkType;
384
+ return new this("", networkType).watchOnly(address) as InstanceType<T>;
385
+ }
386
+
387
+ /**
388
+ * fromPublicKey - create a watch-only wallet in the network derived from the compressed or uncompressed public key
389
+ *
390
+ * such kind of wallet does not have a private key and is unable to spend any funds
391
+ * however it still allows to use many utility functions such as getting and watching balance, etc.
392
+ *
393
+ * @param publicKey compressed or uncompressed public key
394
+ *
395
+ * @returns instantiated wallet
396
+ */
397
+ public static async fromPublicKey<T extends typeof WatchWallet>(
398
+ this: T,
399
+ publicKey: Uint8Array
400
+ ): Promise<InstanceType<T>> {
401
+ this.walletType = WalletTypeEnum.Watch;
402
+
403
+ return new this(
404
+ "",
405
+ networkPrefixMap[this.networkPrefix] as NetworkType
406
+ ).initialize({
407
+ publicKey: publicKey.length === 33 ? undefined : publicKey,
408
+ publicKeyCompressed: publicKey.length === 33 ? publicKey : undefined,
409
+ }) as InstanceType<T>;
410
+ }
411
+
412
+ // Convenience wrapper to verify interface
413
+ public verify(
414
+ message: string,
415
+ sig: string,
416
+ address?: string,
417
+ publicKey?: Uint8Array
418
+ ): VerifyMessageResponseI {
419
+ if (!address) {
420
+ address = this.cashaddr;
421
+ }
422
+
423
+ return super.verify(message, sig, address, publicKey);
424
+ }
425
+ }
426
+
427
+ /**
428
+ * Class to manage a testnet watch wallet.
429
+ */
430
+ export class TestNetWatchWallet extends WatchWallet {
431
+ static networkPrefix = CashAddressNetworkPrefix.testnet;
432
+ static walletType = WalletTypeEnum.Watch;
433
+ constructor(name = "", network = NetworkType.Testnet) {
434
+ super(name, network);
435
+ }
436
+ }
437
+
438
+ /**
439
+ * Class to manage a regtest watch wallet.
440
+ */
441
+ export class RegTestWatchWallet extends WatchWallet {
442
+ static networkPrefix = CashAddressNetworkPrefix.regtest;
443
+ static walletType = WalletTypeEnum.Watch;
444
+ constructor(name = "", network = NetworkType.Regtest) {
445
+ super(name, network);
446
+ }
447
+ }
@@ -32,7 +32,7 @@ describe(`Test bip39 edge cases`, () => {
32
32
  "bchreg:qrvcdmgpk73zyfd8pmdl9wnuld36zh9n4g974kwcsl"
33
33
  );
34
34
  } catch (e: any) {
35
- expect(e.message).toBe(`Invalid mnemonic, must be 12 or 24 words`);
35
+ expect(e.message).toBe(`Invalid mnemonic`);
36
36
  }
37
37
  });
38
38