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 “提交”
Diff: Transaction.cpp
- Revision:
- 10:aabd720e632c
diff -r d7468574ef8f -r aabd720e632c Transaction.cpp --- /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