diff --git a/packages/crypto/src/index.ts b/packages/crypto/src/index.ts index 9365daa2..925865b3 100644 --- a/packages/crypto/src/index.ts +++ b/packages/crypto/src/index.ts @@ -8,6 +8,7 @@ export { xchacha20NonceLength, Argon2id, Argon2idOptions, + isArgon2idOptions, Ed25519, Ed25519Keypair, } from "./libsodium"; diff --git a/packages/crypto/src/libsodium.ts b/packages/crypto/src/libsodium.ts index a68274cc..0776a0c6 100644 --- a/packages/crypto/src/libsodium.ts +++ b/packages/crypto/src/libsodium.ts @@ -3,6 +3,7 @@ // // libsodium.js API: https://gist.github.com/webmaster128/b2dbe6d54d36dd168c9fabf441b9b09c +import { isNonNullObject } from "@cosmjs/utils"; import sodium from "libsodium-wrappers"; export interface Argon2idOptions { @@ -24,6 +25,14 @@ export interface Argon2idOptions { readonly memLimitKib: number; } +export function isArgon2idOptions(thing: unknown): thing is Argon2idOptions { + if (!isNonNullObject(thing)) return false; + if (typeof (thing as Argon2idOptions).outputLength !== "number") return false; + if (typeof (thing as Argon2idOptions).opsLimit !== "number") return false; + if (typeof (thing as Argon2idOptions).memLimitKib !== "number") return false; + return true; +} + export class Argon2id { public static async execute( password: string, diff --git a/packages/crypto/types/index.d.ts b/packages/crypto/types/index.d.ts index 9365daa2..925865b3 100644 --- a/packages/crypto/types/index.d.ts +++ b/packages/crypto/types/index.d.ts @@ -8,6 +8,7 @@ export { xchacha20NonceLength, Argon2id, Argon2idOptions, + isArgon2idOptions, Ed25519, Ed25519Keypair, } from "./libsodium"; diff --git a/packages/crypto/types/libsodium.d.ts b/packages/crypto/types/libsodium.d.ts index 00437296..fbddb7f3 100644 --- a/packages/crypto/types/libsodium.d.ts +++ b/packages/crypto/types/libsodium.d.ts @@ -16,6 +16,7 @@ export interface Argon2idOptions { */ readonly memLimitKib: number; } +export declare function isArgon2idOptions(thing: unknown): thing is Argon2idOptions; export declare class Argon2id { static execute(password: string, salt: Uint8Array, options: Argon2idOptions): Promise; } diff --git a/packages/sdk38/src/wallet.ts b/packages/sdk38/src/wallet.ts index 0b7bba0a..f8e4b1ab 100644 --- a/packages/sdk38/src/wallet.ts +++ b/packages/sdk38/src/wallet.ts @@ -1,6 +1,6 @@ import { Argon2id, - Argon2idOptions, + isArgon2idOptions, Random, Sha256, Sha512, @@ -9,7 +9,6 @@ import { Xchacha20poly1305Ietf, } from "@cosmjs/crypto"; import { toAscii } from "@cosmjs/encoding"; -import { assert } from "@cosmjs/utils"; import { StdSignature } from "./types"; @@ -82,11 +81,8 @@ export interface KdfConfiguration { export async function executeKdf(password: string, configuration: KdfConfiguration): Promise { switch (configuration.algorithm) { case "argon2id": { - const { outputLength, opsLimit, memLimitKib } = configuration.params; - assert(typeof outputLength === "number"); - assert(typeof opsLimit === "number"); - assert(typeof memLimitKib === "number"); - const options: Argon2idOptions = { outputLength, opsLimit, memLimitKib }; + const options = configuration.params; + if (!isArgon2idOptions(options)) throw new Error("Invalid format of argon2id params"); return Argon2id.execute(password, cosmjsSalt, options); } default: