
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
fabric-contract-api
Advanced tools
A node.js implementation of Hyperledger Fabric chaincode shim, to allow endorsing peers and user-provided chaincodes to communicate with each other
The fabric-contract-api
provides the contract interface. a high level API for application developers to implement Smart Contracts. Within Hyperledger Fabric, Smart Contracts are also known as Chaincode. Working with this API provides a high level entry point to writing business logic.
(this contract interface is experimental, and feedback is actively requested; whilst there are no plans to change this api it might be latered as a result of feedback).
The fabric-shim
provides the chaincode interface, a lower level API for implementing "Smart Contracts". It also currently provides the implementation to support communication with Hyperledger Fabric peers for Smart Contracts written using the fabric-contract-api
. 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: http://hyperledger-fabric.readthedocs.io/en/latest/chaincode.html.
npm install --save fabric-contract-api
Implement a class that ends the contract
class, a constructor is needed.
The other functions will be invokable functions of your Smart Contract
// updatevalues.js
'use strict';
// SDK Library to asset with writing the logic
const { Contract } = require('fabric-contract-api');
// Business logic (well just util but still it's general purpose logic)
const util = require('util');
/**
* Support the Updating of values within the SmartContract
*/
class UpdateValuesContract extends Contract
constructor(){
super('org.mynamespace.updates');
}
async transactionA(ctx, newValue) {
// retrieve existing chaincode states
let oldValue = await ctx.stub.getState(key);
await ctx.stub.putState(key, Buffer.from(newValue));
return Buffer.from(newValue.toString());
}
async transactionB(ctx) {
// .....
}
};
module.exports = UpdateValuesContract
As with standard node modules make sure that this class is exported as follows.
// index.js
'use strict';
const UpdateValues = require('./updatevalues')
module.exports.contracts = ['UpdateValues'];
Note: In order to make this contract runnable in version 1.4, also install the fabric-shim
module as below, and ensure that the 'start' script in package.json
refers to startChaincode
"scripts": {
"start": "fabric-chaincode-node start"
}
npm install --save fabric-shim
The chaincode interface contains two methods to be implemented:
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:
shim.start(new Chaincode());
Visit fabric-shim.github.io and click on "Classes" link in the navigation bar on the top to view the list of class APIs.
Tested with node.js 8.9.0 (LTS).
This package is distributed under the Apache License, Version 2.0, see LICENSE.txt for more information.
FAQs
A node.js implementation of Hyperledger Fabric chaincode shim, to allow endorsing peers and user-provided chaincodes to communicate with each other
We found that fabric-contract-api demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.