How to use the @tanker/crypto.utils.toBase64 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 / identity / src / identity.js View on Github external
function _generateAppId(appSecret: Uint8Array): b64string { // eslint-disable-line no-underscore-dangle
  const publicKey = appSecret.subarray(tcrypto.SIGNATURE_PRIVATE_KEY_SIZE - tcrypto.SIGNATURE_PUBLIC_KEY_SIZE, tcrypto.SIGNATURE_PRIVATE_KEY_SIZE);
  return utils.toBase64(utils.generateAppID(publicKey));
}
github TankerHQ / sdk-js / packages / core / src / DataProtection / DataProtector.js View on Github external
const encryption = extractEncryptionFormat(castEncryptedData);
    const encryptedSize = getDataLength(castEncryptedData);
    // $FlowIKnow Already checked we are using a simple encryption
    const clearSize = encryption.getClearSize(encryptedSize);
    const progressHandler = new ProgressHandler(progressOptions).start(clearSize);

    const resourceId = encryption.extractResourceId(castEncryptedData);
    const key = await this._resourceManager.findKeyFromResourceId(resourceId);

    let clearData;

    try {
    // $FlowIKnow Already checked we are using a simple encryption
      clearData = encryption.decrypt(key, encryption.unserialize(castEncryptedData));
    } catch (error) {
      const b64ResourceId = utils.toBase64(resourceId);
      throw new DecryptionFailed({ error, b64ResourceId });
    }

    const castClearData = await castData(clearData, outputOptions);

    progressHandler.report(clearSize);

    return castClearData;
  }
github TankerHQ / sdk-js / packages / core / src / Trustchain / UnverifiedStore / UserGroupsUnverifiedStore.js View on Github external
async addUnverifiedUserGroupEntries(entries: Array): Promise {
    if (entries.length === 0)
      return;
    const mapEntry = new Map();
    const mapEncKeys = new Map();
    for (const entry of entries) {
      if (natureKind(entry.nature) === NATURE_KIND.user_group_creation) {
        const groupCreation: UnverifiedUserGroupCreation = (entry: any);
        const b64GroupId = utils.toBase64(entry.group_id);

        const dbEntry = entryToDbEntry(entry, b64GroupId);
        delete dbEntry.group_public_encryption_key;
        mapEntry.set(dbEntry._id, dbEntry); // eslint-disable-line no-underscore-dangle

        const dbGroupKey = {
          _id: utils.toBase64(groupCreation.public_encryption_key),
          group_id: b64GroupId,
        };
        mapEncKeys.set(dbGroupKey._id, dbGroupKey); // eslint-disable-line no-underscore-dangle
      } else if (natureKind(entry.nature) === NATURE_KIND.user_group_addition) {
        const groupAddition: UnverifiedUserGroupAddition = (entry: any);
        const dbEntry = entryToDbEntry(entry, utils.toBase64(groupAddition.previous_group_block));
        mapEntry.set(dbEntry._id, dbEntry); // eslint-disable-line no-underscore-dangle
      } else {
        throw new InternalError('Assertion failure: entry is not a group entry');
github TankerHQ / sdk-js / packages / core / src / __tests__ / entries.spec.js View on Github external
private_keys: [userPrivKey, userPrivKey]
    };

    notSoSimpleEntry = { ...simpleEntry };

    notSoSimpleEntry.payload_unverified = {
      device_id: hash,
      user_keys: userKeys
    };

    transformedNotSoSimpleEntry = {
      _id: 42,
      index: 0,
      nature: 8,
      hash: utils.toBase64(hash),
      author: utils.toBase64(author),
      signature: utils.toBase64(signature),
      device_id: utils.toBase64(hash),
      user_keys: {
        public_encryption_key: utils.toBase64(key),
        previous_public_encryption_key: utils.toBase64(key),
        encrypted_previous_encryption_key: utils.toBase64(key),
        private_keys: [tansformedUserPrivKey, tansformedUserPrivKey]
      }
    };

    restoredNotSoSimpleEntry = {
      index: 0,
      nature: NATURE.key_publish_to_user,
      hash,
      author,
      signature,
github TankerHQ / sdk-js / packages / core / src / DataProtection / Resource / ResourceStore.js View on Github external
async findResourceKey(resourceId: Uint8Array): Promise {
    try {
      const b64ResourceId = utils.toBase64(resourceId);
      const result = await this._ds.get(TABLE, b64ResourceId);
      const encryptedKey = utils.fromBase64(result.b64EncryptedKey);

      if (encryptedKey.length < encryptionV1.overhead) {
        throw new DecryptionFailed({ message: `truncated encrypted data. Length should be at least ${encryptionV1.overhead} for encryption v1` });
      }
      return encryptionV1.compatDecrypt(this._userSecret, encryptedKey, resourceId);
    } catch (e) {
      if (e instanceof dbErrors.RecordNotFound) {
        return;
      }
      throw e;
    }
  }
github TankerHQ / sdk-js / packages / core / src / Trustchain / UnverifiedStore / UserUnverifiedStore.js View on Github external
async getUserIdFromDeviceId(deviceId: Uint8Array): Promise {
    const deviceToUser = await this._ds.first(UserUnverifiedStore.tables[TABLE_DEVICE_TO_USER].name, {
      selector: {
        device_id: { $eq: utils.toBase64(deviceId) },
      }
    });
    if (deviceToUser) {
      return utils.fromBase64(deviceToUser.user_id);
    }
    return null;
  }
}
github TankerHQ / sdk-js / packages / core / src / Network / Client.js View on Github external
Object.entries(requestObject).forEach(([key, value]) => {
    if (value instanceof Uint8Array) {
      result[key] = utils.toBase64(value);
    } else if (Array.isArray(value)) {
      result[key] = b64RequestObject(value);
    } else if (isObject(value)) {
      result[key] = b64RequestObject(value);
    } else {
      result[key] = value;
    }
  });
github TankerHQ / sdk-js / packages / core / src / Session / LocalUser / ghostDevice.js View on Github external
export const ghostDeviceToUnlockKey = (ghostDevice: GhostDevice) => utils.toB64Json({
  privateEncryptionKey: utils.toBase64(ghostDevice.privateEncryptionKey),
  privateSignatureKey: utils.toBase64(ghostDevice.privateSignatureKey),
});
github TankerHQ / sdk-js / packages / core / src / Users / Manager.js View on Github external
_getDeviceKeysFromIds(userIdToUserMap: Map, deviceIdToUserIdMap: Map, devicesIds: Array): Map {
    const devicesPublicSignatureKeys: Map = new Map();
    for (const deviceId of devicesIds) {
      const userId = deviceIdToUserIdMap.get(utils.toBase64(deviceId));
      if (!userId) {
        throw new InternalError('no such author user id');
      }
      const user = userIdToUserMap.get(userId);
      if (!user) {
        throw new InternalError('no such author user');
      }
      const device = find(user.devices, userDevice => utils.equalArray(userDevice.deviceId, deviceId));
      devicesPublicSignatureKeys.set(utils.toBase64(deviceId), device.devicePublicSignatureKey);
    }
    return devicesPublicSignatureKeys;
  }
github TankerHQ / sdk-js / packages / core / src / Blocks / Block.js View on Github external
export function createBlock(payload: Uint8Array, nature: Nature, trustchainId: Uint8Array, author: Uint8Array, signatureKey: Uint8Array) {
  const block = {
    trustchain_id: trustchainId,
    nature,
    author,
    payload
  };
  const hash = hashBlock(block);
  const signature = tcrypto.sign(hash, signatureKey);

  return { block: utils.toBase64(serializeBlock({ ...block, signature })), hash };
}