github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/handlers/asset-instances-pinning.ts (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import * as apiGateway from '../clients/api-gateway';
    16  import * as ipfs from '../clients/ipfs';
    17  import { BatchManager } from '../lib/batch-manager';
    18  import { IAPIGatewayAsyncResponse, IAPIGatewaySyncResponse, IAssetInstance, IAssetInstancePropertySet, IBatchRecord, IDBBatch, IPinnedBatch, BatchRecordType } from '../lib/interfaces';
    19  import * as utils from '../lib/utils';
    20  
    21  const log = utils.getLogger('lib/asset-instance-pinning.ts');
    22  
    23  export class AssetInstancesPinning {
    24  
    25    private batchManager = new BatchManager('asset-instances', this.processBatch.bind(this));
    26  
    27    public async init() {
    28      await this.batchManager.init();
    29    }
    30  
    31    public async pin(instance: IAssetInstance): Promise<string> {
    32      const pinnedInstance: IBatchRecord = { recordType: BatchRecordType.assetInstance, ...instance };
    33      if (instance.isContentPrivate) delete pinnedInstance.content;
    34      const batchID = await this.batchManager.getProcessor(instance.author).add(pinnedInstance);
    35      log.trace(`Pinning initiated for asset ${instance.assetInstanceID}/${instance.assetInstanceID} in batch ${batchID}`);
    36      return batchID;
    37    }
    38  
    39    public async pinProperty(property: IAssetInstancePropertySet): Promise<string> {
    40      const pinnedProperty: IBatchRecord = { recordType: BatchRecordType.assetProperty, ...property };
    41      const batchID = await this.batchManager.getProcessor(property.author).add(pinnedProperty);
    42      log.trace(`Pinning initiated for property ${property.assetInstanceID}/${property.assetInstanceID}/${property.key} in batch ${batchID}`);
    43      return batchID;
    44    }
    45  
    46    private async processBatch(batch: IDBBatch) {
    47      // Extract the hashable portion, and write it to IPFS, and store the hash
    48      const pinnedBatch: IPinnedBatch = {
    49        type: batch.type,
    50        created: batch.created,
    51        author: batch.author,
    52        completed: batch.completed,
    53        batchID: batch.batchID,
    54        records: batch.records,
    55      };
    56      batch.batchHash = utils.ipfsHashToSha256(await ipfs.uploadString(JSON.stringify(pinnedBatch)));;
    57  
    58      let apiGatewayResponse: IAPIGatewayAsyncResponse | IAPIGatewaySyncResponse;
    59      apiGatewayResponse = await apiGateway.createAssetInstanceBatch(batch.batchHash, batch.author, batch.participants);
    60      batch.receipt = apiGatewayResponse.type === 'async' ? apiGatewayResponse.id : undefined;
    61    
    62      // The batch processor who called us does the store back to the local MongoDB, as part of completing the batch
    63    }
    64  
    65  }
    66  
    67  /**
    68   * Singleton instance
    69   */
    70  export const assetInstancesPinning = new AssetInstancesPinning();