mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Improve demo script for node to show adresses
This commit is contained in:
parent
c3f062dfb1
commit
7fec3a2875
@ -1,19 +1,64 @@
|
||||
const demo = require("../build/demo/node");
|
||||
const { makeCosmoshubPath, makeSignDoc } = require("@cosmjs/amino");
|
||||
const { pathToString } = require("@cosmjs/crypto");
|
||||
const { toBase64 } = require("@cosmjs/encoding");
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
const { LedgerSigner } = require("@cosmjs/ledger-amino");
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
const TransportNodeHid = require("@ledgerhq/hw-transport-node-hid").default;
|
||||
|
||||
const interactiveTimeout = 120_000;
|
||||
const accountNumbers = [0, 1, 2, 10];
|
||||
const paths = accountNumbers.map(makeCosmoshubPath);
|
||||
|
||||
const defaultChainId = "testing";
|
||||
const defaultFee = {
|
||||
amount: [{ amount: "100", denom: "ucosm" }],
|
||||
gas: "250",
|
||||
};
|
||||
const defaultMemo = "Some memo";
|
||||
const defaultSequence = "0";
|
||||
|
||||
async function signMsgSend(signer, accountNumber, fromAddress, toAddress) {
|
||||
const msg = {
|
||||
type: "cosmos-sdk/MsgSend",
|
||||
value: {
|
||||
amount: [
|
||||
{
|
||||
amount: "1234567",
|
||||
denom: "ucosm",
|
||||
},
|
||||
],
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
from_address: fromAddress,
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention
|
||||
to_address: toAddress,
|
||||
},
|
||||
};
|
||||
const signDoc = makeSignDoc([msg], defaultFee, defaultChainId, defaultMemo, accountNumber, defaultSequence);
|
||||
const { signature } = await signer.signAmino(fromAddress, signDoc);
|
||||
return signature;
|
||||
}
|
||||
|
||||
async function run() {
|
||||
const signer = await demo.createSigner();
|
||||
const ledgerTransport = await TransportNodeHid.create(interactiveTimeout, interactiveTimeout);
|
||||
const signer = new LedgerSigner(ledgerTransport, { testModeAllowed: true, hdPaths: paths });
|
||||
|
||||
const accountNumbers = [0, 1, 2, 10];
|
||||
const accounts = await demo.getAccounts(signer);
|
||||
const accounts = await signer.getAccounts();
|
||||
const printableAccounts = accounts.map((account) => ({ ...account, pubkey: toBase64(account.pubkey) }));
|
||||
console.info("Accounts from Ledger device:");
|
||||
console.table(accounts.map((account, i) => ({ ...account, accountNumber: accountNumbers[i] })));
|
||||
console.table(printableAccounts.map((account, i) => ({ ...account, hdPath: pathToString(paths[i]) })));
|
||||
|
||||
console.info("Showing address of first account on device");
|
||||
await signer.showAddress();
|
||||
console.info("Showing address of 3rd account on device");
|
||||
await signer.showAddress(paths[2]); // Path of 3rd account
|
||||
|
||||
const accountNumber0 = 0;
|
||||
const address0 = accounts[accountNumber0].address;
|
||||
console.info(
|
||||
`Signing on Ledger device with account index ${accountNumber0} (${address0}). Please review and approve on the device now.`,
|
||||
);
|
||||
const signature0 = await demo.sign(signer, accountNumber0, address0, address0);
|
||||
const signature0 = await signMsgSend(signer, accountNumber0, address0, address0);
|
||||
console.info("Signature:", signature0);
|
||||
|
||||
// It seems the Ledger device needs a bit of time to recover
|
||||
@ -24,8 +69,14 @@ async function run() {
|
||||
console.info(
|
||||
`Signing on Ledger device with account index ${accountNumber10} (${address10}). Please review and approve on the device now.`,
|
||||
);
|
||||
const signature1 = await demo.sign(signer, accountNumber10, address10, address10);
|
||||
const signature1 = await signMsgSend(signer, accountNumber10, address10, address10);
|
||||
console.info("Signature:", signature1);
|
||||
}
|
||||
|
||||
run().catch(console.error);
|
||||
run().then(
|
||||
() => process.exit(0),
|
||||
(err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
},
|
||||
);
|
||||
|
@ -1,60 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { makeCosmoshubPath, makeSignDoc, StdFee, StdSignature } from "@cosmjs/amino";
|
||||
import { toBase64 } from "@cosmjs/encoding";
|
||||
import TransportNodeHid from "@ledgerhq/hw-transport-node-hid";
|
||||
|
||||
import { LedgerSigner } from "../ledgersigner";
|
||||
|
||||
const defaultChainId = "testing";
|
||||
const defaultFee: StdFee = {
|
||||
amount: [{ amount: "100", denom: "ucosm" }],
|
||||
gas: "250",
|
||||
};
|
||||
const defaultMemo = "Some memo";
|
||||
const defaultSequence = "0";
|
||||
|
||||
export async function createSigner(): Promise<LedgerSigner> {
|
||||
const interactiveTimeout = 120_000;
|
||||
const ledgerTransport = await TransportNodeHid.create(interactiveTimeout, interactiveTimeout);
|
||||
return new LedgerSigner(ledgerTransport, {
|
||||
testModeAllowed: true,
|
||||
hdPaths: [makeCosmoshubPath(0), makeCosmoshubPath(1), makeCosmoshubPath(2), makeCosmoshubPath(10)],
|
||||
});
|
||||
}
|
||||
|
||||
export async function getAccounts(signer: LedgerSigner): Promise<
|
||||
ReadonlyArray<{
|
||||
readonly algo: string;
|
||||
readonly address: string;
|
||||
readonly pubkey: string;
|
||||
}>
|
||||
> {
|
||||
const accounts = await signer.getAccounts();
|
||||
return accounts.map((account) => ({ ...account, pubkey: toBase64(account.pubkey) }));
|
||||
}
|
||||
|
||||
export async function sign(
|
||||
signer: LedgerSigner,
|
||||
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 signDoc = makeSignDoc(msgs, defaultFee, defaultChainId, defaultMemo, accountNumber, defaultSequence);
|
||||
const { signature } = await signer.signAmino(fromAddress, signDoc);
|
||||
return signature;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user