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

Feature update/mesh tx builder native script #235

Merged
merged 4 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions packages/module/src/common/utils/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ export const resolveNativeScriptHash = (script: NativeScript) => {
return toNativeScript(script).hash().to_hex();
};

export const resolveNativeScriptHex = (script: NativeScript) => {
return toNativeScript(script).to_hex();
};

export const resolvePaymentKeyHash = (bech32: string) => {
try {
const paymentKeyHash = [
Expand Down
74 changes: 64 additions & 10 deletions packages/module/src/transaction/meshTxBuilder/meshTxBuilderCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
BuilderData,
Certificate,
TxInParameter,
SimpleScriptTxIn,
} from './type';
import { selectUtxos } from '@mesh/core/CPS-009';
import JSONbig from 'json-bigint';
Expand Down Expand Up @@ -306,17 +307,30 @@ export class MeshTxBuilderCore {
* @param version Optional - The Plutus script version
* @returns The MeshTxBuilder instance
*/
txInScript = (scriptCbor: string, version: LanguageVersion = 'V2') => {
txInScript = (scriptCbor: string, version?: LanguageVersion) => {
if (!this.txInQueueItem) throw Error('Undefined input');
if (this.txInQueueItem.type === 'PubKey')
throw Error('Datum value attempted to be called a non script input');
this.txInQueueItem.scriptTxIn.scriptSource = {
type: 'Provided',
script: {
code: scriptCbor,
version,
},
};
if (this.txInQueueItem.type === 'PubKey') {
this.txInQueueItem = {
type: 'SimpleScript',
txIn: this.txInQueueItem.txIn,
simpleScriptTxIn: {
scriptSource: {
type: 'Provided',
script: scriptCbor,
},
},
};
}
if (this.txInQueueItem.type === 'Script') {
const lang_version = version ? version : 'V2';
this.txInQueueItem.scriptTxIn.scriptSource = {
type: 'Provided',
script: {
code: scriptCbor,
version: lang_version,
},
};
}
return this;
};

Expand All @@ -333,6 +347,10 @@ export class MeshTxBuilderCore {
if (!this.txInQueueItem) throw Error('Undefined input');
if (this.txInQueueItem.type === 'PubKey')
throw Error('Datum value attempted to be called a non script input');
if (this.txInQueueItem.type === 'SimpleScript')
throw Error(
'Datum value attempted to be called on a simple script input'
);

let content = datum;
if (type === 'JSON') {
Expand Down Expand Up @@ -368,6 +386,10 @@ export class MeshTxBuilderCore {
throw Error(
'Inline datum present attempted to be called a non script input'
);
if (this.txInQueueItem.type === 'SimpleScript')
throw Error(
'Inline datum present attempted to be called on a simple script input'
);
const { txHash, txIndex } = this.txInQueueItem.txIn;
if (txHash && txIndex.toString()) {
this.txInQueueItem.scriptTxIn.datumSource = {
Expand Down Expand Up @@ -424,6 +446,10 @@ export class MeshTxBuilderCore {
throw Error(
'Spending tx in reference redeemer attempted to be called a non script input'
);
if (this.txInQueueItem.type === 'SimpleScript')
throw Error(
'Spending tx in reference redeemer attempted to be called on a simple script input'
);
let content = redeemer;
if (type === 'Mesh') {
this.txInQueueItem.scriptTxIn.redeemer = {
Expand Down Expand Up @@ -585,6 +611,10 @@ export class MeshTxBuilderCore {
throw Error(
'Spending tx in reference attempted to be called a non script input'
);
if (this.txInQueueItem.type === 'SimpleScript')
throw Error(
'Spending tx in reference attempted to be called on a simple script input'
);
this.txInQueueItem.scriptTxIn.scriptSource = {
type: 'Inline',
txInInfo: {
Expand Down Expand Up @@ -1108,6 +1138,14 @@ export class MeshTxBuilderCore {
case 'PubKey':
this.addTxIn(currentTxIn as RequiredWith<PubKeyTxIn, 'txIn'>);
break;
case 'SimpleScript':
this.addNativeScriptTxIn(
currentTxIn as RequiredWith<
SimpleScriptTxIn,
'txIn' | 'simpleScriptTxIn'
>
);
break;
case 'Script':
this.addScriptTxIn(
currentTxIn as RequiredWith<ScriptTxIn, 'txIn' | 'scriptTxIn'>
Expand All @@ -1128,6 +1166,22 @@ export class MeshTxBuilderCore {
);
};

private addNativeScriptTxIn = ({
txIn,
simpleScriptTxIn,
}: RequiredWith<SimpleScriptTxIn, 'txIn' | 'simpleScriptTxIn'>) => {
if (simpleScriptTxIn.scriptSource.type === 'Provided') {
this.txBuilder.add_native_script_input(
csl.NativeScript.from_hex(simpleScriptTxIn.scriptSource.script),
csl.TransactionInput.new(
csl.TransactionHash.from_hex(txIn.txHash),
txIn.txIndex
),
toValue(txIn.amount)
);
}
};

private addScriptTxIn = ({
scriptTxIn,
txIn,
Expand Down
25 changes: 24 additions & 1 deletion packages/module/src/transaction/meshTxBuilder/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type MeshTxBuilderBody = {
signingKey: string[];
};

export type TxIn = PubKeyTxIn | ScriptTxIn;
export type TxIn = PubKeyTxIn | SimpleScriptTxIn | ScriptTxIn;

export type PubKeyTxIn = { type: 'PubKey'; txIn: TxInParameter };

Expand All @@ -36,6 +36,29 @@ export type TxInParameter = {
address?: string;
};

export type SimpleScriptTxIn = {
type: 'SimpleScript';
txIn: TxInParameter;
simpleScriptTxIn: SimpleScriptTxInParameter;
};

export type SimpleScriptTxInParameter = {
scriptSource?:
| {
type: 'Provided';
script: string;
}
| {
type: 'Inline';
txInInfo: SimpleScriptSourceInfo;
};
};

export type SimpleScriptSourceInfo = {
txHash: string;
txIndex: number;
};

export type ScriptTxIn = {
type: 'Script';
txIn: TxInParameter;
Expand Down
Loading