github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/clients/api-gateway.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 { config } from '../lib/config';
    16  import { IAPIGatewayAsyncResponse, IAPIGatewaySyncResponse } from '../lib/interfaces';
    17  import * as ethereumGateway from './gateway-providers/ethereum';
    18  import * as cordaGateway from './gateway-providers/corda';
    19  
    20  // Member APIs
    21  
    22  export const upsertMember = async (address: string, name: string, app2appDestination: string,
    23    docExchangeDestination: string, sync: boolean): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponse> => {
    24    return ethereumGateway.upsertMember(address, name, app2appDestination, docExchangeDestination, sync);
    25  };
    26  
    27  
    28  // Asset definition APIs
    29  
    30  export const createAssetDefinition = async (author: string, assetDefinitionHash: string, sync: boolean):
    31    Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponse> => {
    32    return ethereumGateway.createAssetDefinition(author, assetDefinitionHash, sync);
    33  };
    34  
    35  
    36  // Payment definition APIs
    37  
    38  export const createDescribedPaymentDefinition = async (paymentDefinitionID: string, name: string, author: string,
    39    descriptionSchemaHash: string, sync: boolean): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponse> => {
    40    return ethereumGateway.createDescribedPaymentDefinition(paymentDefinitionID, name, author, descriptionSchemaHash, sync);
    41  };
    42  
    43  export const createPaymentDefinition = async (paymentDefinitionID: string, name: string, author: string, sync: boolean):
    44    Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponse> => {
    45    return ethereumGateway.createPaymentDefinition(paymentDefinitionID, name, author, sync);
    46  };
    47  
    48  
    49  // Asset instance APIs
    50  
    51  export const createDescribedAssetInstance = async (assetInstanceID: string, assetDefinitionID: string, author: string,
    52    descriptionHash: string, contentHash: string, participants: string[] | undefined, sync = false): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponse> => {
    53    switch (config.protocol) {
    54      case 'corda':
    55        return cordaGateway.createDescribedAssetInstance(assetInstanceID, assetDefinitionID, descriptionHash, contentHash, participants);
    56      case 'ethereum':
    57        return ethereumGateway.createDescribedAssetInstance(assetInstanceID, assetDefinitionID, author, descriptionHash, contentHash, sync);
    58    }
    59  };
    60  
    61  export const createAssetInstance = async (assetInstanceID: string, assetDefinitionID: string, author: string,
    62    contentHash: string, participants: string[] | undefined, sync = false): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponse> => {
    63    switch (config.protocol) {
    64      case 'corda':
    65        return cordaGateway.createAssetInstance(assetInstanceID, assetDefinitionID, contentHash, participants);
    66      case 'ethereum':
    67        return ethereumGateway.createAssetInstance(assetInstanceID, assetDefinitionID, author, contentHash, sync);
    68    }
    69  };
    70  
    71  export const createAssetInstanceBatch = async (batchHash: string, author: string, participants: string[] | undefined, sync = false): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponse> => {
    72    switch (config.protocol) {
    73      case 'corda':
    74        return cordaGateway.createAssetInstanceBatch(batchHash, participants);
    75      case 'ethereum':
    76        return ethereumGateway.createAssetInstanceBatch(batchHash, author, sync);
    77    }
    78  }
    79  
    80  export const setAssetInstanceProperty = async (assetDefinitionID: string, assetInstanceID: string, author: string, key: string, value: string,
    81    participants: string[] | undefined, sync: boolean): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponse> => {
    82    switch (config.protocol) {
    83      case 'corda':
    84        return cordaGateway.setAssetInstanceProperty(assetDefinitionID, assetInstanceID, key, value, participants);
    85      case 'ethereum':
    86        return ethereumGateway.setAssetInstanceProperty(assetDefinitionID, assetInstanceID, author, key, value, sync);
    87    }
    88  };
    89  
    90  
    91  // Payment instance APIs
    92  
    93  export const createDescribedPaymentInstance = async (paymentInstanceID: string, paymentDefinitionID: string,
    94    author: string, member: string, amount: number, descriptionHash: string, participants: string[] | undefined, sync: boolean):
    95    Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponse> => {
    96    switch (config.protocol) {
    97      case 'corda':
    98        return cordaGateway.createDescribedPaymentInstance(paymentInstanceID, paymentDefinitionID, member, amount, descriptionHash, participants);
    99      case 'ethereum':
   100        return ethereumGateway.createDescribedPaymentInstance(paymentInstanceID, paymentDefinitionID, author, member, amount, descriptionHash, sync);
   101    }
   102  };
   103  
   104  export const createPaymentInstance = async (paymentInstanceID: string, paymentDefinitionID: string,
   105    author: string, member: string, amount: number, participants: string[] | undefined, sync: boolean):
   106    Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponse> => {
   107    switch (config.protocol) {
   108      case 'corda':
   109        return cordaGateway.createPaymentInstance(paymentInstanceID, paymentDefinitionID, member, amount, participants);
   110      case 'ethereum':
   111        return ethereumGateway.createPaymentInstance(paymentInstanceID, paymentDefinitionID, author, member, amount, sync);
   112    }
   113  };