Some checks failed
morphit-release / Build + publish release tarball (push) Has been cancelled
50 lines
1.7 KiB
JavaScript
50 lines
1.7 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.EncryptedMemoDeserializer = void 0;
|
|
const crypto_1 = require("../crypto");
|
|
// The package's Node entry point still calls deprecated `Buffer()`; this build has the same API.
|
|
const RuntimeByteBuffer = require('bytebuffer/dist/bytebuffer');
|
|
const PublicKeyDeserializer = (buf) => {
|
|
const c = fixed_buf(buf, 33);
|
|
return crypto_1.PublicKey.fromBuffer(c);
|
|
};
|
|
const UInt64Deserializer = (b) => b.readUint64();
|
|
const UInt32Deserializer = (b) => b.readUint32();
|
|
const BinaryDeserializer = (b) => {
|
|
const len = b.readVarint32();
|
|
const b_copy = b.copy(b.offset, b.offset + len);
|
|
b.skip(len);
|
|
return Buffer.from(b_copy.toBinary(), 'binary');
|
|
};
|
|
const BufferDeserializer = (keyDeserializers) => (buf) => {
|
|
const obj = {};
|
|
for (const [key, deserializer] of keyDeserializers) {
|
|
try {
|
|
// Decodes a binary encoded string to a ByteBuffer.
|
|
buf = RuntimeByteBuffer.fromBinary(buf.toString('binary'), RuntimeByteBuffer.LITTLE_ENDIAN);
|
|
obj[key] = deserializer(buf);
|
|
}
|
|
catch (error) {
|
|
error.message = `${key}: ${error.message}`;
|
|
throw error;
|
|
}
|
|
}
|
|
return obj;
|
|
};
|
|
const fixed_buf = (b, len) => {
|
|
if (!b) {
|
|
throw Error('No buffer found on first parameter');
|
|
}
|
|
else {
|
|
const b_copy = b.copy(b.offset, b.offset + len);
|
|
b.skip(len);
|
|
return Buffer.from(b_copy.toBinary(), 'binary');
|
|
}
|
|
};
|
|
exports.EncryptedMemoDeserializer = BufferDeserializer([
|
|
['from', PublicKeyDeserializer],
|
|
['to', PublicKeyDeserializer],
|
|
['nonce', UInt64Deserializer],
|
|
['check', UInt32Deserializer],
|
|
['encrypted', BinaryDeserializer]
|
|
]);
|