Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function _generateAppId(appSecret: Uint8Array): b64string { // eslint-disable-line no-underscore-dangle
const publicKey = appSecret.subarray(tcrypto.SIGNATURE_PRIVATE_KEY_SIZE - tcrypto.SIGNATURE_PUBLIC_KEY_SIZE, tcrypto.SIGNATURE_PRIVATE_KEY_SIZE);
return utils.toBase64(utils.generateAppID(publicKey));
}
const encryption = extractEncryptionFormat(castEncryptedData);
const encryptedSize = getDataLength(castEncryptedData);
// $FlowIKnow Already checked we are using a simple encryption
const clearSize = encryption.getClearSize(encryptedSize);
const progressHandler = new ProgressHandler(progressOptions).start(clearSize);
const resourceId = encryption.extractResourceId(castEncryptedData);
const key = await this._resourceManager.findKeyFromResourceId(resourceId);
let clearData;
try {
// $FlowIKnow Already checked we are using a simple encryption
clearData = encryption.decrypt(key, encryption.unserialize(castEncryptedData));
} catch (error) {
const b64ResourceId = utils.toBase64(resourceId);
throw new DecryptionFailed({ error, b64ResourceId });
}
const castClearData = await castData(clearData, outputOptions);
progressHandler.report(clearSize);
return castClearData;
}
async addUnverifiedUserGroupEntries(entries: Array): Promise {
if (entries.length === 0)
return;
const mapEntry = new Map();
const mapEncKeys = new Map();
for (const entry of entries) {
if (natureKind(entry.nature) === NATURE_KIND.user_group_creation) {
const groupCreation: UnverifiedUserGroupCreation = (entry: any);
const b64GroupId = utils.toBase64(entry.group_id);
const dbEntry = entryToDbEntry(entry, b64GroupId);
delete dbEntry.group_public_encryption_key;
mapEntry.set(dbEntry._id, dbEntry); // eslint-disable-line no-underscore-dangle
const dbGroupKey = {
_id: utils.toBase64(groupCreation.public_encryption_key),
group_id: b64GroupId,
};
mapEncKeys.set(dbGroupKey._id, dbGroupKey); // eslint-disable-line no-underscore-dangle
} else if (natureKind(entry.nature) === NATURE_KIND.user_group_addition) {
const groupAddition: UnverifiedUserGroupAddition = (entry: any);
const dbEntry = entryToDbEntry(entry, utils.toBase64(groupAddition.previous_group_block));
mapEntry.set(dbEntry._id, dbEntry); // eslint-disable-line no-underscore-dangle
} else {
throw new InternalError('Assertion failure: entry is not a group entry');
private_keys: [userPrivKey, userPrivKey]
};
notSoSimpleEntry = { ...simpleEntry };
notSoSimpleEntry.payload_unverified = {
device_id: hash,
user_keys: userKeys
};
transformedNotSoSimpleEntry = {
_id: 42,
index: 0,
nature: 8,
hash: utils.toBase64(hash),
author: utils.toBase64(author),
signature: utils.toBase64(signature),
device_id: utils.toBase64(hash),
user_keys: {
public_encryption_key: utils.toBase64(key),
previous_public_encryption_key: utils.toBase64(key),
encrypted_previous_encryption_key: utils.toBase64(key),
private_keys: [tansformedUserPrivKey, tansformedUserPrivKey]
}
};
restoredNotSoSimpleEntry = {
index: 0,
nature: NATURE.key_publish_to_user,
hash,
author,
signature,
async findResourceKey(resourceId: Uint8Array): Promise {
try {
const b64ResourceId = utils.toBase64(resourceId);
const result = await this._ds.get(TABLE, b64ResourceId);
const encryptedKey = utils.fromBase64(result.b64EncryptedKey);
if (encryptedKey.length < encryptionV1.overhead) {
throw new DecryptionFailed({ message: `truncated encrypted data. Length should be at least ${encryptionV1.overhead} for encryption v1` });
}
return encryptionV1.compatDecrypt(this._userSecret, encryptedKey, resourceId);
} catch (e) {
if (e instanceof dbErrors.RecordNotFound) {
return;
}
throw e;
}
}
async getUserIdFromDeviceId(deviceId: Uint8Array): Promise {
const deviceToUser = await this._ds.first(UserUnverifiedStore.tables[TABLE_DEVICE_TO_USER].name, {
selector: {
device_id: { $eq: utils.toBase64(deviceId) },
}
});
if (deviceToUser) {
return utils.fromBase64(deviceToUser.user_id);
}
return null;
}
}
Object.entries(requestObject).forEach(([key, value]) => {
if (value instanceof Uint8Array) {
result[key] = utils.toBase64(value);
} else if (Array.isArray(value)) {
result[key] = b64RequestObject(value);
} else if (isObject(value)) {
result[key] = b64RequestObject(value);
} else {
result[key] = value;
}
});
export const ghostDeviceToUnlockKey = (ghostDevice: GhostDevice) => utils.toB64Json({
privateEncryptionKey: utils.toBase64(ghostDevice.privateEncryptionKey),
privateSignatureKey: utils.toBase64(ghostDevice.privateSignatureKey),
});
_getDeviceKeysFromIds(userIdToUserMap: Map, deviceIdToUserIdMap: Map, devicesIds: Array): Map {
const devicesPublicSignatureKeys: Map = new Map();
for (const deviceId of devicesIds) {
const userId = deviceIdToUserIdMap.get(utils.toBase64(deviceId));
if (!userId) {
throw new InternalError('no such author user id');
}
const user = userIdToUserMap.get(userId);
if (!user) {
throw new InternalError('no such author user');
}
const device = find(user.devices, userDevice => utils.equalArray(userDevice.deviceId, deviceId));
devicesPublicSignatureKeys.set(utils.toBase64(deviceId), device.devicePublicSignatureKey);
}
return devicesPublicSignatureKeys;
}
export function createBlock(payload: Uint8Array, nature: Nature, trustchainId: Uint8Array, author: Uint8Array, signatureKey: Uint8Array) {
const block = {
trustchain_id: trustchainId,
nature,
author,
payload
};
const hash = hashBlock(block);
const signature = tcrypto.sign(hash, signatureKey);
return { block: utils.toBase64(serializeBlock({ ...block, signature })), hash };
}