Skip to content

Commit 4f4e5a0

Browse files
fixup! fixup! feat: tx-builder now supports spending from plutus scripts
1 parent a417c1d commit 4f4e5a0

File tree

5 files changed

+612
-11
lines changed

5 files changed

+612
-11
lines changed

packages/tx-construction/src/tx-builder/TxBuilder.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,20 @@ class LazyTxSigner implements UnwitnessedTx {
136136
tx,
137137
ctx: {
138138
signingContext: { knownAddresses, handleResolutions },
139-
auxiliaryData
139+
auxiliaryData,
140+
witness
140141
},
141142
inputSelection
142143
} = await this.#build();
143-
return { ...tx, auxiliaryData, handleResolutions, inputSelection, ownAddresses: knownAddresses };
144+
145+
return {
146+
...tx,
147+
auxiliaryData,
148+
handleResolutions,
149+
inputSelection,
150+
ownAddresses: knownAddresses,
151+
witness: witness as Cardano.Witness
152+
};
144153
}
145154

146155
async sign(): Promise<WitnessedTx> {
@@ -528,12 +537,9 @@ export class GenericTxBuilder implements TxBuilder {
528537
}
529538

530539
const costModels = getDefaultCostModelsForVersions([...scriptVersions]);
531-
body.scriptIntegrityHash = computeScriptDataHash(
532-
costModels,
533-
[...scriptVersions],
534-
witness.redeemers,
535-
witness.datums
536-
);
540+
body.scriptIntegrityHash = computeScriptDataHash(costModels, [...scriptVersions], witness.redeemers, [
541+
...this.#knownDatums.values()
542+
]);
537543

538544
const serializableBody = Serialization.TransactionBody.fromCore(body);
539545

@@ -903,12 +909,23 @@ export class GenericTxBuilder implements TxBuilder {
903909
inputs.set(inputId, [{ ...input, address: resolvedInput.address }, resolvedInput]);
904910
}
905911

912+
// eslint-disable-next-line sonarjs/cognitive-complexity
906913
#addScriptInput(input: Cardano.TxIn | Cardano.Utxo, scriptUnlockProps: ScriptUnlockProps) {
907914
let txId: TxIdWithIndex;
908915

909916
if (Array.isArray(input)) {
910917
txId = `${input[0].txId}#${input[0].index}`;
911918
this.#mustSpendInputs.set(txId, input);
919+
920+
if (input[1].datum) {
921+
const hash = Serialization.PlutusData.fromCore(input[1].datum).hash();
922+
this.#knownDatums.set(hash, input[1].datum);
923+
this.#knownInlineDatums.add(hash);
924+
}
925+
926+
if (input[1].datumHash) {
927+
this.#unresolvedDatums.push(input[1].datumHash);
928+
}
912929
} else {
913930
txId = `${input.txId}#${input.index}`;
914931

@@ -943,7 +960,6 @@ export class GenericTxBuilder implements TxBuilder {
943960
} else {
944961
const hash = Serialization.PlutusData.fromCore(scriptUnlockProps.datum).hash();
945962
this.#knownDatums.set(hash, scriptUnlockProps.datum);
946-
this.#knownInlineDatums.add(hash);
947963
}
948964
}
949965
}

packages/tx-construction/src/tx-builder/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export type TxInspection = Cardano.TxBodyWithHash & {
162162
auxiliaryData?: Cardano.AuxiliaryData;
163163
inputSelection: SelectionSkeleton;
164164
ownAddresses: GroupedAddress[];
165+
witness?: Cardano.Witness;
165166
};
166167

