From 39e76db42be18bf10f60a7b670d6a209113d762b Mon Sep 17 00:00:00 2001 From: twwu123 Date: Mon, 11 Nov 2024 16:38:14 +0800 Subject: [PATCH 1/6] add script size to tx in type --- packages/mesh-common/src/types/transaction-builder/txin.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/mesh-common/src/types/transaction-builder/txin.ts b/packages/mesh-common/src/types/transaction-builder/txin.ts index 2e067f6d7..38387482c 100644 --- a/packages/mesh-common/src/types/transaction-builder/txin.ts +++ b/packages/mesh-common/src/types/transaction-builder/txin.ts @@ -9,6 +9,7 @@ export type TxInParameter = { txIndex: number; amount?: Asset[]; address?: string; + scriptSize?: number; }; export type TxIn = PubKeyTxIn | SimpleScriptTxIn | ScriptTxIn; From 1479186f8fe689b17f83dfd67fde7f7b5a752a89 Mon Sep 17 00:00:00 2001 From: twwu123 Date: Mon, 11 Nov 2024 16:38:37 +0800 Subject: [PATCH 2/6] reimplement fetching logic in tx builder --- .../src/types/transaction-builder/txin.ts | 2 +- .../src/mesh-tx-builder/index.ts | 58 +++++++++---------- .../src/mesh-tx-builder/tx-builder-core.ts | 4 ++ 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/packages/mesh-common/src/types/transaction-builder/txin.ts b/packages/mesh-common/src/types/transaction-builder/txin.ts index 38387482c..b0e8913e6 100644 --- a/packages/mesh-common/src/types/transaction-builder/txin.ts +++ b/packages/mesh-common/src/types/transaction-builder/txin.ts @@ -2,7 +2,7 @@ import { Asset } from "../asset"; import { DatumSource, Redeemer } from "./data"; import { ScriptSource, SimpleScriptSourceInfo } from "./script"; -export type RefTxIn = { txHash: string; txIndex: number, scriptSize?: number }; +export type RefTxIn = { txHash: string; txIndex: number; scriptSize?: number }; export type TxInParameter = { txHash: string; diff --git a/packages/mesh-transaction/src/mesh-tx-builder/index.ts b/packages/mesh-transaction/src/mesh-tx-builder/index.ts index 76923d9d3..657b8fc65 100644 --- a/packages/mesh-transaction/src/mesh-tx-builder/index.ts +++ b/packages/mesh-transaction/src/mesh-tx-builder/index.ts @@ -84,39 +84,12 @@ export class MeshTxBuilder extends MeshTxBuilderCore { // Checking if all inputs are complete const { inputs, collaterals, mints } = this.meshTxBuilderBody; - // We must check every input for ref scripts - const incompleteTxIns = [...inputs, ...collaterals]; + const incompleteTxIns = [...inputs, ...collaterals].filter( + (txIn) => !this.isInputComplete(txIn), + ); const incompleteMints = mints.filter((mint) => !this.isMintComplete(mint)); // Getting all missing utxo information await this.queryAllTxInfo(incompleteTxIns, incompleteMints); - // Gather all utxos with ref scripts - Object.values(this.queriedUTxOs).forEach((utxos) => { - for (let utxo of utxos) { - if (utxo.output.scriptRef !== undefined) { - this.utxosWithRefScripts.push(utxo); - } - } - }); - const missingRefInput = this.utxosWithRefScripts.filter((utxo) => { - this.meshTxBuilderBody.referenceInputs.forEach((refInput) => { - if ( - refInput.txHash === utxo.input.txHash && - refInput.txIndex === utxo.input.outputIndex - ) { - return false; - } - }); - return true; - }); - // Add any inputs with ref scripts into reference inputs - // serializer will then deduplicate, but keep the script size for fee calc - missingRefInput.forEach((utxo) => { - this.meshTxBuilderBody.referenceInputs.push({ - txHash: utxo.input.txHash, - txIndex: utxo.input.outputIndex, - scriptSize: utxo.output.scriptRef!.length / 2, - }); - }); // Completing all inputs incompleteTxIns.forEach((txIn) => { this.completeTxInformation(txIn); @@ -131,6 +104,22 @@ export class MeshTxBuilder extends MeshTxBuilderCore { this.completeSimpleScriptInfo(scriptSource); } }); + this.meshTxBuilderBody.inputs.forEach((input) => { + if (input.txIn.scriptSize && input.txIn.scriptSize > 0) { + if ( + this.meshTxBuilderBody.referenceInputs.find((refTxIn) => { + refTxIn.txHash === input.txIn.txHash && + refTxIn.txIndex === input.txIn.txIndex; + }) === undefined + ) { + this.meshTxBuilderBody.referenceInputs.push({ + txHash: input.txIn.txHash, + txIndex: input.txIn.txIndex, + scriptSize: input.txIn.scriptSize, + }); + } + } + }); this.addUtxosFromSelection(); let txHex = this.serializer.serializeTxBody( @@ -285,6 +274,11 @@ export class MeshTxBuilder extends MeshTxBuilderCore { ); input.txIn.address = address; } + if (utxo?.output.scriptRef) { + input.txIn.scriptSize = utxo.output.scriptRef.length / 2; + } else { + input.txIn.scriptSize = 0; + } }; protected completeScriptInfo = (scriptSource: ScriptSource) => { @@ -331,8 +325,8 @@ export class MeshTxBuilder extends MeshTxBuilderCore { }; protected isInputInfoComplete = (txIn: TxIn): boolean => { - const { amount, address } = txIn.txIn; - if (!amount || !address) return false; + const { amount, address, scriptSize } = txIn.txIn; + if (!amount || !address || !scriptSize) return false; return true; }; diff --git a/packages/mesh-transaction/src/mesh-tx-builder/tx-builder-core.ts b/packages/mesh-transaction/src/mesh-tx-builder/tx-builder-core.ts index 621088d39..10b3a243c 100644 --- a/packages/mesh-transaction/src/mesh-tx-builder/tx-builder-core.ts +++ b/packages/mesh-transaction/src/mesh-tx-builder/tx-builder-core.ts @@ -73,6 +73,7 @@ export class MeshTxBuilderCore { * @param txIndex The transaction index of the input UTxO * @param amount The asset amount of index of the input UTxO * @param address The address of the input UTxO + * @param scriptSize The size of the ref script at this input (if there isn't one, explicitly put 0 as scriptSize for offline tx building) * @returns The MeshTxBuilder instance */ txIn = ( @@ -80,6 +81,7 @@ export class MeshTxBuilderCore { txIndex: number, amount?: Asset[], address?: string, + scriptSize?: number, ) => { if (this.txInQueueItem) { this.queueInput(); @@ -92,6 +94,7 @@ export class MeshTxBuilderCore { txIndex: txIndex, amount: amount, address: address, + scriptSize: scriptSize, }, }; } else { @@ -102,6 +105,7 @@ export class MeshTxBuilderCore { txIndex: txIndex, amount: amount, address: address, + scriptSize: scriptSize, }, scriptTxIn: {}, }; From ecb8c3b4150dfdc86b3a49e5917ef8b3a28fff99 Mon Sep 17 00:00:00 2001 From: TW Date: Tue, 12 Nov 2024 14:08:24 +0800 Subject: [PATCH 3/6] fix input info completeness check --- packages/mesh-transaction/src/mesh-tx-builder/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mesh-transaction/src/mesh-tx-builder/index.ts b/packages/mesh-transaction/src/mesh-tx-builder/index.ts index 657b8fc65..23a483688 100644 --- a/packages/mesh-transaction/src/mesh-tx-builder/index.ts +++ b/packages/mesh-transaction/src/mesh-tx-builder/index.ts @@ -326,7 +326,7 @@ export class MeshTxBuilder extends MeshTxBuilderCore { protected isInputInfoComplete = (txIn: TxIn): boolean => { const { amount, address, scriptSize } = txIn.txIn; - if (!amount || !address || !scriptSize) return false; + if (!amount || !address || scriptSize === undefined) return false; return true; }; From 064b2b2fd2659702326e2e9c527a7616564332c6 Mon Sep 17 00:00:00 2001 From: TW Date: Tue, 12 Nov 2024 14:09:02 +0800 Subject: [PATCH 4/6] explicitly add scriptSize = 0 for utxos without scriptref --- .../mesh-transaction/src/transaction/index.ts | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/packages/mesh-transaction/src/transaction/index.ts b/packages/mesh-transaction/src/transaction/index.ts index 33c438c47..5c1977336 100644 --- a/packages/mesh-transaction/src/transaction/index.ts +++ b/packages/mesh-transaction/src/transaction/index.ts @@ -24,9 +24,9 @@ import { import { Cardano, CardanoSDKUtil, - deserializeTx, deserializeNativeScript, deserializePlutusScript, + deserializeTx, fromScriptRef, Serialization, Transaction as Tx, @@ -48,15 +48,14 @@ export class Transaction { this.initiator = options.initiator; } - static attachMetadata( - cborTx: string, - cborTxMetadata: string, - ) { + static attachMetadata(cborTx: string, cborTxMetadata: string) { const tx = deserializeTx(cborTx); const txAuxData = tx.auxiliaryData() ?? new Serialization.AuxiliaryData(); txAuxData.setMetadata( - Serialization.GeneralTransactionMetadata.fromCbor(CardanoSDKUtil.HexBlob(cborTxMetadata)) + Serialization.GeneralTransactionMetadata.fromCbor( + CardanoSDKUtil.HexBlob(cborTxMetadata), + ), ); if ( @@ -64,7 +63,7 @@ export class Transaction { tx.body().auxiliaryDataHash()?.toString() ) { throw new Error( - '[Transaction] attachMetadata: The metadata hash does not match the auxiliary data hash.' + "[Transaction] attachMetadata: The metadata hash does not match the auxiliary data hash.", ); } @@ -81,8 +80,15 @@ export class Transaction { const txMetadata = tx.auxiliaryData()?.metadata(); if (txMetadata !== undefined) { - const mockMetadata = new Map(); - txMetadata.metadata()?.forEach((metadatum, label) => mockMetadata.set(label, mask(metadatum))); + const mockMetadata = new Map< + bigint, + Serialization.TransactionMetadatum + >(); + txMetadata + .metadata() + ?.forEach((metadatum, label) => + mockMetadata.set(label, mask(metadatum)), + ); const txAuxData = tx.auxiliaryData(); txMetadata.setMetadata(mockMetadata); txAuxData?.setMetadata(txMetadata); @@ -94,18 +100,17 @@ export class Transaction { static readMetadata(cborTx: string) { const tx = deserializeTx(cborTx); - return tx.auxiliaryData()?.metadata()?.toCbor().toString() ?? ''; + return tx.auxiliaryData()?.metadata()?.toCbor().toString() ?? ""; } - static writeMetadata( - cborTx: string, - cborTxMetadata: string, - ) { + static writeMetadata(cborTx: string, cborTxMetadata: string) { const tx = deserializeTx(cborTx); const txAuxData = tx.auxiliaryData() ?? new Serialization.AuxiliaryData(); txAuxData.setMetadata( - Serialization.GeneralTransactionMetadata.fromCbor(CardanoSDKUtil.HexBlob(cborTxMetadata)) + Serialization.GeneralTransactionMetadata.fromCbor( + CardanoSDKUtil.HexBlob(cborTxMetadata), + ), ); return new Tx(tx.body(), tx.witnessSet(), txAuxData).toCbor().toString(); @@ -216,6 +221,7 @@ export class Transaction { input.input.outputIndex, input.output.amount, input.output.address, + input.output.scriptRef ? input.output.scriptRef.length / 2 : 0, ); }); @@ -715,13 +721,17 @@ export class Transaction { } } -function mask(metadatum: Serialization.TransactionMetadatum): Serialization.TransactionMetadatum { +function mask( + metadatum: Serialization.TransactionMetadatum, +): Serialization.TransactionMetadatum { switch (metadatum.getKind()) { case Serialization.TransactionMetadatumKind.Text: - return Serialization.TransactionMetadatum.newText("0".repeat(metadatum.asText()?.length ?? 0)); + return Serialization.TransactionMetadatum.newText( + "0".repeat(metadatum.asText()?.length ?? 0), + ); case Serialization.TransactionMetadatumKind.Bytes: case Serialization.TransactionMetadatumKind.Integer: - return metadatum + return metadatum; case Serialization.TransactionMetadatumKind.List: const list = new Serialization.MetadatumList(); for (let i = 0; i < (metadatum.asList()?.getLength() ?? 0); i++) { From 42f7d78dd942a79830158f4da15ef312290c43f6 Mon Sep 17 00:00:00 2001 From: TW Date: Tue, 12 Nov 2024 17:24:25 +0800 Subject: [PATCH 5/6] update tests --- .../mesh-tx-builder/info-completeness-checker.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/mesh-transaction/test/mesh-tx-builder/info-completeness-checker.test.ts b/packages/mesh-transaction/test/mesh-tx-builder/info-completeness-checker.test.ts index e34fabbd1..1a54990dc 100644 --- a/packages/mesh-transaction/test/mesh-tx-builder/info-completeness-checker.test.ts +++ b/packages/mesh-transaction/test/mesh-tx-builder/info-completeness-checker.test.ts @@ -41,7 +41,7 @@ describe("MeshTxBuilder", () => { it("should return true for complete PubKey input", () => { const txIn: TxIn = { type: "PubKey", - txIn: { txHash: "hash", txIndex: 0, amount: [], address: "address" }, + txIn: { txHash: "hash", txIndex: 0, amount: [], address: "address", scriptSize: 0 }, }; expect(txBuilder.isInputCompleteExtended(txIn)).toBe(true); }); @@ -89,7 +89,7 @@ describe("MeshTxBuilder", () => { it("should return true for complete Script input", () => { const txIn: TxIn = { type: "Script", - txIn: { txHash: "hash", txIndex: 0, amount: [], address: "address" }, + txIn: { txHash: "hash", txIndex: 0, amount: [], address: "address", scriptSize: 0 }, scriptTxIn: { scriptSource: { type: "Provided", @@ -102,7 +102,7 @@ describe("MeshTxBuilder", () => { it("should return true for complete Script input, with complete script ref", () => { const txIn: TxIn = { type: "Script", - txIn: { txHash: "hash", txIndex: 0, amount: [], address: "address" }, + txIn: { txHash: "hash", txIndex: 0, amount: [], address: "address", scriptSize: 0 }, scriptTxIn: { scriptSource: { type: "Inline", @@ -176,7 +176,7 @@ describe("MeshTxBuilder", () => { it("should return true for complete PubKey input", () => { const txIn: TxIn = { type: "PubKey", - txIn: { txHash: "hash", txIndex: 0, amount: [], address: "address" }, + txIn: { txHash: "hash", txIndex: 0, amount: [], address: "address", scriptSize: 0 }, }; expect(txBuilder.isInputInfoCompleteExtended(txIn)).toBe(true); }); From efbc3ebc36a25fbf7b3768c837d57f670563c730 Mon Sep 17 00:00:00 2001 From: "Hong Jing (Jingles)" Date: Tue, 12 Nov 2024 18:04:12 +0800 Subject: [PATCH 6/6] versiono bump --- packages/mesh-common/package.json | 2 +- packages/mesh-contract/package.json | 8 ++++---- packages/mesh-core-csl/package.json | 4 ++-- packages/mesh-core-cst/package.json | 4 ++-- packages/mesh-core/package.json | 16 ++++++++-------- packages/mesh-provider/package.json | 6 +++--- packages/mesh-react/package.json | 8 ++++---- packages/mesh-transaction/package.json | 8 ++++---- packages/mesh-wallet/package.json | 10 +++++----- scripts/mesh-cli/package.json | 2 +- 10 files changed, 34 insertions(+), 34 deletions(-) diff --git a/packages/mesh-common/package.json b/packages/mesh-common/package.json index 52fb1c9c7..b7e93b6ca 100644 --- a/packages/mesh-common/package.json +++ b/packages/mesh-common/package.json @@ -1,6 +1,6 @@ { "name": "@meshsdk/common", - "version": "1.7.13", + "version": "1.7.14", "description": "", "main": "./dist/index.cjs", "browser": "./dist/index.js", diff --git a/packages/mesh-contract/package.json b/packages/mesh-contract/package.json index a0471d11d..096197fc0 100644 --- a/packages/mesh-contract/package.json +++ b/packages/mesh-contract/package.json @@ -1,6 +1,6 @@ { "name": "@meshsdk/contract", - "version": "1.7.13", + "version": "1.7.14", "description": "", "main": "./dist/index.cjs", "browser": "./dist/index.js", @@ -34,9 +34,9 @@ "typescript": "^5.3.3" }, "dependencies": { - "@meshsdk/common": "1.7.13", - "@meshsdk/core": "1.7.13", - "@meshsdk/core-csl": "1.7.13" + "@meshsdk/common": "1.7.14", + "@meshsdk/core": "1.7.14", + "@meshsdk/core-csl": "1.7.14" }, "prettier": "@meshsdk/configs/prettier", "publishConfig": { diff --git a/packages/mesh-core-csl/package.json b/packages/mesh-core-csl/package.json index 68b8939e8..c76498f34 100644 --- a/packages/mesh-core-csl/package.json +++ b/packages/mesh-core-csl/package.json @@ -1,6 +1,6 @@ { "name": "@meshsdk/core-csl", - "version": "1.7.13", + "version": "1.7.14", "description": "", "main": "./dist/index.cjs", "module": "./dist/index.js", @@ -38,7 +38,7 @@ "typescript": "^5.3.3" }, "dependencies": { - "@meshsdk/common": "1.7.13", + "@meshsdk/common": "1.7.14", "@sidan-lab/sidan-csl-rs-browser": "0.9.5", "@sidan-lab/sidan-csl-rs-nodejs": "0.9.5", "json-bigint": "^1.0.0" diff --git a/packages/mesh-core-cst/package.json b/packages/mesh-core-cst/package.json index 71d450026..2246e1584 100644 --- a/packages/mesh-core-cst/package.json +++ b/packages/mesh-core-cst/package.json @@ -1,6 +1,6 @@ { "name": "@meshsdk/core-cst", - "version": "1.7.13", + "version": "1.7.14", "description": "", "main": "./dist/index.cjs", "browser": "./dist/index.js", @@ -42,7 +42,7 @@ "@harmoniclabs/cbor": "1.3.0", "@harmoniclabs/plutus-data": "1.2.4", "@harmoniclabs/uplc": "1.2.4", - "@meshsdk/common": "1.7.13", + "@meshsdk/common": "1.7.14", "@stricahq/bip32ed25519": "^1.1.0", "@stricahq/cbors": "^1.0.0", "pbkdf2": "^3.1.2" diff --git a/packages/mesh-core/package.json b/packages/mesh-core/package.json index f7e7c2066..ab423ee68 100644 --- a/packages/mesh-core/package.json +++ b/packages/mesh-core/package.json @@ -1,6 +1,6 @@ { "name": "@meshsdk/core", - "version": "1.7.13", + "version": "1.7.14", "description": "", "main": "./dist/index.cjs", "browser": "./dist/index.js", @@ -33,13 +33,13 @@ "typescript": "^5.3.3" }, "dependencies": { - "@meshsdk/common": "1.7.13", - "@meshsdk/core-csl": "1.7.13", - "@meshsdk/core-cst": "1.7.13", - "@meshsdk/provider": "1.7.13", - "@meshsdk/react": "1.7.13", - "@meshsdk/transaction": "1.7.13", - "@meshsdk/wallet": "1.7.13" + "@meshsdk/common": "1.7.14", + "@meshsdk/core-csl": "1.7.14", + "@meshsdk/core-cst": "1.7.14", + "@meshsdk/provider": "1.7.14", + "@meshsdk/react": "1.7.14", + "@meshsdk/transaction": "1.7.14", + "@meshsdk/wallet": "1.7.14" }, "prettier": "@meshsdk/configs/prettier", "publishConfig": { diff --git a/packages/mesh-provider/package.json b/packages/mesh-provider/package.json index 9b3273020..1b37f518d 100644 --- a/packages/mesh-provider/package.json +++ b/packages/mesh-provider/package.json @@ -1,6 +1,6 @@ { "name": "@meshsdk/provider", - "version": "1.7.13", + "version": "1.7.14", "description": "", "main": "./dist/index.cjs", "browser": "./dist/index.js", @@ -34,8 +34,8 @@ "typescript": "^5.3.3" }, "dependencies": { - "@meshsdk/common": "1.7.13", - "@meshsdk/core-cst": "1.7.13", + "@meshsdk/common": "1.7.14", + "@meshsdk/core-cst": "1.7.14", "@utxorpc/sdk": "0.6.2", "@utxorpc/spec": "0.10.1", "axios": "^1.7.2" diff --git a/packages/mesh-react/package.json b/packages/mesh-react/package.json index 5c2278cc8..1e8d5e5be 100644 --- a/packages/mesh-react/package.json +++ b/packages/mesh-react/package.json @@ -1,6 +1,6 @@ { "name": "@meshsdk/react", - "version": "1.7.13", + "version": "1.7.14", "description": "", "main": "./dist/index.cjs", "browser": "./dist/index.js", @@ -30,9 +30,9 @@ }, "dependencies": { "react": "^18.2.0", - "@meshsdk/common": "1.7.13", - "@meshsdk/transaction": "1.7.13", - "@meshsdk/wallet": "1.7.13" + "@meshsdk/common": "1.7.14", + "@meshsdk/transaction": "1.7.14", + "@meshsdk/wallet": "1.7.14" }, "devDependencies": { "@meshsdk/configs": "*", diff --git a/packages/mesh-transaction/package.json b/packages/mesh-transaction/package.json index 2ffedecaa..77dfc0383 100644 --- a/packages/mesh-transaction/package.json +++ b/packages/mesh-transaction/package.json @@ -1,6 +1,6 @@ { "name": "@meshsdk/transaction", - "version": "1.7.13", + "version": "1.7.14", "description": "", "main": "./dist/index.cjs", "browser": "./dist/index.js", @@ -35,9 +35,9 @@ "typescript": "^5.3.3" }, "dependencies": { - "@meshsdk/common": "1.7.13", - "@meshsdk/core-csl": "1.7.13", - "@meshsdk/core-cst": "1.7.13", + "@meshsdk/common": "1.7.14", + "@meshsdk/core-csl": "1.7.14", + "@meshsdk/core-cst": "1.7.14", "json-bigint": "^1.0.0" }, "prettier": "@meshsdk/configs/prettier", diff --git a/packages/mesh-wallet/package.json b/packages/mesh-wallet/package.json index 12c70b344..a2cf32bed 100644 --- a/packages/mesh-wallet/package.json +++ b/packages/mesh-wallet/package.json @@ -1,6 +1,6 @@ { "name": "@meshsdk/wallet", - "version": "1.7.13", + "version": "1.7.14", "description": "", "main": "./dist/index.cjs", "browser": "./dist/index.js", @@ -35,10 +35,10 @@ "typescript": "^5.3.3" }, "dependencies": { - "@meshsdk/common": "1.7.13", - "@meshsdk/core-csl": "1.7.13", - "@meshsdk/core-cst": "1.7.13", - "@meshsdk/transaction": "1.7.13", + "@meshsdk/common": "1.7.14", + "@meshsdk/core-csl": "1.7.14", + "@meshsdk/core-cst": "1.7.14", + "@meshsdk/transaction": "1.7.14", "@nufi/dapp-client-cardano": "0.3.5", "@nufi/dapp-client-core": "0.3.5" }, diff --git a/scripts/mesh-cli/package.json b/scripts/mesh-cli/package.json index f436ef1f7..c454d83b6 100644 --- a/scripts/mesh-cli/package.json +++ b/scripts/mesh-cli/package.json @@ -3,7 +3,7 @@ "description": "A quick and easy way to bootstrap your dApps on Cardano using Mesh.", "homepage": "https://meshjs.dev", "author": "MeshJS", - "version": "1.7.13", + "version": "1.7.14", "license": "Apache-2.0", "type": "module", "main": "./dist/index.cjs",