Skip to main content

Sign Transaction

Using Sui adapter you can either sign a transaction and keep it for later sending, or sign and execute it consecutively.

To create a simple Sui account from public key, use the following function, using the

@mysten/sui package.

import { publicKeyFromRawBytes } from "@mysten/sui/verify";

const createSuiWalletAccountFromString = (publicKey: string): WalletAccount => {
const suiPk = publicKeyFromRawBytes("ED25519", bs58.decode(publicKey));
return {
address: suiPk.toSuiAddress(),
publicKey: suiPk.toRawBytes(),
chains: SUI_CHAINS,
features: [
"standard:connect",
"standard:events",
"sui:signTransactionBlock",
"sui:signAndExecuteTransactionBlock",
"sui:signMessage",
],
};
};

To sign a transaction using the previously created adapter, we recommend you use the function signTransactionBlock() from the adapter. But first you need to create a transaction object that conforms with the type mentioned below.

import type { Transaction } from "@mysten/sui/transactions";

export interface SuiSignTransactionBlockInput {
transactionBlock: Transaction;
account: WalletAccount;
chain: IdentifierString;
}

Now you can create both an account from your public key, and a transaction object.

import { Transaction } from '@mysten/sui'

const account = createSuiWalletAccountFromString(YOUR_PUBLIC_KEY)

const tx = new Transaction()
const coin = tx.splitCoins(tx.gas, [tx.pure.u64(100)])
tx.transferObjects([coin], tx.pure.address(RECEIVER_SUI_ADDRESS))
tx.setSenderIfNotSet(RECEIVER_SUI_ADDRESS)

Finally you can sign the transaction.

await adapter.signTransactionBlock({
tx,
chain: 'sui:testnet',
account: account
})

Or you can sign and execute it immediately, like so.

await adapter.signAndExecuteTransaction({
tx,
chain: 'sui:testnet',
account: account
})