Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] merge metadata by tag #416

Merged
merged 11 commits into from
Dec 16, 2024
2 changes: 1 addition & 1 deletion apps/playground/src/pages/api/donate-mint-mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default async function handler(
.selectUtxosFrom(utxos)
.mint("1", policyId, stringToHex(assetName))
.mintingScript(forgingScript)
.metadataValue("721", fullAssetMetadata)
.metadataValue(721, fullAssetMetadata)
.txOut(donateAddress, [
{ unit: "lovelace", quantity: costLovelace.toString() },
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function Left() {
The specification for the individual strings follow the general design
specification for JSON metadata, which is already implemented and in
operation on the cardano blockchain. The used metadatum label is{" "}
<code>674</code>:, this number was choosen because it is the T9 encoding
<code>674</code>:, this number was chosen because it is the T9 encoding
of the string
<code>msg</code>. The message content has the key <code>msg</code>: and
consists of an array of individual message-strings. The number of theses
Expand Down
12 changes: 6 additions & 6 deletions apps/playground/src/pages/apis/txbuilder/basics/cip20.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function TxbuilderCip20() {

function Left() {
let code = `txBuilder\n`;
code += ` .metadataValue(tag, metadata)\n`;
code += ` .metadataValue(label, metadata)\n`;

return (
<>
Expand All @@ -36,7 +36,7 @@ function Left() {
The specification for the individual strings follow the general design
specification for JSON metadata, which is already implemented and in
operation on the cardano blockchain. The used metadatum label is{" "}
<code>674</code>: this number was choosen because it is the T9 encoding
<code>674</code>: this number was chosen because it is the T9 encoding
of the string
<code>msg</code>. The message content has the key <code>msg</code>: and
consists of an array of individual message-strings. The number of theses
Expand All @@ -60,14 +60,14 @@ function Right() {
const changeAddress = await wallet.getChangeAddress();
const txBuilder = getTxBuilder();

const tag = "674";
const label = 674;
const metadata = {
msg: message.split("\n"),
};

const unsignedTx = await txBuilder
.changeAddress(changeAddress)
.metadataValue(tag.toString(), metadata)
.metadataValue(label, metadata)
.selectUtxosFrom(utxos)
.complete();

Expand All @@ -82,7 +82,7 @@ function Right() {
codeSnippet += `const changeAddress = await wallet.getChangeAddress();\n`;
codeSnippet += `const txBuilder = getTxBuilder();\n`;
codeSnippet += `\n`;
codeSnippet += `const tag = "674";\n`;
codeSnippet += `const label = 674;\n`;
codeSnippet += `const metadata = {\n`;
codeSnippet += ` msg: [\n`;
for (let line of message.split("\n")) {
Expand All @@ -92,7 +92,7 @@ function Right() {
codeSnippet += `});\n\n`;
codeSnippet += `const unsignedTx = await txBuilder\n`;
codeSnippet += ` .changeAddress(changeAddress)\n`;
codeSnippet += ` .metadataValue(tag, metadata)\n`;
codeSnippet += ` .metadataValue(label, metadata)\n`;
codeSnippet += ` .selectUtxosFrom(utxos)\n`;
codeSnippet += ` .complete();\n`;
codeSnippet += `\n`;
Expand Down
6 changes: 3 additions & 3 deletions apps/playground/src/pages/apis/txbuilder/basics/multisig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function Left() {
codeTx += `const unsignedTx = await txBuilder\n`;
codeTx += ` .mint("1", policyId, stringToHex("MeshToken"))\n`;
codeTx += ` .mintingScript(forgingScript)\n`;
codeTx += ` .metadataValue("721", { [policyId]: { [assetName]: demoAssetMetadata } })\n`;
codeTx += ` .metadataValue(721, { [policyId]: { [assetName]: demoAssetMetadata } })\n`;
codeTx += ` .changeAddress(address)\n`;
codeTx += ` .selectUtxosFrom(utxos)\n`;
codeTx += ` .complete();\n`;
Expand Down Expand Up @@ -88,7 +88,7 @@ function Right() {
const unsignedTx = await txBuilder
.mint("1", policyId, stringToHex("MeshToken"))
.mintingScript(forgingScript)
.metadataValue("721", { [policyId]: { [assetName]: demoAssetMetadata } })
.metadataValue(721, { [policyId]: { [assetName]: demoAssetMetadata } })
.changeAddress(address)
.selectUtxosFrom(utxos)
.complete();
Expand Down Expand Up @@ -124,7 +124,7 @@ function Right() {
codeSnippet += `const unsignedTx = await txBuilder\n`;
codeSnippet += ` .mint("1", policyId, stringToHex("MeshToken"))\n`;
codeSnippet += ` .mintingScript(forgingScript)\n`;
codeSnippet += ` .metadataValue("721", { [policyId]: { [assetName]: demoAssetMetadata } })\n`;
codeSnippet += ` .metadataValue(721, { [policyId]: { [assetName]: demoAssetMetadata } })\n`;
codeSnippet += ` .changeAddress(address)\n`;
codeSnippet += ` .selectUtxosFrom(utxos)\n`;
codeSnippet += ` .complete();\n`;
Expand Down
10 changes: 5 additions & 5 deletions apps/playground/src/pages/apis/txbuilder/basics/set-metadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function TxbuilderSetMetadata() {

function Left() {
let code = `txBuilder\n`;
code += ` .metadataValue(tag, metadata)\n`;
code += ` .metadataValue(label, metadata)\n`;

return (
<>
Expand All @@ -47,12 +47,12 @@ function Right() {
const changeAddress = await wallet.getChangeAddress();
const txBuilder = getTxBuilder();

const tag = "0";
const label = 0;
const metadata = "This is a message from the Mesh SDK";

const unsignedTx = await txBuilder
.changeAddress(changeAddress)
.metadataValue(tag.toString(), metadata)
.metadataValue(label, metadata)
.selectUtxosFrom(utxos)
.complete();

Expand All @@ -66,11 +66,11 @@ function Right() {
codeSnippet += `const address = await wallet.getChangeAddress();\n`;
codeSnippet += `const txBuilder = getTxBuilder();\n`;
codeSnippet += `\n`;
codeSnippet += `const tag = "0";\n`;
codeSnippet += `const label = 0;\n`;
codeSnippet += `const metadata = "This is a message from the Mesh SDK";\n\n`;
codeSnippet += `const unsignedTx = await txBuilder\n`;
codeSnippet += ` .changeAddress(address)\n`;
codeSnippet += ` .metadataValue(tag, metadata)\n`;
codeSnippet += ` .metadataValue(label, metadata)\n`;
codeSnippet += ` .selectUtxosFrom(utxos)\n`;
codeSnippet += ` .complete();\n`;
codeSnippet += `\n`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function Right() {
const unsignedTx = await txBuilder
.mint("1", policyId, tokenNameHex)
.mintingScript(forgingScript)
.metadataValue("721", metadata)
.metadataValue(721, metadata)
.changeAddress(changeAddress)
.invalidHereafter(99999999)
.selectUtxosFrom(utxos)
Expand Down Expand Up @@ -125,7 +125,7 @@ const txBuilder = getTxBuilder();
const unsignedTx = await txBuilder
.mint("1", policyId, tokenNameHex)
.mintingScript(forgingScript)
.metadataValue("721", metadata)
.metadataValue(721, metadata)
.changeAddress(changeAddress)
.invalidHereafter(99999999)
.selectUtxosFrom(utxos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function Right() {
const unsignedTx = await txBuilder
.mint("1", policyId, tokenNameHex)
.mintingScript(forgingScript)
.metadataValue("721", metadata)
.metadataValue(721, metadata)
.changeAddress(changeAddress)
.selectUtxosFrom(utxos)
.complete();
Expand Down Expand Up @@ -124,7 +124,7 @@ function Right() {
codeSnippet += `const unsignedTx = await txBuilder\n`;
codeSnippet += ` .mint("1", policyId, tokenNameHex)\n`;
codeSnippet += ` .mintingScript(forgingScript)\n`;
codeSnippet += ` .metadataValue("721", metadata)\n`;
codeSnippet += ` .metadataValue(721, metadata)\n`;
codeSnippet += ` .changeAddress(changeAddress)\n`;
codeSnippet += ` .selectUtxosFrom(utxos)\n`;
codeSnippet += ` .complete();\n`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function Left() {
codeSnippet3 += ` .mint("1", policyId, tokenNameHex)\n`;
codeSnippet3 += ` .mintingScript(demoPlutusMintingScript)\n`;
codeSnippet3 += ` .mintRedeemerValue(mConStr0([userInput]))\n`;
codeSnippet3 += ` .metadataValue("721", metadata)\n`;
codeSnippet3 += ` .metadataValue(721, metadata)\n`;
codeSnippet3 += ` .changeAddress(changeAddress)\n`;
codeSnippet3 += ` .selectUtxosFrom(utxos)\n`;
codeSnippet3 += ` .txInCollateral(\n`;
Expand Down Expand Up @@ -125,7 +125,7 @@ function Right() {
.mint("1", policyId, tokenNameHex)
.mintingScript(demoPlutusMintingScript)
.mintRedeemerValue(mConStr0([userInput]))
.metadataValue("721", metadata)
.metadataValue(721, metadata)
.changeAddress(changeAddress)
.selectUtxosFrom(utxos)
.txInCollateral(
Expand Down Expand Up @@ -159,7 +159,7 @@ function Right() {
code += ` .mint("1", policyId, tokenNameHex)\n`;
code += ` .mintingScript(demoPlutusMintingScript)\n`;
code += ` .mintRedeemerValue(mConStr0(['${userInput}']))\n`;
code += ` .metadataValue("721", metadata)\n`;
code += ` .metadataValue(721, metadata)\n`;
code += ` .changeAddress(changeAddress)\n`;
code += ` .selectUtxosFrom(utxos)\n`;
code += ` .txInCollateral(\n`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function Right() {
const unsignedTx = await txBuilder
.mint("1", policyId, "")
.mintingScript(forgingScript)
.metadataValue("777", assetMetadata)
.metadataValue(777, assetMetadata)
.changeAddress(address)
.selectUtxosFrom(utxos)
.complete();
Expand Down Expand Up @@ -114,7 +114,7 @@ function Right() {
code += `const unsignedTx = await txBuilder\n`;
code += ` .mint("1", policyId, "")\n`;
code += ` .mintingScript(forgingScript)\n`;
code += ` .metadataValue("777", assetMetadata)\n`;
code += ` .metadataValue(777, assetMetadata)\n`;
code += ` .changeAddress(address)\n`;
code += ` .selectUtxosFrom(utxos)\n`;
code += ` .complete();\n`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function Left() {

let codeSnippet2 = ``;
codeSnippet2 += `txBuilder\n`;
codeSnippet2 += ` .metadataValue("721", metadata)\n`;
codeSnippet2 += ` .metadataValue(721, metadata)\n`;
codeSnippet2 += ` .changeAddress(changeAddress)\n`;
codeSnippet2 += ` .selectUtxosFrom(utxos);\n`;
codeSnippet2 += `\n`;
Expand Down Expand Up @@ -88,7 +88,7 @@ function Right() {
}

txBuilder
.metadataValue("721", metadata)
.metadataValue(721, metadata)
.changeAddress(changeAddress)
.selectUtxosFrom(utxos);

Expand Down Expand Up @@ -125,7 +125,7 @@ function Right() {
codeSnippet += `}\n`;
codeSnippet += `\n`;
codeSnippet += `txBuilder\n`;
codeSnippet += ` .metadataValue("721", metadata)\n`;
codeSnippet += ` .metadataValue(721, metadata)\n`;
codeSnippet += ` .changeAddress(changeAddress)\n`;
codeSnippet += ` .selectUtxosFrom(utxos);\n`;
codeSnippet += `\n`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { use, useEffect, useState } from "react";
import { useEffect, useState } from "react";

import { BrowserWallet } from "@meshsdk/core";
import { useWalletList } from "@meshsdk/react";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { use, useEffect } from "react";
import { useEffect } from "react";

import { HydraProvider } from "@meshsdk/core";

Expand Down
11 changes: 9 additions & 2 deletions packages/mesh-common/src/types/transaction-builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type MeshTxBuilderBody = {
referenceInputs: RefTxIn[];
mints: MintItem[];
changeAddress: string;
metadata: Metadata[];
metadata: TxMetadata;
validityRange: ValidityRange;
certificates: Certificate[];
withdrawals: Withdrawal[];
Expand All @@ -50,7 +50,7 @@ export const emptyTxBuilderBody = (): MeshTxBuilderBody => ({
referenceInputs: [],
mints: [],
changeAddress: "",
metadata: [],
metadata: new Map<bigint, Metadatum>(),
validityRange: {},
certificates: [],
withdrawals: [],
Expand All @@ -73,6 +73,13 @@ export type ValidityRange = {

// Mint Types

// Transaction Metadata

export type MetadatumMap = Map<Metadatum, Metadatum>;
export type Metadatum = bigint | number | string | Uint8Array | MetadatumMap | Metadatum[];
export type TxMetadata = Map<bigint, Metadatum>;

// to be used for serialization
export type Metadata = {
tag: string;
metadata: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ export class MeshContentOwnershipContract extends MeshTxInitiator {
const txHex = await this.mesh
.mint("1", policyId, tokenNameHex)
.mintingScript(forgingScript)
.metadataValue("721", metadata)
.metadataValue(721, metadata)
.changeAddress(walletAddress)
.selectUtxosFrom(utxos)
.complete();
Expand Down
2 changes: 1 addition & 1 deletion packages/mesh-contract/src/plutus-nft/offchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export class MeshPlutusNFTContract extends MeshTxInitiator {

if (assetMetadata) {
const metadata = { [policyId]: { [tokenName]: { ...assetMetadata } } };
tx.metadataValue("721", metadata);
tx.metadataValue(721, metadata);
}

tx.mintRedeemerValue(mConStr0([]))
Expand Down
3 changes: 2 additions & 1 deletion packages/mesh-core-csl/src/core/adaptor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { certificateToObj } from "./certificate";
import { mintItemToObj } from "./mint";
import { networkToObj } from "./network";
import { outputToObj } from "./output";
import { txMetadataToObj } from "./metadata";
import { collateralTxInToObj, txInToObj } from "./txIn";
import { voteToObj } from "./vote";
import { withdrawalToObj } from "./withdrawal";
Expand Down Expand Up @@ -33,7 +34,7 @@ export const meshTxBuilderBodyToObj = ({
referenceInputs: referenceInputs,
mints: mints.map((mint) => mintItemToObj(mint)),
changeAddress,
metadata: metadata,
metadata: txMetadataToObj(metadata),
validityRange: validityRangeToObj(validityRange),
certificates: certificates.map(certificateToObj),
signingKey: signingKey,
Expand Down
40 changes: 40 additions & 0 deletions packages/mesh-core-csl/src/core/adaptor/metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import JSONbig from "json-bigint";

import type { Metadata, Metadatum, TxMetadata } from "@meshsdk/common";

export const txMetadataToObj = (metadata: TxMetadata): Metadata[] => {
const result: Metadata[] = [];
metadata.forEach((value: Metadatum, key: bigint) => {
result.push({
tag: key.toString(),
metadata: JSONbig.stringify(metadatumToObj(value)),
});
});
return result;
};

const metadatumToObj = (metadatum: Metadatum): any => {
if (typeof metadatum === "number" || typeof metadatum === "string") {
return metadatum;
} else if (typeof metadatum === "bigint") {
return metadatum.toString();
} else if (metadatum instanceof Uint8Array) {
return uint8ArrayToHex(metadatum);
} else if (metadatum instanceof Map) {
const result: Record<string | number, any> = {};
metadatum.forEach((value, key) => {
result[metadatumToObj(key)] = metadatumToObj(value);
});
return result;
} else if (Array.isArray(metadatum)) {
return metadatum.map(metadatumToObj);
} else {
throw new Error("metadatumToObj: Unsupported Metadatum type");
}
};

const uint8ArrayToHex = (bytes: Uint8Array): string => {
return Array.from(bytes)
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("");
};
2 changes: 1 addition & 1 deletion packages/mesh-core-csl/test/core/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe("Builder", () => {
mints: [],
changeAddress:
"addr_test1qq0yavv5uve45rwvfaw96qynrqt8ckpmkwcg08vlwxxdncxk82f5wz75mzaesmqzl79xqsmedwgucwtuav5str6untqqmykcpn",
metadata: [],
metadata: new Map(),
validityRange: {},
certificates: [],
withdrawals: [],
Expand Down
1 change: 1 addition & 0 deletions packages/mesh-transaction/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./mesh-tx-builder";
export * from "./scripts";
export * from "./transaction";
export * from "./utils";
Loading
Loading