Web3.js
If you're using web3.js
, you'll need to install @cyberlab/cyberwallet-sdk
to connect to CyberWallet.
Installation
npm install @cyberlab/cyberwallet-sdk
Usage
1. Setup the SDK with your dapp information
import { CyberSdk } from "@cyberlab/cyberwallet-sdk";
const sdk = new CyberSdk(
{
dappName: "<Your Dapp Name>",
dappHost: "<Your Dapp Host>",
projectId: "<Your Project ID>",
customHost: "https://stg.passkey.cyber.co", // optional
chainId: 111557560, // optional
},
);
dappName
: Your Dapp namedappHost
: Your Dapp host URLprojectId
: The project ID is used to identify your Dapp.customHost
: The custom host URL is optional. If you don't provide it, the SDK will use the default CyberWallet host URL (https://passkey.cyber.co
). If you want to use the CyberWallet Testnet environment, you can set the custom host URL tohttps://stg.passkey.cyber.co
.chainId
: The chain ID is optional. If you don't provide it, the SDK will use the default Cyber chain ID (7560
). If you want to use the Cyber Testnet, you can set the chain ID to111557560
.
2. Set the CyberWallet provider to web3.js
const cyberProvider = sdk.getCyberProvider();
const web3 = new Web3(cyberProvider);
3. Connect to CyberWallet
// connect
const connect = async () => {
return await web3.provider?.connect();
};
// get connected Accounts
const accounts = await web3.eth.getAccounts();
It will open the CyberWallet connect page and ask the user to confirm using Passkeys. After the user confirms, the accounts
variable will contain the user's account address.
4. Sign a message
// personal_sign
const sign = async () => {
const account = accounts[0];
const message = "Hello, world!";
const signature = await web3.eth.personal.sign(message, account, ""); // we don't need passphrase
return signature;
}
It will open the CyberWallet sign message page and ask the user to sign using the same Passkey. After the user confirms, CyberWallet will return the signature.
5. Send a transaction
// sendTx
const sendTx = async () => {
const account = accounts[0];
const res = await web3.eth.sendTransaction({
from: account,
to: "0x1126DC7AE830415C7D6CbB2F09b1266823f490c4",
value: "100000000000000",
});
console.log('res', res)
return;
};
It will open the CyberWallet send transaction page and ask the user to confirm using the same Passkey. After the user confirms, CyberWallet will return the transaction result.
You can view the transaction details on Cyber Blockchain Explorer: mainnet, testnet.