mainnet-js 1.0.17 → 1.1.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.
Files changed (39) hide show
  1. package/dist/index.html +1 -1
  2. package/dist/{mainnet-1.0.17.js → mainnet-1.1.0.js} +435 -435
  3. package/dist/module/network/Connection.d.ts +1 -0
  4. package/dist/module/network/Connection.d.ts.map +1 -1
  5. package/dist/module/network/Connection.js +6 -16
  6. package/dist/module/network/Connection.js.map +1 -1
  7. package/dist/module/network/default.d.ts +2 -2
  8. package/dist/module/network/default.d.ts.map +1 -1
  9. package/dist/module/network/default.js.map +1 -1
  10. package/dist/module/network/index.d.ts +2 -0
  11. package/dist/module/network/index.d.ts.map +1 -1
  12. package/dist/module/network/index.js +1 -0
  13. package/dist/module/network/index.js.map +1 -1
  14. package/dist/module/util/amountInSatoshi.js +1 -1
  15. package/dist/module/util/amountInSatoshi.js.map +1 -1
  16. package/dist/module/wallet/Bcmr.d.ts.map +1 -1
  17. package/dist/module/wallet/Bcmr.js +3 -3
  18. package/dist/module/wallet/Bcmr.js.map +1 -1
  19. package/dist/module/wallet/Wif.d.ts +2 -2
  20. package/dist/module/wallet/Wif.d.ts.map +1 -1
  21. package/dist/module/wallet/Wif.js +4 -11
  22. package/dist/module/wallet/Wif.js.map +1 -1
  23. package/dist/module/wallet/model.d.ts +2 -18
  24. package/dist/module/wallet/model.d.ts.map +1 -1
  25. package/dist/module/wallet/model.js +35 -39
  26. package/dist/module/wallet/model.js.map +1 -1
  27. package/dist/tsconfig.tsbuildinfo +1 -1
  28. package/package.json +2 -2
  29. package/src/Wallet.test.ts +7 -6
  30. package/src/network/Connection.test.ts +1 -1
  31. package/src/network/Connection.ts +6 -14
  32. package/src/network/default.ts +4 -2
  33. package/src/network/index.ts +2 -0
  34. package/src/util/amountInSatoshi.ts +1 -1
  35. package/src/wallet/Bcmr.ts +4 -3
  36. package/src/wallet/Util.test.ts +5 -5
  37. package/src/wallet/Wif.ts +6 -16
  38. package/src/wallet/model.test.ts +12 -17
  39. package/src/wallet/model.ts +43 -58
@@ -4,3 +4,5 @@ export {
4
4
  initProviders,
5
5
  disconnectProviders,
6
6
  } from "./Connection.js";
