add ethereum app

This commit is contained in:
liangping
2022-06-03 11:24:05 +08:00
parent 4ca5dbce33
commit d8c0f02bc6
7 changed files with 171 additions and 64 deletions
+16 -12
View File
@@ -1,5 +1,5 @@
import { AminoSignResponse, OfflineAminoSigner, Pubkey, StdSignature, StdSignDoc } from "@cosmjs/amino";
import { AccountData, AminoSignResponse, OfflineAminoSigner, Pubkey, StdSignature, StdSignDoc } from "@cosmjs/amino";
import { AddressAndPubkey } from "@cosmjs/ledger-amino";
import Transport from "@ledgerhq/hw-transport";
@@ -12,15 +12,15 @@ import { HdPath } from "@cosmjs/crypto";
import { ethToCosmos } from '@tharsis/address-converter'
import eth from "@tharsis/proto/dist/proto/ethermint/crypto/v1/ethsecp256k1/keys";
export type Algo = "secp256k1" | "ed25519" | "sr25519" | "ethsecp256k1";
export interface AccountData {
/** A printable address (typically bech32 encoded) */
readonly address: string;
readonly algo: Algo;
readonly pubkey: Uint8Array;
}
// export type Algo = "secp256k1" | "ed25519" | "sr25519" | "ethsecp256k1";
// export interface AccountData {
// /** A printable address (typically bech32 encoded) */
// readonly address: string;
// readonly algo: Algo;
// readonly pubkey: Uint8Array;
// }
export class EthereumLedgerSigner {
export class EthereumLedgerSigner implements OfflineAminoSigner{
app: Eth
hdpath: string
@@ -40,12 +40,12 @@ export class EthereumLedgerSigner {
const x1: AccountData = {
pubkey: new TextEncoder().encode(x.publicKey),
address: x.address,
algo: "ethsecp256k1" // should be 'ethsecp256k1'
algo: "secp256k1" // should be 'ethsecp256k1'
}
const x2: AccountData = {
pubkey: new TextEncoder().encode(x.publicKey),
address: ethToCosmos(x.address),
algo: "ethsecp256k1" // // should be 'ethsecp256k1'
algo: "secp256k1" // // should be 'ethsecp256k1'
}
return [x1, x2]
})
@@ -91,7 +91,11 @@ export class EthereumLedgerSigner {
return new Promise((r, j) => { })
}
async signTransaction(rawTxHex: string, resolution: LedgerEthTransactionResolution) {
// async sign712Transaction(rawTxHex: string, resolution: LedgerEthTransactionResolution) {
// return this.app.signEIP712HashedMessage(this.hdpath, rawTxHex, resolution)
// }
async signTransaction(rawTxHex: string, resolution?: LedgerEthTransactionResolution) {
return this.app.signTransaction(this.hdpath, rawTxHex, resolution)
}
}
+77
View File
@@ -0,0 +1,77 @@
import {
AminoTypes,
SignerData,
SigningStargateClient,
SigningStargateClientOptions,
StargateClient
} from '@cosmjs/stargate';
import { Registry, OfflineSigner, EncodeObject } from '@cosmjs/proto-signing';
import { defaultRegistryTypes } from '@cosmjs/stargate';
import { LedgerSigner } from '@cosmjs/ledger-amino';
import { EthereumLedgerSigner } from './EthereumLedgerSigner';
import TransportWebUSB from '@ledgerhq/hw-transport-webusb';
import TransportWebBLE from '@ledgerhq/hw-transport-web-ble'
import { stringToPath } from '@cosmjs/crypto';
import { StdFee } from "@cosmjs/amino";
import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
// export async function sign(device, chainId, signerAddress, messages, fee, memo, signerData) {
// // let transport
// let signer
// const hdpath = getHdPath(signerAddress)
// const coinType = Number(hdpath[1])
// switch (device) {
// case 'ledgerBle':
// signer = await getLedgerAppName(coinType, device, hdpath)
// break
// case 'ledgerUSB':
// signer = await getLedgerAppName(coinType, device, hdpath)
// break
// case 'keplr':
// default:
// if (!window.getOfflineSigner || !window.keplr) {
// throw new Error('Please install keplr extension')
// }
// await window.keplr.enable(chainId)
// signer = window.getOfflineSignerOnlyAmino(chainId)
// }
// // Ensure the address has some tokens to spend
// const client = await PingWalletClient.offline(signer)
// return client.signAmino(device.startsWith('ledger') ? toSignAddress(signerAddress) : signerAddress, messages, fee, memo, signerData)
// }
export declare type SigningClient = SigningStargateClient | SigningEthermintClient;
export async function getSigningClient(device, hdpath) : Promise<SigningClient> {
let ledgerAppName = 'Cosmos'
let coinType = Number(stringToPath(hdpath)[1])
switch (coinType) {
case 60:
return new SigningEthermintClient(await EthereumLedgerSigner.create(device, hdpath)) // 'Ethereum'
case 529:
ledgerAppName = 'Secret' // 'Secret'
break
case 852:
ledgerAppName = 'Desmos' // 'Desmos'
break
case 118:
default:
}
const transport = await (device === 'ledgerBle' ? TransportWebBLE.create() : TransportWebUSB.create())
const signer = new LedgerSigner(transport, { hdPaths: [hdpath], ledgerAppName })
return SigningStargateClient.offline(signer)
}
export class SigningEthermintClient {
readonly signer: OfflineSigner
constructor(signer: OfflineSigner) {
this.signer = signer
}
sign(signerAddress: string, messages: readonly EncodeObject[], fee: StdFee, memo: string, explicitSignerData?: SignerData): Promise<TxRaw> {
return new Promise(()=>{return TxRaw.decode(null)})
}
}
+1 -1
View File
@@ -39,7 +39,7 @@ export default class ChainFetch {
let chain = store.state.chains.selected
const lschains = localStorage.getItem('chains')
if (lschains) {
chain = JSON.parse(lschains)[chain.chain_name]
chain = JSON.parse(lschains)[chain?.chain_name || 'cosmos']
}
if (!chain.sdk_version) {
chain.sdk_version = '0.33'