From e11a0eda5c416fae07faaa5853e95fa279c631b7 Mon Sep 17 00:00:00 2001 From: ognjenkurtic Date: Fri, 5 Jan 2024 16:17:31 +0100 Subject: [PATCH 01/12] Add basic parser with a failing test --- .../circuitInputParser.service.spec.ts | 21 +++++++++ .../circuitInputParser.service.ts | 45 +++++++++++++++++++ .../circuitInputsMapping.ts | 12 +++++ 3 files changed, 78 insertions(+) create mode 100644 examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts create mode 100644 examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts create mode 100644 examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputsMapping.ts diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts new file mode 100644 index 000000000..d654f7bab --- /dev/null +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts @@ -0,0 +1,21 @@ +import { CircuitInputsParserService } from './circuitInputParser.service'; + +let cips: CircuitInputsParserService; + +beforeAll(async () => { + cips = new CircuitInputsParserService(); +}); + +describe('CircuitInputsParserService', () => { + it('Should fail miserably', () => { + // Arrange + const payload = 'test'; + const schema = {} as CircuitInputsMapping; + + // Act + const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); + + // Assert + expect(circuitInputs).toBe({}); + }); +}); diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts new file mode 100644 index 000000000..3f6ba3bb6 --- /dev/null +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts @@ -0,0 +1,45 @@ +import { Injectable } from "@nestjs/common"; + +@Injectable() +export class CircuitInputsParserService { + + public async applyMappingToJSONPayload(payload: string, cim: CircuitInputsMapping) { + const result: any = {}; + + cim.mapping.forEach(mapping => { + const value = this.getJsonValueByPath(payload, mapping.payloadJsonPath); + + switch (mapping.dataType) { + case "string": + result[mapping.circuitInput] = value ?? mapping.defaultValue; + break; + case "integer": + result[mapping.circuitInput] = value ?? mapping.defaultValue; + break; + case "array": + if (mapping.arrayType === "integer") { + result[mapping.circuitInput] = value ?? mapping.defaultValue; + } + break; + default: + result[mapping.circuitInput] = value; + } + }); + + return result; + } + + private getJsonValueByPath(json: any, path: string) { + const parts = path.split('.'); + let currentValue = json; + + for (const part of parts) { + if (currentValue[part] === undefined) { + return undefined; + } + currentValue = currentValue[part]; + } + + return currentValue; + }; +} \ No newline at end of file diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputsMapping.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputsMapping.ts new file mode 100644 index 000000000..dc1a1ceaf --- /dev/null +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputsMapping.ts @@ -0,0 +1,12 @@ +type CircuitInputsMapping = { + mapping: CircuitInputMapping[]; +}; + +type CircuitInputMapping = { + circuitInput: string; + description: string; + payloadJsonPath: string; + dataType: string; + defaultValue?: any; + arrayType?: string; +}; \ No newline at end of file From 2da298b68c7354fcd196004babd24407208fef9b Mon Sep 17 00:00:00 2001 From: ognjenkurtic Date: Fri, 5 Jan 2024 22:14:03 +0100 Subject: [PATCH 02/12] Add exception handling to circuitInputParser and write a first test to assert error handling --- .../circuitInputParser.service.spec.ts | 2 +- .../circuitInputParser.service.ts | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts index d654f7bab..6af991349 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts @@ -16,6 +16,6 @@ describe('CircuitInputsParserService', () => { const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); // Assert - expect(circuitInputs).toBe({}); + expect(circuitInputs).toBeNull(); }); }); diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts index 3f6ba3bb6..d31097465 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts @@ -3,10 +3,11 @@ import { Injectable } from "@nestjs/common"; @Injectable() export class CircuitInputsParserService { - public async applyMappingToJSONPayload(payload: string, cim: CircuitInputsMapping) { + public applyMappingToJSONPayload(payload: string, cim: CircuitInputsMapping) { const result: any = {}; - cim.mapping.forEach(mapping => { + try { + cim.mapping.forEach(mapping => { const value = this.getJsonValueByPath(payload, mapping.payloadJsonPath); switch (mapping.dataType) { @@ -22,9 +23,13 @@ export class CircuitInputsParserService { } break; default: - result[mapping.circuitInput] = value; + return null; } - }); + }); + + } catch (error) { + return null; + } return result; } From cd0b612072099b9252c7a08dd91eaff935eda907 Mon Sep 17 00:00:00 2001 From: ognjenkurtic Date: Fri, 5 Jan 2024 22:41:03 +0100 Subject: [PATCH 03/12] add tests for single string param at root level --- .../circuitInputParser.service.spec.ts | 69 ++++++++++++++++++- .../circuitInputParser.service.ts | 14 ++-- 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts index 6af991349..e72eabfab 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts @@ -7,7 +7,7 @@ beforeAll(async () => { }); describe('CircuitInputsParserService', () => { - it('Should fail miserably', () => { + it('Should return null if empty CircuitInputsMapping passed in', () => { // Arrange const payload = 'test'; const schema = {} as CircuitInputsMapping; @@ -18,4 +18,71 @@ describe('CircuitInputsParserService', () => { // Assert expect(circuitInputs).toBeNull(); }); + + it('Should generate a single circuit input param based on a single string param at root level', () => { + // Arrange + const payload = + `{ + "supplierInvoiceID": "INV123" + }`; + const schema = { + mapping: [{ + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceID', + dataType: 'string', + } as CircuitInputMapping] + } as CircuitInputsMapping; + + // Act + const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); + + // Assert + expect(circuitInputs).toStrictEqual({ "dascircuitinput": "INV123" }); + }); + + it('Should generate a single circuit input param based on a default value for a missing string param at root level', () => { + // Arrange + const payload = + `{ + "somethingElse": "" + }`; + const schema = { + mapping: [{ + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceID', + dataType: 'string', + defaultValue: '555333' + } as CircuitInputMapping] + } as CircuitInputsMapping; + + // Act + const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); + + // Assert + expect(circuitInputs).toStrictEqual({ "dascircuitinput": "555333" }); + }); + + it('Should return null based on a missing default value for a missing string param at root level', () => { + // Arrange + const payload = + `{ + "somethingElse": "" + }`; + const schema = { + mapping: [{ + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceID', + dataType: 'string', + } as CircuitInputMapping] + } as CircuitInputsMapping; + + // Act + const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); + + // Assert + expect(circuitInputs).toBeNull(); + }); }); diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts index d31097465..dca30b90c 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts @@ -7,9 +7,15 @@ export class CircuitInputsParserService { const result: any = {}; try { - cim.mapping.forEach(mapping => { - const value = this.getJsonValueByPath(payload, mapping.payloadJsonPath); + const jsonPayload = JSON.parse(payload); + for (let mapping of cim.mapping) { + const value = this.getJsonValueByPath(jsonPayload, mapping.payloadJsonPath); + + if (!value && !mapping.defaultValue) { + return null; + } + switch (mapping.dataType) { case "string": result[mapping.circuitInput] = value ?? mapping.defaultValue; @@ -25,8 +31,8 @@ export class CircuitInputsParserService { default: return null; } - }); - + + } } catch (error) { return null; } From e886d95d14d755525c8df95ac78ee10d5d05e222 Mon Sep 17 00:00:00 2001 From: ognjenkurtic Date: Mon, 8 Jan 2024 22:44:40 +0100 Subject: [PATCH 04/12] Add tests for integer type param to circuit input parser --- .../circuitInputParser.service.spec.ts | 81 ++++++++++++++++++- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts index e72eabfab..ebd47e053 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts @@ -25,6 +25,7 @@ describe('CircuitInputsParserService', () => { `{ "supplierInvoiceID": "INV123" }`; + const schema = { mapping: [{ circuitInput: 'dascircuitinput', @@ -47,13 +48,16 @@ describe('CircuitInputsParserService', () => { `{ "somethingElse": "" }`; + + const defaultParamValue = '555333'; + const schema = { mapping: [{ circuitInput: 'dascircuitinput', description: 'desc', payloadJsonPath: 'supplierInvoiceID', dataType: 'string', - defaultValue: '555333' + defaultValue: defaultParamValue } as CircuitInputMapping] } as CircuitInputsMapping; @@ -61,7 +65,7 @@ describe('CircuitInputsParserService', () => { const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); // Assert - expect(circuitInputs).toStrictEqual({ "dascircuitinput": "555333" }); + expect(circuitInputs).toStrictEqual({ "dascircuitinput": defaultParamValue }); }); it('Should return null based on a missing default value for a missing string param at root level', () => { @@ -70,6 +74,7 @@ describe('CircuitInputsParserService', () => { `{ "somethingElse": "" }`; + const schema = { mapping: [{ circuitInput: 'dascircuitinput', @@ -85,4 +90,76 @@ describe('CircuitInputsParserService', () => { // Assert expect(circuitInputs).toBeNull(); }); + + it('Should generate a single circuit input param based on a single integer param at root level', () => { + // Arrange + const payload = + `{ + "supplierInvoiceID": 123 + }`; + + const schema = { + mapping: [{ + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceID', + dataType: 'integer', + } as CircuitInputMapping] + } as CircuitInputsMapping; + + // Act + const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); + + // Assert + expect(circuitInputs).toStrictEqual({ "dascircuitinput": 123 }); + }); + + it('Should generate a single circuit input param based on a default value for a missing integer param at root level', () => { + // Arrange + const payload = + `{ + "somethingElse": "" + }`; + + const defaultValue = 555333; + + const schema = { + mapping: [{ + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceID', + dataType: 'integer', + defaultValue: defaultValue + } as CircuitInputMapping] + } as CircuitInputsMapping; + + // Act + const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); + + // Assert + expect(circuitInputs).toStrictEqual({ "dascircuitinput": defaultValue }); + }); + + it('Should return null based on a missing default value for a missing integer param at root level', () => { + // Arrange + const payload = + `{ + "somethingElse": "" + }`; + + const schema = { + mapping: [{ + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceID', + dataType: 'integer', + } as CircuitInputMapping] + } as CircuitInputsMapping; + + // Act + const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); + + // Assert + expect(circuitInputs).toBeNull(); + }); }); From 290cf6614729042d907cfb498b565cc9c0206298 Mon Sep 17 00:00:00 2001 From: ognjenkurtic Date: Mon, 8 Jan 2024 22:53:33 +0100 Subject: [PATCH 05/12] Add TODOs for handling arrays during circuit input mappings --- .../circuitInputParser.service.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts index dca30b90c..8b095289b 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts @@ -17,15 +17,26 @@ export class CircuitInputsParserService { } switch (mapping.dataType) { + case "string": result[mapping.circuitInput] = value ?? mapping.defaultValue; break; + case "integer": result[mapping.circuitInput] = value ?? mapping.defaultValue; break; + case "array": + if (mapping.arrayType === "string") { + // TODO + } + if (mapping.arrayType === "integer") { - result[mapping.circuitInput] = value ?? mapping.defaultValue; + // TODO + } + + if (mapping.arrayType === "object") { + // TODO } break; default: From 652d07cf58dae2516284b505082a06e7d939aef2 Mon Sep 17 00:00:00 2001 From: ognjenkurtic Date: Mon, 8 Jan 2024 23:01:36 +0100 Subject: [PATCH 06/12] add test to verify nested params properly read --- .../circuitInputParser.service.spec.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts index ebd47e053..c0b9101de 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts @@ -91,6 +91,31 @@ describe('CircuitInputsParserService', () => { expect(circuitInputs).toBeNull(); }); + it('Should generate a single circuit input param based on a single string param at root + 1 level', () => { + // Arrange + const payload = + `{ + "supplierInvoice": { + "ID": "INV123" + } + }`; + + const schema = { + mapping: [{ + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoice.ID', + dataType: 'string', + } as CircuitInputMapping] + } as CircuitInputsMapping; + + // Act + const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); + + // Assert + expect(circuitInputs).toStrictEqual({ "dascircuitinput": "INV123" }); + }); + it('Should generate a single circuit input param based on a single integer param at root level', () => { // Arrange const payload = From d3366cd05f61ca14fc7c57370677d385c478ef23 Mon Sep 17 00:00:00 2001 From: ognjenkurtic Date: Mon, 8 Jan 2024 23:08:56 +0100 Subject: [PATCH 07/12] Add err logging to circuit input parser service --- .../circuitInputsParser/circuitInputParser.service.spec.ts | 5 ++++- .../circuitInputsParser/circuitInputParser.service.ts | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts index c0b9101de..b713ae270 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts @@ -1,9 +1,12 @@ +import { DeepMockProxy, mockDeep } from 'jest-mock-extended'; +import { LoggingService } from '../../../../../shared/logging/logging.service'; import { CircuitInputsParserService } from './circuitInputParser.service'; let cips: CircuitInputsParserService; +const loggingServiceMock: DeepMockProxy = mockDeep(); beforeAll(async () => { - cips = new CircuitInputsParserService(); + cips = new CircuitInputsParserService(loggingServiceMock); }); describe('CircuitInputsParserService', () => { diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts index 8b095289b..c9af9c6dc 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts @@ -1,8 +1,11 @@ import { Injectable } from "@nestjs/common"; +import { LoggingService } from "../../../../../shared/logging/logging.service"; @Injectable() export class CircuitInputsParserService { + constructor(private readonly logger: LoggingService) { } + public applyMappingToJSONPayload(payload: string, cim: CircuitInputsMapping) { const result: any = {}; @@ -13,6 +16,7 @@ export class CircuitInputsParserService { const value = this.getJsonValueByPath(jsonPayload, mapping.payloadJsonPath); if (!value && !mapping.defaultValue) { + this.logger.logError(`Missing value and default value for mapping ${cim.mapping} while mapping circuit inputs for payload ${payload}`); return null; } @@ -40,11 +44,13 @@ export class CircuitInputsParserService { } break; default: + this.logger.logError(`Unknown datatype '${mapping.dataType}' while mapping circuit inputs for payload ${payload}`); return null; } } } catch (error) { + this.logger.logError(`Error '${error}' while mapping circuit inputs for payload ${payload}`); return null; } From ef44d4c90d1b552e03c8f9e7dd0e00b0b5084373 Mon Sep 17 00:00:00 2001 From: ognjenkurtic Date: Mon, 8 Jan 2024 23:25:40 +0100 Subject: [PATCH 08/12] Add support for simple array parsing in circuit input parser, together with a test --- .../circuitInputParser.service.spec.ts | 28 +++++++++++++++++++ .../circuitInputParser.service.ts | 4 +-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts index b713ae270..9a2d24c66 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts @@ -190,4 +190,32 @@ describe('CircuitInputsParserService', () => { // Assert expect(circuitInputs).toBeNull(); }); + + it('Should generate a single circuit input param based on a single integer array param at root level', () => { + // Arrange + const payload = + `{ + "supplierInvoiceIDs": [ + 123, + 321, + 454 + ] + }`; + + const schema = { + mapping: [{ + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceIDs', + dataType: 'array', + arrayType: 'integer', + } as CircuitInputMapping] + } as CircuitInputsMapping; + + // Act + const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); + + // Assert + expect(circuitInputs).toStrictEqual({ "dascircuitinput": [123, 321, 454] }); + }); }); diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts index c9af9c6dc..b7da04899 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts @@ -32,11 +32,11 @@ export class CircuitInputsParserService { case "array": if (mapping.arrayType === "string") { - // TODO + result[mapping.circuitInput] = value ?? mapping.defaultValue; } if (mapping.arrayType === "integer") { - // TODO + result[mapping.circuitInput] = value ?? mapping.defaultValue; } if (mapping.arrayType === "object") { From d6cfdacaf08419bb536ec38b3dac3e423166a1c4 Mon Sep 17 00:00:00 2001 From: ognjenkurtic Date: Mon, 8 Jan 2024 23:38:29 +0100 Subject: [PATCH 09/12] Add charcode sum calc for string params in circuitinputparser --- .../circuitInputParser.service.spec.ts | 12 ++++++------ .../circuitInputParser.service.ts | 18 ++++++++++++++++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts index 9a2d24c66..6431b0fb9 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts @@ -22,7 +22,7 @@ describe('CircuitInputsParserService', () => { expect(circuitInputs).toBeNull(); }); - it('Should generate a single circuit input param based on a single string param at root level', () => { + it('Should generate a single circuit input param with charcode sum based on a single string param at root level', () => { // Arrange const payload = `{ @@ -42,10 +42,10 @@ describe('CircuitInputsParserService', () => { const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); // Assert - expect(circuitInputs).toStrictEqual({ "dascircuitinput": "INV123" }); + expect(circuitInputs).toStrictEqual({ "dascircuitinput": 387 }); }); - it('Should generate a single circuit input param based on a default value for a missing string param at root level', () => { + it('Should generate a single circuit input param with charcode sum based on a default value for a missing string param at root level', () => { // Arrange const payload = `{ @@ -68,7 +68,7 @@ describe('CircuitInputsParserService', () => { const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); // Assert - expect(circuitInputs).toStrictEqual({ "dascircuitinput": defaultParamValue }); + expect(circuitInputs).toStrictEqual({ "dascircuitinput": 312 }); }); it('Should return null based on a missing default value for a missing string param at root level', () => { @@ -94,7 +94,7 @@ describe('CircuitInputsParserService', () => { expect(circuitInputs).toBeNull(); }); - it('Should generate a single circuit input param based on a single string param at root + 1 level', () => { + it('Should generate a single circuit input param with charcode sum based on a single string param at root + 1 level', () => { // Arrange const payload = `{ @@ -116,7 +116,7 @@ describe('CircuitInputsParserService', () => { const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); // Assert - expect(circuitInputs).toStrictEqual({ "dascircuitinput": "INV123" }); + expect(circuitInputs).toStrictEqual({ "dascircuitinput": 387 }); }); it('Should generate a single circuit input param based on a single integer param at root level', () => { diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts index b7da04899..7d5498fd0 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts @@ -23,7 +23,9 @@ export class CircuitInputsParserService { switch (mapping.dataType) { case "string": - result[mapping.circuitInput] = value ?? mapping.defaultValue; + result[mapping.circuitInput] = value ? + this.calculateStringCharCodeSum(value) : + this.calculateStringCharCodeSum(mapping.defaultValue); break; case "integer": @@ -32,7 +34,9 @@ export class CircuitInputsParserService { case "array": if (mapping.arrayType === "string") { - result[mapping.circuitInput] = value ?? mapping.defaultValue; + result[mapping.circuitInput] = value ? + value.map(val => this.calculateStringCharCodeSum(val)) : + mapping.defaultValue.map(val => this.calculateStringCharCodeSum(val)); } if (mapping.arrayType === "integer") { @@ -70,4 +74,14 @@ export class CircuitInputsParserService { return currentValue; }; + + private calculateStringCharCodeSum(text: string): number { + let sum = 0; + + for (let i = 0; i < text.length; i++) { + sum += text.charCodeAt(i); + } + + return sum; + }; } \ No newline at end of file From 8b8161796a85667063450fb92c6ed5549fea58e9 Mon Sep 17 00:00:00 2001 From: ognjenkurtic Date: Mon, 8 Jan 2024 23:41:05 +0100 Subject: [PATCH 10/12] Add test to verify charcode sums for array string params --- .../circuitInputParser.service.spec.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts index 6431b0fb9..cb4a1645c 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts @@ -218,4 +218,32 @@ describe('CircuitInputsParserService', () => { // Assert expect(circuitInputs).toStrictEqual({ "dascircuitinput": [123, 321, 454] }); }); + + it('Should generate a single circuit input param with charcode sums based on a single string array param at root level', () => { + // Arrange + const payload = + `{ + "supplierInvoiceIDs": [ + "INV123", + "INV124", + "INV125" + ] + }`; + + const schema = { + mapping: [{ + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceIDs', + dataType: 'array', + arrayType: 'string', + } as CircuitInputMapping] + } as CircuitInputsMapping; + + // Act + const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); + + // Assert + expect(circuitInputs).toStrictEqual({ "dascircuitinput": [387, 388, 389] }); + }); }); From 29fba718f66637f44732b74eb0d0752555d9a402 Mon Sep 17 00:00:00 2001 From: ognjenkurtic Date: Mon, 8 Jan 2024 23:41:35 +0100 Subject: [PATCH 11/12] Run prettier --- .../circuitInputParser.service.spec.ts | 178 +++++++++--------- .../circuitInputParser.service.ts | 70 ++++--- .../circuitInputsMapping.ts | 16 +- 3 files changed, 141 insertions(+), 123 deletions(-) diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts index cb4a1645c..b6f2d5ffe 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.spec.ts @@ -3,7 +3,8 @@ import { LoggingService } from '../../../../../shared/logging/logging.service'; import { CircuitInputsParserService } from './circuitInputParser.service'; let cips: CircuitInputsParserService; -const loggingServiceMock: DeepMockProxy = mockDeep(); +const loggingServiceMock: DeepMockProxy = + mockDeep(); beforeAll(async () => { cips = new CircuitInputsParserService(loggingServiceMock); @@ -24,67 +25,70 @@ describe('CircuitInputsParserService', () => { it('Should generate a single circuit input param with charcode sum based on a single string param at root level', () => { // Arrange - const payload = - `{ + const payload = `{ "supplierInvoiceID": "INV123" }`; const schema = { - mapping: [{ - circuitInput: 'dascircuitinput', - description: 'desc', - payloadJsonPath: 'supplierInvoiceID', - dataType: 'string', - } as CircuitInputMapping] + mapping: [ + { + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceID', + dataType: 'string', + } as CircuitInputMapping, + ], } as CircuitInputsMapping; // Act const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); // Assert - expect(circuitInputs).toStrictEqual({ "dascircuitinput": 387 }); + expect(circuitInputs).toStrictEqual({ dascircuitinput: 387 }); }); it('Should generate a single circuit input param with charcode sum based on a default value for a missing string param at root level', () => { // Arrange - const payload = - `{ + const payload = `{ "somethingElse": "" }`; const defaultParamValue = '555333'; const schema = { - mapping: [{ - circuitInput: 'dascircuitinput', - description: 'desc', - payloadJsonPath: 'supplierInvoiceID', - dataType: 'string', - defaultValue: defaultParamValue - } as CircuitInputMapping] + mapping: [ + { + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceID', + dataType: 'string', + defaultValue: defaultParamValue, + } as CircuitInputMapping, + ], } as CircuitInputsMapping; // Act const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); // Assert - expect(circuitInputs).toStrictEqual({ "dascircuitinput": 312 }); + expect(circuitInputs).toStrictEqual({ dascircuitinput: 312 }); }); it('Should return null based on a missing default value for a missing string param at root level', () => { // Arrange - const payload = - `{ + const payload = `{ "somethingElse": "" }`; const schema = { - mapping: [{ - circuitInput: 'dascircuitinput', - description: 'desc', - payloadJsonPath: 'supplierInvoiceID', - dataType: 'string', - } as CircuitInputMapping] + mapping: [ + { + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceID', + dataType: 'string', + } as CircuitInputMapping, + ], } as CircuitInputsMapping; // Act @@ -96,92 +100,96 @@ describe('CircuitInputsParserService', () => { it('Should generate a single circuit input param with charcode sum based on a single string param at root + 1 level', () => { // Arrange - const payload = - `{ + const payload = `{ "supplierInvoice": { "ID": "INV123" } }`; const schema = { - mapping: [{ - circuitInput: 'dascircuitinput', - description: 'desc', - payloadJsonPath: 'supplierInvoice.ID', - dataType: 'string', - } as CircuitInputMapping] + mapping: [ + { + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoice.ID', + dataType: 'string', + } as CircuitInputMapping, + ], } as CircuitInputsMapping; // Act const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); // Assert - expect(circuitInputs).toStrictEqual({ "dascircuitinput": 387 }); + expect(circuitInputs).toStrictEqual({ dascircuitinput: 387 }); }); it('Should generate a single circuit input param based on a single integer param at root level', () => { // Arrange - const payload = - `{ + const payload = `{ "supplierInvoiceID": 123 }`; const schema = { - mapping: [{ - circuitInput: 'dascircuitinput', - description: 'desc', - payloadJsonPath: 'supplierInvoiceID', - dataType: 'integer', - } as CircuitInputMapping] + mapping: [ + { + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceID', + dataType: 'integer', + } as CircuitInputMapping, + ], } as CircuitInputsMapping; // Act const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); // Assert - expect(circuitInputs).toStrictEqual({ "dascircuitinput": 123 }); + expect(circuitInputs).toStrictEqual({ dascircuitinput: 123 }); }); it('Should generate a single circuit input param based on a default value for a missing integer param at root level', () => { // Arrange - const payload = - `{ + const payload = `{ "somethingElse": "" }`; const defaultValue = 555333; const schema = { - mapping: [{ - circuitInput: 'dascircuitinput', - description: 'desc', - payloadJsonPath: 'supplierInvoiceID', - dataType: 'integer', - defaultValue: defaultValue - } as CircuitInputMapping] + mapping: [ + { + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceID', + dataType: 'integer', + defaultValue: defaultValue, + } as CircuitInputMapping, + ], } as CircuitInputsMapping; // Act const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); // Assert - expect(circuitInputs).toStrictEqual({ "dascircuitinput": defaultValue }); + expect(circuitInputs).toStrictEqual({ dascircuitinput: defaultValue }); }); it('Should return null based on a missing default value for a missing integer param at root level', () => { // Arrange - const payload = - `{ + const payload = `{ "somethingElse": "" }`; const schema = { - mapping: [{ - circuitInput: 'dascircuitinput', - description: 'desc', - payloadJsonPath: 'supplierInvoiceID', - dataType: 'integer', - } as CircuitInputMapping] + mapping: [ + { + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceID', + dataType: 'integer', + } as CircuitInputMapping, + ], } as CircuitInputsMapping; // Act @@ -193,8 +201,7 @@ describe('CircuitInputsParserService', () => { it('Should generate a single circuit input param based on a single integer array param at root level', () => { // Arrange - const payload = - `{ + const payload = `{ "supplierInvoiceIDs": [ 123, 321, @@ -203,26 +210,27 @@ describe('CircuitInputsParserService', () => { }`; const schema = { - mapping: [{ - circuitInput: 'dascircuitinput', - description: 'desc', - payloadJsonPath: 'supplierInvoiceIDs', - dataType: 'array', - arrayType: 'integer', - } as CircuitInputMapping] + mapping: [ + { + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceIDs', + dataType: 'array', + arrayType: 'integer', + } as CircuitInputMapping, + ], } as CircuitInputsMapping; // Act const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); // Assert - expect(circuitInputs).toStrictEqual({ "dascircuitinput": [123, 321, 454] }); + expect(circuitInputs).toStrictEqual({ dascircuitinput: [123, 321, 454] }); }); it('Should generate a single circuit input param with charcode sums based on a single string array param at root level', () => { // Arrange - const payload = - `{ + const payload = `{ "supplierInvoiceIDs": [ "INV123", "INV124", @@ -231,19 +239,21 @@ describe('CircuitInputsParserService', () => { }`; const schema = { - mapping: [{ - circuitInput: 'dascircuitinput', - description: 'desc', - payloadJsonPath: 'supplierInvoiceIDs', - dataType: 'array', - arrayType: 'string', - } as CircuitInputMapping] + mapping: [ + { + circuitInput: 'dascircuitinput', + description: 'desc', + payloadJsonPath: 'supplierInvoiceIDs', + dataType: 'array', + arrayType: 'string', + } as CircuitInputMapping, + ], } as CircuitInputsMapping; // Act const circuitInputs = cips.applyMappingToJSONPayload(payload, schema); // Assert - expect(circuitInputs).toStrictEqual({ "dascircuitinput": [387, 388, 389] }); + expect(circuitInputs).toStrictEqual({ dascircuitinput: [387, 388, 389] }); }); }); diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts index 7d5498fd0..dd541ead0 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts @@ -1,10 +1,9 @@ -import { Injectable } from "@nestjs/common"; -import { LoggingService } from "../../../../../shared/logging/logging.service"; +import { Injectable } from '@nestjs/common'; +import { LoggingService } from '../../../../../shared/logging/logging.service'; @Injectable() export class CircuitInputsParserService { - - constructor(private readonly logger: LoggingService) { } + constructor(private readonly logger: LoggingService) {} public applyMappingToJSONPayload(payload: string, cim: CircuitInputsMapping) { const result: any = {}; @@ -13,48 +12,57 @@ export class CircuitInputsParserService { const jsonPayload = JSON.parse(payload); for (let mapping of cim.mapping) { - const value = this.getJsonValueByPath(jsonPayload, mapping.payloadJsonPath); - + const value = this.getJsonValueByPath( + jsonPayload, + mapping.payloadJsonPath, + ); + if (!value && !mapping.defaultValue) { - this.logger.logError(`Missing value and default value for mapping ${cim.mapping} while mapping circuit inputs for payload ${payload}`); + this.logger.logError( + `Missing value and default value for mapping ${cim.mapping} while mapping circuit inputs for payload ${payload}`, + ); return null; } - - switch (mapping.dataType) { - case "string": - result[mapping.circuitInput] = value ? - this.calculateStringCharCodeSum(value) : - this.calculateStringCharCodeSum(mapping.defaultValue); + switch (mapping.dataType) { + case 'string': + result[mapping.circuitInput] = value + ? this.calculateStringCharCodeSum(value) + : this.calculateStringCharCodeSum(mapping.defaultValue); break; - case "integer": + case 'integer': result[mapping.circuitInput] = value ?? mapping.defaultValue; break; - - case "array": - if (mapping.arrayType === "string") { - result[mapping.circuitInput] = value ? - value.map(val => this.calculateStringCharCodeSum(val)) : - mapping.defaultValue.map(val => this.calculateStringCharCodeSum(val)); + + case 'array': + if (mapping.arrayType === 'string') { + result[mapping.circuitInput] = value + ? value.map((val) => this.calculateStringCharCodeSum(val)) + : mapping.defaultValue.map((val) => + this.calculateStringCharCodeSum(val), + ); } - if (mapping.arrayType === "integer") { + if (mapping.arrayType === 'integer') { result[mapping.circuitInput] = value ?? mapping.defaultValue; } - if (mapping.arrayType === "object") { + if (mapping.arrayType === 'object') { // TODO } break; - default: - this.logger.logError(`Unknown datatype '${mapping.dataType}' while mapping circuit inputs for payload ${payload}`); + default: + this.logger.logError( + `Unknown datatype '${mapping.dataType}' while mapping circuit inputs for payload ${payload}`, + ); return null; } - } } catch (error) { - this.logger.logError(`Error '${error}' while mapping circuit inputs for payload ${payload}`); + this.logger.logError( + `Error '${error}' while mapping circuit inputs for payload ${payload}`, + ); return null; } @@ -64,16 +72,16 @@ export class CircuitInputsParserService { private getJsonValueByPath(json: any, path: string) { const parts = path.split('.'); let currentValue = json; - + for (const part of parts) { if (currentValue[part] === undefined) { return undefined; } currentValue = currentValue[part]; } - + return currentValue; - }; + } private calculateStringCharCodeSum(text: string): number { let sum = 0; @@ -83,5 +91,5 @@ export class CircuitInputsParserService { } return sum; - }; -} \ No newline at end of file + } +} diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputsMapping.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputsMapping.ts index dc1a1ceaf..ec0e1a510 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputsMapping.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputsMapping.ts @@ -1,12 +1,12 @@ type CircuitInputsMapping = { - mapping: CircuitInputMapping[]; + mapping: CircuitInputMapping[]; }; type CircuitInputMapping = { - circuitInput: string; - description: string; - payloadJsonPath: string; - dataType: string; - defaultValue?: any; - arrayType?: string; -}; \ No newline at end of file + circuitInput: string; + description: string; + payloadJsonPath: string; + dataType: string; + defaultValue?: any; + arrayType?: string; +}; From 5762fc2a79c8f5155e2ea5a06f50d0ed55b9031c Mon Sep 17 00:00:00 2001 From: ognjenkurtic Date: Sat, 20 Jan 2024 23:09:46 +0100 Subject: [PATCH 12/12] improve condition to check if the value or the default value of a circuit input is provided in the circuit input parser; Simplify calculateStringCharCodeSum call in case of string type --- .../circuitInputsParser/circuitInputParser.service.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts index dd541ead0..05fe7be54 100644 --- a/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts +++ b/examples/bri-3/src/bri/zeroKnowledgeProof/services/circuit/circuitInputsParser/circuitInputParser.service.ts @@ -17,7 +17,7 @@ export class CircuitInputsParserService { mapping.payloadJsonPath, ); - if (!value && !mapping.defaultValue) { + if (value === undefined && mapping.defaultValue === undefined) { this.logger.logError( `Missing value and default value for mapping ${cim.mapping} while mapping circuit inputs for payload ${payload}`, ); @@ -26,9 +26,7 @@ export class CircuitInputsParserService { switch (mapping.dataType) { case 'string': - result[mapping.circuitInput] = value - ? this.calculateStringCharCodeSum(value) - : this.calculateStringCharCodeSum(mapping.defaultValue); + result[mapping.circuitInput] = this.calculateStringCharCodeSum(value || mapping.defaultValue); break; case 'integer':