Integrate Web3Auth with the Near Protocol
While using the Web3Auth Web SDK for a non-EVM chain like NEAR, you get a
standard provider from which you can get the user's private key. Using this private key, you can use
the corresponding libraries of the blockchain to make blockchain calls like getting a user's
account
, fetching balance
, performing send transaction
, etc. To help you get started, we've
outlined some methods for you to use.
Installation
- npm
- Yarn
- pnpm
- Bun
npm install --save near-api-js@4.0.4
yarn add near-api-js@4.0.4
pnpm add near-api-js@4.0.4
bun add near-api-js@4.0.4
Initializing Provider
Getting the chainConfig
- Mainnet
- Testnet
- Chain Namespace: other
- Chain ID: 0x4e454152
- Public RPC URL: https://mainnet.aurora.dev (Avoid using public rpcTarget in production)
- Display Name: Near
- Block Explorer Link: https://aurorascan.dev
- Ticker: NEAR
- Ticker Name: NEAR
- Chain Namespace: other
- Chain ID: 0x4e454153
- Public RPC URL: https://testnet.aurora.dev (Avoid using public rpcTarget in production)
- Display Name: Near
- Block Explorer Link: https://explorer.testnet.aurora.dev
- Ticker: NEAR
- Ticker Name: NEAR
Get Key Pair and Account
After a user logs in, they receive a provider from the Web3Auth SDK. However, there is no native provider for Near in Web3Auth, so we must use the private key to make RPC calls directly.
To access the user's private key, the application can use the function
web3auth.provider.request({method: "private_key"})
from the Web3Auth provider. Yet, this key
cannot be used with Near since it requires the ed25519
key. Therefore, we must use the
getED25519Key()
function from @web3auth/base-provider
to convert the secp256k1
key to an
ed25519
key.
import { connect, KeyPair, keyStores, utils } from "near-api-js";
import { getED25519Key } from "@web3auth/auth-adapter";
/*
Use code from the above Initializing Provider here
*/
// web3authProvider is web3auth.provider from above
const privateKey = await web3authProvider.request({ method: "private_key" });
// Convert the secp256k1 key to ed25519 key
// When starting your application with "solana" namespace, you can skip the below two lines
// and pass the privateKey directly to buffer.
const privateKeyEd25519 = getED25519Key(privateKey).sk.toString("hex");
// Convert the private key to Buffer
const privateKeyEd25519Buffer = Buffer.from(privateKeyEd25519, "hex");
// Convert the private key to base58
const bs58encode = utils.serialize.base_encode(privateKeyEd25519Buffer);
// Convert the base58 private key to KeyPair
const keyPair = KeyPair.fromString(`ed25519:${bs58encode}`);
// publicAddress
const publicAddress = keyPair?.getPublicKey().data || [];
// accountId is the account address on Near which is where funds will be sent to.
const accountId = Buffer.from(pk58 || []).toString("hex");
Get Balance
import { connect, keyStores, utils } from "near-api-js";
/*
Use code from the above Initializing Provider here
*/
const myKeyStore = new keyStores.InMemoryKeyStore();
await myKeyStore.setKey("testnet", accountId, keyPair); // accountId and keyPair from above
const connectionConfig = {
networkId: "testnet",
keyStore: myKeyStore,
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://wallet.testnet.near.org",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://explorer.testnet.near.org",
};
const nearConnection = await connect(connectionConfig);
const account = await nearConnection.account(accountId);
const accountBalance = await account.getAccountBalance();
const availableBalance = utils.format.formatNearAmount(accountBalance.available);
Send Transaction
import { connect, keyStores, utils } from "near-api-js";
/*
Use code from the above Initializing Provider here
*/
const receiver = "shahbaz17.testnet";
const amount = "2"; // in NEAR
const myKeyStore = new keyStores.InMemoryKeyStore();
await myKeyStore.setKey("testnet", accountId, keyPair); // accountId and keyPair from above
const connectionConfig = {
networkId: "testnet",
keyStore: myKeyStore,
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://wallet.testnet.near.org",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://explorer.testnet.near.org",
};
const nearConnection = await connect(connectionConfig);
const senderAccount = await nearConnection.account(accountId);
const result = await senderAccount.sendMoney(receiver, utils.format.parseNearAmount(amount));