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 Build Tx #14

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion packages/translucent/src/translucent/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,59 @@ export class Tx {
return this;
}

/**
* Unlock UTxO (datumHash) from Plutus Script
*/
unlockWithDatumHash(utxo: UTxO, datum: Datum, redeemer: Redeemer): Tx {
this.tasks.push(async (that) => {
if (!utxo.datumHash) {
throw Error("Only Support UTxO with DatumHash");
}
const coreUtxo = utxoToCore(utxo);
this.UTxOs.push(coreUtxo);
let inputBuilder = C.SingleInputBuilder.new(
coreUtxo.input(),
coreUtxo.output(),
);
let mr: CInputBuilderResult;
let address = coreUtxo.output().address();
let paymentCredential = address.payment_cred();
if (!paymentCredential) {
throw "Address has no payment credential";
}
let scriptHash = paymentCredential.to_scripthash();
if (!scriptHash) {
throw Error("Address isn't a Script Address");
}
let script = this.scripts[scriptHash.to_hex()];
if (!script) {
throw "Script was not attached for UTxO spend";
}
const plutusData = C.PlutusData.from_bytes(fromHex(datum));
if ("inlineScript" in script) {
mr = inputBuilder.plutus_script(
C.PartialPlutusWitness.new(
C.PlutusScriptWitness.from_script(script.inlineScript),
C.PlutusData.from_bytes(fromHex(redeemer)),
),
C.Ed25519KeyHashes.new(),
plutusData,
);
} else {
mr = inputBuilder.plutus_script(
C.PartialPlutusWitness.new(
C.PlutusScriptWitness.from_ref(script.referenceScript.hash()),
C.PlutusData.from_bytes(fromHex(redeemer)),
),
C.Ed25519KeyHashes.new(),
plutusData,
);
}
that.txBuilder.add_input(mr);
});
return this;
}

/**
* A public key or native script input.
* With redeemer it's a plutus script input.
Expand Down Expand Up @@ -320,6 +373,7 @@ export class Tx {
outputBuilder = outputBuilder.with_address(outputAddress);

if (outputData.hash) {
throw Error("Not support, may use asHash instead!");
} else if (outputData.asHash) {
const plutusData = C.PlutusData.from_bytes(fromHex(outputData.asHash));
outputBuilder = outputBuilder.with_communication_data(plutusData);
Expand Down Expand Up @@ -819,6 +873,7 @@ export class Tx {
coinSelection?: boolean;
overEstimateMem?: number;
overEstimateSteps?: number;
witnessSet?: { plutusData?: Datum[], ignoreScriptDataHash?: boolean };
}): Promise<TxComplete> {
if (
[
Expand Down Expand Up @@ -985,7 +1040,8 @@ export class Tx {
redeemers.add(redeemer)
}
let builtTx = this.txBuilder.build(0, changeAddress).build_unchecked();
{

if (!(options?.witnessSet?.ignoreScriptDataHash)) {
const datums = C.PlutusList.new();
const unhashedData = builtTx.witness_set().plutus_data();
let hashes = [];
Expand Down Expand Up @@ -1032,6 +1088,21 @@ export class Tx {
);
}
}

if (options?.witnessSet?.plutusData) {
const cPlutusData = C.PlutusList.new();
for (const plutusData of options?.witnessSet?.plutusData) {
cPlutusData.add(C.PlutusData.from_bytes(fromHex(plutusData)));
}
const witnessSet = builtTx.witness_set();
witnessSet.set_plutus_data(cPlutusData);
builtTx = C.Transaction.new(
builtTx.body(),
witnessSet,
builtTx.auxiliary_data(),
);
}

return new TxComplete(this.translucent, builtTx);
}

Expand Down
12 changes: 11 additions & 1 deletion packages/translucent/src/translucent/tx_complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,18 @@ export class TxComplete {
for (const task of this.tasks) {
await task();
}
const witnessSet = this.txComplete.witness_set();
const plutusData = witnessSet.plutus_data();

this.witnessSetBuilder.add_existing(witnessSet);

// FIX CML
if (plutusData && plutusData.len() > 0) {
for (let i = 0; i < plutusData.len(); i++) {
this.witnessSetBuilder.add_plutus_datum(plutusData.get(i));
}
}

this.witnessSetBuilder.add_existing(this.txComplete.witness_set());
const signedTx = C.Transaction.new(
this.txComplete.body(),
this.witnessSetBuilder.build(),
Expand Down
Loading