mainnet-js 2.7.24 → 2.7.25
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.24.js → mainnet-2.7.25.js} +4 -4
- package/dist/module/network/ElectrumNetworkProvider.d.ts +0 -6
- package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
- package/dist/module/network/ElectrumNetworkProvider.js +3 -43
- package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
- package/dist/module/wallet/Base.d.ts +7 -7
- package/dist/module/wallet/Base.d.ts.map +1 -1
- package/dist/module/wallet/Base.js +5 -0
- package/dist/module/wallet/Base.js.map +1 -1
- package/dist/module/wallet/Wif.d.ts +22 -13
- package/dist/module/wallet/Wif.d.ts.map +1 -1
- package/dist/module/wallet/Wif.js +56 -5
- package/dist/module/wallet/Wif.js.map +1 -1
- package/dist/module/wallet/enum.d.ts +1 -0
- package/dist/module/wallet/enum.d.ts.map +1 -1
- package/dist/module/wallet/enum.js +1 -0
- package/dist/module/wallet/enum.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/network/ElectrumNetworkProvider.ts +3 -52
- package/src/util/derivePublicKeyHash.test.ts +1 -0
- package/src/wallet/Base.ts +12 -7
- package/src/wallet/Wif.ts +81 -18
- package/src/wallet/enum.ts +1 -0
package/src/wallet/Wif.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
//#region Imports
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
encodeHdPublicKey,
|
|
4
|
+
HdKeyNetwork,
|
|
5
|
+
hexToBin,
|
|
6
|
+
secp256k1,
|
|
7
|
+
} from "@bitauth/libauth";
|
|
3
8
|
|
|
4
9
|
import {
|
|
5
10
|
binToHex,
|
|
@@ -54,20 +59,20 @@ import { BaseWallet } from "./Base.js";
|
|
|
54
59
|
* Class to manage a bitcoin cash wallet.
|
|
55
60
|
*/
|
|
56
61
|
export class Wallet extends BaseWallet {
|
|
57
|
-
declare provider: ElectrumNetworkProvider;
|
|
58
|
-
declare cashaddr: string;
|
|
59
|
-
declare tokenaddr: string;
|
|
60
|
-
derivationPath: string = Config.DefaultParentDerivationPath + "/0/0";
|
|
61
|
-
parentDerivationPath: string = Config.DefaultParentDerivationPath;
|
|
62
|
-
mnemonic!: string;
|
|
63
|
-
parentXPubKey!: string;
|
|
64
|
-
privateKey!: Uint8Array;
|
|
65
|
-
publicKeyCompressed!: Uint8Array;
|
|
66
|
-
privateKeyWif!: string;
|
|
67
|
-
publicKey!: Uint8Array;
|
|
68
|
-
declare publicKeyHash: Uint8Array;
|
|
62
|
+
declare readonly provider: ElectrumNetworkProvider;
|
|
63
|
+
declare readonly cashaddr: string;
|
|
64
|
+
declare readonly tokenaddr: string;
|
|
65
|
+
readonly derivationPath: string = Config.DefaultParentDerivationPath + "/0/0";
|
|
66
|
+
readonly parentDerivationPath: string = Config.DefaultParentDerivationPath;
|
|
67
|
+
readonly mnemonic!: string;
|
|
68
|
+
readonly parentXPubKey!: string;
|
|
69
|
+
readonly privateKey!: Uint8Array;
|
|
70
|
+
readonly publicKeyCompressed!: Uint8Array;
|
|
71
|
+
readonly privateKeyWif!: string;
|
|
72
|
+
readonly publicKey!: Uint8Array;
|
|
73
|
+
declare readonly publicKeyHash: Uint8Array;
|
|
69
74
|
declare name: string;
|
|
70
|
-
static signedMessage: SignedMessageI = new SignedMessage();
|
|
75
|
+
static readonly signedMessage: SignedMessageI = new SignedMessage();
|
|
71
76
|
|
|
72
77
|
//#region Accessors
|
|
73
78
|
// Get mnemonic and derivation path for wallet
|
|
@@ -141,6 +146,7 @@ export class Wallet extends BaseWallet {
|
|
|
141
146
|
) {
|
|
142
147
|
super(network);
|
|
143
148
|
this.name = name;
|
|
149
|
+
// @ts-ignore
|
|
144
150
|
this.walletType = walletType;
|
|
145
151
|
}
|
|
146
152
|
|
|
@@ -159,6 +165,20 @@ export class Wallet extends BaseWallet {
|
|
|
159
165
|
return new this().fromId(walletId) as InstanceType<T>;
|
|
160
166
|
}
|
|
161
167
|
|
|
168
|
+
/**
|
|
169
|
+
* fromPrivateKey - create a wallet using the private key supplied in hex or Uint8Array
|
|
170
|
+
*
|
|
171
|
+
* @param wif WIF encoded private key string
|
|
172
|
+
*
|
|
173
|
+
* @returns instantiated wallet
|
|
174
|
+
*/
|
|
175
|
+
public static async fromPrivateKey<T extends typeof Wallet>(
|
|
176
|
+
this: T,
|
|
177
|
+
privateKey: string | Uint8Array
|
|
178
|
+
): Promise<InstanceType<T>> {
|
|
179
|
+
return new this().fromPrivateKey(privateKey) as InstanceType<T>;
|
|
180
|
+
}
|
|
181
|
+
|
|
162
182
|
/**
|
|
163
183
|
* fromWIF - create a wallet using the private key supplied in `Wallet Import Format`
|
|
164
184
|
*
|
|
@@ -229,6 +249,7 @@ export class Wallet extends BaseWallet {
|
|
|
229
249
|
|
|
230
250
|
private async _generateWif() {
|
|
231
251
|
if (!this.privateKey) {
|
|
252
|
+
// @ts-ignore
|
|
232
253
|
this.privateKey = generatePrivateKey(
|
|
233
254
|
() => generateRandomBytes(32) as Uint8Array
|
|
234
255
|
);
|
|
@@ -237,12 +258,14 @@ export class Wallet extends BaseWallet {
|
|
|
237
258
|
}
|
|
238
259
|
|
|
239
260
|
private async _generateMnemonic() {
|
|
261
|
+
// @ts-ignore
|
|
240
262
|
this.mnemonic = generateMnemonic(Config.getWordlist());
|
|
241
263
|
if (this.mnemonic.length == 0)
|
|
242
264
|
throw Error("refusing to create wallet from empty mnemonic");
|
|
243
265
|
const seed = mnemonicToSeedSync(this.mnemonic);
|
|
244
266
|
checkForEmptySeed(seed);
|
|
245
267
|
const network = this.isTestnet ? "testnet" : "mainnet";
|
|
268
|
+
// @ts-ignore
|
|
246
269
|
this.parentXPubKey = getXPubKey(seed, this.parentDerivationPath, network);
|
|
247
270
|
|
|
248
271
|
const hdNode = deriveHdPrivateNodeFromSeed(seed, {
|
|
@@ -254,8 +277,10 @@ export class Wallet extends BaseWallet {
|
|
|
254
277
|
if (typeof zerothChild === "string") {
|
|
255
278
|
throw Error(zerothChild);
|
|
256
279
|
}
|
|
280
|
+
// @ts-ignore
|
|
257
281
|
this.privateKey = zerothChild.privateKey;
|
|
258
282
|
|
|
283
|
+
// @ts-ignore
|
|
259
284
|
this.walletType = WalletTypeEnum.Seed;
|
|
260
285
|
return await this.deriveInfo();
|
|
261
286
|
}
|
|
@@ -270,10 +295,13 @@ export class Wallet extends BaseWallet {
|
|
|
270
295
|
|
|
271
296
|
// "wif:regtest:cNfsPtqN2bMRS7vH5qd8tR8GMvgXyL5BjnGAKgZ8DYEiCrCCQcP6"
|
|
272
297
|
switch (walletType) {
|
|
273
|
-
case
|
|
298
|
+
case WalletTypeEnum.PrivateKey:
|
|
299
|
+
return this.fromPrivateKey(arg1);
|
|
300
|
+
|
|
301
|
+
case WalletTypeEnum.Wif:
|
|
274
302
|
return this.fromWIF(arg1);
|
|
275
303
|
|
|
276
|
-
case
|
|
304
|
+
case WalletTypeEnum.Watch:
|
|
277
305
|
if (arg2) {
|
|
278
306
|
// watch:testnet:bchtest:qq1234567
|
|
279
307
|
return this.watchOnly(`${arg1}:${arg2}`);
|
|
@@ -281,7 +309,7 @@ export class Wallet extends BaseWallet {
|
|
|
281
309
|
// watch:testnet:qq1234567
|
|
282
310
|
return this.watchOnly(`${arg1}`);
|
|
283
311
|
|
|
284
|
-
case
|
|
312
|
+
case WalletTypeEnum.Named:
|
|
285
313
|
if (arg2) {
|
|
286
314
|
// named:testnet:wallet_1:my_database
|
|
287
315
|
return this.named(arg1, arg2);
|
|
@@ -290,7 +318,7 @@ export class Wallet extends BaseWallet {
|
|
|
290
318
|
return this.named(arg1);
|
|
291
319
|
}
|
|
292
320
|
|
|
293
|
-
case
|
|
321
|
+
case WalletTypeEnum.Seed:
|
|
294
322
|
if (arg2) {
|
|
295
323
|
// seed:testnet:table later ... stove kitten pluck:m/44'/0'/0'/0/0
|
|
296
324
|
return this.fromSeed(arg1, arg2);
|
|
@@ -320,6 +348,7 @@ export class Wallet extends BaseWallet {
|
|
|
320
348
|
mnemonic: string,
|
|
321
349
|
derivationPath?: string
|
|
322
350
|
): Promise<this> {
|
|
351
|
+
// @ts-ignore
|
|
323
352
|
this.mnemonic = mnemonic.trim().toLowerCase();
|
|
324
353
|
|
|
325
354
|
if (this.mnemonic.length == 0)
|
|
@@ -331,11 +360,13 @@ export class Wallet extends BaseWallet {
|
|
|
331
360
|
throwErrors: true,
|
|
332
361
|
});
|
|
333
362
|
if (derivationPath) {
|
|
363
|
+
// @ts-ignore
|
|
334
364
|
this.derivationPath = derivationPath;
|
|
335
365
|
|
|
336
366
|
// If the derivation path is for the first account child, set the parent derivation path
|
|
337
367
|
const path = derivationPath.split("/");
|
|
338
368
|
if (path.slice(-2).join("/") == "0/0") {
|
|
369
|
+
// @ts-ignore
|
|
339
370
|
this.parentDerivationPath = path.slice(0, -2).join("/");
|
|
340
371
|
}
|
|
341
372
|
}
|
|
@@ -344,15 +375,18 @@ export class Wallet extends BaseWallet {
|
|
|
344
375
|
if (typeof zerothChild === "string") {
|
|
345
376
|
throw Error(zerothChild);
|
|
346
377
|
}
|
|
378
|
+
// @ts-ignore
|
|
347
379
|
this.privateKey = zerothChild.privateKey;
|
|
348
380
|
|
|
349
381
|
const network = this.isTestnet ? "testnet" : "mainnet";
|
|
382
|
+
// @ts-ignore
|
|
350
383
|
this.parentXPubKey = await getXPubKey(
|
|
351
384
|
seed,
|
|
352
385
|
this.parentDerivationPath,
|
|
353
386
|
network
|
|
354
387
|
);
|
|
355
388
|
|
|
389
|
+
// @ts-ignore
|
|
356
390
|
this.walletType = WalletTypeEnum.Seed;
|
|
357
391
|
await this.deriveInfo();
|
|
358
392
|
return this;
|
|
@@ -406,6 +440,22 @@ export class Wallet extends BaseWallet {
|
|
|
406
440
|
});
|
|
407
441
|
}
|
|
408
442
|
|
|
443
|
+
// Initialize wallet from private key in hex or Uint8Array
|
|
444
|
+
protected async fromPrivateKey(
|
|
445
|
+
privateKey: string | Uint8Array
|
|
446
|
+
): Promise<this> {
|
|
447
|
+
if (typeof privateKey === "string") {
|
|
448
|
+
privateKey = hexToBin(privateKey);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// @ts-ignore
|
|
452
|
+
this.privateKey = privateKey;
|
|
453
|
+
// @ts-ignore
|
|
454
|
+
this.walletType = WalletTypeEnum.PrivateKey;
|
|
455
|
+
await this.deriveInfo();
|
|
456
|
+
return this;
|
|
457
|
+
}
|
|
458
|
+
|
|
409
459
|
// Initialize wallet from Wallet Import Format
|
|
410
460
|
protected async fromWIF(secret: string): Promise<this> {
|
|
411
461
|
checkWifNetwork(secret, this.network);
|
|
@@ -416,8 +466,11 @@ export class Wallet extends BaseWallet {
|
|
|
416
466
|
throw Error(wifResult as string);
|
|
417
467
|
}
|
|
418
468
|
let resultData: PrivateKeyI = wifResult as PrivateKeyI;
|
|
469
|
+
// @ts-ignore
|
|
419
470
|
this.privateKey = resultData.privateKey;
|
|
471
|
+
// @ts-ignore
|
|
420
472
|
this.privateKeyWif = secret;
|
|
473
|
+
// @ts-ignore
|
|
421
474
|
this.walletType = WalletTypeEnum.Wif;
|
|
422
475
|
await this.deriveInfo();
|
|
423
476
|
return this;
|
|
@@ -447,6 +500,8 @@ export class Wallet extends BaseWallet {
|
|
|
447
500
|
public toString() {
|
|
448
501
|
if (this.name) {
|
|
449
502
|
return `named:${this.network}:${this.name}`;
|
|
503
|
+
} else if (this.walletType == WalletTypeEnum.PrivateKey) {
|
|
504
|
+
return `${this.walletType}:${this.network}:${binToHex(this.privateKey)}`;
|
|
450
505
|
} else if (this.walletType == WalletTypeEnum.Seed) {
|
|
451
506
|
return `${this.walletType}:${this.network}:${this.mnemonic}:${this.derivationPath}`;
|
|
452
507
|
} else if (this.walletType === WalletTypeEnum.Wif) {
|
|
@@ -466,6 +521,8 @@ export class Wallet extends BaseWallet {
|
|
|
466
521
|
public toDbString() {
|
|
467
522
|
if (this.walletType == WalletTypeEnum.Seed) {
|
|
468
523
|
return `${this.walletType}:${this.network}:${this.mnemonic}:${this.derivationPath}`;
|
|
524
|
+
} else if (this.walletType == WalletTypeEnum.PrivateKey) {
|
|
525
|
+
return `${this.walletType}:${this.network}:${binToHex(this.privateKey)}`;
|
|
469
526
|
} else if (this.walletType === WalletTypeEnum.Wif) {
|
|
470
527
|
return `${this.walletType}:${this.network}:${this.privateKeyWif}`;
|
|
471
528
|
} else if (this.walletType == WalletTypeEnum.Watch) {
|
|
@@ -553,6 +610,7 @@ export class Wallet extends BaseWallet {
|
|
|
553
610
|
if (typeof publicKey === "string") {
|
|
554
611
|
throw new Error(publicKey);
|
|
555
612
|
}
|
|
613
|
+
// @ts-ignore
|
|
556
614
|
this.publicKey = publicKey;
|
|
557
615
|
const publicKeyCompressed = secp256k1.derivePublicKeyCompressed(
|
|
558
616
|
this.privateKey
|
|
@@ -560,14 +618,19 @@ export class Wallet extends BaseWallet {
|
|
|
560
618
|
if (typeof publicKeyCompressed === "string") {
|
|
561
619
|
throw new Error(publicKeyCompressed);
|
|
562
620
|
}
|
|
621
|
+
// @ts-ignore
|
|
563
622
|
this.publicKeyCompressed = publicKeyCompressed;
|
|
564
623
|
const networkType =
|
|
565
624
|
this.network === NetworkType.Regtest ? NetworkType.Testnet : this.network;
|
|
625
|
+
// @ts-ignore
|
|
566
626
|
this.privateKeyWif = encodePrivateKeyWif(this.privateKey, networkType);
|
|
567
627
|
checkWifNetwork(this.privateKeyWif, this.network);
|
|
568
628
|
|
|
629
|
+
// @ts-ignore
|
|
569
630
|
this.cashaddr = deriveCashaddr(this.privateKey, this.networkPrefix);
|
|
631
|
+
// @ts-ignore
|
|
570
632
|
this.tokenaddr = deriveTokenaddr(this.privateKey, this.networkPrefix);
|
|
633
|
+
// @ts-ignore
|
|
571
634
|
this.publicKeyHash = derivePublicKeyHash(this.cashaddr);
|
|
572
635
|
return this;
|
|
573
636
|
}
|