WalletConnect Browser Client
Browser Client for WalletConnect
For more details, read the documentation
Install
yarn add @walletconnect/browser @walletconnect/qrcode-modal
npm install --save @walletconnect/browser @walletconnect/qrcode-modal
Initiate Connection
import WalletConnect from "@walletconnect/browser";
import WalletConnectQRCodeModal from "@walletconnect/qrcode-modal";
const connector = new WalletConnect({
bridge: "https://bridge.walletconnect.org"
});
if (!connector.connected) {
connector.createSession().then(() => {
const uri = connector.uri;
WalletConnectQRCodeModal.open(uri, () => {
console.log("QR Code Modal closed");
});
});
}
connector.on("connect", (error, payload) => {
if (error) {
throw error;
}
WalletConnectQRCodeModal.close();
const { accounts, chainId } = payload.params[0];
});
connector.on("session_update", (error, payload) => {
if (error) {
throw error;
}
const { accounts, chainId } = payload.params[0];
});
connector.on("disconnect", (error, payload) => {
if (error) {
throw error;
}
});
Send Transaction (eth_sendTransaction)
const tx = {
from: "0xbc28Ea04101F03aA7a94C1379bc3AB32E65e62d3",
to: "0x89D24A7b4cCB1b6fAA2625Fe562bDd9A23260359",
data: "0x",
gasPrice: "0x02540be400",
gasLimit: "0x9c40",
value: "0x00",
nonce: "0x0114"
};
connector
.sendTransaction(tx)
.then(result => {
console.log(result);
})
.catch(error => {
console.error(error);
});
Sign Transaction (eth_signTransaction)
const tx = {
from: "0xbc28Ea04101F03aA7a94C1379bc3AB32E65e62d3",
to: "0x89D24A7b4cCB1b6fAA2625Fe562bDd9A23260359",
data: "0x",
gasPrice: "0x02540be400",
gasLimit: "0x9c40",
value: "0x00",
nonce: "0x0114"
};
connector
.signTransaction(tx)
.then(result => {
console.log(result);
})
.catch(error => {
console.error(error);
});
Sign Personal Message (personal_sign)
const message = "My email is john@doe.com - 1537836206101"
const msgParams = [
convertUtf8ToHex(message)
"0xbc28ea04101f03ea7a94c1379bc3ab32e65e62d3",
];
connector
.signPersonalMessage(msgParams)
.then((result) => {
console.log(result)
})
.catch(error => {
console.error(error);
})
Sign Message (eth_sign)
const message = "My email is john@doe.com - 1537836206101";
const msgParams = [
"0xbc28ea04101f03ea7a94c1379bc3ab32e65e62d3",
keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))
];
connector
.signMessage(msgParams)
.then((result) => {
console.log(result)
})
.catch(error => {
console.error(error);
})
Sign Typed Data (eth_signTypedData)
const typedData = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" }
],
Person: [
{ name: "name", type: "string" },
{ name: "account", type: "address" }
],
Mail: [
{ name: "from", type: "Person" },
{ name: "to", type: "Person" },
{ name: "contents", type: "string" }
]
},
primaryType: "Mail",
domain: {
name: "Example Dapp",
version: "1.0.0-beta",
chainId: 1,
verifyingContract: "0x0000000000000000000000000000000000000000"
},
message: {
from: {
name: "Alice",
account: "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
},
to: {
name: "Bob",
account: "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
},
contents: "Hey, Bob!"
}
};
const msgParams = [
"0xbc28ea04101f03ea7a94c1379bc3ab32e65e62d3",
typedData
];
connector
.signTypedData(msgParams)
.then(result => {
console.log(result);
})
.catch(error => {
console.error(error);
});
Send Custom Request
const customRequest = {
id: 1337,
jsonrpc: "2.0",
method: "eth_signTransaction",
params: [
{
from: "0xbc28Ea04101F03aA7a94C1379bc3AB32E65e62d3",
to: "0x89D24A7b4cCB1b6fAA2625Fe562bDd9A23260359",
data: "0x",
gasPrice: "0x02540be400",
gasLimit: "0x9c40",
value: "0x00",
nonce: "0x0114"
}
]
};
connector
.sendCustomRequest(customRequest)
.then(result => {
console.log(result);
})
.catch(error => {
console.error(error);
});
Create Instant Request
import WalletConnect from "@walletconnect/browser";
import WalletConnectQRCodeModal from "@walletconnect/qrcode-modal";
const connector = new WalletConnect({
bridge: "https://bridge.walletconnect.org"
});
const instantRequest = {
id: 1,
jsonrpc: "2.0",
method: "eth_signTransaction",
params: [
{
from: "0xbc28ea04101f03ea7a94c1379bc3ab32e65e62d3",
to: "0x0000000000000000000000000000000000000000",
nonce: 1,
gas: 100000,
value: 0,
data: "0x0"
}
]
};
connector.on("display_uri", (error, payload) => {
if (error) {
throw error;
}
const uri = payload.params[0].uri;
WalletConnectQRCodeModal.open(uri, () => {
console.log("QR Code Modal closed");
});
});
connector
.createInstantRequest(instantRequest)
.then(result => {
console.log(result);
WalletConnectQRCodeModal.close();
})
.catch(error => {
console.error(error);
});