|
| 1 | +import { fromBase64, fromBech32, toHex } from '@cosmjs/encoding'; |
| 2 | +import { |
| 3 | + Registry, |
| 4 | + TxBodyEncodeObject, |
| 5 | + makeAuthInfoBytes, |
| 6 | + makeSignDoc, |
| 7 | +} from '@cosmjs/proto-signing'; |
| 8 | +import { |
| 9 | + AbstractWallet, |
| 10 | + Account, |
| 11 | + WalletArgument, |
| 12 | + WalletName, |
| 13 | + keyType, |
| 14 | +} from '../Wallet'; |
| 15 | +import { Transaction } from '../../utils/type'; |
| 16 | +import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx'; |
| 17 | +import { Any } from 'cosmjs-types/google/protobuf/any'; |
| 18 | +import { PubKey } from 'cosmjs-types/cosmos/crypto/secp256k1/keys'; |
| 19 | +import { SignMode } from 'cosmjs-types/cosmos/tx/signing/v1beta1/signing'; |
| 20 | +import { AminoTypes, createDefaultAminoConverters } from '@cosmjs/stargate'; |
| 21 | +import { |
| 22 | + makeSignDoc as makeSignDocAmino, |
| 23 | +} from '@cosmjs/amino'; |
| 24 | +import { createWasmAminoConverters } from '@cosmjs/cosmwasm-stargate'; |
| 25 | + |
| 26 | +/** |
| 27 | + * Gate Wallet implementation for Cosmos chains |
| 28 | + * |
| 29 | + * Gate Wallet is fully compatible with Keplr API through window.gatewallet.keplr |
| 30 | + * |
| 31 | + * @see https://www.gate.io/web3 |
| 32 | + */ |
| 33 | +export class GateWallet implements AbstractWallet { |
| 34 | + name: WalletName.GateWallet; |
| 35 | + chainId: string; |
| 36 | + registry: Registry; |
| 37 | + conf: WalletArgument; |
| 38 | + aminoTypes = new AminoTypes({ |
| 39 | + ...createDefaultAminoConverters(), |
| 40 | + ...createWasmAminoConverters(), |
| 41 | + }); |
| 42 | + |
| 43 | + constructor(arg: WalletArgument, registry: Registry) { |
| 44 | + console.log('🦛 [GateWallet] Constructor called', { |
| 45 | + chainId: arg.chainId, |
| 46 | + hasWindow: typeof window !== 'undefined', |
| 47 | + hasGateWallet: typeof window !== 'undefined' && !!window.gatewallet, |
| 48 | + hasKeplr: typeof window !== 'undefined' && !!window.gatewallet?.keplr |
| 49 | + }); |
| 50 | + |
| 51 | + this.chainId = arg.chainId || 'cosmoshub'; |
| 52 | + // @ts-ignore |
| 53 | + if (!window.gatewallet?.keplr) { |
| 54 | + console.error('❌ [GateWallet] Gate Wallet not found!', { |
| 55 | + gatewallet: window.gatewallet, |
| 56 | + keplr: window.keplr |
| 57 | + }); |
| 58 | + throw new Error('Please install Gate Wallet extension'); |
| 59 | + } |
| 60 | + this.registry = registry; |
| 61 | + this.conf = arg; |
| 62 | + console.log('✅ [GateWallet] Constructor completed'); |
| 63 | + } |
| 64 | + |
| 65 | + async getAccounts(): Promise<Account[]> { |
| 66 | + console.log('🦛 [GateWallet] getAccounts called', { |
| 67 | + chainId: this.chainId |
| 68 | + }); |
| 69 | + |
| 70 | + try { |
| 71 | + // @ts-ignore |
| 72 | + console.log('🦛 [GateWallet] Calling enable...'); |
| 73 | + await window.gatewallet.keplr.enable(this.chainId); |
| 74 | + console.log('✅ [GateWallet] Enable successful'); |
| 75 | + |
| 76 | + // @ts-ignore |
| 77 | + console.log('🦛 [GateWallet] Getting offline signer...'); |
| 78 | + const offlineSigner = window.gatewallet.keplr.getOfflineSigner(this.chainId); |
| 79 | + console.log('✅ [GateWallet] Got offline signer'); |
| 80 | + |
| 81 | + console.log('🦛 [GateWallet] Getting accounts...'); |
| 82 | + const accounts = await offlineSigner.getAccounts(); |
| 83 | + console.log('✅ [GateWallet] Got accounts:', accounts); |
| 84 | + |
| 85 | + return accounts; |
| 86 | + } catch (error) { |
| 87 | + console.error('❌ [GateWallet] Error in getAccounts:', error); |
| 88 | + throw error; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + supportCoinType(coinType?: string | undefined): Promise<boolean> { |
| 93 | + return Promise.resolve(true); |
| 94 | + } |
| 95 | + |
| 96 | + isEthermint() { |
| 97 | + return this.conf.hdPath && this.conf.hdPath.startsWith("m/44'/60"); |
| 98 | + } |
| 99 | + |
| 100 | + async sign(transaction: Transaction, direct?: boolean): Promise<TxRaw> { |
| 101 | + // sign wasm tx with signDirect |
| 102 | + if ( |
| 103 | + transaction.messages.findIndex((x) => |
| 104 | + x.typeUrl.startsWith('/cosmwasm.wasm') |
| 105 | + ) > -1 || |
| 106 | + direct |
| 107 | + ) { |
| 108 | + return this.signDirect(transaction); |
| 109 | + } |
| 110 | + return this.signAmino(transaction); |
| 111 | + } |
| 112 | + |
| 113 | + // @deprecated use signAmino instead |
| 114 | + // because signDirect is not supported ledger wallet |
| 115 | + async signDirect(transaction: Transaction): Promise<TxRaw> { |
| 116 | + const accouts = await this.getAccounts(); |
| 117 | + const hex = toHex(fromBech32(transaction.signerAddress).data); |
| 118 | + const accountFromSigner = accouts.find( |
| 119 | + (account) => toHex(fromBech32(account.address).data) === hex |
| 120 | + ); |
| 121 | + if (!accountFromSigner) { |
| 122 | + throw new Error('Failed to retrieve account from signer'); |
| 123 | + } |
| 124 | + const pubkey = Any.fromPartial({ |
| 125 | + typeUrl: keyType(transaction.chainId), |
| 126 | + value: PubKey.encode({ |
| 127 | + key: accountFromSigner.pubkey, |
| 128 | + }).finish(), |
| 129 | + }); |
| 130 | + const txBodyEncodeObject: TxBodyEncodeObject = { |
| 131 | + typeUrl: '/cosmos.tx.v1beta1.TxBody', |
| 132 | + value: { |
| 133 | + messages: transaction.messages, |
| 134 | + memo: transaction.memo, |
| 135 | + }, |
| 136 | + }; |
| 137 | + const txBodyBytes = this.registry.encode(txBodyEncodeObject); |
| 138 | + const gasLimit = Number(transaction.fee.gas); |
| 139 | + const authInfoBytes = makeAuthInfoBytes( |
| 140 | + [{ pubkey, sequence: transaction.signerData.sequence }], |
| 141 | + transaction.fee.amount, |
| 142 | + gasLimit, |
| 143 | + transaction.fee.granter, |
| 144 | + transaction.fee.payer |
| 145 | + ); |
| 146 | + const signDoc = makeSignDoc( |
| 147 | + txBodyBytes, |
| 148 | + authInfoBytes, |
| 149 | + transaction.chainId, |
| 150 | + transaction.signerData.accountNumber |
| 151 | + ); |
| 152 | + |
| 153 | + // @ts-ignore |
| 154 | + const offlineSigner = window.gatewallet.keplr.getOfflineSigner(this.chainId); |
| 155 | + const { signature, signed } = await offlineSigner.signDirect( |
| 156 | + transaction.signerAddress, |
| 157 | + signDoc |
| 158 | + ); |
| 159 | + return TxRaw.fromPartial({ |
| 160 | + bodyBytes: signed.bodyBytes, |
| 161 | + authInfoBytes: signed.authInfoBytes, |
| 162 | + signatures: [fromBase64(signature.signature)], |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + async signAmino(tx: Transaction): Promise<TxRaw> { |
| 167 | + const accounts = await this.getAccounts(); |
| 168 | + const hex = toHex(fromBech32(tx.signerAddress).data); |
| 169 | + const accountFromSigner = accounts.find( |
| 170 | + (account) => toHex(fromBech32(account.address).data) === hex |
| 171 | + ); |
| 172 | + if (!accountFromSigner) { |
| 173 | + throw new Error('Failed to retrieve account from signer'); |
| 174 | + } |
| 175 | + const pubkey = Any.fromPartial({ |
| 176 | + typeUrl: keyType(tx.chainId), |
| 177 | + value: PubKey.encode({ |
| 178 | + key: accountFromSigner.pubkey, |
| 179 | + }).finish(), |
| 180 | + }); |
| 181 | + const signMode = SignMode.SIGN_MODE_LEGACY_AMINO_JSON; |
| 182 | + const msgs = tx.messages.map((msg) => this.aminoTypes.toAmino(msg)); |
| 183 | + const signDoc = makeSignDocAmino( |
| 184 | + msgs, |
| 185 | + tx.fee, |
| 186 | + tx.chainId, |
| 187 | + tx.memo, |
| 188 | + tx.signerData.accountNumber, |
| 189 | + tx.signerData.sequence |
| 190 | + ); |
| 191 | + |
| 192 | + // @ts-ignore |
| 193 | + const offlineSigner = window.gatewallet.keplr.getOfflineSigner(this.chainId); |
| 194 | + const { signature, signed } = await offlineSigner.signAmino( |
| 195 | + tx.signerAddress, |
| 196 | + signDoc |
| 197 | + ); |
| 198 | + |
| 199 | + const signedTxBody = { |
| 200 | + messages: signed.msgs.map((msg) => this.aminoTypes.fromAmino(msg)), |
| 201 | + memo: signed.memo, |
| 202 | + }; |
| 203 | + const signedTxBodyEncodeObject: TxBodyEncodeObject = { |
| 204 | + typeUrl: '/cosmos.tx.v1beta1.TxBody', |
| 205 | + value: signedTxBody, |
| 206 | + }; |
| 207 | + const signedTxBodyBytes = this.registry.encode( |
| 208 | + signedTxBodyEncodeObject |
| 209 | + ); |
| 210 | + |
| 211 | + const signedGasLimit = Number(signed.fee.gas); |
| 212 | + const signedSequence = Number(signed.sequence); |
| 213 | + const signedAuthInfoBytes = makeAuthInfoBytes( |
| 214 | + [{ pubkey, sequence: signedSequence }], |
| 215 | + signed.fee.amount, |
| 216 | + signedGasLimit, |
| 217 | + signed.fee.granter, |
| 218 | + signed.fee.payer, |
| 219 | + signMode |
| 220 | + ); |
| 221 | + return TxRaw.fromPartial({ |
| 222 | + bodyBytes: signedTxBodyBytes, |
| 223 | + authInfoBytes: signedAuthInfoBytes, |
| 224 | + signatures: [fromBase64(signature.signature)], |
| 225 | + }); |
| 226 | + } |
| 227 | +} |
0 commit comments