How to use the @tanker/crypto.utils.concatArrays function in @tanker/crypto

To help you get started, we’ve selected a few @tanker/crypto examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github TankerHQ / sdk-js / packages / core / src / DataProtection / __tests__ / DecryptorStream.spec.js View on Github external
it('can extract header v4, resource id and message', async () => {
    const spy = sinon.spy(mapper, 'findKey');
    const msg = encryptMsg(0, '1st message');
    const emptyMsg = encryptMsg(1, '');

    stream.write(utils.concatArrays(msg.encrypted, emptyMsg.encrypted));
    stream.end();

    await expect(sync.promise).to.be.fulfilled;

    expect(mapper.findKey.withArgs(resourceId).calledOnce).to.be.true;
    expect(buffer.length).to.equal(1);
    expect(buffer[0]).to.deep.equal(msg.clear);

    spy.restore();
  });
github TankerHQ / sdk-js / packages / core / src / __tests__ / TrustchainVerifier.spec.js View on Github external
function setDelegationKey(user: GeneratorUserResult, ephemeralKeys, delegationPrivateSignatureKey): GeneratorUserResult {
  if (!user.entry.user_id)
    throw new Error('missing user_id');
  const delegationToken = utils.concatArrays(ephemeralKeys.publicKey, user.entry.user_id);
  const payload = {
    delegation_signature: tcrypto.sign(delegationToken, delegationPrivateSignatureKey),
    ephemeral_public_signature_key: ephemeralKeys.publicKey,
  };
  return mergeDeviceCreationV3Payload(user, payload, ephemeralKeys.privateKey);
}
github TankerHQ / sdk-js / packages / core / src / Groups / Serialize.js View on Github external
  ...record.encrypted_group_private_encryption_keys_for_users.map(gek => utils.concatArrays(
    gek.user_id,
    gek.public_user_encryption_key,
    gek.encrypted_group_private_encryption_key
  )),
  ...record.encrypted_group_private_encryption_keys_for_provisional_users.map(gek => utils.concatArrays(
github TankerHQ / sdk-js / packages / core / src / Blocks / BlockGenerator.js View on Github external
export function getUserGroupAdditionBlockSignDataV1(record: UserGroupAdditionRecordV1): Uint8Array {
  return utils.concatArrays(
    record.group_id,
    record.previous_group_block,
    ...record.encrypted_group_private_encryption_keys_for_users.map(gek => utils.concatArrays(gek.public_user_encryption_key, gek.encrypted_group_private_encryption_key))
  );
}
github TankerHQ / sdk-js / packages / core / src / Session / ProvisionalIdentity / Serialize.js View on Github external
export const makeProvisionalIdentityClaim = (userId: Uint8Array, deviceId: Uint8Array, userPublicKey: Uint8Array, provisionalUserKeys: ProvisionalUserKeys) => {
  const multiSignedPayload = utils.concatArrays(
    deviceId,
    provisionalUserKeys.appSignatureKeyPair.publicKey,
    provisionalUserKeys.tankerSignatureKeyPair.publicKey,
  );
  const appSignature = tcrypto.sign(multiSignedPayload, provisionalUserKeys.appSignatureKeyPair.privateKey);
  const tankerSignature = tcrypto.sign(multiSignedPayload, provisionalUserKeys.tankerSignatureKeyPair.privateKey);

  const keysToEncrypt = utils.concatArrays(provisionalUserKeys.appEncryptionKeyPair.privateKey, provisionalUserKeys.tankerEncryptionKeyPair.privateKey);
  const encryptedprovisionalUserKeys = tcrypto.sealEncrypt(keysToEncrypt, userPublicKey);

  const payload = {
    user_id: userId,
    app_provisional_identity_signature_public_key: provisionalUserKeys.appSignatureKeyPair.publicKey,
    tanker_provisional_identity_signature_public_key: provisionalUserKeys.tankerSignatureKeyPair.publicKey,
    author_signature_by_app_key: appSignature,
    author_signature_by_tanker_key: tankerSignature,
    recipient_user_public_key: userPublicKey,
    encrypted_provisional_identity_private_keys: encryptedprovisionalUserKeys,
  };

  return { payload: serializeProvisionalIdentityClaim(payload), nature: preferredNature(NATURE_KIND.provisional_identity_claim) };
};
github TankerHQ / sdk-js / packages / core / src / LocalUser / UserCreation.js View on Github external
export const generateDeviceFromGhostDevice = (
  trustchainId: Uint8Array,
  userId: Uint8Array,
  deviceEncryptionKeyPair: tcrypto.SodiumKeyPair,
  deviceSignatureKeyPair: tcrypto.SodiumKeyPair,
  ghostDevice: GhostDevice,
  ghostDeviceId: Uint8Array,
  userKeys: tcrypto.SodiumKeyPair,
) => {
  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,
github TankerHQ / sdk-js / packages / core / src / Users / Verify.js View on Github external
export function verifyDeviceCreation(entry: DeviceCreationEntry, authorUser: ?User, trustchainPublicKey: Uint8Array) {
  if (!utils.isNullArray(entry.last_reset))
    throw new InvalidBlockError('invalid_last_reset', 'last_reset is not null', { entry });

  const userPublicKey = authorUser ? getLastUserPublicKey(authorUser) : null;
  if (userPublicKey && entry.nature !== NATURE.device_creation_v3)
    throw new InvalidBlockError('forbidden', 'device creation version mismatch', { entry });

  if (!tcrypto.verifySignature(entry.hash, entry.signature, entry.ephemeral_public_signature_key))
    throw new InvalidBlockError('invalid_signature', 'signature is invalid', { entry });

  const delegationBuffer = utils.concatArrays(entry.ephemeral_public_signature_key, entry.user_id);

  if (authorUser) {
    const authorDevice: Device = find(authorUser.devices, d => utils.equalArray(d.deviceId, entry.author));
    if (!tcrypto.verifySignature(delegationBuffer, entry.delegation_signature, authorDevice.devicePublicSignatureKey))
      throw new InvalidBlockError('invalid_delegation_signature', 'invalid signature from device creation author', { entry, authorDevice });

    if (authorDevice.revoked)
      throw new InvalidBlockError('revoked_author_error', 'device creation author is revoked', { entry });

    if (entry.nature === NATURE.device_creation_v3 && userPublicKey && entry.user_key_pair
        && !utils.equalArray(entry.user_key_pair.public_encryption_key, userPublicKey))
      throw new InvalidBlockError('invalid_public_user_key', 'public_user_key is different than the author\'s one', { entry, authorDevice });

    if (!authorUser)
      throw new InternalError('Assertion error: We have an author device, but no author user!?');
github TankerHQ / sdk-js / packages / core / src / Groups / Serialize.js View on Github external
export function serializeUserGroupAdditionV1(userGroupAddition: UserGroupAdditionRecordV1): Uint8Array {
  if (userGroupAddition.previous_group_block.length !== tcrypto.HASH_SIZE)
    throw new InternalError('Assertion error: invalid user group addition previous group block size');
  userGroupAddition.encrypted_group_private_encryption_keys_for_users.forEach(k => checkGroupEncryptedKeyV1('user group add V1', k));
  if (userGroupAddition.self_signature_with_current_key.length !== tcrypto.SIGNATURE_SIZE)
    throw new InternalError('Assertion error: invalid user group addition group self signature size');

  return utils.concatArrays(
    userGroupAddition.group_id,
    userGroupAddition.previous_group_block,
    encodeListLength(userGroupAddition.encrypted_group_private_encryption_keys_for_users),
    ...userGroupAddition.encrypted_group_private_encryption_keys_for_users.map(serializeGroupEncryptedKeyV1),
    userGroupAddition.self_signature_with_current_key,
  );
}
github TankerHQ / sdk-js / packages / core / src / Groups / Serialize.js View on Github external
function serializeGroupEncryptedKeyV2(gek: GroupEncryptedKeyV2): Uint8Array {
  return utils.concatArrays(
    gek.user_id,
    gek.public_user_encryption_key,
    gek.encrypted_group_private_encryption_key
  );
}
github TankerHQ / sdk-js / packages / core / src / Groups / Serialize.js View on Github external
  ...record.encrypted_group_private_encryption_keys_for_users.map(gek => utils.concatArrays(gek.public_user_encryption_key, gek.encrypted_group_private_encryption_key))
);