Hyperledger Fabric SDK 示例fabric-samples-《balance-transfer》之二《创建Channel》

来源:互联网 发布:sql联合主键 编辑:程序博客网 时间:2024/05/20 20:55

前言

这里面有两个重要的参数:

  • channel名称
  • channel配置文件路径(../artifacts/channel/mychannel.tx)

这个mychannel.tx是要事先生成好的文件。了解更多channel配置文件相关内容看这里。

路由

app.js

app.post('/channels', function(req, res) {    logger.info('<<<<<<<<<<<<<<<<< C R E A T E  C H A N N E L >>>>>>>>>>>>>>>>>');    logger.debug('End point : /channels');    // 1  校验参数    var channelName = req.body.channelName;    var channelConfigPath = req.body.channelConfigPath;    logger.debug('Channel name : ' + channelName);    logger.debug('channelConfigPath : ' + channelConfigPath); //../artifacts/channel/mychannel.tx    if (!channelName) {   // 参数一: channel名称        res.json(getErrorMessage('\'channelName\''));        return;    }    if (!channelConfigPath) { // 参数二: channel配置文件路径        res.json(getErrorMessage('\'channelConfigPath\''));        return;    }     // 2  创建通道    channels.createChannel(channelName, channelConfigPath, req.username, req.orgname)    .then(function(message) {        res.send(message);    });});

具体实现

create-channel.js

var createChannel = function(channelName, channelConfigPath, username, orgName) {    logger.debug('\n====== Creating Channel \'' + channelName + '\' ======\n');    var client = helper.getClientForOrg(orgName);    var channel = helper.getChannelForOrg(orgName);    //1 读取channel配置 read in the envelope for the channel config raw bytes    var envelope = fs.readFileSync(path.join(__dirname, channelConfigPath));    // 提取配置信息 extract the channel config bytes from the envelope to be signed    var channelConfig = client.extractChannelConfig(envelope);    //2 在admin环境下 来初始化这个channel  Acting as a client in the given organization provided with "orgName" param    return helper.getOrgAdmin(orgName).then((admin) => {        logger.debug(util.format('Successfully acquired admin user for the organization "%s"', orgName));        // sign the channel config bytes as "endorsement", this is required by        // the orderer's channel creation policy          //3 签名channel   order要求 要创建(更新)的channel 必须要admin签名        let signature = client.signChannelConfig(channelConfig);        let request = {            //  byte[]            config: channelConfig,            //  admin  签名后的数据            signatures: [signature],            name: channelName,            //  这个channel 中的order对象            orderer: channel.getOrderers()[0],            txId: client.newTransactionID()  // 交易id        };        //4 通过client创建channel   里面  send to orderer  通过order来广播事务        return client.createChannel(request);    }, (err) => {        logger.error('Failed to enroll user \''+username+'\'. Error: ' + err);        throw new Error('Failed to enroll user \''+username+'\'' + err);    }).then((response) => {        logger.debug(' response ::%j', response);        //5 处理响应结果        if (response && response.status === 'SUCCESS') {            logger.debug('Successfully created the channel.');            let response = {                success: true,                message: 'Channel \'' + channelName + '\' created Successfully'            };          return response;        } else {            logger.error('\n!!!!!!!!! Failed to create the channel \'' + channelName +                '\' !!!!!!!!!\n\n');            throw new Error('Failed to create the channel \'' + channelName + '\'');        }    }, (err) => {        logger.error('Failed to initialize the channel: ' + err.stack ? err.stack :            err);        throw new Error('Failed to initialize the channel: ' + err.stack ? err.stack : err);    });};

API访问

定义好两个参数channelName以及channelConfigPath ,输入shell命令:

echo "POST request Create channel  ..."echocurl -s -X POST \  http://localhost:4000/channels \  -H "authorization: Bearer $ORG1_TOKEN" \  -H "content-type: application/json" \  -d '{    "channelName":"mychannel",    "channelConfigPath":"../artifacts/channel/mychannel.tx"}'echo

$ORG1_TOKEN就是注册用户时返回的token。运行结果:

POST request Create channel  ...{"success":true,"message":"Channel 'mychannel' created Successfully"}

node服务后台打印:

[2017-10-16 11:07:06.446] [DEBUG] SampleWebApp - Decoded from JWT token: username - Jim, orgname - org1[2017-10-16 11:07:06.446] [INFO] SampleWebApp - <<<<<<<<<<<<<<<<< C R E A T E  C H A N N E L >>>>>>>>>>>>>>>>>[2017-10-16 11:07:06.446] [DEBUG] SampleWebApp - End point : /channels[2017-10-16 11:07:06.446] [DEBUG] SampleWebApp - Channel name : mychannel[2017-10-16 11:07:06.446] [DEBUG] SampleWebApp - channelConfigPath : ../artifacts/channel/mychannel.tx[2017-10-16 11:07:06.447] [DEBUG] Create-Channel - ====== Creating Channel 'mychannel' ======[2017-10-16 11:07:06.450] [DEBUG] Helper - [crypto_ecdsa_aes]: constructor, keySize: 256[2017-10-16 11:07:06.451] [DEBUG] Helper - [crypto_ecdsa_aes]: Hash algorithm: SHA2, hash output size: 256[2017-10-16 11:07:06.459] [DEBUG] Helper - [utils.CryptoKeyStore]: CryptoKeyStore, constructor - start[2017-10-16 11:07:06.459] [DEBUG] Helper - [utils.CryptoKeyStore]: constructor, no super class specified, using config: fabric-client/lib/impl/FileKeyValueStore.js[2017-10-16 11:07:06.459] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor[2017-10-16 11:07:06.460] [DEBUG] Helper - Msp ID : Org1MSP[2017-10-16 11:07:06.460] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - start[2017-10-16 11:07:06.462] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - have the key [Circular][2017-10-16 11:07:06.462] [DEBUG] Helper - [utils.CryptoKeyStore]: This class requires a CryptoKeyStore to save keys, using the store: {"opts":{"path":"/tmp/fabric-client-kvs_peerOrg1"}}[2017-10-16 11:07:06.462] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore.js - constructor[2017-10-16 11:07:06.463] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore returning ks[2017-10-16 11:07:06.463] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 0a66f3503a3322b2ca8e9dce03322d486ee6ec5970efaad3f153bc13aa2d942c[2017-10-16 11:07:06.463] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: 312ad05190fa592fb9e8ac2dcdb23c84df7f49e4ee295c2aa1200a50c9146c46[2017-10-16 11:07:06.463] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue[2017-10-16 11:07:06.464] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - start[2017-10-16 11:07:06.464] [DEBUG] Helper - [crypto_ecdsa_aes]: importKey - have the key [Circular][2017-10-16 11:07:06.465] [DEBUG] Helper - [utils.CryptoKeyStore]: _getKeyStore resolving store[2017-10-16 11:07:06.465] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 0a66f3503a3322b2ca8e9dce03322d486ee6ec5970efaad3f153bc13aa2d942c[2017-10-16 11:07:06.465] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: 312ad05190fa592fb9e8ac2dcdb23c84df7f49e4ee295c2aa1200a50c9146c46[2017-10-16 11:07:06.465] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue[2017-10-16 11:07:06.467] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 0a66f3503a3322b2ca8e9dce03322d486ee6ec5970efaad3f153bc13aa2d942c[2017-10-16 11:07:06.468] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: 312ad05190fa592fb9e8ac2dcdb23c84df7f49e4ee295c2aa1200a50c9146c46[2017-10-16 11:07:06.468] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 0a66f3503a3322b2ca8e9dce03322d486ee6ec5970efaad3f153bc13aa2d942c[2017-10-16 11:07:06.469] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: 312ad05190fa592fb9e8ac2dcdb23c84df7f49e4ee295c2aa1200a50c9146c46[2017-10-16 11:07:06.469] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param X: 0a66f3503a3322b2ca8e9dce03322d486ee6ec5970efaad3f153bc13aa2d942c[2017-10-16 11:07:06.469] [DEBUG] Helper - [ecdsa/key.js]: ECDSA curve param Y: 312ad05190fa592fb9e8ac2dcdb23c84df7f49e4ee295c2aa1200a50c9146c46[2017-10-16 11:07:06.469] [DEBUG] Helper - [FileKeyValueStore.js]: FileKeyValueStore -- setValue[2017-10-16 11:07:06.470] [DEBUG] Create-Channel - Successfully acquired admin user for the organization "org1"[2017-10-16 11:07:06.481] [DEBUG] Helper - [crypto_ecdsa_aes]: ecdsa signature:  Signature {  r: <BN: 69008cc4396fed179cc6b9876d94a1c7e95ad3ed023ddbcece4a574788e88419>,  s: <BN: 56e1d31c5585f4d939dab9434a2fdb33c9e2afcf4eaac55200f07f9b8b72bfed>,  recoveryParam: 0 }[2017-10-16 11:07:06.490] [DEBUG] Helper - [crypto_ecdsa_aes]: ecdsa signature:  Signature {  r: <BN: 78af3c9f44a0ecb540b942600949b081480eec07e230b7ebaa0e61218e215807>,  s: <BN: 1ce9943f4853cd41add3f184a570dc3ea52335f8e23704fee3c804277fbfcbf9>,  recoveryParam: 0 }// 创建成功[2017-10-16 11:07:06.561] [DEBUG] Create-Channel -  response ::{"status":"SUCCESS"}[2017-10-16 11:07:06.561] [DEBUG] Create-Channel - Successfully created the channel.
阅读全文
0 0
原创粉丝点击