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 “提交”
Revision 10:aabd720e632c, committed 2017-11-03
- Comitter:
- webmaster
- Date:
- Fri Nov 03 01:07:32 2017 +0000
- Parent:
- 9:d7468574ef8f
- Commit message:
- publish v0.1
Changed in this revision
--- a/Account.cpp Wed Sep 27 01:18:12 2017 +0000 +++ b/Account.cpp Fri Nov 03 01:07:32 2017 +0000 @@ -1,31 +1,116 @@ -/*********************************************************************** - * Module: Account.h - * Author: Administrator - * Modified: 2017Äê8ÔÂ18ÈÕ 11:06:20 - * Purpose: Declaration of the class Account - * Comment: ÕË»§ - ***********************************************************************/ +#include "Account.h" +#include "Uuid.h" + + +Account::Account() +{ + this->id = ""; + this->alias = ""; + //this->tags = "{}"; + this->quorum = 1; +} + +Account::Account(Key &key) +{ + this->id = ""; + this->alias = ""; + //this->tags = "{}"; + this->quorum = 1; + this->m_Keys = key; +} + +Account::~Account() +{ + +} + -#if !defined(__Account_Account_h) -#define __Account_Account_h -#include <string> -#include <map> -#include "Key.h" -#include "Object.h" -using namespace std; +// ���� +// ע���˺�json�Ǹ�����[] +string Account::createAccount() +{ + string strJson = ""; + + MbedJSONValue val; + MbedJSONValue valNull; + MbedJSONValue vals; + + //fill the object + val["alias"] = this->alias; + val["tags"] = valNull; + val["quorum"] = this->getQuorum(); + for (int i=0; i<getQuorum(); i++) + { + // should be Key Get root_xpubs vector keys[i] + val["root_xpubs"][i] = "62b33131fff6493ce37cf86396984902932baaf08eb5260cc17a4138f0403e4f261a0569c75aeddfd8f09548b891b360a8e72a514c082826920d3f87e325ea9b"; + //val["root_xpubs"][i] = m_Keys.getRootXpub(); + } + Uuid u; + //GUID guid = u.CreateGuid(); + //string strUuid = u.GuidToString(guid); + string strUuid = u.getUuid(); + +// GUID guid = CreateGuid(); +// string strUuid = GuidToString(guid); + + val["client_token"] = strUuid;//"17e48179-3500-48db-9335-b69d9af1117f"; -class Account + //serialize it into a JSON string + vals[0] = val; + strJson = vals.serialize(); + if ("null" == strJson) + { + strJson = "{}"; + } + //printf("json: %s\r\n", strJson.c_str()); + + string strRep = ""; + string strUrl = "/create-account"; + client.http_post(strUrl,strJson,strRep); + + // get http response content + string content = client.get_content(strRep); + + // decode content json + MbedJSONValue valRep; + string err = parse(valRep,content.c_str()); + + int ret = 0; + ret = err.find("error"); + if ( ret > 1) + { + return err; + } + + id = valRep[0]["id"].get<std::string>(); + + return id; +} + +string Account::listAccounts() { -public: - std::string id; - std::string alias; - Key* keys; - int quorum; - map<std::string,Object> tags; + string strJson = ""; + + MbedJSONValue val; + + //fill the object + if (!this->id.empty()) + { + string filter = "id='" + this->id +"'"; + val["filter"] = filter; + } -protected: -private: + //serialize it into a JSON string + strJson = val.serialize(); + if ("null" == strJson) + { + strJson = "{}"; + } + //printf("json: %s\r\n", strJson.c_str()); -}; + string strRep = ""; + string strUrl = "/list-accounts"; + client.http_post(strUrl,strJson,strRep); -#endif + return strJson; +} \ No newline at end of file
--- a/Account.h Wed Sep 27 01:18:12 2017 +0000 +++ b/Account.h Fri Nov 03 01:07:32 2017 +0000 @@ -8,19 +8,43 @@ #if !defined(__Account_Account_h) #define __Account_Account_h +#include <string> +#include <map> +#include "Key.h" +#include "Object.h" +using namespace std; class Account { public: - std::string id; - std::string alias; - Key* keys; - int quorum; - Map<std::string,Object> tags; + Account(); + Account(Key &key); + ~Account(); + + std::string id; + std::string alias; + //Key* keys; + int quorum; + map<std::string,Object> tags; + + void setId(const string newId){this->id = newId;}; + string getId(){return this->id;}; + + void setAlias(const string newAlias){this->alias = newAlias;}; + string getAlias(){return this->alias;}; + + void setQuorum(int newQuorum){this->quorum = newQuorum;}; + int getQuorum(){return this->quorum;}; + + string createAccount(); + string listAccounts(); protected: private: + Client client; + MbedJSONValue m_valTags; + Key m_Keys; }; -#endif +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Asset.cpp Fri Nov 03 01:07:32 2017 +0000 @@ -0,0 +1,106 @@ +#include "Asset.h" +#include "Uuid.h" + +Asset::Asset() +{ + this->id = ""; + this->alias = ""; + this->tags = "{}"; + this->quorum = 1; + Object obj; + this->definition["{}"] = obj; +} + +Asset::~Asset() +{ + +} + +// �����ʲ� +// ע���ʲ�json�Ǹ�����[] +string Asset::createAsset() +{ + string strJson = ""; + + MbedJSONValue val; + MbedJSONValue valNull; + MbedJSONValue vals; + + //fill the object + val["alias"] = this->alias; + val["tags"] = valNull; + val["definition"] = valNull; + val["quorum"] = this->getQuorum(); + for (int i=0; i<getQuorum(); i++) + { + // should be Key Get root_xpubs vector keys[i] + val["root_xpubs"][i] = "62b33131fff6493ce37cf86396984902932baaf08eb5260cc17a4138f0403e4f261a0569c75aeddfd8f09548b891b360a8e72a514c082826920d3f87e325ea9b"; + //val["root_xpubs"][i] = m_Keys.getRootXpub(); + } + + Uuid u; + //GUID guid = u.CreateGuid(); + //string strUuid = u.GuidToString(guid); + string strUuid = u.getUuid(); + + val["client_token"] = strUuid;//"17e48179-3500-48db-9335-b69d9af1111f"; + + //serialize it into a JSON string + vals[0] = val; + strJson = vals.serialize(); + if ("null" == strJson) + { + strJson = "{}"; + } + //printf("json: %s\r\n", strJson.c_str()); + + string strRep = ""; + string strUrl = "/create-asset"; + client.http_post(strUrl,strJson,strRep); + + // get http response content + string content = client.get_content(strRep); + + // decode content json + MbedJSONValue valRep; + string err = parse(valRep,content.c_str()); + + int ret = 0; + ret = err.find("error"); + if ( ret > 1) + { + return err; + } + + id = valRep[0]["id"].get<std::string>(); + + return id; +} + +string Asset::listAssets() +{ + string strJson = ""; + + MbedJSONValue val; + + //fill the object + if (!this->id.empty()) + { + string filter = "id='" + this->id +"'"; + val["filter"] = filter; + } + + //serialize it into a JSON string + strJson = val.serialize(); + if ("null" == strJson) + { + strJson = "{}"; + } + //printf("json: %s\r\n", strJson.c_str()); + + string strRep = ""; + string strUrl = "/list-assets"; + client.http_post(strUrl,strJson,strRep); + + return strJson; +} \ No newline at end of file
--- a/Asset.h Wed Sep 27 01:18:12 2017 +0000 +++ b/Asset.h Fri Nov 03 01:07:32 2017 +0000 @@ -15,19 +15,48 @@ #if !defined(__Account_Asset_h) #define __Account_Asset_h +#include <string> +#include "Object.h" +#include "Key.h" +#include "Client.h" +#include "MbedJSONValue.h" +#include <vector> +#include <map> + +using namespace std; + class Asset { public: - std::string id; - std::string alias; - key[]* keys; - int quorum; - map<std::string,Object> definition; - std::string isLocal; + Asset(); + ~Asset(); + + std::string id; + std::string alias; + //Key[]* keys; + vector<Key> keys; + int quorum; + string tags; + map<std::string,Object> definition; + std::string isLocal; + + void setId(const string newId){this->id = newId;}; + string getId(){return this->id;}; + + void setAlias(const string newAlias){this->alias = newAlias;}; + string getAlias(){return this->alias;}; + + void setQuorum(int newQuorum){this->quorum = newQuorum;}; + int getQuorum(){return this->quorum;}; + + + string createAsset(); + string listAssets(); protected: private: + Client client; }; -#endif +#endif \ No newline at end of file
--- a/Client.cpp Wed Sep 27 01:18:12 2017 +0000 +++ b/Client.cpp Fri Nov 03 01:07:32 2017 +0000 @@ -62,6 +62,55 @@ return ret; } +string Client::get_content(const string & strRep) +{ + string content = ""; + content = strRep.substr(strRep.find("\r\n\r\n") + sizeof("\r\n\r\n") - 1); //sizeof("\r\n\r\n") 5 + if (content.at(0) != '{' && content.at(0) != '[') + { + content = content.substr(content.find("\r\n") + sizeof("\r\n") - 1); + } + return content; +} + +bool Client::unbuild_post_raw(const string & strRep, int & content_length, string & url,string & content) +{ + bool ret = true; + + content_length = get_content_length(strRep); + content = strRep.substr(strRep.find("\r\n\r\n")); + + if (strRep.find("200 OK") < 0) + { + ret = false; + } + + return ret; +} + +int Client::get_content_length(const string & strRaw) +{ + int len = -1; // 表示没æ¾å° +/*/ + string strRaw = "\ +HTTP/1.1 200 OK\r\n\ +Blockchain-Id: 13804cbf90e019cc0bcc7243828298492f5d0d87423f776181069eb9af9b2071\r\n\ +Chain-Request-Id: f90965648d6aeb6075e3\r\n\ +Content-Length: 145\r\n\ +X-Frame-Options: DENY\r\n\ +\r\n\r\n\ +{\"alias\":\"t_key_1\",\"xpub\":\"1c926b1e7a88496e5bd7e29736f9ca85c5386bb995c95412b1084ddc1ab5f80e49e7aea51f08b36b3773baa6277c5902f2034d36315b32bf96a3082c048225d5\"}\ +\n"; +*/ + string str = ""; + int pos = strRaw.find("Content-Length:") + sizeof("Content-Length:"); + str = strRaw.substr(pos); + pos = str.find("\r\n"); + str = str.substr(0,pos); + len = atoi(str.c_str()); + + return len; +} int Client::http_post(const string & url,const string & content,string & strOut) { string post_raw = ""; @@ -143,9 +192,10 @@ //get errno return ret; } - +#define DEBUG_LOG 1 #ifdef DEBUG_LOG - cout<< ">>>>>>>>>>HTTP Request (Length = "<< strReq.length() << "):\n" << strReq << endl; + //cout<< ">>>>>>>>>>HTTP Request (Length = "<< strReq.length() << "):\n" << strReq << endl; + printf(">>>>>>>>>>HTTP Request (Length = %d):\r\n%s\r\n", strReq.length(), strReq.c_str()); #endif @@ -172,7 +222,8 @@ strOut = strRep; #ifdef DEBUG_LOG - cout<< ">>>>>>>>>>HTTP Response (Length = "<< strRep.length() << "):\n" << strRep << endl; + //cout<< ">>>>>>>>>>HTTP Response (Length = "<< strRep.length() << "):\n" << strRep << endl; + printf(">>>>>>>>>>HTTP Response (Length = %d):\r\n%s\r\n", strRep.length(), strRep.c_str()); #endif // Clean up
--- a/Client.h Wed Sep 27 01:18:12 2017 +0000 +++ b/Client.h Fri Nov 03 01:07:32 2017 +0000 @@ -1,13 +1,13 @@ /*********************************************************************** * Module: Client.h * Author: Administrator - * Modified: 2017Äê9ÔÂ4ÈÕ 11:08:02 + * Modified: 2017��9��4�� 11:08:02 * Purpose: Declaration of the class Client - * Comment: Client¶ÔÏó°üº¬¶ÔÔ¶³Ì - * APIÖ´ÐÐHTTPÇëÇóËùÐèµÄËùÓÐ - * ÐÅÏ¢¡£ ͨ³££¬Ó¦ÓóÌÐò½«¾ßÓÐÏò - * Á´ºËÐÄ·¢³öÇëÇóµÄ¿Í»§¶ËºÍÏòHSM - * ·þÎñÆ÷·¢³öÇëÇóµÄµ¥¶À¿Í»§¶Ë¡£ + * Comment: Client���������Զ�� + * APIִ��HTTP������������� + * ��Ϣ�� ͨ����Ӧ�ó��ò½«¾ï¿½ï¿½ï¿½ï¿½ï¿½ + * �����ķ�������Ŀͻ��˺���HSM + * ��������������ĵ����ͻ��ˡ� ***********************************************************************/ #if !defined(__Account_Client_h) @@ -38,6 +38,10 @@ bool build_post_raw(const string & url, const string & host, const string & token, const string & content, string & post_raw); int http_post(const string & url,const string & content,string & strOut); int http_post_raw(const string &strIn,string &strOut); + + int get_content_length(const string & strRaw); + string get_content(const string & strRep); + bool unbuild_post_raw(const string & strRep, int & content_length, string & url,string & content); protected: private:
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Input.h Fri Nov 03 01:07:32 2017 +0000 @@ -0,0 +1,40 @@ +/*********************************************************************** + * Module: Input.h + * Author: Administrator + * Modified: 2017å¹´9æ8æ¥ 14:57:48 + * Purpose: Declaration of the class Input + * Comment: å个è¾å ¥å æ¬å¨äºå¡ä¸ã + ***********************************************************************/ + +#if !defined(__Account_Input_h) +#define __Account_Input_h + +#include <map> +#include <string> +#include "Object.h" +using namespace std; + +class Input +{ +public: + std::string type; + std::string assetId; + std::string assetAlias; + map<std::string,Object> assetDefinition; + map<std::string,Object> assetTags; + std::string assetIsLocal; + long amount; + std::string spentOutputId; + std::string accountId; + std::string accountAlias; + map<std::string,Object> accountTags; + std::string issuanceProgram; + map<std::string,Object> referenceData; + std::string isLocal; + +protected: +private: + +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Key.cpp Fri Nov 03 01:07:32 2017 +0000 @@ -0,0 +1,51 @@ +#include "Key.h" + +string Key::listKeys() +{ + string strJson = ""; + + MbedJSONValue val; + + //serialize it into a JSON string + strJson = val.serialize(); + if ("null" == strJson) + { + strJson = "{}"; + } + //printf("json: %s\r\n", strJson.c_str()); + + string strRep = ""; + string strUrl = "/mockhsm/list-keys"; + client.http_post(strUrl,strJson,strRep); + //cout<< "HTTP Response:\n" << strRep.length() << endl << strRep.c_str() << endl; + + ////////////////////////////////////////////////////////////////////////// + // get http response content + string content = client.get_content(strRep); + + // decode content json + MbedJSONValue valRep; + parse(valRep,content.c_str()); + + int items = valRep["items"].size(); + //valRep.hasMember(); + string key_alias = getAlias(); + string str; + for (int i = 0; i < items; i++) + { + string alias = ""; + if (valRep["items"][i]["alias"].serialize() != "null") + { + alias = valRep["items"][i]["alias"].get<std::string>(); + } + + + if (alias == key_alias) + { + string xpub = valRep["items"][i]["xpub"].get<std::string>(); + setRootXpub(xpub); + } + } + + return strJson; +} \ No newline at end of file
--- a/Key.h Wed Sep 27 01:18:12 2017 +0000 +++ b/Key.h Fri Nov 03 01:07:32 2017 +0000 @@ -1,14 +1,21 @@ /*********************************************************************** * Module: Key.h * Author: Administrator - * Modified: 2017Äê8ÔÂ31ÈÕ 15:14:02 + * Modified: 2017å¹´8æ31æ¥ 15:14:02 * Purpose: Declaration of the class Key - * Comment: ´æ´¢¹ØÓÚÓëÕÊ»§Ïà¹ØÁªµÄ¼üµÄÐÅÏ¢µÄÀà¡£ + * Comment: åå¨å ³äºä¸å¸æ·ç¸å ³èçé®çä¿¡æ¯çç±»ã ***********************************************************************/ #if !defined(__Account_Key_h) #define __Account_Key_h +#include <string> +#include "Object.h" +#include "Client.h" +#include "MbedJSONValue.h" + +using namespace std; + class Key { public: @@ -16,10 +23,24 @@ std::string accountXpub; std::string* accountDerivationPath; + // + void setRootXpub(const string &xpub){ this->rootXpub = xpub; }; + string getRootXpub(){ return this->rootXpub; }; + + void setAccountXpub(const string &xpub){ this->accountXpub = xpub; }; + string getAccountXpub(){ return this->accountXpub; }; + + void setAlias(const string &alias){ this->alias = alias; }; + string getAlias(){ return this->alias; }; + + string listKeys(); + protected: private: + string alias; + Client client; }; -#endif +#endif \ No newline at end of file
--- a/MbedJSONValue.h Wed Sep 27 01:18:12 2017 +0000 +++ b/MbedJSONValue.h Fri Nov 03 01:07:32 2017 +0000 @@ -31,7 +31,7 @@ #ifndef _Mbed_RPC_VALUE_H_ #define _Mbed_RPC_VALUE_H_ -#define NB_TOKEN 20 +#define NB_TOKEN 200 /*!< Number maximum of MbedJSONValue in an array or an object */ #include <string>
--- a/MockHsm.cpp Wed Sep 27 01:18:12 2017 +0000 +++ b/MockHsm.cpp Fri Nov 03 01:07:32 2017 +0000 @@ -1,10 +1,14 @@ #include "MockHsm.h" - +//#include "Uuid.h" MockHsm::MockHsm() { +// Uuid u; +// GUID guid = u.CreateGuid(); +// string strUuid = u.GuidToString(guid); + alias = ""; - client_token = "962cd39d-6496-4b23-a2c5-85e445069a78"; + client_token = "962cd39d-6496-4b23-a2c5-85e445069a12"; } MockHsm::~MockHsm() @@ -39,7 +43,7 @@ printf("json: %s\r\n", strJson.c_str()); string strRep = ""; - string strUrl = "/create-key"; + string strUrl = "/mockhsm/create-key"; client.http_post(strUrl,strJson,strRep); //cout<< "HTTP Response:\n" << strRep.length() << endl << strRep.c_str() << endl; @@ -67,3 +71,23 @@ return strJson; } + + +string MockHsm::listTransactionFeeds() +{ + string strJson = ""; + + MbedJSONValue val; + + strJson = val.serialize(); + if ("null" == strJson) + { + strJson = "{}"; + } + + string strRep = ""; + string strUrl = "/list-transaction-feeds"; + client.http_post(strUrl,strJson,strRep); + + return strJson; +} \ No newline at end of file
--- a/MockHsm.h Wed Sep 27 01:18:12 2017 +0000 +++ b/MockHsm.h Fri Nov 03 01:07:32 2017 +0000 @@ -1,9 +1,9 @@ /*********************************************************************** * Module: MockHsm.h * Author: Administrator - * Modified: 2017Äê8ÔÂ31ÈÕ 16:29:48 + * Modified: 2017å¹´8æ31æ¥ 16:29:48 * Purpose: Declaration of the class MockHsm - * Comment: ÓÉÁ´ºËÌṩµÄÄ£ÄâHSMÀ´´¦Àí¿ª·¢ÖеĹؼü²ÄÁÏ¡£ + * Comment: ç±é¾æ ¸æä¾ç模æHSMæ¥å¤çå¼åä¸çå ³é®ææã ***********************************************************************/ #if !defined(__Account_MockHsm_h) @@ -25,6 +25,7 @@ string createKey(); string listKeys(); + string listTransactionFeeds(); protected: private: @@ -33,4 +34,4 @@ Client client; }; -#endif +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Output.h Fri Nov 03 01:07:32 2017 +0000 @@ -0,0 +1,43 @@ +/*********************************************************************** + * Module: Output.h + * Author: Administrator + * Modified: 2017å¹´9æ8æ¥ 15:08:39 + * Purpose: Declaration of the class Output + * Comment: äºå¡ä¸å å«å个è¾åºã + ***********************************************************************/ + +#if !defined(__Account_Output_h) +#define __Account_Output_h + +#include <map> +#include <string> +#include "Object.h" +using namespace std; + + +class Output +{ +public: + std::string id; + std::string type; + std::string purpose; + int position; + std::string assetId; + std::string assetAlias; + map<std::string,Object> assetDefinition; + map<std::string,Object> assetTags; + std::string assetIsLocal; + long amount; + std::string accountId; + std::string accountAlias; + map<std::string,Object> accountTags; + std::string controlProgram; + map<std::string,Object> referenceData; + std::string idLocal; + +protected: +private: + +}; + +#endif \ No newline at end of file
--- a/SDFileSystem.lib Wed Sep 27 01:18:12 2017 +0000 +++ b/SDFileSystem.lib Fri Nov 03 01:07:32 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/SDFileSystem/#ccb4a9e4d456 +https://os.mbed.com/users/webmaster/code/SDFileSystem/#ccb4a9e4d456
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Transaction.cpp Fri Nov 03 01:07:32 2017 +0000 @@ -0,0 +1,311 @@ +#include "Transaction.h" +#include "MbedJSONValue.h" +#include <string> +#include <stdio.h> + +// �������� +// ע��json�Ǹ�����[] +string Transaction::buildTransaction() +{ + string strJson = ""; + + MbedJSONValue val; + MbedJSONValue reference_data; + MbedJSONValue valArray; + + //fill the object + val["actions"][0]["account_alias"] = "t_acc_1"; + val["actions"][0]["asset_alias"] = "t_asset"; + val["actions"][0]["amount"] = 10; + val["actions"][0]["reference_data"] = reference_data; + val["actions"][0]["type"] = "spend_account"; + + val["actions"][1]["account_alias"] = "t_acc_2"; + val["actions"][1]["asset_alias"] = "t_asset"; + val["actions"][1]["amount"] = 10; + val["actions"][1]["reference_data"] = reference_data; + val["actions"][1]["type"] = "control_account"; + + //serialize it into a JSON string + valArray[0] = val; + strJson = valArray.serialize(); + + printf("json: %s\r\n", strJson.c_str()); + + string strRep = ""; + string strUrl = "/build-transaction"; + client.http_post(strUrl,strJson,strRep); + printf("=============4========\n"); + + // get http response content + string content = client.get_content(strRep); + + // decode content json + MbedJSONValue valRep; + parse(valRep,content.c_str()); + m_valRep = valRep; + + std::string raw_transaction = valRep[0]["raw_transaction"].get<std::string>(); + MbedJSONValue valSign; + valSign = valRep[0]["signing_instructions"][0]; + + string xpub; + xpub = valSign["witness_components"][0]["keys"][0]["xpub"].get<std::string>(); + m_strXpub = xpub; + + return strJson; +} + +string Transaction::buildTransaction(string type) +{ + string strJson = ""; + + MbedJSONValue val; + MbedJSONValue reference_data; + MbedJSONValue valArray; + + //fill the object + if (type == "spend_account") + { + amount = 10; + sender = "t_acc_1"; + receiver = "t_acc_2"; + asset_alias = "t_asset"; + + val["actions"][0]["account_alias"] = sender; + val["actions"][0]["asset_alias"] = asset_alias; + val["actions"][0]["amount"] = amount; + val["actions"][0]["reference_data"] = reference_data; + val["actions"][0]["type"] = type; + + val["actions"][1]["account_alias"] = receiver; + val["actions"][1]["asset_alias"] = asset_alias; + val["actions"][1]["amount"] = amount; + val["actions"][1]["reference_data"] = reference_data; + val["actions"][1]["type"] = "control_account"; + } + else if (type == "issue") + { + amount = 1000; + receiver = "t_acc_1"; + asset_alias = "t_asset"; + + val["actions"][0]["asset_alias"] = asset_alias; + val["actions"][0]["amount"] = amount; + val["actions"][0]["reference_data"] = reference_data; + val["actions"][0]["type"] = type; + + val["actions"][1]["account_alias"] = receiver; + val["actions"][1]["asset_alias"] = asset_alias; + val["actions"][1]["amount"] = amount; + val["actions"][1]["reference_data"] = reference_data; + val["actions"][1]["type"] = "control_account"; + } + + + //serialize it into a JSON string + valArray[0] = val; + strJson = valArray.serialize(); + + //printf("json: %s\r\n", strJson.c_str()); + + string strRep = ""; + string strUrl = "/build-transaction"; + client.http_post(strUrl,strJson,strRep); + + + // get http response content + string content = client.get_content(strRep); + + // decode content json + MbedJSONValue valRep; + parse(valRep,content.c_str()); + m_valRep = valRep; + + std::string raw_transaction = valRep[0]["raw_transaction"].get<std::string>(); + MbedJSONValue valSign; + valSign = valRep[0]["signing_instructions"][0]; + + string xpub; + xpub = valSign["witness_components"][0]["keys"][0]["xpub"].get<std::string>(); + m_strXpub = xpub; + + return strJson; +} + +string Transaction::buildTransaction(vector<Actions> &actions) +{ + string strJson = ""; + MbedJSONValue val; + MbedJSONValue reference_data; + MbedJSONValue valArray; + printf("=============3========\r\n"); + //fill the object + vector<Actions>::iterator it; + int i = 0; + for (it = actions.begin(),i = 0; it != actions.end(); it++,i++) + { + printf("=============3.1========\r\n"); + if (it->type == "spend_account") + { + val["actions"][i]["account_alias"] = it->accounts_alias; + val["actions"][i]["asset_alias"] = it->asset_alias; + val["actions"][i]["amount"] = it->amount; + val["actions"][i]["reference_data"] = it->reference_data; + val["actions"][i]["type"] = it->type; + } + else if (it->type == "issue") + { + val["actions"][i]["asset_alias"] = it->asset_alias; + val["actions"][i]["amount"] = it->amount; + val["actions"][i]["reference_data"] = it->reference_data; + val["actions"][i]["type"] = it->type; + } + else if (it->type == "control_account") + { + val["actions"][i]["account_alias"] = it->accounts_alias; + val["actions"][i]["asset_alias"] = it->asset_alias; + val["actions"][i]["amount"] = it->amount; + val["actions"][i]["reference_data"] = it->reference_data; + val["actions"][i]["type"] = it->type; + } + printf("=============3.2========\r\n"); + } + + printf("=============3.3========\r\n"); + + //serialize it into a JSON string + valArray[0] = val; + strJson = valArray.serialize(); + + printf("json: %s\r\n", strJson.c_str()); + + string strRep = ""; + string strUrl = "/build-transaction"; + if (client.http_post(strUrl,strJson,strRep) < 0) + { + return "http post error"; + } + else if (strRep.find("HTTP/1.1 200 OK") < 0) + { + return strRep.substr(0,strRep.find_first_of("\r\n")); + } + + // get http response content + string content = client.get_content(strRep); + if (content.find("raw_transaction") < 0) + { + return content; + } + printf("=============4========\r\n"); + // decode content json + MbedJSONValue valRep; + string err = parse(valRep,content.c_str()); + int ret = 0; + ret = err.find("error"); + if ( ret > 1) + { + return err; + } + m_valRep = valRep; + + std::string raw_transaction = valRep[0]["raw_transaction"].get<std::string>(); + MbedJSONValue valSign; + valSign = valRep[0]["signing_instructions"][0]; + + string xpub; + xpub = valSign["witness_components"][0]["keys"][0]["xpub"].get<std::string>(); + m_strXpub = xpub; + + return strJson; +} + +string Transaction::signTransaction() +{ + string strJson = ""; + + MbedJSONValue val; + + //fill the object + val["transactions"] = m_valRep; + val["xpubs"][0] = m_strXpub; + + //serialize it into a JSON string + strJson = val.serialize(); + + string strRep = ""; + string strUrl = "/mockhsm/sign-transaction"; + client.http_post(strUrl,strJson,strRep); + + + // get http response content + string content = client.get_content(strRep); + + // decode content json + MbedJSONValue valRep; + parse(valRep,content.c_str()); + + m_valRep = valRep; + + return strJson; +} + +string Transaction::submitTransaction() +{ + string strJson = ""; + + MbedJSONValue val; + + //fill the object + val["transactions"] = m_valRep; + + //serialize it into a JSON string + strJson = val.serialize(); + + //printf("json: %s\r\n", strJson.c_str()); + + string strRep = ""; + string strUrl = "/submit-transaction"; + client.http_post(strUrl,strJson,strRep); + + // get http response content + string content = client.get_content(strRep); + + // decode content json + MbedJSONValue valRep; + parse(valRep,content.c_str()); + + id = valRep[0]["id"].get<std::string>(); + + return strJson; +} + +string Transaction::listTransactions() +{ + string strJson = ""; + + MbedJSONValue val; + + //fill the object + val["filter"] = "id='" + id + "'"; + + //serialize it into a JSON string + strJson = val.serialize(); + + //printf("json: %s\r\n", strJson.c_str()); + + string strRep = ""; + string strUrl = "/list-transactions"; + client.http_post(strUrl,strJson,strRep); + + + // get http response content + string content = client.get_content(strRep); + + // decode content json + MbedJSONValue valRep; + parse(valRep,content.c_str()); + m_valRep = valRep; + + return strJson; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Transaction.h Fri Nov 03 01:07:32 2017 +0000 @@ -0,0 +1,94 @@ +/*********************************************************************** + * Module: Transaction.h + * Author: Administrator + * Modified: 2017å¹´9æ8æ¥ 14:50:31 + * Purpose: Declaration of the class Transaction + * Comment: æ为åºåé¾æ ¸å¿ä¸çå个äºå¡ã + ***********************************************************************/ + +#if !defined(__Account_Transaction_h) +#define __Account_Transaction_h +#include <ctime> // struct tm +#include <string> +#include <map> +#include <list> +#include "Client.h" +#include "Input.h" +#include "Output.h" +#include "MbedJSONValue.h" + +using namespace std; + +class Object; +class Input; +class Output; + + +struct Actions +{ + string accounts_alias; + string asset_alias; + int amount; + MbedJSONValue reference_data; + string type; +}; + +class Transaction +{ +public: + std::string id; + struct tm timestamp; + std::string blockId; + int blockHeight; + int position; + map<string,Object> referenceData; + std::string isLocal; + list<Input> inputs; + list<Output> outputs; + + + void setId(const string newId){this->id = newId;}; + string getId(){return this->id;}; + + string m_type; + void setType(const string type){ this->m_type = type;}; + string getType(){ return this->m_type;}; + + string sender; + void setSender(const string & account){ this->sender = account; }; + + string receiver; + void setReceiver(const string & account){ this->receiver = account; }; + + int amount; + void setAmount(int amount){ this->amount = amount; }; + + string asset_alias; + void setAsset(const string & asset){ this->asset_alias = asset; }; + + + // åç»å¯¹åºå¤è´¦æ·çæä½ç¨map管çï¼ææ¶åªæ¯æå°äºä¸¤ä¸ªè´¦æ·ç交æ + // 交æè¿ç¨æ¯æå¤å¯¹å¤ç交æï¼èä¸å顺åºæ²¡ä»ä¹å ³ç³»ï¼åªè¦amountè¾¾å°å¹³è¡¡å°±å¯ä»¥ + map<string,Actions> m_mapActions; + vector<Actions> m_vctActions; + + + string buildTransaction(); + string buildTransaction(string type); + string buildTransaction(vector<Actions> &actions); + //string /mockhsm/sign-transaction; + string signTransaction(); + string submitTransaction(); + string listTransactions(); + + +protected: +private: + Client client; + MbedJSONValue m_valRep; + MbedJSONValue m_valXpub; + string m_strXpub; + +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Uuid.cpp Fri Nov 03 01:07:32 2017 +0000 @@ -0,0 +1,11 @@ +#include "Uuid.h" + +std::string Uuid::getUuid() +{ + string str = "17e48179-3500-48db-9335-b69d9af1117f"; + char pUUID[128] = {0}; + getUniqueIDAsStr(pUUID); + printf("CPUID = %s \n",pUUID); + + return str; +}
--- a/Uuid.h Wed Sep 27 01:18:12 2017 +0000 +++ b/Uuid.h Fri Nov 03 01:07:32 2017 +0000 @@ -1,42 +1,67 @@ -#pragma once +/* + * create UUID +**/ +#pragma once #include <string> #include <stdio.h> #include <iostream> using namespace std; +#include "uniqueCPUID.h" + +class Uuid +{ +public: + std::string getUuid(); +protected: +private: +}; + + +/* #ifdef WIN32 #include <objbase.h> #else #include <uuid/uuid.h> #endif -GUID CreateGuid() +class Uuid { - GUID guid; +public: +// GUID CreateGuid(); +// std::string GuidToString(const GUID &guid); + + GUID CreateGuid() + { + GUID guid; #ifdef WIN32 - CoCreateGuid(&guid); + CoCreateGuid(&guid); #else - uuid_generate(reinterpret_cast<unsigned char *>(&guid)); + uuid_generate(reinterpret_cast<unsigned char *>(&guid)); #endif - return guid; -} + return guid; + }; + -std::string GuidToString(const GUID &guid) -{ - char buf[64] = {0}; + std::string GuidToString(const GUID &guid) + { + char buf[64] = {0}; #ifdef __GNUC__ - snprintf( + snprintf( #else // MSVC - _snprintf_s( + _snprintf_s( #endif - buf, - sizeof(buf), - "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", - guid.Data1, guid.Data2, guid.Data3, - guid.Data4[0], guid.Data4[1], - guid.Data4[2], guid.Data4[3], - guid.Data4[4], guid.Data4[5], - guid.Data4[6], guid.Data4[7]); - return std::string(buf); -} + buf, + sizeof(buf), + "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", + guid.Data1, guid.Data2, guid.Data3, + guid.Data4[0], guid.Data4[1], + guid.Data4[2], guid.Data4[3], + guid.Data4[4], guid.Data4[5], + guid.Data4[6], guid.Data4[7]); + return std::string(buf); + }; + +}; +*/
--- a/define.h Wed Sep 27 01:18:12 2017 +0000 +++ b/define.h Fri Nov 03 01:07:32 2017 +0000 @@ -1,4 +1,4 @@ #pragma once #define DEBUG_LOG 0 -#define BUFSIZE 1024*20 +#define BUFSIZE 1024*20 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo/Account_test.cpp Fri Nov 03 01:07:32 2017 +0000 @@ -0,0 +1,46 @@ +/* +å¸æ·æ¯æ为åºåé¾æ ¸å¿å¹³å°ä¸çä¸ä¸ªå¯¹è±¡ï¼éè¿å建åè·è¸ªæ§å¶ç¨åºæ¥è·è¸ªåºåé¾ä¸çèµäº§çæææã +å建å¸æ·æ¶ï¼æ¨æä¾ä¸ä¸ªæå¤ä¸ªârootâå¯é¥å仲è£ã + +å建å¸æ· +1 +è¾å ¥å¸æ·å«åçå称ï¼ä¾å¦'alice'ï¼'bob'ï¼ + +å¸æ·å«åæ¯ç¨äºåºåå¸æ·çæ ç¾ã +2 +é®å ¥å称以çææ°çHSMå¯é¥ï¼ä¾å¦'alice key'ï¼'bob key'ï¼ + +æ¤å¯é¥å°ç¨äºæ¤å¸æ·ä¸èµäº§åä½çåæ¾å转移ã +3 +ç¹å»âæ交âæé® +*/ + +#include "mbed.h" +#include "EthernetInterface.h" +#include "SDFileSystem.h" +#include <stdio.h> +#include "uniqueCPUID.h" + +#include <iostream> +#include <string> +#include "UnitTest.h" +//#include "BlockChain.h" +#include "MockHsm.h" +#include "Asset.h" +#include "Transaction.h" +#include "Account.h" +#include "Key.h" + + +void Account_test() +{ + Key key; + key.setAlias("t_key"); + key.listKeys(); + + Account act(key); + act.setAlias("t_acc_8"); + act.createAccount(); + act.listAccounts(); + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo/Asset_test.cpp Fri Nov 03 01:07:32 2017 +0000 @@ -0,0 +1,41 @@ +/* +èµäº§æ¯ä¸ç§å¯ä»¥å¨åºåé¾ä¸åå¸çå¼ç±»åã +èµäº§çææåä½åå¯äºæ¢ï¼å¯ä»¥å¨åæ¹ä¹é´ç´æ¥äº¤æï¼æ éåè¡äººåä¸ã + +å建èµäº§ +1 +é®å ¥èµäº§å«åçå称ï¼ä¾å¦'gold'ï¼'silver'ï¼'bronze'ï¼ + +èµäº§å«åæ¯ç¨äºåºåèµäº§çæ ç¾ã +2 +éæ©âReKeyâé®ä»¥ä½¿ç¨ç°æçHSMå¯é® + +æ¤å¯é¥å°ç¨äºæ¤å¸æ·ä¸èµäº§åä½çåæ¾å转移ã +3 +ç¹å»âæ交âæé® +*/ +#include "mbed.h" +#include "EthernetInterface.h" +#include "SDFileSystem.h" +#include <stdio.h> +#include "uniqueCPUID.h" + +#include <iostream> +#include <string> +#include "UnitTest.h" +//#include "BlockChain.h" +#include "MockHsm.h" +#include "Asset.h" +#include "Transaction.h" +#include "Account.h" +#include "Key.h" + + +void Asset_test() +{ + Asset asset; + asset.setAlias("t_asset_6"); + + cout<< asset.createAsset() << endl; + cout<< asset.listAssets() << endl; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo/Key_test.cpp Fri Nov 03 01:07:32 2017 +0000 @@ -0,0 +1,35 @@ +/* +å å¯ç§é¥æ¯åºåé¾ä¸ç主è¦æææºå¶ã +ä»ä»¬æ§å¶èµäº§åä½çåè¡å转让ã èµäº§æå¸æ·å°å®ä¹åè¡æ转移æéçå个å¯é¥ã + +å建HSMå¯é¥ +1 +é®å ¥å¯é¥å«åçå称ï¼ä¾å¦'gold'ï¼'silver'ï¼'bronze'ï¼ + +å¯é¥å«åæ¯ç¨äºåºåå¯é¥çæ ç¾ã +*/ +#include "mbed.h" +#include "EthernetInterface.h" +#include "SDFileSystem.h" +#include <stdio.h> +#include "uniqueCPUID.h" + +#include <iostream> +#include <string> +#include "UnitTest.h" +//#include "BlockChain.h" +#include "MockHsm.h" +#include "Asset.h" +#include "Transaction.h" +#include "Account.h" +#include "Key.h" + + +void Key_test() +{ + MockHsm mh; + mh.setAlias("t_key_6"); + cout<<mh.getAlias()<<endl; + cout<<mh.createKey()<<endl; + cout<< mh.listKeys() <<endl; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/demo/Transaction_test.cpp Fri Nov 03 01:07:32 2017 +0000 @@ -0,0 +1,66 @@ +/* +交æå å«ä¸ä¸ªæå¤ä¸ªè¾å ¥ï¼ä»¥åä¸ä¸ªæå¤ä¸ªè¾åºã +æ为åºåé¾æ ¸å¿å¹³å°çAPIå 许æ¨ä½¿ç¨æä½ï¼å æ¬ååºï¼æ¯åºåè¿è¿ï¼æ建交æã +åå¸èµäº§åä½ +1 +æ·»å âååºâæä½ +2 +为èµäº§å«åéæ©âReAssetâ +3 +è¾å ¥â100âä½ä¸ºéé¢ +4 +æ·»å â使ç¨å¸æ·æ§å¶âæä½ +5 +为å¸æ·å«åéæ©âReAccountâ +6 +为èµäº§å«åéæ©âReAssetâ +7 +è¾å ¥â100âä½ä¸ºéé¢ +8 +ç¹å»âæ交âæé® +*/ + +#include "mbed.h" +#include "EthernetInterface.h" +#include "SDFileSystem.h" +#include <stdio.h> +#include "uniqueCPUID.h" + +#include <iostream> +#include <string> +#include "UnitTest.h" +//#include "BlockChain.h" +#include "MockHsm.h" +#include "Asset.h" +#include "Transaction.h" +#include "Account.h" +#include "Key.h" + + +void Transaction_test() +{ + vector<Actions> vActions; + Actions act; + MbedJSONValue reference_data; + + act.accounts_alias = "t_acc_1"; + act.amount = 51; + act.asset_alias = "t_asset"; + act.reference_data = reference_data; + act.type = "spend_account"; + vActions.push_back(act); + + Actions act1 = {"t_acc_2", "t_asset", 49, reference_data, "spend_account" }; + Actions act2 = {"tom", "t_asset", 80, reference_data, "control_account"}; + Actions act3 = {"Jerry", "t_asset", 20, reference_data, "control_account"}; + + vActions.push_back(act1); + vActions.push_back(act2); + vActions.push_back(act3); + + Transaction ts; + ts.buildTransaction(vActions); + ts.signTransaction(); + ts.submitTransaction(); + ts.listTransactions(); +} \ No newline at end of file
--- a/main.cpp Wed Sep 27 01:18:12 2017 +0000 +++ b/main.cpp Fri Nov 03 01:07:32 2017 +0000 @@ -2,7 +2,6 @@ #include "EthernetInterface.h" #include "SDFileSystem.h" #include <stdio.h> -//#include <string.h> #include "uniqueCPUID.h" #include <iostream> @@ -10,6 +9,10 @@ #include "UnitTest.h" //#include "BlockChain.h" #include "MockHsm.h" +#include "Asset.h" +#include "Transaction.h" +#include "Account.h" +#include "Key.h" using namespace std; @@ -19,6 +22,8 @@ #define HTTPD_MAX_FNAME_LENGTH 127 #define HTTPD_MAX_DNAME_LENGTH 127 +#define DEBUG_LOG 1 + const char* ECHO_SERVER_ADDRESS = "192.168.31.185"; const int ECHO_SERVER_PORT = 10003; @@ -49,15 +54,7 @@ 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}; @@ -66,17 +63,110 @@ //printUniqueId(uart); } +int test_MockHsm() +{ + MockHsm mh; + mh.setAlias("t_key_6"); + cout<<mh.getAlias()<<endl; + cout<<mh.createKey()<<endl; + cout<< mh.listKeys() <<endl; + return 0; +} + +int test_Asset() +{ + Asset asset; + asset.setAlias("t_asset_6"); + + cout<< asset.createAsset() << endl; + cout<< asset.listAssets() << endl; + + + return 0; +} + +int test_Account() +{ + Key key; + key.setAlias("t_key"); + key.listKeys(); + + Account act(key); + act.setAlias("t_acc_8"); + act.createAccount(); + act.listAccounts(); + + return 0; +} + +int test_transaction(string type) +{ + Transaction ts; + //cout << ts.buildTransaction() << endl; + ts.buildTransaction(type); + ts.signTransaction(); + ts.submitTransaction(); + ts.listTransactions(); + return 0; +} + +int test_transaction() +{ + vector<Actions> vActions; + Actions act; + MbedJSONValue reference_data; + + act.accounts_alias = "t_acc_1"; + act.amount = 51; + act.asset_alias = "t_asset"; + act.reference_data = reference_data; + act.type = "spend_account"; + vActions.push_back(act); + + Actions act1 = {"t_acc_2", "t_asset", 49, reference_data, "spend_account" }; + Actions act2 = {"tom", "t_asset", 80, reference_data, "control_account"}; + Actions act3 = {"Jerry", "t_asset", 20, reference_data, "control_account"}; + + vActions.push_back(act1); + vActions.push_back(act2); + vActions.push_back(act3); + printf("=============2========\r\n"); + Transaction ts; + ts.buildTransaction(vActions); + ts.signTransaction(); + ts.submitTransaction(); + ts.listTransactions(); + + return 0; +} + + int main(void) { printf("main start\n"); test_uuid(); + socket_init(); test_MockHsm(); + test_Asset(); + test_Account(); + + //char isContinue = 'y'; + // do + //{ + string type = "spend_account"; + //string type = "issue"; + //test_transaction(type); + test_transaction(); + printf("=============5========\n"); + // }while(1); + //}while (isContinue = getch() != 'q'); - test_socket_client(); + + //test_socket_client(); socket_fini();