Пример #1
0
        /// <summary>
        /// 节点对交易进行背书
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <EndorseResponse> Endorse(TxRequest request)
        {
            EndorseResponse response = new EndorseResponse();
            //背书
            var result = await _node.ChainCodeSubmit(request);

            if (result.StatusCode != StatusCode.Successful)
            {
                response.Status = false;
                response.Msg    = result.StatusCode.ToString() + ":" + result.Message;
                return(response);
            }
            response.TxReadWriteSet       = result.TxReadWriteSet;
            response.Status               = true;
            response.Msg                  = "背书完成";
            response.Endorsement.Identity = _configProviders.GetPublicIndentity();

            //var str = Newtonsoft.Json.JsonConvert.SerializeObject(response);
            var privatekey = _configProviders.GetPrivateKey();

            //节点对背书数据签名
            // response.Endorsement.Signature = DataHelper.RSASignature(str, privatekey);
            response.Endorsement.Signature = RSAHelper.SignData(privatekey, response);

            return(response);
        }
Пример #2
0
        /// <summary>
        /// 创建新通道 默认只加载本地的节点
        /// </summary>
        /// <param name="channelName"></param>
        public async Task <TxResponse> CreateNewChannel(string channelId)
        {
            //创建交易请求
            var txHeader = new TxHeader();

            txHeader.ChannelId     = channelId;
            txHeader.Type          = TxType.Invoke;
            txHeader.ChaincodeName = ConfigKey.SysNetConfigChaincode;
            txHeader.FuncName      = ConfigKey.InitChannelFunc;
            txHeader.Args          = new string[] { channelId };

            var tx = ModelHelper.ToTxRequest(txHeader);

            EndorseResponse response = new EndorseResponse();
            var             result   = ChainCodeSubmit(tx).Result;

            if (result.StatusCode != StatusCode.Successful)
            {
                return(new TxResponse()
                {
                    Status = false, Msg = result.Message
                });
            }
            response.TxReadWriteSet       = result.TxReadWriteSet;
            response.Status               = true;
            response.Msg                  = "背书完成";
            response.Endorsement.Identity = _configProvider.GetPublicIndentity();
            var str        = Newtonsoft.Json.JsonConvert.SerializeObject(response);
            var privatekey = _configProvider.GetPrivateKey();

            //节点对背书数据签名
            response.Endorsement.Signature = RSAHelper.SignData(privatekey, response);

            Envelope envelope = new Envelope();

            envelope.TxReqeust = tx;
            envelope.Endorsements.Add(response.Endorsement);
            envelope.PayloadReponse                = new ProposaResponsePayload();
            envelope.PayloadReponse.Message        = response.Msg;
            envelope.PayloadReponse.Status         = response.Status;
            envelope.PayloadReponse.TxReadWriteSet = response.TxReadWriteSet;

            var block = new Block();

            block.Header.ChannelId = channelId;
            block.Header.Number    = 0;
            block.Header.Term      = 0;
            block.Header.Timestamp = DateTime.Now.Ticks;
            block.Data.Envelopes.Add(envelope);
            block.Signer.Identity = _configProvider.GetPublicIndentity();
            //计算hash·
            block.Header.DataHash = RSAHelper.GenerateMD5(Newtonsoft.Json.JsonConvert.SerializeObject(block));
            //对整个区块进行签名
            block.Signer.Signature = RSAHelper.SignData(_configProvider.GetPrivateKey(), block);
            //上链
            await _dataManager.PutOnChainBlockAsync(block);

            return(new TxResponse()
            {
                Status = true
            });
        }