Skip to content

Commit

Permalink
overrideTxRedeemers
Browse files Browse the repository at this point in the history
  • Loading branch information
michele-nuzzi committed Feb 8, 2024
1 parent 8751765 commit d3d5e58
Show file tree
Hide file tree
Showing 4 changed files with 435 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/offchain/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@harmoniclabs/plu-ts-offchain",
"version": "0.1.12",
"version": "0.1.13-dev0",
"description": "An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
47 changes: 47 additions & 0 deletions packages/offchain/src/TxBuilder/TxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,53 @@ export class TxBuilder
*/
build!: (args: ITxBuildArgs, opts?: ITxBuildOptions) => Promise<Tx>

/**
* replaces the redeemers and clears vkeyWitnesses in the witness set
* and re-computes the `scriptDataHash` in the body
*
* the input transaction is readonly and is not modified
*
* **A NEW TRANSACTION IS CREATED** with vkey witness set empty
* (the new transaction is unsigned)
*
* to summarize, the new transaction differs in:
* 1) `tx.body.scriptDataHash`
* 2) `tx.witnesses.redeemers`
* 3) `tx.witnesses.vkeyWitnesses` (empty)
*/
overrideTxRedeemers( tx: Tx, newRedeemers: TxRedeemer[] ): Tx
{
// datums passed by hash
const datums = tx.witnesses.datums ?? [];
return new Tx({
...tx,
body: new TxBody({
...tx.body,
scriptDataHash: getScriptDataHash(
newRedeemers,
datums.length > 0 ?
Array.from(
Cbor.encode(
new CborArray(
datums.map( dataToCborObj )
)
).toBuffer()
)
: [],
costModelsToLanguageViewCbor(
this.protocolParamters.costModels,
{ mustHaveV2: true, mustHaveV1: false }
).toBuffer()
)
}),
witnesses: new TxWitnessSet({
...tx.witnesses,
vkeyWitnesses: [],
redeemers: newRedeemers
})
});
}

buildSync(
buildArgs: ITxBuildArgs,
{
Expand Down
Loading

0 comments on commit d3d5e58

Please sign in to comment.