Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async _unlockedVerifySingleUser(user: ?User, entry: UserEntry): Promise {
switch (natureKind(entry.nature)) {
case NATURE_KIND.device_creation: {
// $FlowIKnow The type is checked by the switch
const deviceEntry: DeviceCreationEntry = entry;
return this._unlockedVerifySingleUserDeviceCreation(user, deviceEntry);
}
case NATURE_KIND.device_revocation: {
// $FlowIKnow Type is checked by the switch
const revocationEntry: UnverifiedDeviceRevocation = entry;
return this._unlockedVerifySingleUserDeviceRevocation(user, revocationEntry);
}
default:
throw new InternalError(`Assertion error: unexpected nature ${entry.nature}`);
}
}
async removeListener(id: number) {
const synchronizedListener = this.synchronizedListeners[id];
if (!synchronizedListener)
throw new InternalError(`could not find synchronizedListener with id=${id}`);
this.subEmitter.removeListener(synchronizedListener.eventName, synchronizedListener.listener);
await synchronizedListener.disable();
delete this.synchronizedListeners[id];
}
}
function checkGroupEncryptedKeyV2(blockType: string, key: GroupEncryptedKeyV2): void {
if (key.user_id.length !== tcrypto.HASH_SIZE)
throw new InternalError(`Assertion error: invalid ${blockType} recipient user id size`);
if (key.public_user_encryption_key.length !== tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE)
throw new InternalError(`Assertion error: invalid ${blockType} recipient user public key size`);
if (key.encrypted_group_private_encryption_key.length !== tcrypto.SEALED_ENCRYPTION_PRIVATE_KEY_SIZE)
throw new InternalError(`Assertion error: invalid ${blockType} encrypted group private encryption key size`);
}
async _simpleEncryptDataWithResourceId(clearData: Data, sharingOptions: SharingOptions, outputOptions: OutputOptions, progressOptions: ProgressOptions, b64ResourceId: b64string): Promise {
const encryption = getSimpleEncryptionWithFixedResourceId();
const clearSize = getDataLength(clearData);
const encryptedSize = encryption.getEncryptedSize(clearSize);
const progressHandler = new ProgressHandler(progressOptions).start(encryptedSize);
const castClearData = await castData(clearData, { type: Uint8Array });
if (typeof b64ResourceId !== 'string')
throw new InternalError('Assertion error: called _simpleEncryptDataWithResourceId without a resourceId');
const resourceId = utils.fromBase64(b64ResourceId);
const key = await this._resourceManager.findKeyFromResourceId(resourceId);
const encryptedData = encryption.serialize(encryption.encrypt(key, castClearData, resourceId));
const castEncryptedData = await castData(encryptedData, outputOptions);
progressHandler.report(encryptedSize);
return castEncryptedData;
}
return { ...userGroupAction, author, signature, nature, hash, index };
}
if (block.nature === NATURE.user_group_creation_v2) {
const userGroupAction = unserializeUserGroupCreationV2(block.payload);
return { ...userGroupAction, author, signature, nature, hash, index };
}
if (block.nature === NATURE.user_group_addition_v1) {
const userGroupAction = unserializeUserGroupAdditionV1(block.payload);
return { ...userGroupAction, author, signature, nature, hash, index };
}
if (block.nature === NATURE.user_group_addition_v2) {
const userGroupAction = unserializeUserGroupAdditionV2(block.payload);
return { ...userGroupAction, author, signature, nature, hash, index };
}
throw new InternalError('Assertion error: wrong type for getGroupEntryFromBlock');
}
export function serializeUserGroupCreationV2(userGroupCreation: UserGroupCreationRecordV2): Uint8Array {
if (userGroupCreation.public_signature_key.length !== tcrypto.SIGNATURE_PUBLIC_KEY_SIZE)
throw new InternalError('Assertion error: invalid user group creation group public signature key size');
if (userGroupCreation.public_encryption_key.length !== tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE)
throw new InternalError('Assertion error: invalid user group creation group public encryption key size');
if (userGroupCreation.encrypted_group_private_signature_key.length !== tcrypto.SEALED_SIGNATURE_PRIVATE_KEY_SIZE)
throw new InternalError('Assertion error: invalid user group creation encrypted group private signature key size');
userGroupCreation.encrypted_group_private_encryption_keys_for_users.forEach(k => checkGroupEncryptedKeyV2('user group creation V2', k));
userGroupCreation.encrypted_group_private_encryption_keys_for_provisional_users.forEach(k => checkProvisionalGroupEncryptedKeyV2('user group creation V2', k));
if (userGroupCreation.self_signature.length !== tcrypto.SIGNATURE_SIZE)
throw new InternalError('Assertion error: invalid user group creation group self signature size');
return utils.concatArrays(
userGroupCreation.public_signature_key,
userGroupCreation.public_encryption_key,
userGroupCreation.encrypted_group_private_signature_key,
encodeListLength(userGroupCreation.encrypted_group_private_encryption_keys_for_users),
...userGroupCreation.encrypted_group_private_encryption_keys_for_users.map(serializeGroupEncryptedKeyV2),
encodeListLength(userGroupCreation.encrypted_group_private_encryption_keys_for_provisional_users),
...userGroupCreation.encrypted_group_private_encryption_keys_for_provisional_users.map(serializeProvisionalGroupEncryptedKeyV2),
userGroupCreation.self_signature,
);
}
export function serializeDeviceRevocationV2(deviceRevocation: DeviceRevocationRecord): Uint8Array {
if (deviceRevocation.device_id.length !== tcrypto.HASH_SIZE)
throw new InternalError('Assertion error: invalid device revocation device_id size');
if (!deviceRevocation.user_keys)
throw new InternalError('Assertion error: invalid user device user keys');
if (deviceRevocation.user_keys.public_encryption_key.length !== tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE)
throw new InternalError('Assertion error: invalid user device user public encryption key size');
if (deviceRevocation.user_keys.previous_public_encryption_key.length !== tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE)
throw new InternalError('Assertion error: invalid user device user previous public encryption key size');
if (deviceRevocation.user_keys.encrypted_previous_encryption_key.length !== tcrypto.SEALED_KEY_SIZE)
throw new InternalError('Assertion error: invalid user device user previous encrypted private encryption key size');
for (const key of deviceRevocation.user_keys.private_keys) {
if (key.recipient.length !== tcrypto.HASH_SIZE)
throw new InternalError('Assertion error: invalid user device encrypted key recipient size');
if (key.key.length !== tcrypto.SEALED_KEY_SIZE)
throw new InternalError('Assertion error: invalid user device user encrypted private encryption key size');
}
return utils.concatArrays(
async _unlockedVerifySingleUserDeviceRevocation(targetUser: ?User, entry: DeviceRevocationEntry): Promise {
if (!targetUser)
throw new InternalError('Cannot revoke device of non existing user');
verifyDeviceRevocation(entry, targetUser);
return entry;
}
export function serializeProvisionalIdentityClaim(provisionalIdentityClaim: ProvisionalIdentityClaimRecord): Uint8Array {
if (provisionalIdentityClaim.user_id.length !== tcrypto.HASH_SIZE)
throw new InternalError('Assertion error: invalid claim provisional user id size');
if (provisionalIdentityClaim.app_provisional_identity_signature_public_key.length !== tcrypto.SIGNATURE_PUBLIC_KEY_SIZE)
throw new InternalError('Assertion error: invalid claim provisional app public key size');
if (provisionalIdentityClaim.tanker_provisional_identity_signature_public_key.length !== tcrypto.SIGNATURE_PUBLIC_KEY_SIZE)
throw new InternalError('Assertion error: invalid claim provisional tanker public key size');
if (provisionalIdentityClaim.author_signature_by_app_key.length !== tcrypto.SIGNATURE_SIZE)
throw new InternalError('Assertion error: invalid claim provisional app signature size');
if (provisionalIdentityClaim.author_signature_by_tanker_key.length !== tcrypto.SIGNATURE_SIZE)
throw new InternalError('Assertion error: invalid claim provisional tanker signature size');
if (provisionalIdentityClaim.recipient_user_public_key.length !== tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE)
throw new InternalError('Assertion error: invalid claim provisional recipient key size');
if (provisionalIdentityClaim.encrypted_provisional_identity_private_keys.length !== tcrypto.ENCRYPTION_PRIVATE_KEY_SIZE * 2
+ tcrypto.SEAL_OVERHEAD)
throw new InternalError('Assertion error: invalid claim provisional encrypted keys size');
return utils.concatArrays(
provisionalIdentityClaim.user_id,
provisionalIdentityClaim.app_provisional_identity_signature_public_key,
provisionalIdentityClaim.tanker_provisional_identity_signature_public_key,
provisionalIdentityClaim.author_signature_by_app_key,
provisionalIdentityClaim.author_signature_by_tanker_key,
provisionalIdentityClaim.recipient_user_public_key,
provisionalIdentityClaim.encrypted_provisional_identity_private_keys,
);
}
export function serializeBlock(block: Block): Uint8Array {
if (block.author.length !== hashSize)
throw new InternalError('Assertion error: invalid block author size');
if (block.signature.length !== signatureSize)
throw new InternalError('Assertion error: invalid block signature size');
if (block.trustchain_id.length !== trustchainIdSize)
throw new InternalError('Assertion error: invalid block trustchain_id size');
return utils.concatArrays(
new Uint8Array(varint.encode(currentVersion)),
new Uint8Array(varint.encode(block.index)),
block.trustchain_id,
new Uint8Array(varint.encode(block.nature)),
encodeArrayLength(block.payload),
block.payload,
block.author,
block.signature
);
}