Skip to content

Commit

Permalink
update docs and fix drepid
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglescode committed Sep 3, 2024
1 parent ee4b1c0 commit 4205fb5
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 50 deletions.
5 changes: 2 additions & 3 deletions apps/playground/src/data/links-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const metaDataJson = {
link: "/apis/data/json",
icon: Bars3Icon,
};
// todoß
export const metaDataValue = {
title: "Value",
desc: "Manipulate Cardano Value Easily",
Expand All @@ -52,8 +51,8 @@ export const linksData: MenuItem[] = [
metaDataMesh,
metaDataJson,
metaDataValue,
// metaDataCbor,
// metaDataUtils,
// metaDataCbor, // todo
// metaDataUtils, // todo
];

export const metaData = {
Expand Down
3 changes: 3 additions & 0 deletions apps/playground/src/pages/apis/txbuilder/basics/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import TxbuilderSendValues from "./send-values";
import TxbuilderSetNetwork from "./set-network";
import TxbuilderSetRequiredSigners from "./set-required-signers";
import TxbuilderSetTime from "./set-time";
import TxbuilderMultisigNativeScript from "./multisig-native-script";

const ReactPage: NextPage = () => {
const sidebarItems = [
{ label: "Initialize Tx Builder", to: "initializeTxbuilder" },
{ label: "Send value", to: "sendValue" },
{ label: "Multi-signature", to: "multisig" },
// { 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 @@ -68,6 +70,7 @@ const ReactPage: NextPage = () => {
{/* <TxbuilderCommonFunctions /> */}
<TxbuilderSendValues />
<TxbuilderMultisig />
{/* <TxbuilderMultisigNativeScript /> */}
<TxbuilderBuildWithObject />
{/* <TxbuilderSetMetadata /> */}
<TxbuilderCoinSelection />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
import {
AppWallet,
deserializeAddress,
ForgeScript,
MeshTxBuilder,
MeshWallet,
NativeScript,
resolveScriptHash,
serializeNativeScript,
stringToHex,
} from "@meshsdk/core";
import { useWallet } from "@meshsdk/react";

import { getProvider } from "~/components/cardano/mesh-wallet";
import LiveCodeDemo from "~/components/sections/live-code-demo";
import TwoColumnsScroll from "~/components/sections/two-columns-scroll";
import Codeblock from "~/components/text/codeblock";
import { demoAssetMetadata, demoMnemonic } from "~/data/cardano";
import { getTxBuilder } from "../common";

// const scriptAddress =
// "addr_test1vpd5480qj5jj4pnjwq9yxnac8l9dw2k3y6gz2cpp6jawzwq838jl8";
// const scriptCbor =
// "8201828200581c556f3a70b8a68081cf36c918dd9933abdca34f20fc534499c817182b8200581c5867c3b8e27840f556ac268b781578b14c5661fc63ee720dbeab663f";

export default function TxbuilderMultisigNativeScript() {
return (
<TwoColumnsScroll
sidebarTo="multisigNativeScript"
title="Multi-signature Transaction with Native Script"
leftSection={Left()}
rightSection={Right()}
/>
);
}

function Left() {
let codeTx = ``;

return (
<>
<p></p>
<Codeblock data={codeTx} />
</>
);
}

function Right() {
const { wallet, connected } = useWallet();

function getMeshWallet() {
const blockchainProvider = getProvider();

const wallet = new MeshWallet({
networkId: 0,
fetcher: blockchainProvider,
submitter: blockchainProvider,
key: {
type: "mnemonic",
words: "solution,".repeat(24).split(",").slice(0, 24),
},
});

const walletAddress = wallet.getChangeAddress();

const { pubKeyHash: keyHash } = deserializeAddress(walletAddress);
return { wallet, keyHash, walletAddress };
}

async function getScript() {
if (!connected) return;

// first wallet
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: [
{
type: "sig",
keyHash: keyHash1,
},
{
type: "sig",
keyHash: keyHash2,
},
],
};

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! };
}

async function runDemo() {
if (!connected) return;

const script = await getScript();
if (!script) return;
const { scriptAddress, scriptCbor } = script;

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

if (utxos.length === 0) {
console.log("No utxos");
return;
}
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 txBuilder = getTxBuilder();

const unsignedTx = await txBuilder
.txIn(
utxo.input.txHash,
utxo.input.outputIndex,
utxo.output.amount,
utxo.output.address,
)
.txInScript(scriptCbor)
.txOut(
"addr_test1vpvx0sacufuypa2k4sngk7q40zc5c4npl337uusdh64kv0c7e4cxr",
[{ unit: "lovelace", quantity: "2000000" }],
)
.changeAddress(scriptAddress)
.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."
code={codeSnippet}
runCodeFunction={runDemo}
disabled={!connected}
runDemoButtonTooltip={
!connected ? "Connect wallet to run this demo" : undefined
}
runDemoShowBrowseWalletConnect={true}
></LiveCodeDemo>
);
}
12 changes: 6 additions & 6 deletions apps/playground/src/pages/apis/txbuilder/basics/multisig.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
AppWallet,
MeshWallet,
ForgeScript,
resolveScriptHash,
stringToHex,
Expand Down Expand Up @@ -64,7 +64,7 @@ function Right() {

async function runDemo() {
const blockchainProvider = getProvider();
const mintingWallet = new AppWallet({
const mintingWallet = new MeshWallet({
networkId: 0,
fetcher: blockchainProvider,
submitter: blockchainProvider,
Expand All @@ -74,7 +74,7 @@ function Right() {
},
});
const forgingScript = ForgeScript.withOneSignature(
mintingWallet.getPaymentAddress(),
mintingWallet.getChangeAddress(),
);
const assetName = "MeshToken";
const policyId = resolveScriptHash(forgingScript);
Expand All @@ -99,8 +99,8 @@ function Right() {
return txHash;
}

let codeSnippet = `import { AppWallet, ForgeScript, Mint, Transaction } from '@meshsdk/core';\n\n`;
codeSnippet += `const mintingWallet = new AppWallet({\n`;
let codeSnippet = `import { MeshWallet, ForgeScript, Mint, Transaction } from '@meshsdk/core';\n\n`;
codeSnippet += `const mintingWallet = new MeshWallet({\n`;
codeSnippet += ` networkId: 0,\n`;
codeSnippet += ` fetcher: blockchainProvider,\n`;
codeSnippet += ` submitter: blockchainProvider,\n`;
Expand All @@ -111,7 +111,7 @@ function Right() {
codeSnippet += `});\n`;
codeSnippet += `\n`;
codeSnippet += `const forgingScript = ForgeScript.withOneSignature(\n`;
codeSnippet += ` mintingWallet.getPaymentAddress(),\n`;
codeSnippet += ` mintingWallet.getChangeAddress(),\n`;
codeSnippet += `);\n`;
codeSnippet += `\n`;
codeSnippet += `const assetName = "MeshToken";\n`;
Expand Down
1 change: 1 addition & 0 deletions apps/playground/src/pages/apis/txbuilder/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export function getTxBuilder() {
const txBuilder = new MeshTxBuilder({
fetcher: blockchainProvider,
evaluator: blockchainProvider,
verbose: true,
});
txBuilder.setNetwork("preprod");
return txBuilder;
Expand Down
Loading

0 comments on commit 4205fb5

Please sign in to comment.