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

Fix empty objects in credential throws error #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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.2",
"version": "0.31.3",
"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
29 changes: 18 additions & 11 deletions src/anonymous-credentials/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ export class CredentialSchema extends Versioned {

static validateGeneric(schema: object, ignoreKeys: Set<string> = new Set()) {
const [names, values] = this.flattenSchemaObj(schema);

for (let i = 0; i < names.length; i++) {
if (ignoreKeys.has(names[i])) {
continue;
Expand Down Expand Up @@ -644,16 +645,16 @@ export class CredentialSchema extends Versioned {
properties: {
id: {
type: 'string'
},
},
}
}
},
proof: {
type: 'object',
properties: {
type: {
type: 'string'
},
},
}
}
}
}
};
Expand Down Expand Up @@ -1101,13 +1102,19 @@ export class CredentialSchema extends Versioned {
} else if (schemaProps[key]['type'] == 'object' && typ == 'object') {
const schemaKeys = new Set([...Object.keys(schemaProps[key]['properties'])]);
const valKeys = new Set([...Object.keys(value)]);
for (const vk of valKeys) {
CredentialSchema.generateFromCredential(value, schemaProps[key]['properties']);
}
// Delete extra keys not in cred
for (const sk of schemaKeys) {
if (value[sk] === undefined) {
delete schemaKeys[sk];

// If empty object, skip it here otherwise causes problems downstream
if (schemaKeys.size === 0) {
delete schemaProps[key];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed as it will ignore credential values for which the schema has an empty object type. Such a schema is not valid and an error should be thrown. Secondly, is ignoring those fields what you want?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed as it will ignore credential values for which the schema has an empty object type

that is intended, because that value cant exactly be encoded (unless its stringified to {} but that seems pointless to me?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? The schema should never have an empty object.

} else {
for (const vk of valKeys) {
CredentialSchema.generateFromCredential(value, schemaProps[key]['properties']);
}
// Delete extra keys not in cred
for (const sk of schemaKeys) {
if (value[sk] === undefined) {
delete schemaKeys[sk];
}
}
}
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/anonymous-credentials/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { ValueType, ValueTypes } from './schema';
import { Encoder } from '../bbs-plus';
import { SetupParam, Statement, WitnessEqualityMetaStatement } from '../composite-proof';
import { SetupParamsTracker } from './setup-params-tracker';
import { isEmptyObject } from '../util';

export function dockAccumulatorParams(): AccumulatorParams {
return Accumulator.generateParams(ACCUMULATOR_PARAMS_LABEL_BYTES);
Expand All @@ -56,7 +57,8 @@ export function dockSaverEncryptionGensUncompressed(): SaverEncryptionGensUncomp
export function flattenTill2ndLastKey(obj: object): [string[], object[]] {
const flattened = {};
const temp = flatten(obj) as object;
for (const k of Object.keys(temp)) {
const tempKeys = Object.keys(temp).filter((key) => typeof temp[key] !== 'object' || !isEmptyObject(temp[key]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following for loop is going to iterate over keys of temp. Rather than iterating again, move these checks inside the loop.

for (const k of tempKeys) {
// taken from https://stackoverflow.com/a/5555607
const pos = k.lastIndexOf('.');
const name = k.substring(0, pos);
Expand Down
13 changes: 9 additions & 4 deletions src/bbs-plus/encoder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SignatureG1 } from './signature';
import { flattenObjectToKeyValuesList, isPositiveInteger } from '../util';
import { flattenObjectToKeyValuesList, isEmptyObject, isPositiveInteger } from '../util';

/**
* A function that encodes the input to field element bytes
Expand Down Expand Up @@ -65,10 +65,15 @@ export class Encoder {
encodeMessageObject(messages: object, strict = false): [string[], Uint8Array[]] {
const [names, values] = flattenObjectToKeyValuesList(messages);
const encoded: Uint8Array[] = [];
for (let i = 0; i < names.length; i++) {
encoded.push(this.encodeMessage(names[i], values[i], strict));
for (let i = names.length - 1; i >= 0; i--) {
// Filter to remove empty objects, this is done because schema generation removes empty objects + nothing to encode
if (typeof values[i] !== 'object' || !isEmptyObject(values[i])) {
encoded.push(this.encodeMessage(names[i], values[i], strict));
} else {
names.splice(i, 1);
}
}
return [names, encoded];
return [names, encoded.reverse()];
}

encodeDefault(value: unknown, strict = false): Uint8Array {
Expand Down
13 changes: 13 additions & 0 deletions tests/anonymous-credentials/credential.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -871,4 +871,17 @@ describe('Credential signing and verification', () => {
checkResult(recreatedCred.verify(pk));
}
});

it('for credential with auto-generated schema and empty objects', () => {
const builder = new CredentialBuilder();
builder.schema = new CredentialSchema(CredentialSchema.essential());
builder.subject = {
fname: 'John',
emptyObject: {},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a similar test for an empty array.

lname: 'Smith',
};

const cred = builder.sign(sk, undefined, {requireSameFieldsAsSchema: false});
checkResult(cred.verify(pk));
});
});