7
+ export { default as ElectrumNetworkProvider } from "./ElectrumNetworkProvider.js";
8
+ export { default as NetworkProvider } from "./NetworkProvider.js";
@@ -19,7 +19,7 @@ export async function amountInSatoshi(
19
19
  const unit = sanitizeUnit(rawUnit);
20
20
  switch (unit) {
21
21
  case UnitEnum.BCH:
22
- return value * bchParam.subUnits;
22
+ return Math.round(value * bchParam.subUnits);
23
23
  case UnitEnum.SATOSHI:
24
24
  return value;
25
25
  case UnitEnum.SAT:
@@ -14,6 +14,7 @@ import { getGlobalProvider } from "../network/default.js";
14
14
  import ElectrumNetworkProvider from "../network/ElectrumNetworkProvider.js";
15
15
  import { ElectrumRawTransaction } from "../network/interface.js";
16
16
  import { IdentitySnapshot, Registry } from "./bcmr-v1.schema.js";
17
+ import { initProvider } from "../network/Connection.js";
17
18
 
18
19
  export interface AuthChainElement {
19
20
  txHash: string;
@@ -145,9 +146,9 @@ export class BCMR {
145
146
  options.resolveBase = false;
146
147
  }
147
148
 
148
- const provider = getGlobalProvider(
149
+ const provider = (await initProvider(
149
150
  options.network
150
- ) as ElectrumNetworkProvider;
151
+ )!) as ElectrumNetworkProvider;
151
152
 
152
153
  if (options.rawTx === undefined) {
153
154
  options.rawTx = await provider.getRawTransactionObject(
@@ -229,7 +230,7 @@ export class BCMR {
229
230
  val.indexOf("6a0442434d52") === 0 ||
230
231
  val.indexOf("6a4c0442434d52") === 0 ||
231
232
  val.indexOf("6a4d040042434d52") === 0 ||
232
- val.indexOf("6ade0400000042434d52") === 0
233
+ val.indexOf("6a4e0400000042434d52") === 0
233
234
  );
234
235
 
235
236
  if (bcmrOpReturns.length === 0) {
@@ -31,21 +31,21 @@ test("Should throw on non-existent transaction and invalid hash", async () => {
31
31
 
32
32
  test("Should get raw transaction", async () => {
33
33
  let wallet = await RegTestWallet.fromId(process.env.ALICE_ID!);
34
- const utxo = (await wallet.getUtxos()).utxos![0];
34
+ const utxo = (await wallet.getUtxos())[0];
35
35
  const transaction = (await wallet.provider!.getRawTransactionObject(
36
- utxo.txId
36
+ utxo.txid
37
37
  )) as ElectrumRawTransaction;
38
38
  expect((await wallet.util.decodeTransaction(transaction.hash)).hash).toBe(
39
- utxo.txId
39
+ utxo.txid
40
40
  );
41
41
  expect((await wallet.util.decodeTransaction(transaction.hex)).txid).toBe(
42
- utxo.txId
42
+ utxo.txid
43
43
  );
44
44
 
45
45
  // test static accessor
46
46
  expect(
47
47
  (await RegTestWallet.util.decodeTransaction(transaction.hex)).txid
48
- ).toBe(utxo.txId);
48
+ ).toBe(utxo.txid);
49
49
  });
50
50
 
51
51
  test("Should decode a transaction from fist block", async () => {
package/src/wallet/Wif.ts CHANGED
@@ -33,6 +33,7 @@ import {
33
33
  } from "./interface.js";
34
34
 
35
35
  import {
36
+ fromUtxoId,
36
37
  OpReturnData,
37
38
  SendRequest,
38
39
  SendRequestArray,
@@ -42,8 +43,6 @@ import {
42
43
  TokenGenesisRequest,
43
44
  TokenMintRequest,
44
45
  TokenSendRequest,
45
- UtxoItem,
46
- UtxoResponse,
47
46
  XPubKey,
48
47
  } from "./model.js";
49
48
 
@@ -659,14 +658,7 @@ export class Wallet extends BaseWallet {
659
658
  if (!this.cashaddr) {
660
659
  throw Error("Attempted to get utxos without an address");
661
660
  }
662
- let utxos = await this.getAddressUtxos(this.cashaddr);
663
- let resp = new UtxoResponse();
664
- resp.utxos = await Promise.all(
665
- utxos.map(async (o: UtxoI) => {
666
- return UtxoItem.fromElectrum(o);
667
- })
668
- );
669
- return resp;
661
+ return await this.getAddressUtxos(this.cashaddr);
670
662
  }
671
663
 
672
664
  // gets wallet balance in sats, bch and usd
@@ -882,8 +874,8 @@ export class Wallet extends BaseWallet {
882
874
  // get inputs
883
875
  let utxos: UtxoI[];
884
876
  if (params.options && params.options.utxoIds) {
885
- utxos = params.options.utxoIds.map((utxoId) =>
886
- UtxoItem.fromId(utxoId).asElectrum()
877
+ utxos = params.options.utxoIds.map((utxoId: UtxoI | string) =>
878
+ typeof utxoId === "string" ? fromUtxoId(utxoId) : utxoId
887
879
  );
888
880
  } else {
889
881
  utxos = (await this.getAddressUtxos(this.cashaddr)).filter(
@@ -1117,10 +1109,8 @@ export class Wallet extends BaseWallet {
1117
1109
  // get inputs from options or query all inputs
1118
1110
  let utxos: UtxoI[];
1119
1111
  if (options && options.utxoIds) {
1120
- utxos = options.utxoIds.map((utxoId) =>
1121
- typeof utxoId === "string"
1122
- ? UtxoItem.fromId(utxoId).asElectrum()
1123
- : utxoId
1112
+ utxos = options.utxoIds.map((utxoId: UtxoI | string) =>
1113
+ typeof utxoId === "string" ? fromUtxoId(utxoId) : utxoId
1124
1114
  );
1125
1115
  } else {
1126
1116
  utxos = await this.getAddressUtxos(this.cashaddr);
@@ -1,32 +1,27 @@
1
1
  import { UtxoI } from "../interface";
2
- import { UtxoItem } from "./model";
2
+ import { fromUtxoId, toUtxoId } from "./model";
3
3
 
4
4
  test("Should serialize utxo", () => {
5
- let utxo = new UtxoItem({
6
- txId: "this",
7
- index: 42,
8
- value: 1,
5
+ const utxo = toUtxoId({
6
+ txid: "this",
7
+ vout: 42,
8
+ satoshis: 1,
9
9
  }).toString();
10
- expect(utxo.toString()).toBe("this:42:1");
10
+ expect(utxo).toBe("this:42:1");
11
11
  });
12
12
 
13
13
  test("Should deserialize utxo", () => {
14
- let utxo = UtxoItem.fromId("this:42:1");
15
- expect(utxo!.txId).toBe("this");
16
- expect(utxo!.index).toBe(42);
17
- expect(utxo!.value).toBe(1);
18
- expect(utxo!.toString()).toBe("this:42:1");
14
+ const utxo = fromUtxoId("this:42:1");
15
+ expect(utxo.txid).toBe("this");
16
+ expect(utxo.vout).toBe(42);
17
+ expect(utxo.satoshis).toBe(1);
19
18
  });
20
19
 
21
20
  test("Should deserialize utxo", () => {
22
- let u = {
21
+ const u = {
23
22
  txid: "this",
24
23
  vout: 42,
25
24
  satoshis: 1,
26
25
  } as UtxoI;
27
- let utxo = UtxoItem.fromElectrum(u);
28
- expect(utxo!.txId).toBe("this");
29
- expect(utxo!.index).toBe(42);
30
- expect(utxo!.value).toBe(1);
31
- expect(utxo!.toString()).toBe("this:42:1");
26
+ expect(toUtxoId(u)).toBe("this:42:1");
32
27
  });
@@ -3,7 +3,12 @@ import { sanitizeUnit } from "../util/sanitizeUnit.js";
3
3
  import { UnitEnum } from "../enum.js";
4
4
  import { NFTCapability, UtxoI } from "../interface.js";
5
5
  import { DELIMITER } from "../constant.js";
6
- import { binToNumberUint16LE, binToUtf8, hexToBin, utf8ToBin } from "@bitauth/libauth";
6
+ import {
7
+ binToNumberUint16LE,
8
+ binToUtf8,
9
+ hexToBin,
10
+ utf8ToBin,
11
+ } from "@bitauth/libauth";
7
12
  import { Config } from "../config.js";
8
13
  import { checkTokenaddr } from "../util/deriveCashaddr.js";
9
14
 
@@ -238,9 +243,11 @@ export class OpReturnData {
238
243
  throw new Error("Wrong data array element");
239
244
  }
240
245
 
241
- if (length < 76) { // OP_PUSHDATA_1
246
+ if (length < 76) {
247
+ // OP_PUSHDATA_1
242
248
  lengthData = [length];
243
- } else if (length < 223) { // default max `-datacarriersize`
249
+ } else if (length < 223) {
250
+ // default max `-datacarriersize`
244
251
  lengthData = [0x4c, length];
245
252
  } else {
246
253
  throw new Error("OP_RETURN data can not exceed 220 bytes in size");
@@ -299,66 +306,12 @@ export class OpReturnData {
299
306
  * @returns array of binary data chunks pushed, converted to utf8 strings
300
307
  */
301
308
  public static parse(opReturnHex: string): string[] {
302
- return this.parseBinary(hexToBin(opReturnHex)).map(val => binToUtf8(val));
309
+ return this.parseBinary(hexToBin(opReturnHex)).map((val) => binToUtf8(val));
303
310
  }
304
311
  }
305
312
 
306
313
  export type SendRequestArray = Array<string | number | UnitEnum | Buffer>;
307
314
 
308
- export class UtxoItem {
309
- index: number;
310
- value: number;
311
- utxoId: string;
312
- txId: string;
313
-
314
- constructor({
315
- index,
316
- value,
317
- txId,
318
- }: {
319
- index: number;
320
- value: number;
321
- txId: string;
322
- }) {
323
- this.value = value;
324
- this.txId = txId;
325
- this.index = index;
326
- this.utxoId = this.toString();
327
- }
328
-
329
- public toString() {
330
- return [this.txId, this.index, this.value].join(DELIMITER);
331
- }
332
-
333
- public static fromId(utxoId: string) {
334
- let [txid, vout, satoshis] = utxoId.split(DELIMITER);
335
- return new this({
336
- txId: txid,
337
- index: parseInt(vout),
338
- value: parseInt(satoshis),
339
- });
340
- }
341
- public static fromElectrum(u: UtxoI) {
342
- return new this({
343
- txId: u.txid,
344
- index: u.vout,
345
- value: u.satoshis,
346
- });
347
- }
348
-
349
- public asElectrum(): UtxoI {
350
- return {
351
- txid: this.txId,
352
- vout: this.index,
353
- satoshis: this.value,
354
- } as UtxoI;
355
- }
356
- }
357
-
358
- export class UtxoResponse {
359
- "utxos"?: Array<UtxoItem>;
360
- }
361
-
362
315
  export class SendResponse {
363
316
  txId?: string;
364
317
  balance?: BalanceResponse;
@@ -401,3 +354,35 @@ export class XPubKey {
401
354
  };
402
355
  }
403
356
  }
357
+
358
+ export const fromUtxoId = (utxoId: string): UtxoI => {
359
+ const [txid, vout, satoshis, tokenId, amount, capability, commitment] =
360
+ utxoId.split(DELIMITER);
361
+ return {
362
+ satoshis: satoshis ? parseInt(satoshis) : 0,
363
+ vout: parseInt(vout),
364
+ txid,
365
+ token: tokenId
366
+ ? {
367
+ tokenId,
368
+ amount: parseInt(amount),
369
+ capability: capability || undefined,
370
+ commitment: commitment || undefined,
371
+ }
372
+ : undefined,
373
+ } as UtxoI;
374
+ };
375
+
376
+ export const toUtxoId = (utxo: UtxoI): string => {
377
+ return [
378
+ utxo.txid,
379
+ utxo.vout,
380
+ utxo.satoshis,
381
+ utxo.token?.tokenId,
382
+ utxo.token?.amount,
383
+ utxo.token?.capability,
384
+ utxo.token?.commitment,
385
+ ]
386
+ .join(DELIMITER)
387
+ .replace(/:+$/, "");
388
+ };