Cosmos
Develop Cosmos based chains dApps
XDEFI Wallet integrates Cosmos based chains.
It allows dApps to be developed on Cosmos based chains.
It injects a Keplr Provider in
window.xfi.keplr
and if Keplr isn't instantiated, takes Keplr wallet window.keplr
object and allow you to use its basic API via CosmJS.window.onload = async () => {
if (!window.xfi?.keplr) {
alert("Please install XDEFI extension");
} else {
const chainId = "cosmoshub-4";
const keplr = window.xfi.keplr;
// Enabling before using the Keplr is recommended.
// This method will ask the user whether to allow access if they haven't visited this website.
// Also, it will request that the user unlock the wallet if the wallet is locked.
await keplr.enable(chainId);
const offlineSigner = keplr.getOfflineSigner(chainId);
// You can get the address/public keys by `getAccounts` method.
// It can return the array of address/public key.
// But, currently, Keplr extension manages only one address/public key pair.
// XXX: This line is needed to set the sender address for SigningCosmosClient.
const accounts = await offlineSigner.getAccounts();
// Initialize the gaia api with the offline signer that is injected by Keplr extension.
const cosmJS = new SigningCosmosClient(
"https://lcd-cosmoshub.keplr.app",
accounts[0].address,
offlineSigner,
);
}
}
If you’d like to differentiate Keplr and XDEFI
window.keplr
, you can use window.keplr.isXDEFI
to check if it’s XDEFI’s Keplr provider.if (window.keplr.isXDEFI) {
// this is XDEFI's Keplr provider
} else {
// this is Keplr's Keplr provider
}
As of release v26, XDEFI supports the following chains (chainId).
- Cosmos Hub,
cosmoshub-4
- Osmosis,
osmosis-1
- Axelar,
axelar-dojo-1
- JUNO,
juno-1
- Crescent,
crescent-1
- KAVA,
kava_2222-10
- Stargaze,
stargaze-1
- Akash,
akashnet-2
- Crypto Org,
crypto-org-chain-mainnet-1
- Kujira,
kaiyo-1
- Sei Testnet,
atlantic-2
- Stride,
stride-1
- Mars Protocol,
mars-1
import { Window as KeplrWindow } from "@keplr-wallet/types";
declare global {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Window extends KeplrWindow {}
}
The
@keplr-wallet/types
package has the type definition related to Keplr. If you’re using TypeScript, run npm install --save-dev @keplr-wallet/types
or yarn add -D @keplr-wallet/types
to install @keplr-wallet/types
. Then, you can add the @keplr-wallet/types
window to a global window object and register the Keplr related types.enable(chainIds: string[]): Promise<void>
The
window.keplr.enable(chainIds)
method requests the extension to be unlocked if it’s currently locked. If the user hasn’t given permission to the webpage, it will ask the user to give permission for the webpage to access XDEFI.getKey(chainId: string): Promise<{
// Name of the selected key store.
name: string;
algo: string;
pubKey: Uint8Array;
address: Uint8Array;
bech32Address: string;
}>
signAmino(chainId: string, signer: string, signDoc: StdSignDoc): Promise<AminoSignResponse>
Similar to CosmJS OfflineSigner’s signAmino, but Keplr’s signAmino takes the chain-id as a required parameter. Signs Amino-encoded StdSignDoc.
signDirect(chainId:string, signer:string, signDoc: {
/** SignDoc bodyBytes */
bodyBytes?: Uint8Array | null;
/** SignDoc authInfoBytes */
authInfoBytes?: Uint8Array | null;
/** SignDoc chainId */
chainId?: string | null;
/** SignDoc accountNumber */
accountNumber?: Long | null;
}): Promise<DirectSignResponse>
Similar to CosmJS OfflineDirectSigner’s signDirect, but Keplr’s signDirect takes the chain-id as a required parameter. Signs Proto-encoded StdSignDoc.
sendTx(
chainId: string,
tx: Uint8Array,
mode: BroadcastMode
): Promise<Uint8Array>;
This function requests Keplr to delegates the broadcasting of the transaction to Keplr’s LCD endpoints (rather than the webpage broadcasting the transaction). This method returns the transaction hash if it succeeds to broadcast, if else the method will throw an error. When Keplr broadcasts the transaction, Keplr will send the notification on the transaction’s progress.
signArbitrary(
chainId: string,
signer: string,
data: string | Uint8Array
): Promise<StdSignature>;
verifyArbitrary(
chainId: string,
signer: string,
data: string | Uint8Array,
signature: StdSignature
): Promise<boolean>;
We don’t support yet (13-Jan-2023)
signEthereum
- Secret Network / SecretJS
- Suggest Chain
experimentalSuggestChain
Connect to XDEFI’s Keplr Provider using CosmJS
// Enabling before using the Keplr is recommended.
// This method will ask the user whether or not to allow access if they haven't visited this website.
// Also, it will request user to unlock the wallet if the wallet is locked.
await window.keplr.enable(chainId);
const offlineSigner = window.getOfflineSigner(chainId);
// You can get the address/public keys by `getAccounts` method.
// It can return the array of address/public key.
// But, currently, Keplr extension manages only one address/public key pair.
// XXX: This line is needed to set the sender address for SigningCosmosClient.
const accounts = await offlineSigner.getAccounts();
// Initialize the gaia api with the offline signer that is injected by Keplr extension.
const cosmJS = new SigningCosmosClient(
"https://lcd-cosmoshub.keplr.app/rest",
accounts[0].address,
offlineSigner,
);
Last modified 30d ago