mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
launchpad-ledger: Add multiple accounts to Node.js demo
This commit is contained in:
parent
7b005b4244
commit
e095c1561e
@ -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);
|
||||
|
@ -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<StdSignature> {
|
||||
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<StdSignature> {
|
||||
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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user