I am trying to integrate my chaincode with the frontend by creating APIs. I am currently using the chaincode from fabric-samples (/asset-basic-transfer/chaincode-javascript).
If I try to create the APIs using the traditional way, I get this error:
Cannot read property 'putState' of undefined
I am getting the error when I use:
await ctx.stub.putState(id, Buffer.from(stringify(sortKeysRecursive(asset))));.
When I just call the function using:
assetTransfer.CreateAsset(ctx, id, data) - I get an error saying that CreateAsset is not a function.
How to fix this error? Or, alternatively, how to create APIs that can be called?
It is worth making sure you understand the key concepts of a Hyperledger Fabric blockchain network, and how the different components relate:
https://hyperledger-fabric.readthedocs.io/en/release-2.2/network/network.html#invoking-a-smart-contract
https://hyperledger-fabric.readthedocs.io/en/release-2.2/smartcontract/smartcontract.html
A chaincode (or smart contract) runs at the server side and is invoked by an endorsing peer in response to client proposals. So you would not invoke chaincode functions or APIs directly from your application layer. Instead, you should look to use one of the Fabric application APIs to submit or evaluate transactions that will drive your deployed smart contract:
https://hyperledger-fabric.readthedocs.io/en/release-2.2/getting_started.html#hyperledger-fabric-application-sdks
Your application layer would provide an API that can be invoked by your frontend, and would use a Fabric application API to interact with Fabric on behalf of the frontend.
Related
i have a current channel of hyperledger fabric with chaincode that does CRUD operations my question is that is there any way to make a method in a chaincode that registers the new user when certain condition match.
You can use one of several methods to get the identity of the user:
GetCreator
GetId
GetMSPId
They are part of the Client Identity Chaincode Library, documented here: https://pkg.go.dev/github.com/hyperledger/fabric-chaincode-go/pkg/cid#section-readme
The Hyperledger Fabric docs provide some help for access control also if you're interested in that: https://hyperledger-fabric.readthedocs.io/en/latest/private-data/private-data.html?highlight=getmspid#private-data-sharing-patterns
In Hyperledger ledger, how could I list endorsing peers of a specific chaincode?
I have a channel with 5 peers but I need that only three of them endorse a transaction while the other two need only to access to same ledger.
You can use the discovery service to get the endorsing peer. Here you can find some information:
https://hyperledger-fabric.readthedocs.io/en/latest/discovery-overview.html
Hyperledger fabric has a CLI tool to query the discovery service:
https://hyperledger-fabric.readthedocs.io/en/latest/discovery-cli.html
For node.js applications, you can follow the tutorial here:
https://hyperledger.github.io/fabric-sdk-node/release-1.4/tutorial-discovery.html
With the latest Fabric Client SDKs (personally using GO client), you don't need to deal with endorsement policies. Client SDK already does this dirty work for you. It extracts:
Which organizations' endorsements are required for this chaincode, key (key level endorsement policy can be defined as well), etc.
Which peers currently exist in the system and what are their MSPs (from discovery service).
According to these information, client SDK builds a set of necessary peers then sends your transaction request to all. It waits response from requested endorsers. Once it collects all responses, it sends to orderer and so on.
If you have a special case which you need to manually set endorsing peers of your choice (I had one use case about private collections so I had to use this approach) check out discovery service API of your client SDK.
I'm wondering how is possible to send notifications from the chaincode of a fabric network.
What I would like to do is send a notification to an external legacy system every time a new transaction is successfully committed to the ledger.
To achieve this I thought about two different ways:
HTTP Request - Is possible to execute an http request directly from
the chaincode? If yes, is it possible to send an http request to the
endpoint of the legacy system to notify the transaction?
Event - I understood that there is the possibility to create
events in the chaincode. Is possible to listen these events without
using the Fabric SDK (I can't integrate the SDK in the legacy
system)?
What are your considerations about these two approaches?
Do you have any suggestion?
HTTP Request
Yes, this possible, but definitely not recommended. The chaincode is used to endorse transaction proposals, but one peer's endorsement of a transaction proposal does not necessarily mean that transaction itself will be committed. Sending the request from chaincode execution would be premature. You can read more about transaction flow here and here
Additionally, the chaincode is not necessarily (and not supposed to be) executed on only one peer. If you have the request being sent from the chaincode and you have 10 peers executing the chaincode, you're going to have 10 requests going to your legacy system.
Event
You can set custom events in the chaincode, but you won't need them as each sdk supports notification of transaction commitment. While I understand it is not reasonable to embed the sdk within your legacy service, the sdk is most likely the best place for you to listen for events.
Peer Channel Event Service
From 1.3 moving forward the peer will has a specific channel event service. I will not go into this as I have not used it yet and I'm assuming if your legacy system cannot integrate an sdk it most likely will not support making grpc calls against a peer service.
Solution
You will probably need to cobble together the HTTP Request and Event solutions. Have a separate service use the sdk to send transaction proposals and listen for notifications of transaction commitment. Once a transaction is committed, use this service to send a request to your legacy system's endpoint.
The GET Endpoints for transactions only return the transactionId but I would like to get the block number too.
Is it possible to get a transaction's block number using the Hyperledger Composer REST API?. Any other workarounds involving other tools are appreciated.
This is not possible via the Composer REST API.
However it should be possible using the Composer Javascript API - using the getNativeAPI() method from the BusinessNetworkConnection class. This calls through to the Fabric SDK and queryTransaction() method of the channel class provides block information. Alternatively queryInfo() method will provide the block height.
The Composer API doc can be found here:
https://hyperledger.github.io/composer/latest/api/api-doc-index
and the Fabric SDK here:
https://fabric-sdk-node.github.io/Channel.html
Note that the getNativeAPI() function available in the Composer Transaction Processor only exposes the chaincodeStub class in the Fabric SDK and so can't find Block information, which is why this is not possible in the REST API.
The Blockchain Explorer, has a REST API which exposes block information, however it is another component to configure and manage.
Does Hyperledger Fabric support possibility to create a cryptocurrency like well know Bitcoin/Ethereum?
I don't mean tokens which I can implement by chaincode.
You can implement any business logic by using Hyperledger Fabric chaincode, which essentially a simple program. Chaincode manages ledger state by operation on transactions submitted by application and ensure to have it consistent across network peers.
Hyperledger Fabric currently supports chaincodes written in Go, while in a future will be added support for nodeJS and Java. Chaincode interface defined as following:
// Chaincode interface must be implemented by all chaincodes. The fabric runs
// the transactions by calling these functions as specified.
type Chaincode interface {
// Init is called during Instantiate transaction after the chaincode container
// has been established for the first time, allowing the chaincode to
// initialize its internal data
Init(stub ChaincodeStubInterface) pb.Response
// Invoke is called to update or query the ledger in a proposal transaction.
// Updated state variables are not committed to the ledger until the
// transaction is committed.
Invoke(stub ChaincodeStubInterface) pb.Response
}
So you can implement your cryptocurrency into chaincode. To get an inspiration on how you can implement it, you might want to take a look on following demo application of balance-transfer.
There is a Token feature in the alpha release of 2.0, you can check it out: https://hyperledger-fabric.readthedocs.io/en/latest/whatsnew.html#fabtoken
Also check here for
Can we create non-fungible tokens with Hyperledger?
The platform-neutral Token Taxonomy Initiative overseen by the Enterprise Ethereum Alliance (EEA) has announced the publication of the Token Taxonomy Framework (TTF) V 1.0, which enables businesses and developers to universally understand and define what a token is in non-technical terms, regardless of how it is implemented.