From 085cc6cfd3cd53af5c81fa9c51893a8995f1a23e Mon Sep 17 00:00:00 2001 From: Alessandro Candeago <54709706+alecande11@users.noreply.github.com> Date: Sat, 16 Mar 2024 16:46:19 +0100 Subject: [PATCH] fix keplr connector --- .packages.json | 4 +-- package.json | 2 +- .../station-connector/cosmjsOfflineSigner.ts | 19 ++++++++----- src/@terra-money/station-connector/index.ts | 16 ++++++++++- .../station-connector/keplrConnector.ts | 27 ++++++++++++++++--- src/@terra-money/station-connector/log.ts | 5 ++++ 6 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 src/@terra-money/station-connector/log.ts diff --git a/.packages.json b/.packages.json index 8bfd266..6290d73 100644 --- a/.packages.json +++ b/.packages.json @@ -1,7 +1,7 @@ { "$schema": "https://rocket-hangar.github.io/rocket-punch/schemas/packages.json", "@terra-money/*": { - "version": "1.0.19-beta.0", - "tag": "beta" + "version": "1.1.0", + "tag": "latest" } } diff --git a/package.json b/package.json index 509bbd7..5d9f301 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wallet-kit", - "version": "1.0.19-beta.0", + "version": "1.1.0", "description": "new version of wallet-provider", "main": "index.js", "scripts": { diff --git a/src/@terra-money/station-connector/cosmjsOfflineSigner.ts b/src/@terra-money/station-connector/cosmjsOfflineSigner.ts index fb25264..7e8bc36 100644 --- a/src/@terra-money/station-connector/cosmjsOfflineSigner.ts +++ b/src/@terra-money/station-connector/cosmjsOfflineSigner.ts @@ -5,7 +5,8 @@ import { StdSignDoc, StdSignature, } from '@cosmjs/amino' -import { Fee, Msg, SignatureV2 } from '@terra-money/feather.js' +import { Fee, Msg, SignDoc, SignatureV2 } from '@terra-money/feather.js' +import { log } from './log' export default class StationOfflineSigner implements OfflineAminoSigner { private chainID: string @@ -59,7 +60,7 @@ export default class StationOfflineSigner implements OfflineAminoSigner { ) if (addresses[this.chainID] !== signerAddress) throw new Error( - `Account mismatch: you are trying to sign a tx with ${signerAddress}, but you address on ${ + `Account mismatch: you are trying to sign a tx with ${signerAddress}, but your address on ${ this.chainID } is ${addresses[this.chainID]}.`, ) @@ -97,15 +98,19 @@ export default class StationOfflineSigner implements OfflineAminoSigner { signature: signResponse.signatures[0], } - return { + // @ts-expect-error + delete signDoc['timeout_height'] + + const result = { signed: { ...signDoc, - fee: { - ...signDocFee, - amount: [{ denom: feeDenom, amount: feeAmount }], - }, + fee: signResponse.fee, }, signature, } + + log("'signAmino' result", result) + + return result } } diff --git a/src/@terra-money/station-connector/index.ts b/src/@terra-money/station-connector/index.ts index c4e1622..bf2c766 100644 --- a/src/@terra-money/station-connector/index.ts +++ b/src/@terra-money/station-connector/index.ts @@ -1,6 +1,7 @@ -import { PublicKey } from '@terra-money/feather.js' +import { Fee, PublicKey } from '@terra-money/feather.js' import StationOfflineSigner from './cosmjsOfflineSigner' import KeplrConnector from './keplrConnector' +import { log } from './log' type NetworkName = 'mainnet' | 'testnet' | 'classic' | 'localterra' type ChainID = string @@ -56,6 +57,7 @@ export type SignResponse = { auth_info: Object body: Object signatures: string[] + fee: Fee.Amino } export type SignArbitraryResponse = { @@ -75,6 +77,7 @@ export default class Station { { resolve: (data: any) => void; reject: (data: any) => void } > = {} public keplr: KeplrConnector + public debugMode: boolean = false constructor() { const origin = window.location.origin @@ -92,6 +95,8 @@ export default class Station { ? this._pendingRequests[reqID].resolve(data) : this._pendingRequests[reqID].reject(data?.error?.message ?? data) delete this._pendingRequests[reqID] + + log('response sent', { reqID, data }) }) this.keplr = new KeplrConnector() @@ -111,6 +116,7 @@ export default class Station { } async info(): Promise { + log("'info' request received") return new Promise((resolve, reject) => { const reqID = crypto.randomUUID() this._sendMessage({ type: 'interchain-info' }, reqID) @@ -119,6 +125,7 @@ export default class Station { } async connect(): Promise { + log("'connect' request received") return new Promise((resolve, reject) => { const reqID = crypto.randomUUID() this._sendMessage({ type: 'connect' }, reqID) @@ -127,6 +134,7 @@ export default class Station { } async getPublicKey(): Promise { + log("'pubKey' request received") return new Promise((resolve, reject) => { const reqID = crypto.randomUUID() this._sendMessage({ type: 'get-pubkey' }, reqID) @@ -135,6 +143,7 @@ export default class Station { } async theme(): Promise { + log("'theme' request received") return new Promise((resolve, reject) => { const reqID = crypto.randomUUID() this._sendMessage({ type: 'theme' }, reqID) @@ -143,6 +152,7 @@ export default class Station { } async post(tx: TxRequest, purgeQueue = true): Promise { + log("'post' request received", { tx, purgeQueue }) return new Promise((resolve, reject) => { const reqID = crypto.randomUUID() this._sendMessage( @@ -157,6 +167,7 @@ export default class Station { } async sign(tx: TxRequest, purgeQueue = true): Promise { + log("'sign' request received", { tx, purgeQueue }) return new Promise((resolve, reject) => { const reqID = crypto.randomUUID() this._sendMessage( @@ -175,6 +186,7 @@ export default class Station { chainID?: string, purgeQueue = true, ): Promise { + log("'signBytes' request received", { bytes, chainID, purgeQueue }) const fixedPurgeQueue = typeof chainID !== 'string' ? chainID : purgeQueue const fixedChainID = typeof chainID === 'string' ? chainID : undefined @@ -212,6 +224,7 @@ export default class Station { chainID?: string, purgeQueue = true, ): Promise { + log("'signArbitrary' request received", { bytes, chainID, purgeQueue }) const fixedPurgeQueue = typeof chainID !== 'string' ? chainID : purgeQueue const fixedChainID = typeof chainID === 'string' ? chainID : undefined @@ -256,6 +269,7 @@ export default class Station { network: NetworkName, purgeQueue = true, ): Promise<{ success: true; network: NetworkName }> { + log("'switchNetwork' request received", { network, purgeQueue }) return new Promise((resolve, reject) => { const reqID = crypto.randomUUID() this._sendMessage( diff --git a/src/@terra-money/station-connector/keplrConnector.ts b/src/@terra-money/station-connector/keplrConnector.ts index 4625196..1539828 100644 --- a/src/@terra-money/station-connector/keplrConnector.ts +++ b/src/@terra-money/station-connector/keplrConnector.ts @@ -3,6 +3,7 @@ import StationOfflineSigner from './cosmjsOfflineSigner' import { bech32 } from 'bech32' import axios from 'axios' import { PublicKey } from '@terra-money/feather.js' +import { log } from './log' export type GetKeyResponse = { // Name of the selected key store. @@ -65,26 +66,33 @@ export default class KeplrConnector { readonly mode = 'extension' getOfflineSigner(chainID: string): StationOfflineSigner { + log("'getOfflineSigner' requested") return new StationOfflineSigner(chainID) } getOfflineSignerOnlyAmino(chainID: string): StationOfflineSigner { + log("'getOfflineSignerOnlyAmino' requested") return new StationOfflineSigner(chainID) } async getOfflineSignerAuto(chainID: string): Promise { + log("'getOfflineSignerAuto' requested") return new StationOfflineSigner(chainID) } async enable(chainID: string | string[]): Promise { + log("'enable' requested", { chainID }) if (!window.station) throw new Error('Station not available') await window.station.connect() } - async disable(chainID?: string | string[]): Promise {} + async disable(chainID?: string | string[]): Promise { + log("'disable' requested", { chainID }) + } async getKey(chainID: string): Promise { + log("'getKey' requested", { chainID }) if (!window.station) throw new Error('Station not available') const info = (await window.station.info())[chainID] @@ -97,7 +105,7 @@ export default class KeplrConnector { connectedWallet.pubkey?.[info.coinType] ?? ((await window.station.getPublicKey()).pubkey?.[info.coinType] as string) - return { + const result = { name: connectedWallet.name, algo: 'secp256k1', pubKey: Buffer.from(pubkey, 'base64'), @@ -108,6 +116,10 @@ export default class KeplrConnector { // since protobuf is not supported by Station, we set this as true isNanoLedger: true, } + + log("'getKey' result", result) + + return result } async signAmino( @@ -115,8 +127,9 @@ export default class KeplrConnector { signer: string, signDoc: StdSignDoc, ): Promise { + log("'signAmino' requested", { chainID, signer, signDoc }) const offlineSigner = this.getOfflineSigner(chainID) - return offlineSigner.signAmino(signer, signDoc) + return await offlineSigner.signAmino(signer, signDoc) } async signArbitrary( @@ -124,6 +137,7 @@ export default class KeplrConnector { _: string, data: string | Uint8Array, ): Promise { + log("'signArbitrary' requested", { chainID, _, data }) const parsedData = Buffer.from(data).toString('base64') const response = await window.station?.signArbitrary(parsedData, chainID) @@ -133,13 +147,18 @@ export default class KeplrConnector { 'Signature failed, empty response received from the extension.', ) - return { + const result = { ...response, pub_key: PublicKey.fromData(response.pub_key).toAmino(), } + + log("'signArbitrary' result", result) + + return result } async signDirect(...args: any): Promise { + log("'signDirect' requested", args) throw new Error('signDirect not supported by Station') } diff --git a/src/@terra-money/station-connector/log.ts b/src/@terra-money/station-connector/log.ts new file mode 100644 index 0000000..a6fdd62 --- /dev/null +++ b/src/@terra-money/station-connector/log.ts @@ -0,0 +1,5 @@ +export function log(message: string, extra?: any) { + if (window.station?.debugMode) { + console.log(`🛰️ STATION EXTENSION: ${message}`, extra) + } +}