Integrate Web3Auth with the StarkNet Blockchain
While using the Web3Auth Web SDK for a non EVM chain like StarkNet you get a standard provider from which you can
get the private key of the user. Using this private key, you can use the corresponding libraries of the blockchain to make blockchain calls like
getting user's account
, fetch balance
, sign transaction
, send transaction
, read
from and write
to the smart contract, etc. We have
highlighted a few methods here for getting you started quickly on that.
This reference is for the Web
, however, you can use StarkNet on other Mobile and Gaming Platforms as well. Please follow our reference for
EVM Blockchains, and similarly use StarkNet libraries that support the platforms to use the private key and make
blockchain calls accordingly.
Installation
- npm
- Yarn
- pnpm
npm install --save web3 starknet elliptic bn.js @starkware-industries/starkware-crypto-utils
yarn add web3 starknet elliptic bn.js @starkware-industries/starkware-crypto-utils
pnpm add web3 starknet elliptic bn.js @starkware-industries/starkware-crypto-utils
Initializing Provider
Getting the chainConfig
- Mainnet
- Testnet
const chainConfig = {
chainNamespace: "other",
chainId: "0x1", //
rpcTarget: "https://rpc.ankr.com/eth",
// Avoid using public rpcTarget in production.
displayName: "StarkNet Mainnet",
blockExplorerUrl: "https://explorer.immutable.com",
ticker: "STRK",
tickerName: "StarkNet",
};
const chainConfig = {
chainNamespace: "other",
chainId: "0xaa36a7", //
rpcTarget: "https://rpc.ankr.com/eth_sepolia",
// Avoid using public rpcTarget in production.
displayName: "StarkNet Testnet",
blockExplorerUrl: "https://sepolia.etherscan.io",
ticker: "STRK",
tickerName: "StarkNet",
};
Initializing and instantiating the Web3Auth SDK
- PnP Modal SDK
- PnP NoModal SDK
- CoreKit SFA Web SDK
- CoreKit MFA tKey JS SDK
import { Web3Auth } from "@web3auth/modal";
import { CommonPrivateKeyProvider } from "@web3auth/base-provider";
import { WEB3AUTH_NETWORK } from "@web3auth/base";
const privateKeyProvider = new CommonPrivateKeyProvider({
config: { chainConfig: chainConfig }
});
const web3auth = new Web3Auth({
// Get it from Web3Auth Dashboard
clientId,
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
privateKeyProvider: privateKeyProvider,
});
import { Web3AuthNoModal } from "@web3auth/no-modal";
import { OpenloginAdapter } from "@web3auth/openlogin-adapter";
import { CommonPrivateKeyProvider } from "@web3auth/base-provider";
import { WEB3AUTH_NETWORK } from "@web3auth/base";
const privateKeyProvider = new CommonPrivateKeyProvider({
config: {chainConfig},
});
const web3auth = new Web3AuthNoModal({
clientId, // Get it from Web3Auth Dashboard
web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
});
const openloginAdapter = new OpenloginAdapter();
web3auth.configureAdapter(openloginAdapter);
import { Web3Auth } from "@web3auth/single-factor-auth";
import { CommonPrivateKeyProvider } from "@web3auth/base-provider";
const web3auth = new Web3Auth({
clientId, // Get your Client ID from Web3Auth Dashboard
web3AuthNetwork: "sapphire_mainnet",
});
const privateKeyProvider = new CommonPrivateKeyProvider({
config: { chainConfig },
});
import { SfaServiceProvider } from "@tkey/service-provider-sfa";
import { CommonPrivateKeyProvider } from "@web3auth/base-provider";
// Initialization of private key provider
const privateKeyProvider = new CommonPrivateKeyProvider({
config: {
chainConfig,
},
});
await (tKey.serviceProvider as SfaServiceProvider).init(privateKeyProvider);
Get User Info
After logging in, the Web3Auth instance will provide you with information regarding the user that is logged in. This information is obtained directly from the JWT token and is not stored by Web3Auth. Therefore, this information can only be accessed through social logins after the user has logged into your application.
const user = await web3auth.getUserInfo(); // web3auth instance
Get Account and Key
Once a user logs in, the Web3Auth SDK returns a provider. Even though StarkNet is a layer 2 on Ethereum, it is not EVM compatible. Hence to use it,
one cannot use the native EIP1193
provider that Web3Auth gives for EVM blockchains. Hence we need to
directly use the private key to make the calls here.
Using the function, web3auth.provider.request({method: "private_key"})
from Web3Auth provider, the application can have access to the user's private
key. However, we cannot use this key with StarkNet EC Curve specific signing functions, hence, we first derive the Stark Key using the getStarkKey()
function.
On the underhood, it uses the starkwareCrypto.ec.keyFromPrivate()
function, where we pass our Private key with the hex
argument since it is hex
encoded. Further, we get the public key from this using the starkwareCrypto.ec.keyFromPublic()
function, and we take its X-coordinate using
.pub.getX()
which is our starkKey
. We use this starkKey
on StarkNet transactions.
For the deployAccount()
, we use the file ArgentAccount.json
which gives us the ABI of the compiled contract. We use that contract in Starknet's
defaultProvider.deployContract()
function to deploy the contract.
Get ArgentAccount.json from this gist.
import starkwareCrypto from "@starkware-industries/starkware-crypto-utils";
import { ec as elliptic } from "elliptic";
/*
Use code from the above Initializing Provider here
*/
// web3authProvider is web3auth.provider from above
const privateKey = await web3authProvider.request({ method: "private_key" });
const keyPair = starkwareCrypto.ec.keyFromPrivate(privateKey, "hex");
// get the account address from the public key
const account = starkwareCrypto.ec.keyFromPublic(keyPair.getPublic(true, "hex"), "hex");
// Use this key for the StarkNet transactions
const startKey = account.pub.getX().toString("hex");
Deploy Account
import { AddTransactionResponse, defaultProvider } from "starknet";
import starkwareCrypto from "@starkware-industries/starkware-crypto-utils";
import { ec as elliptic } from "elliptic";
import CompiledAccountContractAbi from "./ArgentAccount.json";
/*
Use code from the above Initializing Provider here
*/
// web3authProvider is web3auth.provider from above
const privateKey = await web3authProvider.request({ method: "private_key" });
const keyPair = starkwareCrypto.ec.keyFromPrivate(privateKey, "hex");
// get the account address from the public key
const account = starkwareCrypto.ec.keyFromPublic(keyPair.getPublic(true, "hex"), "hex");
const contract = JSON.parse(JSON.stringify(CompiledAccountContractAbi));
const response = await defaultProvider.deployContract({ contract });