+ // based on ecies-lite's encryption logic, the encryption buffer is concatenated as follows:
+ // [ epk + iv + ct + mac ] whereby:
+ // - The first 32 or 64 chars of the encryptionBuffer is the epk
+ // - Both iv and ct params are 16 chars each, hence their combined substring is 32 chars from the end of the epk string
+ // - within this combined iv/ct substring, the first 16 chars is the iv param, and ct param being the later half
+ // - The mac param is appended to the end of the encryption buffer
+
+ // validate input buffer
+ if (!encryptionBuffer) {
+ throw new Error(
+ 'cashmethods.convertToEncryptStruct() error: input must be a buffer',
+ );
+ }
+
+ try {
+ // variable tracking the starting char position for string extraction purposes
+ let startOfBuf = 0;
+
+ // *** epk param extraction ***
+ // The first char of the encryptionBuffer indicates the type of the public key
+ // If the first char is 4, then the public key is 64 chars
+ // If the first char is 3 or 2, then the public key is 32 chars
+ // Otherwise this is not a valid encryption buffer compatible with the ecies-lite library
+ let publicKey;
+ switch (encryptionBuffer[0]) {
+ case 4:
+ publicKey = encryptionBuffer.slice(0, 65); // extract first 64 chars as public key
+ break;
+ case 3:
+ case 2:
+ publicKey = encryptionBuffer.slice(0, 33); // extract first 32 chars as public key
+ break;
+ default:
+ throw new Error(`Invalid type: ${encryptionBuffer[0]}`);
+ }
+
+ // *** iv and ct param extraction ***
+ startOfBuf += publicKey.length; // sets the starting char position to the end of the public key (epk) in order to extract subsequent iv and ct substrings
+ const encryptionTagLength = 32; // the length of the encryption tag (i.e. mac param) computed from each block of ciphertext, and is used to verify no one has tampered with the encrypted data
+ const ivCtSubstring = encryptionBuffer.slice(
+ startOfBuf,
+ encryptionBuffer.length - encryptionTagLength,
+ ); // extract the substring containing both iv and ct params, which is after the public key but before the mac param i.e. the 'encryption tag'
+ const ivbufParam = ivCtSubstring.slice(0, 16); // extract the first 16 chars of substring as the iv param
+ const ctbufParam = ivCtSubstring.slice(16); // extract the last 16 chars as substring the ct param
+
+ // *** mac param extraction ***
+ const macParam = encryptionBuffer.slice(
+ encryptionBuffer.length - encryptionTagLength,
+ encryptionBuffer.length,
+ ); // extract the mac param appended to the end of the buffer