2.0.0 • Published 2 years ago

@cef-ebsi/fabric-wallet-lib v2.0.0

Weekly downloads
-
License
EUPL-1.2
Repository
-
Last release
2 years ago

EBSI Logo

Fabric Wallet Library

Node.js library to interact with EBSI's Fabric environment.

Table of Contents

  1. Install
  2. Usage
  3. Linting
  4. Auditing the dependencies
  5. Tests
  6. Generate docs
  7. License

Install

npm install @cef-ebsi/fabric-wallet-lib

Or with yarn:

yarn add @cef-ebsi/fabric-wallet-lib

Usage

import {
  EbsiFabricWallet,
  ProposalResponseBase64,
} from "@cef-ebsi/fabric-wallet-lib";
import axios from "axios";
import crypto from "crypto";
import { v4 as uuidv4 } from "uuid";

const iossvatid = "ABCDEF568134";
const startDate = "2021-11-04";
const endDate = "2022-11-04";
const currentDate = "2021-11-04 12:00:00";
const uuid = uuidv4();
const salt = crypto.randomBytes(10).toString("hex");

async function main() {
  // Load wallet
  const wallet = new EbsiFabricWallet({
    channelName: "iossdrpocchannel",
    contractName: "iossdrpociossvatid",
  });
  await wallet.loadWallet("./wallet", "user1_be_tax");

  // Create a proposal
  const proposal = wallet.createProposal({
    fcn: "registerIossVatId",
    args: [startDate, endDate, currentDate, uuid, salt],
    generateTransactionId: false,
    transientMap: {
      iossvatid: Buffer.from(JSON.stringify({ iossvatid })),
    },
  });
  console.log(proposal);
  // {
  //   action: {
  //     init: false,
  //     transientMap: { iossvatid: 'eyJpb3Nz...' },
  //     transactionId: 'ced95fb...',
  //     args: [
  //       'cmVnaXN0ZXJJb3NzVmF0SWQ=',
  //       'MjAyMS0xMS0wNA==',
  //       'MjAyMi0xMS0wNA==',
  //       'MjAyMS0xMS0wNCAxMjowMDowMA==',
  //       'MjBlZWY1NzktY2E3My00OGQ1LWE0YTAtMjkxNmFiODAzMmZj',
  //       'MDExODNhMmY2NjlmNTc2ODE2YmY='
  //     ],
  //     fcn: 'registerIossVatId',
  //     header: {
  //       signature_header: 'CugIChFCRVRB...',
  //       channel_header: 'CAMQARoMCNyMkI...'
  //     },
  //     proposal: {
  //       header: 'Cn4IAxABGgwI3Iy...',
  //       payload: 'CpsBCpgBCAESFB...'
  //     }
  //   },
  //   payload: 'CogKCn4IAxABGg...',
  //   signature: 'MEUCIQCVt5FzM16CY...',
  //   transactionId: 'ced95fb...'
  // }

  // Send proposal
  const response1 = await axios.post(
    "https://api.test.intebsi.xyz/ledger/v2/blockchains/fabric/jsonrpc",
    {
      jsonrpc: "2.0",
      id: 1,
      method: "sendProposal",
      params: [
        {
          channelName: wallet.channelName,
          contractName: wallet.contractName,
          action: proposal.action,
          payload: proposal.payload,
          signature: proposal.signature,
        },
      ],
    }
  );
  const { result: resultProposal } = response1.data as {
    result: ProposalResponseBase64[];
  };
  console.log(resultProposal);
  // [
  //   {
  //     endorsement: {
  //       endorser: 'ChFCRVRBWElP...',
  //       signature: 'MEUCIQDpNqO07//I8WR...'
  //     },
  //     payload: 'CiAdhuVxH2oeXMYa...',
  //     response: { status: 200, message: '', payload: '' }
  //   },
  //   {
  //     endorsement: {
  //       endorser: 'ChFERUNTVElP...',
  //       signature: 'MEUCIQDAqttLmC4szoh...'
  //     },
  //     payload: 'CiAdhuVxH2oeXMYa...',
  //     response: { status: 200, message: '', payload: '' }
  //   },
  //   {
  //     endorsement: {
  //       endorser: 'ChFGUkNTVElP...',
  //       signature: 'MEUCIQDAbMmSa/3S1sM...'
  //     },
  //     payload: 'CiAdhuVxH2oeXMYa...',
  //     response: { status: 200, message: '', payload: '' }
  //   }
  // ]

  // Create a commit with the proposal responses
  const commit = wallet.createCommit(resultProposal);
  console.log(commit);
  // {
  //   action: {
  //     init: false,
  //     payload: {
  //       header: {
  //         signature_header: 'CugIChFCRVRB...',
  //         channel_header: 'CAMQARoMCNCQkIw...'
  //       },
  //       data: 'CoEsCoUJCug...'
  //     }
  //   },
  //   payload: 'CogKCn4IAxABG...',
  //   signature: 'MEQCICAfiHqaEfZ5...',
  //   transactionId: 'ced95fb3919459346...'
  // }

  // Commit transaction
  const response2 = await axios.post(
    "https://api.test.intebsi.xyz/ledger/v2/blockchains/fabric/jsonrpc",
    {
      jsonrpc: "2.0",
      id: 1,
      method: "commitTransaction",
      params: [
        {
          channelName: wallet.channelName,
          contractName: wallet.contractName,
          action: commit.action,
          payload: commit.payload,
          signature: commit.signature,
          transactionId: commit.transactionId,
        },
      ],
    }
  );
  const { result: resultCommit } = response2.data as { result: string };
  console.log(resultCommit);
  // OK

  // Verify the new iossvatid inserted
  const response3 = await axios.post(
    "https://api.test.intebsi.xyz/ledger/v2/blockchains/fabric/jsonrpc",
    {
      jsonrpc: "2.0",
      id: 1,
      method: "readContract",
      params: [
        {
          channelName: wallet.channelName,
          contractName: wallet.contractName,
          fcn: "getIossVatId",
          args: [iossvatid],
        },
      ],
    }
  );
  const { result: resultConsult } = response3.data as { result: string };
  const result = JSON.parse(Buffer.from(resultConsult, "base64").toString());
  console.log(result);
  // {
  //   keyws: 'org.iossdrpoc.iossvatidvalidity.20eef579-ca73-48d5-a4a0-2916ab8032fc',
  //   keypdc: 'org.iossdrpoc.iossvatid.20eef579-ca73-48d5-a4a0-2916ab8032fc',
  //   iossvatid: 'ABCDEF568134',
  //   iossvatidsalted: 'ABCDEF56813401183a2f669f576816bf',
  //   startdate: '2021-11-04',
  //   enddate: '2022-11-04',
  //   operation: 'C',
  //   modificationdatetime: '2021-11-04 12:00:00'
  // }
}

