Integrate Embedded Wallets with the Tezos Blockchain
While using the Embedded Wallets Web SDK (formerly Web3Auth) for a non-EVM chain like Tezos, you can get the user's private key from the provider. Using this private key, you can use the corresponding libraries of the blockchain to make blockchain calls like getting the user's account
, fetching balance
, sign transaction
, send transaction
, read
from and write
to the smart contract, etc. We have highlighted a few methods here to get you started quickly.
The SDKs are now branded as MetaMask Embedded Wallet SDKs (formerly Web3Auth Plug and Play SDKs). Package names and APIs remain Web3Auth (for example, Web3Auth React SDK), and code snippets may reference web3auth
identifiers.
Installation
- npm
- Yarn
- pnpm
- Bun
npm install --save @taquito/signer @taquito/taquito @taquito/utils @tezos-core-tools/crypto-utils
yarn add @taquito/signer @taquito/taquito @taquito/utils @tezos-core-tools/crypto-utils
pnpm add @taquito/signer @taquito/taquito @taquito/utils @tezos-core-tools/crypto-utils
bun add @taquito/signer @taquito/taquito @taquito/utils @tezos-core-tools/crypto-utils
Initializing Provider
Getting the chainConfig
- Mainnet
- Testnet
- Chain Namespace: other
- Chain ID: 0x1
- Public RPC URL: https://rpc.tzbeta.net/ (Avoid using public rpcTarget in production)
- Display Name: Tezos Mainnet
- Block Explorer Link: https://tzstats.com
- Ticker: XTZ
- Ticker Name: Tezos
- Chain Namespace: other
- Chain ID: 0x5
- Public RPC URL: https://rpc.tzbeta.net/ (Avoid using public rpcTarget in production)
- Display Name: Tezos Testnet
- Block Explorer Link: https://tzstats.com
- Ticker: XTZ
- Ticker Name: Tezos
Get Account, Balance and Key
Once a user logs in, the Embedded Wallets SDK returns a provider. Since there isn't a native provider for Tezos, we directly use the private key to make RPC calls.
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 Tezos EC Curve specific signing functions, hence, we first derive the Tezos Key using the getTezosKeyPair()
function.
On the underhood, it uses the tezosCrypto.utils.seedToKeyPair()
function, where we need to pass the hex2buf(privateKey)
, ie. the hexadecimal to byteArray converted private key. We can use the returned private key pair from this and use on Tezos transactions.
import * as tezosCrypto from '@tezos-core-tools/crypto-utils'
import { IProvider } from '@web3auth/base'
import { TezosToolkit } from '@taquito/taquito'
import { hex2buf } from '@taquito/utils'
import { InMemorySigner } from '@taquito/signer'
const tezos = new TezosToolkit('https://ithacanet.ecadinfra.com')
/*
Use code from the above Initializing Provider here
*/
// web3authProvider is web3auth.provider from above
const privateKey = await web3authProvider.request({ method: 'private_key' })
// derive the Tezos Key Pair from the private key
const keyPair = tezosCrypto.utils.seedToKeyPair(hex2buf(privateKey))
// keyPair.pkh is the account address.
const account = keyPair?.pkh
// get balance of the account
const balance = await tezos.tz.getBalance(account)
Send Transaction
import * as tezosCrypto from '@tezos-core-tools/crypto-utils'
import { IProvider } from '@web3auth/base'
import { TezosToolkit } from '@taquito/taquito'
import { hex2buf } from '@taquito/utils'
import { InMemorySigner } from '@taquito/signer'
const tezos = new TezosToolkit('https://ithacanet.ecadinfra.com')
/*
Use code from the above Initializing Provider here
*/
// web3authProvider is web3auth.provider from above
const privateKey = await web3authProvider.request({ method: 'private_key' })
// derive the Tezos Key Pair from the private key
const keyPair = tezosCrypto.utils.seedToKeyPair(hex2buf(privateKey))
// keyPair.pkh is the account address.
const account = keyPair?.pkh
// use TacoInfra's RemoteSigner for better security on mainnet..
tezos.setSignerProvider(await InMemorySigner.fromSecretKey(keyPair?.sk))
// example address.
const address = 'tz1dHzQTA4PGBk2igZ3kBrDsVXuvHdN8kvTQ'
// NOTE: The account which is used to send tezos should have some balance for this transaction to go through.
// If there is no balance, then you will receive an error - "implicit.empty_implicit_contract"
// To solve this error, use a faucet account to send some tzs to the account.
// Alternate solution:
// 1. Use this link: https://tezostaquito.io/docs/making_transfers#transfer-from-an-implicit-tz1-address-to-a-tz1-address
// 2. Modify the address and use the pkh key extracted from web3auth seed in the live code editor and click run code.
// 3. Check balance in the account and have some fun.
const op = await tezos.wallet
.transfer({
to: address,
amount: 0.00005,
})
.send()
const txRes = await op.confirmation()
Sign Message
import * as tezosCrypto from '@tezos-core-tools/crypto-utils'
import { IProvider } from '@web3auth/base'
import { TezosToolkit } from '@taquito/taquito'
import { hex2buf } from '@taquito/utils'
import { InMemorySigner } from '@taquito/signer'
const tezos = new TezosToolkit('https://ithacanet.ecadinfra.com')
// List of available RPC Nodes -- https://tezostaquito.io/docs/rpc_nodes
/*
Use code from the above Initializing Provider here
*/
// web3authProvider is web3auth.provider from above
const privateKey = await web3authProvider.request({ method: 'private_key' })
// derive the Tezos Key Pair from the private key
const keyPair = tezosCrypto.utils.seedToKeyPair(hex2buf(privateKey))
const signer = new InMemorySigner(keyPair.sk)
const message = '0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad'
const signature = await signer.sign(message)