Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: install evmosjs, create signEthereum path #4257

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/common-common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export enum ChainNetwork {
SPL = 'spl', // solana token
AxieInfinity = 'axie-infinity',
Evmos = 'evmos',
EvmosDevnet = 'evmos-devnet',
Kava = 'kava',
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CosmosEvmWebWalletController implements IWebWallet<string> {
public readonly label = 'Metamask';
public readonly chain = ChainBase.CosmosSDK;
public readonly defaultNetwork = ChainNetwork.Injective;
public readonly specificChains = ['injective', 'evmos'];
public readonly specificChains = ['injective', 'evmos', 'evmos-devnet'];

public get available() {
return !!window.ethereum;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class EVMKeplrWebWalletController implements IWebWallet<AccountData> {
public readonly label = 'Keplr';
public readonly chain = ChainBase.CosmosSDK;
public readonly defaultNetwork = ChainNetwork.Evmos;
public readonly specificChains = ['evmos', 'injective'];
public readonly specificChains = ['evmos', 'injective', 'evmos-devnet'];

public get available() {
return !!window.keplr;
Expand Down Expand Up @@ -133,24 +133,24 @@ class EVMKeplrWebWalletController implements IWebWallet<AccountData> {
currencies: [
{
coinDenom: app.chain.meta.default_symbol,
coinMinimalDenom: `u${app.chain.meta.default_symbol.toLowerCase()}`,
coinMinimalDenom: `a${app.chain.meta.default_symbol.toLowerCase()}`,
coinDecimals: app.chain.meta.decimals || 6,
},
],
feeCurrencies: [
{
coinDenom: app.chain.meta.default_symbol,
coinMinimalDenom: `u${app.chain.meta.default_symbol.toLowerCase()}`,
coinMinimalDenom: `a${app.chain.meta.default_symbol.toLowerCase()}`,
coinDecimals: app.chain.meta.decimals || 6,
},
],
stakeCurrency: {
coinDenom: app.chain.meta.default_symbol,
coinMinimalDenom: `u${app.chain.meta.default_symbol.toLowerCase()}`,
coinMinimalDenom: `a${app.chain.meta.default_symbol.toLowerCase()}`,
coinDecimals: app.chain.meta.decimals || 6,
},
gasPriceStep: { low: 0, average: 0.025, high: 0.03 },
features: ['stargate'],
features: ['stargate', 'eth-address-gen', 'eth-key-sign'],
};
await window.keplr.experimentalSuggestChain(info);
await window.keplr.enable(this._chainId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
import type { EncodeObject } from '@cosmjs/proto-signing';

import { EthSignType } from '@keplr-wallet/types';
import { createTxRaw } from '@evmos/proto';
import {
generateEndpointBroadcast,
generatePostBodyBroadcast,
} from '@evmos/provider';

import {
Chain,
Sender,
Fee,
TxContext,
MsgSendParams,
MsgSubmitProposalParams,
createTxMsgSend,
createTxMsgVote,
createTxMsgSubmitProposal,
TxPayload,
} from '@evmos/transactions';

import type {
BankExtension,
GovExtension,
Expand Down Expand Up @@ -188,6 +208,71 @@ class CosmosChain implements IChainModule<CosmosToken, CosmosAccount> {
}
}

public async sendTxEthereum(
account: CosmosAccount,
tx: TxPayload
): Promise<any]> {
// ): Promise<readonly Event[]> {
const wallet = WebWalletController.Instance.getByName(
WalletId.Keplr
) as KeplrWebWalletController;
if (!wallet) throw new Error('Keplr wallet not found');
if (!wallet.enabled) {
await wallet.enable();
}
try {
// First, populate a TxContext object and create a signable Tx payload.
// (See 'Create a Signable Transaction' to learn how to create these).

const chain = this.app.chain.id; //TODO
const sender = account.address;

const eip712Payload = JSON.stringify(tx.eipToSign);
const signature = await window?.keplr?.signEthereum(
chain,
sender,
eip712Payload,
EthSignType.TRANSACTION
);

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]);

// broadcast

// First, sign a transaction using MetaMask or Keplr.

// Find a node URL from a network endpoint:
// https://docs.evmos.org/develop/api/networks.
const nodeUrl = this.app.chain.meta.node.url;

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();

console.log('response', response);

return response;
} catch (err) {
console.log(err.message);
throw err;
}
}

public createTXModalData(
author: CosmosAccount,
txFunc,
Expand Down
Loading