hwelltech block chain cpp sdk
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed uniqueCPUID
Fork of bcsdk by
bcsdk 是 block chain sdk 的简写。它是由江苏恒为信息科技有限公司开发的 BlockChain 的 基于 mbed os 的 C++ 版本SDK,它能帮助开发者快速的在支持 mbed os 的芯片上开发 BlockChain 的应用。 bcsdk 的demo文件中包含了BlockChain中的 Key、Account、Asset、Transaction等方面的示例。其中: (1) Key 加密私钥是区块链上的主要授权机制。他们控制资产单位的发行和转让。 资产或帐户将定义发行或转移所需的单个密钥。 在 Key_test.cpp 中,我们实现了: 创建HSM密钥 键入密钥别名的名称(例如'gold','silver','bronze'),密钥别名是用于区分密钥的标签。
(2) Asset 资产是一种可以在区块链上发布的值类型。资产的所有单位均可互换,可以在各方之间直接交易,无需发行人参与。 在 Asset_test.cpp 中,我们实现了: 创建资产 键入资产别名的名称(例如'gold','silver','bronze'),资产别名是用于区分资产的标签。 选择“Key”键以使用现有的HSM密键,此密钥将用于此帐户中资产单位的发放和转移。
(3) Account 加帐户是恒为区块链核心平台中的一个对象,通过创建和跟踪控制程序来跟踪区块链上的资产的所有权。创建帐户时,您提供一个或多个“root”密钥和仲裁。 在 Account_test.cpp 中,我们实现了: 创建帐户 1 输入帐户别名的名称(例如'alice','bob'),帐户别名是用于区分帐户的标签。 2 键入名称以生成新的HSM密钥(例如'alice key','bob key'),此密钥将用于此帐户中资产单位的发放和转移。
(4) Transaction 交易包含一个或多个输入,以及一个或多个输出。恒为区块链核心平台的API允许您使用操作(包括发出,支出和返还)构建交易。 在 Transaction_test.cpp 中,我们实现了: 资产的交易 1 添加“账户支出”操作 2 为资产别名选择“Asset” 3 输入“100”作为金额 4 添加“使用帐户控制”操作 5 为帐户别名选择“Account” 6 为资产别名选择“Asset” 7 输入“100”作为金额 8 “提交”
Client.cpp@10:aabd720e632c, 2017-11-03 (annotated)
- Committer:
- webmaster
- Date:
- Fri Nov 03 01:07:32 2017 +0000
- Revision:
- 10:aabd720e632c
- Parent:
- 8:f2a567ee3a46
publish v0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
webmaster | 8:f2a567ee3a46 | 1 | #include "Client.h" |
webmaster | 8:f2a567ee3a46 | 2 | //#include <WINSOCK2.H> |
webmaster | 8:f2a567ee3a46 | 3 | #include "mbed.h" |
webmaster | 8:f2a567ee3a46 | 4 | #include "EthernetInterface.h" |
webmaster | 8:f2a567ee3a46 | 5 | //#include "BlockChain.h" |
webmaster | 8:f2a567ee3a46 | 6 | |
webmaster | 8:f2a567ee3a46 | 7 | |
webmaster | 8:f2a567ee3a46 | 8 | Client::Client() |
webmaster | 8:f2a567ee3a46 | 9 | { |
webmaster | 8:f2a567ee3a46 | 10 | m_strIP = "101.201.199.150"; |
webmaster | 8:f2a567ee3a46 | 11 | m_sPort = 80; |
webmaster | 8:f2a567ee3a46 | 12 | m_url = "/info"; |
webmaster | 8:f2a567ee3a46 | 13 | m_host = "testbc.hwelltech.com"; |
webmaster | 8:f2a567ee3a46 | 14 | m_token = "962cd39d-6496-4b23-a2c5-85e445069a78"; |
webmaster | 8:f2a567ee3a46 | 15 | } |
webmaster | 8:f2a567ee3a46 | 16 | |
webmaster | 8:f2a567ee3a46 | 17 | Client::Client(string ip,short port) |
webmaster | 8:f2a567ee3a46 | 18 | { |
webmaster | 8:f2a567ee3a46 | 19 | this->m_strIP = ip; |
webmaster | 8:f2a567ee3a46 | 20 | this->m_sPort = port; |
webmaster | 8:f2a567ee3a46 | 21 | |
webmaster | 8:f2a567ee3a46 | 22 | m_url = "/info"; |
webmaster | 8:f2a567ee3a46 | 23 | m_host = "testbc.hwelltech.com"; |
webmaster | 8:f2a567ee3a46 | 24 | m_token = "962cd39d-6496-4b23-a2c5-85e445069a78"; |
webmaster | 8:f2a567ee3a46 | 25 | } |
webmaster | 8:f2a567ee3a46 | 26 | |
webmaster | 8:f2a567ee3a46 | 27 | Client::~Client() |
webmaster | 8:f2a567ee3a46 | 28 | { |
webmaster | 8:f2a567ee3a46 | 29 | |
webmaster | 8:f2a567ee3a46 | 30 | } |
webmaster | 8:f2a567ee3a46 | 31 | |
webmaster | 8:f2a567ee3a46 | 32 | bool Client::build_post_raw(const string & url,const string & content, string & post_raw) |
webmaster | 8:f2a567ee3a46 | 33 | { |
webmaster | 8:f2a567ee3a46 | 34 | return build_post_raw(url,m_host,m_token,content,post_raw); |
webmaster | 8:f2a567ee3a46 | 35 | } |
webmaster | 8:f2a567ee3a46 | 36 | |
webmaster | 8:f2a567ee3a46 | 37 | bool Client::build_post_raw(const string & url, const string & host, const string & token, const string & content, string & post_raw) |
webmaster | 8:f2a567ee3a46 | 38 | { |
webmaster | 8:f2a567ee3a46 | 39 | bool ret = true; |
webmaster | 8:f2a567ee3a46 | 40 | |
webmaster | 8:f2a567ee3a46 | 41 | post_raw = ""; |
webmaster | 8:f2a567ee3a46 | 42 | post_raw.append("POST "+ url + " HTTP/1.1\r\n"); |
webmaster | 8:f2a567ee3a46 | 43 | post_raw.append("Host: " + host + "\r\n"); |
webmaster | 8:f2a567ee3a46 | 44 | post_raw.append("Connection: keep-alive\r\n"); |
webmaster | 8:f2a567ee3a46 | 45 | post_raw.append("accept: application/json\r\n"); |
webmaster | 8:f2a567ee3a46 | 46 | post_raw.append("content-type: application/json\r\n"); |
webmaster | 8:f2a567ee3a46 | 47 | string authorization = "Basic YmxvY2tnZW5lcmF0b3I6MTM3NzQzMDJlNTcyYmU4MWMxZmRmZjg2NGZiODA2Yjc2NjcxMzg5NzMwZjBjMDYwZDNlODExNTQ4OGRjNjQ2Mg=="; |
webmaster | 8:f2a567ee3a46 | 48 | //if(!token.empty()) |
webmaster | 8:f2a567ee3a46 | 49 | // authorization = Base64Encode(token) |
webmaster | 8:f2a567ee3a46 | 50 | post_raw.append("authorization: " + authorization + "\r\n"); |
webmaster | 8:f2a567ee3a46 | 51 | |
webmaster | 8:f2a567ee3a46 | 52 | char pLen[16] = {0}; |
webmaster | 8:f2a567ee3a46 | 53 | //sprintf_s(pLen, sizeof(pLen), "%d", content.length()); |
webmaster | 8:f2a567ee3a46 | 54 | sprintf(pLen, "%d", content.length()); |
webmaster | 8:f2a567ee3a46 | 55 | string strLen = pLen; |
webmaster | 8:f2a567ee3a46 | 56 | |
webmaster | 8:f2a567ee3a46 | 57 | post_raw.append("Content-Length: " + strLen + "\r\n"); |
webmaster | 8:f2a567ee3a46 | 58 | post_raw.append("\r\n"); |
webmaster | 8:f2a567ee3a46 | 59 | post_raw.append(content); |
webmaster | 8:f2a567ee3a46 | 60 | post_raw.append("\r\n\r\n"); |
webmaster | 8:f2a567ee3a46 | 61 | |
webmaster | 8:f2a567ee3a46 | 62 | return ret; |
webmaster | 8:f2a567ee3a46 | 63 | } |
webmaster | 8:f2a567ee3a46 | 64 | |
webmaster | 10:aabd720e632c | 65 | string Client::get_content(const string & strRep) |
webmaster | 10:aabd720e632c | 66 | { |
webmaster | 10:aabd720e632c | 67 | string content = ""; |
webmaster | 10:aabd720e632c | 68 | content = strRep.substr(strRep.find("\r\n\r\n") + sizeof("\r\n\r\n") - 1); //sizeof("\r\n\r\n") 5 |
webmaster | 10:aabd720e632c | 69 | if (content.at(0) != '{' && content.at(0) != '[') |
webmaster | 10:aabd720e632c | 70 | { |
webmaster | 10:aabd720e632c | 71 | content = content.substr(content.find("\r\n") + sizeof("\r\n") - 1); |
webmaster | 10:aabd720e632c | 72 | } |
webmaster | 10:aabd720e632c | 73 | return content; |
webmaster | 10:aabd720e632c | 74 | } |
webmaster | 10:aabd720e632c | 75 | |
webmaster | 10:aabd720e632c | 76 | bool Client::unbuild_post_raw(const string & strRep, int & content_length, string & url,string & content) |
webmaster | 10:aabd720e632c | 77 | { |
webmaster | 10:aabd720e632c | 78 | bool ret = true; |
webmaster | 10:aabd720e632c | 79 | |
webmaster | 10:aabd720e632c | 80 | content_length = get_content_length(strRep); |
webmaster | 10:aabd720e632c | 81 | content = strRep.substr(strRep.find("\r\n\r\n")); |
webmaster | 10:aabd720e632c | 82 | |
webmaster | 10:aabd720e632c | 83 | if (strRep.find("200 OK") < 0) |
webmaster | 10:aabd720e632c | 84 | { |
webmaster | 10:aabd720e632c | 85 | ret = false; |
webmaster | 10:aabd720e632c | 86 | } |
webmaster | 10:aabd720e632c | 87 | |
webmaster | 10:aabd720e632c | 88 | return ret; |
webmaster | 10:aabd720e632c | 89 | } |
webmaster | 10:aabd720e632c | 90 | |
webmaster | 10:aabd720e632c | 91 | int Client::get_content_length(const string & strRaw) |
webmaster | 10:aabd720e632c | 92 | { |
webmaster | 10:aabd720e632c | 93 | int len = -1; // 表示没找到 |
webmaster | 10:aabd720e632c | 94 | /*/ |
webmaster | 10:aabd720e632c | 95 | string strRaw = "\ |
webmaster | 10:aabd720e632c | 96 | HTTP/1.1 200 OK\r\n\ |
webmaster | 10:aabd720e632c | 97 | Blockchain-Id: 13804cbf90e019cc0bcc7243828298492f5d0d87423f776181069eb9af9b2071\r\n\ |
webmaster | 10:aabd720e632c | 98 | Chain-Request-Id: f90965648d6aeb6075e3\r\n\ |
webmaster | 10:aabd720e632c | 99 | Content-Length: 145\r\n\ |
webmaster | 10:aabd720e632c | 100 | X-Frame-Options: DENY\r\n\ |
webmaster | 10:aabd720e632c | 101 | \r\n\r\n\ |
webmaster | 10:aabd720e632c | 102 | {\"alias\":\"t_key_1\",\"xpub\":\"1c926b1e7a88496e5bd7e29736f9ca85c5386bb995c95412b1084ddc1ab5f80e49e7aea51f08b36b3773baa6277c5902f2034d36315b32bf96a3082c048225d5\"}\ |
webmaster | 10:aabd720e632c | 103 | \n"; |
webmaster | 10:aabd720e632c | 104 | */ |
webmaster | 10:aabd720e632c | 105 | string str = ""; |
webmaster | 10:aabd720e632c | 106 | int pos = strRaw.find("Content-Length:") + sizeof("Content-Length:"); |
webmaster | 10:aabd720e632c | 107 | str = strRaw.substr(pos); |
webmaster | 10:aabd720e632c | 108 | pos = str.find("\r\n"); |
webmaster | 10:aabd720e632c | 109 | str = str.substr(0,pos); |
webmaster | 10:aabd720e632c | 110 | len = atoi(str.c_str()); |
webmaster | 10:aabd720e632c | 111 | |
webmaster | 10:aabd720e632c | 112 | return len; |
webmaster | 10:aabd720e632c | 113 | } |
webmaster | 8:f2a567ee3a46 | 114 | int Client::http_post(const string & url,const string & content,string & strOut) |
webmaster | 8:f2a567ee3a46 | 115 | { |
webmaster | 8:f2a567ee3a46 | 116 | string post_raw = ""; |
webmaster | 8:f2a567ee3a46 | 117 | build_post_raw(url,content,post_raw); |
webmaster | 8:f2a567ee3a46 | 118 | return http_post_raw(post_raw,strOut); |
webmaster | 8:f2a567ee3a46 | 119 | } |
webmaster | 8:f2a567ee3a46 | 120 | |
webmaster | 8:f2a567ee3a46 | 121 | int Client::http_post_raw(const string &strIn,string &strOut) |
webmaster | 8:f2a567ee3a46 | 122 | { |
webmaster | 8:f2a567ee3a46 | 123 | string strReq; |
webmaster | 8:f2a567ee3a46 | 124 | string strRep; |
webmaster | 8:f2a567ee3a46 | 125 | strReq = strIn; |
webmaster | 8:f2a567ee3a46 | 126 | |
webmaster | 8:f2a567ee3a46 | 127 | //SOCKET sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
webmaster | 8:f2a567ee3a46 | 128 | TCPSocketConnection sClient; |
webmaster | 8:f2a567ee3a46 | 129 | /* |
webmaster | 8:f2a567ee3a46 | 130 | if(sClient == INVALID_SOCKET) |
webmaster | 8:f2a567ee3a46 | 131 | { |
webmaster | 8:f2a567ee3a46 | 132 | printf("invalid socket!"); |
webmaster | 8:f2a567ee3a46 | 133 | return 0; |
webmaster | 8:f2a567ee3a46 | 134 | } |
webmaster | 8:f2a567ee3a46 | 135 | */ |
webmaster | 8:f2a567ee3a46 | 136 | #ifdef WIN32 |
webmaster | 8:f2a567ee3a46 | 137 | // windows下是这样设置超时时间 |
webmaster | 8:f2a567ee3a46 | 138 | int recvTimeout = 3 * 1000; //30s |
webmaster | 8:f2a567ee3a46 | 139 | int sendTimeout = 3 * 1000; //30s |
webmaster | 8:f2a567ee3a46 | 140 | setsockopt(sClient, SOL_SOCKET, SO_RCVTIMEO, (char *)&recvTimeout ,sizeof(int)); |
webmaster | 8:f2a567ee3a46 | 141 | setsockopt(sClient, SOL_SOCKET, SO_SNDTIMEO, (char *)&sendTimeout ,sizeof(int)); |
webmaster | 8:f2a567ee3a46 | 142 | #elif __LINUX__ |
webmaster | 8:f2a567ee3a46 | 143 | // linux下是这样设置超时时间 |
webmaster | 8:f2a567ee3a46 | 144 | struct timeval timeout = {3,0}; |
webmaster | 8:f2a567ee3a46 | 145 | setsockopt(sClient, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,sizeof(struct timeval)); |
webmaster | 8:f2a567ee3a46 | 146 | setsockopt(sClient, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,sizeof(struct timeval)); |
webmaster | 8:f2a567ee3a46 | 147 | #else |
webmaster | 8:f2a567ee3a46 | 148 | unsigned int timeout = 1500; |
webmaster | 8:f2a567ee3a46 | 149 | sClient.set_blocking(false, timeout); // Timeout after (1.5)s |
webmaster | 8:f2a567ee3a46 | 150 | #endif |
webmaster | 8:f2a567ee3a46 | 151 | |
webmaster | 8:f2a567ee3a46 | 152 | |
webmaster | 8:f2a567ee3a46 | 153 | /* |
webmaster | 8:f2a567ee3a46 | 154 | sockaddr_in serAddr; |
webmaster | 8:f2a567ee3a46 | 155 | serAddr.sin_family = AF_INET; |
webmaster | 8:f2a567ee3a46 | 156 | serAddr.sin_port = htons(m_sPort); |
webmaster | 8:f2a567ee3a46 | 157 | serAddr.sin_addr.S_un.S_addr = inet_addr(m_strIP.c_str()); |
webmaster | 8:f2a567ee3a46 | 158 | if(connect(sClient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR) |
webmaster | 8:f2a567ee3a46 | 159 | { |
webmaster | 8:f2a567ee3a46 | 160 | printf("connect error !"); |
webmaster | 8:f2a567ee3a46 | 161 | closesocket(sClient); |
webmaster | 8:f2a567ee3a46 | 162 | return SOCKET_ERROR; |
webmaster | 8:f2a567ee3a46 | 163 | } |
webmaster | 8:f2a567ee3a46 | 164 | */ |
webmaster | 8:f2a567ee3a46 | 165 | while (sClient.connect(m_strIP.c_str(), m_sPort) < 0) { |
webmaster | 8:f2a567ee3a46 | 166 | printf("Unable to connect to (%s) on port (%d)\n", m_strIP.c_str(), m_sPort); |
webmaster | 8:f2a567ee3a46 | 167 | wait(1); |
webmaster | 8:f2a567ee3a46 | 168 | } |
webmaster | 8:f2a567ee3a46 | 169 | printf("Connected to Server at %s\n", m_strIP.c_str()); |
webmaster | 8:f2a567ee3a46 | 170 | |
webmaster | 8:f2a567ee3a46 | 171 | int ret = 0; |
webmaster | 8:f2a567ee3a46 | 172 | |
webmaster | 8:f2a567ee3a46 | 173 | /* |
webmaster | 8:f2a567ee3a46 | 174 | int left = strReq.length(); |
webmaster | 8:f2a567ee3a46 | 175 | unsigned int pos = 0; |
webmaster | 8:f2a567ee3a46 | 176 | ret = send(sClient, strReq.c_str(), strReq.length(), 0); |
webmaster | 8:f2a567ee3a46 | 177 | while (ret < left ) |
webmaster | 8:f2a567ee3a46 | 178 | { |
webmaster | 8:f2a567ee3a46 | 179 | pos = pos + ret; |
webmaster | 8:f2a567ee3a46 | 180 | ret = send(sClient, strReq.c_str() + pos, strReq.length() - pos, 0); |
webmaster | 8:f2a567ee3a46 | 181 | ret = sClient.send(strReq.c_str() + pos, strReq.length() - pos); |
webmaster | 8:f2a567ee3a46 | 182 | if (ret < 0) |
webmaster | 8:f2a567ee3a46 | 183 | { |
webmaster | 8:f2a567ee3a46 | 184 | return SOCKET_ERROR; |
webmaster | 8:f2a567ee3a46 | 185 | } |
webmaster | 8:f2a567ee3a46 | 186 | } |
webmaster | 8:f2a567ee3a46 | 187 | */ |
webmaster | 8:f2a567ee3a46 | 188 | ret = sClient.send_all((char *)strReq.c_str(), strReq.length()); |
webmaster | 8:f2a567ee3a46 | 189 | if (ret < 0) |
webmaster | 8:f2a567ee3a46 | 190 | { |
webmaster | 8:f2a567ee3a46 | 191 | //get last error |
webmaster | 8:f2a567ee3a46 | 192 | //get errno |
webmaster | 8:f2a567ee3a46 | 193 | return ret; |
webmaster | 8:f2a567ee3a46 | 194 | } |
webmaster | 10:aabd720e632c | 195 | #define DEBUG_LOG 1 |
webmaster | 8:f2a567ee3a46 | 196 | #ifdef DEBUG_LOG |
webmaster | 10:aabd720e632c | 197 | //cout<< ">>>>>>>>>>HTTP Request (Length = "<< strReq.length() << "):\n" << strReq << endl; |
webmaster | 10:aabd720e632c | 198 | printf(">>>>>>>>>>HTTP Request (Length = %d):\r\n%s\r\n", strReq.length(), strReq.c_str()); |
webmaster | 8:f2a567ee3a46 | 199 | #endif |
webmaster | 8:f2a567ee3a46 | 200 | |
webmaster | 8:f2a567ee3a46 | 201 | |
webmaster | 8:f2a567ee3a46 | 202 | char recData[BUFSIZE+1] = {0}; |
webmaster | 8:f2a567ee3a46 | 203 | |
webmaster | 8:f2a567ee3a46 | 204 | ret = 0; |
webmaster | 8:f2a567ee3a46 | 205 | //ret = recv(sClient, recData, BUFSIZE, 0); |
webmaster | 8:f2a567ee3a46 | 206 | ret = sClient.receive(recData, BUFSIZE); |
webmaster | 8:f2a567ee3a46 | 207 | strRep.append(recData); |
webmaster | 8:f2a567ee3a46 | 208 | |
webmaster | 8:f2a567ee3a46 | 209 | // 第一次收数据可能没有收完全,所以一直收到没有数据为止,这里会堵塞 |
webmaster | 8:f2a567ee3a46 | 210 | while (ret > 0) |
webmaster | 8:f2a567ee3a46 | 211 | { |
webmaster | 8:f2a567ee3a46 | 212 | memset(recData,0,BUFSIZE); |
webmaster | 8:f2a567ee3a46 | 213 | //ret = recv(sClient, recData, BUFSIZE, 0); |
webmaster | 8:f2a567ee3a46 | 214 | ret = sClient.receive(recData, BUFSIZE); |
webmaster | 8:f2a567ee3a46 | 215 | |
webmaster | 8:f2a567ee3a46 | 216 | if (ret > 0) |
webmaster | 8:f2a567ee3a46 | 217 | { |
webmaster | 8:f2a567ee3a46 | 218 | strRep.append(recData); |
webmaster | 8:f2a567ee3a46 | 219 | } |
webmaster | 8:f2a567ee3a46 | 220 | } |
webmaster | 8:f2a567ee3a46 | 221 | |
webmaster | 8:f2a567ee3a46 | 222 | strOut = strRep; |
webmaster | 8:f2a567ee3a46 | 223 | |
webmaster | 8:f2a567ee3a46 | 224 | #ifdef DEBUG_LOG |
webmaster | 10:aabd720e632c | 225 | //cout<< ">>>>>>>>>>HTTP Response (Length = "<< strRep.length() << "):\n" << strRep << endl; |
webmaster | 10:aabd720e632c | 226 | printf(">>>>>>>>>>HTTP Response (Length = %d):\r\n%s\r\n", strRep.length(), strRep.c_str()); |
webmaster | 8:f2a567ee3a46 | 227 | #endif |
webmaster | 8:f2a567ee3a46 | 228 | |
webmaster | 8:f2a567ee3a46 | 229 | // Clean up |
webmaster | 8:f2a567ee3a46 | 230 | //closesocket(sClient); |
webmaster | 8:f2a567ee3a46 | 231 | sClient.close(); |
webmaster | 8:f2a567ee3a46 | 232 | return 0; |
webmaster | 8:f2a567ee3a46 | 233 | } |