mainnet-js 2.7.31 → 3.0.0-next.0
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.
- package/dist/index.html +1 -1
- package/dist/{mainnet-2.7.31.js → mainnet-3.0.0-next.0.js} +718 -678
- package/dist/module/cache/MemoryCache.d.ts +0 -1
- package/dist/module/cache/MemoryCache.d.ts.map +1 -1
- package/dist/module/cache/MemoryCache.js +5 -7
- package/dist/module/cache/MemoryCache.js.map +1 -1
- package/dist/module/cache/index.d.ts +1 -0
- package/dist/module/cache/index.d.ts.map +1 -1
- package/dist/module/cache/index.js +1 -0
- package/dist/module/cache/index.js.map +1 -1
- package/dist/module/cache/walletCache.d.ts +39 -0
- package/dist/module/cache/walletCache.d.ts.map +1 -0
- package/dist/module/cache/walletCache.js +141 -0
- package/dist/module/cache/walletCache.js.map +1 -0
- package/dist/module/history/{electrumTransformer.d.ts → getHistory.d.ts} +3 -3
- package/dist/module/history/getHistory.d.ts.map +1 -0
- package/dist/module/history/{electrumTransformer.js → getHistory.js} +47 -14
- package/dist/module/history/getHistory.js.map +1 -0
- package/dist/module/index.d.ts +3 -1
- package/dist/module/index.d.ts.map +1 -1
- package/dist/module/index.js +3 -1
- package/dist/module/index.js.map +1 -1
- package/dist/module/interface.d.ts +7 -1
- package/dist/module/interface.d.ts.map +1 -1
- package/dist/module/interface.js.map +1 -1
- package/dist/module/message/interface.d.ts +2 -2
- package/dist/module/message/interface.d.ts.map +1 -1
- package/dist/module/message/interface.js +0 -3
- package/dist/module/message/interface.js.map +1 -1
- package/dist/module/message/signed.d.ts +5 -5
- package/dist/module/message/signed.d.ts.map +1 -1
- package/dist/module/message/signed.js +14 -14
- package/dist/module/message/signed.js.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.d.ts +2 -2
- package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.js +2 -1
- package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
- package/dist/module/network/NetworkProvider.d.ts +2 -2
- package/dist/module/network/NetworkProvider.d.ts.map +1 -1
- package/dist/module/network/constant.js +4 -4
- package/dist/module/network/constant.js.map +1 -1
- package/dist/module/transaction/Wif.d.ts +21 -19
- package/dist/module/transaction/Wif.d.ts.map +1 -1
- package/dist/module/transaction/Wif.js +18 -15
- package/dist/module/transaction/Wif.js.map +1 -1
- package/dist/module/util/checkUtxos.d.ts +2 -2
- package/dist/module/util/checkUtxos.d.ts.map +1 -1
- package/dist/module/util/checkUtxos.js +11 -12
- package/dist/module/util/checkUtxos.js.map +1 -1
- package/dist/module/util/deriveCashaddr.d.ts.map +1 -1
- package/dist/module/util/deriveCashaddr.js +6 -0
- package/dist/module/util/deriveCashaddr.js.map +1 -1
- package/dist/module/util/deriveNetwork.js +1 -1
- package/dist/module/util/deriveNetwork.js.map +1 -1
- package/dist/module/util/hd.d.ts +3 -0
- package/dist/module/util/hd.d.ts.map +1 -0
- package/dist/module/util/hd.js +11 -0
- package/dist/module/util/hd.js.map +1 -0
- package/dist/module/util/index.d.ts +1 -0
- package/dist/module/util/index.d.ts.map +1 -1
- package/dist/module/util/index.js +1 -0
- package/dist/module/util/index.js.map +1 -1
- package/dist/module/util/sumUtxoValue.d.ts +3 -3
- package/dist/module/util/sumUtxoValue.d.ts.map +1 -1
- package/dist/module/util/sumUtxoValue.js.map +1 -1
- package/dist/module/wallet/Base.d.ts +37 -93
- package/dist/module/wallet/Base.d.ts.map +1 -1
- package/dist/module/wallet/Base.js +83 -250
- package/dist/module/wallet/Base.js.map +1 -1
- package/dist/module/wallet/HDWallet.d.ts +164 -0
- package/dist/module/wallet/HDWallet.d.ts.map +1 -0
- package/dist/module/wallet/HDWallet.js +486 -0
- package/dist/module/wallet/HDWallet.js.map +1 -0
- package/dist/module/wallet/Watch.d.ts +151 -0
- package/dist/module/wallet/Watch.d.ts.map +1 -0
- package/dist/module/wallet/Watch.js +307 -0
- package/dist/module/wallet/Watch.js.map +1 -0
- package/dist/module/wallet/Wif.d.ts +23 -29
- package/dist/module/wallet/Wif.d.ts.map +1 -1
- package/dist/module/wallet/Wif.js +204 -267
- package/dist/module/wallet/Wif.js.map +1 -1
- package/dist/module/wallet/createWallet.d.ts +7 -1
- package/dist/module/wallet/createWallet.d.ts.map +1 -1
- package/dist/module/wallet/createWallet.js +26 -17
- package/dist/module/wallet/createWallet.js.map +1 -1
- package/dist/module/wallet/interface.d.ts +3 -3
- package/dist/module/wallet/interface.d.ts.map +1 -1
- package/dist/module/wallet/model.d.ts +3 -3
- package/dist/module/wallet/model.d.ts.map +1 -1
- package/dist/module/wallet/model.js +2 -18
- package/dist/module/wallet/model.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/cache/MemoryCache.ts +5 -5
- package/src/cache/index.ts +1 -0
- package/src/cache/walletCache.ts +252 -0
- package/src/history/{electrumTransformer.test.ts → getHistory.test.ts} +6 -19
- package/src/history/{electrumTransformer.ts → getHistory.ts} +63 -15
- package/src/index.ts +3 -1
- package/src/interface.ts +8 -1
- package/src/message/interface.ts +2 -28
- package/src/message/signed.test.ts +56 -48
- package/src/message/signed.ts +15 -18
- package/src/network/ElectrumNetworkProvider.ts +4 -4
- package/src/network/NetworkProvider.ts +2 -2
- package/src/network/Rpc.test.ts +1 -1
- package/src/network/constant.ts +4 -4
- package/src/transaction/Wif.ts +41 -35
- package/src/util/checkUtxos.ts +21 -26
- package/src/util/deriveCashaddr.ts +8 -0
- package/src/util/deriveNetwork.ts +1 -1
- package/src/util/derivePublicKeyHash.test.ts +0 -13
- package/src/util/hd.ts +16 -0
- package/src/util/index.ts +1 -0
- package/src/util/sumUtxoValue.ts +5 -5
- package/src/wallet/Base.ts +123 -332
- package/src/wallet/HDWallet.test.ts +372 -0
- package/src/wallet/HDWallet.ts +764 -0
- package/src/wallet/Watch.ts +447 -0
- package/src/wallet/Wif.ts +258 -283
- package/src/wallet/createWallet.ts +28 -18
- package/src/wallet/interface.ts +3 -3
- package/src/wallet/model.test.ts +2 -2
- package/src/wallet/model.ts +6 -23
- package/dist/module/history/electrumTransformer.d.ts.map +0 -1
- package/dist/module/history/electrumTransformer.js.map +0 -1
|
@@ -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
|
+
}
|