Klaytn Provider

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']
}

Properties

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.

klaytn.networkVersion

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.

klaytn.selectedAddress

Returns a hex-prefixed string representing the current user's selected address, ex: "0xfdea65c8e26263f6d9a1b5de9555d2931a33b825".

klaytn.isKaikas

Returns true or false, depending on whether the user has Kaikas installed.

Methods

klaytn.enable()

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 login
console.error(error)
}

klaytn.sendAsync(options, callback)

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

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 token
image: tokenImage // A string url of the token logo
}
},
id: Math.round(Math.random() * 100000)
},
(err, result) => console.log(err, result)
)

klaytn.autoRefreshOnNetworkChange

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)

klaytn.on(eventName, callback)

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
})

klaytn._kaikas

For developers' convenience, Kaikas provides 3 useful methods under _kaikas namespace.

klaytn._kaikas.isEnabled()

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.

klaytn._kaikas.isApproved()

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

klaytn._kaikas.isUnlocked()

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.