Kaikas injects a global API into websites visited by its users at window.klaytn
. This API allows websites to request user login, load data from blockchains, and create transactions. You can use this API to detect whether the user uses Kaikas.
if (typeof window.klaytn !== 'undefined') {// Kaikas user detected. You can now use the provider.const provider = window['klaytn']}
These properties can be used to check the current state of the connected user, which can be important things to verify before sending a transaction.
Returns a numeric string representing the current blockchain's network ID.
'1001': Baobab Test Network'8217': Cypress Main Network
Baobab
and Cypress
are names for Klaytn test network and main network respectively.
Returns a hex-prefixed string representing the current user's selected address, ex: "0xfdea65c8e26263f6d9a1b5de9555d2931a33b825"
.
Returns true
or false
, depending on whether the user has Kaikas installed.
Send connection request to Kaikas user. Returns a promise of an array of hex-prefixed Klaytn address strings. If the user accepts the request, BApp is granted access to user's account information.
try {const accounts = await klaytn.enable()// You now have an array of accounts!// Currently only one:// ['0xFDEa65C8e26263F6d9A1B5de9555D2931A33b825']} catch (error) {// Handle error. Likely the user rejected the loginconsole.error(error)}
Sends a message to the Kaikas browser. Message format maps to the format of Klaytn JSON-RPC API.
Here's an example of klay_sendTransaction
const transactionParameters = {gas: '0x2710',to: '0x0000000000000000000000000000000000000000',from: klaytn.selectedAddress,value: '0xff'}​klaytn.sendAsync({method: 'klay_sendTransaction',params: [transactionParameters],from: klaytn.selectedAddress},callback)
wallet_watchAsset
is a special method for registering tokens. For detailed information, please refer to registering your token​
klaytn.sendAsync({method: 'wallet_watchAsset',params: {type: 'ERC20', // Initially only supports ERC20, but eventually more!options: {address: tokenAddress, // The address that the token is at.symbol: tokenSymbol, // A ticker symbol or shorthand, up to 5 chars.decimals: tokenDecimals, // The number of decimals in the tokenimage: tokenImage // A string url of the token logo}},id: Math.round(Math.random() * 100000)},(err, result) => console.log(err, result))
When the network is changed, Kaikas will reload any pages that have made requests to the provider.
To disable auto-refresh on a network change you can do:
klaytn.autoRefreshOnNetworkChange = false
This can be toggled on or off at any time. (Default value is true
)
The provider supports listening for some events:
accountsChanged
, returns updated account array.
networkChanged
, returns network ID string.
klaytn.on('accountsChanged', function(accounts) {// Time to reload your interface with accounts[0]!})​klaytn.on('networkChanged', function() {// `networkChanged` event is only useful when auto-refresh on network is disabled// Otherwise, Kaikas will auto-reload pages upon network change})
For developers' convenience, Kaikas provides 3 useful methods under _kaikas
namespace.
This method returns a Boolean
indicating if the current domain has access to user accounts. This is useful for determining whether the user has approved account access for the current session.
This method returns a Promise
that resolves to a Boolean
indicating if the current domain has a cached approval. This is useful for determining if an approval popup will show when klaytn.enable()
is called, since it indicates if a past approval exists
This method returns a Promise
that resolves to a Boolean
indicating whether Kaikas is unlocked. Note that this does not indicate whether the user has approved account exposure.