diff --git a/src/core/operations/JSONToArrayBuffer.mjs b/src/core/operations/JSONToArrayBuffer.mjs new file mode 100644 index 00000000..09c21582 --- /dev/null +++ b/src/core/operations/JSONToArrayBuffer.mjs @@ -0,0 +1,44 @@ +/** + * @author Configured Things Ltd. [getconfigured@configuredthings.com] + * @copyright Crown Copyright 2025 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; + +/** + * JSON to ArrayBuffer operation + */ +class JSONToArrayBuffer extends Operation { + + /** + * JSONToArrayBuffer constructor + */ + constructor() { + super(); + + this.name = "JSON to ArrayBuffer"; + this.module = "Default"; + this.description = "Serialises a JSON object to an ArrayBuffer"; + 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 messageStr = JSON.stringify(input); + const textEncoder = new TextEncoder(); + const messageAb = textEncoder.encode(messageStr).buffer; + return messageAb; + } + +} + +export default JSONToArrayBuffer; diff --git a/src/core/operations/LibHydrogenCurve25519Sign.mjs b/src/core/operations/LibHydrogenCurve25519Sign.mjs index d8f9ad90..fd21426d 100644 --- a/src/core/operations/LibHydrogenCurve25519Sign.mjs +++ b/src/core/operations/LibHydrogenCurve25519Sign.mjs @@ -23,7 +23,7 @@ class LibHydrogenCurve25519Signing extends Operation { this.module = "Crypto"; this.description = "Computes a signature for a message using the lightweight LibHydrogen cryptography library"; this.infoURL = "https://libhydrogen.org/"; - this.inputType = "JSON"; + this.inputType = "ArrayBuffer"; this.outputType = "JSON"; this.args = [ { @@ -41,7 +41,7 @@ class LibHydrogenCurve25519Signing extends Operation { /* eslint-disable camelcase */ /** - * @param {JSON} input + * @param {ArrayBuffer} input * @param {Object[]} args * @returns {JSON} */ @@ -240,8 +240,8 @@ function reserve(offset, length) { */ /** - * Digital signing of a JSON object - * @param {JSON} input - A JSON object to be signed + * Digital signing of an ArrayBuffer's contents + * @param {ArrayBuffer} input - An ArrayBuffer to be signed * @param {string} context - A string used to define the context of the signing operation, * see {@link https://github.com/jedisct1/libhydrogen/wiki/Contexts} * @param {Uint8Array} privateKey - The private key to use for the digital signing operation @@ -264,12 +264,11 @@ async function sign(input, context, privateKey) { contextArr.set([contextAb.at(i)], i); } - const messageStr = JSON.stringify(input); - const messageAb = textEncoder.encode(messageStr); - const messageArr = reserve(offset, messageAb.length); + const messageTypedArr = new Uint8Array(input); + const messageArr = reserve(offset, messageTypedArr.length); - for (let i = 0; i < messageAb.length; i++) { - messageArr.set([messageAb.at(i)], i); + for (let i = 0; i < input.length; i++) { + messageArr.set([messageTypedArr.at(i)], i); } // Generate a key pair diff --git a/src/core/operations/ConfiguredThingsDigitalSecurityByDesignSignedJSONToText.mjs b/src/core/operations/SignedJSONToArrayBuffer.mjs similarity index 81% rename from src/core/operations/ConfiguredThingsDigitalSecurityByDesignSignedJSONToText.mjs rename to src/core/operations/SignedJSONToArrayBuffer.mjs index b0c82fe8..87d61a9d 100644 --- a/src/core/operations/ConfiguredThingsDigitalSecurityByDesignSignedJSONToText.mjs +++ b/src/core/operations/SignedJSONToArrayBuffer.mjs @@ -10,15 +10,15 @@ import OperationError from "../errors/OperationError.mjs"; /** * Configured Things - Digital Security by Design - Signed JSON to Text operation */ -class ConfiguredThingsDigitalSecurityByDesignSignedJSONToText extends Operation { +class SignedJSONToArrayBuffer extends Operation { /** - * ConfiguredThingsDigitalSecurityByDesignSignedJSONToText constructor + * SignedJSONToArrayBuffer constructor */ constructor() { super(); - this.name = "Configured Things - Digital Security by Design - LibHydrogen Signed JSON to Text"; + this.name = "Signed JSON to ArrayBuffer"; 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) @@ -49,4 +49,4 @@ class ConfiguredThingsDigitalSecurityByDesignSignedJSONToText extends Operation } } -export default ConfiguredThingsDigitalSecurityByDesignSignedJSONToText; +export default SignedJSONToArrayBuffer;