Skip to content

Commit

Permalink
Merge pull request #157 from Xiphiar/update-readme
Browse files Browse the repository at this point in the history
Add Ledger integration example to readme
  • Loading branch information
assafmo authored Oct 2, 2023
2 parents b0aa682 + b0dcfc1 commit 5cf2450
Showing 1 changed file with 58 additions and 8 deletions.
66 changes: 58 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- [Deep linking](#deep-linking)
- [Leap Cosmos Wallet](#leap-cosmos-wallet)
- [StarShell Wallet](#starshell-wallet)
- [Ledger Wallet](#ledger-wallet)
- [API](#api)
- [Wallet](#wallet)
- [Importing account from mnemonic](#importing-account-from-mnemonic)
Expand Down Expand Up @@ -104,18 +105,18 @@ Follow the instruction of [react-native-get-random-values](https://www.npmjs.com

# Usage Examples

Note: Public gRPC-web endpoints can be found in https://github.com/scrtlabs/api-registry for both mainnet and testnet.
Note: Public LCD endpoints can be found in https://github.com/scrtlabs/api-registry for both mainnet and testnet.

For a lot more usage examples [refer to the tests](./test/test.ts).

## Sending Queries

```ts
import { SecretNetworkClient, grpc } from "secretjs";
import { SecretNetworkClient } from "secretjs";

const url = "TODO get from https://github.com/scrtlabs/api-registry";

// To create a readonly secret.js client, just pass in a gRPC-web endpoint
// To create a readonly secret.js client, just pass in a LCD endpoint
const secretjs = new SecretNetworkClient({
url,
chainId: "secret-4",
Expand Down Expand Up @@ -210,7 +211,7 @@ Notes:
1. MetaMask supports mobile!
2. MetaMask supports Ledger.
3. You might want to pass `encryptionSeed` to `SecretNetworkClient.create()` to use the same encryption key for the user across sessions. This value should be a true random 32 byte number that is stored securly in your app, such that only the user can decrypt it. This can also be a `sha256(user_password)` but might impair UX.
4. See Keplr's [`getOfflineSignerOnlyAmino()`](#getofflinesigneronlyamino) for list of unsupported transactions.
4. See Keplr's [`getOfflineSignerOnlyAmino()`](#windowkeplrgetofflinesigneronlyamino) for list of unsupported transactions.

<img src="./media/metamask-signing-example.jpg" width="55%" style="border-radius: 10px;" />

Expand Down Expand Up @@ -269,9 +270,9 @@ Links:

TLDR:

- [`getOfflineSignerOnlyAmino()`](#getofflinesigneronlyamino): The recommended way. Supports Ledger, has a nice UI.
- [`getOfflineSigner()`](#getofflinesigner): No Ledger support, ugly UI, can send IBC **relayer** txs and submit IBC gov proposals.
- [`getOfflineSignerAuto()`](#getofflinesignerauto): If Ledger alias for `getOfflineSignerOnlyAmino()`, otherwise alias for `getOfflineSigner()`.
- [`getOfflineSignerOnlyAmino()`](#windowkeplrgetofflinesigneronlyamino): The recommended way. Supports Ledger, has a nice UI.
- [`getOfflineSigner()`](#windowkeplrgetofflinesigner): No Ledger support, ugly UI, can send IBC **relayer** txs and submit IBC gov proposals.
- [`getOfflineSignerAuto()`](#windowkeplrgetofflinesignerauto): If Ledger alias for `getOfflineSignerOnlyAmino()`, otherwise alias for `getOfflineSigner()`.

#### `window.keplr.getOfflineSignerOnlyAmino()`

Expand Down Expand Up @@ -382,6 +383,55 @@ Links:

- <a href="https://starshell.net" target="_blank"><strong>Official StarShell Website »</strong></a>

## Ledger Wallet

`@cosmjs/ledger-amino` can be used to sign transactions with a Ledger wallet running the Cosmos app.

```ts
import { SecretNetworkClient } from 'secretjs';
import { makeCosmoshubPath } from "@cosmjs/amino";
import { LedgerSigner } from "@cosmjs/ledger-amino";

// NodeJS only
import TransportNodeHid from "@ledgerhq/hw-transport-node-hid";

// Browser only
//import TransportNodeHid from "@ledgerhq/hw-transport-webusb";

const interactiveTimeout = 120_000;
const accountIndex = 0;
const cosmosPath = makeCosmoshubPath(accountIndex);

const ledgerTransport = await TransportNodeHid.create(interactiveTimeout, interactiveTimeout);
const ledgerSigner = new LedgerSigner(
ledgerTransport,
{
testModeAllowed: true,
hdPaths: [cosmosPath],
prefix: 'secret'
}
);
const [{ address }] = await signer.getAccounts();

const client = new SecretNetworkClient({
url: "TODO get from https://github.com/scrtlabs/api-registry",
chainId: "secret-4",
wallet: ledgerSigner,
walletAddress: address,
});
```

Notes:

1. Use the appropriate `hw-transport` package for your environment (Node or Browser)
2. The Ledger Cosmos app only supports coin type 118
3. You might want to pass `encryptionSeed` to `SecretNetworkClient.create()` to use the same encryption key for the user across sessions. This value should be a true random 32 byte number that is stored securly in your app, such that only the user can decrypt it. This can also be a `sha256(user_password)` but might impair UX.
4. See Keplr's [`getOfflineSignerOnlyAmino()`](#windowkeplrgetofflinesigneronlyamino) for list of unsupported transactions.

Links:

- <a href="https://cosmos.github.io/cosmjs/latest/ledger-amino/" target="_blank"><strong>@cosmjs/ledger-amino Documentation »</strong></a>

# API

## Wallet
Expand Down Expand Up @@ -424,7 +474,7 @@ import { SecretNetworkClient } from "secretjs";

const url = "TODO get from https://github.com/scrtlabs/api-registry";

// To create a readonly secret.js client, just pass in a gRPC-web endpoint
// To create a readonly secret.js client, just pass in a LCD endpoint
const secretjs = new SecretNetworkClient({
chainId: "secret-4",
url,
Expand Down

0 comments on commit 5cf2450

Please sign in to comment.