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

Fix/keplr evm #488

Open
wants to merge 7 commits into
base: staging
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@cosmos-kit/leap": "^0.15.12",
"@improbable-eng/grpc-web": "^0.15.0",
"@improbable-eng/grpc-web-node-http-transport": "^0.15.0",
"@keplr-wallet/types": "^0.11.64",
"@keplr-wallet/types": "^0.12.107",
"@ledgerhq/hw-transport-webhid": "^6.20.0",
"@ledgerhq/hw-transport-webusb": "^6.20.0",
"@metamask/detect-provider": "^2.0.0",
Expand Down Expand Up @@ -89,4 +89,4 @@
"eslint": "^8.42.0",
"prettier": "2.5.1"
}
}
}
8 changes: 1 addition & 7 deletions src/constant/ibc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ export const EmbedChainInfosInit: SimpleMap<ChainInfoExplorerTmRpc> = {
coinGeckoId: "cosmos",
},
],
coinType: 118,
features: ["ibc-transfer", "ibc-go"],
explorerUrlToTx: "https://www.mintscan.io/cosmos/txs/{txHash}",
tmRpc: "https://rpc.cosmos.network/",
Expand Down Expand Up @@ -301,7 +300,6 @@ export const EmbedChainInfosInit: SimpleMap<ChainInfoExplorerTmRpc> = {
coinGeckoId: "secret",
},
],
coinType: 118,
features: ["ibc-transfer", "ibc-go"],
explorerUrlToTx: "https://secretnodes.com/secret/chains/secret-4/transactions/{txHash}",
},
Expand Down Expand Up @@ -336,7 +334,6 @@ export const EmbedChainInfosInit: SimpleMap<ChainInfoExplorerTmRpc> = {
coinGeckoId: "akash-network",
},
],
coinType: 118,
features: ["ibc-transfer", "ibc-go"],
explorerUrlToTx: "https://www.mintscan.io/akash/txs/{txHash}",
},
Expand All @@ -346,7 +343,7 @@ export const EmbedChainInfosInit: SimpleMap<ChainInfoExplorerTmRpc> = {
chainId: "regen-1",
chainName: "Regen Network",
stakeCurrency: {
coinDenom: "REGEN",
coinDenom: "REGEN",
coinMinimalDenom: "uregen",
coinDecimals: 6,
coinGeckoId: "regen",
Expand Down Expand Up @@ -1136,7 +1133,6 @@ export const EmbedChainInfosInit: SimpleMap<ChainInfoExplorerTmRpc> = {
coinGeckoId: "pool:ulum",
},
],
coinType: 118,
features: ["ibc-transfer", "ibc-go"],
explorerUrlToTx: "https://www.mintscan.io/lum/txs/{txHash}",
tmRpc: "https://node0.mainnet.lum.network/rpc/",
Expand Down Expand Up @@ -1172,7 +1168,6 @@ export const EmbedChainInfosInit: SimpleMap<ChainInfoExplorerTmRpc> = {
coinGeckoId: "vidulum",
},
],
coinType: 370,
features: ["ibc-transfer", "ibc-go"],
explorerUrlToTx: "https://explorers.vidulum.app/vidulum/tx/{txHash}",
tmRpc: "https://mainnet-rpc.vidulum.app/",
Expand Down Expand Up @@ -1887,7 +1882,6 @@ export const EmbedChainInfosInit: SimpleMap<ChainInfoExplorerTmRpc> = {
coinType: 60,
},
bech32Config: IBCAddress.defaultBech32Config("canto"),
coinType: 60,
currencies: [
{
coinDenom: "CANTO",
Expand Down
21 changes: 13 additions & 8 deletions src/provider/keplr/KeplrAccount.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Carbon } from "@carbon-sdk/CarbonSDK";
import { CARBON_GAS_PRICE, Network, NetworkConfigs, decTypeDecimals } from "@carbon-sdk/constant";
import { CARBON_GAS_PRICE, CarbonEvmChainIDs, Network, NetworkConfigs, decTypeDecimals } from "@carbon-sdk/constant";
import { CarbonSDK, Models } from "@carbon-sdk/index";
import { AddressUtils, CarbonTx, FetchUtils, NumberUtils } from "@carbon-sdk/util";
import { CarbonSigner, CarbonSignerTypes } from "@carbon-sdk/wallet";
import { Algo } from "@cosmjs/proto-signing";
import { AppCurrency, ChainInfo, EthSignType, FeeCurrency, Keplr, Key } from "@keplr-wallet/types";
import SDKProvider from "../sdk";
import { ethers } from "ethers";
import { PUBLIC_KEY_SIGNING_TEXT, populateEvmTransactionDetails } from "@carbon-sdk/util/ethermint";
import { PUBLIC_KEY_SIGNING_TEXT, parseChainId, populateUnsignedEvmTranscation } from "@carbon-sdk/util/ethermint";
import { signTransactionWrapper } from "@carbon-sdk/util/provider";
import { parseEvmError } from "../metamask/error";

Expand Down Expand Up @@ -57,16 +57,16 @@ class KeplrAccount {

const sendEvmTransaction = async (api: CarbonSDK, req: ethers.providers.TransactionRequest): Promise<string> => {
try {
const request = await populateEvmTransactionDetails(api, req)
const unsignedTx = await populateUnsignedEvmTranscation(api, req)
const signedTx = await keplr!.signEthereum(
// carbon chain id
api.wallet?.getChainId() ?? '',
// cosmos address
api.wallet?.bech32Address ?? '',
JSON.stringify(request),
JSON.stringify(unsignedTx),
EthSignType.TRANSACTION,
)
const rlpEncodedHex = `0x${Buffer.from(signedTx).toString('hex')}`;
const rlpEncodedHex = ethers.utils.serializeTransaction(unsignedTx, signedTx)
const provider = new ethers.providers.JsonRpcProvider(NetworkConfigs[api.network].evmJsonRpcUrl)
return (await provider.sendTransaction(rlpEncodedHex)).hash
}
Expand Down Expand Up @@ -107,16 +107,17 @@ class KeplrAccount {

const sendEvmTransaction = async (api: CarbonSDK, req: ethers.providers.TransactionRequest): Promise<string> => {
try {
const request = await populateEvmTransactionDetails(api, req)
const unsignedTx = await populateUnsignedEvmTranscation(api, req)
const signedTx = await keplr!.signEthereum(
// carbon chain id
api.wallet?.getChainId() ?? '',
// cosmos address
api.wallet?.bech32Address ?? '',
JSON.stringify(request),
JSON.stringify(unsignedTx),
EthSignType.TRANSACTION,
)
const rlpEncodedHex = `0x${Buffer.from(signedTx).toString('hex')}`;
// const rlpEncodedHex = `0x${Buffer.from(signedTx).toString('hex')}`
const rlpEncodedHex = ethers.utils.serializeTransaction(unsignedTx, signedTx)
const provider = new ethers.providers.JsonRpcProvider(NetworkConfigs[api.network].evmJsonRpcUrl)
return (await provider.sendTransaction(rlpEncodedHex)).hash
}
Expand Down Expand Up @@ -221,6 +222,10 @@ class KeplrAccount {
rpc: config.tmRpcUrl,
chainName: `Carbon (${config.network})`,
chainId: chainId,
evm: {
chainId: Number(parseChainId(CarbonEvmChainIDs[config.network])),
rpc: config.evmJsonRpcUrl,
},
bech32Config: {
bech32PrefixAccAddr: `${bech32Prefix}`,
bech32PrefixAccPub: `${bech32Prefix}pub`,
Expand Down
61 changes: 56 additions & 5 deletions src/provider/leap/LeapAccount.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Carbon } from "@carbon-sdk/CarbonSDK";
import { CARBON_GAS_PRICE, Network, decTypeDecimals } from "@carbon-sdk/constant";
import { CARBON_GAS_PRICE, Network, decTypeDecimals, NetworkConfigs } from "@carbon-sdk/constant";
import { CarbonSDK, Models } from "@carbon-sdk/index";
import { AddressUtils, CarbonTx, NumberUtils } from "@carbon-sdk/util";
import { CarbonSigner, CarbonSignerTypes } from "@carbon-sdk/wallet";
import { Algo } from "@cosmjs/proto-signing";
import { Leap } from "@cosmos-kit/leap-extension";

import { AppCurrency, ChainInfo, FeeCurrency } from "@keplr-wallet/types";
import { AppCurrency, ChainInfo, EthSignType, FeeCurrency } from "@keplr-wallet/types";
import SDKProvider from "../sdk";
import { ethers } from "ethers";
import { signTransactionWrapper } from "@carbon-sdk/util/provider";
import { populateUnsignedEvmTranscation } from "@carbon-sdk/util/ethermint";
import { parseEvmError } from "../metamask/error";

const SWTH: FeeCurrency = {
coinDenom: "SWTH",
Expand Down Expand Up @@ -54,7 +56,31 @@ class LeapAccount {
};

const sendEvmTransaction = async (api: CarbonSDK, req: ethers.providers.TransactionRequest) => { // eslint-disable-line
throw new Error("signing not available");
try {
// workaround for version mismatch in cosmos-kit/leap and leap-extension
const leapInterface = leap as any
if (typeof (leapInterface as any).signEthereum === 'function') {

const unsignedTx = await populateUnsignedEvmTranscation(api, req)
const signedTx = await leapInterface!.signEthereum(
// carbon chain id
api.wallet?.getChainId() ?? '',
// cosmos address
api.wallet?.bech32Address ?? '',
JSON.stringify(unsignedTx),
EthSignType.TRANSACTION,
) as Uint8Array
const rlpEncodedHex = ethers.utils.serializeTransaction(unsignedTx, signedTx)
const provider = new ethers.providers.JsonRpcProvider(NetworkConfigs[api.network].evmJsonRpcUrl)
return (await provider.sendTransaction(rlpEncodedHex)).hash
} else {
throw new Error("signing not available")
}
}
catch (error) {
console.error(error)
throw (parseEvmError(error as Error))
}
}

const signMessage = async (address: string, message: string): Promise<string> => {
Expand Down Expand Up @@ -91,8 +117,33 @@ class LeapAccount {
];
};


const sendEvmTransaction = async (api: CarbonSDK, req: ethers.providers.TransactionRequest) => { // eslint-disable-line
throw new Error("signing not available");
try {
const leapInterface = leap as any
if (typeof (leapInterface as any).signEthereum === 'function') {

const unsignedTx = await populateUnsignedEvmTranscation(api, req)
console.log('xx unsignedTx', unsignedTx)
const signedTx = await leapInterface!.signEthereum(
// carbon chain id
api.wallet?.getChainId() ?? '',
// cosmos address
api.wallet?.bech32Address ?? '',
JSON.stringify(unsignedTx),
EthSignType.TRANSACTION,
) as Uint8Array
const rlpEncodedHex = ethers.utils.serializeTransaction(unsignedTx, signedTx)
const provider = new ethers.providers.JsonRpcProvider(NetworkConfigs[api.network].evmJsonRpcUrl)
return (await provider.sendTransaction(rlpEncodedHex)).hash
} else {
throw new Error("signing not available")
}
}
catch (error) {
console.error(error)
throw (parseEvmError(error as Error))
}
}

const signMessage = async (address: string, message: string): Promise<string> => {
Expand Down Expand Up @@ -167,7 +218,7 @@ class LeapAccount {
}
}

namespace LeapAccount {}
namespace LeapAccount { }

export interface LeapExtended extends Leap {
experimentalSuggestChain(chainInfo: ChainInfo): Promise<void>;
Expand Down
58 changes: 58 additions & 0 deletions src/util/ethermint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,61 @@ export async function populateEvmTransactionDetails(api: CarbonSDK, req: ethers.
}

}

export async function populateUnsignedEvmTranscation(api: CarbonSDK, req: ethers.providers.TransactionRequest): Promise<ethers.utils.UnsignedTransaction> {
const provider = new ethers.providers.JsonRpcProvider(NetworkConfigs[api.network].evmJsonRpcUrl)
const evmHexAddress = api.wallet?.evmHexAddress ?? ''
const nonce = req.nonce ?? (await provider.getTransactionCount(evmHexAddress))
const chainId = req.chainId ?? Number(parseChainId(await api.wallet?.getEvmChainId() ?? ''))
const value = req.value ? `0x${Number(req.value).toString(16)}` : undefined

const baseTx: ethers.providers.TransactionRequest = {
to: req.to ?? '',
from: req.from ?? api.wallet?.evmHexAddress,
nonce,
data: req.data,
value,
chainId,
// type = 0, 1 or 2 where 0 = legacyTx, 1 = AccessListTx, 2 = DynamicTx. Defaults to DynamicTx
type: req.type ?? 2,
accessList: req.accessList,
}
const gasLimit = (await provider.estimateGas(baseTx)).toHexString()
const gasFee = await provider.getFeeData()

const unsignedTx: ethers.utils.UnsignedTransaction = {
to: req.to ?? '',
nonce: parseInt(nonce.toString()),
data: req.data,
value,
chainId,
// type = 0, 1 or 2 where 0 = legacyTx, 1 = AccessListTx, 2 = DynamicTx. Defaults to DynamicTx
type: req.type ?? 2,
accessList: req.accessList,
}

if (!req.gasPrice) {
// Dynamic Tx
return {
...unsignedTx,
maxPriorityFeePerGas: req.maxPriorityFeePerGas ?? gasFee.maxPriorityFeePerGas?.toHexString(),
maxFeePerGas: req.maxFeePerGas ?? gasFee.maxFeePerGas?.toHexString(),
gasLimit,
type: 2,
}
}
if (req.accessList) {
// AccessList Tx
return {
...unsignedTx,
gasLimit,
type: 1,
}
}
// LegacyTx
return {
...unsignedTx,
gasLimit,
type: 0,
}
}
15 changes: 11 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -891,14 +891,21 @@
long "^4.0.0"
secretjs "0.17.7"

"@keplr-wallet/[email protected]", "@keplr-wallet/types@^0.11.64":
"@keplr-wallet/[email protected]":
version "0.11.64"
resolved "https://registry.yarnpkg.com/@keplr-wallet/types/-/types-0.11.64.tgz#5a308c8c019b4e18f894e0f35f0904b60134d605"
integrity sha512-GgzeLDHHfZFyne3O7UIfFHj/uYqVbxAZI31RbBwt460OBbvwQzjrlZwvJW3vieWRAgxKSITjzEDBl2WneFTQdQ==
dependencies:
axios "^0.27.2"
long "^4.0.0"

"@keplr-wallet/types@^0.12.107":
version "0.12.108"
resolved "https://registry.yarnpkg.com/@keplr-wallet/types/-/types-0.12.108.tgz#cd7cef0e48fce556bbd86794a5d9ea45ba42f430"
integrity sha512-ILoRa46WKGUubh8sf4+4X2Qi7vQxIPjC5uCPhnLajRKNYUJe/Y7e+uWUt7pYEqrGPchn0XWs6xNYtR5gwKXJdA==
dependencies:
long "^4.0.0"

"@keplr-wallet/[email protected]":
version "0.11.16"
resolved "https://registry.yarnpkg.com/@keplr-wallet/unit/-/unit-0.11.16.tgz#21c19bd985620b32a6de6f06850e9c0fdeb4f88c"
Expand Down Expand Up @@ -3296,9 +3303,9 @@ follow-redirects@^1.10.0, follow-redirects@^1.14.0:
integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==

follow-redirects@^1.14.9:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==
version "1.15.6"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b"
integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==

form-data@^3.0.0:
version "3.0.1"
Expand Down