blockchain , sdchain cpp sdk and demo

Dependencies:   EthernetInterface mbed-rtos mbed uniqueCPUID

Fork of bcsdk by SDchain C Plus Plus Team

Committer:
webmaster
Date:
Wed Aug 08 09:51:32 2018 +0800
Revision:
18:ec6cd7cfe3f8
Parent:
8:f2a567ee3a46
rm demo files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
webmaster 8:f2a567ee3a46 1 #pragma once
webmaster 8:f2a567ee3a46 2 #include <string>
webmaster 8:f2a567ee3a46 3 //#include "Uuid.h"
webmaster 8:f2a567ee3a46 4 #include "mbed.h"
webmaster 8:f2a567ee3a46 5 #include "EthernetInterface.h"
webmaster 8:f2a567ee3a46 6
webmaster 8:f2a567ee3a46 7 using namespace std;
webmaster 8:f2a567ee3a46 8
webmaster 8:f2a567ee3a46 9 #define IPSTR "101.201.199.150"
webmaster 8:f2a567ee3a46 10 #define PORT 80
webmaster 8:f2a567ee3a46 11 #define BUFSIZE 1024*20
webmaster 8:f2a567ee3a46 12
webmaster 8:f2a567ee3a46 13 struct http_post_params
webmaster 8:f2a567ee3a46 14 {
webmaster 8:f2a567ee3a46 15 string cmd;
webmaster 8:f2a567ee3a46 16 string url;
webmaster 8:f2a567ee3a46 17 string host;
webmaster 8:f2a567ee3a46 18 string token;
webmaster 8:f2a567ee3a46 19 string content;
webmaster 8:f2a567ee3a46 20 };
webmaster 8:f2a567ee3a46 21
webmaster 8:f2a567ee3a46 22 struct http_post_params bc_params[] =
webmaster 8:f2a567ee3a46 23 {
webmaster 8:f2a567ee3a46 24 {"info", "/info", "testbc.hwelltech.com", "", "{}"},
webmaster 8:f2a567ee3a46 25 {"create-key", "/mockhsm/create-key", "testbc.hwelltech.com", "", "{\"alias\":\"mbed_key_a1\",\"client_token\":\"a37b1fe0-d952-4e1e-a09f-d1fe2c9dc282\"}"},
webmaster 8:f2a567ee3a46 26 {"create-account", "/create-account", "testbc.hwelltech.com", "", "[{\"alias\":\"mbed_account_a1\",\"tags\":{},\"quorum\":1,\"root_xpubs\":[\"62b33131fff6493ce37cf86396984902932baaf08eb5260cc17a4138f0403e4f261a0569c75aeddfd8f09548b891b360a8e72a514c082826920d3f87e325ea9b\"],\"client_token\":\"641dc851-4bce-45d8-bedb-96766abff223\"}]"},
webmaster 8:f2a567ee3a46 27 {"create-asset", "/create-asset", "testbc.hwelltech.com", "", "[{\"alias\":\"mbed_asset\",\"tags\":{},\"definition\":{},\"quorum\":1,\"root_xpubs\":[\"62b33131fff6493ce37cf86396984902932baaf08eb5260cc17a4138f0403e4f261a0569c75aeddfd8f09548b891b360a8e72a514c082826920d3f87e325ea9b\"],\"client_token\":\"de4bf755-1278-400d-9a16-bdca5ffaed0f\"}]"},
webmaster 8:f2a567ee3a46 28 {"list-assets", "/list-assets", "testbc.hwelltech.com", "", "{\"filter\":\"id='bd3b443fc05c2d1d6be7c27d6a34c98070efa178fc53d2ddcd444542ef5b7a8b'\"}"},
webmaster 8:f2a567ee3a46 29 {"build-transaction", "/build-transaction", "testbc.hwelltech.com", "", "[{\"actions\":[{\"account_alias\":\"t_acc_1\",\"asset_alias\":\"t_asset\",\"amount\":123,\"reference_data\":{},\"type\":\"spend_account\"},{\"account_alias\":\"t_acc_2\",\"asset_alias\":\"t_asset\",\"amount\":123,\"reference_data\":{},\"type\":\"control_account\"}]}]"},
webmaster 8:f2a567ee3a46 30 {"sign-transaction", "/sign-transaction", "testbc.hwelltech.com", "", "{\"transactions\":[{\"raw_transaction\":\"07010700fae2ffd5e52b000101710187249e9a82f75589f67b6d3166454b8cca8882d7f74c456f4b8b14278f84f4ee4fa592bcb4e3aaaf2f6a7460ce61b708ea9289af5519be46d1481b83c1f237a110c445012b766baa2029072ca3b5f2b3f77d28cc9cb44dbbfe54a03d9a9a95fb60aea517f296adb47d5151ad696c00c0027b7d010002014fa592bcb4e3aaaf2f6a7460ce61b708ea9289af5519be46d1481b83c1f237a110c944012b766baa20754817056c29955a9787b7db7fe98b135d1adecc7986b4c957e57fd2b6da86225151ad696c00c00000014ea592bcb4e3aaaf2f6a7460ce61b708ea9289af5519be46d1481b83c1f237a1107b012b766baa20e10131cfd7c51d3f43c933fe1e7be4ff75860e6bcf3d1a495332305df95b067e5151ad696c00c0027b7d0000\",\"signing_instructions\":[{\"position\":0,\"asset_id\":\"a592bcb4e3aaaf2f6a7460ce61b708ea9289af5519be46d1481b83c1f237a110\",\"amount\":8900,\"witness_components\":[{\"type\":\"signature\",\"quorum\":1,\"keys\":[{\"xpub\":\"eae173767ebfdf7ef6ee10ee4edcd9e809492b97b8a5f3135754924d54641d53337d717aff01468e9561d7b54c04f042f23aee956ec5135654cf111d1cb3fb9d\",\"derivation_path\":[\"014200000000000000\",\"6eea000000000000\"]}],\"signatures\":null}]}],\"local\":true,\"allow_additional_actions\":false}],\"xpubs\":[\"eae173767ebfdf7ef6ee10ee4edcd9e809492b97b8a5f3135754924d54641d53337d717aff01468e9561d7b54c04f042f23aee956ec5135654cf111d1cb3fb9d\"]}"},
webmaster 8:f2a567ee3a46 31 {"submit-transaction", "/submit-transaction", "testbc.hwelltech.com", "", "{\"transactions\":[{\"raw_transaction\":\"07010700fae2ffd5e52b000101710187249e9a82f75589f67b6d3166454b8cca8882d7f74c456f4b8b14278f84f4ee4fa592bcb4e3aaaf2f6a7460ce61b708ea9289af5519be46d1481b83c1f237a110c445012b766baa2029072ca3b5f2b3f77d28cc9cb44dbbfe54a03d9a9a95fb60aea517f296adb47d5151ad696c00c0027b7d670300407c1aa8c9e4795a3c7380cc569371d667a0002521f8c1305352463809e1aca463a07474c5f1a4075fbbb14fde77306cf1a7fa448f748aa02c70143d7d1452770a2320e52e1d2df43edd81ef65fd92430a390e42fa05b731aaf87eb5fecd8a74555b57ae8702014fa592bcb4e3aaaf2f6a7460ce61b708ea9289af5519be46d1481b83c1f237a110c944012b766baa20754817056c29955a9787b7db7fe98b135d1adecc7986b4c957e57fd2b6da86225151ad696c00c00000014ea592bcb4e3aaaf2f6a7460ce61b708ea9289af5519be46d1481b83c1f237a1107b012b766baa20e10131cfd7c51d3f43c933fe1e7be4ff75860e6bcf3d1a495332305df95b067e5151ad696c00c0027b7d0000\",\"signing_instructions\":[{\"position\":0,\"asset_id\":\"a592bcb4e3aaaf2f6a7460ce61b708ea9289af5519be46d1481b83c1f237a110\",\"amount\":8900,\"witness_components\":[{\"type\":\"signature\",\"quorum\":1,\"keys\":[{\"xpub\":\"eae173767ebfdf7ef6ee10ee4edcd9e809492b97b8a5f3135754924d54641d53337d717aff01468e9561d7b54c04f042f23aee956ec5135654cf111d1cb3fb9d\",\"derivation_path\":[\"014200000000000000\",\"6eea000000000000\"]}],\"signatures\":[\"7c1aa8c9e4795a3c7380cc569371d667a0002521f8c1305352463809e1aca463a07474c5f1a4075fbbb14fde77306cf1a7fa448f748aa02c70143d7d1452770a\"]}]}],\"local\":true,\"allow_additional_actions\":false}]}"},
webmaster 8:f2a567ee3a46 32 {"list-transactions", "/list-transactions", "testbc.hwelltech.com", "", "{\"filter\":\"id='7e5f97a9295370f19e73ccbf3ae8eb66ee01a5551eb073b310aedbde360c0628'\"}"}
webmaster 8:f2a567ee3a46 33 };
webmaster 8:f2a567ee3a46 34
webmaster 8:f2a567ee3a46 35
webmaster 8:f2a567ee3a46 36 bool build_post_raw(const string & url, const string & host, const string & token, const string & content, string & post_raw)
webmaster 8:f2a567ee3a46 37 {
webmaster 8:f2a567ee3a46 38 bool ret = true;
webmaster 8:f2a567ee3a46 39
webmaster 8:f2a567ee3a46 40 post_raw = "";
webmaster 8:f2a567ee3a46 41 post_raw.append("POST "+ url + " HTTP/1.1\n");
webmaster 8:f2a567ee3a46 42 post_raw.append("Host: testbc.hwelltech.com " + host + "\n");
webmaster 8:f2a567ee3a46 43 post_raw.append("Connection: keep-alive\n");
webmaster 8:f2a567ee3a46 44 post_raw.append("accept: application/json\n");
webmaster 8:f2a567ee3a46 45 post_raw.append("content-type: application/json\n");
webmaster 8:f2a567ee3a46 46 string authorization = "Basic YmxvY2tnZW5lcmF0b3I6MTM3NzQzMDJlNTcyYmU4MWMxZmRmZjg2NGZiODA2Yjc2NjcxMzg5NzMwZjBjMDYwZDNlODExNTQ4OGRjNjQ2Mg==";
webmaster 8:f2a567ee3a46 47 //if(!token.empty())
webmaster 8:f2a567ee3a46 48 // authorization = Base64Encode(token)
webmaster 8:f2a567ee3a46 49 post_raw.append("authorization: " + authorization + "\n");
webmaster 8:f2a567ee3a46 50
webmaster 8:f2a567ee3a46 51 char pLen[16] = {0};
webmaster 8:f2a567ee3a46 52 //sprintf_s(pLen, sizeof(pLen), "%d", content.length());
webmaster 8:f2a567ee3a46 53 sprintf(pLen, "%d", content.length());
webmaster 8:f2a567ee3a46 54 string strLen = pLen;
webmaster 8:f2a567ee3a46 55
webmaster 8:f2a567ee3a46 56 post_raw.append("Content-Length: " + strLen + "\n");
webmaster 8:f2a567ee3a46 57 post_raw.append("\n\n");
webmaster 8:f2a567ee3a46 58 post_raw.append(content);
webmaster 8:f2a567ee3a46 59 post_raw.append("\r\n\r\n");
webmaster 8:f2a567ee3a46 60
webmaster 8:f2a567ee3a46 61 return ret;
webmaster 8:f2a567ee3a46 62 }
webmaster 8:f2a567ee3a46 63
webmaster 8:f2a567ee3a46 64 void test_bc_2()
webmaster 8:f2a567ee3a46 65 {
webmaster 8:f2a567ee3a46 66 string strReq = "";
webmaster 8:f2a567ee3a46 67 //build_post_raw();
webmaster 8:f2a567ee3a46 68 }
webmaster 8:f2a567ee3a46 69
webmaster 8:f2a567ee3a46 70
webmaster 8:f2a567ee3a46 71 int http_post_bc(const string &strIn,string &strOut)
webmaster 8:f2a567ee3a46 72 {
webmaster 8:f2a567ee3a46 73 string strReq;
webmaster 8:f2a567ee3a46 74 string strRep;
webmaster 8:f2a567ee3a46 75 strReq = strIn;
webmaster 8:f2a567ee3a46 76
webmaster 8:f2a567ee3a46 77 /*
webmaster 8:f2a567ee3a46 78 SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
webmaster 8:f2a567ee3a46 79 if(sclient == INVALID_SOCKET)
webmaster 8:f2a567ee3a46 80 {
webmaster 8:f2a567ee3a46 81 printf("invalid socket!");
webmaster 8:f2a567ee3a46 82 return 0;
webmaster 8:f2a567ee3a46 83 }
webmaster 8:f2a567ee3a46 84 */
webmaster 8:f2a567ee3a46 85 TCPSocketConnection sClient;
webmaster 8:f2a567ee3a46 86 /*
webmaster 8:f2a567ee3a46 87 sockaddr_in serAddr;
webmaster 8:f2a567ee3a46 88 serAddr.sin_family = AF_INET;
webmaster 8:f2a567ee3a46 89 serAddr.sin_port = htons(80);
webmaster 8:f2a567ee3a46 90 serAddr.sin_addr.S_un.S_addr = inet_addr("101.201.199.150");
webmaster 8:f2a567ee3a46 91 if(connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
webmaster 8:f2a567ee3a46 92 {
webmaster 8:f2a567ee3a46 93 //����ʧ��
webmaster 8:f2a567ee3a46 94 printf("connect error !");
webmaster 8:f2a567ee3a46 95 closesocket(sclient);
webmaster 8:f2a567ee3a46 96 return 0;
webmaster 8:f2a567ee3a46 97 }
webmaster 8:f2a567ee3a46 98 */
webmaster 8:f2a567ee3a46 99 while (sClient.connect(IPSTR, PORT) < 0) {
webmaster 8:f2a567ee3a46 100 printf("Unable to connect to (%s) on port (%d)\n", IPSTR, PORT);
webmaster 8:f2a567ee3a46 101 wait(1);
webmaster 8:f2a567ee3a46 102 }
webmaster 8:f2a567ee3a46 103 printf("Connected to Server at %s\n", IPSTR);
webmaster 8:f2a567ee3a46 104
webmaster 8:f2a567ee3a46 105 int ret = 0;
webmaster 8:f2a567ee3a46 106 //send(sclient, strReq.c_str(), strReq.length(), 0);
webmaster 8:f2a567ee3a46 107 ret = sClient.send_all((char *)strReq.c_str(), strReq.length());
webmaster 8:f2a567ee3a46 108 if (ret < 0)
webmaster 8:f2a567ee3a46 109 {
webmaster 8:f2a567ee3a46 110 //get last error
webmaster 8:f2a567ee3a46 111 //get errno
webmaster 8:f2a567ee3a46 112 return ret;
webmaster 8:f2a567ee3a46 113 }
webmaster 8:f2a567ee3a46 114
webmaster 8:f2a567ee3a46 115 #ifdef DEBUG_LOG
webmaster 8:f2a567ee3a46 116 cout<< ">>>>>>>>>>HTTP Request (Length = "<< strReq.length() << "):\n" << strReq << endl;
webmaster 8:f2a567ee3a46 117 #endif
webmaster 8:f2a567ee3a46 118
webmaster 8:f2a567ee3a46 119 char recData[BUFSIZE+1] = {0};
webmaster 8:f2a567ee3a46 120
webmaster 8:f2a567ee3a46 121 ret = 0;
webmaster 8:f2a567ee3a46 122 //ret = recv(sClient, recData, BUFSIZE, 0);
webmaster 8:f2a567ee3a46 123 ret = sClient.receive(recData, BUFSIZE);
webmaster 8:f2a567ee3a46 124 strRep.append(recData);
webmaster 8:f2a567ee3a46 125
webmaster 8:f2a567ee3a46 126 // 第一次收数据可能没有收完全,所以一直收到没有数据为止,这里会堵塞
webmaster 8:f2a567ee3a46 127 while (ret > 0)
webmaster 8:f2a567ee3a46 128 {
webmaster 8:f2a567ee3a46 129 memset(recData,0,BUFSIZE);
webmaster 8:f2a567ee3a46 130 //ret = recv(sClient, recData, BUFSIZE, 0);
webmaster 8:f2a567ee3a46 131 ret = sClient.receive(recData, BUFSIZE);
webmaster 8:f2a567ee3a46 132
webmaster 8:f2a567ee3a46 133 if (ret > 0)
webmaster 8:f2a567ee3a46 134 {
webmaster 8:f2a567ee3a46 135 strRep.append(recData);
webmaster 8:f2a567ee3a46 136 }
webmaster 8:f2a567ee3a46 137 }
webmaster 8:f2a567ee3a46 138
webmaster 8:f2a567ee3a46 139 strOut = strRep;
webmaster 8:f2a567ee3a46 140
webmaster 8:f2a567ee3a46 141 #ifdef DEBUG_LOG
webmaster 8:f2a567ee3a46 142 cout<< ">>>>>>>>>>HTTP Response (Length = "<< strRep.length() << "):\n" << strRep << endl;
webmaster 8:f2a567ee3a46 143 #endif
webmaster 8:f2a567ee3a46 144
webmaster 8:f2a567ee3a46 145 // Clean up
webmaster 8:f2a567ee3a46 146 //closesocket(sClient);
webmaster 8:f2a567ee3a46 147 sClient.close();
webmaster 8:f2a567ee3a46 148 return 0;
webmaster 8:f2a567ee3a46 149 }
webmaster 8:f2a567ee3a46 150
webmaster 8:f2a567ee3a46 151 /*
webmaster 8:f2a567ee3a46 152 void test_uuid()
webmaster 8:f2a567ee3a46 153 {
webmaster 8:f2a567ee3a46 154 GUID guid = CreateGuid();
webmaster 8:f2a567ee3a46 155 string strUuid = GuidToString(guid);
webmaster 8:f2a567ee3a46 156 cout<< "uuid = " << strUuid.c_str() << endl;
webmaster 8:f2a567ee3a46 157
webmaster 8:f2a567ee3a46 158 }
webmaster 8:f2a567ee3a46 159 */