Quick start
Base on evm
Install Detect
Make sure user installed wallet, or guide to download page.
js
const provider = window.bitkeep && window.bitkeep.ethereum;
if (!provider) {
window.open('https://web3.bitget.com/en/wallet-download?type=2');
}
const provider = window.bitkeep && window.bitkeep.ethereum;
if (!provider) {
window.open('https://web3.bitget.com/en/wallet-download?type=2');
}
Connect Wallet
Get account after connected
js
provider.request({
method: "eth_requestAccounts"
}).then((accounts) => {
//success
const account = accounts[0];
}).catch((error) => {
//fail
});
provider.request({
method: "eth_requestAccounts"
}).then((accounts) => {
//success
const account = accounts[0];
}).catch((error) => {
//fail
});
Listen accountsChanged
js
provider.on('accountsChanged', (accounts) => {
//success
const account = accounts[0];
});
provider.on('accountsChanged', (accounts) => {
//success
const account = accounts[0];
});
Add netword
Use eth_chainId
get current netword ID
js
provider.request({
method: 'eth_chainId'
}).then((chainId)=>{
// success
}).catch((error) => {
// fail
});
provider.request({
method: 'eth_chainId'
}).then((chainId)=>{
// success
}).catch((error) => {
// fail
});
Listen chain switch
provider.on('chainChanged', handleChainChanged);
function handleChainChanged(chainId) {
//please reload
window.location.reload();
}
provider.on('chainChanged', handleChainChanged);
function handleChainChanged(chainId) {
//please reload
window.location.reload();
}
Add network if not exist
js
const config = {
name: 'bsctest',
chainId: '0x61',
chainName: 'Smart Chain - Testnet',
nativeCurrency: {
name: 'TBNB',
symbol: 'TBNB',
decimals: 18
},
rpcUrls: ['https://data-seed-prebsc-1-s1.binance.org:8545/'],
};
provider.request({
method: "wallet_addEthereumChain",
params: [config]
}).then((result) => {
// success
}).catch((error) => {
// error
});
const config = {
name: 'bsctest',
chainId: '0x61',
chainName: 'Smart Chain - Testnet',
nativeCurrency: {
name: 'TBNB',
symbol: 'TBNB',
decimals: 18
},
rpcUrls: ['https://data-seed-prebsc-1-s1.binance.org:8545/'],
};
provider.request({
method: "wallet_addEthereumChain",
params: [config]
}).then((result) => {
// success
}).catch((error) => {
// error
});
Switch Chain
js
provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x64' }]
}).then((res)=>{
// success
}).catch((error)=>{
//could add netword here
});
provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0x64' }]
}).then((res)=>{
// success
}).catch((error)=>{
//could add netword here
});
Listen chain switched
js
provider.on('chainChanged', (chainId) => {
});
provider.on('chainChanged', (chainId) => {
});
Sign
Sign structure data from EIP-712 https://eips.ethereum.org/EIPS/eip-712
js
provider.request({
"method": "eth_signTypedData_v4",
"params": [
"0x0000000000000000000000000000000000000000",
{
"types": {
"EIP712Domain": [
{
"name": "name",
"type": "string"
},
{
"name": "version",
"type": "string"
},
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "address"
}
],
"Person": [
{
"name": "name",
"type": "string"
},
{
"name": "wallet",
"type": "address"
}
],
"Mail": [
{
"name": "from",
"type": "Person"
},
{
"name": "to",
"type": "Person"
},
{
"name": "contents",
"type": "string"
}
]
},
"primaryType": "Mail",
"domain": {
"name": "Ether Mail",
"version": "1",
"chainId": 1,
"verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
},
"message": {
"from": {
"name": "Cow",
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
},
"to": {
"name": "Bob",
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
},
"contents": "Hello, Bob!"
}
}
]
}).then((result) => {
// success
}).catch((error) => {
// error
});
provider.request({
"method": "eth_signTypedData_v4",
"params": [
"0x0000000000000000000000000000000000000000",
{
"types": {
"EIP712Domain": [
{
"name": "name",
"type": "string"
},
{
"name": "version",
"type": "string"
},
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "address"
}
],
"Person": [
{
"name": "name",
"type": "string"
},
{
"name": "wallet",
"type": "address"
}
],
"Mail": [
{
"name": "from",
"type": "Person"
},
{
"name": "to",
"type": "Person"
},
{
"name": "contents",
"type": "string"
}
]
},
"primaryType": "Mail",
"domain": {
"name": "Ether Mail",
"version": "1",
"chainId": 1,
"verifyingContract": "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
},
"message": {
"from": {
"name": "Cow",
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
},
"to": {
"name": "Bob",
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
},
"contents": "Hello, Bob!"
}
}
]
}).then((result) => {
// success
}).catch((error) => {
// error
});