Difference between revisions of "IT-SDK-Hyperledger-Fabric-Dev"
Jump to navigation
Jump to search
Samerhijazi (talk | contribs) (→Node.js / JavaScript) |
Samerhijazi (talk | contribs) (→Wallet) |
||
| (38 intermediate revisions by the same user not shown) | |||
| Line 10: | Line 10: | ||
*https://nodejs.org/docs/latest-v12.x/api/ ('''Node.js''') | *https://nodejs.org/docs/latest-v12.x/api/ ('''Node.js''') | ||
*https://developer.mozilla.org/en-US/docs/Web/JavaScript ('''MDN''') | *https://developer.mozilla.org/en-US/docs/Web/JavaScript ('''MDN''') | ||
| + | |||
| + | =Begriffe= | ||
| + | * '''Ref''': https://hyperledger-fabric.readthedocs.io/en/latest/ledger/ledger.html#the-ledger | ||
| + | * '''Actor''': Is an Participants like peers, users, orderers. | ||
| + | * '''Identity''': Every actor has an identity certificate. | ||
| + | * '''Leger''': Record all state transitions, result of chaincode invocations "transactions". One Leger per Channel. Ledger consists of ('''World-State'''/'''Blockchain''') | ||
| + | * '''Blockchain''': is a transaction log/history that records all the changes/modifications that have resulted in the current world-state. Is immutable (unchangeable). | ||
| + | * '''World-State''': is a database that represent the current values of ledger states. | ||
| + | * '''Smart-Contract''': Software-Program that defines business logic. | ||
| + | * '''Chaincode''': A Smart-Contract packaged in a Chaincode then deployed to blockchain-network. | ||
| + | * '''Client-Application''': interact with blockchain network on behalf of useres. | ||
| + | * '''Wallet''': contains a sset of user identities to connects to a channel | ||
| + | * '''Gateway''': manges the network interactions on behalf of application. | ||
| + | * '''Transaction''': | ||
| + | * '''Wallet''': contains a set of user identities. With these identities user connects to a channel and access a leger. | ||
=Components= | =Components= | ||
| − | * ''' | + | * '''Ref''': https://hyperledger-fabric.readthedocs.io/en/release-2.2/peers/peers.html |
| − | |||
| − | |||
| − | |||
| − | |||
* '''Blockchain-Network''': | * '''Blockchain-Network''': | ||
| − | * ''' | + | * '''Channel''': Each channel has a completely separate '''ledger'''. This means a completely separate '''blockchain''', and completely separate '''world-states'''. |
| + | * '''CA''' (Certificate authorities): Issue identities by generating public and private key forming key-pair that can be used to prove an identity. | ||
| + | * '''MSP''' (Memberahip Service Provider): contains a list of permissioned identities (public keys). | ||
| + | * '''Peer''': Is a Nodes that host a ledger and a Smart-Contract/Chaincode. Chaincodes generate transaction and access the leger. | ||
| + | * '''Orderer''': | ||
| + | |||
| + | =Chaincode Deployment/Upgrade= | ||
| + | *'''Package''' the chaincode. | ||
| + | *'''Install''' the chaincode package. | ||
| + | *'''Approval''' the chaincode definition. | ||
| + | *'''Commit''' the chaincode defintion to a channel. | ||
| + | |||
| + | =Commands= | ||
| + | <pre class="code"> | ||
| + | peer lifecycle chaincode package $PACKAGE_NAME.tar.gz --path $PATH_SOURCECODE --lang $LANG --label $LABEL | ||
| + | peer lifecycle chaincode install $PACKAGE_NAME.tat.gz | ||
| + | peer lifecycle chaincode queryinstalled | ||
| + | peer lifecycle chaincode approveformyorg --orderer $ORDERER --channelID $CHANNEL_ID --name $CHAINCOD_NAME --version 1.0 --package-id $PACKAGE_ID --sequence 1 | ||
| + | peer lifecycle chaincode checkcommitreadiness --channelID $CHANNEL_ID --name $CHAINCOD_NAME --version 1.0 --sequence 1 --output json | ||
| + | peer lifecycle chaincode commit --orderer $ORDERER --channelID $CHANNEL_ID --name $CHAINCODE_NAME --version 1.0 --sequence 1 --peerAddresses $PEER_ADDRESSE | ||
| + | peer lifecycle chaincode querycommitted --channelID $CHANNEL_ID --name $CHAINCODE_NAME | ||
| + | peer chaincode invoke/query --orderer $ORDERER --channelID $CHANNEL_ID --name $CHAINCODE_NAME --ctor $MASSAGE | ||
| + | ------------------------------------------------------------------------------------------------------------------ | ||
| + | ./network.sh deployCC -ccn $NAME -ccl javascript -ccp $PATH -ccv 1.0 -ccs 1 -cci init | ||
| + | </pre> | ||
| − | = | + | =Dev-Chaincode= |
| − | * | + | <pre class="code"> |
| − | * | + | ctx.stub.getState(key); |
| − | * Gateway: | + | ctx.stub.deleteState(key); |
| − | = | + | ctx.stub.putState(key,value); |
| − | + | ctx.stub.getStateByRange(keyFrom, keyTo); | |
| − | + | --------------------------------------------------------- | |
| − | + | ctx.stub.createCompositeKey(objType, [key]); | |
| − | + | ctx.stub.splitCompositeKey(key); | |
| + | ctx.stub.getStateByPartialCompositeKey(objType, []); | ||
| + | --------------------------------------------------------- | ||
| + | ctx.stub.getQueryResult(queryString); | ||
| + | ctx.stub.getHistoryForKey(compositeKey); | ||
| + | --------------------------------------------------------- | ||
| + | ctx.clientIdentity.getMSPID(); | ||
| + | ctx.clientIdentity.getID(); | ||
| + | --------------------------------------------------------- | ||
| + | ctx.stub.invokeChaincode(chaincode, args, channel); | ||
| + | </pre> | ||
| + | =Dev-Application= | ||
| + | * Interaction with blockchain-network (Channel & Chaincode) through a Gateway. (fabric-network) | ||
| + | * CA-Actions: Register user, enroll user, revoke a user. (fabric-ca-client) | ||
| + | * Wallet-Types: FileSystemWallet, InMemoryWallet, CouchDBWallet | ||
| + | * Gateway: Connection profile/configuration | ||
| + | ==Wallet== | ||
| + | <pre class="code"> | ||
| + | await Wallets.newFileSystemWallet('./wallet'); | ||
| + | await wallet.put(identityLabel, identity); | ||
| + | await wallet.get(identityLabel); | ||
| + | await wallet.remove(identityLabel); | ||
| + | await wallet.list(); | ||
| + | </pre> | ||
| + | ==Gateway== | ||
| + | <pre class="code"> | ||
| + | const gateway = new Gateway(); | ||
| + | await gateway.connect(connectionProfile, connectionOptions); | ||
| + | const network = await gateway.getNetwork('mychannel'); | ||
| + | const contract = network.getContract('balance_transfer'); | ||
| + | const response = await contract.submitTransaction(functionName, ...chaincodeArgs); | ||
| + | gateway.disconnect(); | ||
| + | </pre> | ||
| + | |||
| + | ==Enroll-User== | ||
| + | <pre class="code"> | ||
| + | ### enrollUser.js #### | ||
| + | const ca = new FabricCAServices(connectionProfile['certificateAuthorities'][`ca.${orgName}`].url); | ||
| + | let enrollmentRequest = { | ||
| + | enrollmentID: enrollmentID, | ||
| + | enrollmentSecret: enrollmentSecret, | ||
| + | attr_reqs: enrollmentAttributes | ||
| + | }; | ||
| + | const enrollment = await ca.enroll(enrollmentRequest); | ||
| + | identity = { | ||
| + | credentials: { | ||
| + | certificate: enrollment.certificate, | ||
| + | privateKey: enrollment.key.toBytes(), | ||
| + | }, | ||
| + | mspId: `${orgNameCapitalized}MSP`, | ||
| + | type: 'X.509', | ||
| + | }; | ||
| + | await wallet.put(identityLabel, identity); | ||
| + | </pre> | ||
| + | ==Register-User== | ||
| + | <pre class="code"> | ||
| + | ### registerUser.js ### | ||
| + | const ca = new FabricCAServices(connectionProfile['certificateAuthorities'][`ca.${orgName}`].url); | ||
| + | const provider = wallet.getProviderRegistry().getProvider(registrarIdentity.type); | ||
| + | const registrarUser = await provider.getUserContext(registrarIdentity, registrarLabel); | ||
| + | let registerRequest = { | ||
| + | enrollmentID: enrollmentID, | ||
| + | enrollmentSecret: optional.secret || "", | ||
| + | role: 'client', | ||
| + | attrs: optional.attrs || [] | ||
| + | }; | ||
| + | const secret = await ca.register(registerRequest, registrarUser); | ||
| + | </pre> | ||
| + | ==NPM-Commands== | ||
| + | <pre class="code"> | ||
| + | npm init -y | ||
| + | npm install | ||
| + | node addToWallet.js | ||
| + | --------------------------------------------------------------------------------- | ||
| + | node enrollUser.js 'CAAdmin@org1.example.com' admin adminpw | ||
| + | --------------------------------------------------------------------------------- | ||
| + | node registerUser.js 'CAAdmin@org1.example.com' 'User@org1.example.com' '{"secret": "userpw"}' | ||
| + | node enrollUser.js 'User@org1.example.com' 'User@org1.example.com' userpw | ||
| + | </pre> | ||
Latest revision as of 20:25, 19 December 2021
Contents
Resources Allowed for Certification
Hyperledger Fabric
- https://hyperledger-fabric.readthedocs.io/en/release-2.2/ (Documentation)
- https://hyperledger-fabric.readthedocs.io/en/release-2.2/command_ref.html (Command Reference)
- https://hyperledger.github.io/fabric-chaincode-node/release-2.2/api/index.html (Fabric-Contract API)
- https://hyperledger.github.io/fabric-sdk-node/release-2.2/index.html (Fabric-Application API)
- https://wiki.hyperledger.org/display/fabric/Hyperledger+Fabric (WIKI)
Node.js / JavaScript
- https://nodejs.org/docs/latest-v12.x/api/ (Node.js)
- https://developer.mozilla.org/en-US/docs/Web/JavaScript (MDN)
Begriffe
- Ref: https://hyperledger-fabric.readthedocs.io/en/latest/ledger/ledger.html#the-ledger
- Actor: Is an Participants like peers, users, orderers.
- Identity: Every actor has an identity certificate.
- Leger: Record all state transitions, result of chaincode invocations "transactions". One Leger per Channel. Ledger consists of (World-State/Blockchain)
- Blockchain: is a transaction log/history that records all the changes/modifications that have resulted in the current world-state. Is immutable (unchangeable).
- World-State: is a database that represent the current values of ledger states.
- Smart-Contract: Software-Program that defines business logic.
- Chaincode: A Smart-Contract packaged in a Chaincode then deployed to blockchain-network.
- Client-Application: interact with blockchain network on behalf of useres.
- Wallet: contains a sset of user identities to connects to a channel
- Gateway: manges the network interactions on behalf of application.
- Transaction:
- Wallet: contains a set of user identities. With these identities user connects to a channel and access a leger.
Components
- Ref: https://hyperledger-fabric.readthedocs.io/en/release-2.2/peers/peers.html
- Blockchain-Network:
- Channel: Each channel has a completely separate ledger. This means a completely separate blockchain, and completely separate world-states.
- CA (Certificate authorities): Issue identities by generating public and private key forming key-pair that can be used to prove an identity.
- MSP (Memberahip Service Provider): contains a list of permissioned identities (public keys).
- Peer: Is a Nodes that host a ledger and a Smart-Contract/Chaincode. Chaincodes generate transaction and access the leger.
- Orderer:
Chaincode Deployment/Upgrade
- Package the chaincode.
- Install the chaincode package.
- Approval the chaincode definition.
- Commit the chaincode defintion to a channel.
Commands
peer lifecycle chaincode package $PACKAGE_NAME.tar.gz --path $PATH_SOURCECODE --lang $LANG --label $LABEL peer lifecycle chaincode install $PACKAGE_NAME.tat.gz peer lifecycle chaincode queryinstalled peer lifecycle chaincode approveformyorg --orderer $ORDERER --channelID $CHANNEL_ID --name $CHAINCOD_NAME --version 1.0 --package-id $PACKAGE_ID --sequence 1 peer lifecycle chaincode checkcommitreadiness --channelID $CHANNEL_ID --name $CHAINCOD_NAME --version 1.0 --sequence 1 --output json peer lifecycle chaincode commit --orderer $ORDERER --channelID $CHANNEL_ID --name $CHAINCODE_NAME --version 1.0 --sequence 1 --peerAddresses $PEER_ADDRESSE peer lifecycle chaincode querycommitted --channelID $CHANNEL_ID --name $CHAINCODE_NAME peer chaincode invoke/query --orderer $ORDERER --channelID $CHANNEL_ID --name $CHAINCODE_NAME --ctor $MASSAGE ------------------------------------------------------------------------------------------------------------------ ./network.sh deployCC -ccn $NAME -ccl javascript -ccp $PATH -ccv 1.0 -ccs 1 -cci init
Dev-Chaincode
ctx.stub.getState(key); ctx.stub.deleteState(key); ctx.stub.putState(key,value); ctx.stub.getStateByRange(keyFrom, keyTo); --------------------------------------------------------- ctx.stub.createCompositeKey(objType, [key]); ctx.stub.splitCompositeKey(key); ctx.stub.getStateByPartialCompositeKey(objType, []); --------------------------------------------------------- ctx.stub.getQueryResult(queryString); ctx.stub.getHistoryForKey(compositeKey); --------------------------------------------------------- ctx.clientIdentity.getMSPID(); ctx.clientIdentity.getID(); --------------------------------------------------------- ctx.stub.invokeChaincode(chaincode, args, channel);
Dev-Application
- Interaction with blockchain-network (Channel & Chaincode) through a Gateway. (fabric-network)
- CA-Actions: Register user, enroll user, revoke a user. (fabric-ca-client)
- Wallet-Types: FileSystemWallet, InMemoryWallet, CouchDBWallet
- Gateway: Connection profile/configuration
Wallet
await Wallets.newFileSystemWallet('./wallet');
await wallet.put(identityLabel, identity);
await wallet.get(identityLabel);
await wallet.remove(identityLabel);
await wallet.list();
Gateway
const gateway = new Gateway();
await gateway.connect(connectionProfile, connectionOptions);
const network = await gateway.getNetwork('mychannel');
const contract = network.getContract('balance_transfer');
const response = await contract.submitTransaction(functionName, ...chaincodeArgs);
gateway.disconnect();
Enroll-User
### enrollUser.js ####
const ca = new FabricCAServices(connectionProfile['certificateAuthorities'][`ca.${orgName}`].url);
let enrollmentRequest = {
enrollmentID: enrollmentID,
enrollmentSecret: enrollmentSecret,
attr_reqs: enrollmentAttributes
};
const enrollment = await ca.enroll(enrollmentRequest);
identity = {
credentials: {
certificate: enrollment.certificate,
privateKey: enrollment.key.toBytes(),
},
mspId: `${orgNameCapitalized}MSP`,
type: 'X.509',
};
await wallet.put(identityLabel, identity);
Register-User
### registerUser.js ###
const ca = new FabricCAServices(connectionProfile['certificateAuthorities'][`ca.${orgName}`].url);
const provider = wallet.getProviderRegistry().getProvider(registrarIdentity.type);
const registrarUser = await provider.getUserContext(registrarIdentity, registrarLabel);
let registerRequest = {
enrollmentID: enrollmentID,
enrollmentSecret: optional.secret || "",
role: 'client',
attrs: optional.attrs || []
};
const secret = await ca.register(registerRequest, registrarUser);
NPM-Commands
npm init -y
npm install
node addToWallet.js
---------------------------------------------------------------------------------
node enrollUser.js 'CAAdmin@org1.example.com' admin adminpw
---------------------------------------------------------------------------------
node registerUser.js 'CAAdmin@org1.example.com' 'User@org1.example.com' '{"secret": "userpw"}'
node enrollUser.js 'User@org1.example.com' 'User@org1.example.com' userpw