mainnet-js 1.1.7 → 1.1.9

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.
@@ -10,10 +10,9 @@ import {
10
10
  } from "@bitauth/libauth";
11
11
  import axios from "axios";
12
12
  import { Network, TxI } from "../interface.js";
13
- import { getGlobalProvider } from "../network/default.js";
14
13
  import ElectrumNetworkProvider from "../network/ElectrumNetworkProvider.js";
15
14
  import { ElectrumRawTransaction } from "../network/interface.js";
16
- import { IdentitySnapshot, Registry } from "./bcmr-v1.schema.js";
15
+ import { IdentitySnapshot, Registry } from "./bcmr-v2.schema.js";
17
16
  import { initProvider } from "../network/Connection.js";
18
17
 
19
18
  export interface AuthChainElement {
@@ -324,9 +323,11 @@ export class BCMR {
324
323
  if (
325
324
  registry.extensions &&
326
325
  registry.extensions["authchain"] &&
327
- (registry.extensions["authchain"] as string[]).length
326
+ Object.keys(registry.extensions["authchain"]).length
328
327
  ) {
329
- const chainTxArray = registry.extensions!["authchain"] as string[];
328
+ const chainTxArray = Object.values(
329
+ registry.extensions!["authchain"]
330
+ ) as string[];
330
331
 
331
332
  chainBase = chainTxArray
332
333
  .map((tx) => {
@@ -435,28 +436,16 @@ export class BCMR {
435
436
  */
436
437
  public static getTokenInfo(tokenId: string): IdentitySnapshot | undefined {
437
438
  for (const registry of this.metadataRegistries.slice().reverse()) {
438
- // registry identity is an authbase string pointer
439
- if (typeof registry.registryIdentity === "string") {
440
- // enforce spec, ensure identities have this authbase
441
- if (registry.identities?.[registry.registryIdentity]) {
442
- // find the latest identity in history and add it to the list
443
- const latestIdentityInHistory = registry.identities![tokenId]?.[0];
444
- if (latestIdentityInHistory) {
445
- return latestIdentityInHistory;
446
- }
447
- }
448
- } else {
449
- // if the token identity is the registry identity and categories match, return it
450
- if (registry.registryIdentity.token?.category === tokenId) {
451
- return registry.registryIdentity;
452
- }
453
-
454
- // find the latest identity in history and add it to the list
455
- const latestIdentityInHistory = registry.identities![tokenId]?.[0];
456
- if (latestIdentityInHistory) {
457
- return latestIdentityInHistory;
458
- }
439
+ const history = registry.identities?.[tokenId];
440
+ if (!history) {
441
+ continue;
459
442
  }
443
+ const latestIdentityIndex = Object.keys(history)[0];
444
+ if (latestIdentityIndex === undefined) {
445
+ continue;
446
+ }
447
+
448
+ return history[latestIdentityIndex];
460
449
  }
461
450
 
462
451
  return undefined;
@@ -993,23 +993,76 @@ describe(`Test cashtokens`, () => {
993
993
  });
994
994
 
995
995
  test("Test enforcing token addresses", async () => {
996
+ const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
996
997
  const bob = await RegTestWallet.newRandom();
998
+ const genesisResponse = await alice.tokenGenesis({
999
+ amount: 100,
1000
+ });
1001
+ const tokenId = genesisResponse.tokenIds![0];
997
1002
 
998
1003
  const previousValue = Config.EnforceCashTokenReceiptAddresses;
999
1004
 
1000
1005
  const wrap = (addr) => {
1001
- return new Promise(() => {
1002
- return new TokenSendRequest({ cashaddr: addr, tokenId: "" });
1006
+ return new Promise((resolve) => {
1007
+ resolve(new TokenSendRequest({ cashaddr: addr, tokenId: "" }));
1003
1008
  });
1004
1009
  };
1005
1010
 
1006
1011
  Config.EnforceCashTokenReceiptAddresses = false;
1007
- expect(wrap(bob.cashaddr)).resolves.not.toThrow();
1008
- expect(wrap(bob.tokenaddr)).resolves.not.toThrow();
1012
+ await expect(wrap(alice.cashaddr)).resolves.not.toThrow();
1013
+ await expect(wrap(alice.tokenaddr)).resolves.not.toThrow();
1014
+
1015
+ await alice.send(
1016
+ new TokenSendRequest({
1017
+ cashaddr: bob.cashaddr!,
1018
+ tokenId: tokenId,
1019
+ amount: 1,
1020
+ })
1021
+ );
1022
+ await expect(
1023
+ alice.send(
1024
+ new TokenSendRequest({
1025
+ cashaddr: bob.cashaddr!,
1026
+ tokenId: tokenId,
1027
+ amount: 1,
1028
+ })
1029
+ )
1030
+ ).resolves.not.toThrow();
1031
+
1032
+ await expect(
1033
+ alice.send(
1034
+ new TokenSendRequest({
1035
+ cashaddr: bob.tokenaddr!,
1036
+ tokenId: tokenId,
1037
+ amount: 1,
1038
+ })
1039
+ )
1040
+ ).resolves.not.toThrow();
1009
1041
 
1010
1042
  Config.EnforceCashTokenReceiptAddresses = true;
1011
- expect(wrap(bob.cashaddr)).rejects.toThrow();
1012
- expect(wrap(bob.tokenaddr)).resolves.not.toThrow();
1043
+ await expect(wrap(alice.cashaddr)).rejects.toThrow();
1044
+ await expect(wrap(alice.tokenaddr)).resolves.not.toThrow();
1045
+
1046
+ await expect(
1047
+ (async () =>
1048
+ await alice.send(
1049
+ new TokenSendRequest({
1050
+ cashaddr: bob.cashaddr!,
1051
+ tokenId: tokenId,
1052
+ amount: 1,
1053
+ })
1054
+ ))()
1055
+ ).rejects.toThrow();
1056
+
1057
+ await expect(
1058
+ alice.send(
1059
+ new TokenSendRequest({
1060
+ cashaddr: bob.tokenaddr!,
1061
+ tokenId: tokenId,
1062
+ amount: 1,
1063
+ })
1064
+ )
1065
+ ).resolves.not.toThrow();
1013
1066
 
1014
1067
  Config.EnforceCashTokenReceiptAddresses = previousValue;
1015
1068
  });
package/src/wallet/Wif.ts CHANGED
@@ -61,7 +61,11 @@ import {
61
61
  BalanceResponse,
62
62
  } from "../util/balanceObjectFromSatoshi.js";
63
63
  import { checkWifNetwork } from "../util/checkWifNetwork.js";
64
- import { deriveCashaddr, deriveTokenaddr } from "../util/deriveCashaddr.js";
64
+ import {
65
+ deriveCashaddr,
66
+ deriveTokenaddr,
67
+ toTokenaddr,
68
+ } from "../util/deriveCashaddr.js";
65
69
  import {
66
70
  derivePrefix,
67
71
  derivePublicKeyHash,
@@ -107,7 +111,7 @@ import { DERIVATION_PATHS, DUST_UTXO_THRESHOLD } from "../constant.js";
107
111
 
108
112
  import { TransactionHistoryI } from "../history/interface.js";
109
113
  import { getAddressHistory } from "../history/electrumTransformer.js";
110
- import { IdentitySnapshot, Registry } from "./bcmr-v1.schema.js";
114
+ import { IdentitySnapshot, Registry } from "./bcmr-v2.schema.js";
111
115
  import { BCMR } from "./Bcmr.js";
112
116
  import { qrAddress } from "../qr/Qr.js";
113
117
  import { ImageI } from "../qr/interface.js";
@@ -1172,7 +1176,7 @@ export class Wallet extends BaseWallet {
1172
1176
  if (change > 0) {
1173
1177
  outputs.push(
1174
1178
  new TokenSendRequest({
1175
- cashaddr: changeAddress || this.tokenaddr!,
1179
+ cashaddr: toTokenaddr(changeAddress) || this.tokenaddr!,
1176
1180
  amount: change,
1177
1181
  tokenId: tokenId,
1178
1182
  commitment: tokenOutputs[0].commitment,