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 “提交”
MbedJSONValue.cpp@8:f2a567ee3a46, 2017-09-21 (annotated)
- Committer:
- webmaster
- Date:
- Thu Sep 21 02:09:04 2017 +0000
- Revision:
- 8:f2a567ee3a46
test create asset;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
webmaster | 8:f2a567ee3a46 | 1 | #include "MbedJSONValue.h" |
webmaster | 8:f2a567ee3a46 | 2 | |
webmaster | 8:f2a567ee3a46 | 3 | # include <stdlib.h> |
webmaster | 8:f2a567ee3a46 | 4 | # include <stdio.h> |
webmaster | 8:f2a567ee3a46 | 5 | |
webmaster | 8:f2a567ee3a46 | 6 | // Clean up |
webmaster | 8:f2a567ee3a46 | 7 | void MbedJSONValue::clean() { |
webmaster | 8:f2a567ee3a46 | 8 | switch (_type) { |
webmaster | 8:f2a567ee3a46 | 9 | case TypeString: |
webmaster | 8:f2a567ee3a46 | 10 | delete _value.asString; |
webmaster | 8:f2a567ee3a46 | 11 | break; |
webmaster | 8:f2a567ee3a46 | 12 | case TypeArray: |
webmaster | 8:f2a567ee3a46 | 13 | for (int i = 0; i < index_array; i++) |
webmaster | 8:f2a567ee3a46 | 14 | delete array[i]; |
webmaster | 8:f2a567ee3a46 | 15 | index_array = 0; |
webmaster | 8:f2a567ee3a46 | 16 | break; |
webmaster | 8:f2a567ee3a46 | 17 | case TypeObject: |
webmaster | 8:f2a567ee3a46 | 18 | for (int i = 0; i < index_token; i++) { |
webmaster | 8:f2a567ee3a46 | 19 | delete token[i]; |
webmaster | 8:f2a567ee3a46 | 20 | delete token_name[i]; |
webmaster | 8:f2a567ee3a46 | 21 | } |
webmaster | 8:f2a567ee3a46 | 22 | index_token = 0; |
webmaster | 8:f2a567ee3a46 | 23 | break; |
webmaster | 8:f2a567ee3a46 | 24 | default: |
webmaster | 8:f2a567ee3a46 | 25 | break; |
webmaster | 8:f2a567ee3a46 | 26 | } |
webmaster | 8:f2a567ee3a46 | 27 | _type = TypeNull; |
webmaster | 8:f2a567ee3a46 | 28 | _type = TypeNull; |
webmaster | 8:f2a567ee3a46 | 29 | } |
webmaster | 8:f2a567ee3a46 | 30 | |
webmaster | 8:f2a567ee3a46 | 31 | bool MbedJSONValue::hasMember(char * name) |
webmaster | 8:f2a567ee3a46 | 32 | { |
webmaster | 8:f2a567ee3a46 | 33 | for(int i = 0; i < index_token; i++) |
webmaster | 8:f2a567ee3a46 | 34 | if( !strcmp(name, (*(token_name[i])).c_str() )) |
webmaster | 8:f2a567ee3a46 | 35 | return true; |
webmaster | 8:f2a567ee3a46 | 36 | return false; |
webmaster | 8:f2a567ee3a46 | 37 | } |
webmaster | 8:f2a567ee3a46 | 38 | |
webmaster | 8:f2a567ee3a46 | 39 | |
webmaster | 8:f2a567ee3a46 | 40 | void copy(const std::string& s, std::back_insert_iterator<std::string> oi) { |
webmaster | 8:f2a567ee3a46 | 41 | std::copy(s.begin(), s.end(), oi); |
webmaster | 8:f2a567ee3a46 | 42 | } |
webmaster | 8:f2a567ee3a46 | 43 | |
webmaster | 8:f2a567ee3a46 | 44 | void serialize_str(const std::string& s, std::back_insert_iterator<std::string> oi) { |
webmaster | 8:f2a567ee3a46 | 45 | *oi++ = '"'; |
webmaster | 8:f2a567ee3a46 | 46 | for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { |
webmaster | 8:f2a567ee3a46 | 47 | switch (*i) { |
webmaster | 8:f2a567ee3a46 | 48 | #define MAP(val, sym) case val: copy(sym, oi); break |
webmaster | 8:f2a567ee3a46 | 49 | MAP('"', "\\\""); |
webmaster | 8:f2a567ee3a46 | 50 | MAP('\\', "\\\\"); |
webmaster | 8:f2a567ee3a46 | 51 | MAP('/', "\\/"); |
webmaster | 8:f2a567ee3a46 | 52 | MAP('\b', "\\b"); |
webmaster | 8:f2a567ee3a46 | 53 | MAP('\f', "\\f"); |
webmaster | 8:f2a567ee3a46 | 54 | MAP('\n', "\\n"); |
webmaster | 8:f2a567ee3a46 | 55 | MAP('\r', "\\r"); |
webmaster | 8:f2a567ee3a46 | 56 | MAP('\t', "\\t"); |
webmaster | 8:f2a567ee3a46 | 57 | #undef MAP |
webmaster | 8:f2a567ee3a46 | 58 | default: |
webmaster | 8:f2a567ee3a46 | 59 | if ((unsigned char)*i < 0x20 || *i == 0x7f) { |
webmaster | 8:f2a567ee3a46 | 60 | char buf[7]; |
webmaster | 8:f2a567ee3a46 | 61 | sprintf(buf, "\\u%04x", *i & 0xff); |
webmaster | 8:f2a567ee3a46 | 62 | copy(buf, buf + 6, oi); |
webmaster | 8:f2a567ee3a46 | 63 | } else { |
webmaster | 8:f2a567ee3a46 | 64 | *oi++ = *i; |
webmaster | 8:f2a567ee3a46 | 65 | } |
webmaster | 8:f2a567ee3a46 | 66 | break; |
webmaster | 8:f2a567ee3a46 | 67 | } |
webmaster | 8:f2a567ee3a46 | 68 | } |
webmaster | 8:f2a567ee3a46 | 69 | *oi++ = '"'; |
webmaster | 8:f2a567ee3a46 | 70 | } |
webmaster | 8:f2a567ee3a46 | 71 | |
webmaster | 8:f2a567ee3a46 | 72 | std::string MbedJSONValue::serialize(){ |
webmaster | 8:f2a567ee3a46 | 73 | std::string s; |
webmaster | 8:f2a567ee3a46 | 74 | serialize(std::back_inserter(s)); |
webmaster | 8:f2a567ee3a46 | 75 | return s; |
webmaster | 8:f2a567ee3a46 | 76 | } |
webmaster | 8:f2a567ee3a46 | 77 | |
webmaster | 8:f2a567ee3a46 | 78 | std::string MbedJSONValue::to_str(){ |
webmaster | 8:f2a567ee3a46 | 79 | switch (_type) { |
webmaster | 8:f2a567ee3a46 | 80 | case TypeNull: |
webmaster | 8:f2a567ee3a46 | 81 | return "null"; |
webmaster | 8:f2a567ee3a46 | 82 | case TypeBoolean: |
webmaster | 8:f2a567ee3a46 | 83 | return _value.asBool ? "true" : "false"; |
webmaster | 8:f2a567ee3a46 | 84 | case TypeInt: { |
webmaster | 8:f2a567ee3a46 | 85 | char buf[10]; |
webmaster | 8:f2a567ee3a46 | 86 | sprintf(buf, "%d", _value.asInt); |
webmaster | 8:f2a567ee3a46 | 87 | return buf; |
webmaster | 8:f2a567ee3a46 | 88 | } |
webmaster | 8:f2a567ee3a46 | 89 | case TypeDouble: { |
webmaster | 8:f2a567ee3a46 | 90 | char buf[10]; |
webmaster | 8:f2a567ee3a46 | 91 | sprintf(buf, "%f", _value.asDouble); |
webmaster | 8:f2a567ee3a46 | 92 | return buf; |
webmaster | 8:f2a567ee3a46 | 93 | } |
webmaster | 8:f2a567ee3a46 | 94 | default: |
webmaster | 8:f2a567ee3a46 | 95 | break; |
webmaster | 8:f2a567ee3a46 | 96 | } |
webmaster | 8:f2a567ee3a46 | 97 | return NULL; |
webmaster | 8:f2a567ee3a46 | 98 | } |
webmaster | 8:f2a567ee3a46 | 99 | |
webmaster | 8:f2a567ee3a46 | 100 | |
webmaster | 8:f2a567ee3a46 | 101 | |
webmaster | 8:f2a567ee3a46 | 102 | void MbedJSONValue::serialize(std::back_insert_iterator<std::string> oi) { |
webmaster | 8:f2a567ee3a46 | 103 | switch (_type) { |
webmaster | 8:f2a567ee3a46 | 104 | case TypeString: |
webmaster | 8:f2a567ee3a46 | 105 | serialize_str(*_value.asString, oi); |
webmaster | 8:f2a567ee3a46 | 106 | break; |
webmaster | 8:f2a567ee3a46 | 107 | case TypeArray: { |
webmaster | 8:f2a567ee3a46 | 108 | *oi++ = '['; |
webmaster | 8:f2a567ee3a46 | 109 | for (int i = 0; i < index_array; i++) { |
webmaster | 8:f2a567ee3a46 | 110 | if (i) |
webmaster | 8:f2a567ee3a46 | 111 | *oi++ = ','; |
webmaster | 8:f2a567ee3a46 | 112 | (*this)[i].serialize(oi); |
webmaster | 8:f2a567ee3a46 | 113 | } |
webmaster | 8:f2a567ee3a46 | 114 | *oi++ = ']'; |
webmaster | 8:f2a567ee3a46 | 115 | break; |
webmaster | 8:f2a567ee3a46 | 116 | } |
webmaster | 8:f2a567ee3a46 | 117 | case TypeObject: { |
webmaster | 8:f2a567ee3a46 | 118 | *oi++ = '{'; |
webmaster | 8:f2a567ee3a46 | 119 | for (int i = 0; i < index_token; i++) { |
webmaster | 8:f2a567ee3a46 | 120 | if (i) |
webmaster | 8:f2a567ee3a46 | 121 | *oi++ = ','; |
webmaster | 8:f2a567ee3a46 | 122 | serialize_str(*(token_name[i]), oi); |
webmaster | 8:f2a567ee3a46 | 123 | *oi++ = ':'; |
webmaster | 8:f2a567ee3a46 | 124 | (*(token[i])).serialize(oi); |
webmaster | 8:f2a567ee3a46 | 125 | } |
webmaster | 8:f2a567ee3a46 | 126 | *oi++ = '}'; |
webmaster | 8:f2a567ee3a46 | 127 | break; |
webmaster | 8:f2a567ee3a46 | 128 | } |
webmaster | 8:f2a567ee3a46 | 129 | default: |
webmaster | 8:f2a567ee3a46 | 130 | copy(to_str(), oi); |
webmaster | 8:f2a567ee3a46 | 131 | break; |
webmaster | 8:f2a567ee3a46 | 132 | } |
webmaster | 8:f2a567ee3a46 | 133 | } |
webmaster | 8:f2a567ee3a46 | 134 | |
webmaster | 8:f2a567ee3a46 | 135 | |
webmaster | 8:f2a567ee3a46 | 136 | |
webmaster | 8:f2a567ee3a46 | 137 | MbedJSONValue& MbedJSONValue::operator[](int i) { |
webmaster | 8:f2a567ee3a46 | 138 | _type = TypeArray; |
webmaster | 8:f2a567ee3a46 | 139 | if (i < NB_TOKEN && index_array == i ) { |
webmaster | 8:f2a567ee3a46 | 140 | #ifdef DEBUG |
webmaster | 8:f2a567ee3a46 | 141 | printf("will add an element to the array\r\n"); |
webmaster | 8:f2a567ee3a46 | 142 | #endif |
webmaster | 8:f2a567ee3a46 | 143 | array[i] = new MbedJSONValue(); |
webmaster | 8:f2a567ee3a46 | 144 | index_array++; |
webmaster | 8:f2a567ee3a46 | 145 | return *(array[i]); |
webmaster | 8:f2a567ee3a46 | 146 | } |
webmaster | 8:f2a567ee3a46 | 147 | if (i < NB_TOKEN && index_array > i) |
webmaster | 8:f2a567ee3a46 | 148 | return *(array[i]); |
webmaster | 8:f2a567ee3a46 | 149 | |
webmaster | 8:f2a567ee3a46 | 150 | //if the user is not doing something wrong, this code is never executed!! |
webmaster | 8:f2a567ee3a46 | 151 | return *(new MbedJSONValue()); |
webmaster | 8:f2a567ee3a46 | 152 | } |
webmaster | 8:f2a567ee3a46 | 153 | |
webmaster | 8:f2a567ee3a46 | 154 | MbedJSONValue& MbedJSONValue::operator[](std::string k) { |
webmaster | 8:f2a567ee3a46 | 155 | _type = TypeObject; |
webmaster | 8:f2a567ee3a46 | 156 | for (int i = 0; i < index_token; i++) { |
webmaster | 8:f2a567ee3a46 | 157 | #ifdef DEBUG |
webmaster | 8:f2a567ee3a46 | 158 | printf("k: %s\r\n", k.c_str()); |
webmaster | 8:f2a567ee3a46 | 159 | printf("str: %s\r\n", token_name[i]->c_str()); |
webmaster | 8:f2a567ee3a46 | 160 | #endif |
webmaster | 8:f2a567ee3a46 | 161 | //existing token |
webmaster | 8:f2a567ee3a46 | 162 | if (!strcmp(k.c_str(), token_name[i]->c_str())) { |
webmaster | 8:f2a567ee3a46 | 163 | #ifdef DEBUG |
webmaster | 8:f2a567ee3a46 | 164 | printf("token found: %d\r\n", i); |
webmaster | 8:f2a567ee3a46 | 165 | #endif |
webmaster | 8:f2a567ee3a46 | 166 | return *(token[i]); |
webmaster | 8:f2a567ee3a46 | 167 | } |
webmaster | 8:f2a567ee3a46 | 168 | } |
webmaster | 8:f2a567ee3a46 | 169 | |
webmaster | 8:f2a567ee3a46 | 170 | if(index_token >= NB_TOKEN) |
webmaster | 8:f2a567ee3a46 | 171 | index_token = NB_TOKEN - 1; |
webmaster | 8:f2a567ee3a46 | 172 | //non existing token |
webmaster | 8:f2a567ee3a46 | 173 | token_name[index_token] = new std::string(k); |
webmaster | 8:f2a567ee3a46 | 174 | token[index_token] = new MbedJSONValue(); |
webmaster | 8:f2a567ee3a46 | 175 | index_token++; |
webmaster | 8:f2a567ee3a46 | 176 | return *(token[index_token - 1]); |
webmaster | 8:f2a567ee3a46 | 177 | } |
webmaster | 8:f2a567ee3a46 | 178 | |
webmaster | 8:f2a567ee3a46 | 179 | MbedJSONValue& MbedJSONValue::operator[](std::string k) const |
webmaster | 8:f2a567ee3a46 | 180 | { |
webmaster | 8:f2a567ee3a46 | 181 | for (int i = 0; i < index_token; i++) { |
webmaster | 8:f2a567ee3a46 | 182 | #ifdef DEBUG |
webmaster | 8:f2a567ee3a46 | 183 | printf("k: %s\r\n", k.c_str()); |
webmaster | 8:f2a567ee3a46 | 184 | printf("str: %s\r\n", token_name[i]->c_str()); |
webmaster | 8:f2a567ee3a46 | 185 | #endif |
webmaster | 8:f2a567ee3a46 | 186 | if (!strcmp(k.c_str(), token_name[i]->c_str())) { |
webmaster | 8:f2a567ee3a46 | 187 | #ifdef DEBUG |
webmaster | 8:f2a567ee3a46 | 188 | printf("token found: %d\r\n", i); |
webmaster | 8:f2a567ee3a46 | 189 | #endif |
webmaster | 8:f2a567ee3a46 | 190 | return *(token[i]); |
webmaster | 8:f2a567ee3a46 | 191 | } |
webmaster | 8:f2a567ee3a46 | 192 | } |
webmaster | 8:f2a567ee3a46 | 193 | |
webmaster | 8:f2a567ee3a46 | 194 | //if the user is not doing something wrong, this code is never executed!! |
webmaster | 8:f2a567ee3a46 | 195 | return *(new MbedJSONValue()); |
webmaster | 8:f2a567ee3a46 | 196 | } |
webmaster | 8:f2a567ee3a46 | 197 | |
webmaster | 8:f2a567ee3a46 | 198 | |
webmaster | 8:f2a567ee3a46 | 199 | // Operators |
webmaster | 8:f2a567ee3a46 | 200 | MbedJSONValue& MbedJSONValue::operator=(MbedJSONValue const& rhs) { |
webmaster | 8:f2a567ee3a46 | 201 | if (this != &rhs) { |
webmaster | 8:f2a567ee3a46 | 202 | clean(); |
webmaster | 8:f2a567ee3a46 | 203 | _type = rhs._type; |
webmaster | 8:f2a567ee3a46 | 204 | switch (_type) { |
webmaster | 8:f2a567ee3a46 | 205 | case TypeBoolean: |
webmaster | 8:f2a567ee3a46 | 206 | _value.asBool = rhs._value.asBool; |
webmaster | 8:f2a567ee3a46 | 207 | break; |
webmaster | 8:f2a567ee3a46 | 208 | case TypeInt: |
webmaster | 8:f2a567ee3a46 | 209 | _value.asInt = rhs._value.asInt; |
webmaster | 8:f2a567ee3a46 | 210 | break; |
webmaster | 8:f2a567ee3a46 | 211 | case TypeDouble: |
webmaster | 8:f2a567ee3a46 | 212 | _value.asDouble = rhs._value.asDouble; |
webmaster | 8:f2a567ee3a46 | 213 | break; |
webmaster | 8:f2a567ee3a46 | 214 | case TypeString: |
webmaster | 8:f2a567ee3a46 | 215 | _value.asString = new std::string(*rhs._value.asString); |
webmaster | 8:f2a567ee3a46 | 216 | break; |
webmaster | 8:f2a567ee3a46 | 217 | case TypeArray: |
webmaster | 8:f2a567ee3a46 | 218 | for (int i = 0; i < rhs.index_array; i++) |
webmaster | 8:f2a567ee3a46 | 219 | (*this)[i] = rhs[i]; |
webmaster | 8:f2a567ee3a46 | 220 | case TypeObject: |
webmaster | 8:f2a567ee3a46 | 221 | for (int i = 0; i < rhs.index_token; i++) |
webmaster | 8:f2a567ee3a46 | 222 | (*this)[*(rhs.token_name[i])] = rhs[*(rhs.token_name[i])]; |
webmaster | 8:f2a567ee3a46 | 223 | default: |
webmaster | 8:f2a567ee3a46 | 224 | break; |
webmaster | 8:f2a567ee3a46 | 225 | } |
webmaster | 8:f2a567ee3a46 | 226 | } |
webmaster | 8:f2a567ee3a46 | 227 | return *this; |
webmaster | 8:f2a567ee3a46 | 228 | } |
webmaster | 8:f2a567ee3a46 | 229 | |
webmaster | 8:f2a567ee3a46 | 230 | |
webmaster | 8:f2a567ee3a46 | 231 | // Works for strings, arrays, and structs. |
webmaster | 8:f2a567ee3a46 | 232 | int MbedJSONValue::size() const { |
webmaster | 8:f2a567ee3a46 | 233 | switch (_type) { |
webmaster | 8:f2a567ee3a46 | 234 | case TypeString: |
webmaster | 8:f2a567ee3a46 | 235 | return int(_value.asString->size()); |
webmaster | 8:f2a567ee3a46 | 236 | case TypeArray: |
webmaster | 8:f2a567ee3a46 | 237 | return index_array; |
webmaster | 8:f2a567ee3a46 | 238 | case TypeObject: |
webmaster | 8:f2a567ee3a46 | 239 | return index_token; |
webmaster | 8:f2a567ee3a46 | 240 | default: |
webmaster | 8:f2a567ee3a46 | 241 | break; |
webmaster | 8:f2a567ee3a46 | 242 | } |
webmaster | 8:f2a567ee3a46 | 243 | return -1; |
webmaster | 8:f2a567ee3a46 | 244 | } |
webmaster | 8:f2a567ee3a46 | 245 | |
webmaster | 8:f2a567ee3a46 | 246 |