Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for amino/wallet.ts #1532

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions packages/amino/src/wallet.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { fromHex } from "@cosmjs/encoding";

import { decrypt, encrypt, executeKdf, KdfConfiguration } from "./wallet";

describe("wallet", () => {
describe("executeKdf", () => {
describe("with supported algorithm", () => {
it("can execute", async () => {
const password = "123";
const kdfConfiguration: KdfConfiguration = {
algorithm: "argon2id",
params: {
outputLength: 32,
opsLimit: 4,
memLimitKib: 3 * 1024,
},
};
await expectAsync(executeKdf(password, kdfConfiguration)).not.toBeRejected();
});

describe("with invalid params", () => {
it("throws", async () => {
const password = "123";
const kdfConfiguration: KdfConfiguration = {
algorithm: "argon2id",
params: {
outputLength: "invalid",
opsLimit: "invalid",
memLimitKib: "invalid",
},
};
await expectAsync(executeKdf(password, kdfConfiguration)).toBeRejectedWith(
new Error("Invalid format of argon2id params"),
);
});
});
});

describe("with unsupported algorithm", () => {
it("throws", async () => {
const password = "123";
const kdfConfiguration: KdfConfiguration = {
algorithm: "unsupported",
params: {},
};
await expectAsync(executeKdf(password, kdfConfiguration)).toBeRejectedWith(
new Error("Unsupported KDF algorithm"),
);
});
});
});

describe("encrypt", () => {
describe("with supported algorithm", () => {
it("can encrypt without throw", async () => {
const plaintext = new Uint8Array([0x11, 0x22, 0x33, 0x44]);
const encryptionKey = fromHex("1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d8");
const encryptionConfiguration = {
algorithm: "xchacha20poly1305-ietf",
params: {},
};
await expectAsync(encrypt(plaintext, encryptionKey, encryptionConfiguration)).not.toBeRejected();
});
});

describe("with unsupported algorithm", () => {
it("throws", async () => {
const plaintext = new Uint8Array([0x11, 0x22, 0x33, 0x44]);
const encryptionKey = fromHex("1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d8");
const encryptionConfiguration = {
algorithm: "unsupported",
params: {},
};
await expectAsync(encrypt(plaintext, encryptionKey, encryptionConfiguration)).toBeRejectedWith(
new Error("Unsupported encryption algorithm: 'unsupported'"),
);
});
});
});

describe("decrypt", () => {
const plaintext = new Uint8Array([0x11, 0x22, 0x33, 0x44]);
const encryptionKey = fromHex("1324cdddc4b94e625bbabcac862c9429ba011e2184a1ccad60e7c3f6ff4916d8");
const encryptionConfiguration = {
algorithm: "xchacha20poly1305-ietf",
params: {},
};

describe("with supported algorithm", () => {
it("can decrypt without throw", async () => {
const ciphertext = await encrypt(plaintext, encryptionKey, encryptionConfiguration);
await decrypt(ciphertext, encryptionKey, encryptionConfiguration);
await expectAsync(decrypt(ciphertext, encryptionKey, encryptionConfiguration)).not.toBeRejected();
});
});

describe("with unsupported algorithm", () => {
it("throws", async () => {
const ciphertext = await encrypt(plaintext, encryptionKey, encryptionConfiguration);
await expectAsync(
decrypt(ciphertext, encryptionKey, {
algorithm: "unsupported",
params: {},
}),
).toBeRejectedWith(new Error("Unsupported encryption algorithm: 'unsupported'"));
});
});
});
});