Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Leap - Cosmos #190

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/purple-books-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@babylonlabs-io/bbn-wallet-connect": patch
---

add babylon wallet - leap
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/core/wallets/bbn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { BBNConfig, ChainMetadata, IBBNProvider } from "@/core/types";
import icon from "./babylon.jpeg";
import injectable from "./injectable";
import keplr from "./keplr";
import leap from "./leap";

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

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

import logo from "./logo.svg";
import { LeapProvider, WALLET_PROVIDER_NAME } from "./provider";

const metadata: WalletMetadata<IBBNProvider, BBNConfig> = {
id: "leap",
name: WALLET_PROVIDER_NAME,
icon: logo,
docs: "https://www.leapwallet.io/",
wallet: "leap",
createProvider: (wallet, config) => new LeapProvider(wallet, config),
networks: [Network.MAINNET, Network.SIGNET],
};

export default metadata;
1 change: 1 addition & 0 deletions src/core/wallets/bbn/leap/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 98 additions & 0 deletions src/core/wallets/bbn/leap/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { OfflineAminoSigner, OfflineDirectSigner } from "@keplr-wallet/types/src/cosmjs";
import { Buffer } from "buffer";

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

import logo from "./logo.svg";

export const WALLET_PROVIDER_NAME = "Leap";

export class LeapProvider implements IBBNProvider {
private walletInfo: WalletInfo | undefined;
private chainId: string | undefined;
private rpc: string | undefined;
private chainData: BBNConfig["chainData"];

constructor(
private wallet: any,
config: BBNConfig,
) {
if (!wallet) {
throw new Error("Leap extension not found");
}
this.chainId = config.chainId;
this.rpc = config.rpc;
this.chainData = config.chainData;
}

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

try {
await this.wallet.enable(this.chainId);
} catch (error: Error | any) {
if (error?.message.includes(this.chainId) || error?.message.includes("chain id")) {
try {
// User has no BBN chain in their wallet
await this.wallet.experimentalSuggestChain(this.chainData);
gbarkhatov marked this conversation as resolved.
Show resolved Hide resolved
await this.wallet.enable(this.chainId);
} catch {
throw new Error("Failed to add BBN chain");
}
} else {
if (error?.message.includes("rejected")) {
throw new Error("Leap wallet connection request rejected");
} else if (error?.message.includes("context invalidated")) {
throw new Error("Leap extension context invalidated");
} else {
throw new Error(error?.message || "Failed to connect to Leap");
}
}
}
const key = await this.wallet.getKey(this.chainId);

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

const { bech32Address, pubKey } = key;

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

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 getWalletProviderName(): Promise<string> {
return WALLET_PROVIDER_NAME;
}

async getWalletProviderIcon(): Promise<string> {
return logo;
}

async getOfflineSigner(): Promise<OfflineAminoSigner & OfflineDirectSigner> {
if (!this.wallet) throw new Error("Leap extension not found");
if (!this.chainId) throw new Error("Chain ID is not initialized");

try {
return this.wallet.getOfflineSigner(this.chainId);
} catch {
throw new Error("Failed to get offline signer");
}
}
}
1 change: 0 additions & 1 deletion src/core/wallets/btc/unisat/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export class UnisatProvider implements IBTCProvider {
}

connectWallet = async (): Promise<void> => {
console.log("new version 18");
let accounts;
try {
accounts = await this.provider.requestAccounts();
Expand Down