Search in sources :

Example 1 with TopicVerifyReqProtocol

use of org.fisco.bcos.channel.protocol.TopicVerifyReqProtocol in project web3sdk by FISCO-BCOS.

the class Service method checkTopicVerify.

public void checkTopicVerify(ChannelHandlerContext ctx, TopicVerifyMessage message) throws IOException {
    SocketChannel socketChannel = (SocketChannel) ctx.channel();
    logger.info("get rand value request ChannelResponse seq:{} msgtype:{} address:{} port:{}", message.getSeq(), message.getType(), socketChannel.remoteAddress().getAddress().getHostAddress(), socketChannel.remoteAddress().getPort());
    logger.info("get rand value request :{} length:{}", Arrays.toString(message.getData()), message.getLength());
    sendResponse2Node(ctx, message);
    String content = new String(message.getData());
    logger.info("content:{} content:{}", content, Arrays.toString(content.getBytes()));
    NodeRequestSdkVerifyTopic nodeRequestSdkVerifyTopic = ObjectMapperFactory.getObjectMapper().readValue(content, NodeRequestSdkVerifyTopic.class);
    String topic = nodeRequestSdkVerifyTopic.getTopic();
    String topicForCert = nodeRequestSdkVerifyTopic.getTopicForCert();
    String nodeid = nodeRequestSdkVerifyTopic.getNodeId();
    logger.info("topic:{} topicForCert:{} nodeid:{}", topic, topicForCert, nodeid);
    ChannelRequest request = new ChannelRequest();
    request.setToTopic(topicForCert);
    request.setMessageID(newSeq());
    request.setTimeout(5000);
    request.setType((short) ChannelMessageType.AMOP_REQUEST.getType());
    String randValue = UUID.randomUUID().toString().replaceAll("-", "");
    TopicVerifyReqProtocol topicVerifyProtocol = new TopicVerifyReqProtocol();
    topicVerifyProtocol.setRandValue(randValue);
    topicVerifyProtocol.setTopic(topic);
    String jsonStr = ObjectMapperFactory.getObjectMapper().writeValueAsString(topicVerifyProtocol);
    logger.info("generate rand value jsonStr:{} topic:{} messageid:{}", jsonStr, request.getToTopic(), message.getSeq());
    byte[] bytes = topicVerify.getByteBuffByString(request.getToTopic(), jsonStr);
    request.setContent(bytes);
    asyncSendChannelMessage2(request, new ChannelResponseCallback2() {

        @Override
        public void onResponseMessage(ChannelResponse response) {
            logger.info("get response messageid:{}", response.getMessageID());
            try {
                checkSignForAmop(topic, String.valueOf(randValue), nodeid, ctx, response);
            } catch (IOException e) {
                logger.error("check sign for amop failed:{}", e);
            }
        }
    });
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) NodeRequestSdkVerifyTopic(org.fisco.bcos.channel.protocol.NodeRequestSdkVerifyTopic) ChannelRequest(org.fisco.bcos.channel.dto.ChannelRequest) TopicVerifyReqProtocol(org.fisco.bcos.channel.protocol.TopicVerifyReqProtocol) ChannelResponse(org.fisco.bcos.channel.dto.ChannelResponse) IOException(java.io.IOException)

Example 2 with TopicVerifyReqProtocol

use of org.fisco.bcos.channel.protocol.TopicVerifyReqProtocol in project web3sdk by FISCO-BCOS.

the class Service method signForAmop.

public void signForAmop(ChannelHandlerContext ctx, ChannelMessage2 message) throws IOException {
    SocketChannel socketChannel = (SocketChannel) ctx.channel();
    logger.info("sign ChannelResponse seq:{} msgtype:{} address:{} port:{}", message.getSeq(), message.getType(), socketChannel.remoteAddress().getAddress().getHostAddress(), socketChannel.remoteAddress().getPort());
    logger.info("sign request :{} length:{}", Arrays.toString(message.getData()), message.getLength());
    String content = topicVerify.parseDataFromPush(message.getLength(), message.getData());
    logger.info("content:{} content:{}", content, Arrays.toString(content.getBytes()));
    TopicVerifyReqProtocol topicVerifyProtocol = ObjectMapperFactory.getObjectMapper().readValue(content, TopicVerifyReqProtocol.class);
    String randValue = topicVerifyProtocol.getRandValue();
    String topic = topicVerifyProtocol.getTopic();
    logger.info("sign rand_value:{} sign topic:{}", randValue, topic);
    String signature = topicVerify.signatureForRandValue(topic, randValue);
    TopicVerifyRespProtocol topicVerifyRespProtocol = new TopicVerifyRespProtocol();
    topicVerifyRespProtocol.setSignature(signature);
    String jsonStr = ObjectMapperFactory.getObjectMapper().writeValueAsString(topicVerifyRespProtocol);
    logger.info("signature jsonStr result:{}", jsonStr);
    byte[] bytes = topicVerify.getByteBuffByString(message.getTopic(), jsonStr);
    ChannelResponse response = new ChannelResponse();
    response.setMessageID(message.getSeq());
    response.setErrorCode(0);
    response.setContent(bytes);
    sendResponseMessage2(response, ctx, message.getSeq(), message.getTopic());
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) TopicVerifyReqProtocol(org.fisco.bcos.channel.protocol.TopicVerifyReqProtocol) ChannelResponse(org.fisco.bcos.channel.dto.ChannelResponse) TopicVerifyRespProtocol(org.fisco.bcos.channel.protocol.TopicVerifyRespProtocol)

Aggregations

SocketChannel (io.netty.channel.socket.SocketChannel)2 ChannelResponse (org.fisco.bcos.channel.dto.ChannelResponse)2 TopicVerifyReqProtocol (org.fisco.bcos.channel.protocol.TopicVerifyReqProtocol)2 IOException (java.io.IOException)1 ChannelRequest (org.fisco.bcos.channel.dto.ChannelRequest)1 NodeRequestSdkVerifyTopic (org.fisco.bcos.channel.protocol.NodeRequestSdkVerifyTopic)1 TopicVerifyRespProtocol (org.fisco.bcos.channel.protocol.TopicVerifyRespProtocol)1