167168
/**

packages/tx-construction/test/tx-builder/TxBuilder.test.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
util
1313
} from '@cardano-sdk/key-management';
1414
import { AssetId, mockProviders as mocks } from '@cardano-sdk/util-dev';
15-
import { BigIntMath } from '@cardano-sdk/util';
15+
import { BigIntMath, HexBlob } from '@cardano-sdk/util';
1616
import { Cardano, Handle, ProviderError, ProviderFailure } from '@cardano-sdk/core';
1717
import {
1818
GenericTxBuilder,
@@ -367,16 +367,34 @@ describe.each([
367367
expect(outputBuilder.toTxOut().address).toEqual(address);
368368
});
369369

370-
it('can set datum', () => {
370+
it('can set datum hash', () => {
371371
outputBuilder.datum(datumHash);
372372
expect(outputBuilder.toTxOut().datumHash).toEqual(datumHash);
373373
});
374374

375+
it('can set inline datum', () => {
376+
outputBuilder.datum(1n);
377+
expect(outputBuilder.toTxOut().datum).toEqual(1n);
378+
});
379+
375380
it('can set handle', () => {
376381
outputBuilder.handle(handle);
377382
expect(outputBuilder.toTxOut().handle).toEqual(handle);
378383
});
379384

385+
it('can set script reference', () => {
386+
const alwaysSucceedsScript: Cardano.PlutusScript = {
387+
__type: Cardano.ScriptType.Plutus,
388+
bytes: HexBlob(
389+
'59079201000033232323232323232323232323232332232323232323232222232325335333006300800530070043333573466E1CD55CEA80124000466442466002006004646464646464646464646464646666AE68CDC39AAB9D500C480008CCCCCCCCCCCC88888888888848CCCCCCCCCCCC00403403002C02802402001C01801401000C008CD4060064D5D0A80619A80C00C9ABA1500B33501801A35742A014666AA038EB9406CD5D0A804999AA80E3AE501B35742A01066A0300466AE85401CCCD54070091D69ABA150063232323333573466E1CD55CEA801240004664424660020060046464646666AE68CDC39AAB9D5002480008CC8848CC00400C008CD40B9D69ABA15002302F357426AE8940088C98C80C8CD5CE01981901809AAB9E5001137540026AE854008C8C8C8CCCD5CD19B8735573AA004900011991091980080180119A8173AD35742A004605E6AE84D5D1280111931901919AB9C033032030135573CA00226EA8004D5D09ABA2500223263202E33573805E05C05826AAE7940044DD50009ABA1500533501875C6AE854010CCD540700808004D5D0A801999AA80E3AE200135742A00460446AE84D5D1280111931901519AB9C02B02A028135744A00226AE8940044D5D1280089ABA25001135744A00226AE8940044D5D1280089ABA25001135744A00226AE8940044D55CF280089BAA00135742A00460246AE84D5D1280111931900E19AB9C01D01C01A101B13263201B3357389201035054350001B135573CA00226EA80054049404448C88C008DD6000990009AA80A911999AAB9F0012500A233500930043574200460066AE880080548C8C8CCCD5CD19B8735573AA004900011991091980080180118061ABA150023005357426AE8940088C98C8054CD5CE00B00A80989AAB9E5001137540024646464646666AE68CDC39AAB9D5004480008CCCC888848CCCC00401401000C008C8C8C8CCCD5CD19B8735573AA0049000119910919800801801180A9ABA1500233500F014357426AE8940088C98C8068CD5CE00D80D00C09AAB9E5001137540026AE854010CCD54021D728039ABA150033232323333573466E1D4005200423212223002004357426AAE79400C8CCCD5CD19B875002480088C84888C004010DD71ABA135573CA00846666AE68CDC3A801A400042444006464C6403866AE700740700680640604D55CEA80089BAA00135742A00466A016EB8D5D09ABA2500223263201633573802E02C02826AE8940044D5D1280089AAB9E500113754002266AA002EB9D6889119118011BAB00132001355012223233335573E0044A010466A00E66442466002006004600C6AAE754008C014D55CF280118021ABA200301313574200222440042442446600200800624464646666AE68CDC3A800A40004642446004006600A6AE84D55CF280191999AB9A3370EA0049001109100091931900899AB9C01201100F00E135573AA00226EA80048C8C8CCCD5CD19B875001480188C848888C010014C01CD5D09AAB9E500323333573466E1D400920042321222230020053009357426AAE7940108CCCD5CD19B875003480088C848888C004014C01CD5D09AAB9E500523333573466E1D40112000232122223003005375C6AE84D55CF280311931900899AB9C01201100F00E00D00C135573AA00226EA80048C8C8CCCD5CD19B8735573AA004900011991091980080180118029ABA15002375A6AE84D5D1280111931900699AB9C00E00D00B135573CA00226EA80048C8CCCD5CD19B8735573AA002900011BAE357426AAE7940088C98C802CCD5CE00600580489BAA001232323232323333573466E1D4005200C21222222200323333573466E1D4009200A21222222200423333573466E1D400D2008233221222222233001009008375C6AE854014DD69ABA135744A00A46666AE68CDC3A8022400C4664424444444660040120106EB8D5D0A8039BAE357426AE89401C8CCCD5CD19B875005480108CC8848888888CC018024020C030D5D0A8049BAE357426AE8940248CCCD5CD19B875006480088C848888888C01C020C034D5D09AAB9E500B23333573466E1D401D2000232122222223005008300E357426AAE7940308C98C8050CD5CE00A80A00900880800780700680609AAB9D5004135573CA00626AAE7940084D55CF280089BAA0012323232323333573466E1D400520022333222122333001005004003375A6AE854010DD69ABA15003375A6AE84D5D1280191999AB9A3370EA0049000119091180100198041ABA135573CA00C464C6401A66AE7003803402C0284D55CEA80189ABA25001135573CA00226EA80048C8C8CCCD5CD19B875001480088C8488C00400CDD71ABA135573CA00646666AE68CDC3A8012400046424460040066EB8D5D09AAB9E500423263200A33573801601401000E26AAE7540044DD500089119191999AB9A3370EA00290021091100091999AB9A3370EA00490011190911180180218031ABA135573CA00846666AE68CDC3A801A400042444004464C6401666AE7003002C02402001C4D55CEA80089BAA0012323333573466E1D40052002212200223333573466E1D40092000212200123263200733573801000E00A00826AAE74DD5000891999AB9A3370E6AAE74DD5000A40004008464C6400866AE700140100092612001490103505431001123230010012233003300200200122212200201'
390+
),
391+
version: Cardano.PlutusLanguageVersion.V2
392+
};
393+
394+
outputBuilder.scriptReference(alwaysSucceedsScript);
395+
expect(outputBuilder.toTxOut().scriptReference).toEqual(alwaysSucceedsScript);
396+
});
397+
380398
it('throws an error if attempting to set handle without a handleProvider', async () => {
381399
try {
382400
await txBuilderWithoutHandleProvider.buildOutput().handle(address).build();

0 commit comments

Comments
 (0)