Signing Transactions
Relevant source files
The following files were used as context for generating this wiki page:The sui-svelte-wallet-kit provides a streamlined API for dApps to request cryptographic signatures from a user's connected Sui wallet. This includes signing and executing on-chain transactions as well as signing arbitrary off-chain messages. The functionality is exposed through a set of exported helper functions that abstract the underlying wallet adapter's implementation details, providing a consistent interface regardless of which wallet is connected.
All signing functions first ensure a wallet is connected; otherwise, they throw an error. The core logic resides within the SuiModule.svelte component, which manages the active wallet adapter and delegates signing requests to it. The system intelligently checks for different feature implementations (e.g., standard vs. legacy methods) to maximize compatibility across the Sui wallet ecosystem.
Sources: README.md, src/lib/SuiModule/SuiModule.svelte:1018-1116
Transaction Signing and Execution
The primary function for interacting with the Sui network is signAndExecuteTransaction. This function takes a transaction object, requests the user's signature via their wallet, and submits it to the network for execution in a single atomic operation.
Sources: README.md, src/lib/SuiModule/SuiModule.svelte:1020-1048
Core Logic and Flow
When signAndExecuteTransaction is called, it performs a series of checks to find a compatible method on the currently connected walletAdapter. This ensures support for wallets that may implement different versions of the wallet standard.
The following diagram illustrates the sequence of operations when a dApp requests a transaction signature.
This diagram shows the fallback mechanism for finding a supported signing method on the wallet adapter.
Sources: src/lib/SuiModule/SuiModule.svelte:1020-1048
Function Signature
The signAndExecuteTransaction function accepts a transaction object and returns a promise that resolves with the execution result.
| Parameter | Type | Description |
|---|---|---|
transaction | any | The transaction object to be signed and executed. Typically an instance of Transaction from @mysten/sui/transactions. |
Sources: src/lib/SuiModule/SuiModule.svelte:1020-1021, src/lib/SuiModule/types.ts:68-73
Usage Example
The following example from the demo page demonstrates how to build a simple transaction and call signAndExecuteTransaction.
<script>
import { signAndExecuteTransaction, useCurrentAccount } from 'sui-svelte-wallet-kit';
import { Transaction } from '@mysten/sui/transactions';
let account = $derived(useCurrentAccount());
let isLoading = false;
let transactionResult = null;
let error = null;
const testTransaction = async () => {
if (!account) {
error = 'Please connect your wallet first';
return;
}
isLoading = true;
error = null;
transactionResult = null;
try {
const tx = new Transaction();
// Example: transfer 0 SUI to self (no-op)
tx.transferObjects([tx.splitCoins(tx.gas, [0])], account.address);
transactionResult = await signAndExecuteTransaction(tx);
} catch (err) {
error = err?.message || 'Transaction failed';
} finally {
isLoading = false;
}
};
</script>
<button onclick={testTransaction} disabled={isLoading}>
{isLoading ? 'Signing Transaction...' : 'Test Transaction (0 SUI transfer)'}
</button>
Sources: README.md, src/routes/+page.svelte:90-113
Message Signing
For off-chain operations, such as authentication or proving ownership of an address, the kit provides the signMessage function. This allows the dApp to request a signature for an arbitrary string or byte array.
Sources: README.md, src/lib/SuiModule/SuiModule.svelte:1050-1100
Core Logic and Flow
Similar to transaction signing, signMessage checks the connected wallet adapter for a supported method. It prioritizes the Wallet Standard sui:signMessage feature, with fallbacks for sui:signPersonalMessage and older, adapter-specific implementations.
The message is always encoded to a Uint8Array before being passed to the adapter.
This diagram shows the fallback logic for message signing, prioritizing standard features.
Sources: src/lib/SuiModule/SuiModule.svelte:1050-1100
Supporting Functions
signMessage
Requests a signature for a given message.
| Parameter | Type | Description |
|---|---|---|
message | string | Uint8Array | The message to be signed. |
Returns: Promise<{ signature: string; messageBytes: string }> - An object containing the base64 signature and the hex-encoded message bytes.
Sources: src/lib/SuiModule/SuiModule.svelte:1050-1053, src/lib/SuiModule/types.ts:88-95
canSignMessage
A utility function to check if the currently connected wallet supports any of the message signing methods. This is useful for conditionally rendering UI elements related to message signing.
Returns: boolean - true if the wallet supports message signing, false otherwise.
Sources: src/lib/SuiModule/SuiModule.svelte:1102-1116
Usage Example
The following example shows how to use canSignMessage to conditionally display a signing button and then call signMessage.
<script>
import { canSignMessage, signMessage, useCurrentAccount } from 'sui-svelte-wallet-kit';
let account = $derived(useCurrentAccount());
let message = 'Hello, Sui blockchain!';
let signatureResult = null;
let isSigningMessage = false;
let error = null;
const testSignMessage = async () => {
if (!account) {
error = 'Please connect your wallet first';
return;
}
isSigningMessage = true;
try {
signatureResult = await signMessage(message);
} catch (err) {
error = err?.message || 'Message signing failed';
} finally {
isSigningMessage = false;
}
};
</script>
{#if account}
{#if canSignMessage()}
<input bind:value={message} placeholder="Enter message to sign" />
<button onclick={testSignMessage} disabled={isSigningMessage}>
{isSigningMessage ? 'Signing Message...' : 'Sign Message'}
</button>
{:else}
<p>Current wallet does not support message signing.</p>
{/if}
{/if}
{#if signatureResult}
<p><strong>Signature:</strong> <code>{signatureResult.signature}</code></p>
{/if}
Sources: README.md, src/routes/+page.svelte:115-140, src/lib/index.d.ts:20-21
Summary
The signing functionality in sui-svelte-wallet-kit is designed for robustness and developer convenience. By abstracting wallet-specific implementations behind signAndExecuteTransaction and signMessage, it provides a unified API for core blockchain interactions. The built-in fallback logic ensures broad compatibility with wallets adhering to different stages of the Wallet Standard, making the development of portable and future-proof Sui dApps simpler.
Sources: src/lib/SuiModule/SuiModule.svelte, README.md