From 26c37ea522e7ec0f9cc1d257b50fa61c1816c897 Mon Sep 17 00:00:00 2001 From: Simon Arnell <14029547+simonarnell@users.noreply.github.com> Date: Wed, 29 Jan 2025 15:18:29 +0200 Subject: [PATCH] added marshalling operation --- ...igitalSecurityByDesignSignedJSONToText.mjs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/core/operations/ConfiguredThingsDigitalSecurityByDesignSignedJSONToText.mjs diff --git a/src/core/operations/ConfiguredThingsDigitalSecurityByDesignSignedJSONToText.mjs b/src/core/operations/ConfiguredThingsDigitalSecurityByDesignSignedJSONToText.mjs new file mode 100644 index 00000000..b0c82fe8 --- /dev/null +++ b/src/core/operations/ConfiguredThingsDigitalSecurityByDesignSignedJSONToText.mjs @@ -0,0 +1,52 @@ +/** + * @author Configured Things Ltd. [getconfigured@configuredthings.com] + * @copyright Crown Copyright 2025 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; + +/** + * Configured Things - Digital Security by Design - Signed JSON to Text operation + */ +class ConfiguredThingsDigitalSecurityByDesignSignedJSONToText extends Operation { + + /** + * ConfiguredThingsDigitalSecurityByDesignSignedJSONToText constructor + */ + constructor() { + super(); + + this.name = "Configured Things - Digital Security by Design - LibHydrogen Signed JSON to Text"; + this.module = "Default"; + this.description = "Converts signed JSON to text"; + this.infoURL = ""; // Usually a Wikipedia link. Remember to remove localisation (i.e. https://wikipedia.org/etc rather than https://en.wikipedia.org/etc) + this.inputType = "JSON"; + this.outputType = "ArrayBuffer"; + this.args = [ + ]; + } + + /** + * @param {JSON} input + * @param {Object[]} args + * @returns {ArrayBuffer} + */ + run(input, args) { + const properties = ["context", "signature", "input"]; + const output = properties.reduce((previousBytes, prop) => { + if (!Object.hasOwn(input, prop)) { + throw new OperationError(`Input missing '${prop}' property`); + } else { + const combinedBytes = new Uint8Array(previousBytes.byteLength + input[prop].byteLength); + combinedBytes.set(previousBytes); + combinedBytes.set(input[prop], previousBytes.byteLength); + return combinedBytes; + } + }, new Uint8Array()); + return output.buffer; + } +} + +export default ConfiguredThingsDigitalSecurityByDesignSignedJSONToText;