Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
_decryptUserKeys = (encryptedUserKeys: Array, deviceId: Uint8Array): LocalUserKeys => {
let localUserKeys;
for (const encryptedUserKey of encryptedUserKeys) {
// Key for local device
if (encryptedUserKey.encrypted_private_encryption_key) {
localUserKeys = this._localUserKeysFromPrivateKey(encryptedUserKey.encrypted_private_encryption_key, this._deviceEncryptionKeyPair, localUserKeys);
continue;
}
// Upgrade from userV1 to userV3
if (utils.equalArray(new Uint8Array(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE), encryptedUserKey.previous_public_encryption_key))
continue;
// Key encrypted before our device creation
const existingUserKey = localUserKeys && localUserKeys.userKeys[utils.toBase64(encryptedUserKey.public_encryption_key)];
if (existingUserKey) {
localUserKeys = this._localUserKeysFromPrivateKey(encryptedUserKey.encrypted_previous_encryption_key, existingUserKey, localUserKeys);
// Key encrypted after our device creation
} else {
const privKeyIndex = findIndex(encryptedUserKey.private_keys, k => utils.equalArray(k.recipient, deviceId));
if (privKeyIndex === -1)
throw new InternalError('Assertion error: Couldn\'t decrypt user keys from revocation');
localUserKeys = this._localUserKeysFromPrivateKey(encryptedUserKey.private_keys[privKeyIndex].key, this._deviceEncryptionKeyPair, localUserKeys);
}
}
if (!localUserKeys) {
devices: parentDevice.testUser.devices.map(d => {
if (utils.equalArray(d.id, deviceIdToRevoke)) {
return { ...d, revoked: true };
}
return { ...d };
}),
userKeys: [...parentDevice.testUser.userKeys]
const remainingDevices = devices.filter(device => device.revoked === false && !utils.equalArray(device.deviceId, deviceId));
const authorDevice: Device = find(authorUser.devices, d => utils.equalArray(d.deviceId, entry.author));
if (!tcrypto.verifySignature(delegationBuffer, entry.delegation_signature, authorDevice.devicePublicSignatureKey))
async _unlockedVerifySingleUserDeviceCreation(user: ?User, entry: DeviceCreationEntry): Promise {
const trustchainPublicKey = this._storage.trustchainStore.trustchainPublicKey;
if (utils.equalArray(entry.author, this._trustchainId)) {
verifyDeviceCreation(entry, null, trustchainPublicKey);
} else {
if (!user)
throw new InvalidBlockError('unknown_author', 'can\'t find block author\'s user', { entry });
verifyDeviceCreation(entry, user, trustchainPublicKey);
}
return entry;
}
export function verifyUserGroupCreation(entry: UserGroupEntry, devicePublicSignatureKey: Uint8Array, existingGroup: ?Group) {
const currentPayload: UserGroupCreationRecord = (entry: any);
if (!tcrypto.verifySignature(entry.hash, entry.signature, devicePublicSignatureKey))
throw new InvalidBlockError('invalid_signature', 'signature is invalid', entry);
if (existingGroup && !utils.equalArray(existingGroup.publicEncryptionKey, currentPayload.public_encryption_key)) {
throw new InvalidBlockError('group_already_exists', 'a group with the same public signature key already exists', entry);
}
let selfSigBuffer;
if (entry.nature === NATURE.user_group_creation_v1) {
const versionedPayload: UserGroupCreationRecordV1 = (currentPayload: any);
selfSigBuffer = getUserGroupCreationBlockSignDataV1(versionedPayload);
} else if (entry.nature === NATURE.user_group_creation_v2) {
const versionedPayload: UserGroupCreationRecordV2 = (currentPayload: any);
selfSigBuffer = getUserGroupCreationBlockSignDataV2(versionedPayload);
} else {
throw new InvalidBlockError('invalid_nature', 'invalid nature for user group creation', { entry });
}
if (!tcrypto.verifySignature(selfSigBuffer, currentPayload.self_signature, currentPayload.public_signature_key))
throw new InvalidBlockError('invalid_self_signature', 'self signature is invalid', entry);
}
const device = find(user.devices, userDevice => utils.equalArray(userDevice.deviceId, deviceId));
devicesPublicSignatureKeys.set(utils.toBase64(deviceId), device.devicePublicSignatureKey);
if (find(deviceIds, deviceId => utils.equalArray(deviceId, device.deviceId))) {
devicesMap.set(utils.toBase64(device.deviceId), device);
_initializeWithUserBlocks = (userBlocks: Array) => {
let user = null;
const encryptedUserKeys: Array = [];
let deviceId;
for (const b64Block of userBlocks) {
const userEntry = userEntryFromBlock(b64Block);
if (isDeviceCreation(userEntry.nature)) {
const deviceCreationEntry = ((userEntry: any): DeviceCreationEntry);
verifyDeviceCreation(deviceCreationEntry, user, this.trustchainPublicKey);
user = applyDeviceCreationToUser(deviceCreationEntry, user);
if (utils.equalArray(this._deviceEncryptionKeyPair.publicKey, deviceCreationEntry.public_encryption_key)) {
deviceId = deviceCreationEntry.hash;
if (deviceCreationEntry.user_key_pair) {
encryptedUserKeys.unshift(deviceCreationEntry.user_key_pair);
}
}
} else if (isDeviceRevocation(userEntry.nature)) {
if (!user) {
throw new InternalError('Assertion error: Cannot revoke device of non existing user');
}
const deviceRevocationEntry = ((userEntry: any): DeviceRevocationEntry);
verifyDeviceRevocation(deviceRevocationEntry, user);
user = applyDeviceRevocationToUser(deviceRevocationEntry, user);
if (this._deviceId && utils.equalArray(deviceRevocationEntry.device_id, this._deviceId)) {
throw new DeviceRevoked();
}
if (deviceRevocationEntry.user_keys) {
const privKeyIndex = findIndex(encryptedUserKey.private_keys, k => utils.equalArray(k.recipient, deviceId));
if (privKeyIndex === -1)