@meshsdk/wallet 2.0.0-beta.8 → 2.0.0-beta.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.
package/dist/index.cjs CHANGED
@@ -4,6 +4,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __getProtoOf = Object.getPrototypeOf;
6
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
8
  var __commonJS = (cb, mod) => function __require() {
8
9
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
9
10
  };
@@ -28,6 +29,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
29
  mod
29
30
  ));
30
31
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
31
33
 
32
34
  // node_modules/@noble/hashes/cryptoNode.js
33
35
  var require_cryptoNode = __commonJS({
@@ -24906,6 +24908,49 @@ var import_bech32 = __toESM(require_dist(), 1);
24906
24908
  var import_blake2b = __toESM(require_blake2b2(), 1);
24907
24909
  var import_blakejs = __toESM(require_blakejs(), 1);
24908
24910
  var import_bip393 = __toESM(require_src(), 1);
24911
+ var POLICY_ID_LENGTH = 56;
24912
+ var byteString = (bytes) => {
24913
+ if (bytes.length % 2 !== 0) {
24914
+ throw new Error("Invalid hex string - odd length: " + bytes);
24915
+ }
24916
+ if (!/^[0-9a-fA-F]*$/.test(bytes)) {
24917
+ throw new Error("Invalid hex string - non-hex string character: " + bytes);
24918
+ }
24919
+ return {
24920
+ bytes
24921
+ };
24922
+ };
24923
+ var integer = (int) => ({ int });
24924
+ var assocMap = (mapItems, validation = true) => ({
24925
+ map: mapItems.map(([k, v]) => {
24926
+ if (validation) {
24927
+ if (typeof k !== "object" || typeof v !== "object") {
24928
+ throw new Error(
24929
+ `Map item of JSON Cardano data type must be an object - ${k}, ${v}`
24930
+ );
24931
+ }
24932
+ }
24933
+ return { k, v };
24934
+ })
24935
+ });
24936
+ var policyId = (bytes) => {
24937
+ if (bytes.length !== POLICY_ID_LENGTH && bytes !== "") {
24938
+ throw new Error(
24939
+ `Invalid policy id for [${bytes}] - should be ${POLICY_ID_LENGTH / 2} bytes (${POLICY_ID_LENGTH} hex length) long or empty string for lovelace`
24940
+ );
24941
+ }
24942
+ return byteString(bytes);
24943
+ };
24944
+ var currencySymbol = (bytes) => policyId(bytes);
24945
+ var assetName = (bytes) => {
24946
+ if (bytes.length > 64) {
24947
+ throw new Error(
24948
+ `Invalid asset name for [${bytes}] - should be less than 32 bytes (64 hex length) long`
24949
+ );
24950
+ }
24951
+ return byteString(bytes);
24952
+ };
24953
+ var tokenName = (bytes) => assetName(bytes);
24909
24954
  var stringToHex = (str) => Buffer.from(str, "utf8").toString("hex");
24910
24955
  var isHexString = (hex) => /^[0-9A-F]*$/i.test(hex);
24911
24956
  var SLOT_CONFIG_NETWORK = {
@@ -24942,6 +24987,364 @@ var SLOT_CONFIG_NETWORK = {
24942
24987
  epochLength: 0
24943
24988
  }
24944
24989
  };
24990
+ var BigNum = class _BigNum {
24991
+ constructor(value2) {
24992
+ __publicField(this, "value");
24993
+ if (!value2) {
24994
+ this.value = BigInt(0);
24995
+ return;
24996
+ }
24997
+ this.value = BigInt(value2);
24998
+ }
24999
+ static new(value2) {
25000
+ if (!value2) {
25001
+ return new _BigNum(0);
25002
+ }
25003
+ return new _BigNum(value2);
25004
+ }
25005
+ // Operators
25006
+ divFloor(other) {
25007
+ this.value = this.value / other.value;
25008
+ return this;
25009
+ }
25010
+ checkedMul(other) {
25011
+ this.value *= other.value;
25012
+ return this;
25013
+ }
25014
+ checkedAdd(other) {
25015
+ this.value += other.value;
25016
+ return this;
25017
+ }
25018
+ checkedSub(other) {
25019
+ this.value -= other.value;
25020
+ return this;
25021
+ }
25022
+ clampedSub(other) {
25023
+ const result = this.value - other.value;
25024
+ this.value = result < 0n ? 0n : result;
25025
+ return this;
25026
+ }
25027
+ // Comparators
25028
+ lessThan(other) {
25029
+ return this.value < other.value;
25030
+ }
25031
+ compare(other) {
25032
+ if (this.value === other.value) {
25033
+ return 0;
25034
+ } else if (this.value < other.value) {
25035
+ return -1;
25036
+ } else {
25037
+ return 1;
25038
+ }
25039
+ }
25040
+ // Override
25041
+ toString() {
25042
+ return this.value.toString();
25043
+ }
25044
+ };
25045
+ var compareByteOrder = (a, b) => a < b ? -1 : a > b ? 1 : 0;
25046
+ var _a;
25047
+ var MeshValue = (_a = class {
25048
+ constructor(value2 = {}) {
25049
+ __publicField(this, "value");
25050
+ /**
25051
+ * Add an asset to the Value class's value record.
25052
+ * @param asset The asset to add
25053
+ * @returns The updated MeshValue object
25054
+ */
25055
+ __publicField(this, "addAsset", (asset) => {
25056
+ const quantity = BigInt(asset.quantity);
25057
+ const { unit } = asset;
25058
+ if (this.value[unit]) {
25059
+ this.value[unit] += quantity;
25060
+ } else {
25061
+ this.value[unit] = quantity;
25062
+ }
25063
+ return this;
25064
+ });
25065
+ /**
25066
+ * Add an array of assets to the Value class's value record.
25067
+ * @param assets The assets to add
25068
+ * @returns The updated MeshValue object
25069
+ */
25070
+ __publicField(this, "addAssets", (assets) => {
25071
+ assets.forEach((asset) => {
25072
+ this.addAsset(asset);
25073
+ });
25074
+ return this;
25075
+ });
25076
+ /**
25077
+ * Substract an asset from the Value class's value record.
25078
+ * @param asset The asset to subtract
25079
+ * @returns The updated MeshValue object
25080
+ */
25081
+ __publicField(this, "negateAsset", (asset) => {
25082
+ const { unit, quantity: assetQty } = asset;
25083
+ const quantity = BigNum.new(this.value[unit]);
25084
+ quantity.clampedSub(BigNum.new(assetQty));
25085
+ if (quantity.value === BigInt(0)) {
25086
+ delete this.value[unit];
25087
+ } else {
25088
+ this.value[unit] = quantity.value;
25089
+ }
25090
+ return this;
25091
+ });
25092
+ /**
25093
+ * Subtract an array of assets from the Value class's value record.
25094
+ * @param assets The assets to subtract
25095
+ * @returns The updated MeshValue object
25096
+ */
25097
+ __publicField(this, "negateAssets", (assets) => {
25098
+ assets.forEach((asset) => {
25099
+ this.negateAsset(asset);
25100
+ });
25101
+ return this;
25102
+ });
25103
+ /**
25104
+ * Get the quantity of asset object per unit
25105
+ * @param unit The unit to get the quantity of
25106
+ * @returns The quantity of the asset
25107
+ */
25108
+ __publicField(this, "get", (unit) => {
25109
+ return this.value[unit] ? BigInt(this.value[unit]) : BigInt(0);
25110
+ });
25111
+ /**
25112
+ * Get all assets that belong to a specific policy ID
25113
+ * @param policyId The policy ID to filter by
25114
+ * @returns Array of assets that match the policy ID
25115
+ */
25116
+ __publicField(this, "getPolicyAssets", (policyId2) => {
25117
+ const assets = [];
25118
+ Object.entries(this.value).forEach(([unit, quantity]) => {
25119
+ if (unit.startsWith(policyId2)) {
25120
+ assets.push({
25121
+ unit,
25122
+ quantity: quantity.toString()
25123
+ });
25124
+ }
25125
+ });
25126
+ return assets;
25127
+ });
25128
+ /**
25129
+ * Get all asset units
25130
+ * @returns The asset units
25131
+ */
25132
+ __publicField(this, "units", () => {
25133
+ return Object.keys(this.value);
25134
+ });
25135
+ /**
25136
+ * Check if the value is greater than or equal to another value
25137
+ * @param other - The value to compare against
25138
+ * @returns boolean
25139
+ */
25140
+ __publicField(this, "geq", (other) => {
25141
+ return Object.keys(other.value).every((key) => this.geqUnit(key, other));
25142
+ });
25143
+ /**
25144
+ * Check if the specific unit of value is greater than or equal to that unit of another value
25145
+ * @param unit - The unit to compare
25146
+ * @param other - The value to compare against
25147
+ * @returns boolean
25148
+ */
25149
+ __publicField(this, "geqUnit", (unit, other) => {
25150
+ if (this.value[unit] === void 0 || other.value[unit] === void 0) {
25151
+ return false;
25152
+ }
25153
+ return BigInt(this.value[unit]) >= BigInt(other.value[unit]);
25154
+ });
25155
+ /**
25156
+ * Check if the value is less than or equal to another value
25157
+ * @param other - The value to compare against
25158
+ * @returns boolean
25159
+ */
25160
+ __publicField(this, "leq", (other) => {
25161
+ return Object.keys(this.value).every((key) => this.leqUnit(key, other));
25162
+ });
25163
+ /**
25164
+ * Check if the specific unit of value is less than or equal to that unit of another value
25165
+ * @param unit - The unit to compare
25166
+ * @param other - The value to compare against
25167
+ * @returns boolean
25168
+ */
25169
+ __publicField(this, "leqUnit", (unit, other) => {
25170
+ if (this.value[unit] === void 0 || other.value[unit] === void 0) {
25171
+ return false;
25172
+ }
25173
+ return BigInt(this.value[unit]) <= BigInt(other.value[unit]);
25174
+ });
25175
+ /**
25176
+ * Check if the value is equal to another value
25177
+ * @param other - The value to compare against
25178
+ * @returns boolean
25179
+ */
25180
+ __publicField(this, "eq", (other) => {
25181
+ return Object.keys(this.value).every((key) => this.eqUnit(key, other));
25182
+ });
25183
+ /**
25184
+ * Check if the specific unit of value is equal to that unit of another value
25185
+ * @param unit - The unit to compare
25186
+ * @param other - The value to compare against
25187
+ * @returns boolean
25188
+ */
25189
+ __publicField(this, "eqUnit", (unit, other) => {
25190
+ if (this.value[unit] === void 0 || other.value[unit] === void 0) {
25191
+ return false;
25192
+ }
25193
+ return BigInt(this.value[unit]) === BigInt(other.value[unit]);
25194
+ });
25195
+ /**
25196
+ * Check if the value is empty
25197
+ * @returns boolean
25198
+ */
25199
+ __publicField(this, "isEmpty", () => {
25200
+ return Object.keys(this.value).length === 0;
25201
+ });
25202
+ /**
25203
+ * Merge the given values
25204
+ * @param values The other values to merge
25205
+ * @returns this
25206
+ */
25207
+ __publicField(this, "merge", (values) => {
25208
+ const valuesArray = Array.isArray(values) ? values : [values];
25209
+ valuesArray.forEach((other) => {
25210
+ Object.entries(other.value).forEach(([key, value2]) => {
25211
+ this.value[key] = (this.value[key] !== void 0 ? BigInt(this.value[key]) : BigInt(0)) + BigInt(value2);
25212
+ });
25213
+ });
25214
+ return this;
25215
+ });
25216
+ /**
25217
+ * Convert the MeshValue object into an array of Asset
25218
+ * @returns The array of Asset
25219
+ */
25220
+ __publicField(this, "toAssets", () => {
25221
+ const assets = [];
25222
+ Object.entries(this.value).forEach(([unit, quantity]) => {
25223
+ assets.push({ unit, quantity: quantity.toString() });
25224
+ });
25225
+ return assets;
25226
+ });
25227
+ /**
25228
+ * Convert the MeshValue object into Cardano data Value in Mesh Data type
25229
+ * Entries are sorted by byte ordering of policy ID, then token name
25230
+ */
25231
+ __publicField(this, "toData", () => {
25232
+ const unsortedMap = /* @__PURE__ */ new Map();
25233
+ this.toAssets().forEach((asset) => {
25234
+ const sanitizedName = asset.unit.replace("lovelace", "");
25235
+ const policy = sanitizedName.slice(0, 56) || "";
25236
+ const token = sanitizedName.slice(56) || "";
25237
+ if (!unsortedMap.has(policy)) {
25238
+ unsortedMap.set(policy, /* @__PURE__ */ new Map());
25239
+ }
25240
+ const tokenMap = unsortedMap.get(policy);
25241
+ const quantity = tokenMap?.get(token);
25242
+ if (!quantity) {
25243
+ tokenMap.set(token, BigInt(asset.quantity));
25244
+ } else {
25245
+ tokenMap.set(token, quantity + BigInt(asset.quantity));
25246
+ }
25247
+ });
25248
+ const sortedPolicies = Array.from(unsortedMap.keys()).sort(compareByteOrder);
25249
+ const valueMap = /* @__PURE__ */ new Map();
25250
+ sortedPolicies.forEach((policy) => {
25251
+ const unsortedTokenMap = unsortedMap.get(policy);
25252
+ const sortedTokens = Array.from(unsortedTokenMap.keys()).sort(
25253
+ compareByteOrder
25254
+ );
25255
+ const sortedTokenMap = /* @__PURE__ */ new Map();
25256
+ sortedTokens.forEach((token) => {
25257
+ sortedTokenMap.set(token, unsortedTokenMap.get(token));
25258
+ });
25259
+ valueMap.set(policy, sortedTokenMap);
25260
+ });
25261
+ return valueMap;
25262
+ });
25263
+ /**
25264
+ * Convert the MeshValue object into a JSON representation of Cardano data Value
25265
+ * Entries are sorted by byte ordering of policy ID, then token name
25266
+ * @returns Cardano data Value in JSON
25267
+ */
25268
+ __publicField(this, "toJSON", () => {
25269
+ const valueMapToParse = [];
25270
+ const valueMap = {};
25271
+ this.toAssets().forEach((asset) => {
25272
+ const sanitizedName = asset.unit.replace("lovelace", "");
25273
+ const policy = sanitizedName.slice(0, 56) || "";
25274
+ const token = sanitizedName.slice(56) || "";
25275
+ if (!valueMap[policy]) {
25276
+ valueMap[policy] = {};
25277
+ }
25278
+ if (!valueMap[policy][token]) {
25279
+ valueMap[policy][token] = Number(asset.quantity);
25280
+ } else {
25281
+ valueMap[policy][token] += Number(asset.quantity);
25282
+ }
25283
+ });
25284
+ const sortedPolicies = Object.keys(valueMap).sort(compareByteOrder);
25285
+ sortedPolicies.forEach((policy) => {
25286
+ const policyByte = currencySymbol(policy);
25287
+ const sortedTokenNames = Object.keys(valueMap[policy]).sort(
25288
+ compareByteOrder
25289
+ );
25290
+ const tokens = sortedTokenNames.map((name) => [
25291
+ tokenName(name),
25292
+ integer(valueMap[policy][name])
25293
+ ]);
25294
+ const policyMap = assocMap(tokens);
25295
+ valueMapToParse.push([policyByte, policyMap]);
25296
+ });
25297
+ return assocMap(valueMapToParse);
25298
+ });
25299
+ this.value = value2;
25300
+ }
25301
+ }, /**
25302
+ * Sort a Value (JSON representation) by policy ID then token name
25303
+ * @param plutusValue The Value to sort
25304
+ * @returns Sorted Value
25305
+ */
25306
+ __publicField(_a, "sortValue", (plutusValue) => {
25307
+ const sortedPolicies = [...plutusValue.map].sort(
25308
+ (a, b) => compareByteOrder(a.k.bytes, b.k.bytes)
25309
+ );
25310
+ const sortedMap = sortedPolicies.map((policyEntry) => {
25311
+ const sortedTokens = [...policyEntry.v.map].sort(
25312
+ (a, b) => compareByteOrder(a.k.bytes, b.k.bytes)
25313
+ );
25314
+ return {
25315
+ k: policyEntry.k,
25316
+ v: { map: sortedTokens }
25317
+ };
25318
+ });
25319
+ return { map: sortedMap };
25320
+ }), /**
25321
+ * Converting assets into MeshValue
25322
+ * @param assets The assets to convert
25323
+ * @returns MeshValue
25324
+ */
25325
+ __publicField(_a, "fromAssets", (assets) => {
25326
+ const value2 = new _a();
25327
+ value2.addAssets(assets);
25328
+ return value2;
25329
+ }), /**
25330
+ * Converting Value (the JSON representation of Cardano data Value) into MeshValue
25331
+ * @param plutusValue The Value to convert
25332
+ * @returns MeshValue
25333
+ */
25334
+ __publicField(_a, "fromValue", (plutusValue) => {
25335
+ const assets = [];
25336
+ plutusValue.map.forEach((policyMap) => {
25337
+ const policy = policyMap.k.bytes;
25338
+ policyMap.v.map.forEach((tokenMap) => {
25339
+ const token = tokenMap.k.bytes;
25340
+ const quantity = tokenMap.v.int.toString();
25341
+ const unsanitizedUnit = policy + token;
25342
+ const unit = unsanitizedUnit === "" ? "lovelace" : unsanitizedUnit;
25343
+ assets.push({ unit, quantity });
25344
+ });
25345
+ });
25346
+ return _a.fromAssets(assets);
25347
+ }), _a);
24945
25348
 
24946
25349
  // src/cardano/signer/cip-8.ts
24947
25350
  var Crypto = __toESM(require("@cardano-sdk/crypto"), 1);
@@ -25819,7 +26222,9 @@ var MeshCardanoHeadlessWallet = class _MeshCardanoHeadlessWallet extends Cardano
25819
26222
  throw new Error("[CardanoWallet] No fetcher provided");
25820
26223
  }
25821
26224
  const utxos = await this.fetchAccountUtxos();
25822
- return utxos.map((utxo) => utxo.output.amount).flat();
26225
+ const values = utxos.map((utxo) => utxo.output.amount).flat();
26226
+ const meshValue = MeshValue.fromAssets(values);
26227
+ return meshValue.toAssets();
25823
26228
  }
