In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to configure the identity in fabric node sdk". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. The working principle of fabric node sdk
In the hyperledger Fabric block chain architecture, the application accesses the chain code installed on the peer node through the node's RPC protocol API interface:
Similar to the chain code communication protocol encapsulated by shim api, Fabric Node SDK provides the encapsulation of node RPC protocol interface. The entry class is Client, and the channel operation is encapsulated in the Channel class:
2. Identity in fabric node sdk
Because hyperledger fabric is a Permissioned blockchain, every member participating in the fabric blockchain needs to have a clear identity, so the Client instance in fabric node sdk needs to use a User instance to represent its identity of accessing the Fabric blockchain network. For example, the following node.js code loads the PEM-encoded user certificate and the corresponding key in the local msp directory, generates the User object, and then sets it to the current identity of the Client instance:
Let client = new Clientlet keyPem = fs.readFileSync ('.msp/keystore/user-key.pem','utf-8') let certPem = fs.readFileSync ('. / msp/signcerts/user-cert.pem','utf-8') let user = await client.createUser ({/ / create User object username: 'user', / / user name mspid:' SampleOrg' / / ID cryptoContent of the MSP to which it belongs: {privateKeyPEM: keyPem, / / user private key signedCertPEM: certPem / / user certificate}, skipPersistence: true / / not included in the cache}) client.setUserContext (user,true) / / set to the current identity of client, fabric node sdk tunnel configuration
You can create an empty channel object using the newChannel () method of the Client instance, and we also need to add Peer and Orderer instances so that the channel object knows the topology of the fabric network to be accessed. For example, the following node.js code configures the peer peer node and orderer sorting node deployed on the local machine for the channel object. By default, the peer node listens on port 7051 and the sorting node listens on port 7052:
Let channel = client.newChannel ('ch2') channel.addPeer (client.newPeer (' grpc://127.0.0.1:7051')) channel.addOrderer (client.newOrderer ('grpc://127.0.0.1:7050')) 3, fabric node sdk query chain code status
In fabric node sdk, the queryByChaincode () method of the channel object is used to query the chain code status. For example, the following node.js code calls the value () method defined in the chain code counter-cc to get the current value of the counter:
Let req = {chaincodeId: 'counter-cc', fcn:' value', args: []} let ret = await channel.queryByChaincode (req) 4, fabric node sdk submit chain code transaction
In fabric node sdk, submitting a transaction to the chain code is more complicated, and the two methods sendTransactionProposal () and sendTransaction () need to be used in turn. This is because hyperledger fabric introduces an endorsement mechanism, and before the application submits the transaction to the sorting node, it needs to obtain the endorsement of the peer node according to the established strategy.
The timing of submitting chain code transactions is as follows:
The following code shows the two-phase commit process of a chain code transaction in fabric node sdk:
Let req = {chaincodeId: 'counter-cc', fcn:' inc', args: ['10'], txId: client.newTransactionID ()} let prsp = await channel.sendTransactionProposal (req) / / get peer endorsement let rsp = await channel.sendTransaction ({/ / submit transaction proposalResponses: prsp [0], proposal: prsp [1]}) "how to configure identities in fabric node sdk" ends here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.