main();

Linting

You can lint the files (ESLint, tsc) and run Prettier with one command:

yarn lint

Or you can run the different linters independently:

ESLint

yarn lint:eslint

or with yarn:

yarn eslint . --ext .js,.ts

Run eslint and precommit rules:

.git/hooks/pre-commit

tsc

yarn lint:tsc

or with yarn:

yarn tsc --noEmit --incremental false

Prettier

yarn lint:prettier

or with yarn:

yarn prettier . --check

Auditing the dependencies

Using audit-ci (this is the one we run during CI):

yarn run audit

Or using Yarn's built-in audit command, to get more information:

yarn audit

Tests

Note: before running the e2e tests, make sure to create a local .env file and fill it with the correct environment variables. You can check .env.example to see what is expected.

Run all the tests:

yarn test

Run unit tests only:

yarn test:unit

# Or in CI mode (with coverage)
yarn test:ci

Run all the end-to-end tests:

yarn test:e2e

Generate docs

The documentation is generated with TypeDoc, based on the TSDoc comments we leave in the code.

Run the following command to generate the docs:

yarn generate:docs

Serve the docs:

yarn serve:docs

License

Copyright (c) 2019 European Commission Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work except in compliance with the Licence. You may obtain a copy of the Licence at:

Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Licence for the specific language governing permissions and limitations under the Licence.

1.0.0

2 years ago

2.0.0-alpha.0

2 years ago

2.0.0

2 years ago

1.0.0-rc.2

2 years ago

1.0.0-rc.1

2 years ago

1.0.0-rc.0

2 years ago