Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglescode committed Sep 3, 2024
1 parent d097448 commit f52010c
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 59 deletions.
4 changes: 2 additions & 2 deletions apps/playground/src/pages/apis/txbuilder/basics/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const ReactPage: NextPage = () => {
{ label: "Initialize Tx Builder", to: "initializeTxbuilder" },
{ label: "Send value", to: "sendValue" },
{ label: "Multi-signature", to: "multisig" },
// { label: "Multisig native script", to: "multisigNativeScript" },
{ label: "Multisig native script", to: "multisigNativeScript" },
{ label: "Build with object", to: "buildWithObject" },
{ label: "Coin selection", to: "coinSelection" },
{ label: "Set metadata", to: "cip20" },
Expand Down Expand Up @@ -70,7 +70,7 @@ const ReactPage: NextPage = () => {
{/* <TxbuilderCommonFunctions /> */}
<TxbuilderSendValues />
<TxbuilderMultisig />
{/* <TxbuilderMultisigNativeScript /> */}
<TxbuilderMultisigNativeScript />
<TxbuilderBuildWithObject />
{/* <TxbuilderSetMetadata /> */}
<TxbuilderCoinSelection />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
MeshTxBuilder,
MeshWallet,
NativeScript,
resolveNativeScriptAddress,
resolveNativeScriptHash,
resolveScriptHash,
serializeNativeScript,
stringToHex,
Expand Down Expand Up @@ -35,12 +37,88 @@ export default function TxbuilderMultisigNativeScript() {
}

function Left() {
let codeKeyHash = ``;
codeKeyHash += `const { pubKeyHash: keyHash1 } = deserializeAddress(walletAddress1);\n`;
codeKeyHash += `const { pubKeyHash: keyHash2 } = deserializeAddress(walletAddress2);\n`;

let codeNativeScript = ``;
codeNativeScript += `const nativeScript: NativeScript = {\n`;
codeNativeScript += ` type: "all",\n`;
codeNativeScript += ` scripts: [\n`;
codeNativeScript += ` {\n`;
codeNativeScript += ` type: "sig",\n`;
codeNativeScript += ` keyHash: keyHash1,\n`;
codeNativeScript += ` },\n`;
codeNativeScript += ` {\n`;
codeNativeScript += ` type: "sig",\n`;
codeNativeScript += ` keyHash: keyHash2,\n`;
codeNativeScript += ` },\n`;
codeNativeScript += ` ],\n`;
codeNativeScript += `};\n`;

let codeSerializeNativeScript = ``;
codeSerializeNativeScript += `const { address: scriptAddress, scriptCbor } =\n`;
codeSerializeNativeScript += ` serializeNativeScript(nativeScript);\n`;

let codeTx = ``;
codeTx += `// get utxo from script\n`;
codeTx += `const utxos = await blockchainProvider.fetchAddressUTxOs(scriptAddress);\n`;
codeTx += `const utxo = utxos[0];\n`;
codeTx += `\n`;
codeTx += `// create tx\n`;
codeTx += `const unsignedTx = await txBuilder\n`;
codeTx += ` .txIn(\n`;
codeTx += ` utxo.input.txHash,\n`;
codeTx += ` utxo.input.outputIndex,\n`;
codeTx += ` utxo.output.amount,\n`;
codeTx += ` utxo.output.address,\n`;
codeTx += ` )\n`;
codeTx += ` .txInScript(scriptCbor)\n`;
codeTx += ` .txOut(\n`;
codeTx += ` "addr_test1vpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0c7e4cxr",\n`;
codeTx += ` [{ unit: "lovelace", quantity: "2000000" }],\n`;
codeTx += ` )\n`;
codeTx += ` .changeAddress(scriptAddress)\n`;
codeTx += ` .selectUtxosFrom(utxos)\n`;
codeTx += ` .complete();\n`;

let codeSign = ``;
codeSign += `const signedTx1 = await wallet1.signTx(unsignedTx, true);\n`;
codeSign += `const signedTx2 = await wallet2.signTx(signedTx1, true);\n`;
codeSign += `\n`;
codeSign += `const txHash = await wallet.submitTx(signedTx2);\n`;

return (
<>
<p></p>
<h4>Create native script</h4>
<p>
First, we need to create a native script. In this example, we will
create a native script with two signatures. That means we need to get
the key hashes of the two wallets.
</p>
<Codeblock data={codeKeyHash} />
<p>
Next, we will create a native script object with the two key hashes. The
native script object will be used to create a multi-signature
transaction.
</p>
<Codeblock data={codeNativeScript} />
<p>
The native script object is then serialized into a CBOR object and an
address.
</p>
<Codeblock data={codeSerializeNativeScript} />
<h4>Create transaction</h4>
<p>
Now that we have the native script, we can create a transaction with the
script. We first need to get the UTXO from the script address.
</p>
<Codeblock data={codeTx} />
<p>
Finally, we sign the transaction with the two wallets and submit the
transaction.
</p>
<Codeblock data={codeSign} />
</>
);
}
Expand Down Expand Up @@ -74,49 +152,10 @@ function Right() {
const walletAddress = (await wallet.getUsedAddresses())[0];
if (!walletAddress) return;
const { pubKeyHash: keyHash1 } = deserializeAddress(walletAddress);
// const nativeScriptA: NativeScript = {
// type: "all",
// scripts: [
// {
// type: "sig",
// keyHash: keyHash,
// },
// ],
// };

// console.log("Native script A");
// const { address: scriptAddressA, scriptCbor: scriptCborA } =
// serializeNativeScript(nativeScriptA);
// console.log("Script address:", scriptAddressA);
// console.log("Script CBOR:", scriptCborA);
console.log("keyHash1", keyHash1);

// second wallet
const { keyHash: keyHash2 } = getMeshWallet();
// const nativeScriptB: NativeScript = {
// type: "all",
// scripts: [
// {
// type: "sig",
// keyHash: keyHash2,
// },
// ],
// };

// console.log("Native script B");
// const { address: scriptAddressB, scriptCbor: scriptCborB } =
// serializeNativeScript(nativeScriptB);
// console.log("Script address:", scriptAddressB);
// console.log("Script CBOR:", scriptCborB);
console.log("keyHash2", keyHash2);

// combine

// const nativeScript: NativeScript = {
// type: "atLeast",
// required: 2,
// scripts: [nativeScriptA, nativeScriptB],
// };

const nativeScript: NativeScript = {
type: "all",
scripts: [
Expand All @@ -131,11 +170,8 @@ function Right() {
],
};

console.log("Native script combine");
const { address: scriptAddress, scriptCbor } =
serializeNativeScript(nativeScript);
console.log("Script address:", scriptAddress);
console.log("Script CBOR:", scriptCbor);

return { scriptAddress, scriptCbor: scriptCbor! };
}
Expand All @@ -144,23 +180,20 @@ function Right() {
if (!connected) return;

const script = await getScript();
if (!script) return;
if (!script) {
throw new Error("Failed to get script");
}
const { scriptAddress, scriptCbor } = script;

const blockchainProvider = getProvider();
const utxos = await blockchainProvider.fetchAddressUTxOs(scriptAddress);

if (utxos.length === 0) {
console.log("No utxos");
return;
throw new Error(`No utxos, fund address ${scriptAddress}`);
}
const utxo = utxos[0]!;
console.log("utxo", utxo);

// const walletAddress = (await wallet.getUsedAddresses())[0];
// if (!walletAddress) return;
// const { pubKeyHash: keyHash1 } = deserializeAddress(walletAddress);
const { wallet: walletB, keyHash: keyHash2 } = getMeshWallet();
const { wallet: walletB } = getMeshWallet();

const txBuilder = getTxBuilder();

Expand All @@ -180,22 +213,19 @@ function Right() {
.selectUtxosFrom(utxos)
.complete();

console.log("unsignedTx", unsignedTx);

const signedTx1 = await wallet.signTx(unsignedTx, true);
const signedTx2 = await walletB.signTx(signedTx1, true);

const txHash = await wallet.submitTx(signedTx2);
console.log("txHash", txHash);
return txHash;
}

let codeSnippet = ``;

return (
<LiveCodeDemo
title="Multi-signature Transaction"
subtitle="Create a multi-signature transaction. In this demo, we will create a transaction with two signatures, where one signature is from the user wallet and the other is from a minting wallet."
title="Multi-signature Transaction with native script "
subtitle="Create a multi-signature transaction with a native script. In this demo, we will create a transaction with two signatures, where one signature is from the user wallet and the other is from a minting wallet."
code={codeSnippet}
runCodeFunction={runDemo}
disabled={!connected}
Expand Down

0 comments on commit f52010c

Please sign in to comment.