How to use the @tanker/crypto.utils.fromBase64 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 / __tests__ / identity.spec.js View on Github external
function checkDelegationSignature(identity, trustchainPublicKey) {
  const signedData = utils.concatArrays(
    utils.fromBase64(identity.ephemeral_public_signature_key),
    utils.fromBase64(identity.value)
  );

  expect(tcrypto.verifySignature(
    signedData,
    utils.fromBase64(identity.delegation_signature),
    utils.fromBase64(trustchainPublicKey)
  )).to.equal(true);
}
github TankerHQ / sdk-js / packages / identity / src / identity.js View on Github external
export async function createIdentity(appId: b64string, appSecret: b64string, userId: string): Promise {
  if (!appId || typeof appId !== 'string')
    throw new InvalidArgument('appId', 'b64string', appId);
  if (!appSecret || typeof appSecret !== 'string')
    throw new InvalidArgument('appSecret', 'b64string', appSecret);
  if (!userId || typeof userId !== 'string')
    throw new InvalidArgument('userId', 'string', userId);
  const obfuscatedUserId = obfuscateUserId(utils.fromBase64(appId), userId);

  const appSecretBytes = utils.fromBase64(appSecret);
  const gerenatedAppId = _generateAppId(appSecretBytes);
  if (gerenatedAppId !== appId)
    throw new InvalidArgument('app secret and app ID mismatch');

  const ephemeralKeyPair = tcrypto.makeSignKeyPair();

  const toSign = utils.concatArrays(ephemeralKeyPair.publicKey, obfuscatedUserId);
  const delegationSignature = tcrypto.sign(toSign, appSecretBytes);

  const userSecret = createUserSecretB64(appId, userId);

  const permanentIdentity: SecretPermanentIdentity = {
    trustchain_id: appId,
    target: 'user',
github TankerHQ / sdk-js / packages / core / src / ProvisionalIdentity / ProvisionalIdentityManager.js View on Github external
return tankerPublicKeys.map((tpk, i) => ({
      trustchainId: utils.fromBase64(provisionalIdentities[i].trustchain_id),
      target: provisionalIdentities[i].target,
      value: provisionalIdentities[i].value,
      appEncryptionPublicKey: utils.fromBase64(provisionalIdentities[i].public_encryption_key),
      appSignaturePublicKey: utils.fromBase64(provisionalIdentities[i].public_signature_key),
      tankerEncryptionPublicKey: utils.fromBase64(tpk.encryption_public_key),
      tankerSignaturePublicKey: utils.fromBase64(tpk.signature_public_key),
    }));
  }
github TankerHQ / sdk-js / packages / core / src / DataProtection / DataProtector.js View on Github external
async makeEncryptorStream(sharingOptions: SharingOptions, b64ResourceId?: b64string): Promise {
    let encryptorStream;

    if (b64ResourceId) {
      const resourceId = utils.fromBase64(b64ResourceId);
      const key = await this._resourceManager.findKeyFromResourceId(resourceId);
      encryptorStream = new EncryptorStream(resourceId, key);
    } else {
      const resource = makeResource();
      await this._shareResources([resource], sharingOptions, true);
      encryptorStream = new EncryptorStream(resource.resourceId, resource.key);
    }

    return encryptorStream;
  }
github TankerHQ / sdk-js / packages / core / src / ProvisionalIdentity / ProvisionalIdentityManager.js View on Github external
async _claimProvisionalIdentity(provisionalIdentity: SecretProvisionalIdentity, tankerKeys: TankerProvisionalKeys): Promise {
    await this._localUserManager.updateLocalUser();

    const appProvisionalUserPrivateSignatureKey = utils.fromBase64(provisionalIdentity.private_signature_key);
    const appProvisionalUserPrivateEncryptionKey = utils.fromBase64(provisionalIdentity.private_encryption_key);

    const provisionalUserKeys = {
      ...tankerKeys,
      appEncryptionKeyPair: tcrypto.getEncryptionKeyPairFromPrivateKey(appProvisionalUserPrivateEncryptionKey),
      appSignatureKeyPair: tcrypto.getSignatureKeyPairFromPrivateKey(appProvisionalUserPrivateSignatureKey),
    };

    const { userId, deviceId, currentUserKey } = this._localUserManager.localUser;
    const { payload, nature } = makeProvisionalIdentityClaim(userId, deviceId, currentUserKey.publicKey, provisionalUserKeys);

    await this._client.send('push block', this._localUserManager.localUser.makeBlock(payload, nature), true);
  }
}
github TankerHQ / sdk-js / packages / core / src / Session / LocalUser / Serialize.js View on Github external
export function trustchainCreationFromBlock(b64Block: string): TrustchainCreationEntry {
  const block = unserializeBlock(utils.fromBase64(b64Block));
  const author = block.author;
  const signature = block.signature;
  const nature = block.nature;
  const hash = hashBlock(block);

  if (block.nature !== NATURE.trustchain_creation) {
    throw new InternalError(`Assertion error: invalid block nature ${block.nature} for trustchainCreationFromBlock`);
  }

  const trustchainCreationRecord = unserializeTrustchainCreation(block.payload);
  return { ...trustchainCreationRecord, author, signature, nature, hash };
}
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 / Users / UserStore.js View on Github external
function userFromRecord(record: any): User {
  return { ...record, userId: utils.fromBase64(record.userId) };
}
github TankerHQ / sdk-js / packages / core / src / ProvisionalIdentity / ProvisionalIdentityManager.js View on Github external
async _claimProvisionalIdentity(provisionalIdentity: SecretProvisionalIdentity, tankerKeys: TankerProvisionalKeys): Promise {
    await this._localUserManager.updateLocalUser();

    const appProvisionalUserPrivateSignatureKey = utils.fromBase64(provisionalIdentity.private_signature_key);
    const appProvisionalUserPrivateEncryptionKey = utils.fromBase64(provisionalIdentity.private_encryption_key);

    const provisionalUserKeys = {
      ...tankerKeys,
      appEncryptionKeyPair: tcrypto.getEncryptionKeyPairFromPrivateKey(appProvisionalUserPrivateEncryptionKey),
      appSignatureKeyPair: tcrypto.getSignatureKeyPairFromPrivateKey(appProvisionalUserPrivateSignatureKey),
    };

    const { userId, deviceId, currentUserKey } = this._localUserManager.localUser;
    const { payload, nature } = makeProvisionalIdentityClaim(userId, deviceId, currentUserKey.publicKey, provisionalUserKeys);

    await this._client.send('push block', this._localUserManager.localUser.makeBlock(payload, nature), true);
  }
}