How to use the @tanker/crypto.tcrypto.sign 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 / __tests__ / Generator.js View on Github external
// 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);
    }

    const block = signBlock({
github TankerHQ / sdk-js / packages / core / src / __tests__ / TestGenerator.js View on Github external
function createDelegationToken(userId: Uint8Array, trustchainPrivateKey: Uint8Array): DelegationToken {
  const ephemeralKeys = tcrypto.makeSignKeyPair();

  const delegationBuffer = utils.concatArrays(ephemeralKeys.publicKey, userId);

  return {
    ephemeral_private_signature_key: ephemeralKeys.privateKey,
    ephemeral_public_signature_key: ephemeralKeys.publicKey,
    user_id: userId,
    delegation_signature: tcrypto.sign(delegationBuffer, trustchainPrivateKey),
    last_reset: new Uint8Array(32),
  };
}
github TankerHQ / sdk-js / packages / core / src / Unlock / unlock.js View on Github external
}: 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;
}
github TankerHQ / sdk-js / packages / core / src / Network / Authenticator.js View on Github external
export function takeChallenge(trustchainId: Uint8Array, userId: Uint8Array, signatureKeyPair: tcrypto.SodiumKeyPair, challenge: string): AuthDeviceParams {
  if (challenge.substr(0, CHALLENGE_PREFIX.length) !== CHALLENGE_PREFIX)
    throw new InternalError('Received auth challenge has the wrong prefix! The server may not be up to date, or we may be under attack.');
  const signature = tcrypto.sign(utils.fromString(challenge), signatureKeyPair.privateKey);
  return {
    signature,
    publicSignatureKey: signatureKeyPair.publicKey,
    trustchainId,
    userId,
  };
}
github TankerHQ / sdk-js / packages / core / src / Groups / Serialize.js View on Github external
tanker_provisional_user_public_signature_key: u.tankerSignaturePublicKey,
      encrypted_group_private_encryption_key: encryptedKey,
    };
  });

  const payload = {
    public_signature_key: signatureKeyPair.publicKey,
    public_encryption_key: encryptionKeyPair.publicKey,
    encrypted_group_private_signature_key: encryptedPrivateSignatureKey,
    encrypted_group_private_encryption_keys_for_users: keysForUsers,
    encrypted_group_private_encryption_keys_for_provisional_users: keysForProvisionalUsers,
    self_signature: new Uint8Array(0),
  };

  const signData = getUserGroupCreationBlockSignDataV2(payload);
  payload.self_signature = tcrypto.sign(signData, signatureKeyPair.privateKey);

  return { payload: serializeUserGroupCreationV2(payload), nature: preferredNature(NATURE_KIND.user_group_creation) };
};
github TankerHQ / sdk-js / packages / core / src / Blocks / BlockGenerator.js View on Github external
tanker_provisional_user_public_signature_key: u.tankerSignaturePublicKey,
        encrypted_group_private_encryption_key: encryptedKey,
      };
    });

    const payload = {
      public_signature_key: signatureKeyPair.publicKey,
      public_encryption_key: encryptionKeyPair.publicKey,
      encrypted_group_private_signature_key: encryptedPrivateSignatureKey,
      encrypted_group_private_encryption_keys_for_users: keysForUsers,
      encrypted_group_private_encryption_keys_for_provisional_users: keysForProvisionalUsers,
      self_signature: new Uint8Array(0),
    };

    const signData = getUserGroupCreationBlockSignDataV2(payload);
    payload.self_signature = tcrypto.sign(signData, signatureKeyPair.privateKey);

    return createBlock(
      serializeUserGroupCreationV2(payload),
      preferredNature(NATURE_KIND.user_group_creation),
      this.trustchainId,
      this.deviceId,
      this.privateSignatureKey
    );
  }
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 / Blocks / BlockGenerator.js View on Github external
makeNewDeviceBlock(args: NewDeviceParams) {
    const ephemeralKeys = tcrypto.makeSignKeyPair();
    const delegationBuffer = utils.concatArrays(ephemeralKeys.publicKey, args.userId);

    return this._makeDeviceBlock({ ...args,
      author: this.deviceId,
      ephemeralKey: ephemeralKeys.publicKey,
      delegationSignature: tcrypto.sign(delegationBuffer, this.privateSignatureKey),
      blockSignatureKey: ephemeralKeys.privateKey,
    });
  }
github TankerHQ / sdk-js / packages / core / src / Blocks / BlockGenerator.js View on Github external
makeProvisionalIdentityClaimBlock(userId: Uint8Array, userPublicKey: Uint8Array, provisionalUserKeys: ProvisionalUserKeys) {
    const multiSignedPayload = utils.concatArrays(
      this.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 createBlock(
github TankerHQ / sdk-js / packages / core / src / LocalUser / UserCreation.js View on Github external
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,
    },
    is_ghost_device: false,
    revoked: Number.MAX_SAFE_INTEGER,
  });

  return createBlock(
    payload,
    preferredNature(NATURE_KIND.device_creation),
    trustchainId,
    ghostDeviceId,