25824
26229
  /**
25825
26230
  * Get the used addresses for the wallet.
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __getProtoOf = Object.getPrototypeOf;
6
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
8
  var __commonJS = (cb, mod) => function __require() {
8
9
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
9
10
  };
@@ -23,6 +24,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
24
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
24
25
  mod
25
26
  ));
27
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
26
28
 
27
29
  // node_modules/@noble/hashes/crypto.js
28
30
  var require_crypto = __commonJS({
@@ -25492,6 +25494,49 @@ var import_bech32 = __toESM(require_dist(), 1);
25492
25494
  var import_blake2b = __toESM(require_blake2b2(), 1);
25493
25495
  var import_blakejs = __toESM(require_blakejs(), 1);
25494
25496
  var import_bip393 = __toESM(require_src(), 1);
25497
+ var POLICY_ID_LENGTH = 56;
25498
+ var byteString = (bytes) => {
25499
+ if (bytes.length % 2 !== 0) {
25500
+ throw new Error("Invalid hex string - odd length: " + bytes);
25501
+ }
25502
+ if (!/^[0-9a-fA-F]*$/.test(bytes)) {
25503
+ throw new Error("Invalid hex string - non-hex string character: " + bytes);
25504
+ }
25505
+ return {
25506
+ bytes
25507
+ };
25508
+ };
25509
+ var integer = (int) => ({ int });
25510
+ var assocMap = (mapItems, validation = true) => ({
25511
+ map: mapItems.map(([k, v]) => {
25512
+ if (validation) {
25513
+ if (typeof k !== "object" || typeof v !== "object") {
25514
+ throw new Error(
25515
+ `Map item of JSON Cardano data type must be an object - ${k}, ${v}`
25516
+ );
25517
+ }
25518
+ }
25519
+ return { k, v };
25520
+ })
25521
+ });
25522
+ var policyId = (bytes) => {
25523
+ if (bytes.length !== POLICY_ID_LENGTH && bytes !== "") {
25524
+ throw new Error(
25525
+ `Invalid policy id for [${bytes}] - should be ${POLICY_ID_LENGTH / 2} bytes (${POLICY_ID_LENGTH} hex length) long or empty string for lovelace`
25526
+ );
25527
+ }
25528
+ return byteString(bytes);
25529
+ };
25530
+ var currencySymbol = (bytes) => policyId(bytes);
25531
+ var assetName = (bytes) => {
25532
+ if (bytes.length > 64) {
25533
+ throw new Error(
25534
+ `Invalid asset name for [${bytes}] - should be less than 32 bytes (64 hex length) long`
25535
+ );
25536
+ }
25537
+ return byteString(bytes);
25538
+ };
25539
+ var tokenName = (bytes) => assetName(bytes);
25495
25540
  var stringToHex = (str) => Buffer.from(str, "utf8").toString("hex");
25496
25541
  var isHexString = (hex) => /^[0-9A-F]*$/i.test(hex);
25497
25542
  var SLOT_CONFIG_NETWORK = {
@@ -25528,6 +25573,364 @@ var SLOT_CONFIG_NETWORK = {
25528
25573
  epochLength: 0
25529
25574
  }
25530
25575
  };
25576
+ var BigNum = class _BigNum {
25577
+ constructor(value2) {
25578
+ __publicField(this, "value");
25579
+ if (!value2) {
25580
+ this.value = BigInt(0);
25581
+ return;
25582
+ }
25583
+ this.value = BigInt(value2);
25584
+ }
25585
+ static new(value2) {
25586
+ if (!value2) {
25587
+ return new _BigNum(0);
25588
+ }
25589
+ return new _BigNum(value2);
25590
+ }
25591
+ // Operators
25592
+ divFloor(other) {
25593
+ this.value = this.value / other.value;
25594
+ return this;
25595
+ }
25596
+ checkedMul(other) {
25597
+ this.value *= other.value;
25598
+ return this;
25599
+ }
25600
+ checkedAdd(other) {
25601
+ this.value += other.value;
25602
+ return this;
25603
+ }
25604
+ checkedSub(other) {
25605
+ this.value -= other.value;
25606
+ return this;
25607
+ }
25608
+ clampedSub(other) {
25609
+ const result = this.value - other.value;
25610
+ this.value = result < 0n ? 0n : result;
25611
+ return this;
25612
+ }
25613
+ // Comparators
25614
+ lessThan(other) {
25615
+ return this.value < other.value;
25616
+ }
25617
+ compare(other) {
25618
+ if (this.value === other.value) {
25619
+ return 0;
25620
+ } else if (this.value < other.value) {
25621
+ return -1;
25622
+ } else {
25623
+ return 1;
25624
+ }
25625
+ }
25626
+ // Override
25627
+ toString() {
25628
+ return this.value.toString();
25629
+ }
25630
+ };
25631
+ var compareByteOrder = (a, b) => a < b ? -1 : a > b ? 1 : 0;
25632
+ var _a;
25633
+ var MeshValue = (_a = class {
25634
+ constructor(value2 = {}) {
25635
+ __publicField(this, "value");
25636
+ /**
25637
+ * Add an asset to the Value class's value record.
25638
+ * @param asset The asset to add
25639
+ * @returns The updated MeshValue object
25640
+ */
25641
+ __publicField(this, "addAsset", (asset) => {
25642
+ const quantity = BigInt(asset.quantity);
25643
+ const { unit } = asset;
25644
+ if (this.value[unit]) {
25645
+ this.value[unit] += quantity;
25646
+ } else {
25647
+ this.value[unit] = quantity;
25648
+ }
25649
+ return this;
25650
+ });
25651
+ /**
25652
+ * Add an array of assets to the Value class's value record.
25653
+ * @param assets The assets to add
25654
+ * @returns The updated MeshValue object
25655
+ */
25656
+ __publicField(this, "addAssets", (assets) => {
25657
+ assets.forEach((asset) => {
25658
+ this.addAsset(asset);
25659
+ });
25660
+ return this;
25661
+ });
25662
+ /**
25663
+ * Substract an asset from the Value class's value record.
25664
+ * @param asset The asset to subtract
25665
+ * @returns The updated MeshValue object
25666
+ */
25667
+ __publicField(this, "negateAsset", (asset) => {
25668
+ const { unit, quantity: assetQty } = asset;
25669
+ const quantity = BigNum.new(this.value[unit]);
25670
+ quantity.clampedSub(BigNum.new(assetQty));
25671
+ if (quantity.value === BigInt(0)) {
25672
+ delete this.value[unit];
25673
+ } else {
25674
+ this.value[unit] = quantity.value;
25675
+ }
25676
+ return this;
25677
+ });
25678
+ /**
25679
+ * Subtract an array of assets from the Value class's value record.
25680
+ * @param assets The assets to subtract
25681
+ * @returns The updated MeshValue object
25682
+ */
25683
+ __publicField(this, "negateAssets", (assets) => {
25684
+ assets.forEach((asset) => {
25685
+ this.negateAsset(asset);
25686
+ });
25687
+ return this;
25688
+ });
25689
+ /**
25690
+ * Get the quantity of asset object per unit
25691
+ * @param unit The unit to get the quantity of
25692
+ * @returns The quantity of the asset
25693
+ */
25694
+ __publicField(this, "get", (unit) => {
25695
+ return this.value[unit] ? BigInt(this.value[unit]) : BigInt(0);
25696
+ });
25697
+ /**
25698
+ * Get all assets that belong to a specific policy ID
25699
+ * @param policyId The policy ID to filter by
25700
+ * @returns Array of assets that match the policy ID
25701
+ */
25702
+ __publicField(this, "getPolicyAssets", (policyId2) => {
25703
+ const assets = [];
25704
+ Object.entries(this.value).forEach(([unit, quantity]) => {
25705
+ if (unit.startsWith(policyId2)) {
25706
+ assets.push({
25707
+ unit,
25708
+ quantity: quantity.toString()
25709
+ });
25710
+ }
25711
+ });
25712
+ return assets;
25713
+ });
25714
+ /**
25715
+ * Get all asset units
25716
+ * @returns The asset units
25717
+ */
25718
+ __publicField(this, "units", () => {
25719
+ return Object.keys(this.value);
25720
+ });
25721
+ /**
25722
+ * Check if the value is greater than or equal to another value
25723
+ * @param other - The value to compare against
25724
+ * @returns boolean
25725
+ */
25726
+ __publicField(this, "geq", (other) => {
25727
+ return Object.keys(other.value).every((key) => this.geqUnit(key, other));
25728
+ });
25729
+ /**
25730
+ * Check if the specific unit of value is greater than or equal to that unit of another value
25731
+ * @param unit - The unit to compare
25732
+ * @param other - The value to compare against
25733
+ * @returns boolean
25734
+ */
25735
+ __publicField(this, "geqUnit", (unit, other) => {
25736
+ if (this.value[unit] === void 0 || other.value[unit] === void 0) {
25737
+ return false;
25738
+ }
25739
+ return BigInt(this.value[unit]) >= BigInt(other.value[unit]);
25740
+ });
25741
+ /**
25742
+ * Check if the value is less than or equal to another value
25743
+ * @param other - The value to compare against
25744
+ * @returns boolean
25745
+ */
25746
+ __publicField(this, "leq", (other) => {
25747
+ return Object.keys(this.value).every((key) => this.leqUnit(key, other));
25748
+ });
25749
+ /**
25750
+ * Check if the specific unit of value is less than or equal to that unit of another value
25751
+ * @param unit - The unit to compare
25752
+ * @param other - The value to compare against
25753
+ * @returns boolean
25754
+ */
25755
+ __publicField(this, "leqUnit", (unit, other) => {
25756
+ if (this.value[unit] === void 0 || other.value[unit] === void 0) {
25757
+ return false;
25758
+ }
25759
+ return BigInt(this.value[unit]) <= BigInt(other.value[unit]);
25760
+ });
25761
+ /**
25762
+ * Check if the value is equal to another value
25763
+ * @param other - The value to compare against
25764
+ * @returns boolean
25765
+ */
25766
+ __publicField(this, "eq", (other) => {
25767
+ return Object.keys(this.value).every((key) => this.eqUnit(key, other));
25768
+ });
25769
+ /**
25770
+ * Check if the specific unit of value is equal to that unit of another value
25771
+ * @param unit - The unit to compare
25772
+ * @param other - The value to compare against
25773
+ * @returns boolean
25774
+ */
25775
+ __publicField(this, "eqUnit", (unit, other) => {
25776
+ if (this.value[unit] === void 0 || other.value[unit] === void 0) {
25777
+ return false;
25778
+ }
25779
+ return BigInt(this.value[unit]) === BigInt(other.value[unit]);
25780
+ });
25781
+ /**
25782
+ * Check if the value is empty
25783
+ * @returns boolean
25784
+ */
25785
+ __publicField(this, "isEmpty", () => {
25786
+ return Object.keys(this.value).length === 0;
25787
+ });
25788
+ /**
25789
+ * Merge the given values
25790
+ * @param values The other values to merge
25791
+ * @returns this
25792
+ */
25793
+ __publicField(this, "merge", (values) => {
25794
+ const valuesArray = Array.isArray(values) ? values : [values];
25795
+ valuesArray.forEach((other) => {
25796
+ Object.entries(other.value).forEach(([key, value2]) => {
25797
+ this.value[key] = (this.value[key] !== void 0 ? BigInt(this.value[key]) : BigInt(0)) + BigInt(value2);
25798
+ });
25799
+ });
25800
+ return this;
25801
+ });
25802
+ /**
25803
+ * Convert the MeshValue object into an array of Asset
25804
+ * @returns The array of Asset
25805
+ */
25806
+ __publicField(this, "toAssets", () => {
25807
+ const assets = [];
25808
+ Object.entries(this.value).forEach(([unit, quantity]) => {
25809
+ assets.push({ unit, quantity: quantity.toString() });
25810
+ });
25811
+ return assets;
25812
+ });
25813
+ /**
25814
+ * Convert the MeshValue object into Cardano data Value in Mesh Data type
25815
+ * Entries are sorted by byte ordering of policy ID, then token name
25816
+ */
25817
+ __publicField(this, "toData", () => {
25818
+ const unsortedMap = /* @__PURE__ */ new Map();
25819
+ this.toAssets().forEach((asset) => {
25820
+ const sanitizedName = asset.unit.replace("lovelace", "");
25821
+ const policy = sanitizedName.slice(0, 56) || "";
25822
+ const token = sanitizedName.slice(56) || "";
25823
+ if (!unsortedMap.has(policy)) {
25824
+ unsortedMap.set(policy, /* @__PURE__ */ new Map());
25825
+ }
25826
+ const tokenMap = unsortedMap.get(policy);
25827
+ const quantity = tokenMap?.get(token);
25828
+ if (!quantity) {
25829
+ tokenMap.set(token, BigInt(asset.quantity));
25830
+ } else {
25831
+ tokenMap.set(token, quantity + BigInt(asset.quantity));
25832
+ }
25833
+ });
25834
+ const sortedPolicies = Array.from(unsortedMap.keys()).sort(compareByteOrder);
25835
+ const valueMap = /* @__PURE__ */ new Map();
25836
+ sortedPolicies.forEach((policy) => {
25837
+ const unsortedTokenMap = unsortedMap.get(policy);
25838
+ const sortedTokens = Array.from(unsortedTokenMap.keys()).sort(
25839
+ compareByteOrder
25840
+ );
25841
+ const sortedTokenMap = /* @__PURE__ */ new Map();
25842
+ sortedTokens.forEach((token) => {
25843
+ sortedTokenMap.set(token, unsortedTokenMap.get(token));
25844
+ });
25845
+ valueMap.set(policy, sortedTokenMap);
25846
+ });
25847
+ return valueMap;
25848
+ });
25849
+ /**
25850
+ * Convert the MeshValue object into a JSON representation of Cardano data Value
25851
+ * Entries are sorted by byte ordering of policy ID, then token name
25852
+ * @returns Cardano data Value in JSON
25853
+ */
25854
+ __publicField(this, "toJSON", () => {
25855
+ const valueMapToParse = [];
25856
+ const valueMap = {};
25857
+ this.toAssets().forEach((asset) => {
25858
+ const sanitizedName = asset.unit.replace("lovelace", "");
25859
+ const policy = sanitizedName.slice(0, 56) || "";
25860
+ const token = sanitizedName.slice(56) || "";
25861
+ if (!valueMap[policy]) {
25862
+ valueMap[policy] = {};
25863
+ }
25864
+ if (!valueMap[policy][token]) {
25865
+ valueMap[policy][token] = Number(asset.quantity);
25866
+ } else {
25867
+ valueMap[policy][token] += Number(asset.quantity);
25868
+ }
25869
+ });
25870
+ const sortedPolicies = Object.keys(valueMap).sort(compareByteOrder);
25871
+ sortedPolicies.forEach((policy) => {
25872
+ const policyByte = currencySymbol(policy);
25873
+ const sortedTokenNames = Object.keys(valueMap[policy]).sort(
25874
+ compareByteOrder
25875
+ );
25876
+ const tokens = sortedTokenNames.map((name) => [
25877
+ tokenName(name),
25878
+ integer(valueMap[policy][name])
25879
+ ]);
25880
+ const policyMap = assocMap(tokens);
25881
+ valueMapToParse.push([policyByte, policyMap]);
25882
+ });
25883
+ return assocMap(valueMapToParse);
25884
+ });
25885
+ this.value = value2;
25886
+ }
25887
+ }, /**
25888
+ * Sort a Value (JSON representation) by policy ID then token name
25889
+ * @param plutusValue The Value to sort
25890
+ * @returns Sorted Value
25891
+ */
25892
+ __publicField(_a, "sortValue", (plutusValue) => {
25893
+ const sortedPolicies = [...plutusValue.map].sort(
25894
+ (a, b) => compareByteOrder(a.k.bytes, b.k.bytes)
25895
+ );
25896
+ const sortedMap = sortedPolicies.map((policyEntry) => {
25897
+ const sortedTokens = [...policyEntry.v.map].sort(
25898
+ (a, b) => compareByteOrder(a.k.bytes, b.k.bytes)
25899
+ );
25900
+ return {
25901
+ k: policyEntry.k,
25902
+ v: { map: sortedTokens }
25903
+ };
25904
+ });
25905
+ return { map: sortedMap };
25906
+ }), /**
25907
+ * Converting assets into MeshValue
25908
+ * @param assets The assets to convert
25909
+ * @returns MeshValue
25910
+ */
25911
+ __publicField(_a, "fromAssets", (assets) => {
25912
+ const value2 = new _a();
25913
+ value2.addAssets(assets);
25914
+ return value2;
25915
+ }), /**
25916
+ * Converting Value (the JSON representation of Cardano data Value) into MeshValue
25917
+ * @param plutusValue The Value to convert
25918
+ * @returns MeshValue
25919
+ */
25920
+ __publicField(_a, "fromValue", (plutusValue) => {
25921
+ const assets = [];
25922
+ plutusValue.map.forEach((policyMap) => {
25923
+ const policy = policyMap.k.bytes;
25924
+ policyMap.v.map.forEach((tokenMap) => {
25925
+ const token = tokenMap.k.bytes;
25926
+ const quantity = tokenMap.v.int.toString();
25927
+ const unsanitizedUnit = policy + token;
25928
+ const unit = unsanitizedUnit === "" ? "lovelace" : unsanitizedUnit;
25929
+ assets.push({ unit, quantity });
25930
+ });
25931
+ });
25932
+ return _a.fromAssets(assets);
25933
+ }), _a);
25531
25934
 
