async EncryptionConk({libraryId, objectId, writeToken}) {
    const owner = await this.authClient.Owner({id: objectId, abi: ContentContract.abi});

    if(!this.utils.EqualAddress(owner, this.signer.address)) {
      // Target decryption
      if(!this.reencryptionConks[objectId]) {
        this.reencryptionConks[objectId] = await this.authClient.ReEncryptionConk({libraryId, objectId});
      }

      return this.reencryptionConks[objectId];
    }

    // Primary encryption
    if(!this.encryptionConks[objectId]) {
      const capKey = `eluv.caps.iusr${this.utils.AddressToHash(this.signer.address)}`;

      const existingCap =
        await this.ContentObjectMetadata({
          libraryId,
          // Cap may only exist in draft
          objectId: writeToken || objectId,
          metadataSubtree: capKey
        });

      if(existingCap) {
        this.encryptionConks[objectId] = await Crypto.DecryptCap(existingCap, this.signer.signingKey.privateKey);
      } else {
        this.encryptionConks[objectId] = await Crypto.GeneratePrimaryConk();

        // If write token is specified, add it to the metadata
        if(writeToken) {
          const kmsAddress = await this.authClient.KMSAddress({objectId});
          const kmsPublicKey = (await this.authClient.KMSInfo({objectId})).publicKey;
          const kmsCapKey = `eluv.caps.ikms${this.utils.AddressToHash(kmsAddress)}`;

          let metadata = {};

          metadata[capKey] = await Crypto.EncryptConk(this.encryptionConks[objectId], this.signer.signingKey.publicKey);

          try {
            metadata[kmsCapKey] = await Crypto.EncryptConk(this.encryptionConks[objectId], kmsPublicKey);
          } catch(error) {
            // eslint-disable-next-line no-console
            console.error("Failed to create encryption cap for KMS with public key " + kmsPublicKey);
          }

          await this.MergeMetadata({
            libraryId,
            objectId,
            writeToken,
            metadata
          });
        }
      }
    }

    return this.encryptionConks[objectId];
  }
/**
   * Retrieve the encryption conk for the specified object. If one has not yet been created
   * and a writeToken has been specified, this method will create a new conk and
   * save it to the draft metadata
   *
   * @methodGroup Encryption
   *
   * @namedParams
   * @param {string} libraryId - ID of the library
   * @param {string} objectId - ID of the object
   * @param {string} writeToken - Write token of the content object draft
   *
   * @return Promise<Object> - The encryption conk for the object
   */
Retrieve the encryption conk for the specified object. If one has not yet been created
and a writeToken has been specified, this method will create a new conk and
save it to the draft metadata

@methodGroup Encryption

@namedParams
@param {string} libraryId - ID of the library
@param {string} objectId - ID of the object
@param {string} writeToken - Write token of the content object draft

@return Promise - The encryption conk for the object
Retrieve the encryption conk for the specified object . If one has not yet been created and a writeToken has been specified this method will create a new conk and save it to the draft metadata @methodGroup Encryption @namedParams @param { string } libraryId - ID of the library @param { string } objectId - ID of the object @param { string } writeToken - Write token of the content object draft @return Promise - The encryption conk for the object
async EncryptionConk({libraryId, objectId, writeToken}) {
    const owner = await this.authClient.Owner({id: objectId, abi: ContentContract.abi});
    if(!this.utils.EqualAddress(owner, this.signer.address)) {
      if(!this.reencryptionConks[objectId]) {
        this.reencryptionConks[objectId] = await this.authClient.ReEncryptionConk({libraryId, objectId});
      }
      return this.reencryptionConks[objectId];
    }
    if(!this.encryptionConks[objectId]) {
      const capKey = `eluv.caps.iusr${this.utils.AddressToHash(this.signer.address)}`;
      const existingCap =
        await this.ContentObjectMetadata({
          libraryId,
          objectId: writeToken || objectId,
          metadataSubtree: capKey
        });
      if(existingCap) {
        this.encryptionConks[objectId] = await Crypto.DecryptCap(existingCap, this.signer.signingKey.privateKey);
      } else {
        this.encryptionConks[objectId] = await Crypto.GeneratePrimaryConk();
        if(writeToken) {
          const kmsAddress = await this.authClient.KMSAddress({objectId});
          const kmsPublicKey = (await this.authClient.KMSInfo({objectId})).publicKey;
          const kmsCapKey = `eluv.caps.ikms${this.utils.AddressToHash(kmsAddress)}`;
          let metadata = {};
          metadata[capKey] = await Crypto.EncryptConk(this.encryptionConks[objectId], this.signer.signingKey.publicKey);
          try {
            metadata[kmsCapKey] = await Crypto.EncryptConk(this.encryptionConks[objectId], kmsPublicKey);
          } catch(error) {
            console.error("Failed to create encryption cap for KMS with public key " + kmsPublicKey);
          }
          await this.MergeMetadata({
            libraryId,
            objectId,
            writeToken,
            metadata
          });
        }
      }
    }
    return this.encryptionConks[objectId];
  }
async EncryptionConk ( { libraryId , objectId , writeToken } ) { const owner = await this . authClient . Owner ( { id : objectId , abi : ContentContract . abi } ) ; if ( ! this . utils . EqualAddress ( owner , this . signer . address ) ) { if ( ! this . reencryptionConks [ objectId ] ) { this . reencryptionConks [ objectId ] = await this . authClient . ReEncryptionConk ( { libraryId , objectId } ) ; } return this . reencryptionConks [ objectId ] ; } if ( ! this . encryptionConks [ objectId ] ) { const capKey = ` ${ this . utils . AddressToHash ( this . signer . address ) } ` ; const existingCap = await this . ContentObjectMetadata ( { libraryId , objectId : writeToken || objectId , metadataSubtree : capKey } ) ; if ( existingCap ) { this . encryptionConks [ objectId ] = await Crypto . DecryptCap ( existingCap , this . signer . signingKey . privateKey ) ; } else { this . encryptionConks [ objectId ] = await Crypto . GeneratePrimaryConk ( ) ; if ( writeToken ) { const kmsAddress = await this . authClient . KMSAddress ( { objectId } ) ; const kmsPublicKey = ( await this . authClient . KMSInfo ( { objectId } ) ) . publicKey ; const kmsCapKey = ` ${ this . utils . AddressToHash ( kmsAddress ) } ` ; let metadata = { } ; metadata [ capKey ] = await Crypto . EncryptConk ( this . encryptionConks [ objectId ] , this . signer . signingKey . publicKey ) ; try { metadata [ kmsCapKey ] = await Crypto . EncryptConk ( this . encryptionConks [ objectId ] , kmsPublicKey ) ; } catch ( error ) { console . error ( "Failed to create encryption cap for KMS with public key " + kmsPublicKey ) ; } await this . MergeMetadata ( { libraryId , objectId , writeToken , metadata } ) ; } } } return this . encryptionConks [ objectId ] ; }
Retrieve the encryption conk for the specified object.
Retrieve the encryption conk for the specified object .
// Target decryption // Primary encryption // Cap may only exist in draft // If write token is specified, add it to the metadata // eslint-disable-next-line no-console