# fabric-shim

> A node.js implementation of Hyperledger Fabric chaincode shim, to allow endorsing peers and user-provided chaincodes to communicate with each other

Latest version **2.5.8** (published 2024-12-04) · Apache-2.0 license · 22.2K weekly downloads

## Install

```sh
npm install fabric-shim
pnpm add fabric-shim
yarn add fabric-shim
bun add fabric-shim
```

Provides the command `fabric-chaincode-node`.

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 2.5.8 |
| Published | 2024-12-04 |
| First published | 2017-08-17 |
| Weekly downloads | 22.2K |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=18 |
| Dependencies | 13 |
| Unpacked size | 1.6 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 263 |
| Maintainers | hyperledger-lf, hyperledger-ghci |
| Keywords | fabric-shim, Hyperledger Fabric, Fabric Shim |

## Links

- npm: https://www.npmjs.com/package/fabric-shim
- Repository: https://github.com/hyperledger/fabric-chaincode-node
- Homepage: https://github.com/hyperledger/fabric-chaincode-node#readme
- Issues: https://github.com/hyperledger/fabric-chaincode-node/issues
- npm.io page: https://npm.io/package/fabric-shim

## Dependencies (13)

- [ajv](https://npm.io/package/ajv.md) ^6.12.2
- [long](https://npm.io/package/long.md) ^5.2.3
- [yargs](https://npm.io/package/yargs.md) ^17.4.0
- [winston](https://npm.io/package/winston.md) ^3.7.2
- [@fidm/x509](https://npm.io/package/@fidm/x509.md) ^1.2.1
- [@types/node](https://npm.io/package/@types/node.md) ^16.11.1
- [yargs-parser](https://npm.io/package/yargs-parser.md) ^21.0.1
- [@grpc/grpc-js](https://npm.io/package/@grpc/grpc-js.md) ^1.11.0
- [fabric-shim-api](https://npm.io/package/fabric-shim-api.md) 2.5.8
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [fabric-contract-api](https://npm.io/package/fabric-contract-api.md) 2.5.8
- [fast-safe-stringify](https://npm.io/package/fast-safe-stringify.md) ^2.1.1
- [@hyperledger/fabric-protos](https://npm.io/package/@hyperledger/fabric-protos.md) ^0.2.2

## Recent versions

- 2.5.8 (latest) — 2024-12-04
- 2.5.3 (unstable) — 2023-03-17
- 2.4.2-dev-20220504 (release) — 2022-05-04
- 2.4.0-dev-20211109 (beta) — 2021-11-09
- 1.4.6 (latest-1.4) — 2020-09-16
- 2.0.0-alpha (alpha) — 2019-04-09
- 1.1.5 (latest-1.1) — 2018-12-21
- 1.2.4 (latest-1.2) — 2018-12-21
- 1.4.0-rc2 (rc2) — 2018-12-20
- 1.4.0-rc1 (rc1) — 2018-12-11
- 2.5.7 — 2024-09-27
- 2.5.6 — 2024-06-13
- 2.5.5 — 2024-05-31
- 2.5.4 — 2023-06-16
- 2.5.2 — 2023-01-25
- … 290 more at https://npm.io/package/fabric-shim/versions

## README

[![NPM](https://nodei.co/npm/fabric-shim.svg?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/fabric-contract-api/)



[![Version](https://badge.fury.io/js/fabric-shim.svg)](http://badge.fury.io/js/fabric-shim)


The `fabric-shim` provides the *chaincode interface*, a lower level API for implementing "Smart Contracts". To confirm that this is the same as the `fabric-shim` in previous versions of Hyperledger Fabric.

Detailed explanation on the concept and programming model can be found here: [https://hyperledger-fabric.readthedocs.io/en/latest/smartcontract/smartcontract.html](https://hyperledger-fabric.readthedocs.io/en/latest/smartcontract/smartcontract.html).


## Chaincode Interface

### Installation
```sh
npm install --save fabric-shim
```

### Usage
The [chaincode interface](https://hyperledger.github.io/fabric-chaincode-node/main/api/fabric-shim.ChaincodeInterface.html) contains two methods to be implemented:
```javascript
const shim = require('fabric-shim');

const Chaincode = class {
	async Init(stub) {
		// use the instantiate input arguments to decide initial chaincode state values

		// save the initial states
		await stub.putState(key, Buffer.from(aStringValue));

		return shim.success(Buffer.from('Initialized Successfully!'));
	}

	async Invoke(stub) {
		// use the invoke input arguments to decide intended changes

		// retrieve existing chaincode states
		let oldValue = await stub.getState(key);

		// calculate new state values and saves them
		let newValue = oldValue + delta;
		await stub.putState(key, Buffer.from(newValue));

		return shim.success(Buffer.from(newValue.toString()));
	}
};
```

Start the chaincode process and listen for incoming endorsement requests:
```javascript
shim.start(new Chaincode());
```

### API Reference
Visit [API Reference](https://hyperledger.github.io/fabric-chaincode-node/main/api/) and click on "Classes" link in the navigation bar on the top to view the list of class APIs.

## Support
Tested with node.js 8.9.0 (LTS).

## License

This package is distributed under the
[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0),
see LICENSE.txt for more information.

---
_Source: https://npm.io/package/fabric-shim · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
