From 20dce4e7644b83eaf395eab9e870bdd257cfde06 Mon Sep 17 00:00:00 2001 From: murrayee Date: Fri, 22 Dec 2023 23:04:07 +0800 Subject: [PATCH 1/5] chore: add tests for amino --- packages/amino/src/secp256k1hdwallet.spec.ts | 46 +++++++++++++++++++- packages/amino/src/secp256k1hdwallet.ts | 2 +- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/packages/amino/src/secp256k1hdwallet.spec.ts b/packages/amino/src/secp256k1hdwallet.spec.ts index 493e12bdc9..c96aa0efaa 100644 --- a/packages/amino/src/secp256k1hdwallet.spec.ts +++ b/packages/amino/src/secp256k1hdwallet.spec.ts @@ -3,7 +3,7 @@ import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto"; import { fromBase64, fromHex } from "@cosmjs/encoding"; import { makeCosmoshubPath } from "./paths"; -import { extractKdfConfiguration, Secp256k1HdWallet } from "./secp256k1hdwallet"; +import { extractKdfConfiguration, isDerivationJson, Secp256k1HdWallet } from "./secp256k1hdwallet"; import { serializeSignDoc, StdSignDoc } from "./signdoc"; import { base64Matcher } from "./testutils.spec"; import { executeKdf, KdfConfiguration } from "./wallet"; @@ -311,4 +311,48 @@ describe("Secp256k1HdWallet", () => { }); }); }); + + describe('isDerivationJson function', () => { + it('returns true for valid DerivationInfoJson', () => { + const validInput: unknown = { + hdPath: 'validHdPath', + prefix: 'validPrefix' + }; + expect(isDerivationJson(validInput)).toBe(true); + }); + + it('returns false for undefined input', () => { + expect(isDerivationJson(undefined)).toBe(false); + }); + + it('returns false for null input', () => { + expect(isDerivationJson(null)).toBe(false); + }); + + it('returns false for non-object input', () => { + expect(isDerivationJson(42)).toBe(false); // Replace with your desired non-object value + }); + + it('returns false for missing hdPath property', () => { + const missingHdPath: unknown = { + }; + expect(isDerivationJson(missingHdPath)).toBe(false); + }); + + it('returns false for missing prefix property', () => { + const missingPrefix: unknown = { + hdPath: 'validHdPath' + }; + expect(isDerivationJson(missingPrefix)).toBe(false); + }); + + it('returns false for incorrect hdPath type', () => { + const incorrectHdPathType: unknown = { + hdPath: 123, + prefix: 'validPrefix' + }; + expect(isDerivationJson(incorrectHdPathType)).toBe(false); + }); + }); + }); diff --git a/packages/amino/src/secp256k1hdwallet.ts b/packages/amino/src/secp256k1hdwallet.ts index c65644c09b..8aada72aed 100644 --- a/packages/amino/src/secp256k1hdwallet.ts +++ b/packages/amino/src/secp256k1hdwallet.ts @@ -71,7 +71,7 @@ interface DerivationInfoJson { readonly prefix: string; } -function isDerivationJson(thing: unknown): thing is DerivationInfoJson { +export function isDerivationJson(thing: unknown): thing is DerivationInfoJson { if (!isNonNullObject(thing)) return false; if (typeof (thing as DerivationInfoJson).hdPath !== "string") return false; if (typeof (thing as DerivationInfoJson).prefix !== "string") return false; From 2d6393ca4b12ea23fd50ab1c841525b7db320304 Mon Sep 17 00:00:00 2001 From: murrayee Date: Fri, 22 Dec 2023 23:05:08 +0800 Subject: [PATCH 2/5] chore: add tests for cli helper --- packages/cli/src/helpers.spec.ts | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/helpers.spec.ts b/packages/cli/src/helpers.spec.ts index 6571d3cf40..6a4b38607a 100644 --- a/packages/cli/src/helpers.spec.ts +++ b/packages/cli/src/helpers.spec.ts @@ -1,6 +1,6 @@ import { createContext } from "vm"; - -import { executeJavaScript, executeJavaScriptAsync } from "./helpers"; +import { TSError } from "ts-node"; +import { executeJavaScript, executeJavaScriptAsync, isRecoverable } from "./helpers"; describe("Helpers", () => { describe("executeJavaScript", () => { @@ -144,4 +144,23 @@ describe("Helpers", () => { expect(await executeJavaScriptAsync(code, "myfile.js", context)).toEqual("job done"); }); }); + + describe("isRecoverable",()=>{ + it('should return true for recoverable errors', () => { + const recoverableError = { + diagnosticCodes: [1003, 1160, 2355], + } as TSError; + + expect(isRecoverable(recoverableError)).toBe(true); + }); + + it('should return false for non-recoverable errors', () => { + const nonRecoverableError = { + diagnosticCodes: [1234, 5678], + } as TSError; + + expect(isRecoverable(nonRecoverableError)).toBe(false); + }); + }) + }); From 1f49e2a514cae187789e623fe2867f770ff9008d Mon Sep 17 00:00:00 2001 From: murrayee Date: Fri, 22 Dec 2023 23:13:40 +0800 Subject: [PATCH 3/5] chore: remove note --- packages/amino/src/secp256k1hdwallet.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/amino/src/secp256k1hdwallet.spec.ts b/packages/amino/src/secp256k1hdwallet.spec.ts index c96aa0efaa..069789c896 100644 --- a/packages/amino/src/secp256k1hdwallet.spec.ts +++ b/packages/amino/src/secp256k1hdwallet.spec.ts @@ -330,7 +330,7 @@ describe("Secp256k1HdWallet", () => { }); it('returns false for non-object input', () => { - expect(isDerivationJson(42)).toBe(false); // Replace with your desired non-object value + expect(isDerivationJson(42)).toBe(false); }); it('returns false for missing hdPath property', () => { From 317639293273399697b000aed407e4539cf424d3 Mon Sep 17 00:00:00 2001 From: murrayee Date: Fri, 22 Dec 2023 23:47:15 +0800 Subject: [PATCH 4/5] chore: add tests for encoding throw error --- packages/amino/src/encoding.spec.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/amino/src/encoding.spec.ts b/packages/amino/src/encoding.spec.ts index 0a056e9826..8b1d5b2fd2 100644 --- a/packages/amino/src/encoding.spec.ts +++ b/packages/amino/src/encoding.spec.ts @@ -20,6 +20,7 @@ import { testgroup4, testgroup4PubkeyBech32, } from "./testutils.spec"; +import { TSError } from "ts-node"; describe("encoding", () => { describe("encodeSecp256k1Pubkey", () => { @@ -69,6 +70,7 @@ describe("encoding", () => { }); }); + it("works for ed25519", () => { // Encoded from `corald tendermint show-validator` // Decoded from http://localhost:26657/validators @@ -131,6 +133,15 @@ describe("encoding", () => { expect(() => decodeAminoPubkey(fromHex("22C1F7E20705"))).toThrowError(/expecting 0x08 prefix/i); }); + + it('throws error for invalid secp256k1 pubkey length', () => { + const data = new Uint8Array([0, 1, 2, 3, 4]); + try { + decodeAminoPubkey(data) + } catch (error) { + expect((error as TSError).message).toBe('Unsupported public key type. Amino data starts with: 0001020304'); + } + }); }); describe("decodeBech32Pubkey", () => { From 944abf8b5b004031475089b2287c52bed8d0b243 Mon Sep 17 00:00:00 2001 From: murrayee Date: Fri, 22 Dec 2023 23:58:36 +0800 Subject: [PATCH 5/5] chore: fix lint error --- packages/amino/src/encoding.spec.ts | 11 ++++---- packages/amino/src/secp256k1hdwallet.spec.ts | 28 +++++++++----------- packages/cli/src/helpers.spec.ts | 12 ++++----- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/packages/amino/src/encoding.spec.ts b/packages/amino/src/encoding.spec.ts index 8b1d5b2fd2..387e075953 100644 --- a/packages/amino/src/encoding.spec.ts +++ b/packages/amino/src/encoding.spec.ts @@ -1,5 +1,6 @@ import { Random } from "@cosmjs/crypto"; import { fromBase64, fromBech32, fromHex } from "@cosmjs/encoding"; +import { TSError } from "ts-node"; import { decodeAminoPubkey, @@ -20,7 +21,6 @@ import { testgroup4, testgroup4PubkeyBech32, } from "./testutils.spec"; -import { TSError } from "ts-node"; describe("encoding", () => { describe("encodeSecp256k1Pubkey", () => { @@ -70,7 +70,6 @@ describe("encoding", () => { }); }); - it("works for ed25519", () => { // Encoded from `corald tendermint show-validator` // Decoded from http://localhost:26657/validators @@ -134,12 +133,14 @@ describe("encoding", () => { expect(() => decodeAminoPubkey(fromHex("22C1F7E20705"))).toThrowError(/expecting 0x08 prefix/i); }); - it('throws error for invalid secp256k1 pubkey length', () => { + it("throws error for invalid secp256k1 pubkey length", () => { const data = new Uint8Array([0, 1, 2, 3, 4]); try { - decodeAminoPubkey(data) + decodeAminoPubkey(data); } catch (error) { - expect((error as TSError).message).toBe('Unsupported public key type. Amino data starts with: 0001020304'); + expect((error as TSError).message).toBe( + "Unsupported public key type. Amino data starts with: 0001020304", + ); } }); }); diff --git a/packages/amino/src/secp256k1hdwallet.spec.ts b/packages/amino/src/secp256k1hdwallet.spec.ts index 069789c896..86958543ad 100644 --- a/packages/amino/src/secp256k1hdwallet.spec.ts +++ b/packages/amino/src/secp256k1hdwallet.spec.ts @@ -312,47 +312,45 @@ describe("Secp256k1HdWallet", () => { }); }); - describe('isDerivationJson function', () => { - it('returns true for valid DerivationInfoJson', () => { + describe("isDerivationJson function", () => { + it("returns true for valid DerivationInfoJson", () => { const validInput: unknown = { - hdPath: 'validHdPath', - prefix: 'validPrefix' + hdPath: "validHdPath", + prefix: "validPrefix", }; expect(isDerivationJson(validInput)).toBe(true); }); - it('returns false for undefined input', () => { + it("returns false for undefined input", () => { expect(isDerivationJson(undefined)).toBe(false); }); - it('returns false for null input', () => { + it("returns false for null input", () => { expect(isDerivationJson(null)).toBe(false); }); - it('returns false for non-object input', () => { + it("returns false for non-object input", () => { expect(isDerivationJson(42)).toBe(false); }); - it('returns false for missing hdPath property', () => { - const missingHdPath: unknown = { - }; + it("returns false for missing hdPath property", () => { + const missingHdPath: unknown = {}; expect(isDerivationJson(missingHdPath)).toBe(false); }); - it('returns false for missing prefix property', () => { + it("returns false for missing prefix property", () => { const missingPrefix: unknown = { - hdPath: 'validHdPath' + hdPath: "validHdPath", }; expect(isDerivationJson(missingPrefix)).toBe(false); }); - it('returns false for incorrect hdPath type', () => { + it("returns false for incorrect hdPath type", () => { const incorrectHdPathType: unknown = { hdPath: 123, - prefix: 'validPrefix' + prefix: "validPrefix", }; expect(isDerivationJson(incorrectHdPathType)).toBe(false); }); }); - }); diff --git a/packages/cli/src/helpers.spec.ts b/packages/cli/src/helpers.spec.ts index 6a4b38607a..65fa9dafea 100644 --- a/packages/cli/src/helpers.spec.ts +++ b/packages/cli/src/helpers.spec.ts @@ -1,5 +1,6 @@ -import { createContext } from "vm"; import { TSError } from "ts-node"; +import { createContext } from "vm"; + import { executeJavaScript, executeJavaScriptAsync, isRecoverable } from "./helpers"; describe("Helpers", () => { @@ -145,8 +146,8 @@ describe("Helpers", () => { }); }); - describe("isRecoverable",()=>{ - it('should return true for recoverable errors', () => { + describe("isRecoverable", () => { + it("should return true for recoverable errors", () => { const recoverableError = { diagnosticCodes: [1003, 1160, 2355], } as TSError; @@ -154,13 +155,12 @@ describe("Helpers", () => { expect(isRecoverable(recoverableError)).toBe(true); }); - it('should return false for non-recoverable errors', () => { + it("should return false for non-recoverable errors", () => { const nonRecoverableError = { diagnosticCodes: [1234, 5678], } as TSError; expect(isRecoverable(nonRecoverableError)).toBe(false); }); - }) - + }); });