Skip to content

Commit

Permalink
Throw error when trying to encode undefined value (#15)
Browse files Browse the repository at this point in the history
* Throw error when trying to encode undefined value

Signed-off-by: Samuel Hellawell <[email protected]>

* test case

Signed-off-by: Samuel Hellawell <[email protected]>

---------

Signed-off-by: Samuel Hellawell <[email protected]>
  • Loading branch information
cykoder authored Mar 20, 2023
1 parent 235b7f0 commit f6e26c3
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/crypto-wasm-ts",
"version": "0.31.1",
"version": "0.31.2",
"description": "Typescript abstractions over Dock's Rust crypto library's WASM wrapper",
"homepage": "https://github.com/docknetwork/crypto-wasm-ts",
"main": "lib/index.js",
Expand Down
3 changes: 3 additions & 0 deletions src/bbs-plus/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ export class Encoder {
encodeMessage(name: string, value: unknown, strict = false): Uint8Array {
const encoder = this.encoders?.get(name) || this.defaultEncoder;
if (encoder !== undefined) {
if (typeof value === undefined) {
throw new Error(`Cannot encode message with name ${name} as it is undefined`);
}
return encoder(value);
} else {
if (!strict && value instanceof Uint8Array) {
Expand Down
3 changes: 3 additions & 0 deletions tests/composite-proofs/msg-js-obj/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ describe('Utils', () => {
encoders1.set('foo', Encoder.positiveIntegerEncoder());
const encoder1 = new Encoder(encoders1);

// Throws for undefined message
expect(() => encoder1.encodeMessage('bar', undefined)).toThrow();

// Throws for unknown message name when no default encoder
expect(() => encoder1.encodeMessage('bar', 6)).toThrow();
expect(() => encoder1.encodeMessageObject({ bar: 6, foo: 10 })).toThrow();
Expand Down

0 comments on commit f6e26c3

Please sign in to comment.