This repository has been archived by the owner on Dec 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from CosmWasm/helper-methods
Adding some setup functions
- Loading branch information
Showing
6 changed files
with
294 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/** | ||
* Setup helper functions | ||
* | ||
* These are multiple helper function to get quickly started. | ||
* There are currently 4 different functions to choose from: | ||
* (a) Web / Keplr | ||
* (b) Web / Ledger | ||
* (c) Node / Local Mnemonic | ||
* (d) Node / Ledger | ||
*/ | ||
import { makeCosmoshubPath } from "../amino"; | ||
import { SigningCosmWasmClient } from "../cosmwasm-stargate"; | ||
import { LedgerSigner } from "../ledger-amino"; | ||
import { DirectSecp256k1HdWallet } from "../proto-signing"; | ||
import { GasPrice } from "../stargate"; | ||
|
||
/** | ||
* All setup functions are using the same config pattern | ||
*/ | ||
interface Config { | ||
chainId: string; | ||
rpcEndpoint: string; | ||
prefix: string; | ||
gasPrice?: GasPrice; | ||
} | ||
|
||
// Window has to be re-declared to get keplr working | ||
declare const window: any; | ||
|
||
/** | ||
* (a) Web / Keplr | ||
* Prompts keplr and returns a signing client after the user | ||
* gave permissions. | ||
* | ||
* @param config | ||
* @returns SigningCosmWasmClient | ||
*/ | ||
export async function setupWebKeplr(config: Config): Promise<SigningCosmWasmClient> { | ||
// check browser compatibility | ||
if (!window.keplr) { | ||
throw new Error("Keplr is not supported or installed on this browser!"); | ||
} | ||
|
||
// try to enable keplr with given chainId | ||
await window.keplr.enable(config.chainId).catch(() => { | ||
throw new Error("Keplr can't connect to this chainId!"); | ||
}); | ||
|
||
const { prefix, gasPrice } = config; | ||
|
||
// Setup signer | ||
const offlineSigner = await window.getOfflineSignerAuto(config.chainId); | ||
|
||
// Init SigningCosmWasmClient client | ||
const signingClient = await SigningCosmWasmClient.connectWithSigner(config.rpcEndpoint, offlineSigner, { | ||
prefix, | ||
gasPrice, | ||
}); | ||
|
||
return signingClient; | ||
} | ||
|
||
/** | ||
* (b) Web / Ledger | ||
* Returns a signing client after the user gave permissions. | ||
* | ||
* @param config | ||
* @returns SigningCosmWasmClient | ||
*/ | ||
export async function setupWebLedger(config: Config, transport: any): Promise<SigningCosmWasmClient> { | ||
const { prefix, gasPrice } = config; | ||
const interactiveTimeout = 120_000; | ||
|
||
// Prepare ledger | ||
const ledgerTransport = await transport.create(interactiveTimeout, interactiveTimeout); | ||
|
||
// Setup signer | ||
const offlineSigner = new LedgerSigner(ledgerTransport, { | ||
hdPaths: [makeCosmoshubPath(0)], | ||
prefix: prefix, | ||
}); | ||
|
||
// Init SigningCosmWasmClient client | ||
const client = await SigningCosmWasmClient.connectWithSigner(config.rpcEndpoint, offlineSigner, { | ||
prefix, | ||
gasPrice, | ||
}); | ||
|
||
const chainId = await client.getChainId(); | ||
|
||
if (chainId !== config.chainId) { | ||
throw Error("Given ChainId doesn't match the clients ChainID!"); | ||
} | ||
|
||
return client; | ||
} | ||
|
||
/** | ||
* (c) Node / Local Mnemonic | ||
* Using a local mnemonic and returns a signing clien | ||
* | ||
* @param config | ||
* @param mnemonic | ||
* @returns SigningCosmWasmClient | ||
*/ | ||
export async function setupNodeLocal(config: Config, mnemonic: string): Promise<SigningCosmWasmClient> { | ||
const { prefix, gasPrice } = config; | ||
|
||
// Setup signer | ||
const offlineSigner = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix }); | ||
|
||
// Init SigningCosmWasmClient client | ||
const client = await SigningCosmWasmClient.connectWithSigner(config.rpcEndpoint, offlineSigner, { | ||
prefix, | ||
gasPrice, | ||
}); | ||
|
||
const chainId = await client.getChainId(); | ||
|
||
if (chainId !== config.chainId) { | ||
throw Error("Given ChainId doesn't match the clients ChainID!"); | ||
} | ||
|
||
return client; | ||
} | ||
|
||
/** | ||
* (d) Node / Ledger | ||
* Returns a signing client after the user gave permissions. | ||
* | ||
* @param config | ||
* @returns SigningCosmWasmClient | ||
*/ | ||
export async function setupNodeLedger(config: Config, transport: any): Promise<SigningCosmWasmClient> { | ||
const { prefix, gasPrice } = config; | ||
const interactiveTimeout = 120_000; | ||
|
||
// Prepare ledger | ||
const ledgerTransport = await transport.create(interactiveTimeout, interactiveTimeout); | ||
|
||
// Setup signer | ||
const offlineSigner = new LedgerSigner(ledgerTransport, { | ||
hdPaths: [makeCosmoshubPath(0)], | ||
prefix: prefix, | ||
}); | ||
|
||
const client = await SigningCosmWasmClient.connectWithSigner(config.rpcEndpoint, offlineSigner, { | ||
prefix: prefix, | ||
gasPrice: gasPrice, | ||
}); | ||
|
||
const chainId = await client.getChainId(); | ||
|
||
if (chainId !== config.chainId) { | ||
throw Error("Given ChainId doesn't match the clients ChainID!"); | ||
} | ||
|
||
return client; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
/** | ||
* Exporting all the defined CosmJS symbols | ||
*/ | ||
export * from "./amino"; | ||
export * from "./cosmwasm-stargate"; | ||
export * from "./crypto"; | ||
export * from "./encoding"; | ||
export * from "./faucet-client"; | ||
export * from "./proto-signing"; | ||
export * from "./stargate"; | ||
|
||
/** | ||
* Exporting CosmWasmJS Helpers | ||
*/ | ||
export { setupNodeLedger, setupNodeLocal, setupWebKeplr, setupWebLedger } from "./helpers/setup"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { LedgerSigner } from "@cosmjs/ledger-amino"; |
Oops, something went wrong.