From e095c1561e42acac28ece61fe14847c0175151d0 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Wed, 16 Sep 2020 12:45:22 +0200 Subject: [PATCH] launchpad-ledger: Add multiple accounts to Node.js demo --- packages/launchpad-ledger/demo/node.js | 18 +++-- packages/launchpad-ledger/src/demo/node.ts | 76 +++++++++++++--------- 2 files changed, 59 insertions(+), 35 deletions(-) diff --git a/packages/launchpad-ledger/demo/node.js b/packages/launchpad-ledger/demo/node.js index 345c531f..a703a712 100644 --- a/packages/launchpad-ledger/demo/node.js +++ b/packages/launchpad-ledger/demo/node.js @@ -5,10 +5,20 @@ async function run() { console.info("Accounts from Ledger device:"); console.table(accounts); - const address = accounts[0].address; - const signature = await demo.sign(address, address); - console.info("Signature from Ledger device:"); - console.info(signature); + const accountNumber0 = 0; + const address0 = accounts[accountNumber0].address; + const signature0 = await demo.sign(accountNumber0, address0, address0); + console.info(`Signature from Ledger device for account number 0 (${address0}):`); + console.info(signature0); + + // It seems the Ledger device needs a bit of time to recover + await new Promise((resolve) => setTimeout(resolve, 1000)); + + const accountNumber1 = 1; + const address1 = accounts[accountNumber1].address; + const signature1 = await demo.sign(accountNumber1, address1, address1); + console.info(`Signature from Ledger device for account number 1 (${address1}):`); + console.info(signature1); } run().catch(console.error); diff --git a/packages/launchpad-ledger/src/demo/node.ts b/packages/launchpad-ledger/src/demo/node.ts index 3de64c36..86483529 100644 --- a/packages/launchpad-ledger/src/demo/node.ts +++ b/packages/launchpad-ledger/src/demo/node.ts @@ -1,33 +1,22 @@ -import { toHex, toUtf8 } from "@cosmjs/encoding"; -import { StdSignature } from "@cosmjs/launchpad"; +/* eslint-disable @typescript-eslint/naming-convention */ +import { toBase64 } from "@cosmjs/encoding"; +import { makeCosmoshubPath, makeSignBytes, StdFee, StdSignature } from "@cosmjs/launchpad"; import { LedgerSigner } from "../ledgersigner"; -function createMessage(fromAddress: string, toAddress: string): string { - return `{ - "account_number": 0, - "chain_id": "testing", - "fee": { - "amount": [{ "amount": 100, "denom": "ucosm" }], - "gas": 250 - }, - "memo": "Some memo", - "msgs": [{ - "type": "cosmos-sdk/MsgSend", - "value": { - "amount": [{ - "amount": "1234567", - "denom": "ucosm" - }], - "from_address": "${fromAddress}", - "to_address": "${toAddress}" - } - }], - "sequence": 0 - }`; -} +const defaultChainId = "testing"; +const defaultFee: StdFee = { + amount: [{ amount: "100", denom: "ucosm" }], + gas: "250", +}; +const defaultMemo = "Some memo"; +const defaultSequence = "0"; +const defaultPrehashType = undefined; -const signer = new LedgerSigner({ testModeAllowed: true }); +const signer = new LedgerSigner({ + testModeAllowed: true, + hdPaths: [makeCosmoshubPath(0), makeCosmoshubPath(1), makeCosmoshubPath(2)], +}); export async function getAccounts(): Promise< ReadonlyArray<{ @@ -37,11 +26,36 @@ export async function getAccounts(): Promise< }> > { const accounts = await signer.getAccounts(); - return accounts.map((account) => ({ ...account, pubkey: toHex(account.pubkey) })); + return accounts.map((account) => ({ ...account, pubkey: toBase64(account.pubkey) })); } -export async function sign(fromAddress: string, toAddress: string): Promise { - const rawMessage = createMessage(fromAddress, toAddress); - const message = JSON.stringify(JSON.parse(rawMessage)); - return signer.sign(fromAddress, toUtf8(message)); +export async function sign( + accountNumber: number, + fromAddress: string, + toAddress: string, +): Promise { + const msgs = [ + { + type: "cosmos-sdk/MsgSend", + value: { + amount: [ + { + amount: "1234567", + denom: "ucosm", + }, + ], + from_address: fromAddress, + to_address: toAddress, + }, + }, + ]; + const signBytes = makeSignBytes( + msgs, + defaultFee, + defaultChainId, + defaultMemo, + accountNumber, + defaultSequence, + ); + return signer.sign(fromAddress, signBytes, defaultPrehashType, accountNumber); }