Skip to content

Commit

Permalink
fix keplr connector
Browse files Browse the repository at this point in the history
  • Loading branch information
alecande11 committed Mar 16, 2024
1 parent 1eb004e commit 085cc6c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .packages.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
19 changes: 12 additions & 7 deletions src/@terra-money/station-connector/cosmjsOfflineSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]}.`,
)
Expand Down Expand Up @@ -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
}
}
16 changes: 15 additions & 1 deletion src/@terra-money/station-connector/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -56,6 +57,7 @@ export type SignResponse = {
auth_info: Object
body: Object
signatures: string[]
fee: Fee.Amino
}

export type SignArbitraryResponse = {
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -111,6 +116,7 @@ export default class Station {
}

async info(): Promise<InfoResponse> {
log("'info' request received")
return new Promise((resolve, reject) => {
const reqID = crypto.randomUUID()
this._sendMessage({ type: 'interchain-info' }, reqID)
Expand All @@ -119,6 +125,7 @@ export default class Station {
}

async connect(): Promise<ConnectResponse> {
log("'connect' request received")
return new Promise((resolve, reject) => {
const reqID = crypto.randomUUID()
this._sendMessage({ type: 'connect' }, reqID)
Expand All @@ -127,6 +134,7 @@ export default class Station {
}

async getPublicKey(): Promise<ConnectResponse> {
log("'pubKey' request received")
return new Promise((resolve, reject) => {
const reqID = crypto.randomUUID()
this._sendMessage({ type: 'get-pubkey' }, reqID)
Expand All @@ -135,6 +143,7 @@ export default class Station {
}

async theme(): Promise<string> {
log("'theme' request received")
return new Promise((resolve, reject) => {
const reqID = crypto.randomUUID()
this._sendMessage({ type: 'theme' }, reqID)
Expand All @@ -143,6 +152,7 @@ export default class Station {
}

async post(tx: TxRequest, purgeQueue = true): Promise<PostResponse> {
log("'post' request received", { tx, purgeQueue })
return new Promise((resolve, reject) => {
const reqID = crypto.randomUUID()
this._sendMessage(
Expand All @@ -157,6 +167,7 @@ export default class Station {
}

async sign(tx: TxRequest, purgeQueue = true): Promise<SignResponse> {
log("'sign' request received", { tx, purgeQueue })
return new Promise((resolve, reject) => {
const reqID = crypto.randomUUID()
this._sendMessage(
Expand All @@ -175,6 +186,7 @@ export default class Station {
chainID?: string,
purgeQueue = true,
): Promise<SignBytesResponse> {
log("'signBytes' request received", { bytes, chainID, purgeQueue })
const fixedPurgeQueue = typeof chainID !== 'string' ? chainID : purgeQueue
const fixedChainID = typeof chainID === 'string' ? chainID : undefined

Expand Down Expand Up @@ -212,6 +224,7 @@ export default class Station {
chainID?: string,
purgeQueue = true,
): Promise<SignArbitraryResponse> {
log("'signArbitrary' request received", { bytes, chainID, purgeQueue })
const fixedPurgeQueue = typeof chainID !== 'string' ? chainID : purgeQueue
const fixedChainID = typeof chainID === 'string' ? chainID : undefined

Expand Down Expand Up @@ -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(
Expand Down
27 changes: 23 additions & 4 deletions src/@terra-money/station-connector/keplrConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<StationOfflineSigner> {
log("'getOfflineSignerAuto' requested")
return new StationOfflineSigner(chainID)
}

async enable(chainID: string | string[]): Promise<void> {
log("'enable' requested", { chainID })
if (!window.station) throw new Error('Station not available')

await window.station.connect()
}

async disable(chainID?: string | string[]): Promise<void> {}
async disable(chainID?: string | string[]): Promise<void> {
log("'disable' requested", { chainID })
}

async getKey(chainID: string): Promise<GetKeyResponse> {
log("'getKey' requested", { chainID })
if (!window.station) throw new Error('Station not available')

const info = (await window.station.info())[chainID]
Expand All @@ -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'),
Expand All @@ -108,22 +116,28 @@ 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(
chainID: string,
signer: string,
signDoc: StdSignDoc,
): Promise<AminoSignResponse> {
log("'signAmino' requested", { chainID, signer, signDoc })
const offlineSigner = this.getOfflineSigner(chainID)
return offlineSigner.signAmino(signer, signDoc)
return await offlineSigner.signAmino(signer, signDoc)
}

async signArbitrary(
chainID: string,
_: string,
data: string | Uint8Array,
): Promise<StdSignature> {
log("'signArbitrary' requested", { chainID, _, data })
const parsedData = Buffer.from(data).toString('base64')

const response = await window.station?.signArbitrary(parsedData, chainID)
Expand All @@ -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<any> {
log("'signDirect' requested", args)
throw new Error('signDirect not supported by Station')
}

Expand Down
5 changes: 5 additions & 0 deletions src/@terra-money/station-connector/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function log(message: string, extra?: any) {
if (window.station?.debugMode) {
console.log(`🛰️ STATION EXTENSION: ${message}`, extra)
}
}

0 comments on commit 085cc6c

Please sign in to comment.