Skip to content

Commit

Permalink
feat: update examples to use new signTxnAndVerifySignature
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceeewong committed Nov 2, 2024
1 parent b4e6032 commit 33b9e5e
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 38 deletions.
52 changes: 52 additions & 0 deletions examples/with-next-app-router/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { useMemo } from "react";
import { Transaction } from "@mysten/sui/transactions";
import { SuiSignAndExecuteTransactionOutput } from "@mysten/wallet-standard";
import { SuiTransactionBlockResponse } from "@mysten/sui/client";
import { Ed25519PublicKey } from "@mysten/sui/keypairs/ed25519";
import { Buffer } from "buffer";

const sampleNft = new Map([
[
Expand Down Expand Up @@ -122,6 +124,51 @@ export default function Home() {
}
}

const handleSignTxnAndVerifySignature = async (contractAddress: string) => {
const txn = new Transaction();
txn.moveCall({
target: contractAddress as any,
arguments: [
txn.pure.string("Suiet NFT"),
txn.pure.string("Suiet Sample NFT"),
txn.pure.string(
"https://xc6fbqjny4wfkgukliockypoutzhcqwjmlw2gigombpp2ynufaxa.arweave.net/uLxQwS3HLFUailocJWHupPJxQsli7aMgzmBe_WG0KC4"
),
],
});
txn.setSender(wallet.account?.address as string);

try {
const signedTxn = await wallet.signTransaction({
transaction: txn,
});

console.log(`Sign and verify txn:`);
console.log("--wallet: ", wallet.adapter?.name);
console.log("--account: ", wallet.account?.address);
const publicKey = wallet.account?.publicKey;
if (!publicKey) {
console.error("no public key provided by wallet");
return;
}
console.log("-- publicKey: ", publicKey);
const pubKey = new Ed25519PublicKey(publicKey);
console.log("-- signed txnBytes: ", signedTxn.bytes);
console.log("-- signed signature: ", signedTxn.signature);
const txnBytes = new Uint8Array(Buffer.from(signedTxn.bytes, "base64"));
const isValid = await pubKey.verifyTransaction(txnBytes, signedTxn.signature);
console.log("-- use pubKey to verify transaction: ", isValid);
if (!isValid) {
alert(`signTransaction succeed, but verify transaction failed`);
} else {
alert(`signTransaction succeed, and verify transaction succeed!`);
}
} catch (e) {
console.error("signTransaction failed", e);
alert("signTransaction failed (see response in the console)");
}
};

const chainName = (chainId: string | undefined) => {
switch (chainId) {
case SuiChainId.MAIN_NET:
Expand Down Expand Up @@ -222,6 +269,11 @@ export default function Home() {
<button className={"mt-4"} onClick={handleSignMsg}>
signMessage
</button>
{nftContractAddr && (
<button className={"mt-4"} onClick={() => handleSignTxnAndVerifySignature(nftContractAddr)}>
Sign & Verify Transaction
</button>
)}
</div>
</div>
)}
Expand Down
52 changes: 52 additions & 0 deletions examples/with-next/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
} from "@suiet/wallet-kit";
import { Transaction } from "@mysten/sui/transactions";
import { useMemo } from "react";
import { Ed25519PublicKey } from "@mysten/sui/keypairs/ed25519";
import { Buffer } from "buffer";

const sampleNft = new Map([
[
Expand Down Expand Up @@ -87,6 +89,51 @@ function App() {
}
}

const handleSignTxnAndVerifySignature = async (contractAddress: string) => {
const txn = new Transaction();
txn.moveCall({
target: contractAddress as any,
arguments: [
txn.pure.string("Suiet NFT"),
txn.pure.string("Suiet Sample NFT"),
txn.pure.string(
"https://xc6fbqjny4wfkgukliockypoutzhcqwjmlw2gigombpp2ynufaxa.arweave.net/uLxQwS3HLFUailocJWHupPJxQsli7aMgzmBe_WG0KC4"
),
],
});
txn.setSender(wallet.account?.address as string);

try {
const signedTxn = await wallet.signTransaction({
transaction: txn,
});

console.log(`Sign and verify txn:`);
console.log("--wallet: ", wallet.adapter?.name);
console.log("--account: ", wallet.account?.address);
const publicKey = wallet.account?.publicKey;
if (!publicKey) {
console.error("no public key provided by wallet");
return;
}
console.log("-- publicKey: ", publicKey);
const pubKey = new Ed25519PublicKey(publicKey);
console.log("-- signed txnBytes: ", signedTxn.bytes);
console.log("-- signed signature: ", signedTxn.signature);
const txnBytes = new Uint8Array(Buffer.from(signedTxn.bytes, "base64"));
const isValid = await pubKey.verifyTransaction(txnBytes, signedTxn.signature);
console.log("-- use pubKey to verify transaction: ", isValid);
if (!isValid) {
alert(`signTransaction succeed, but verify transaction failed`);
} else {
alert(`signTransaction succeed, and verify transaction succeed!`);
}
} catch (e) {
console.error("signTransaction failed", e);
alert("signTransaction failed (see response in the console)");
}
};

const chainName = (chainId: string | undefined) => {
switch (chainId) {
case SuiChainId.MAIN_NET:
Expand Down Expand Up @@ -156,6 +203,11 @@ function App() {
</button>
)}
<button onClick={handleSignMsg}>signMessage</button>
{nftContractAddr && (
<button onClick={() => handleSignTxnAndVerifySignature(nftContractAddr)}>
Sign & Verify Transaction
</button>
)}
</div>
</div>
)}
Expand Down
83 changes: 69 additions & 14 deletions examples/with-vite/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
import "@suiet/wallet-kit/style.css";
import { Transaction } from "@mysten/sui/transactions";
import { useMemo } from "react";
import { Ed25519PublicKey } from "@mysten/sui/keypairs/ed25519";
import { Buffer } from "buffer";

const sampleNft = new Map([
[
Expand All @@ -28,6 +30,21 @@ const sampleNft = new Map([
],
]);

function createMintNftTxb(contractAddress: string) {
const tx = new Transaction();
tx.moveCall({
target: contractAddress,
arguments: [
tx.pure.string("Suiet NFT"),
tx.pure.string("Suiet Sample NFT"),
tx.pure.string(
"https://xc6fbqjny4wfkgukliockypoutzhcqwjmlw2gigombpp2ynufaxa.arweave.net/uLxQwS3HLFUailocJWHupPJxQsli7aMgzmBe_WG0KC4"
),
],
});
return tx;
}

function App() {
const wallet = useWallet();
const client = useSuiClient();
Expand All @@ -52,17 +69,7 @@ function App() {
) {
if (!target) return;
try {
const tx = new Transaction();
tx.moveCall({
target: target,
arguments: [
tx.pure.string("Suiet NFT"),
tx.pure.string("Suiet Sample NFT"),
tx.pure.string(
"https://xc6fbqjny4wfkgukliockypoutzhcqwjmlw2gigombpp2ynufaxa.arweave.net/uLxQwS3HLFUailocJWHupPJxQsli7aMgzmBe_WG0KC4"
),
],
});
const tx = createMintNftTxb(target);

if (!opts?.isCustomExecution) {
const resData = await wallet.signAndExecuteTransaction({
Expand Down Expand Up @@ -90,10 +97,10 @@ function App() {
console.log("signAndExecuteTransaction success", resData);
}

alert("executeMoveCall succeeded (see response in the console)");
alert("executeTransactionBlock succeeded (see response in the console)");
} catch (e) {
console.error("executeMoveCall failed", e);
alert("executeMoveCall failed (see response in the console)");
alert("executeTransactionBlock failed (see response in the console)");
}
}

Expand Down Expand Up @@ -121,6 +128,43 @@ function App() {
}
}

const handleSignTxnAndVerifySignature = async (contractAddress: string) => {
const txn = createMintNftTxb(contractAddress);
txn.setSender(wallet.account?.address as string);
try {
const signedTxn = await wallet.signTransaction({
transaction: txn,
});

console.log(`Sign and verify txn:`);
console.log("--wallet: ", wallet.adapter?.name);
console.log("--account: ", wallet.account?.address);
const publicKey = wallet.account?.publicKey;
if (!publicKey) {
console.error("no public key provided by wallet");
return;
}
console.log("-- publicKey: ", publicKey);
const pubKey = new Ed25519PublicKey(publicKey);
console.log("-- signed txnBytes: ", signedTxn.bytes);
console.log("-- signed signature: ", signedTxn.signature);
const txnBytes = new Uint8Array(Buffer.from(signedTxn.bytes, "base64"));
const isValid = await pubKey.verifyTransaction(
txnBytes,
signedTxn.signature
);
console.log("-- use pubKey to verify transaction: ", isValid);
if (!isValid) {
alert(`signTransaction succeed, but verify transaction failed`);
} else {
alert(`signTransaction succeed, and verify transaction succeed!`);
}
} catch (e) {
console.error("signTransaction failed", e);
alert("signTransaction failed (see response in the console)");
}
};

const chainName = (chainId: string | undefined) => {
switch (chainId) {
case SuiChainId.MAIN_NET:
Expand Down Expand Up @@ -197,7 +241,18 @@ function App() {
Mint {chainName(wallet.chain?.id)} NFT
</button>
)}
<button onClick={handleSignMsg}>signMessage</button>
<button onClick={handleSignMsg}>
Sign & Verify PersonalMessage
</button>
{nftContractAddr && (
<button
onClick={() =>
handleSignTxnAndVerifySignature(nftContractAddr)
}
>
Sign & Verify Transaction
</button>
)}
</div>
</div>
)}
Expand Down
Loading

0 comments on commit 33b9e5e

Please sign in to comment.