Hyperledger Fabric Shim API

2.5.8 · active · verified Wed Apr 22

The `fabric-shim-api` package provides the Node.js API for Hyperledger Fabric chaincode shim, facilitating communication between endorsing peers and user-provided chaincodes. Its primary role is to offer TypeScript type definitions for the `fabric-shim` module, and it serves as a dependency for `fabric-contract-api`, enabling the use of annotations in client applications without pulling in unnecessary runtime dependencies. The current stable version is 2.5.8, part of the v2.5 LTS series. Releases typically follow the Hyperledger Fabric LTS cadence, with point releases addressing fixes and dependency updates. Key differentiators include its lightweight nature as a type-only package, its integral role in the Fabric Node.js chaincode ecosystem, and its support for modern Node.js runtimes (currently Node 22 for the latest versions).

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic Hyperledger Fabric chaincode in TypeScript, implementing `ChaincodeInterface` with `Init` and `Invoke` methods, and a simple `createAsset` private function. It shows how to interact with the `Stub` and return appropriate `ChaincodeResponse` objects.

import { ChaincodeInterface, Stub, SuccessResponse, ErrorResponse, ChaincodeResponse } from 'fabric-shim-api';

class MyChaincode implements ChaincodeInterface {
    async Init(stub: Stub): Promise<ChaincodeResponse> {
        console.info('=========== Instantiated MyChaincode ===========');
        return SuccessResponse.newSuccess('Chaincode initialized successfully.');
    }

    async Invoke(stub: Stub): Promise<ChaincodeResponse> {
        console.info('=========== Invoked MyChaincode ===========');
        const ret = stub.getFunctionAndParameters();
        const method = this[ret.fcn as keyof MyChaincode];

        if (!method) {
            console.error(`No method found for function: ${ret.fcn}`);
            return ErrorResponse.newError(`Unknown function: ${ret.fcn}`);
        }

        try {
            const payload = await method.apply(this, [stub, ret.params]);
            return SuccessResponse.newSuccess(payload);
        } catch (err: any) {
            console.error(`Error calling function ${ret.fcn}: ${err.message}`);
            return ErrorResponse.newError(err.message);
        }
    }

    private async createAsset(stub: Stub, args: string[]): Promise<Buffer> {
        if (args.length !== 2) {
            throw new Error('Incorrect number of arguments. Expecting 2 (assetName, assetValue).');
        }
        const [assetName, assetValue] = args;
        await stub.putState(assetName, Buffer.from(assetValue));
        return Buffer.from(`Asset ${assetName} created with value ${assetValue}`);
    }
}

// For demonstration, typically registered with chaincode-api or shim directly
// For a real chaincode, you'd usually pass this to start() from 'fabric-shim'
// For example: chaincode.start(new MyChaincode());

export { MyChaincode };

view raw JSON →