Establishing a Connection
Once an application has detected the provider, it can then request to connect to OneKey. This connection request will prompt the user for permission to share their public key, indicating that they are willing to interact further. Users must approve a connection request before the app can make additional requests such as signing a message or sending a transaction.
Once permission is established for the first time, the web application's domain will be whitelisted for future connection requests. After a connection is established, it is possible to terminate the connection from both the application and the user side.
Connecting
The recommended and easiest way to connect to OneKey is by calling window.$onekey.solana.connect()
. However, the provider also exposes a request
JSON RPC interface.
The connect()
call will return a Promise that resolves when the user accepts the connection request, and reject (throw when awaited) when the user declines the request or closes the pop-up. See Errors for a breakdown of error messages OneKey may emit.
When the user accepts the request to connect, the provider will also emit a connect
event.
Once the web application is connected to OneKey, it will be able to read the connected account's public key and prompt the user for additional transactions. It also exposes a convenience isConnected
boolean.
Eagerly Connecting
After a web application connects to OneKey for the first time, it becomes trusted. Once trusted, it's possible for the application to automatically connect to OneKey on subsequent visits or page refreshes, without prompting the user for permission. This is referred to as "eagerly connecting".
To implement this, applications should pass an onlyIfTrusted
option into the connect()
call.
If this flag is present, OneKey will only eagerly connect and emit a connect
event if the application is trusted. If the application is not trusted, OneKey will throw a 4001
error and remain disconnected until the user is prompted to connect without an onlyIfTrusted
flag. In either case, OneKey will not open a pop-up window, making this convenient to use on all page loads.
The following is an example of how a React application can eagerly connect to OneKey.
For a live demo, please refer to the handleConnect
portion of our sandbox.
If a wallet disconnects from a trusted app and then attempts to reconnect at a later time, OneKey will still eagerly connect. Once an app is trusted, OneKey will only require the user to approve a connection request if the user revokes the app from within their Trusted Apps settings.
Disconnecting
Disconnecting mirrors the same process as connecting. However, it is also possible for the wallet to initiate the disconnection, rather than the application itself.
The following is an example of how a React application can gracefully handle a disconnect
event.
Changing Accounts
OneKey allows users to seamlessly manage multiple accounts (i.e. Keypairs) from within a single extension or mobile app. Whenever a user switches accounts, OneKey will emit an accountChanged
event.
If a user changes accounts while already connected to an application, and the new account had already whitelisted that application, then the user will stay connected and OneKey will pass the PublicKey of the new account:
If OneKey does not pass the public key of the new account, an application can either do nothing or attempt to reconnect:
Last updated