github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/clients/gateway-providers/corda.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 axios from 'axios';
    16  import { config } from '../../lib/config';
    17  import { IAPIGatewayAsyncResponse, IAPIGatewaySyncResponse } from '../../lib/interfaces';
    18  import { getLogger } from '../../lib/utils';
    19  const log = getLogger('gateway-providers/corda.ts');
    20  
    21  // Asset instance APIs
    22  
    23  export const createDescribedAssetInstance = async (assetInstanceID: string, assetDefinitionID: string,
    24    descriptionHash: string, contentHash: string, participants: string[] | undefined): Promise<IAPIGatewaySyncResponse> => {
    25    try {
    26      const response = await axios({
    27        method: 'post',
    28        url: `${config.apiGateway.apiEndpoint}/createDescribedAssetInstance`,
    29        auth: {
    30          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
    31          password: config.apiGateway.auth?.password ?? config.appCredentials.password
    32        },
    33        data: {
    34          assetInstanceID: assetInstanceID,
    35          assetDefinitionID: assetDefinitionID,
    36          descriptionHash,
    37          contentHash,
    38          participants
    39        }
    40      });
    41      return { ...response.data, type: 'sync' };
    42    } catch (err) {
    43      return handleError(`Failed to create described asset instance ${assetInstanceID}`, err);
    44    }
    45  };
    46  
    47  export const createAssetInstance = async (assetInstanceID: string, assetDefinitionID: string,
    48    contentHash: string, participants: string[] | undefined): Promise<IAPIGatewaySyncResponse> => {
    49    try {
    50      const response = await axios({
    51        method: 'post',
    52        url: `${config.apiGateway.apiEndpoint}/createAssetInstance`,
    53        auth: {
    54          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
    55          password: config.apiGateway.auth?.password ?? config.appCredentials.password
    56        },
    57        data: {
    58          assetInstanceID: assetInstanceID,
    59          assetDefinitionID: assetDefinitionID,
    60          contentHash,
    61          participants
    62        }
    63      });
    64      return { ...response.data, type: 'sync' };
    65    } catch (err) {
    66      return handleError(`Failed to create asset instance ${assetInstanceID}`, err);
    67    }
    68  };
    69  
    70  export const createAssetInstanceBatch = async (batchHash: string, participants: string[] | undefined): Promise<IAPIGatewaySyncResponse> => {
    71    try {
    72      const response = await axios({
    73        method: 'post',
    74        url: `${config.apiGateway.apiEndpoint}/createAssetInstanceBatch`,
    75        auth: {
    76          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
    77          password: config.apiGateway.auth?.password ?? config.appCredentials.password
    78        },
    79        data: {
    80          batchHash,
    81          participants
    82        }
    83      });
    84      return { ...response.data, type: 'sync' };
    85    } catch (err) {
    86      return handleError(`Failed to create asset instance batch ${batchHash}`, err);
    87    }
    88  };
    89  
    90  export const setAssetInstanceProperty = async (assetDefinitionID: string, assetInstanceID: string, key: string, value: string,
    91    participants: string[] | undefined | undefined): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponse> => {
    92    try {
    93      const response = await axios({
    94        method: 'post',
    95        url: `${config.apiGateway.apiEndpoint}/setAssetInstanceProperty`,
    96        auth: {
    97          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
    98          password: config.apiGateway.auth?.password ?? config.appCredentials.password
    99        },
   100        data: {
   101          assetDefinitionID: assetDefinitionID,
   102          assetInstanceID: assetInstanceID,
   103          key,
   104          value,
   105          participants
   106        }
   107      });
   108      return { ...response.data, type: 'sync' };
   109    } catch (err) {
   110      return handleError(`Failed to set asset instance property ${key} (instance=${assetInstanceID})`, err);
   111    }
   112  };
   113  
   114  export const createDescribedPaymentInstance = async (paymentInstanceID: string, paymentDefinitionID: string, member: string, amount: number, descriptionHash: string, participants: string[] | undefined):
   115    Promise<IAPIGatewaySyncResponse> => {
   116    try {
   117      const response = await axios({
   118        method: 'post',
   119        url: `${config.apiGateway.apiEndpoint}/createDescribedPaymentInstance`,
   120        auth: {
   121          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
   122          password: config.apiGateway.auth?.password ?? config.appCredentials.password
   123        },
   124        data: {
   125          paymentInstanceID: paymentInstanceID,
   126          paymentDefinitionID: paymentDefinitionID,
   127          member,
   128          amount,
   129          descriptionHash,
   130          participants
   131        }
   132      });
   133      return { ...response.data, type: 'sync' };
   134    } catch (err) {
   135      return handleError(`Failed to create described asset payment instance ${paymentInstanceID}`, err);
   136    }
   137  };
   138  
   139  export const createPaymentInstance = async (paymentInstanceID: string, paymentDefinitionID: string,
   140    member: string, amount: number, participants: string[] | undefined): Promise<IAPIGatewaySyncResponse> => {
   141    try {
   142      const response = await axios({
   143        method: 'post',
   144        url: `${config.apiGateway.apiEndpoint}/createPaymentInstance`,
   145        auth: {
   146          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
   147          password: config.apiGateway.auth?.password ?? config.appCredentials.password
   148        },
   149        data: {
   150          paymentInstanceID: paymentInstanceID,
   151          paymentDefinitionID: paymentDefinitionID,
   152          member,
   153          amount,
   154          participants
   155        }
   156      });
   157      return { ...response.data, type: 'sync' };
   158    } catch (err) {
   159      return handleError(`Failed to create asset payment instance ${paymentInstanceID}`, err);
   160    }
   161  };
   162  
   163  function handleError(msg: string, err: any): Promise<IAPIGatewaySyncResponse> {
   164    const errMsg = err.response?.data?.error ?? err.response.data.message ?? err.toString();
   165    log.error(`${msg}. ${errMsg}`);
   166    throw new Error(msg);
   167  }