Skip to main content

Sign Transaction

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

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

@iota/iota-sdk package.

import { publicKeyFromRawBytes } from '@iota/iota-sdk/verify'
import type {
WalletAccount,
} from '@iota/wallet-standard'

const createIotaWalletAccountFromString = (publicKey: string): WalletAccount => {
const iotaPk = publicKeyFromRawBytes('ED25519', bs58.decode(publicKey))
return {
address: iotaPk.toIotaAddress(),
publicKey: iotaPk.toRawBytes(),
chains: ['iota:devnet', 'iota:testnet', 'iota:mainnet'],
features: [
'standard:connect',
'standard:events',
'iota:signTransaction',
'iota:signAndExecuteTransaction',
'iota:signPersonalMessage'
]
}
}

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

import type { IdentifierString, WalletAccount } from '@wallet-standard/core';

export interface IotaSignTransactionBlockInput {
transaction: {
toJSON: () => Promise<string>;
};
account: WalletAccount;
chain: IdentifierString;
}

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

import { Transaction } from '@iota/iota-sdk/transactions'

const account = createIotaWalletAccountFromString(YOUR_PUBLIC_KEY)

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

Finally you can sign the transaction.

await adapter.features['iota:signTransaction'].signTransaction({
tx,
chain: 'iota:testnet',
account: account
})

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

await adapter.features['iota:signAndExecuteTransaction'].signAndExecuteTransaction({
tx,
chain: 'iota:testnet',
account: account
})