From 648cebb4c73967076f9ec2441f62d959f4405a86 Mon Sep 17 00:00:00 2001 From: Govard Barkhatov Date: Thu, 12 Dec 2024 00:40:02 +0200 Subject: [PATCH 1/2] Keplr offline signer, minimize interface --- .changeset/big-eggs-attack.md | 5 ++++ src/core/types.ts | 25 ++++++++++++-------- src/core/wallets/bbn/BBNProvider.ts | 25 +++++++++++++------- src/core/wallets/bbn/keplr/provider.ts | 32 ++++++++++++-------------- 4 files changed, 53 insertions(+), 34 deletions(-) create mode 100644 .changeset/big-eggs-attack.md diff --git a/.changeset/big-eggs-attack.md b/.changeset/big-eggs-attack.md new file mode 100644 index 0000000..d0ea307 --- /dev/null +++ b/.changeset/big-eggs-attack.md @@ -0,0 +1,5 @@ +--- +"@babylonlabs-io/bbn-wallet-connect": patch +--- + +Keplr offline signer, minimize interface diff --git a/src/core/types.ts b/src/core/types.ts index f758384..8c54046 100644 --- a/src/core/types.ts +++ b/src/core/types.ts @@ -1,5 +1,4 @@ -import type { SigningStargateClient, SigningStargateClientOptions } from "@cosmjs/stargate"; -import { ChainInfo } from "@keplr-wallet/types"; +import { ChainInfo, OfflineAminoSigner, OfflineDirectSigner } from "@keplr-wallet/types"; import { ComponentType } from "react"; export type Fees = { @@ -257,15 +256,23 @@ export interface IBBNProvider extends IProvider { getPublicKeyHex(): Promise; /** - * Gets the signing stargate client. - * @returns A promise that resolves to the signing stargate client. + * Gets the name of the wallet provider. + * @returns A promise that resolves to the name of the wallet provider. */ - getSigningStargateClient(options?: SigningStargateClientOptions): Promise; + getWalletProviderName(): Promise; + /** - * Gets the balance of the connected wallet. - * @param searchDenom - The denomination to search for in the wallet's balance. - * @returns A promise that resolves to the balance of the connected wallet. + * Gets the icon URL of the wallet provider. + * @returns A promise that resolves to the icon URL of the wallet provider. */ + getWalletProviderIcon(): Promise; - getBalance(searchDenom: string): Promise; + /** + * Retrieves an offline signer that supports both Amino and Direct signing methods. + * This signer is used for signing transactions offline before broadcasting them to the network. + * + * @returns {Promise} A promise that resolves to a signer supporting both Amino and Direct signing + * @throws {Error} If wallet connection is not established or signer cannot be retrieved + */ + getOfflineSigner(): Promise; } diff --git a/src/core/wallets/bbn/BBNProvider.ts b/src/core/wallets/bbn/BBNProvider.ts index 215510d..30c4b4b 100644 --- a/src/core/wallets/bbn/BBNProvider.ts +++ b/src/core/wallets/bbn/BBNProvider.ts @@ -1,4 +1,4 @@ -import type { SigningStargateClient, SigningStargateClientOptions } from "@cosmjs/stargate"; +import { OfflineAminoSigner, OfflineDirectSigner } from "@keplr-wallet/types"; import { IBBNProvider } from "@/core/types"; @@ -23,14 +23,23 @@ export abstract class BBNProvider implements IBBNProvider { abstract getPublicKeyHex(): Promise; /** - * Gets the signing stargate client. - * @returns A promise that resolves to the signing stargate client. + * Gets the name of the wallet provider. + * @returns A promise that resolves to the name of the wallet provider. */ - abstract getSigningStargateClient(options?: SigningStargateClientOptions): Promise; + abstract getWalletProviderName(): Promise; + + /** + * Gets the icon URL of the wallet provider. + * @returns A promise that resolves to the icon URL of the wallet provider. + */ + abstract getWalletProviderIcon(): Promise; + /** - * Gets the balance of the connected wallet. - * @param searchDenom - The denomination to search for in the wallet's balance. - * @returns A promise that resolves to the balance of the connected wallet. + * Retrieves an offline signer that supports both Amino and Direct signing methods. + * This signer is used for signing transactions offline before broadcasting them to the network. + * + * @returns {Promise} A promise that resolves to a signer supporting both Amino and Direct signing + * @throws {Error} If wallet connection is not established or signer cannot be retrieved */ - abstract getBalance(searchDenom: string): Promise; + abstract getOfflineSigner(): Promise; } diff --git a/src/core/wallets/bbn/keplr/provider.ts b/src/core/wallets/bbn/keplr/provider.ts index cbcaa38..f8a4ec4 100644 --- a/src/core/wallets/bbn/keplr/provider.ts +++ b/src/core/wallets/bbn/keplr/provider.ts @@ -1,4 +1,3 @@ -import { SigningStargateClient, SigningStargateClientOptions } from "@cosmjs/stargate"; import { Window as KeplrWindow } from "@keplr-wallet/types"; import { OfflineAminoSigner, OfflineDirectSigner } from "@keplr-wallet/types/src/cosmjs"; import { Buffer } from "buffer"; @@ -6,6 +5,8 @@ import { Buffer } from "buffer"; import { BBNConfig, WalletInfo } from "@/core/types"; import { BBNProvider } from "@/core/wallets/bbn/BBNProvider"; +import logo from "./logo.svg"; + declare global { // eslint-disable-next-line @typescript-eslint/no-empty-object-type interface Window extends KeplrWindow {} @@ -15,8 +16,6 @@ export class KeplrProvider extends BBNProvider { private walletInfo: WalletInfo | undefined; private chainId: string | undefined; private rpc: string | undefined; - private offlineSigner?: OfflineAminoSigner & OfflineDirectSigner; - private stargateClient?: Promise; constructor( private keplr: Window["keplr"], @@ -39,8 +38,6 @@ export class KeplrProvider extends BBNProvider { if (!key) throw new Error("Failed to get Keplr key"); - this.offlineSigner = this.keplr?.getOfflineSigner(this.chainId); - const { bech32Address, pubKey } = key; if (bech32Address && pubKey) { @@ -63,21 +60,22 @@ export class KeplrProvider extends BBNProvider { return this.walletInfo.publicKeyHex; } - async getSigningStargateClient(options?: SigningStargateClientOptions) { - if (!this.stargateClient) { - this.stargateClient = this.createSigningStargateClient(options); - } - return await this.stargateClient; + async getWalletProviderName(): Promise { + return "Keplr"; } - async createSigningStargateClient(options?: SigningStargateClientOptions) { - if (!this.rpc) throw new Error("RPC URL is not initialized"); - if (!this.offlineSigner) throw new Error("Offline signer is not initialized"); - return await SigningStargateClient.connectWithSigner(this.rpc, this.offlineSigner, options); + async getWalletProviderIcon(): Promise { + return logo; } - async getBalance(searchDenom: string) { - const signingStargateClient = await this.getSigningStargateClient(); - return BigInt((await signingStargateClient.getBalance(await this.getAddress(), searchDenom)).amount); + async getOfflineSigner(): Promise { + if (!this.keplr) throw new Error("Keplr extension not found"); + if (!this.chainId) throw new Error("Chain ID is not initialized"); + + try { + return this.keplr.getOfflineSigner(this.chainId); + } catch { + throw new Error("Failed to get offline signer"); + } } } From 14f2401f69fc4565c2c0d621af9304199d6e434e Mon Sep 17 00:00:00 2001 From: Govard Barkhatov Date: Fri, 13 Dec 2024 13:54:58 +0200 Subject: [PATCH 2/2] chain data --- src/core/wallets/bbn/keplr/provider.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/wallets/bbn/keplr/provider.ts b/src/core/wallets/bbn/keplr/provider.ts index 832d203..cd30897 100644 --- a/src/core/wallets/bbn/keplr/provider.ts +++ b/src/core/wallets/bbn/keplr/provider.ts @@ -16,6 +16,7 @@ export class KeplrProvider extends BBNProvider { private walletInfo: WalletInfo | undefined; private chainId: string | undefined; private rpc: string | undefined; + private chainData: BBNConfig["chainData"]; constructor( private keplr: Window["keplr"], @@ -60,8 +61,6 @@ export class KeplrProvider extends BBNProvider { if (!key) throw new Error("Failed to get Keplr key"); - this.offlineSigner = this.keplr.getOfflineSigner(this.chainId); - const { bech32Address, pubKey } = key; if (bech32Address && pubKey) {