mainnet-js 2.7.24 → 2.7.27

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 (35) hide show
  1. package/dist/index.html +1 -1
  2. package/dist/{mainnet-2.7.24.js → mainnet-2.7.27.js} +13 -13
  3. package/dist/module/config.d.ts +1 -0
  4. package/dist/module/config.d.ts.map +1 -1
  5. package/dist/module/config.js +2 -0
  6. package/dist/module/config.js.map +1 -1
  7. package/dist/module/network/ElectrumNetworkProvider.d.ts +0 -6
  8. package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
  9. package/dist/module/network/ElectrumNetworkProvider.js +3 -43
  10. package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
  11. package/dist/module/rate/ExchangeRate.d.ts.map +1 -1
  12. package/dist/module/rate/ExchangeRate.js +4 -0
  13. package/dist/module/rate/ExchangeRate.js.map +1 -1
  14. package/dist/module/wallet/Base.d.ts +7 -7
  15. package/dist/module/wallet/Base.d.ts.map +1 -1
  16. package/dist/module/wallet/Base.js +5 -0
  17. package/dist/module/wallet/Base.js.map +1 -1
  18. package/dist/module/wallet/Wif.d.ts +22 -13
  19. package/dist/module/wallet/Wif.d.ts.map +1 -1
  20. package/dist/module/wallet/Wif.js +56 -5
  21. package/dist/module/wallet/Wif.js.map +1 -1
  22. package/dist/module/wallet/enum.d.ts +1 -0
  23. package/dist/module/wallet/enum.d.ts.map +1 -1
  24. package/dist/module/wallet/enum.js +1 -0
  25. package/dist/module/wallet/enum.js.map +1 -1
  26. package/dist/tsconfig.tsbuildinfo +1 -1
  27. package/package.json +1 -3
  28. package/src/config.ts +5 -0
  29. package/src/network/ElectrumNetworkProvider.ts +3 -52
  30. package/src/rate/ExchangeRate.test.ts +20 -1
  31. package/src/rate/ExchangeRate.ts +5 -0
  32. package/src/util/derivePublicKeyHash.test.ts +1 -0
  33. package/src/wallet/Base.ts +12 -7
  34. package/src/wallet/Wif.ts +81 -18
  35. package/src/wallet/enum.ts +1 -0
@@ -56,6 +56,7 @@ test("Should calculate public key hash consistent with hash160", async () => {
56
56
 
57
57
  test("Should return the a public key hash of manually set wallet", async () => {
58
58
  let w = new Wallet();
59
+ // @ts-ignore
59
60
  w.privateKey = hexToBin(
60
61
  "e284129cc0922579a535bbf4d1a3b25773090d28c909bc0fed73b5e0222cc372"
61
62
  );
@@ -80,14 +80,14 @@ const placeholderPrivateKey =
80
80
  export class BaseWallet implements WalletI {
81
81
  public static StorageProvider?: typeof StorageProvider;
82
82
 
83
- provider: ElectrumNetworkProvider;
84
- network: NetworkType;
85
- walletType: WalletTypeEnum;
83
+ readonly provider: ElectrumNetworkProvider;
84
+ readonly network: NetworkType;
85
+ readonly walletType: WalletTypeEnum;
86
86
  _slpSemiAware: boolean = false; // a flag which requires an utxo to have more than 546 sats to be spendable and counted in the balance
87
- publicKeyHash!: Uint8Array;
88
- cashaddr!: string;
89
- tokenaddr!: string;
90
- isTestnet: boolean;
87
+ readonly publicKeyHash!: Uint8Array;
88
+ readonly cashaddr!: string;
89
+ readonly tokenaddr!: string;
90
+ readonly isTestnet: boolean;
91
91
  name: string = "";
92
92
  _util?: Util;
93
93
 
@@ -479,6 +479,7 @@ export class BaseWallet implements WalletI {
479
479
 
480
480
  // Initialize a watch only wallet from a cash addr
481
481
  protected async watchOnly(address: string): Promise<this> {
482
+ // @ts-ignore
482
483
  this.walletType = WalletTypeEnum.Watch;
483
484
  const addressComponents = address.split(":");
484
485
  let addressPrefix: string;
@@ -504,6 +505,7 @@ export class BaseWallet implements WalletI {
504
505
  const addressData = decodeCashAddress(prefixedAddress);
505
506
  if (typeof addressData === "string") throw addressData;
506
507
 
508
+ // @ts-ignore
507
509
  this.publicKeyHash = addressData.payload;
508
510
 
509
511
  let nonTokenAwareType = addressData.type;
@@ -512,13 +514,16 @@ export class BaseWallet implements WalletI {
512
514
  if (nonTokenAwareType == CashAddressType.p2shWithTokens)
513
515
  nonTokenAwareType = CashAddressType.p2sh;
514
516
  if (nonTokenAwareType == CashAddressType.p2pkh)
517
+ // @ts-ignore
515
518
  this.publicKeyHash = addressData.payload;
516
519
 
520
+ // @ts-ignore
517
521
  this.cashaddr = encodeCashAddress({
518
522
  prefix: addressData.prefix as CashAddressNetworkPrefix,
519
523
  type: nonTokenAwareType,
520
524
  payload: addressData.payload,
521
525
  }).address;
526
+ // @ts-ignore
522
527
  this.tokenaddr = deriveTokenaddr(addressData.payload, this.networkPrefix);
523
528
 
524
529
  return this;
package/src/wallet/Wif.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  //#region Imports
2
- import { encodeHdPublicKey, HdKeyNetwork, secp256k1 } from "@bitauth/libauth";
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 "wif":
298
+ case WalletTypeEnum.PrivateKey:
299
+ return this.fromPrivateKey(arg1);
300
+
301
+ case WalletTypeEnum.Wif:
274
302
  return this.fromWIF(arg1);
275
303
 
276
- case "watch":
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 "named":
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 "seed":
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
  }
@@ -1,4 +1,5 @@
1
1
  export enum WalletTypeEnum {
2
+ Named = "named",
2
3
  Wif = "wif",
3
4
  Seed = "seed",
4
5
  Hd = "hd",