25532
25935
  // src/cardano/signer/cip-8.ts
25533
25936
  var import_blakejs2 = __toESM(require_blakejs(), 1);
@@ -26416,7 +26819,9 @@ var MeshCardanoHeadlessWallet = class _MeshCardanoHeadlessWallet extends Cardano
26416
26819
  throw new Error("[CardanoWallet] No fetcher provided");
26417
26820
  }
26418
26821
  const utxos = await this.fetchAccountUtxos();
26419
- return utxos.map((utxo) => utxo.output.amount).flat();
26822
+ const values = utxos.map((utxo) => utxo.output.amount).flat();
26823
+ const meshValue = MeshValue.fromAssets(values);
26824
+ return meshValue.toAssets();
26420
26825
  }
26421
26826
  /**
26422
26827
  * Get the used addresses for the wallet.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meshsdk/wallet",
3
- "version": "2.0.0-beta.8",
3
+ "version": "2.0.0-beta.9",
4
4
  "description": "Wallets - https://meshjs.dev/apis/wallets",
5
5
  "main": "./dist/index.cjs",
6
6
  "browser": "./dist/index.js",
@@ -34,7 +34,8 @@
34
34
  "@ianvs/prettier-plugin-sort-imports": "^4.4.1",
35
35
  "@meshsdk/common": "1.9.0-beta.100",
36
36
  "@meshsdk/provider": "1.9.0-beta.99",
37
- "@types/jest": "^29.5.12",
37
+ "@types/jest": "^29.5.14",
38
+ "@types/mocha": "^10.0.10",
38
39
  "dotenv": "^16.4.5",
39
40
  "eslint": "^8.57.0",
40
41
  "jest": "^29.7.0",