cacti-weaver-fabric-cli 2.0.0-dev.93
Install from the command line:
Learn more about npm packages
$ npm install @hyperledger/cacti-weaver-fabric-cli@2.0.0-dev.93
Install via package.json:
"@hyperledger/cacti-weaver-fabric-cli": "2.0.0-dev.93"
About this version
A CLI for interacting with the Fabric test-net and relays. Made using gluegun.js
fabric-cli options:
--help can be called on every command and provides examples and usage.
More documentation can be found here
.
├── build # Compiled files
├── docs # Documentation files
├── scripts # Utility scripts
├── src # Source files
│ ├── commands # Commands for the cli. Subcommands are in named folders
│ ├── data # Data used across the commands, includes credentials for other networks and basic data to initialise the network with
│ ├── helpers # Helper functions that are used across the commands
│ └── wallet-${networkname} # Wallets used by the CLI to connect with the Fabric networks.
└── ...
Another naming convention made is inside the commands folder files suffixed with "-helper
" are used to for the --help
options for each command.
Notes |
---|
If you are using a linux system make sure that lib64 is installed |
Tested on Node v11.14.0 to v16.0.0; requires Node >= 11.14.0 <= 16.0.0 |
The Fabric CLI can be configured either to depend on the published Weaver SDK or the locally built version.
The fabric-cli
depends on the npm package @hyperledger/cacti-weaver-sdk-fabric
. To pull this package from Github, run the following steps:
- Create a Personal Access Token from Github with read access to packages. Refer Creating a Personal Access Token for help. \
- Create a copy of
.npmrc.template
as.npmrc
. \ - Replace in copied
.npmrc
file with your personal access token. \ - Run
npm install
to check if it is working before testing/deployment.
To install package dependencies, run:
make build
Make sure that the protos-js and weaver-fabric-interop-sdk have been built already according to the given instructions.
To install package dependencies, run:
make build-local
Set up .env
by copying across the .env.template
and updating the values
Set up config.json
by adding the connection profile and relay port for each network. (Can be set with env variable CONFIG_PATH
)
(Editing of the env and config can be done via the CLI with the fabric-cli env set
and fabric-cli config set
commands)
Run:
./bin/fabric-cli [commands-and-parameters]
To view list of commands and associated parameters, run:
./bin/fabric-cli -h
Set up .npmrc
by copying across the .npmrc.template
and updating the values as specified earlier.
Run make build-image
to build fabric-cli docker image.
Then run docker-compose up -d
to start fabric-cli container.
Then run docker exec -it fabric-cli bash
, to open interactive shell for fabric-cli, where regular fabric-cli calls can be made.
Setting env is not required inside docker, as env variables are already declared, so next steps would be:
./bin/fabric-cli config set-file $CONFG_PATH
-
./bin/fabric-cli configure all network1 network2
.
To record a key test
with the value teststate
via the simplestate
contract deployed on the mychannel
channel in network1
, run:
$ ./bin/fabric-cli chaincode invoke mychannel simplestate create '["test", "teststate"]' --local-network=network1
To now query the value of the the test
key, run:
$ ./bin/fabric-cli chaincode query mychannel simplestate read '["test"]' --local-network=network1
NOTE: Use the --help
flag with any command to view examples and usage.
-
DEFAULT_CHANNEL
(OPTIONAL) The default channel used by the CLI when invoking chaincode on a network. -
DEFAULT_CHAINCODE
(OPTIONAL) The default chaincode id used by the CLI when invoking chaincode on a network. -
MEMBER_CREDENTIAL_FOLDER
(OPTIONAL) The folder where network configurations will be stored and pulled from when generating network config and loading the chaincode. -
CONFIG_PATH
(OPTIONAL) Path to the configuration file used by the CLI when creating network credentials and communicating with the relays.
The configuration file is used by the CLI when generating credentials to communicate with the network as well as the endpoint used to communicate with the relay. Each network can specify a unique relay and connection profile.
Example config:
{
"network1": {
"connProfilePath": "",
"relayEndpoint": "",
"mspId": "",
"channelName": "",
"chaincode": ""
},
"network2": {
"connProfilePath": "",
"relayEndpoint": "",
"mspId": "",
"channelName": "",
"chaincode": ""
}
}
Examples of cross-network queries (via relays) are as follows (the last part of the command is a view address, and you will see the remote view being logged to the console):
- Fabric network
network1
requesting a view from Fabric networknetwork2
./bin/fabric-cli interop --local-network=network1 --requesting-org=Org1MSP localhost:9083/network2/mychannel:simplestate:Read:Arcturus
- Fabric network
network1
requesting a view from Corda networkCorda_network
./bin/fabric-cli interop --local-network=network2 --sign=true --requesting-org=Org1MSP localhost:9081/Corda_Network/localhost:10006#com.cordaSimpleApplication.flow.GetStateByKey:H --debug=true
- Both of the above examples assume an insecure (plaintext) gRPC connection with the local network's relay. If the relay is TLS-enabled, you will need to specify the server's TLS CA certificates as follows:
./bin/fabric-cli interop --local-network=network1 --requesting-org=Org1MSP --relay-tls=true --relay-tls-ca-files=ca_cert.pem localhost:9083/network2/mychannel:simplestate:Read:Arcturus ./bin/fabric-cli interop --local-network=network1 --sign=true --requesting-org=Org1MSP --relay-tls=true --relay-tls-ca-files=ca_cert1.pem:ca_cert2.pem localhost:9081/Corda_Network/localhost:10006#com.cordaSimpleApplication.flow.GetStateByKey:H --debug=true
- The
--relay-tls
switch indicates whether or not the local network relay requires a TLS connection - The
--relay-tls-ca-files
switch indicates the path to the relay server's TLS CA certificates. Specify a colon-separated list if there are more than one.
- The
- Fabric network
network1
requesting a view from Fabric networknetwork2
with the view contents confidential between thenetwork1
client and thenetwork2
peers./bin/fabric-cli interop --local-network=network1 --requesting-org=Org1MSP --e2e-confidentiality=true localhost:9083/network2/mychannel:simplestate:Read:Arcturus
Below are the steps to exercise asset exchange using the fabric-cli
tool.
-
Spin up both
network1
andnetwork2
with interoperation chaincode installed along with thesimpleasset
application by navigating to thetests/network-setups/fabric/dev
folder and running eithermake start-interop CHAINCODE_NAME=simpleasset
or
make start-interop-local CHAINCODE_NAME=simpleasset
depending on whether you are using pre-built Github packages or using local builds from this clone of the repository.
-
You can use the
fabric-cli
executable in thebin
folder (for the executable built using Node.js) or the one built using the Golang version after runningmake build-local
here. -
If a
config.json
(or equivalent, as set in theCONFIG_PATH
property in.env
) doesn't exist, create one by making a copy ofconfig.template.json
. In theconfig.json
, set thechaincode
field in both thenetwork1
andnetwork2
sections tosimpleasset
. -
Run the below script that performs: setting the enviroment, adding the users
Alice
andBob
to both the networks and finally adding the non-fungible (i.e., bonds) and fungible (i.e., tokens) assets into the accounts ofAlice
andBob
../scripts/initAsset.sh
-
Check the status of the assets owned by
Alice
andBob
in the two networksnetwork1
andnetwork2
, by running./scripts/getAssetStatus.sh
-
Initiate exchange of bond asset
bond01:a04
ofBob
innetwork1
with token assetstoken1:100
ofAlice
innetwork2
, by running (it only works with SHA256)./bin/fabric-cli asset exchange-all --network1=network1 --network2=network2 --secret=secrettext --timeout-duration=100 bob:bond01:a04:alice:token1:100
Repeat the step 3 to observe the change in the ownership of assets as a result of the
asset exchange
exercise. -
Generate Secret-Hash Pair using following command (prints hash in base64):
./bin/fabric-cli hash --hash_fn=SHA256 secrettext
-
The same asset exchange experiment in the above step, can be carried out by manually triggering below commands in serial order (with the help of
fabric-cli asset exchange <operation>
CLI commands) (change hash function by specifying--hash_fn=<SHA256|SHA512>
in lock and claim commands):./bin/fabric-cli asset exchange lock --timeout-duration=3600 --locker=alice --recipient=bob --hashBase64=<hash-value-base64> --target-network=network1 --param=bond01:a03 ./bin/fabric-cli asset exchange is-locked --locker=alice --recipient=bob --target-network=network1 --param=bond01:a03 ./bin/fabric-cli asset exchange lock --fungible --timeout-duration=1800 --locker=bob --recipient=alice --hashBase64=<hash-value-base64> --target-network=network2 --param=token1:100 ./bin/fabric-cli asset exchange is-locked --fungible --locker=bob --recipient=alice --target-network=network2 --contract-id=<contract-id> ./bin/fabric-cli asset exchange claim --fungible --recipient=alice --target-network=network2 --contract-id=<contract-id> --secret=<hash-pre-image> ./bin/fabric-cli asset exchange claim --recipient=bob --locker=alice --target-network=network1 --param=bond01:a03 --secret=<hash-pre-image> ./bin/fabric-cli asset exchange unlock --locker=alice --recipient=bob --target-network=network1 --param=bond01:a03 ./bin/fabric-cli asset exchange unlock --fungible --locker=bob --target-network=network2 --contract-id=<contract-id>
The asset exchange
scenario is demonstrated above using an application chaincode simpleasset
and the interop
chaincode, where the application chaincode makes invocations into the interop
chaincode when it needs to lock, claim, or reclaim/unlock assets. However, the same asset exchange
scenario can be demonstrated with the help of just the application chaincode simpleassetandinterop
which also includes the scripts to lock, claim, or reclaim/unlock assets. This requires the steps 1-5 to be exercised with minor modifications as indicated below:
- Spin up the networks by running the below command (equivalent of step 1 above)
or
make start CHAINCODE_NAME=simpleassetandinterop
depending on whether you are using pre-built Github packages or using local builds from this clone of the repository.make start-interop-local CHAINCODE_NAME=simpleassetandinterop
- Replace
simpleasset
withsimpleassetandinterop
in theconfig.json
file used to populate the.env
file that is part of the scriptinitAsset.sh
(equivalent of step 2 above) - Replace
simpleasset
withsimpleassetandinterop
in the scriptgetAssetStatus.sh
(equivalent of step 3 above)
You can repeat the above experiment with the simpleassettransfer
chaincode as well, though this chaincode contains augmentations that serve to demonstrate asset transfers (see below).
Below are the steps to exercise asset transfers from network1
to network2
using the fabric-cli
tool.
- Spin up both
network1
andnetwork2
with interoperation chaincode installed along with thesimpleassettransfer
application by navigating to thetests/network-setups/fabric/dev
folder and running eitherormake start-interop CHAINCODE_NAME=simpleassettransfer
depending on whether you are using pre-built Github packages or using local builds from this clone of the repository.make start-interop-local CHAINCODE_NAME=simpleassettransfer
- Start relays and Fabric drivers for both
network1
andnetwork2
. - Go to the Fabric Client
fabric-cli
directory (samples/fabric/fabric-cli
). - Create a
config.json
(you can pick a different name, but make sure you set the right reference in theCONFIG_PATH
property in.env
) by copyingconfig.template.json
and set certain properties as shown below (you can leave the others unchanged):{ "network1": { ... "chaincode": "simpleassettransfer", ... }, "network2": { ... "chaincode": "simpleassettransfer", "aclPolicyPrincipalType": "ca" } }
- Set the
DEFAULT_APPLICATION_CHAINCODE
property in the.env
tosimpleassettransfer
. - Set environment variables and network configuration properties as follows:
./bin/fabric-cli env set-file ./.env ./bin/fabric-cli config set-file ./config.json
- Create default access control and verification policies and record them in the two networks' ledgers as follows:
./bin/fabric-cli configure create all --local-network=network1 ./bin/fabric-cli configure create all --local-network=network2 ./bin/fabric-cli configure network --local-network=network1 ./bin/fabric-cli configure network --local-network=network2
- Initialize bond and token asset states and ownerships on the
network1
ledger as follows:This step will also create a user./scripts/initAssetsForTransfer.sh
alice
innetwork1
and a userbob
innetwork2
.
The following sequence of operations will illustrate transfers and recoveries of a bond (non-fungible) asset.
- Verify that
alice
owns bonds with idsa03
anda04
as follows:You should see a JSON structure corresponding to the bond being logged on the console in each case../bin/fabric-cli chaincode query --user=alice mychannel simpleassettransfer ReadAsset '["bond01","a03"]' --local-network=network1 ./bin/fabric-cli chaincode query --user=alice mychannel simpleassettransfer ReadAsset '["bond01","a04"]' --local-network=network1
- Get
alice
innetwork1
to pledge bonda03
tobob
innetwork2
as follows (with a 1 hour timeout):You should see a message containing the unique ID of this pledge on the console as./bin/fabric-cli asset transfer pledge --source-network=network1 --dest-network=network2 --recipient=bob --expiry-secs=3600 --type=bond --ref=a03 --data-file=src/data/assetsForTransfer.json
Asset pledged with ID <pledge-id>
(<pledge-id>
is a hexadecimal string). - Get
bob
innetwork2
to claim this bond asset as follows:
./bin/fabric-cli asset transfer claim --source-network=network1 --dest-network=network2 --user=bob --owner=alice --type=bond --pledge-id=<pledge-id> --param=bond01:a03
- Verify that
alice
innetwork1
does not own this asset as follows:
./bin/fabric-cli chaincode query --user=alice mychannel simpleassettransfer ReadAsset '["bond01","a03"]' --local-network=network1
You should see an error message like Error: the asset a03 does not exist
.
12. Verify that bob
in network2
now owns this asset as follows:
./bin/fabric-cli chaincode query --user=bob mychannel simpleassettransfer ReadAsset '["bond01","a03"]' --local-network=network2
- Now get
alice
innetwork1
to pledge bonda04
tobob
innetwork2
as follows (with a 1 minute timeout):
./bin/fabric-cli asset transfer pledge --source-network=network1 --dest-network=network2 --recipient=bob --expiry-secs=60 --type=bond --ref=a04 --data-file=src/data/assetsForTransfer.json
Wait for a minute as follows:
sleep 60
You should see a message containing the unique ID of this pledge on the console as Asset pledged with ID <pledge-id>
(<pledge-id>
is a hexadecimal string).
14. Now get bob
in network2
to claim this bond asset as follows:
./bin/fabric-cli asset transfer claim --source-network=network1 --dest-network=network2 --user=bob --owner=alice --type=bond --pledge-id=<pledge-id> --param=bond01:a04
This should fail as the pledge has already expired.
15. Now get alice
in network1
to reclaim the asset as follows:
./bin/fabric-cli asset transfer reclaim --source-network=network1 --user=alice --type=bond --pledge-id=<pledge-id> --param=bond01:a04
- Verify that
alice
innetwork1
owns this asset as follows:
./bin/fabric-cli chaincode query --user=alice mychannel simpleassettransfer ReadAsset '["bond01","a04"]' --local-network=network1
- Verify that
bob
innetwork2
does not own this asset as follows:
./bin/fabric-cli chaincode query --user=bob mychannel simpleassettransfer ReadAsset '["bond01","a04"]' --local-network=network2
You should see an error message like Error: the asset a04 does not exist
.
The following sequence of operations will illustrate transfers and recoveries of token (fungible) assets.
- Verify that
alice
innetwork1
owns10000
tokens as follows:
./scripts/getTokenBalance.sh network1 alice
- Verify that
bob
innetwork2
owns no tokens as follows:
./scripts/getTokenBalance.sh network2 bob
You should see an error message like Error: owner does not have a wallet
.
20. Get alice
in network1
to pledge 50 tokens to bob
in network2
as follows (with a 1 hour timeout):
./bin/fabric-cli asset transfer pledge --source-network=network1 --dest-network=network2 --recipient=bob --expiry-secs=3600 --type=token --units=50 --owner=alice --data-file=src/data/tokensForTransfer.json
You should see a message containing the unique ID of this pledge on the console as Asset pledged with ID <pledge-id>
(<pledge-id>
is a hexadecimal string).
21. Get bob
in network2
to claim these tokens as follows (replace <pledge-id>
with the above hexadecimal value):
./bin/fabric-cli asset transfer claim --source-network=network1 --dest-network=network2 --user=bob --owner=alice --type=token --pledge-id=<pledge-id> --param=token1:50
- Verify that
alice
innetwork1
owns9950
tokens (after losing50
) as follows:
./scripts/getTokenBalance.sh network1 alice
- Verify that
bob
innetwork2
now owns50
tokens as follows:
./scripts/getTokenBalance.sh network2 bob
- Now get
alice
innetwork1
to pledge 100 tokens tobob
innetwork2
as follows (with a 1 minute timeout):
./bin/fabric-cli asset transfer pledge --source-network=network1 --dest-network=network2 --recipient=bob --expiry-secs=60 --type=token --units=100 --owner=alice --data-file=src/data/tokensForTransfer.json
Wait for a minute as follows:
sleep 60
You should see a message containing the unique ID of this pledge on the console as Asset pledged with ID <pledge-id>
(<pledge-id>
is a hexadecimal string).
25. Now get bob
in network2
to claim these tokens as follows (replace <pledge-id>
with the above hexadecimal value):
./bin/fabric-cli asset transfer claim --source-network=network1 --dest-network=network2 --user=bob --owner=alice --type=token --pledge-id=<pledge-id> --param=token1:100
This should fail as the pledge has already expired.
26. Now get alice
in network1
to reclaim these tokens as follows:
./bin/fabric-cli asset transfer reclaim --source-network=network1 --user=alice --type=token --pledge-id=<pledge-id> --param=token1:100
- Verify that
alice
innetwork1
still owns9950
tokens (after losing50
) as follows:
./scripts/getTokenBalance.sh network1 alice
- Verify that
bob
innetwork2
still owns only50
tokens as follows:
./scripts/getTokenBalance.sh network2 bob
- To subscribe for one or more events, generate a JSON file like: Sample Event Sub JSON, which contains an array of event subscription specifications. The samples provided here assume that the driver of
network1
is subscribing to an event ofnetwork2
. Then run the following command (and note the request id which will be used to unsubscribe or get status):./bin/fabric-cli event subscribe --network=network1 src/data/event_sub_sample.json
- To unsuscribe for one or more events, use the same JSON file as in subscription like: Sample Event Sub JSON. Then run the following command:
./bin/fabric-cli event unsubscribe --network=network1 --request-ids=<colon-separated-list-of-request-ids> src/data/event_sub_sample.json
- To get the status of an event subscription, run the following command:
./bin/fabric-cli event get-subscription-status --network=network1 --request-id=<request-id>
- To start the HTTP server to listen to the published events by the destination/importing relay (fetched from the source/exporting network), run the following command:
By default, it will start the below endpoint:./bin/fabric-cli event receive
http://localhost:8080/simple-event-callback
- To fetch the statuses of all the events received, run the following command:
./bin/fabric-cli event get-all --network=network1 --request-id=<request-id>
Due to how Fabric works and the CA works once a wallet has been created with identities in the CA you cannot create new wallet without fist revoking the original credentials. This can have some issues if you have deleted a wallet and are trying to recreate one.
MIT - see LICENSE
Details
- cacti-weaver-fabric-cli
- hyperledger
- about 1 year ago
- Apache-2.0
- 36 dependencies
Assets
- cacti-weaver-fabric-cli-2.0.0-dev.93.tgz
Download activity
- Total downloads 0
- Last 30 days 0
- Last week 0
- Today 0