github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/kat/src/clients/gateway-providers/ethereum.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, IAPIGatewaySyncResponseEthereum } from '../../lib/interfaces';
    18  import * as utils from '../../lib/utils';
    19  const log = utils.getLogger('gateway-providers/ethereum.ts');
    20  
    21  // Member APIs
    22  
    23  export const upsertMember = async (address: string, name: string, app2appDestination: string,
    24    docExchangeDestination: string, sync: boolean): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponseEthereum> => {
    25    try {
    26      const response = await axios({
    27        method: 'post',
    28        url: `${config.apiGateway.apiEndpoint}/registerMember?kld-from=${address}&kld-sync=${sync}`,
    29        auth: {
    30          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
    31          password: config.apiGateway.auth?.password ?? config.appCredentials.password
    32        },
    33        data: {
    34          name,
    35          assetTrailInstanceID: config.assetTrailInstanceID,
    36          app2appDestination,
    37          docExchangeDestination
    38        }
    39      });
    40      return { ...response.data, type: sync ? 'sync' : 'async' };
    41    } catch (err) {
    42      return handleError(`Failed to register new member ${name}`, err);
    43    }
    44  };
    45  
    46  
    47  // Asset definition APIs
    48  
    49  export const createAssetDefinition = async (author: string, assetDefinitionHash: string, sync: boolean):
    50    Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponseEthereum> => {
    51    try {
    52      const response = await axios({
    53        method: 'post',
    54        url: `${config.apiGateway.apiEndpoint}/createAssetDefinition?kld-from=${author}&kld-sync=${sync}`,
    55        auth: {
    56          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
    57          password: config.apiGateway.auth?.password ?? config.appCredentials.password
    58        },
    59        data: {
    60          assetDefinitionHash
    61        }
    62      });
    63      return { ...response.data, type: sync ? 'sync' : 'async' };
    64    } catch (err) {
    65      return handleError(`Failed to create asset definition ${assetDefinitionHash}`, err);
    66    }
    67  };
    68  
    69  
    70  // Payment definition APIs
    71  
    72  export const createDescribedPaymentDefinition = async (paymentDefinitionID: string, name: string, author: string,
    73    descriptionSchemaHash: string, sync: boolean): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponseEthereum> => {
    74    try {
    75      const response = await axios({
    76        method: 'post',
    77        url: `${config.apiGateway.apiEndpoint}/createDescribedPaymentDefinition?kld-from=${author}&kld-sync=${sync}`,
    78        auth: {
    79          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
    80          password: config.apiGateway.auth?.password ?? config.appCredentials.password
    81        },
    82        data: {
    83          paymentDefinitionID: utils.uuidToHex(paymentDefinitionID),
    84          name,
    85          descriptionSchemaHash
    86        }
    87      });
    88      return { ...response.data, type: sync ? 'sync' : 'async' };
    89    } catch (err) {
    90      return handleError(`Failed to create described payment definition ${paymentDefinitionID}`, err);
    91    }
    92  };
    93  
    94  export const createPaymentDefinition = async (paymentDefinitionID: string, name: string, author: string, sync: boolean):
    95    Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponseEthereum> => {
    96    try {
    97      const response = await axios({
    98        method: 'post',
    99        url: `${config.apiGateway.apiEndpoint}/createPaymentDefinition?kld-from=${author}&kld-sync=${sync}`,
   100        auth: {
   101          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
   102          password: config.apiGateway.auth?.password ?? config.appCredentials.password
   103        },
   104        data: {
   105          paymentDefinitionID: utils.uuidToHex(paymentDefinitionID),
   106          name
   107        }
   108      });
   109      return { ...response.data, type: sync ? 'sync' : 'async' };
   110    } catch (err) {
   111      return handleError(`Failed to create payment definition ${paymentDefinitionID}`, err);
   112    }
   113  };
   114  
   115  
   116  // Asset instance APIs
   117  
   118  export const createDescribedAssetInstance = async (assetInstanceID: string, assetDefinitionID: string, author: string,
   119    descriptionHash: string, contentHash: string, sync = false): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponseEthereum> => {
   120    try {
   121      const response = await axios({
   122        method: 'post',
   123        url: `${config.apiGateway.apiEndpoint}/createDescribedAssetInstance?kld-from=${author}&kld-sync=${sync}`,
   124        auth: {
   125          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
   126          password: config.apiGateway.auth?.password ?? config.appCredentials.password
   127        },
   128        data: {
   129          assetInstanceID: utils.uuidToHex(assetInstanceID),
   130          assetDefinitionID: utils.uuidToHex(assetDefinitionID),
   131          descriptionHash,
   132          contentHash
   133        }
   134      });
   135      return { ...response.data, type: sync ? 'sync' : 'async' };
   136    } catch (err) {
   137      return handleError(`Failed to create described asset instance ${assetInstanceID}`, err);
   138    }
   139  };
   140  
   141  export const createAssetInstance = async (assetInstanceID: string, assetDefinitionID: string, author: string,
   142    contentHash: string, sync = false): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponseEthereum> => {
   143    try {
   144      const response = await axios({
   145        method: 'post',
   146        url: `${config.apiGateway.apiEndpoint}/createAssetInstance?kld-from=${author}&kld-sync=${sync}`,
   147        auth: {
   148          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
   149          password: config.apiGateway.auth?.password ?? config.appCredentials.password
   150        },
   151        data: {
   152          assetInstanceID: utils.uuidToHex(assetInstanceID),
   153          assetDefinitionID: utils.uuidToHex(assetDefinitionID),
   154          contentHash
   155        }
   156      });
   157      return { ...response.data, type: sync ? 'sync' : 'async' };
   158    } catch (err) {
   159      return handleError(`Failed to create asset instance ${assetInstanceID}`, err);
   160    }
   161  };
   162  
   163  export const createAssetInstanceBatch = async (batchHash: string, author: string, sync = false): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponseEthereum> => {
   164    try {
   165      const response = await axios({
   166        method: 'post',
   167        url: `${config.apiGateway.apiEndpoint}/createAssetInstanceBatch?kld-from=${author}&kld-sync=${sync}`,
   168        auth: {
   169          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
   170          password: config.apiGateway.auth?.password ?? config.appCredentials.password
   171        },
   172        data: {
   173          batchHash,
   174        }
   175      });
   176      return { ...response.data, type: sync ? 'sync' : 'async' };
   177    } catch (err) {
   178      return handleError(`Failed to create asset instance batch ${batchHash}`, err);
   179    }
   180  };
   181  
   182  export const setAssetInstanceProperty = async (assetDefinitionID: string, assetInstanceID: string, author: string, key: string, value: string,
   183    sync: boolean): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponseEthereum> => {
   184    try {
   185      const response = await axios({
   186        method: 'post',
   187        url: `${config.apiGateway.apiEndpoint}/setAssetInstanceProperty?kld-from=${author}&kld-sync=${sync}`,
   188        auth: {
   189          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
   190          password: config.apiGateway.auth?.password ?? config.appCredentials.password
   191        },
   192        data: {
   193          assetDefinitionID: utils.uuidToHex(assetDefinitionID),
   194          assetInstanceID: utils.uuidToHex(assetInstanceID),
   195          key,
   196          value
   197        }
   198      });
   199      return { ...response.data, type: sync ? 'sync' : 'async' };
   200    } catch (err) {
   201      return handleError(`Failed to set asset instance property ${key} (instance=${assetInstanceID})`, err);
   202    }
   203  };
   204  
   205  
   206  // Payment instance APIs
   207  
   208  export const createDescribedPaymentInstance = async (paymentInstanceID: string, paymentDefinitionID: string,
   209    author: string, member: string, amount: number, descriptionHash: string, sync: boolean):
   210    Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponseEthereum> => {
   211    try {
   212      const response = await axios({
   213        method: 'post',
   214        url: `${config.apiGateway.apiEndpoint}/createDescribedPaymentInstance?kld-from=${author}&kld-sync=${sync}`,
   215        auth: {
   216          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
   217          password: config.apiGateway.auth?.password ?? config.appCredentials.password
   218        },
   219        data: {
   220          paymentInstanceID: utils.uuidToHex(paymentInstanceID),
   221          paymentDefinitionID: utils.uuidToHex(paymentDefinitionID),
   222          member,
   223          amount,
   224          descriptionHash
   225        }
   226      });
   227      return { ...response.data, type: sync ? 'sync' : 'async' };
   228    } catch (err) {
   229      return handleError(`Failed to create described payment instance ${paymentInstanceID}`, err);
   230    }
   231  };
   232  
   233  export const createPaymentInstance = async (paymentInstanceID: string, paymentDefinitionID: string, author: string,
   234    member: string, amount: number, sync: boolean): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponseEthereum> => {
   235    try {
   236      const response = await axios({
   237        method: 'post',
   238        url: `${config.apiGateway.apiEndpoint}/createPaymentInstance?kld-from=${author}&kld-sync=${sync}`,
   239        auth: {
   240          username: config.apiGateway.auth?.user ?? config.appCredentials.user,
   241          password: config.apiGateway.auth?.password ?? config.appCredentials.password
   242        },
   243        data: {
   244          paymentInstanceID: utils.uuidToHex(paymentInstanceID),
   245          paymentDefinitionID: utils.uuidToHex(paymentDefinitionID),
   246          member,
   247          amount
   248        }
   249      });
   250      return { ...response.data, type: sync ? 'sync' : 'async' };
   251    } catch (err) {
   252      return handleError(`Failed to create payment instance ${paymentInstanceID}`, err);
   253    }
   254  };
   255  
   256  function handleError(msg: string, err: any): Promise<IAPIGatewayAsyncResponse | IAPIGatewaySyncResponseEthereum> {
   257    const errMsg = err.response?.data?.error ?? err.response.data.message ?? err.toString();
   258    log.error(`${msg}. ${errMsg}`);
   259    throw new Error(msg);
   260  }