Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/bitcoin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/bitcoin",
"version": "1.9.0-beta.94",
"version": "1.9.0-beta.95",
"description": "Mesh Bitcoin package",
"main": "./dist/index.cjs",
"browser": "./dist/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/mesh-common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/common",
"version": "1.9.0-beta.94",
"version": "1.9.0-beta.95",
"description": "Contains constants, types and interfaces used across the SDK and different serialization libraries",
"main": "./dist/index.cjs",
"browser": "./dist/index.js",
Expand Down
72 changes: 65 additions & 7 deletions packages/mesh-common/src/data/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ import {
tokenName,
} from "./json";

/**
* Compare two hex strings by byte ordering (lexicographic comparison)
* Works because hex digits 0-9, a-f are in ascending ASCII order
*/
const compareByteOrder = (a: string, b: string): number =>
a < b ? -1 : a > b ? 1 : 0;

/**
* Aiken alias
* Value is the JSON representation of Cardano data Value
Expand Down Expand Up @@ -60,6 +67,29 @@ export class MeshValue {
this.value = value;
}

/**
* Sort a Value (JSON representation) by policy ID then token name
* @param plutusValue The Value to sort
* @returns Sorted Value
*/
static sortValue = (plutusValue: Value): Value => {
const sortedPolicies = [...plutusValue.map].sort((a, b) =>
compareByteOrder(a.k.bytes, b.k.bytes),
);

const sortedMap = sortedPolicies.map((policyEntry) => {
const sortedTokens = [...policyEntry.v.map].sort((a, b) =>
compareByteOrder(a.k.bytes, b.k.bytes),
);
return {
k: policyEntry.k,
v: { map: sortedTokens },
};
});

return { map: sortedMap };
};

