Skip to content

Commit

Permalink
fix: show error message when @talismn/scale is used but not initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
alecdwm committed Nov 9, 2023
1 parent f57ad2b commit 6e39409
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
38 changes: 36 additions & 2 deletions packages/scale/src/capi/crypto/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,44 @@ import { Hasher } from "wat-the-crypto/types/common/hasher"
// and falls back to `./nosimd` re-exports for devices which do not.
//

/**
* Show a useful error message when someone attempts to use one of the classes
* from this lib without waiting for them to be initialized.
*/
class UninitializedHasher implements Hasher {
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any
constructor(...args: any[]) {
throw new Error(
`This class cannot be used before @talismn/scale has initialized it. Please await 'watCryptoWaitReady' before attempting to construct this class.`
)
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
update(input: Uint8Array): void {
throw new Error(
`This class cannot be used before @talismn/scale has initialized it. Please await 'watCryptoWaitReady' before attempting to construct this class.`
)
}

digest(): Uint8Array {
throw new Error(
`This class cannot be used before @talismn/scale has initialized it. Please await 'watCryptoWaitReady' before attempting to construct this class.`
)
}
digestInto(digest: Uint8Array): void {
digest.set(this.digest())
}
dispose(): void {
throw new Error(
`This class cannot be used before @talismn/scale has initialized it. Please await 'watCryptoWaitReady' before attempting to construct this class.`
)
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export let Blake2b: new (...args: any[]) => Hasher
export let Blake2b: new (...args: any[]) => Hasher = UninitializedHasher
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export let Xxhash: new (...args: any[]) => Hasher
export let Xxhash: new (...args: any[]) => Hasher = UninitializedHasher

let readyPromise: Promise<void> | null = null

Expand Down
2 changes: 0 additions & 2 deletions packages/scale/src/capi/crypto/util/nosimd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export class Xxhash implements Hasher {
return xxhashAsU8a(this.input, (this.rounds * 64) as XxHashBitLength)
}
digestInto(digest: Uint8Array): void {
// TODO: Test that this correctly mutates the input var to be the value of the digest
digest.set(this.digest())
}
dispose(): void {
Expand All @@ -46,7 +45,6 @@ export class Blake2b implements Hasher {
return blake2AsU8a(this.input, (this.digestSize * 8) as Blake2bBitLength)
}
digestInto(digest: Uint8Array): void {
// TODO: Test that this correctly mutates the input var to be the value of the digest
digest.set(this.digest())
}
dispose(): void {
Expand Down

0 comments on commit 6e39409

Please sign in to comment.