Skip to content

Commit

Permalink
Inject windoe.bbnwallet
Browse files Browse the repository at this point in the history
  • Loading branch information
Thunnini committed Jan 8, 2025
1 parent 838ea6b commit 402bafc
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
99 changes: 99 additions & 0 deletions packages/provider/src/babylon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import {
IBBNProvider,
Keplr,
OfflineAminoSigner,
OfflineDirectSigner,
} from "@keplr-wallet/types";

const BabylonChainInfo = {
chainId: "bbn-test-5",
chainName: "Babylon Phase-2 Testnet",
chainSymbolImageUrl:
"https://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/bbn-test/chain.png",
rpc: "https://babylon-testnet-rpc.nodes.guru",
rest: "https://babylon-testnet-api.nodes.guru",
nodeProvider: {
name: "NodesGuru",
email: "[email protected]",
website: "https://nodes.guru/",
},
bip44: {
coinType: 118,
},
bech32Config: {
bech32PrefixAccAddr: "bbn",
bech32PrefixAccPub: "bbnpub",
bech32PrefixValAddr: "bbnvaloper",
bech32PrefixValPub: "bbnvaloperpub",
bech32PrefixConsAddr: "bbnvalcons",
bech32PrefixConsPub: "bbnvalconspub",
},
currencies: [
{
coinDenom: "BABY",
coinMinimalDenom: "ubbn",
coinDecimals: 6,
coinImageUrl:
"https://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/bbn-test/chain.png",
},
],
feeCurrencies: [
{
coinDenom: "BABY",
coinMinimalDenom: "ubbn",
coinDecimals: 6,
coinImageUrl:
"https://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/bbn-test/chain.png",
gasPriceStep: {
low: 0.007,
average: 0.007,
high: 0.01,
},
},
],
stakeCurrency: {
coinDenom: "BABY",
coinMinimalDenom: "ubbn",
coinDecimals: 6,
coinImageUrl:
"https://raw.githubusercontent.com/chainapsis/keplr-chain-registry/main/images/bbn-test/chain.png",
},
features: ["cosmwasm"],
};

export class KeplrBBNProvider implements IBBNProvider {
constructor(protected readonly keplr: Keplr) {}

async connectWallet(): Promise<void> {
await this.keplr.experimentalSuggestChain(BabylonChainInfo);
await this.keplr.enable(BabylonChainInfo.chainId);
}

async getAddress(): Promise<string> {
await this.keplr.experimentalSuggestChain(BabylonChainInfo);
await this.keplr.enable(BabylonChainInfo.chainId);
return (await this.keplr.getKey(BabylonChainInfo.chainId)).bech32Address;
}

async getOfflineSigner(): Promise<OfflineAminoSigner & OfflineDirectSigner> {
return this.keplr.getOfflineSigner(BabylonChainInfo.chainId);
}

async getPublicKeyHex(): Promise<string> {
await this.keplr.experimentalSuggestChain(BabylonChainInfo);
await this.keplr.enable(BabylonChainInfo.chainId);
return Array.from(
(await this.keplr.getKey(BabylonChainInfo.chainId)).pubKey
)
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("");
}

async getWalletProviderIcon(): Promise<string> {
return process.env["KEPLR_EXT_EIP6963_PROVIDER_INFO_ICON"] || "";
}

async getWalletProviderName(): Promise<string> {
return "Keplr";
}
}
6 changes: 6 additions & 0 deletions packages/provider/src/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
InvocationsSignerDetails,
ProviderInterface,
} from "starknet";
import { KeplrBBNProvider } from "./babylon";

export interface ProxyRequest {
type: "proxy-request";
Expand Down Expand Up @@ -105,6 +106,11 @@ export function injectKeplrToWindow(keplr: IKeplr): void {
);

defineUnwritablePropertyIfPossible(window, "starknet_keplr", keplr.starknet);
defineUnwritablePropertyIfPossible(
window,
"bbnwallet",
new KeplrBBNProvider(keplr)
);
}

/**
Expand Down
43 changes: 43 additions & 0 deletions packages/types/src/babylon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { OfflineAminoSigner, OfflineDirectSigner } from "./cosmjs";

export interface IBBNProvider {
/**
* Connects to the wallet and returns the instance of the wallet provider.
* @returns A promise that resolves to an instance of the wrapper wallet provider.
* @throws An error if the wallet is not installed or if connection fails.
*/
connectWallet(): Promise<void>;

/**
* Gets the address of the connected wallet.
* @returns A promise that resolves to the address of the connected wallet.
*/
getAddress(): Promise<string>;

/**
* Gets the public key of the connected wallet.
* @returns A promise that resolves to the public key of the connected wallet.
*/
getPublicKeyHex(): Promise<string>;

/**
* Gets the name of the wallet provider.
* @returns A promise that resolves to the name of the wallet provider.
*/
getWalletProviderName(): Promise<string>;

/**
* Gets the icon of the wallet provider.
* @returns A promise that resolves to the icon of the wallet provider.
*/
getWalletProviderIcon(): Promise<string>;

/**
* 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<OfflineAminoSigner & OfflineDirectSigner>} 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<OfflineAminoSigner & OfflineDirectSigner>;
}
1 change: 1 addition & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from "./cosmjs";
export * from "./cosmjs-alt";
export * from "./secretjs";
export * from "./settled";
export * from "./babylon";

0 comments on commit 402bafc

Please sign in to comment.