/**
* Converting assets into MeshValue
* @param assets The assets to convert
Expand Down Expand Up @@ -296,19 +326,20 @@ export class MeshValue {

/**
* Convert the MeshValue object into Cardano data Value in Mesh Data type
* Entries are sorted by byte ordering of policy ID, then token name
*/
toData = (): MValue => {
const valueMap: MValue = new Map();
const unsortedMap: Map<string, Map<string, bigint>> = new Map();
this.toAssets().forEach((asset) => {
const sanitizedName = asset.unit.replace("lovelace", "");
const policy = sanitizedName.slice(0, 56) || "";
const token = sanitizedName.slice(56) || "";

if (!valueMap.has(policy)) {
valueMap.set(policy, new Map());
if (!unsortedMap.has(policy)) {
unsortedMap.set(policy, new Map());
}

const tokenMap = valueMap.get(policy)!;
const tokenMap = unsortedMap.get(policy)!;
const quantity = tokenMap?.get(token);
if (!quantity) {
tokenMap.set(token, BigInt(asset.quantity));
Expand All @@ -317,11 +348,30 @@ export class MeshValue {
}
});

// Sort by policy ID (byte ordering), then by token name (byte ordering)
const sortedPolicies = Array.from(unsortedMap.keys()).sort(compareByteOrder);
const valueMap: MValue = new Map();

sortedPolicies.forEach((policy) => {
const unsortedTokenMap = unsortedMap.get(policy)!;
const sortedTokens = Array.from(unsortedTokenMap.keys()).sort(
compareByteOrder,
);
const sortedTokenMap = new Map<string, bigint>();

sortedTokens.forEach((token) => {
sortedTokenMap.set(token, unsortedTokenMap.get(token)!);
});

valueMap.set(policy, sortedTokenMap);
});

return valueMap;
};

/**
* Convert the MeshValue object into a JSON representation of Cardano data Value
* Entries are sorted by byte ordering of policy ID, then token name
* @returns Cardano data Value in JSON
*/
toJSON = (): Value => {
Expand All @@ -345,11 +395,19 @@ export class MeshValue {
}
});

Object.keys(valueMap).forEach((policy) => {
// Sort policies by byte ordering
const sortedPolicies = Object.keys(valueMap).sort(compareByteOrder);

sortedPolicies.forEach((policy) => {
const policyByte = currencySymbol(policy);
const tokens: [TokenName, Integer][] = Object.keys(valueMap[policy]!).map(
(name) => [tokenName(name), integer(valueMap[policy]![name]!)],
// Sort tokens by byte ordering
const sortedTokenNames = Object.keys(valueMap[policy]!).sort(
compareByteOrder,
);
const tokens: [TokenName, Integer][] = sortedTokenNames.map((name) => [
tokenName(name),
integer(valueMap[policy]![name]!),
]);

const policyMap = assocMap(tokens);
valueMapToParse.push([policyByte, policyMap]);
Expand Down
77 changes: 77 additions & 0 deletions packages/mesh-common/test/data/value/convertor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,54 @@ import {

import { mockUnit } from "./common";

const unsortedValue: Value = assocMap([
[currencySymbol(""), assocMap([[tokenName(""), integer(200000000)]])],
[
currencySymbol("c69b981db7a65e339a6d783755f85a2e03afa1cece9714c55fe4c913"),
assocMap([[tokenName("5553444d"), integer(200000000)]]),
],
[
currencySymbol("a2818ba06a88bb6c08d10f4f9b897c09768f28d274093628ad7086fc"),
assocMap([[tokenName("484f534b59"), integer(100000000)]]),
],
[
currencySymbol("82e46eb16633bf8bfa820c83ffeb63192c6e21757d2bf91290b2f41d"),
assocMap([[tokenName("494147"), integer(100000000)]]),
],
[
currencySymbol("378f9732c755ed6f4fc8d406f1461d0cca95d7d2e69416784684df39"),
assocMap([[tokenName("534e454b"), integer(100000000)]]),
],
[
currencySymbol("3363b99384d6ee4c4b009068af396c8fdf92dafd111e58a857af0429"),
assocMap([[tokenName("4e49474854"), integer(100000000)]]),
],
]);

const sortedValue: Value = assocMap([
[currencySymbol(""), assocMap([[tokenName(""), integer(200000000)]])],
[
currencySymbol("3363b99384d6ee4c4b009068af396c8fdf92dafd111e58a857af0429"),
assocMap([[tokenName("4e49474854"), integer(100000000)]]),
],
[
currencySymbol("378f9732c755ed6f4fc8d406f1461d0cca95d7d2e69416784684df39"),
assocMap([[tokenName("534e454b"), integer(100000000)]]),
],
[
currencySymbol("82e46eb16633bf8bfa820c83ffeb63192c6e21757d2bf91290b2f41d"),
assocMap([[tokenName("494147"), integer(100000000)]]),
],
[
currencySymbol("a2818ba06a88bb6c08d10f4f9b897c09768f28d274093628ad7086fc"),
assocMap([[tokenName("484f534b59"), integer(100000000)]]),
],
[
currencySymbol("c69b981db7a65e339a6d783755f85a2e03afa1cece9714c55fe4c913"),
assocMap([[tokenName("5553444d"), integer(200000000)]]),
],
]);

describe("value", () => {
it("should create a new Value instance with the correct value", () => {
const val: Asset[] = [{ unit: "lovelace", quantity: "1000000" }];
Expand Down Expand Up @@ -269,4 +317,33 @@ describe("MeshValue class", () => {
expect(JSON.stringify(jsonValue)).toEqual(JSON.stringify(expectedValue));
});
});
describe("sortValue", () => {
test("should sort policies and tokens by byte ordering", () => {
const sortedValue = MeshValue.sortValue(unsortedValue);
expect(JSON.stringify(sortedValue)).toEqual(JSON.stringify(sortedValue));
});

test("should handle empty Value", () => {
const emptyValue: Value = assocMap([]);
const sortedValue = MeshValue.sortValue(emptyValue);
expect(JSON.stringify(sortedValue)).toEqual(JSON.stringify(emptyValue));
});

test("should handle already sorted Value", () => {
const alreadySorted: Value = assocMap([
[currencySymbol(""), assocMap([[tokenName(""), integer(200000000)]])],
[
currencySymbol(
"3363b99384d6ee4c4b009068af396c8fdf92dafd111e58a857af0429",
),
assocMap([[tokenName("4e49474854"), integer(100000000)]]),
],
]);

const sortedValue = MeshValue.sortValue(alreadySorted);
expect(JSON.stringify(sortedValue)).toEqual(
JSON.stringify(alreadySorted),
);
});
});
});
6 changes: 3 additions & 3 deletions packages/mesh-contract/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/contract",
"version": "1.9.0-beta.94",
"version": "1.9.0-beta.95",
"description": "List of open-source smart contracts, complete with documentation, live demos, and end-to-end source code. https://meshjs.dev/smart-contracts",
"main": "./dist/index.cjs",
"browser": "./dist/index.js",
Expand Down Expand Up @@ -34,8 +34,8 @@
"typescript": "^5.3.3"
},
"dependencies": {
"@meshsdk/common": "1.9.0-beta.94",
"@meshsdk/core": "1.9.0-beta.94",
"@meshsdk/common": "1.9.0-beta.95",
"@meshsdk/core": "1.9.0-beta.95",
"libsodium-wrappers-sumo": "0.7.15"
},
"prettier": "@meshsdk/configs/prettier",
Expand Down
6 changes: 3 additions & 3 deletions packages/mesh-core-csl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/core-csl",
"version": "1.9.0-beta.94",
"version": "1.9.0-beta.95",
"description": "Types and utilities functions between Mesh and cardano-serialization-lib",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
Expand Down Expand Up @@ -31,15 +31,15 @@
},
"devDependencies": {
"@meshsdk/configs": "*",
"@meshsdk/provider": "1.9.0-beta.94",
"@meshsdk/provider": "1.9.0-beta.95",
"@types/json-bigint": "^1.0.4",
"eslint": "^8.57.0",
"ts-jest": "^29.1.4",
"tsup": "^8.0.2",
"typescript": "^5.3.3"
},
"dependencies": {
"@meshsdk/common": "1.9.0-beta.94",
"@meshsdk/common": "1.9.0-beta.95",
"@sidan-lab/whisky-js-browser": "^1.0.11",
"@sidan-lab/whisky-js-nodejs": "^1.0.11",
"@types/base32-encoding": "^1.0.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/mesh-core-cst/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/core-cst",
"version": "1.9.0-beta.94",
"version": "1.9.0-beta.95",
"description": "Types and utilities functions between Mesh and cardano-js-sdk",
"main": "./dist/index.cjs",
"browser": "./dist/index.js",
Expand Down Expand Up @@ -44,7 +44,7 @@
"@harmoniclabs/plutus-data": "1.2.6",
"@harmoniclabs/uplc": "1.4.1",
"@harmoniclabs/pair": "^1.0.0",
"@meshsdk/common": "1.9.0-beta.94",
"@meshsdk/common": "1.9.0-beta.95",
"@types/base32-encoding": "^1.0.2",
"base32-encoding": "^1.0.0",
"bech32": "^2.0.0",
Expand Down
12 changes: 6 additions & 6 deletions packages/mesh-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/core",
"version": "1.9.0-beta.94",
"version": "1.9.0-beta.95",
"description": "Mesh SDK Core - https://meshjs.dev/",
"main": "./dist/index.cjs",
"browser": "./dist/index.js",
Expand Down Expand Up @@ -33,11 +33,11 @@
"typescript": "^5.3.3"
},
"dependencies": {
"@meshsdk/common": "1.9.0-beta.94",
"@meshsdk/core-cst": "1.9.0-beta.94",
"@meshsdk/provider": "1.9.0-beta.94",
"@meshsdk/transaction": "1.9.0-beta.94",
"@meshsdk/wallet": "1.9.0-beta.94",
"@meshsdk/common": "1.9.0-beta.95",
"@meshsdk/core-cst": "1.9.0-beta.95",
"@meshsdk/provider": "1.9.0-beta.95",
"@meshsdk/transaction": "1.9.0-beta.95",
"@meshsdk/wallet": "1.9.0-beta.95",
"libsodium-wrappers-sumo": "0.7.15"
},
"prettier": "@meshsdk/configs/prettier",
Expand Down
8 changes: 4 additions & 4 deletions packages/mesh-hydra/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/hydra",
"version": "1.9.0-beta.94",
"version": "1.9.0-beta.95",
"description": "Mesh Hydra package",
"main": "./dist/index.cjs",
"browser": "./dist/index.js",
Expand All @@ -27,9 +27,9 @@
"test": "jest"
},
"dependencies": {
"@meshsdk/common": "1.9.0-beta.94",
"@meshsdk/core": "1.9.0-beta.94",
"@meshsdk/core-cst": "1.9.0-beta.94",
"@meshsdk/common": "1.9.0-beta.95",
"@meshsdk/core": "1.9.0-beta.95",
"@meshsdk/core-cst": "1.9.0-beta.95",
"axios": "^1.7.2"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions packages/mesh-provider/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/provider",
"version": "1.9.0-beta.94",
"version": "1.9.0-beta.95",
"description": "Blockchain data providers - https://meshjs.dev/providers",
"main": "./dist/index.cjs",
"browser": "./dist/index.js",
Expand Down Expand Up @@ -35,9 +35,9 @@
"typescript": "^5.3.3"
},
"dependencies": {
"@meshsdk/bitcoin": "1.9.0-beta.94",
"@meshsdk/common": "1.9.0-beta.94",
"@meshsdk/core-cst": "1.9.0-beta.94",
"@meshsdk/bitcoin": "1.9.0-beta.95",
"@meshsdk/common": "1.9.0-beta.95",
"@meshsdk/core-cst": "1.9.0-beta.95",
"@utxorpc/sdk": "^0.6.7",
"@utxorpc/spec": "^0.16.0",
"axios": "^1.7.2",
Expand Down
10 changes: 5 additions & 5 deletions packages/mesh-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/react",
"version": "1.9.0-beta.94",
"version": "1.9.0-beta.95",
"description": "React component library - https://meshjs.dev/react",
"main": "./dist/index.cjs",
"browser": "./dist/index.js",
Expand Down Expand Up @@ -30,10 +30,10 @@
},
"dependencies": {
"@cardananium/cardano-peer-connect": "^1.2.19",
"@meshsdk/bitcoin": "1.9.0-beta.94",
"@meshsdk/common": "1.9.0-beta.94",
"@meshsdk/transaction": "1.9.0-beta.94",
"@meshsdk/wallet": "1.9.0-beta.94",
"@meshsdk/bitcoin": "1.9.0-beta.95",
"@meshsdk/common": "1.9.0-beta.95",
"@meshsdk/transaction": "1.9.0-beta.95",
"@meshsdk/wallet": "1.9.0-beta.95",
"@meshsdk/web3-sdk": "0.0.50",
"@radix-ui/react-dialog": "^1.1.2",
"@radix-ui/react-dropdown-menu": "^2.1.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/mesh-svelte/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meshsdk/svelte",
"version": "1.9.0-beta.94",
"version": "1.9.0-beta.95",
"description": "Svelte component library - https://meshjs.dev/svelte",
"type": "module",
"exports": {
Expand All @@ -26,7 +26,7 @@
"dev": "vite dev"
},
"dependencies": {
"@meshsdk/core": "1.9.0-beta.94",
"@meshsdk/core": "1.9.0-beta.95",
"bits-ui": "1.0.0-next.65"
},
"devDependencies": {
Expand Down
Loading
Loading