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

setNativeScriptInput #231

Merged
merged 2 commits into from
May 29, 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
3 changes: 3 additions & 0 deletions packages/demo/components/pages/apis/resolvers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import ResolveTxHash from './resolveTxHash';
import ResolveEpochNo from './resolveEpochNo';
import ResolveSlotNo from './resolveSlotNo';
import ResolveNativeScriptHash from './resolveNativeScriptHash';
import ResolveNativeScriptAddress from './resolveNativeScriptAddress';

export default function Resolvers() {
const sidebarItems = [
{ label: 'Data Hash', to: 'resolveDataHash' },
{ label: 'Fingerprint', to: 'resolveFingerprint' },
{ label: 'Native Script Address', to: 'resolveNativeScriptAddress' },
{ label: 'Native Script Hash', to: 'resolveNativeScriptHash' },
{ label: 'Payment Key Hash', to: 'resolvePaymentKeyHash' },
{ label: 'Plutus Script Address', to: 'resolvePlutusScriptAddress' },
Expand All @@ -42,6 +44,7 @@ function Main() {
<>
<ResolveDataHash />
<ResolveFingerprint />
<ResolveNativeScriptAddress />
<ResolveNativeScriptHash />
<ResolvePaymentKeyHash />
<ResolvePlutusScriptAddress />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { useState } from 'react';
import Codeblock from '../../../ui/codeblock';
import Card from '../../../ui/card';
import SectionTwoCol from '../../../common/sectionTwoCol';
import RunDemoButton from '../../../common/runDemoButton';
import RunDemoResult from '../../../common/runDemoResult';
import {
resolveNativeScriptAddress,
resolvePaymentKeyHash,
resolveSlotNo,
} from '@meshsdk/core';
import Input from '../../../ui/input';
import type { NativeScript } from '@meshsdk/core';
import { demoAddresses } from '../../../../configs/demo';

export default function ResolveNativeScriptAddress() {
const [userinput, setUserinput] = useState<string>(demoAddresses.mainnet);
const [userinput2, setUserinput2] = useState<string>('meshtoken');

return (
<SectionTwoCol
sidebarTo="resolveNativeScriptAddress"
header="Resolve Native Script Address"
leftFn={Left(userinput, userinput2)}
rightFn={Right(userinput, setUserinput, userinput2, setUserinput2)}
/>
);
}

function Left(userinput, userinput2) {
let code1 = `import { resolveNativeScriptHash, resolvePaymentKeyHash, resolveSlotNo } from '@meshsdk/core';\n\n`;
code1 += `const keyHash = resolvePaymentKeyHash('${userinput}');\n`;
code1 += `\n`;
code1 += `let oneYearFromNow = new Date();\n`;
code1 += `oneYearFromNow.setFullYear(oneYearFromNow.getFullYear() + 1);\n`;
code1 += `const slot = resolveSlotNo('mainnet', oneYearFromNow.getTime());\n`;
code1 += `\n`;
code1 += `const nativeScript: NativeScript = {\n`;
code1 += ` type: 'all',\n`;
code1 += ` scripts: [\n`;
code1 += ` {\n`;
code1 += ` type: 'before',\n`;
code1 += ` slot: slot,\n`;
code1 += ` },\n`;
code1 += ` {\n`;
code1 += ` type: 'sig',\n`;
code1 += ` keyHash: keyHash,\n`;
code1 += ` },\n`;
code1 += ` ],\n`;
code1 += `};\n`;
code1 += `\n`;
code1 += `const address = resolveNativeScriptAddress(nativeScript, ${
userinput.substring(0, 5) === 'addr1' ? 1 : 0
});\n`;

return (
<>
<p>
Converts <code>NativeScript</code> into address.
</p>
<Codeblock data={code1} isJson={false} />
</>
);
}

function Right(userinput, setUserinput, userinput2, setUserinput2) {
const [loading, setLoading] = useState<boolean>(false);
const [response, setResponse] = useState<null | any>(null);
const [responseError, setResponseError] = useState<null | any>(null);

async function runDemo() {
setLoading(true);
setResponse(null);
setResponseError(null);

try {
const keyHash = resolvePaymentKeyHash(userinput);

let oneYearFromNow = new Date();
oneYearFromNow.setFullYear(oneYearFromNow.getFullYear() + 1);
const slot = resolveSlotNo('mainnet', oneYearFromNow.getTime());

const nativeScript: NativeScript = {
type: 'all',
scripts: [
{
type: 'before',
slot: slot,
},
{
type: 'sig',
keyHash: keyHash,
},
],
};

const address = resolveNativeScriptAddress(
nativeScript,
userinput.substring(0, 5) === 'addr1' ? 1 : 0
);
setResponse(address);
} catch (error) {
setResponseError(`${error}`);
}
setLoading(false);
}

return (
<>
<Card>
<Input
value={userinput}
onChange={(e) => setUserinput(e.target.value)}
placeholder="Address"
label="Address"
/>
<RunDemoButton
runDemoFn={runDemo}
loading={loading}
response={response}
/>
<RunDemoResult response={response} />
<RunDemoResult response={responseError} label="Error" />
</Card>
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function Left({}) {
<p></p>
<Codeblock data={codeSnippet} isJson={false} />
<p>
where <code>UTxO</code> has the following format:
where <code>UTxO</code> has the following format (use one of our providers):
</p>
<Codeblock data={codeUtxo} isJson={false} />
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import Codeblock from '../../../../ui/codeblock';
import SectionTwoCol from '../../../../common/sectionTwoCol';

export default function SetNativeScriptInput() {
return (
<SectionTwoCol
sidebarTo="setNativeScriptInput"
header="Set Native Script"
leftFn={Left({})}
rightFn={Right({})}
/>
);
}

function Left({}) {
let codeSnippet = ``;
codeSnippet += `import { Transaction } from '@meshsdk/core';\n`;
codeSnippet += `\n`;
codeSnippet += `const tx = new Transaction({ initiator: wallet });\n`;
codeSnippet += `tx.setNativeScriptInput(nativeScript, utxo);\n`;

let codeUtxo = ``;
codeUtxo += `{\n`;
codeUtxo += ` input: {\n`;
codeUtxo += ` outputIndex: number;\n`;
codeUtxo += ` txHash: string;\n`;
codeUtxo += ` };\n`;
codeUtxo += ` output: {\n`;
codeUtxo += ` address: string;\n`;
codeUtxo += ` amount: Asset[];\n`;
codeUtxo += ` dataHash?: string;\n`;
codeUtxo += ` plutusData?: string;\n`;
codeUtxo += ` scriptRef?: string;\n`;
codeUtxo += ` };\n`;
codeUtxo += `}\n`;

let codeScript = ``;
codeScript += `import type { NativeScript } from '@meshsdk/core';\n`;
codeScript += `\n`;
codeScript += `const nativeScript: NativeScript = {\n`;
codeScript += ` type: 'all',\n`;
codeScript += ` scripts:\n`;
codeScript += ` [\n`;
codeScript += ` {\n`;
codeScript += ` type: 'before',\n`;
codeScript += ` slot: '<insert slot here>'\n`;
codeScript += ` },\n`;
codeScript += ` {\n`;
codeScript += ` type: 'sig',\n`;
codeScript += ` keyHash: '<insert keyHash here>'\n`;
codeScript += ` }\n`;
codeScript += ` ]\n`;
codeScript += `};\n`;

return (
<>
<p>
This function allows you to set the Native Script input for the
transaction.
</p>
<Codeblock data={codeSnippet} isJson={false} />
<p>
where <code>NativeScript</code> has the following format:
</p>
<Codeblock data={codeScript} isJson={false} />
<p>
where <code>UTxO</code> has the following format (use one of our providers):
</p>
<Codeblock data={codeUtxo} isJson={false} />
</>
);
}

function Right({}) {
return <></>;
}
3 changes: 3 additions & 0 deletions packages/demo/pages/apis/transaction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Metatags from '../../../components/site/metatags';
import SetCollateral from '../../../components/pages/apis/transaction/basic/setCollateral';
import SetRequiredSigners from '../../../components/pages/apis/transaction/basic/setRequiredSigners';
import SendValue from '../../../components/pages/apis/transaction/basic/sendValue';
import SetNativeScriptInput from '../../../components/pages/apis/transaction/basic/setNativeScriptInput';

const TransactionPage: NextPage = () => {
const sidebarItems = [
Expand All @@ -26,6 +27,7 @@ const TransactionPage: NextPage = () => {
{ label: 'Set required signers', to: 'setRequiredSigners' },
{ label: 'Set time limit', to: 'setTimeLimit' },
{ label: 'Set metadata', to: 'setMetadata' },
{ label: 'Set Native Script', to: 'setNativeScriptInput' },
];

let codeRecipient = ``;
Expand Down Expand Up @@ -62,6 +64,7 @@ const TransactionPage: NextPage = () => {
<SetRequiredSigners />
<SetTimeLimit />
<TxSetMetadata />
<SetNativeScriptInput />
</CommonLayout>
</>
);
Expand Down
Loading
Loading