Add initial Ledger tests

This commit is contained in:
Simon Warta 2020-09-29 15:58:13 +02:00
parent 70b213d13d
commit d79dd01711
4 changed files with 150 additions and 2 deletions

View File

@ -0,0 +1,33 @@
#!/usr/bin/env node
/* eslint-disable @typescript-eslint/naming-convention */
require("source-map-support").install();
const defaultSpecReporterConfig = require("../../jasmine-spec-reporter.config.json");
// setup Jasmine
const Jasmine = require("jasmine");
const jasmine = new Jasmine();
jasmine.loadConfig({
spec_dir: "build",
spec_files: ["**/*.spec.js"],
helpers: [],
random: false,
seed: null,
stopSpecOnExpectationFailure: false,
});
jasmine.jasmine.DEFAULT_TIMEOUT_INTERVAL = 15 * 1000;
// setup reporter
const { SpecReporter } = require("jasmine-spec-reporter");
const reporter = new SpecReporter({
...defaultSpecReporterConfig,
spec: {
...defaultSpecReporterConfig.spec,
displaySuccessful: !process.argv.includes("--quiet"),
},
});
// initialize and execute
jasmine.env.clearReporters();
jasmine.addReporter(reporter);
jasmine.execute();

View File

@ -28,13 +28,15 @@
"format-text": "prettier --write --prose-wrap always --print-width 80 \"./*.md\"",
"lint": "eslint --max-warnings 0 \"**/*.{js,ts}\"",
"lint-fix": "eslint --max-warnings 0 \"**/*.{js,ts}\" --fix",
"move-types": "shx rm -rf ./types/* && shx mv build/types/* ./types && rm -rf ./types/testdata && shx rm -rf ./types/demo",
"premove-types": "shx rm -rf ./build/types/demo",
"move-types": "shx rm -r ./types/* && shx mv build/types/* ./types && rm -rf ./types/testdata && shx rm -f ./types/*.spec.d.ts",
"format-types": "prettier --write --loglevel warn \"./types/**/*.d.ts\"",
"prebuild": "shx rm -rf ./build",
"build": "tsc",
"postbuild": "yarn move-types && yarn format-types",
"build-or-skip": "[ -n \"$SKIP_BUILD\" ] || yarn build",
"test": "echo 'Please check README for information on how to manually run the demo'",
"test-node": "node jasmine-testrunner.js",
"test": "yarn build-or-skip && yarn test-node",
"demo-node": "yarn build-or-skip && node ./demo/node.js",
"coverage": "nyc --reporter=text --reporter=lcov yarn test --quiet",
"pack-web": "yarn build-or-skip && webpack --mode development --config webpack.demo.config.js"

View File

@ -0,0 +1,94 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Secp256k1, Secp256k1Signature, Sha256 } from "@cosmjs/crypto";
import { fromBase64 } from "@cosmjs/encoding";
import { coins, makeCosmoshubPath, makeSignDoc, Msg, serializeSignDoc, StdFee } from "@cosmjs/launchpad";
import { LedgerSigner } from "./ledgersigner";
import { pendingWithoutLedger, pendingWithoutLedgerInteractive } from "./testutils.spec";
const interactiveTimeout = 120_000;
describe("LedgerSigner", () => {
const defaultChainId = "testing";
const defaultFee: StdFee = {
amount: coins(100, "ucosm"),
gas: "250",
};
const defaultMemo = "Some memo";
const defaultSequence = "0";
const defaultAccountNumber = "42";
const defaultRecipient = "cosmos1p6xs63q4g7np99ttv5nd3yzkt8n4qxa47w8aea";
describe("getAccount", () => {
it("works", async () => {
pendingWithoutLedger();
const signer = new LedgerSigner({
testModeAllowed: true,
hdPaths: [makeCosmoshubPath(0), makeCosmoshubPath(1), makeCosmoshubPath(10)],
});
const accounts = await signer.getAccounts();
expect(accounts.length).toEqual(3);
expect(accounts).toEqual([
{
address: "cosmos1p6xs63q4g7np99ttv5nd3yzkt8n4qxa47w8aea",
algo: "secp256k1",
pubkey: fromBase64("A66JoCNaNSXDsyj4qW7JgqXPTz5rOnfE6EKEArf4jJEK"),
},
{
address: "cosmos1meeu3jl268txxytwmmrsljk8rawh6n2majstn2",
algo: "secp256k1",
pubkey: fromBase64("AtvmGuZvEN3NwL05BQdxl3XygUf+Vl/930fhFMt1HTyU"),
},
{
address: "cosmos1f3pws3ztnp3s4nn5zxqdrl9vlqv5avkqmlrus4",
algo: "secp256k1",
pubkey: fromBase64("A2ZnLEcbpyjS30H5UF1vezq29aBcT9oo5EARATIW9Cpj"),
},
]);
});
});
describe("sign", () => {
it(
"works",
async () => {
pendingWithoutLedgerInteractive();
const signer = new LedgerSigner({
testModeAllowed: true,
hdPaths: [makeCosmoshubPath(0), makeCosmoshubPath(1), makeCosmoshubPath(10)],
});
const [fistAccount] = await signer.getAccounts();
const msgs: readonly Msg[] = [
{
type: "cosmos-sdk/MsgSend",
value: {
amount: coins(1234567, "ucosm"),
from_address: fistAccount.address,
to_address: defaultRecipient,
},
},
];
const signDoc = makeSignDoc(
msgs,
defaultFee,
defaultChainId,
defaultMemo,
defaultAccountNumber,
defaultSequence,
);
const { signed, signature } = await signer.sign(fistAccount.address, signDoc);
expect(signed).toEqual(signDoc);
const valid = await Secp256k1.verifySignature(
Secp256k1Signature.fromFixedLength(fromBase64(signature.signature)),
new Sha256(serializeSignDoc(signed)).digest(),
fistAccount.pubkey,
);
expect(valid).toEqual(true);
},
interactiveTimeout,
);
});
});

View File

@ -0,0 +1,19 @@
export function ledgerEnabled(): boolean {
return !!process.env.LEDGER_ENABLED;
}
export function pendingWithoutLedger(): void {
if (!ledgerEnabled()) {
return pending("Set LEDGER_ENABLED to enable Wasmd based tests");
}
}
export function ledgerInteractiveEnabled(): boolean {
return !!process.env.LEDGER_INTERACTIVE_ENABLED;
}
export function pendingWithoutLedgerInteractive(): void {
if (!ledgerInteractiveEnabled()) {
return pending("Set LEDGER_INTERACTIVE_ENABLED to enable Wasmd based tests");
}
}