Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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),
};
}
const payload: DeviceCreationRecord = {
last_reset: new Uint8Array(tcrypto.HASH_SIZE),
ephemeral_public_signature_key: ephemeralKeys.publicKey,
user_id: obfuscatedUserId,
delegation_signature: tcrypto.sign(delegationBuffer, authorPrivateKey),
public_signature_key: signKeys.publicKey,
public_encryption_key: encryptionKeys.publicKey,
is_ghost_device: false,
revoked: Number.MAX_SAFE_INTEGER,
user_key_pair: userKeyPair,
};
this.trustchainIndex += 1;
let serializedPayload = null;
if (args.nature === NATURE.device_creation_v3) {
serializedPayload = serializeUserDeviceV3(payload);
} else {
serializedPayload = serializeUserDeviceV1(payload);
it('should reject a key publish to device with a null recipient', async () => {
const kp = await builder.generator.newKeyPublishToDevice({ toDevice: userV1.device, fromDevice: author.device });
const newRecipient = new Uint8Array(tcrypto.HASH_SIZE);
const newKp = setRecipientKeyPublish(kp, newRecipient, NATURE.key_publish_to_device);
const verif = async () => {
builder.trustchainVerifier._verifyKeyPublish(newKp.unverifiedKeyPublish, verifiedAuthorDevice);
};
await assertFailsWithNature(verif(), 'invalid_recipient');
});
it('can inflate multiple users from different blocks', async () => {
const userId = random(tcrypto.HASH_SIZE);
const userId2 = random(tcrypto.HASH_SIZE);
const testGenerator = new TestGenerator();
const trustchainCreation = testGenerator.makeTrustchainCreation();
const userCreation = await testGenerator.makeUserCreation(userId);
const deviceCreation = await testGenerator.makeDeviceCreation(userCreation);
const deviceCreation2 = await testGenerator.makeDeviceCreation(deviceCreation);
const deviceRevocation = await testGenerator.makeDeviceRevocation(deviceCreation2, deviceCreation2.testDevice.id);
const userCreation2 = await testGenerator.makeUserCreation(userId2);
const deviceCreationUser2 = await testGenerator.makeDeviceCreation(userCreation2);
const blocks = [userCreation.block, userCreation2.block, deviceCreation.block, deviceCreationUser2.block, deviceCreation2.block, deviceRevocation.block];
const { userIdToUserMap, deviceIdToUserIdMap } = await usersFromBlocks(blocks, trustchainCreation.trustchainKeys.publicKey);
it('correctly deserializes a KeyPublishV2 test vector', async () => {
const keyPublish = {
recipient: makeUint8Array('recipient user', tcrypto.HASH_SIZE),
resourceId: makeUint8Array('resource mac', tcrypto.MAC_SIZE),
key: makeUint8Array('encrypted key...', tcrypto.SEALED_KEY_SIZE),
};
const payload = new Uint8Array([
0x72, 0x65, 0x63, 0x69, 0x70, 0x69, 0x65, 0x6e, 0x74, 0x20, 0x75, 0x73, 0x65, 0x72, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x72, 0x65, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x20, 0x6d, 0x61, 0x63, 0x00, 0x00, 0x00, 0x00,
0x65, 0x6e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x65, 0x64, 0x20, 0x6b, 0x65, 0x79, 0x2e, 0x2e, 0x2e,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
]);
expect(unserializeKeyPublish(payload)).to.deep.equal(keyPublish);
it('should throw when unserializing unsupported block version', async () => {
const block = {
author: random(tcrypto.HASH_SIZE),
signature: random(tcrypto.SIGNATURE_SIZE),
trustchain_id: random(tcrypto.HASH_SIZE),
payload: new Uint8Array(0),
nature: NATURE_KIND.key_publish_to_device,
};
const serializedBlock = serializeBlock(block);
serializedBlock[0] = 99;
expect(() => unserializeBlock(serializedBlock)).to.throw(UpgradeRequiredError);
});
it('order stuff correctly for UserGroupCreationV2 sign data', async () => {
const record = {
public_signature_key: random(tcrypto.SIGNATURE_PUBLIC_KEY_SIZE),
public_encryption_key: random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
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),
},
],
before(async () => {
userId = random(tcrypto.HASH_SIZE);
const userCreation = await testGenerator.makeUserCreation(userId);
claim = testGenerator.makeProvisionalIdentityClaim(userCreation, userId, random(tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE));
await unverifiedStore.addUnverifiedProvisionalIdentityClaimEntries([claim.unverifiedProvisionalIdentityClaim]);
});
beforeEach(async () => {
testUserCreation = await testGenerator.makeUserCreation(random(tcrypto.HASH_SIZE));
testGroup = testGenerator.makeUserGroupCreation(testUserCreation, [testUserCreation.user]);
await groupStore.put(testGroup.group);
groupId = testGroup.group.groupId;
});
) => {
const ephemeralKeys = tcrypto.makeSignKeyPair();
const delegationBuffer = utils.concatArrays(ephemeralKeys.publicKey, userId);
const encryptedUserKeyForNewDevice = tcrypto.sealEncrypt(
userKeys.privateKey,
deviceEncryptionKeyPair.publicKey
);
const payload = serializeUserDeviceV3({
ephemeral_public_signature_key: ephemeralKeys.publicKey,
user_id: userId,
delegation_signature: tcrypto.sign(delegationBuffer, ghostDevice.privateSignatureKey),
public_signature_key: deviceSignatureKeyPair.publicKey,
public_encryption_key: deviceEncryptionKeyPair.publicKey,
last_reset: new Uint8Array(tcrypto.HASH_SIZE),
user_key_pair: {
public_encryption_key: userKeys.publicKey,
encrypted_private_encryption_key: encryptedUserKeyForNewDevice,
},
is_ghost_device: false,
revoked: Number.MAX_SAFE_INTEGER,
});
return createBlock(
payload,
preferredNature(NATURE_KIND.device_creation),
trustchainId,
ghostDeviceId,
ephemeralKeys.privateKey
).block;
};
ghostDeviceKeys: GhostDeviceKeys,
delegationToken: DelegationToken
) => {
const userKeys = tcrypto.makeEncryptionKeyPair();
const encryptedUserKey = tcrypto.sealEncrypt(
userKeys.privateKey,
ghostDeviceKeys.encryptionKeyPair.publicKey,
);
const ghostDevicePayload = serializeUserDeviceV3({
ephemeral_public_signature_key: delegationToken.ephemeral_public_signature_key,
user_id: userId,
delegation_signature: delegationToken.delegation_signature,
public_signature_key: ghostDeviceKeys.signatureKeyPair.publicKey,
public_encryption_key: ghostDeviceKeys.encryptionKeyPair.publicKey,
last_reset: new Uint8Array(tcrypto.HASH_SIZE),
user_key_pair: {
public_encryption_key: userKeys.publicKey,
encrypted_private_encryption_key: encryptedUserKey,
},
is_ghost_device: true,
revoked: Number.MAX_SAFE_INTEGER,
});
const { block, hash } = createBlock(
ghostDevicePayload,
preferredNature(NATURE_KIND.device_creation),
trustchainId,
trustchainId,
delegationToken.ephemeral_private_signature_key
);