⚠️ This librairy is no longer maintained. Many of the features are no longer working due to changes in the Evmos protocol that were not mirrored in this repository.Also we encourage builders to interact with the protocol using EVM Extensions instead of building complex Cosmos transactions. EVM Extensions are basically precompiled stateful smart contracts. You can interact with them the same way you interact with EVM Solidity smart contracts.
For examples on how to integrate with Keplr, Metamask or how to process cosmos or evm transactions we invite you to checkout this repository as a reference: https://github.com/evmos/apps
evmosjs uses buf.build to manage Evmos Protobuf dependencies. To install evmosjs packages in your project, follow the instructions corresponding to your package manager.
Add the following line to an .npmrc
file in your project root:
@buf:registry=https://buf.build/gen/npm/v1
Then run:
npm install evmosjs
Or:
npm install @evmos/<package>
Add the following to an .yarnrc.yml
file in your project root:
npmScopes:
buf:
npmRegistryServer: "https://buf.build/gen/npm/v1"
Then run:
yarn add evmosjs
Or:
yarn add @evmos/<package>
Note that Yarn v1 is not supported (see explanation).
Query the account number, sequence, and pubkey for a given address.
import { generateEndpointAccount } from '@evmos/provider'
const address = 'evmos1...'
// Find node urls for either mainnet or testnet here:
// https://docs.evmos.org/develop/api/networks.
const nodeUrl = '...'
const queryEndpoint = `${nodeUrl}${generateEndpointAccount(address)}`
const restOptions = {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
}
// Note that the node will return a 400 status code if the account does not exist.
const rawResult = await fetch(
queryEndpoint,
restOptions,
)
const result = await rawResult.json()
// The response format is available at @evmos/provider/rest/account/AccountResponse.
// Note that the `pub_key` will be `null` if the address has not sent any transactions.
/*
account: {
'@type': string
base_account: {
address: string
pub_key?: {
'@type': string
key: string
}
account_number: string
sequence: string
}
code_hash: string
}
*/
Use Keplr or MetaMask to retrieve an account's public key
if it is not returned in the query response.
The public key is necessary in order to sign and broadcast
transactions, and it must be encoded as a compressed key in
base64
.
const cosmosChainID = 'evmos_9001-2' // Use 'evmos_9000-4' for testnet
const account = await window?.keplr?.getKey(cosmosChainID)
const pk = Buffer.from(account.pubKey).toString('base64')
Since MetaMask does not provide an interface to retrieve a user's public key, we must sign a message and recover the key from a signature.
import { hashMessage } from '@ethersproject/hash'
import {
computePublicKey,
recoverPublicKey,
} from '@ethersproject/signing-key'
const accounts = await window?.ethereum?.request({
method: 'eth_requestAccounts',
})
// Handle errors if MetaMask fails to return any accounts.
const message = 'Verify Public Key'
const signature = await window?.ethereum?.request({
method: 'personal_sign',
params: [message, accounts[0], ''],
})
// Compress the key, since the client expects
// public keys to be compressed.
const uncompressedPk = recoverPublicKey(
hashMessage(message),
signature,
)
const hexPk = computePublicKey(uncompressedPk, true)
const pk = Buffer.from(
hexPk.replace('0x', ''), 'hex',
).toString('base64')
Create a transaction payload which can be signed using either Metamask or Keplr.
This example uses MsgSend
. View all signable transaction payloads in the Transaction Docs.
import {
Chain,
Sender,
Fee,
TxContext,
MsgSendParams,
createTxMsgSend,
TxPayload,
} from '@evmos/transactions'
const chain: Chain = {
chainId: 9001,
cosmosChainId: 'evmos_9001-2',
}
// Populate the transaction sender parameters using the
// query API.
const sender: Sender = {
accountAddress: <sender_account_address>,
sequence: <sender_sequence>,
accountNumber: <sender_account_number>,
// Use the public key from the account query, or retrieve
// the public key from the code snippet above.
pubkey: <sender_pub_key>,
}
const fee: Fee = {
amount: '4000000000000000',
denom: 'aevmos',
gas: '200000',
}
const memo = ''
const context: TxContext = {
chain,
sender,
fee,
memo,
}
const params: MsgSendParams = {
destinationAddress: <destination_address>,
amount: <transaction_amount>,
denom: 'aevmos',
}
const tx: TxPayload = createTxMsgSend(context, params)
Evmos supports EIP-712 signatures for Cosmos payloads to be signed using Ethereum wallets such as MetaMask.
import { createTxRaw } from '@evmos/proto'
import { evmosToEth } from '@evmos/address-converter'
// First, populate a TxContext object and create a signable Tx payload.
// (See 'Create a Signable Transaction' to learn how to create these).
const context = ...
const tx = ...
const { sender } = context
// Initialize MetaMask and sign the EIP-712 payload.
await window.ethereum.enable()
const senderHexAddress = evmosToEth(sender.accountAddress)
const eip712Payload = JSON.stringify(tx.eipToSign)
const signature = await window.ethereum.request({
method: 'eth_signTypedData_v4',
params: [senderHexAddress, eip712Payload],
})
// Create a signed Tx payload that can be broadcast to a node.
const signatureBytes = Buffer.from(signature.replace('0x', ''), 'hex')
const { signDirect } = tx
const bodyBytes = signDirect.body.toBinary()
const authInfoBytes = signDirect.authInfo.toBinary()
const signedTx = createTxRaw(
bodyBytes,
authInfoBytes,
[signatureBytes],
)
EvmosJS supports Cosmos SDK SignDirect
payloads that can be signed using Keplr.
import { createTxRaw } from '@evmos/proto'
// First, populate a TxContext object and create a signable Tx payload.
// (See 'Create a Signable Transaction' to learn how to create these).
const context = ...
const tx = ...
const { chain, sender } = context
const { signDirect } = tx
const signResponse = await window?.keplr?.signDirect(
chain.cosmosChainId,
sender.accountAddress,
{
bodyBytes: signDirect.body.toBinary(),
authInfoBytes: signDirect.authInfo.toBinary(),
chainId: chain.cosmosChainId,
accountNumber: new Long(sender.accountNumber),
},
)
if (!signResponse) {
// Handle signature failure here.
}
const signatures = [
new Uint8Array(Buffer.from(signResponse.signature.signature, 'base64')),
]
const { signed } = signResponse
const signedTx = createTxRaw(
signed.bodyBytes,
signed.authInfoBytes,
signatures,
)
EvmosJS also supports signing EIP-712 payloads using Keplr. This is necessary for Ledger users on Keplr, since the Ledger device cannot sign SignDirect
payloads.
import { EthSignType } from '@keplr-wallet/types';
import { createTxRaw } from '@evmos/proto'
// First, populate a TxContext object and create a signable Tx payload.
// (See 'Create a Signable Transaction' to learn how to create these).
const context = ...
const tx = ...
const { chain, sender } = context
const eip712Payload = JSON.stringify(tx.eipToSign)
const signature = await window?.keplr?.signEthereum(
chain.cosmosChainId,
sender.accountAddress,
eip712Payload,
EthSignType.EIP712,
)
if (!signature) {
// Handle signature failure here.
}
const { signDirect } = tx
const bodyBytes = signDirect.body.toBinary()
const authInfoBytes = signDirect.authInfo.toBinary()
const signedTx = createTxRaw(
bodyBytes,
authInfoBytes,
[signature],
)
Regardless of how the transaction is signed, broadcasting takes place the same way.
import {
generateEndpointBroadcast,
generatePostBodyBroadcast,
} from '@evmos/provider'
// First, sign a transaction using MetaMask or Keplr.
const signedTx = createTxRaw(...)
// Find a node URL from a network endpoint:
// https://docs.evmos.org/develop/api/networks.
const nodeUrl = ...
const postOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: generatePostBodyBroadcast(signedTx),
}
const broadcastEndpoint = `${nodeUrl}${generateEndpointBroadcast()}`
const broadcastPost = await fetch(
broadcastEndpoint,
postOptions,
)
const response = await broadcastPost.json()