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 “提交”
main.cpp
- Committer:
- webmaster
- Date:
- 2017-09-27
- Revision:
- 9:d7468574ef8f
- Parent:
- 8:f2a567ee3a46
- Child:
- 10:aabd720e632c
File content as of revision 9:d7468574ef8f:
#include "mbed.h" #include "EthernetInterface.h" #include "SDFileSystem.h" #include <stdio.h> //#include <string.h> #include "uniqueCPUID.h" #include <iostream> #include <string> #include "UnitTest.h" //#include "BlockChain.h" #include "MockHsm.h" using namespace std; #define HTTPD_SERVER_PORT 80 #define HTTPD_MAX_REQ_LENGTH 1023 #define HTTPD_MAX_HDR_LENGTH 255 #define HTTPD_MAX_FNAME_LENGTH 127 #define HTTPD_MAX_DNAME_LENGTH 127 const char* ECHO_SERVER_ADDRESS = "192.168.31.185"; const int ECHO_SERVER_PORT = 10003; Serial uart(USBTX, USBRX); //SDFileSystem sd(p5, p6, p7, p8, "sd"); // LPC1768 MBD2PMD //SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // K64F EthernetInterface eth; TCPSocketServer server; TCPSocketConnection client; TCPSocketConnection my_client; char buffer[HTTPD_MAX_REQ_LENGTH+1]; char httpHeader[HTTPD_MAX_HDR_LENGTH+1]; char fileName[HTTPD_MAX_FNAME_LENGTH+1]; char dirName[HTTPD_MAX_DNAME_LENGTH+1]; char *uristr; char *eou; char *qrystr; FILE *fp; int rdCnt; // function declation int test_socket_client(void); int socket_init(); int socket_fini(); int test_MockHsm() { MockHsm mh; mh.setAlias("cpp_key"); //cout<<mh.getAlias()<<endl; //cout<<mh.createKey()<<endl; cout<< mh.listKeys() <<endl; return 0; } void test_uuid() { char pUUID[128] = {0}; getUniqueIDAsStr(pUUID); printf("CPUID = %s \n",pUUID); //printUniqueId(uart); } int main(void) { printf("main start\n"); test_uuid(); socket_init(); test_MockHsm(); test_socket_client(); socket_fini(); printf("main end\n"); return 0; } int socket_init() { int ret = 0; // EthernetInterface eth; printf("Initializing Ethernet\n"); ret = eth.init(); //Use DHCP //eth.init("192.168.31.186", "255.255.255.0", "192.168.31.1"); //Use static ip printf("Connecting\n"); ret = eth.connect(); printf("IP Address is %s\n", eth.getIPAddress()); return ret; } int socket_fini() { return eth.disconnect(); } int test_socket_client(void) { TCPSocketConnection socket; while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); wait(1); } printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS); // Send message to server char hello[] = "Hello World"; printf("Sending message to Server : '%s' \n",hello); socket.send_all(hello, sizeof(hello) - 1); // Receive message from server char buf[256]; int n = socket.receive(buf, 256); buf[n] = '\0'; printf("Received message from server: '%s'\n", buf); // Clean up socket.close(); return 0; } void get_file(char* uri) { uart.printf("get_file %s\n", uri); char *lstchr = strrchr(uri, NULL) -1; if ('/' == *lstchr) { uart.printf("Open directory /sd%s\n", uri); *lstchr = 0; sprintf(fileName, "/sd%s", uri); DIR *d = opendir(fileName); if (d != NULL) { sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); client.send(httpHeader,strlen(httpHeader)); sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri); client.send(httpHeader,strlen(httpHeader)); struct dirent *p; while((p = readdir(d)) != NULL) { sprintf(dirName, "%s/%s", fileName, p->d_name); uart.printf("%s\n", dirName); DIR *subDir = opendir(dirName); if (subDir != NULL) { sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name); } else { sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name); } client.send(httpHeader,strlen(httpHeader)); } } closedir(d); uart.printf("Directory closed\n"); sprintf(httpHeader,"</ul></body></html>"); client.send(httpHeader,strlen(httpHeader)); } else { sprintf(fileName, "/sd%s", uri); fp = fopen(fileName, "r"); if (fp == NULL) { uart.printf("File not found\n"); sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); client.send(httpHeader,strlen(httpHeader)); client.send(uri,strlen(uri)); } else { uart.printf("Sending: header"); sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); client.send(httpHeader,strlen(httpHeader)); uart.printf(" file"); while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) { client.send(buffer, rdCnt); uart.printf("."); } client.send(buffer, rdCnt); fclose(fp); uart.printf("done\n"); } } } int main_1 (void) { // Serial Interface eth; uart.baud(115200); uart.printf("Initializing\n"); // Check File System uart.printf("Checking File System\n"); DIR *d = opendir("/sd/"); if (d != NULL) { uart.printf("SD Card Present\n"); } else { uart.printf("SD Card Root Directory Not Found\n"); } // EthernetInterface eth; uart.printf("Initializing Ethernet\n"); //eth.init(); //Use DHCP eth.init("192.168.31.186", "255.255.255.0", "192.168.31.1"); //Use uart.printf("Connecting\n"); eth.connect(); uart.printf("IP Address is %s\n", eth.getIPAddress()); //test socket client //my_client.connect("192.168.31.185",10003); //my_client.send_all("hello world",11); TCPSocketConnection socket; while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { uart.printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); wait(1); } uart.printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS); // Send message to server char hello[] = "Hello World"; uart.printf("Sending message to Server : '%s' \n",hello); socket.send_all(hello, sizeof(hello) - 1); // Receive message from server char buf[256]; int n = socket.receive(buf, 256); buf[n] = '\0'; uart.printf("Received message from server: '%s'\n", buf); // Clean up socket.close(); //////////////////////////////////////////////////////////////////////////// // TCPSocketServer server; server.bind(HTTPD_SERVER_PORT); server.listen(); uart.printf("Server Listening\n"); while (true) { uart.printf("\nWait for new connection...\r\n"); server.accept(client); client.set_blocking(false, 1500); // Timeout after (1.5)s my_client.send_all("hello world",11); uart.printf("Connection from: %s\r\n", client.get_address()); while (true) { int n = client.receive(buffer, sizeof(buffer)); if (n <= 0) break; uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer); if (n >= 1024) { sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); client.send(httpHeader,strlen(httpHeader)); client.send(buffer,n); break; } else { buffer[n]=0; } if (!strncmp(buffer, "GET ", 4)) { uristr = buffer + 4; eou = strstr(uristr, " "); if (eou == NULL) { sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); client.send(httpHeader,strlen(httpHeader)); client.send(buffer,n); } else { *eou = 0; get_file(uristr); } } } client.close(); } } int main_2() { // Serial Interface eth; uart.baud(115200); uart.printf("Initializing\n"); EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); printf("\nClient IP Address is %s\n", eth.getIPAddress()); // Connect to Server TCPSocketConnection socket; while (socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT) < 0) { printf("Unable to connect to (%s) on port (%d)\n", ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); wait(1); } printf("Connected to Server at %s\n",ECHO_SERVER_ADDRESS); // Send message to server char hello[] = "Hello World"; printf("Sending message to Server : '%s' \n",hello); socket.send_all(hello, sizeof(hello) - 1); // Receive message from server char buf[256]; int n = socket.receive(buf, 256); buf[n] = '\0'; printf("Received message from server: '%s'\n", buf); // Clean up socket.close(); eth.disconnect(); while(true) {} }