Skip to content

Commit

Permalink
Tong/416 do not rollup cosmo (#70)
Browse files Browse the repository at this point in the history
* Revert "fix: keplr poc"

This reverts commit eec7db6.

* do not rollup @cosmjs/stargate because it does not work with pre-rendering in NextJs
  • Loading branch information
supertong authored Nov 30, 2024
1 parent 5baf871 commit 651f91f
Show file tree
Hide file tree
Showing 10 changed files with 1,225 additions and 138 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-donkeys-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@babylonlabs-io/bbn-wallet-connect": patch
---

Re-add @cosmjs/stargate
1,129 changes: 1,101 additions & 28 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"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 @@ -38,10 +40,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"
"tailwind-merge": "^2.5.4",
"@babylonlabs-io/bbn-core-ui": "^0.2.0"
},
"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: any = {
export const bbnDevnet: ChainInfo = {
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: any;
chainData: ChainInfo;
};

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<any>;
abstract getSigningStargateClient(): Promise<SigningStargateClient>;
/**
* 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: [],
wallets: [keplr],
};

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);
}
}
9 changes: 8 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ export default defineConfig({
fileName: (format) => `index.${format}.js`,
},
rollupOptions: {
external: ["react", "react-dom", "react/jsx-runtime", "tailwind-merge", "@babylonlabs-io/bbn-core-ui"],
external: [
"react",
"react-dom",
"react/jsx-runtime",
"tailwind-merge",
"@babylonlabs-io/bbn-core-ui",
"@cosmjs/stargate",
],
output: {
sourcemapExcludeSources: true,
},
Expand Down

0 comments on commit 651f91f

Please sign in to comment.