Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
createUser(args: { userId: string, parentDevice?: GeneratorDevice, userKeys: tcrypto.SodiumKeyPair, nature: Nature}): CreateUserResult {
const ephemeralKeys = tcrypto.makeSignKeyPair();
const signKeys = tcrypto.makeSignKeyPair();
const encryptionKeys = tcrypto.makeEncryptionKeyPair();
const obfuscatedUserId = obfuscateUserId(this.trustchainId, args.userId);
const delegationBuffer = utils.concatArrays(ephemeralKeys.publicKey, obfuscatedUserId);
let authorPrivateKey = this.appSignKeys.privateKey;
let author = this.root.entry.hash;
if (args.parentDevice) {
// A parent device exists so we are in the add Device case
authorPrivateKey = args.parentDevice.signKeys.privateKey;
author = args.parentDevice.id;
}
let userKeyPair = null;
if (args.nature === NATURE.device_creation_v3) {
userKeyPair = {
public_encryption_key: args.userKeys.publicKey,
encrypted_private_encryption_key: new Uint8Array(SEALED_KEY_SIZE),
it('saves a key pair and finds group public key', async () => {
const groupKeyPair = tcrypto.makeEncryptionKeyPair();
const groupId = random(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE);
await groupStore.saveGroupKeyPair(groupId, groupKeyPair);
const resKey = await groupStore.findGroupsPublicKeys([groupId]);
expect(resKey).to.deep.equal([{ groupId, publicEncryptionKey: groupKeyPair.publicKey }]);
});
it('can create a user group', async () => {
const groupSignatureKeyPair = tcrypto.makeSignKeyPair();
const groupEncryptionKeyPair = tcrypto.makeEncryptionKeyPair();
const block = blockGenerator.createUserGroup(
groupSignatureKeyPair,
groupEncryptionKeyPair,
[user],
[]
);
const entry = getGroupEntryFromBlock(block);
const record: UserGroupCreationRecordV2 = (entry: any);
expect(record.public_signature_key).to.deep.equal(groupSignatureKeyPair.publicKey);
expect(record.public_encryption_key).to.deep.equal(groupEncryptionKeyPair.publicKey);
expect(tcrypto.sealDecrypt(record.encrypted_group_private_signature_key, groupEncryptionKeyPair)).to.deep.equal(groupSignatureKeyPair.privateKey);
expect(record.encrypted_group_private_encryption_keys_for_users.length).to.deep.equal(1);
expect(record.encrypted_group_private_encryption_keys_for_users[0].public_user_encryption_key).to.deep.equal(userKeys.publicKey);
expect(tcrypto.sealDecrypt(record.encrypted_group_private_encryption_keys_for_users[0].encrypted_group_private_encryption_key, userKeys)).to.deep.equal(groupEncryptionKeyPair.privateKey);
it('returns null when asked for non existing group key pair', async () => {
const groupKeyPair = tcrypto.makeEncryptionKeyPair();
const groupId = random(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE);
await groupStore.saveGroupKeyPair(groupId, groupKeyPair);
const publicKey = random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE);
const resKeyPair = await groupStore.findGroupKeyPair(publicKey);
expect(resKeyPair).to.be.null;
});
it('ignores updates to a group private key', async () => {
const groupKeyPair = tcrypto.makeEncryptionKeyPair();
const groupId = random(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE);
const groupId2 = random(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE);
const groupKeyPair2 = {
publicKey: groupKeyPair.publicKey,
privateKey: random(tcrypto.ENCRYPTION_PRIVATE_KEY_SIZE),
};
await groupStore.saveGroupKeyPair(groupId, groupKeyPair);
await groupStore.saveGroupKeyPair(groupId2, groupKeyPair2);
const resKeyPair = await groupStore.findGroupKeyPair(groupKeyPair.publicKey);
expect(resKeyPair).to.deep.equal(groupKeyPair);
});
async newDeviceCreationV1(args: { userId: string, parentIndex: number }): Promise {
const { userId, parentIndex } = args;
if (!this.users[userId])
throw new Error(`Generator: cannot add device: ${userId} does not exist`);
const user = this.users[userId];
const { devices } = user;
if (parentIndex > devices.length)
throw new Error('Generator: cannot add device: index out of bounds');
const parentDevice = devices[parentIndex];
const result = this.createUser({ userId, userKeys: tcrypto.makeEncryptionKeyPair(), parentDevice, nature: NATURE.device_creation_v1 });
this.users[userId] = { ...user, devices: [...user.devices, result.device] };
this.usersDevices[utils.toBase64(result.entry.hash)] = userId;
this.pushedBlocks.push(result.block);
return {
...result,
user: { ...this.users[userId] },
};
}
constructor() {
const signatureKeyPair = tcrypto.makeSignKeyPair();
const encryptionKeyPair = tcrypto.makeEncryptionKeyPair();
const userKeyPair = tcrypto.makeEncryptionKeyPair();
this.keyStore = {
publicSignatureKey: signatureKeyPair.publicKey,
privateSignatureKey: signatureKeyPair.privateKey,
publicEncryptionKey: encryptionKeyPair.publicKey,
signatureKeyPair,
encryptionKeyPair,
wasRevoked: false,
deviceId: random(tcrypto.HASH_SIZE),
userKeys: [userKeyPair]
};
this.userStore = {
setCallbacks: () => {},
};
}
hasLocalDevice = () => true;
makeDeviceCreation = (parentDevice: TestDeviceCreation): TestDeviceCreation => {
const deviceSignatureKeyPair = tcrypto.makeSignKeyPair();
const deviceEncryptionKeyPair = tcrypto.makeEncryptionKeyPair();
const testUserKeys = parentDevice.testUser.userKeys[parentDevice.testUser.userKeys.length - 1];
const userKeys = { publicKey: testUserKeys.publicKey, privateKey: testUserKeys.privateKey };
const newDeviceBlock = generateDeviceFromGhostDevice(this._trustchainId, parentDevice.testUser.id, deviceEncryptionKeyPair, deviceSignatureKeyPair, parentDevice.testUser.ghostDevice, parentDevice.testUser.devices[0].id, userKeys);
const unverifiedDeviceCreation = ((userEntryFromBlock(newDeviceBlock): any): DeviceCreationEntry);
const testDevice: TestDevice = {
id: unverifiedDeviceCreation.hash,
signKeys: deviceSignatureKeyPair,
encryptionKeys: deviceEncryptionKeyPair,
revoked: false,
isGhost: false,
};
const testUser = { ...parentDevice.testUser };
testUser.devices.push(testDevice);
const rotateUserKeys = (devices: Array, currentUserKey: tcrypto.SodiumKeyPair): UserKeys => {
const newUserKeyPair = tcrypto.makeEncryptionKeyPair();
const encryptedPreviousUserKey = tcrypto.sealEncrypt(
currentUserKey.privateKey,
newUserKeyPair.publicKey,
);
const encryptedUserKeyForDevices = devices.map(device => {
const encryptedUserKey = tcrypto.sealEncrypt(
newUserKeyPair.privateKey,
device.devicePublicEncryptionKey,
);
return {
recipient: device.deviceId,
key: encryptedUserKey,
};
});