diff --git a/packages/mesh-core-cst/src/types/cardano-sdk.ts b/packages/mesh-core-cst/src/types/cardano-sdk.ts index b9ad1403..40ec412a 100644 --- a/packages/mesh-core-cst/src/types/cardano-sdk.ts +++ b/packages/mesh-core-cst/src/types/cardano-sdk.ts @@ -286,6 +286,9 @@ export type Metadatum = Cardano.Metadatum; export type HexBlob = CardanoHexBlob; export const HexBlob = CardanoHexBlob; +export type TxCBOR = Serialization.TxCBOR; +export const TxCBOR = Serialization.TxCBOR; + export type Ed25519PrivateKey = Crypto.Ed25519PrivateKey; export const Ed25519PrivateKey = Crypto.Ed25519PrivateKey; diff --git a/packages/mesh-core-cst/src/utils/index.ts b/packages/mesh-core-cst/src/utils/index.ts index 8dc511eb..b4f7837e 100644 --- a/packages/mesh-core-cst/src/utils/index.ts +++ b/packages/mesh-core-cst/src/utils/index.ts @@ -5,3 +5,4 @@ export * from "./value"; export * from "./data"; export * from "./address"; export * from "./encoding"; +export * from "./witness-set"; diff --git a/packages/mesh-core-cst/src/utils/witness-set.ts b/packages/mesh-core-cst/src/utils/witness-set.ts new file mode 100644 index 00000000..6a541370 --- /dev/null +++ b/packages/mesh-core-cst/src/utils/witness-set.ts @@ -0,0 +1,35 @@ +import { + CborSet, + HexBlob, + Transaction, + TransactionWitnessSet, + TxCBOR, + VkeyWitness, +} from "../types"; + +export const addVKeyWitnessSetToTransaction = ( + txHex: string, + vkeyWitnessSet: string, +): string => { + const tx = Transaction.fromCbor(TxCBOR(txHex)); + const currentWitnessSet = tx.witnessSet(); + const newVkeyWitnessSet = TransactionWitnessSet.fromCbor( + HexBlob(vkeyWitnessSet), + ); + + const currentVkeyWitnesses = currentWitnessSet.vkeys(); + const newVkeyWitnesses = newVkeyWitnessSet.vkeys(); + const allVkeyWitnesses = [ + ...(currentVkeyWitnesses?.values() ?? []), + ...(newVkeyWitnesses?.values() ?? []), + ]; + + currentWitnessSet.setVkeys( + CborSet.fromCore( + allVkeyWitnesses.map((vkw) => vkw.toCore()), + VkeyWitness.fromCore, + ), + ); + tx.setWitnessSet(currentWitnessSet); + return tx.toCbor(); +}; diff --git a/packages/mesh-core-cst/test/utils/witness-set.test.ts b/packages/mesh-core-cst/test/utils/witness-set.test.ts new file mode 100644 index 00000000..97b60dc6 --- /dev/null +++ b/packages/mesh-core-cst/test/utils/witness-set.test.ts @@ -0,0 +1,15 @@ +import { addVKeyWitnessSetToTransaction } from "../../src/utils/witness-set"; + +describe("Witness Set", () => { + it("should append witness set vkeys correctly", () => { + const tx = + "84a40081825820ad91133a2c769247d384c0519206766e755b50847b41504b8555932917e492a90201818258390004845038ee499ee8bc0afe56f688f27b2dd76f230d3698a9afcc1b66e0464447c1f51adaefe1ebfb0dd485a349a70479ced1d198cbdf7fe71a38c5ed05021a00028b21075820bdaa99eb158414dea0a91d6c727e2268574b23efe6e08ab3b841abe8059a030ca0f5d90103a0"; + const signature = + "a1008182582089f4b576f05f5aad99bce0bdd51afe48529772f7561bb2ac9d84a4afbda1ecd658402702ebaab96e33259eacc175edffcf5b74cdb2bc28d8bf0a99e5c3bb4999597828470aa46ea6baf2d3234efc11e81bb7907a992a0ff97c94725e4fe290eef006"; + + const newTx = addVKeyWitnessSetToTransaction(tx, signature); + expect(newTx).toEqual( + "84a40081825820ad91133a2c769247d384c0519206766e755b50847b41504b8555932917e492a90201818258390004845038ee499ee8bc0afe56f688f27b2dd76f230d3698a9afcc1b66e0464447c1f51adaefe1ebfb0dd485a349a70479ced1d198cbdf7fe71a38c5ed05021a00028b21075820bdaa99eb158414dea0a91d6c727e2268574b23efe6e08ab3b841abe8059a030ca1008182582089f4b576f05f5aad99bce0bdd51afe48529772f7561bb2ac9d84a4afbda1ecd658402702ebaab96e33259eacc175edffcf5b74cdb2bc28d8bf0a99e5c3bb4999597828470aa46ea6baf2d3234efc11e81bb7907a992a0ff97c94725e4fe290eef006f5d90103a0", + ); + }); +});