Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should serialize/unserialize a UserDeviceV3', async () => {
const ephemeralKeys = tcrypto.makeSignKeyPair();
const signatureKeys = tcrypto.makeSignKeyPair();
const encryptionKeys = tcrypto.makeEncryptionKeyPair();
const userDevice = {
last_reset: new Uint8Array(tcrypto.HASH_SIZE),
ephemeral_public_signature_key: ephemeralKeys.publicKey,
user_id: utils.fromString('12341234123412341234123412341234'),
delegation_signature: utils.fromString('1234123412341234123412341234123412341234123412341234123412341234'),
public_signature_key: signatureKeys.publicKey,
public_encryption_key: encryptionKeys.publicKey,
user_key_pair: {
public_encryption_key: makeUint8Array('user pub enc key', tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
encrypted_private_encryption_key: makeUint8Array('user enc priv key', tcrypto.SEALED_KEY_SIZE),
},
is_ghost_device: true,
revoked: Number.MAX_SAFE_INTEGER,
};
expect(unserializeUserDeviceV3(serializeUserDeviceV3(userDevice))).to.deep.equal(userDevice);
});
it('throws when decrypting using a session in an invalid state', async () => {
await expect(bobLaptop.decrypt(utils.fromString('test'))).to.be.rejectedWith(errors.PreconditionFailed);
});
});
it('should throw if the last reset is not null when serializing a new userDeviceV3', async () => {
const ephemeralKeys = tcrypto.makeSignKeyPair();
const signatureKeys = tcrypto.makeSignKeyPair();
const encryptionKeys = tcrypto.makeEncryptionKeyPair();
const userDevice = {
last_reset: new Uint8Array(Array.from({ length: tcrypto.HASH_SIZE }, () => 1)),
ephemeral_public_signature_key: ephemeralKeys.publicKey,
user_id: utils.fromString('12341234123412341234123412341234'),
delegation_signature: utils.fromString('1234123412341234123412341234123412341234123412341234123412341234'),
public_signature_key: signatureKeys.publicKey,
public_encryption_key: encryptionKeys.publicKey,
user_key_pair: {
public_encryption_key: makeUint8Array('user pub enc key', tcrypto.ENCRYPTION_PUBLIC_KEY_SIZE),
encrypted_private_encryption_key: makeUint8Array('user enc priv key', tcrypto.SEALED_KEY_SIZE),
},
is_ghost_device: true,
revoked: Number.MAX_SAFE_INTEGER,
};
expect(() => serializeUserDeviceV3(userDevice)).to.throw();
});
before(() => {
key = utils.fromString('12345678123456781234567812345678');
resourceId = random(tcrypto.MAC_SIZE);
});
async encrypt(plain: string, options?: $Shape & ProgressOptions>): Promise {
this.assert(statuses.READY, 'encrypt');
if (typeof plain !== 'string')
throw new InvalidArgument('plain', 'string', plain);
return this.encryptData(utils.fromString(plain), options);
}
async _encryptAndShareMetadata(metadata: Object, b64ResourceId: b64string): Promise {
const jsonMetadata = JSON.stringify(metadata);
const clearMetadata = utils.fromString(jsonMetadata);
const encryptedMetadata = await this._dataProtector.encryptData(clearMetadata, {}, { type: Uint8Array }, {}, b64ResourceId);
return utils.toBase64(encryptedMetadata);
}
unlockKey,
userSecret,
privateSigKey
}: CreateUnlockKeyMessageParams): Promise {
const message = {
trustchainId,
deviceId,
claims: {},
signature: new Uint8Array(0)
};
if (email)
message.claims.email = utils.fromString(email);
if (password)
message.claims.password = generichash(utils.fromString(password));
if (unlockKey)
message.claims.unlockKey = EncryptorV2.encrypt(userSecret, utils.fromString(unlockKey));
const buff = getSignData(message);
message.signature = tcrypto.sign(buff, privateSigKey);
return message;
}