Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
beforeEach(async () => {
testGenerator.makeTrustchainCreation();
const userId = random(tcrypto.HASH_SIZE);
const userCreation = await testGenerator.makeUserCreation(userId);
user = userCreation.user;
const provisionalIdentity = testGenerator.makeProvisionalUser().publicProvisionalUser;
const userGroupCreation = testGenerator.makeUserGroupCreation(userCreation, [user], [provisionalIdentity]);
group = userGroupCreation.group;
// Second user
const userId2 = random(tcrypto.HASH_SIZE);
const userCreation2 = await testGenerator.makeUserCreation(userId2);
const userGroupAddition = testGenerator.makeUserGroupAddition(userCreation, userGroupCreation.group, [userCreation2.user]);
userGroupEntry = userGroupAddition.userGroupEntry;
});
if (typeof index !== 'number')
throw new InvalidArgument('index', 'number', index);
if (!(clearData instanceof Uint8Array))
throw new InvalidArgument('clearData', 'Uint8Array', clearData);
if (index < 0)
throw new ChunkIndexOutOfRange(index, this.chunkKeys.length);
if (index > this.chunkKeys.length) {
const rangeSize = index - this.chunkKeys.length;
this.chunkKeys.push(...makeNullArray(rangeSize));
}
const ephemeralKey = random(tcrypto.SYMMETRIC_KEY_SIZE);
// do not use encryptor.encrypt here, it would save the key and possibly share it!
const encryptedData = EncryptorV1.encrypt(ephemeralKey, clearData);
this.chunkKeys[index] = ephemeralKey;
return encryptedData;
}
it('order stuff correctly for UserGroupAdditionV2 sign data', async () => {
const record: UserGroupAdditionRecordV2 = {
group_id: makeUint8Array('group id', tcrypto.HASH_SIZE),
previous_group_block: makeUint8Array('prev group block', tcrypto.HASH_SIZE),
encrypted_group_private_encryption_keys_for_users: [
{
user_id: random(tcrypto.HASH_SIZE),
public_user_encryption_key: makeUint8Array('user pub enc key', tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
encrypted_group_private_encryption_key: makeUint8Array('enc group priv enc key', tcrypto.SEALED_ENCRYPTION_PRIVATE_KEY_SIZE),
},
{
user_id: random(tcrypto.HASH_SIZE),
public_user_encryption_key: makeUint8Array('user pub enc key 2', tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
encrypted_group_private_encryption_key: makeUint8Array('enc group priv enc key 2', tcrypto.SEALED_ENCRYPTION_PRIVATE_KEY_SIZE),
}],
encrypted_group_private_encryption_keys_for_provisional_users: [
{
app_provisional_user_public_signature_key: random(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
tanker_provisional_user_public_signature_key: random(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
encrypted_group_private_encryption_key: random(TWO_TIMES_SEALED_KEY_SIZE),
},
],
self_signature_with_current_key: new Uint8Array(0),
function makeMockClient() {
return {
_callbacks: { blockAvailable: [], started: [], invalid: [] },
_send: async (/*event, payload*/) => { expect.fail(true, false, 'this method should have been called in the tests'); },
emit: function emit(event) { this._callbacks[event].forEach(cb => cb()); },
on: function on(event, cb) { this._callbacks[event].push(cb); },
socket: true,
trustchainId: random(32),
};
}
it('should serialize/unserialize a DeviceRevocation', async () => {
const deviceRevocation = {
device_id: random(tcrypto.HASH_SIZE),
user_keys: {
public_encryption_key: random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
previous_public_encryption_key: random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
encrypted_previous_encryption_key: random(tcrypto.SEALED_KEY_SIZE),
private_keys: [],
},
};
expect(unserializeDeviceRevocationV2(serializeDeviceRevocationV2(deviceRevocation))).to.deep.equal(deviceRevocation);
});
before(() => {
trustchainId = random(tcrypto.HASH_SIZE);
trustchainKeyPair = tcrypto.makeSignKeyPair();
});
beforeEach(() => {
key = random(tcrypto.SYMMETRIC_KEY_SIZE);
resourceId = random(16);
mapper = { findKey: () => Promise.resolve(key) };
stream = new DecryptorStream(mapper);
sync = watchStream(stream);
});
encrypted_group_private_signature_key: random(tcrypto.SEALED_SIGNATURE_PRIVATE_KEY_SIZE),
encrypted_group_private_encryption_keys_for_users: [
{
user_id: random(tcrypto.HASH_SIZE),
public_user_encryption_key: random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
encrypted_group_private_encryption_key: random(tcrypto.SEALED_ENCRYPTION_PRIVATE_KEY_SIZE),
},
{
user_id: random(tcrypto.HASH_SIZE),
public_user_encryption_key: random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
encrypted_group_private_encryption_key: random(tcrypto.SEALED_ENCRYPTION_PRIVATE_KEY_SIZE),
}
],
encrypted_group_private_encryption_keys_for_provisional_users: [
{
app_provisional_user_public_signature_key: random(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
tanker_provisional_user_public_signature_key: random(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
encrypted_group_private_encryption_key: random(TWO_TIMES_SEALED_KEY_SIZE),
},
],
self_signature: new Uint8Array(0),
};
const expectedSignData = utils.concatArrays(
record.public_signature_key,
record.public_encryption_key,
record.encrypted_group_private_signature_key,
record.encrypted_group_private_encryption_keys_for_users[0].user_id,
record.encrypted_group_private_encryption_keys_for_users[0].public_user_encryption_key,
record.encrypted_group_private_encryption_keys_for_users[0].encrypted_group_private_encryption_key,
record.encrypted_group_private_encryption_keys_for_users[1].user_id,
record.encrypted_group_private_encryption_keys_for_users[1].public_user_encryption_key,
export function createUserSecretBinary(trustchainId: b64string, userId: string): Uint8Array {
if (typeof trustchainId !== 'string')
throw new Error('Assertion error: invalid Trustchain ID.');
if (typeof userId !== 'string')
throw new Error('Assertion error: invalid user ID.');
const rand = random(USER_SECRET_SIZE - 1);
const checkByte = checksumByte(rand, obfuscateUserId(fromBase64(trustchainId), userId));
const secret = new Uint8Array(USER_SECRET_SIZE);
secret.set(rand);
secret[USER_SECRET_SIZE - 1] = checkByte;
return secret;
}