Wallet Standard
Browser extension wallets built for IOTA use the Wallet standard. This is a cross-chain standard that defines how dApps can automatically discover and interact with wallets.
If you are building a wallet, the helper library @iota/wallet-standard
provides types and utilities to help get started.
Working With Wallets
The Wallet standard includes features to help build wallets.
Creating a Wallet Interface
Create a class that represents your wallet. Use the Wallet
interface from
@iota/wallet-standard
to help ensure your class adheres to the standard.
import { IOTA_DEVNET_CHAIN, Wallet } from '@iota/wallet-standard';
class YourWallet implements Wallet {
get version() {
// Return the version of the Wallet Standard this implements (in this case, 1.0.0).
return '1.0.0';
}
get name() {
return 'Wallet Name';
}
get icon() {
return 'some-icon-data-url';
}
// Return the IOTA chains that your wallet supports.
get chains() {
return [IOTA_DEVNET_CHAIN];
}
}
Implementing Features
Features are standard methods consumers can use to interact with a wallet. To be listed in the IOTA wallet adapter, you must implement the following features in your wallet:
standard:connect
- Use to initiate a connection to the wallet.standard:events
- Use to listen for changes that happen within the wallet, such as accounts being added or removed.iota:signPersonalMessage
- Use to prompt the user to sign a personal message and return the message signature back to the dApp. Use this to verify the user’s public key.iota:signTransaction
- Use to prompt the user to sign a transaction and return the serialized transaction and signature back to the dApp. This method does not submit the transaction for execution.iota:signAndExecuteTransaction
- Use to prompt the user to sign a transaction, then submit it for execution to the blockchain.iota:reportTransactionEffects
- Use to report the effects of a transaction executed in the dApp to the wallet. This allows the wallet to update its internal state to reflect the changes the transaction makes.
Implement these features in your wallet class under the features
property:
import {
ConnectFeature,
ConnectMethod,
EventsFeature,
EventsOnMethod,
IOTAFeatures,
IOTASignPersonalMessageMethod,
IOTASignTransactionMethod,
IOTASignAndExecuteTransactionMethod,
IOTAReportTransactionEffectsMethod
} from "@iota/wallet-standard";
class YourWallet implements Wallet {
/* ... existing code from above ... */
get features(): ConnectFeature & EventsFeature & IOTAFeatures {
return {
"standard:connect": {
version: "1.0.0",
connect: this.#connect,
},
"standard:events": {
version: "1.0.0",
on: this.#on,
},
"iota:signPersonalMessage": {
version: "1.0.0",
signPersonalMessage: this.#signPersonalMessage,
},
"iota:signTransaction": {
version: "2.0.0",
signTransaction: this.#signTransaction,
},
"iota:signAndExecuteTransaction": {
version: "2.0.0",
signAndExecuteTransaction: this.#signAndExecuteTransaction,
},
"iota:reportTransactionEffects": {
version: "1.0.0",
reportTransactionEffects: this.#reportTransactionEffects,
};
},
#on: EventsOnMethod = () => {
// Your wallet's events on implementation.
};
#connect: ConnectMethod = () => {
// Your wallet's connect implementation
};
#signPersonalMessage: IOTASignPersonalMessageMethod = () => {
// Your wallet's signTransaction implementation
};
#signTransaction: IOTASignTransactionMethod = () => {
// Your wallet's signTransaction implementation
};
#signAndExecuteTransaction: IOTASignAndExecuteTransactionMethod = () => {
// Your wallet's signAndExecuteTransaction implementation
};
#reportTransactionEffects: IOTAReportTransactionEffectsMethod = () => {
// Your wallet's reportTransactionEffects implementation
};
}
Exposing Accounts
The last requirement of the wallet interface is to expose an accounts
interface. This should
expose all of the accounts that a connected dApp has access to. It can be empty prior to initiating
a connection through the standard:connect
feature.
The accounts use the ReadonlyWalletAccount
class to construct an account matching the
required interface.
import { ReadonlyWalletAccount } from '@iota/wallet-standard';
class YourWallet implements Wallet {
get accounts() {
// Assuming we already have some internal representation of accounts:
return someWalletAccounts.map(
(walletAccount) =>
// Return
new ReadonlyWalletAccount({
address: walletAccount.iotaAddress,
publicKey: walletAccount.pubkey,
// The IOTA chains that your wallet supports.
chains: [IOTA_DEVNET_CHAIN],
// The features that this account supports. This can be a subset of the wallet's supported features.
// These features must exist on the wallet as well.
features: [
'iota:signPersonalMessage',
'iota:signTransaction',
'iota:signAndExecuteTransaction',
],
}),
);
}
}
Registering in the Window
After you have a compatible interface for your wallet, use the registerWallet
function to register it.
import { registerWallet } from '@iota/wallet-standard';
registerWallet(new YourWallet());
Best Practices for Efficient Transaction Execution
The Wallet standard uses the iota:signAndExecuteTransaction
feature.
The features of this method are useful, regardless of which API you use to execute transactions.
However, this usefulness comes at the expense of flexibility in what iota:signAndExecuteTransaction
returns.
To solve this problem, use the iota:signTransaction
feature to sign transactions, and
leave transaction execution to the dApp. The dApp can query for additional data during
execution using whichever API it chooses. This is consistent with the default @iota/dapp-kit
uses for the useSignAndExecuteTransaction
hook, and enables dApps to take
advantage of read-after-write consistency when interacting with the Full-node based JSON RPC.
The downside of this strategy is that wallets often use different RPC nodes than the dApp, and might not have indexed the previous transaction when executing multiple transactions in rapid succession. This leads to building transactions using stale data that fail upon execution.
To mitigate this, wallets can use the iota:reportTransactionEffects
feature so that dApps can report
the effects of transactions to the wallet. Transaction effects contain the updated versions and
digests of any objects that a transaction uses or creates. By caching these values, wallets can build
transactions without needing to resolve the most recent versions through an API call.
The @iota/iota/transaction
SDK exports the SerialTransactionExecutor
class, which you can use
to build transactions using an object cache. The class has a method to update its internal cache using the
effects of a transaction.
Using the combination of iota:signTransaction
and iota:reportTransactionEffects
, dApps can use
either API to execute transactions and query for any data the API exposes. The dApp can then report the effects of the transaction to the wallet, and the wallet can then execute transactions without running into issues caused by a lagging indexer.
Managing Wallets
The Wallet standard includes features to help your apps interact with wallets.
Wallet Data
To query the installed wallets in a user's browser, use the get
function of getWallets
.
import { getWallets } from "@iota/wallet-standard";
const availableWallets = getWallets().get();
The return from this call (availableWallets
in the previous code) is an array of Wallet
types.
Use the Wallet.icon
and Wallet.name
attributes to display the wallet details on your web page.
The Wallet.accounts
is an array of WalletAccount
s. Each WalletAccount
type has address
and publicKey
properties, which are most useful during development. This data fills and caches after connection.
Features
Both the Wallet
type and the WalletAccount
type have a property called features
. The main wallet functionality is found here. The mandatory features that wallets must implement are listed in the previous code.
Many wallets choose to omit some non-mandatory features or add some custom features, so be sure to check the relevant wallet documentation if you intend to integrate a specific wallet.
Connecting a Wallet
Connecting in the context of a wallet refers to a user that joins the web site for the first time and has to choose the wallet and addresses to use.
The feature that provides this functionality is called standard:connect
. To connect using this feature, make the following call:
wallet.features['standard:connect'].connect() // connect call
This call results in the wallet opening a pop-up dialog for the user to continue the connection process.
Disconnecting a Wallet
Similar to the connecting feature, the Wallet standard also includes standard:disconnect
. The following example calls this feature:
wallet.features['standard:disconnect'].disconnect();
Transactions - Suggested Approach
Upon wallet connection, your app has the necessary information to execute transactions, such as address and method.
Construct the transaction separately with the @iota/iota
library and then sign it with the private key of the user. Use the iota:signTransaction
feature to achieve this:
wallet.features[iota:signTransaction].singTransaction(<Transaction>, <WalletAccount>);
Similar to connections, this process opens a pop-up dialog for the user to either accept or decline the transaction. Upon accepting, the function returns an object in the form {bytes: String, signature: Uint8Array}
. The bytes
value is the b64
encoding of the transaction and the signature
value is the transaction signature.
To execute the transaction, use IOTAClient
from @iota/iota
:
const client: IOTAClient
client.executeTransactionBlock({
transactionBlock: bytes,
signature: signature,
options: {}
})
Your app then sends the transaction effects back to the wallet, which reports results to the user. The wallet expects the effects to be b64
encoded.
wallet.features['iota:reportTransactionEffects'].reportTransactionEffects(
effects: Array.isArray(transactionResponse.effects) ? toB64(
Uint8Array.from(transactionResponse.effects) : transactionResponse.effects,
account: wallet.accounts[0], // for example
chain: wallet.chains[0]
)
Transactions - Abbreviated Approach
Many wallets abstract the above flow into one feature: iota:signAndExecuteTransaction
. The required arguments for this feature are the raw transaction and the options with the desired information to be included in the response:
showEffects
: Include the transaction effects.showEvents
: Include the transaction events.showObjectChanges
: Include all the objects that were deleted, created, or mutated.showBalanceChanges
: Include any coin transfer that took place.showInput
: Include the transaction's input.showRawInput
: Same asshowInput
but the format is raw.
Events Wallets Emit
The wallet emits events on certain user actions that apps can listen to. These events allow your app to be responsive to user actions on their wallets.
The wallet standard only defines the change event that can apply to chains, features, or accounts.
chains
: A change event on the chains means the user switched the wallet's active network, such as from Devnet to Testnet.features
: The user added or removed permission for your app to access certain wallet features.accounts
: The user added or removed an account (address) to interact with your app.
To subscribe your apps to events with the following call:
const unsubscribe = wallet.features['standard:events'].on ('change', callback);
This call returns a function that can be called to unsubscribe from listening to the events.
The callback is the handler that contains the logic to perform when the event fires. The input to the callback function is an object with the following type:
{
accounts: WalletAccount[],
chains: IdentifierArray,
features: IdentifierRecord<unknown>
}
These values are all arrays containing the new or changed items. Consequently, every event populates only one array in most cases, the rest are empty.
Implementation Example
IOTA offers a bare bones scaffold for React-based applications called @iota/dapp-kit
. See the dApp Kit documentation for more information.