Are you looking for the Marbles app demo? That’s not here, head to the marbles example
ibm-blockchain-js
This is a Node.js library for easier interaction IBM Blockchain chaincode.
All documentation is on this page.
Table Of Contents:
- v1.0.0 Migration!
- IBC-js Function Documentation
- Chaincode Functions
- Object Formats
- Chaincode Summary File
- FAQ
Installation
npm install ibm-blockchain-js
Usage Steps!
(example code also provided below)
- Require this module
- Pass network + chaincode parameters to ibc.load(options, my_cb):
- Receive chaincode object from callback to ibc.load(). ie: my_cb(e, chaincode)
- You can now deploy your chaincode (if needed) with chaincode.deploy(func, args, null, cb)
- Use dot notation on chaincode to call any of your chaincode functions ie:
chaincode.query.read(['a'], cb);
chaincode.invoke.write(['a', 'test'], cb);
chaincode.invoke.remove(['a'], cb);
chaincode.invoke.init_marbles([ARGS], cb);
Example
var Ibc1 = require('ibm-blockchain-js');
var ibc = new Ibc1();
var chaincode = {};
var options = {
network:{
peers: [{
"api_host": "xxx.xxx.xxx.xxx",
"api_port": "xxxxx",
"api_port_tls": "xxx",
"id": "xxxxxx-xxxx-xxx-xxx-xxxxxxxxxxxx_vpx"
}],
users: [{
"enrollID": "user1",
"enrollSecret": "xxxxxxxx"
}],
options: {
quiet: true,
timeout: 60000
}
},
chaincode:{
zip_url: 'https://github.com/ibm-blockchain/marbles-chaincode/archive/master.zip',
unzip_dir: 'marbles-chaincode-master/part2_v1.0.0',
git_url: 'https://github.com/ibm-blockchain/marbles-chaincode/part2_v1.0.0'
}
};
ibc.load(options, cb_ready);
function cb_ready(err, cc){
app1.setup(ibc, cc);
app2.setup(ibc, cc);
if(cc.details.deployed_name === ""){
cc.deploy('init', ['99'], null, cb_deployed);
}
else{
console.log('chaincode summary file indicates chaincode has been previously deployed');
cb_deployed();
}
}
function cb_deployed(err){
console.log('sdk has deployed code and waited');
chaincode.query.read(['a']);
}
Migrating from v0.0.x to v1.x.x
The interface to your chaincode functions has changed in v1.0.0 from v0.0.13!
It is only a minor syntax change that should make it more clear to newcomers.
All invocation functions can now be found under chaincode.invoke
and all query functions can be found under chaincode.query
.
Examples:
query changes - name change
chaincode.read('a');
chaincode.query.read(['a']);
invoke changes - name change
chaincode.init_marble(args);
chaincode.remove(args);
chaincode.write(name, value);
chaincode.invoke.init_marble(args);
chaincode.invoke.remove(args);
chaincode.invoke.write(args);
deploy changes - added options parameter
chaincode.deploy('init', ['99'], './cc_summaries', cb_deployed);
chaincode.deploy('init', ['99'], {save_path: './cc_summaries', delay_ms: 60000}, cb_deployed);
register changes - added new parameter
ibc.register(i, enrollId, enrollSecret, [callback]);
ibc.register(i, enrollId, enrollSecret, maxRetry, [callback]);
IBM-Blockchain-JS Documentation
ibc.load(options, [callback])
This is a function that wraps a typical startup using a standard Bluemix IBM Blockchain network.
Take a look at how this function works, especially how it uses the register() function.
If this is not applicable for your network (ie you have a custom IBM Blockchain network) you can easily create your own version of ibc.load()
for your needs.
It will run in order:
- ibc.network(options.network.peers, options.network.options) check out other options in ibc.network()
- ibc.register(...)
- It will register the first peer with the first enrollID, the 2nd peer against the 2nd enrollID and so on.
- This function only runs if valid users are found in options.network.users. A valid user is one that contains 'type_1'.
- Any errors in register will stop execution and run callback(err).
- ibc.load_chaincode(options.chaincode, [callback])
- callback(err, cc)
Ex:
var options = {
network:{
peers: [{
"api_host": "xxx.xxx.xxx.xxx",
"api_port": "xxxxx",
"api_port_tls": "xxx",
"id": "xxxxxx-xxxx-xxx-xxx-xxxxxxxxxxxx_vpx"
}],
users: [{
"enrollID": "user1",
"enrollSecret": "xxxxxxxx"
}],
options: {
quiet: true,
timeout: 60000,
tls: false
}
},
chaincode:{
zip_url: 'https://github.com/ibm-blockchain/marbles-chaincode/archive/master.zip',
unzip_dir: 'marbles-chaincode-master/part2_v1.0.0',
git_url: 'https://github.com/ibm-blockchain/marbles-chaincode/part2_v1.0.0',
deployed_name: null
}
};
ibc.load(options, function(err, data){
});
ibc.load_chaincode(options, [callback])
Load the chaincode you want to use.
It will be downloaded and parsed.
The callback will receive (e, obj) where e
is the error format and obj
is the chaincode object.
"e" is null when there are no errors.
The chaincode object will have dot notation to the functions in the your chaincode.
Ex:
var options = {
zip_url: 'https://github.com/ibm-blockchain/marbles-chaincode/archive/master.zip',
unzip_dir: 'marbles-chaincode-master/part2_v1.0.0',
git_url: 'https://github.com/ibm-blockchain/marbles-chaincode/part2_v1.0.0',
deployed_name: null
};
ibc.load_chaincode(options, cb_ready);
ibc.network(arrayPeers, [options])
Set the information about the peers in the network.
This should be an array of peer objects.
The optional options parameter should be an object with the field quiet
and/or timeout
.
- quiet = boolean - when true will print out only minimal HTTP debug information. Defaults
true
. - timeout = integer - time in ms to wait for a http response. Defaults
60000
. - tls = boolean - when
false
will use HTTP instead of HTTPS. Defaults true
.
Ex:
var peers = [
{
"api_host": "xxx.xxx.xxx.xxx",
"api_port": "xxxxx",
"api_port_tls": "xxx",
"id": "xxxxxx-xxxx-xxx-xxx-xxxxxxxxxxxx_vpx"
}
]
ibc.network(peers, {quiet: false, timeout: 120000});
ibc.save(path [callback])
Save the Chaincode Summary File to a path.
Ex:
ibc.save('./');
ibc.clear([callback])
Clear any loaded chaincode files including the downloaded chaincode repo, and Chaincode Summary File.
Ex:
ibc.clear();
ibc.chain_stats([callback])
Get statistics on the network's chain.
Ex:
ibc.chain_stats(function(e, stats){
console.log('got some stats', stats);
});
Example Chain Stats:
{
"height": 10,
"currentBlockHash": "n7uMlNMiOSUM8s02cslTRzZQQlVfm8wKT9FtL54o0ywy6BkvPMwSzN5R1tpquvqOwFFHyLSoW44n6rkFyvAsBw==",
"previousBlockHash": "OESGPzacJO2Xc+5PB2zpmYVM8XlrwnEky0L2Ghok9oK1Lr/DWoxuBo2WwBca5zzJGq0fOeRQ7aOHgCjMupfL+Q=="
}
ibc.block_stats(id, [callback])
Get statistics on a particular block in the chain.
Ex:
ibc.block_stats(function(e, stats){
console.log('got some stats', stats);
});
Example Response:
{
"transactions": [
{
"type": 3,
"chaincodeID": "EoABNWUzNGJmNWI1MWM1MWZiYzhlMWFmOThkYThhZDg0MGM2OWFjOWM5YTg4ODVlM2U0ZDBlNjNiM2I4MDc0ZWU2NjY2OWFjOTAzNTg4MzE1YTZjOGQ4ODY4M2Y1NjM0MThlMzMwNzQ3ZmVhZmU3ZWYyMGExY2Q1NGZmNzY4NWRhMTk=",
"payload": "CrABCAESgwESgAE1ZTM0YmY1YjUxYzUxZmJjOGUxYWY5OGRhOGFkODQwYzY5YWM5YzlhODg4NWUzZTRkMGU2M2IzYjgwNzRlZTY2NjY5YWM5MDM1ODgzMTVhNmM4ZDg4NjgzZjU2MzQxOGUzMzA3NDdmZWFmZTdlZjIwYTFjZDU0ZmY3Njg1ZGExORomCgtpbml0X21hcmJsZRIHcng2YXRzcBIFZ3JlZW4SAjM1EgNCb2I=",
"uuid": "b3da1d08-19b8-4d8c-a116-b46defb07a7c",
"timestamp": {
"seconds": 1453997627,
"nanos": 856894462
}
}
],
"stateHash": "81ci8IAOeDh0ZwFM6hE/b3SfXt4tnZFemib7sI95cOsNcYMmtRxBWRBA7qnjPOCGU6snBRsFVnAliZXUigQ03w==",
"previousBlockHash": "tpjUh4sgbaUQFO8wm8S8nrm7yCrBa4rphIiujfaYAlEVfzI8IZ0mjYMf+GiOZ6CZRNWPmf+5bekmGIfr0H6zdw==",
"nonHashData": {
"localLedgerCommitTimestamp": {
"seconds": 1453997627,
"nanos": 868868790
}
}
}
ibc.switchPeer(peerIndex)
The SDK will default to use peer[0]. This function will switch the default peer to another index.
Ex:
ibc.switchPeer(2);
ibc.register(peerIndex, enrollID, enrollsecret, maxRetry, [callback])
Only applicable on a network with security enabled.
register()
will register against peer[peerIndex] with the provided credentials.
If successful, the peer will now use this enrollID
to perform any http requests.
- peerIndex = integer - position of peer in peers array (the one you fed ibc.networks()) you want to register against.
- enrollID = string - name of secure context user.
- enrollSecret = string - password/secret/api key of secure context user.
- maxRetry = integer - number of times to retry this call before giving up.
Ex:
ibc.register(3, 'user1', 'xxxxxx', 3, my_cb);
ibc.monitor_blockheight(callback)
This will call your callback function whenever the block height has changed.
ie. whenever a new block has been written to the chain.
It will also pass you the same response as in chain_stats()
.
Ex:
ibc.monitor_blockheight(my_callback);
function my_callback(e, chainstats){
console.log('got a new block!', chainstats);
}
ibc.get_transaction(udid, [callback])
Get information about a particular transaction ID.
Ex:
ibc.get_transaction('d30a1445-185f-4853-b4d6-ee7b4dfa5534', function(err, data){
console.log('found trans', err, data);
});
##Chaincode Functions
- Chaincode functions are dependent on actually be found inside your Go chaincode
- My advice is to build your chaincode off of the Marble Application one. This way you get the basic CRUD functions below:
chaincode.deploy(func, args, [options], [enrollID], [callback])
Deploy the chaincode.
Call GoLang function named 'func' and feed it 'args'.
Usually "args" is an array of strings.
The enrollID
parameter should be the desired secure context enrollID that has already been registered against the selected peer.
If left null
the SDK will use a known enrollID for the selected peer. (this is only relevant in a permissioned network)
Options:
- save_path = save the Chaincode Summary File to 'save_path'.
- delay_ms = time in milliseconds to postpone the callback after deploy. Default is
40000
Ex:
chaincode.deploy('init', ['99'], {delay_ms: 60000}, cb_deployed);
chaincode.query.CUSTOM_FUNCTION_NAME(args, [enrollID], [callback])
Will invoke your Go function CUSTOM_FUNCTION_NAME and pass it args
.
Usually args
is an array of strings.
The enrollID
parameter should be the desired secure context enrollID that has already been registered against the selected peer.
If left null
the SDK will use a known enrollID for the selected peer. (this is only relevant in a permissioned network)
Ex:
chaincode.query.read(['abc'], function(err, data){
console.log('read abc:', data, err);
});
chaincode.invoke.CUSTOM_FUNCTION_NAME(args, [enrollID], [callback])
Will query your Go function CUSTOM_FUNCTION_NAME and pass it args
.
Usually args
is an array of strings.
The enrollID
parameter should be the desired secure context enrollID that has already been registered against the selected peer.
If left null
the SDK will use a known enrollID for the selected peer. (this is only relevant in a permissioned network)
Ex:
chaincode.invoke.init_marbles([args], function(err, data){
console.log('create marble response:', data, err);
});
chaincode.query.read(name, [enrollID], [callback]) depreciated 4/1/2016
This function is only here to help people transition from ibc v0.0.x to v1.x.x.
You should create your own read() function in your chaincode which will overwrite this prebuilt one.
This function will put the name
argument into args[0]
and set function
to query
.
These are passed to the chaincode function Query(stub *shim.ChaincodeStub, function string, args []string)
.
Read variable named name from chaincode state.
This will call the Query()
function in the Go chaincode.
The enrollID
parameter should be the desired secure context enrollID that has already been registered against the selected peer.
If left null
the SDK will use a known enrollID for the selected peer. (this is only relevant in a permissioned network)
##Formats
Chaincode Object
This is the main guy.
It is returned in the callback to load_chaincode() and contains all your cc functions + some of the setup/input data.
chaincode =
{
query: {
CUSTOM_FUNCTION_NAME1: function(args, cb){etc...};
CUSTOM_FUNCTION_NAME2: function(args, cb){etc...};
^^ etc...
}
invoke: {
CUSTOM_FUNCTION_NAME1: function(args, cb){etc...};
CUSTOM_FUNCTION_NAME2: function(args, cb){etc...};
^^ etc...
}
deploy: function(func, args, path, cb),
details:{
deployed_name: '',
func: {
invoke: [],
query: []
},
git_url: '',
peers: [],
timestamp: 0,
users: [],
unzip_dir: '',
zip_url: '',
}
};
Error Format
{
name: "input error",
code: 400,
details: {msg: "did not provide git_url"}
};
Chaincode Summary File
This file is used internally.
It is created in ibc.load_chaincode() and updated with chaincode.deploy().
A copy can be saved elsewhere with ibc.save(path).
I found it handy in niche cases, but it will probably be unhelpful to most developers.
{
"details": {
"deployed_name": "f6c084c42b3bde90c03f214ac6e0426e3e594807901fb1464287f2c3a18ade717bc495298958287594f81bb0d0cfdd3b4346d438d3b587d4fc73cf78ae8f7dfe",
"func": {
"invoke": ["init", "delete", "write", "init_marble", "set_user", "open_trade", "perform_trade"],
},
{
"query": []
},
"git_url": 'https://github.com/ibm-blockchain/marbles-chaincode/part2_v1.0.0'
"peers": [{
"name": "vp1-xxx.xxx.xxx.xxx",
"api_host": "xxx.xxx.xxx.xxx",
"api_port": "xxx",
"id": "xxxxx_vp1",
"tls": false,
"enrollID": "user1"
}],
"timestamp": 1459779181971,
"users": [{
"enrollId": "user_type1_xxx",
"enrollSecret": "xxx"
}],
"unzip_dir": 'marbles-chaincode-master/part2_v1.0.0',
"zip_url": 'https://github.com/ibm-blockchain/marbles-chaincode/archive/master.zip',
"options": {}
}
}
#FAQ
- ibc.load() appears to ignore all of my users for secure context. Then it complains it found "No membership users" and never registers with a Peer!
Correct behavior of ibc.load()
is to remove any enrollIDs that do not contain 'type_1' in their name.
This is to conform to the OBC Peer spec of what enrollIDs a dev's app should use.
If this is not applicable for your network (ie you have a custom IBM Blockchain network) you can easily create your own version of ibc.load()
for your needs.
I would copy the code found in ibc.load()
then modify it to fit your own needs.
Everything important that ibc.load()
calls is exposed in this module.
- Do you have any examples that use this?
Yes! Head over to the Marbles Node.js Demo