@toruslabs/metadata-helpers
Version:
Helper methods for metadata
104 lines (100 loc) • 4.14 kB
JavaScript
;
var eccrypto = require('@toruslabs/eccrypto');
var bytes = require('./helpers/bytes.js');
var utils = require('./helpers/utils.js');
var utils_js = require('@noble/curves/utils.js');
const WEBAUTHN_TORUS_SHARE = "webauthn_torus_share";
const WEBAUTHN_DEVICE_SHARE = "webauthn_device_share";
function encParamsHexToBuf(encParamsHex) {
return {
iv: bytes.hexToBytes(encParamsHex.iv),
ephemPublicKey: bytes.hexToBytes(encParamsHex.ephemPublicKey),
ciphertext: bytes.hexToBytes(encParamsHex.ciphertext),
mac: bytes.hexToBytes(encParamsHex.mac)
};
}
function encParamsBufToHex(encParams) {
return {
iv: utils_js.bytesToHex(encParams.iv),
ephemPublicKey: utils_js.bytesToHex(encParams.ephemPublicKey),
ciphertext: utils_js.bytesToHex(encParams.ciphertext),
mac: utils_js.bytesToHex(encParams.mac)
};
}
async function encryptData(privKeyBytes, d) {
const serializedData = bytes.utf8ToBytes(JSON.stringify(d));
const encParams = await eccrypto.encrypt(eccrypto.getPublic(privKeyBytes), serializedData);
const encParamsHex = encParamsBufToHex(encParams);
return JSON.stringify(encParamsHex);
}
async function decryptData(privKeyBytes, d) {
const encParamsHex = JSON.parse(d);
const encParams = encParamsHexToBuf(encParamsHex);
const serializedBytes = await eccrypto.decrypt(privKeyBytes, encParams);
const data = JSON.parse(bytes.bytesToUtf8(serializedBytes));
return data;
}
async function getAndDecryptData(m, privKeyBytes, namespace) {
const {
x,
y
} = utils.getPublicKeyCoords(privKeyBytes);
const serializedData = await m.getMetadata({
pub_key_X: x,
pub_key_Y: y
}, namespace);
if (!serializedData) {
return null;
}
const data = await decryptData(privKeyBytes, serializedData);
return data;
}
async function encryptAndSetData(m, privKeyBytes, d, namespace) {
const sData = await encryptData(privKeyBytes, d);
const metadataParams = m.generateMetadataParams(sData, privKeyBytes);
await m.setMetadata(metadataParams, namespace);
}
async function setTorusShare(m, webAuthnPubKey, webAuthnRefBytes, subspace, subspaceData) {
const pubKeyBytes = utils.coordsToPublicKey(webAuthnPubKey.pub_key_X, webAuthnPubKey.pub_key_Y);
const data = await getAndDecryptData(m, webAuthnRefBytes, WEBAUTHN_TORUS_SHARE);
let d = {};
if (data) d = data;
const serializedSubspaceData = bytes.utf8ToBytes(JSON.stringify(subspaceData));
const encSubspaceData = await eccrypto.encrypt(pubKeyBytes, serializedSubspaceData);
const encSubspaceDataHex = encParamsBufToHex(encSubspaceData);
d[subspace] = encSubspaceDataHex;
await encryptAndSetData(m, webAuthnRefBytes, d, WEBAUTHN_TORUS_SHARE);
}
async function setDeviceShare(m, webAuthnRefBytes, subspace, subspaceData) {
const data = await getAndDecryptData(m, webAuthnRefBytes, WEBAUTHN_DEVICE_SHARE);
let d = {};
if (data) d = data;
d[subspace] = subspaceData;
await encryptAndSetData(m, webAuthnRefBytes, d, WEBAUTHN_DEVICE_SHARE);
}
async function getTorusShare(m, webAuthnKeyBytes, webAuthnRefBytes, subspace) {
const data = await getAndDecryptData(m, webAuthnRefBytes, WEBAUTHN_TORUS_SHARE);
if (!data) return null;
const encParamsHex = data[subspace];
if (!encParamsHex) return null;
const encParams = encParamsHexToBuf(encParamsHex);
const privKeyBytes = webAuthnKeyBytes;
const serializedBytes = await eccrypto.decrypt(privKeyBytes, encParams);
const subspaceData = JSON.parse(bytes.bytesToUtf8(serializedBytes));
return subspaceData;
}
async function getDeviceShare(m, webAuthnRefBytes, subspace) {
const data = await getAndDecryptData(m, webAuthnRefBytes, WEBAUTHN_DEVICE_SHARE);
if (data) return data[subspace];
return null;
}
exports.decryptData = decryptData;
exports.encParamsBufToHex = encParamsBufToHex;
exports.encParamsHexToBuf = encParamsHexToBuf;
exports.encryptAndSetData = encryptAndSetData;
exports.encryptData = encryptData;
exports.getAndDecryptData = getAndDecryptData;
exports.getDeviceShare = getDeviceShare;
exports.getTorusShare = getTorusShare;
exports.setDeviceShare = setDeviceShare;
exports.setTorusShare = setTorusShare;