Skip to content

Ethers.js

If you're using ethers.js in your project, you can install @cyberlab/cyberwallet-sdk to integrate with 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>",
    projectId: "<Your Project ID>",
    customHost: "https://stg.passkey.cyber.co", // optional
    chainId: 111557560, // optional
  },
);
  • dappName: Your Dapp name
  • projectId: 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 to https://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 to 111557560.

2. Set the CyberWallet provider for Ethers

import { ethers } from 'ethers';
 
const provider = new ethers.BrowserProvider(sdk.getCyberProvider());
 
// get signer
const signer = await provider.getSigner();

3. Connect to CyberWallet

// connect
const connect = async () => {
  return await provider.send("eth_requestAccounts", []);
};
 

It will open the CyberWallet connect page and ask the user to confirm using Passkeys.

4. Sign a message

const sig = await signer.signMessage('Hello World');

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 res = await signer.sendTransaction({
      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.