Skip to content

Commit

Permalink
fix: keplr poc (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
totraev authored Nov 29, 2024
1 parent 3b052e4 commit d064396
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 1,213 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-dolphins-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@babylonlabs-io/bbn-wallet-connect": patch
---

keplr poc
1,131 changes: 29 additions & 1,102 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
"release": "npm run build && changeset publish"
},
"dependencies": {
"@cosmjs/stargate": "^0.32.4",
"@keplr-wallet/types": "^0.12.156",
"buffer": "^6.0.3",
"nanoevents": "^9.1.0",
"react-icons": "^5.3.0"
Expand All @@ -40,10 +38,10 @@
"dist"
],
"peerDependencies": {
"@babylonlabs-io/bbn-core-ui": "^0.2.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"tailwind-merge": "^2.5.4",
"@babylonlabs-io/bbn-core-ui": "^0.2.0"
"tailwind-merge": "^2.5.4"
},
"devDependencies": {
"@changesets/cli": "^2.27.9",
Expand Down
4 changes: 2 additions & 2 deletions src/core/chains/bbnDevnet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ChainInfo } from "@keplr-wallet/types";
// import { ChainInfo } from "@keplr-wallet/types";

export const bbnDevnet: ChainInfo = {
export const bbnDevnet: any = {
chainId: "devnet-7",
chainName: "Babylon Devnet 7",
chainSymbolImageUrl:
Expand Down
4 changes: 2 additions & 2 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainInfo } from "@keplr-wallet/types";
// import { ChainInfo } from "@keplr-wallet/types";

export type Fees = {
// fee for inclusion in the next block
Expand Down Expand Up @@ -55,7 +55,7 @@ export interface BTCConfig {
export type BBNConfig = {
chainId: string;
rpc: string;
chainData: ChainInfo;
chainData: any;
};

export interface IProvider {
Expand Down
4 changes: 2 additions & 2 deletions src/core/wallets/bbn/BBNProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IProvider } from "@/core/types";
import { SigningStargateClient } from "@cosmjs/stargate";
// import { SigningStargateClient } from "@cosmjs/stargate";

export abstract class BBNProvider implements IProvider {
/**
Expand All @@ -25,7 +25,7 @@ export abstract class BBNProvider implements IProvider {
* Gets the signing stargate client.
* @returns A promise that resolves to the signing stargate client.
*/
abstract getSigningStargateClient(): Promise<SigningStargateClient>;
abstract getSigningStargateClient(): Promise<any>;
/**
* Gets the balance of the connected wallet.
* @param searchDenom - The denomination to search for in the wallet's balance.
Expand Down
4 changes: 2 additions & 2 deletions src/core/wallets/bbn/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BBNProvider } from "./BBNProvider";
import icon from "./babylon.jpeg";
import keplr from "./keplr";
// import keplr from "./keplr";

import { BBNConfig, ChainMetadata } from "@/core/types";

const metadata: ChainMetadata<"BBN", BBNProvider, BBNConfig> = {
chain: "BBN",
name: "Babylon Chain",
icon,
wallets: [keplr],
wallets: [],
};

export default metadata;
28 changes: 14 additions & 14 deletions src/core/wallets/bbn/keplr/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Network, type WalletMetadata, type BBNConfig } from "@/core/types";
// import { Network, type WalletMetadata, type BBNConfig } from "@/core/types";

import logo from "./logo.svg";
import type { BBNProvider } from "../BBNProvider";
import { KeplrProvider } from "./provider";
// import logo from "./logo.svg";
// import type { BBNProvider } from "../BBNProvider";
// import { KeplrProvider } from "./provider";

const metadata: WalletMetadata<BBNProvider, BBNConfig> = {
id: "keplr",
name: "Keplr",
icon: logo,
docs: "https://www.keplr.app/",
wallet: "keplr",
createProvider: (wallet, config) => new KeplrProvider(wallet, config),
networks: [Network.MAINNET, Network.SIGNET],
};
// const metadata: WalletMetadata<BBNProvider, BBNConfig> = {
// id: "keplr",
// name: "Keplr",
// icon: logo,
// docs: "https://www.keplr.app/",
// wallet: "keplr",
// createProvider: (wallet, config) => new KeplrProvider(wallet, config),
// networks: [Network.MAINNET, Network.SIGNET],
// };

export default metadata;
// export default metadata;
170 changes: 85 additions & 85 deletions src/core/wallets/bbn/keplr/provider.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,85 @@
import { Window as KeplrWindow } from "@keplr-wallet/types";
import { Buffer } from "buffer";

import { BBNConfig, WalletInfo } from "@/core/types";
import { BBNProvider } from "@/core/wallets/bbn/BBNProvider";

import { OfflineAminoSigner, OfflineDirectSigner } from "@keplr-wallet/types/src/cosmjs";
import { SigningStargateClient, SigningStargateClientOptions } from "@cosmjs/stargate";

declare global {
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface Window extends KeplrWindow {}
}

export class KeplrProvider extends BBNProvider {
private walletInfo: WalletInfo | undefined;
private chainId: string | undefined;
private rpc: string | undefined;
private offlineSigner?: OfflineAminoSigner & OfflineDirectSigner;
private stargateClient?: Promise<SigningStargateClient>;

constructor(
private keplr: Window["keplr"],
config: BBNConfig,
) {
super();
if (!keplr) {
throw new Error("Keplr extension not found");
}
this.chainId = config.chainId;
this.rpc = config.rpc;
}

async connectWallet(): Promise<this> {
if (!this.chainId) throw new Error("Chain ID is not initialized");
if (!this.rpc) throw new Error("RPC URL is not initialized");

await this.keplr?.enable(this.chainId);
const key = await this.keplr?.getKey(this.chainId);

if (!key) throw new Error("Failed to get Keplr key");

this.offlineSigner = this.keplr?.getOfflineSigner(this.chainId);

const { bech32Address, pubKey } = key;

if (bech32Address && pubKey) {
this.walletInfo = {
publicKeyHex: Buffer.from(key.pubKey).toString("hex"),
address: bech32Address,
};
return this;
} else {
throw new Error("Could not connect to Keplr");
}
}

async getAddress(): Promise<string> {
if (!this.walletInfo) throw new Error("Wallet not connected");
return this.walletInfo.address;
}

async getPublicKeyHex(): Promise<string> {
if (!this.walletInfo) throw new Error("Wallet not connected");
return this.walletInfo.publicKeyHex;
}

async getSigningStargateClient(options?: SigningStargateClientOptions) {
if (!this.stargateClient) {
this.stargateClient = this.createSigningStargateClient(options);
}
return await this.stargateClient;
}

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 getBalance(searchDenom: string) {
const signingStargateClient = await this.getSigningStargateClient();
return BigInt((await signingStargateClient.getBalance(await this.getAddress(), searchDenom)).amount);
}
}
// import { Window as KeplrWindow } from "@keplr-wallet/types";
// import { Buffer } from "buffer";

// import { BBNConfig, WalletInfo } from "@/core/types";
// import { BBNProvider } from "@/core/wallets/bbn/BBNProvider";

// import { OfflineAminoSigner, OfflineDirectSigner } from "@keplr-wallet/types/src/cosmjs";
// import { SigningStargateClient, SigningStargateClientOptions } from "@cosmjs/stargate";

// declare global {
// // eslint-disable-next-line @typescript-eslint/no-empty-object-type
// interface Window extends KeplrWindow {}
// }

// export class KeplrProvider extends BBNProvider {
// private walletInfo: WalletInfo | undefined;
// private chainId: string | undefined;
// private rpc: string | undefined;
// private offlineSigner?: OfflineAminoSigner & OfflineDirectSigner;
// private stargateClient?: Promise<SigningStargateClient>;

// constructor(
// private keplr: Window["keplr"],
// config: BBNConfig,
// ) {
// super();
// if (!keplr) {
// throw new Error("Keplr extension not found");
// }
// this.chainId = config.chainId;
// this.rpc = config.rpc;
// }

// async connectWallet(): Promise<this> {
// if (!this.chainId) throw new Error("Chain ID is not initialized");
// if (!this.rpc) throw new Error("RPC URL is not initialized");

// await this.keplr?.enable(this.chainId);
// const key = await this.keplr?.getKey(this.chainId);

// if (!key) throw new Error("Failed to get Keplr key");

// this.offlineSigner = this.keplr?.getOfflineSigner(this.chainId);

// const { bech32Address, pubKey } = key;

// if (bech32Address && pubKey) {
// this.walletInfo = {
// publicKeyHex: Buffer.from(key.pubKey).toString("hex"),
// address: bech32Address,
// };
// return this;
// } else {
// throw new Error("Could not connect to Keplr");
// }
// }

// async getAddress(): Promise<string> {
// if (!this.walletInfo) throw new Error("Wallet not connected");
// return this.walletInfo.address;
// }

// async getPublicKeyHex(): Promise<string> {
// if (!this.walletInfo) throw new Error("Wallet not connected");
// return this.walletInfo.publicKeyHex;
// }

// async getSigningStargateClient(options?: SigningStargateClientOptions) {
// if (!this.stargateClient) {
// this.stargateClient = this.createSigningStargateClient(options);
// }
// return await this.stargateClient;
// }

// 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 getBalance(searchDenom: string) {
// const signingStargateClient = await this.getSigningStargateClient();
// return BigInt((await signingStargateClient.getBalance(await this.getAddress(), searchDenom)).amount);
// }
// }

0 comments on commit d064396

Please sign in to comment.