nem.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /**
  2. * Copyright (c) 2017 Saleem Rashid
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included
  12. * in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, E1PRESS
  15. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  18. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "options.h"
  23. #if USE_NEM
  24. #include "nem.h"
  25. #include <string.h>
  26. #include "base32.h"
  27. #include "ed25519_donna/ed25519_keccak.h"
  28. #include "memzero.h"
  29. #include "ripemd160.h"
  30. #include "sha3.h"
  31. #define CAN_WRITE(NEEDED) ((ctx->offset + (NEEDED)) <= ctx->size)
  32. #define SERIALIZE_U32(DATA) \
  33. do { \
  34. if(!nem_write_u32(ctx, (DATA))) return false; \
  35. } while(0)
  36. #define SERIALIZE_U64(DATA) \
  37. do { \
  38. if(!nem_write_u64(ctx, (DATA))) return false; \
  39. } while(0)
  40. #define SERIALIZE_TAGGED(DATA, LENGTH) \
  41. do { \
  42. if(!nem_write_tagged(ctx, (DATA), (LENGTH))) return false; \
  43. } while(0)
  44. const char* nem_network_name(uint8_t network) {
  45. switch(network) {
  46. case NEM_NETWORK_MAINNET:
  47. return "NEM Mainnet";
  48. case NEM_NETWORK_TESTNET:
  49. return "NEM Testnet";
  50. case NEM_NETWORK_MIJIN:
  51. return "Mijin";
  52. default:
  53. return NULL;
  54. }
  55. }
  56. static inline bool
  57. nem_write_checked(nem_transaction_ctx* ctx, const uint8_t* data, uint32_t length) {
  58. if(!CAN_WRITE(length)) {
  59. return false;
  60. }
  61. memcpy(&ctx->buffer[ctx->offset], data, length);
  62. ctx->offset += length;
  63. return true;
  64. }
  65. static inline bool nem_write_u32(nem_transaction_ctx* ctx, uint32_t data) {
  66. if(!CAN_WRITE(4)) {
  67. return false;
  68. }
  69. ctx->buffer[ctx->offset++] = (data >> 0) & 0xff;
  70. ctx->buffer[ctx->offset++] = (data >> 8) & 0xff;
  71. ctx->buffer[ctx->offset++] = (data >> 16) & 0xff;
  72. ctx->buffer[ctx->offset++] = (data >> 24) & 0xff;
  73. return true;
  74. }
  75. static inline bool nem_write_u64(nem_transaction_ctx* ctx, uint64_t data) {
  76. SERIALIZE_U32((data >> 0) & 0xffffffff);
  77. SERIALIZE_U32((data >> 32) & 0xffffffff);
  78. return true;
  79. }
  80. static inline bool
  81. nem_write_tagged(nem_transaction_ctx* ctx, const uint8_t* data, uint32_t length) {
  82. SERIALIZE_U32(length);
  83. return nem_write_checked(ctx, data, length);
  84. }
  85. static inline bool
  86. nem_write_mosaic_str(nem_transaction_ctx* ctx, const char* name, const char* value) {
  87. uint32_t name_length = strlen(name);
  88. uint32_t value_length = strlen(value);
  89. SERIALIZE_U32(sizeof(uint32_t) + name_length + sizeof(uint32_t) + value_length);
  90. SERIALIZE_TAGGED((const uint8_t*)name, name_length);
  91. SERIALIZE_TAGGED((const uint8_t*)value, value_length);
  92. return true;
  93. }
  94. static inline bool nem_write_mosaic_bool(nem_transaction_ctx* ctx, const char* name, bool value) {
  95. return nem_write_mosaic_str(ctx, name, value ? "true" : "false");
  96. }
  97. static inline bool
  98. nem_write_mosaic_u64(nem_transaction_ctx* ctx, const char* name, uint64_t value) {
  99. char buffer[21] = {0};
  100. if(bn_format_uint64(value, NULL, NULL, 0, 0, false, 0, buffer, sizeof(buffer)) == 0) {
  101. return false;
  102. }
  103. return nem_write_mosaic_str(ctx, name, buffer);
  104. }
  105. void nem_get_address_raw(const ed25519_public_key public_key, uint8_t version, uint8_t* address) {
  106. uint8_t hash[SHA3_256_DIGEST_LENGTH] = {0};
  107. /* 1. Perform 256-bit Sha3 on the public key */
  108. keccak_256(public_key, sizeof(ed25519_public_key), hash);
  109. /* 2. Perform 160-bit Ripemd of hash resulting from step 1. */
  110. ripemd160(hash, SHA3_256_DIGEST_LENGTH, &address[1]);
  111. /* 3. Prepend version byte to Ripemd hash (either 0x68 or 0x98) */
  112. address[0] = version;
  113. /* 4. Perform 256-bit Sha3 on the result, take the first four bytes as a
  114. * checksum */
  115. keccak_256(address, 1 + RIPEMD160_DIGEST_LENGTH, hash);
  116. /* 5. Concatenate output of step 3 and the checksum from step 4 */
  117. memcpy(&address[1 + RIPEMD160_DIGEST_LENGTH], hash, 4);
  118. memzero(hash, sizeof(hash));
  119. }
  120. bool nem_get_address(const ed25519_public_key public_key, uint8_t version, char* address) {
  121. uint8_t pubkeyhash[NEM_ADDRESS_SIZE_RAW] = {0};
  122. nem_get_address_raw(public_key, version, pubkeyhash);
  123. char* ret = base32_encode(
  124. pubkeyhash, sizeof(pubkeyhash), address, NEM_ADDRESS_SIZE + 1, BASE32_ALPHABET_RFC4648);
  125. memzero(pubkeyhash, sizeof(pubkeyhash));
  126. return (ret != NULL);
  127. }
  128. bool nem_validate_address_raw(const uint8_t* address, uint8_t network) {
  129. if(!nem_network_name(network) || address[0] != network) {
  130. return false;
  131. }
  132. uint8_t hash[SHA3_256_DIGEST_LENGTH] = {0};
  133. keccak_256(address, 1 + RIPEMD160_DIGEST_LENGTH, hash);
  134. bool valid = (memcmp(&address[1 + RIPEMD160_DIGEST_LENGTH], hash, 4) == 0);
  135. memzero(hash, sizeof(hash));
  136. return valid;
  137. }
  138. bool nem_validate_address(const char* address, uint8_t network) {
  139. uint8_t pubkeyhash[NEM_ADDRESS_SIZE_RAW] = {0};
  140. if(strlen(address) != NEM_ADDRESS_SIZE) {
  141. return false;
  142. }
  143. uint8_t* ret = base32_decode(
  144. address, NEM_ADDRESS_SIZE, pubkeyhash, sizeof(pubkeyhash), BASE32_ALPHABET_RFC4648);
  145. bool valid = (ret != NULL) && nem_validate_address_raw(pubkeyhash, network);
  146. memzero(pubkeyhash, sizeof(pubkeyhash));
  147. return valid;
  148. }
  149. void nem_transaction_start(
  150. nem_transaction_ctx* ctx,
  151. const ed25519_public_key public_key,
  152. uint8_t* buffer,
  153. size_t size) {
  154. memcpy(ctx->public_key, public_key, sizeof(ctx->public_key));
  155. ctx->buffer = buffer;
  156. ctx->offset = 0;
  157. ctx->size = size;
  158. }
  159. size_t nem_transaction_end(
  160. nem_transaction_ctx* ctx,
  161. const ed25519_secret_key private_key,
  162. ed25519_signature signature) {
  163. if(private_key != NULL && signature != NULL) {
  164. ed25519_sign_keccak(ctx->buffer, ctx->offset, private_key, signature);
  165. }
  166. return ctx->offset;
  167. }
  168. bool nem_transaction_write_common(
  169. nem_transaction_ctx* ctx,
  170. uint32_t type,
  171. uint32_t version,
  172. uint32_t timestamp,
  173. const ed25519_public_key signer,
  174. uint64_t fee,
  175. uint32_t deadline) {
  176. SERIALIZE_U32(type);
  177. SERIALIZE_U32(version);
  178. SERIALIZE_U32(timestamp);
  179. SERIALIZE_TAGGED(signer, sizeof(ed25519_public_key));
  180. SERIALIZE_U64(fee);
  181. SERIALIZE_U32(deadline);
  182. return true;
  183. }
  184. bool nem_transaction_create_transfer(
  185. nem_transaction_ctx* ctx,
  186. uint8_t network,
  187. uint32_t timestamp,
  188. const ed25519_public_key signer,
  189. uint64_t fee,
  190. uint32_t deadline,
  191. const char* recipient,
  192. uint64_t amount,
  193. const uint8_t* payload,
  194. uint32_t length,
  195. bool encrypted,
  196. uint32_t mosaics) {
  197. if(!signer) {
  198. signer = ctx->public_key;
  199. }
  200. if(!payload) {
  201. length = 0;
  202. }
  203. bool ret = nem_transaction_write_common(
  204. ctx,
  205. NEM_TRANSACTION_TYPE_TRANSFER,
  206. (uint32_t)network << 24 | (mosaics ? 2 : 1),
  207. timestamp,
  208. signer,
  209. fee,
  210. deadline);
  211. if(!ret) return false;
  212. SERIALIZE_TAGGED((const uint8_t*)recipient, NEM_ADDRESS_SIZE);
  213. SERIALIZE_U64(amount);
  214. if(length) {
  215. SERIALIZE_U32(sizeof(uint32_t) + sizeof(uint32_t) + length);
  216. SERIALIZE_U32(encrypted ? 0x02 : 0x01);
  217. SERIALIZE_TAGGED(payload, length);
  218. } else {
  219. SERIALIZE_U32(0);
  220. }
  221. if(mosaics) {
  222. SERIALIZE_U32(mosaics);
  223. }
  224. return true;
  225. }
  226. bool nem_transaction_write_mosaic(
  227. nem_transaction_ctx* ctx,
  228. const char* namespace,
  229. const char* mosaic,
  230. uint64_t quantity) {
  231. size_t namespace_length = strlen(namespace);
  232. size_t mosaic_length = strlen(mosaic);
  233. size_t identifier_length =
  234. sizeof(uint32_t) + namespace_length + sizeof(uint32_t) + mosaic_length;
  235. SERIALIZE_U32(sizeof(uint32_t) + sizeof(uint64_t) + identifier_length);
  236. SERIALIZE_U32(identifier_length);
  237. SERIALIZE_TAGGED((const uint8_t*)namespace, namespace_length);
  238. SERIALIZE_TAGGED((const uint8_t*)mosaic, mosaic_length);
  239. SERIALIZE_U64(quantity);
  240. return true;
  241. }
  242. bool nem_transaction_create_multisig(
  243. nem_transaction_ctx* ctx,
  244. uint8_t network,
  245. uint32_t timestamp,
  246. const ed25519_public_key signer,
  247. uint64_t fee,
  248. uint32_t deadline,
  249. const nem_transaction_ctx* inner) {
  250. if(!signer) {
  251. signer = ctx->public_key;
  252. }
  253. bool ret = nem_transaction_write_common(
  254. ctx,
  255. NEM_TRANSACTION_TYPE_MULTISIG,
  256. (uint32_t)network << 24 | 1,
  257. timestamp,
  258. signer,
  259. fee,
  260. deadline);
  261. if(!ret) return false;
  262. SERIALIZE_TAGGED(inner->buffer, inner->offset);
  263. return true;
  264. }
  265. bool nem_transaction_create_multisig_signature(
  266. nem_transaction_ctx* ctx,
  267. uint8_t network,
  268. uint32_t timestamp,
  269. const ed25519_public_key signer,
  270. uint64_t fee,
  271. uint32_t deadline,
  272. const nem_transaction_ctx* inner) {
  273. if(!signer) {
  274. signer = ctx->public_key;
  275. }
  276. bool ret = nem_transaction_write_common(
  277. ctx,
  278. NEM_TRANSACTION_TYPE_MULTISIG_SIGNATURE,
  279. (uint32_t)network << 24 | 1,
  280. timestamp,
  281. signer,
  282. fee,
  283. deadline);
  284. if(!ret) return false;
  285. char address[NEM_ADDRESS_SIZE + 1] = {0};
  286. nem_get_address(inner->public_key, network, address);
  287. uint8_t hash[SHA3_256_DIGEST_LENGTH] = {0};
  288. keccak_256(inner->buffer, inner->offset, hash);
  289. SERIALIZE_U32(sizeof(uint32_t) + SHA3_256_DIGEST_LENGTH);
  290. SERIALIZE_TAGGED(hash, SHA3_256_DIGEST_LENGTH);
  291. SERIALIZE_TAGGED((const uint8_t*)address, NEM_ADDRESS_SIZE);
  292. return true;
  293. }
  294. bool nem_transaction_create_provision_namespace(
  295. nem_transaction_ctx* ctx,
  296. uint8_t network,
  297. uint32_t timestamp,
  298. const ed25519_public_key signer,
  299. uint64_t fee,
  300. uint32_t deadline,
  301. const char* namespace,
  302. const char* parent,
  303. const char* rental_sink,
  304. uint64_t rental_fee) {
  305. if(!signer) {
  306. signer = ctx->public_key;
  307. }
  308. bool ret = nem_transaction_write_common(
  309. ctx,
  310. NEM_TRANSACTION_TYPE_PROVISION_NAMESPACE,
  311. (uint32_t)network << 24 | 1,
  312. timestamp,
  313. signer,
  314. fee,
  315. deadline);
  316. if(!ret) return false;
  317. if(parent) {
  318. SERIALIZE_TAGGED((const uint8_t*)rental_sink, NEM_ADDRESS_SIZE);
  319. SERIALIZE_U64(rental_fee);
  320. SERIALIZE_TAGGED((const uint8_t*)namespace, strlen(namespace));
  321. SERIALIZE_TAGGED((const uint8_t*)parent, strlen(parent));
  322. } else {
  323. SERIALIZE_TAGGED((const uint8_t*)rental_sink, NEM_ADDRESS_SIZE);
  324. SERIALIZE_U64(rental_fee);
  325. SERIALIZE_TAGGED((const uint8_t*)namespace, strlen(namespace));
  326. SERIALIZE_U32(0xffffffff);
  327. }
  328. return true;
  329. }
  330. bool nem_transaction_create_mosaic_creation(
  331. nem_transaction_ctx* ctx,
  332. uint8_t network,
  333. uint32_t timestamp,
  334. const ed25519_public_key signer,
  335. uint64_t fee,
  336. uint32_t deadline,
  337. const char* namespace,
  338. const char* mosaic,
  339. const char* description,
  340. uint32_t divisibility,
  341. uint64_t supply,
  342. bool mutable_supply,
  343. bool transferable,
  344. uint32_t levy_type,
  345. uint64_t levy_fee,
  346. const char* levy_address,
  347. const char* levy_namespace,
  348. const char* levy_mosaic,
  349. const char* creation_sink,
  350. uint64_t creation_fee) {
  351. if(!signer) {
  352. signer = ctx->public_key;
  353. }
  354. bool ret = nem_transaction_write_common(
  355. ctx,
  356. NEM_TRANSACTION_TYPE_MOSAIC_CREATION,
  357. (uint32_t)network << 24 | 1,
  358. timestamp,
  359. signer,
  360. fee,
  361. deadline);
  362. if(!ret) return false;
  363. size_t namespace_length = strlen(namespace);
  364. size_t mosaic_length = strlen(mosaic);
  365. size_t identifier_length =
  366. sizeof(uint32_t) + namespace_length + sizeof(uint32_t) + mosaic_length;
  367. // This length will be rewritten later on
  368. nem_transaction_ctx state = {0};
  369. memcpy(&state, ctx, sizeof(state));
  370. SERIALIZE_U32(0);
  371. SERIALIZE_TAGGED(signer, sizeof(ed25519_public_key));
  372. SERIALIZE_U32(identifier_length);
  373. SERIALIZE_TAGGED((const uint8_t*)namespace, namespace_length);
  374. SERIALIZE_TAGGED((const uint8_t*)mosaic, mosaic_length);
  375. SERIALIZE_TAGGED((const uint8_t*)description, strlen(description));
  376. SERIALIZE_U32(4); // Number of properties
  377. if(!nem_write_mosaic_u64(ctx, "divisibility", divisibility)) return false;
  378. if(!nem_write_mosaic_u64(ctx, "initialSupply", supply)) return false;
  379. if(!nem_write_mosaic_bool(ctx, "supplyMutable", mutable_supply)) return false;
  380. if(!nem_write_mosaic_bool(ctx, "transferable", transferable)) return false;
  381. if(levy_type) {
  382. size_t levy_namespace_length = strlen(levy_namespace);
  383. size_t levy_mosaic_length = strlen(levy_mosaic);
  384. size_t levy_identifier_length =
  385. sizeof(uint32_t) + levy_namespace_length + sizeof(uint32_t) + levy_mosaic_length;
  386. SERIALIZE_U32(
  387. sizeof(uint32_t) + sizeof(uint32_t) + NEM_ADDRESS_SIZE + sizeof(uint32_t) +
  388. levy_identifier_length + sizeof(uint64_t));
  389. SERIALIZE_U32(levy_type);
  390. SERIALIZE_TAGGED((const uint8_t*)levy_address, NEM_ADDRESS_SIZE);
  391. SERIALIZE_U32(levy_identifier_length);
  392. SERIALIZE_TAGGED((const uint8_t*)levy_namespace, levy_namespace_length);
  393. SERIALIZE_TAGGED((const uint8_t*)levy_mosaic, levy_mosaic_length);
  394. SERIALIZE_U64(levy_fee);
  395. } else {
  396. SERIALIZE_U32(0);
  397. }
  398. // Rewrite length
  399. nem_write_u32(&state, ctx->offset - state.offset - sizeof(uint32_t));
  400. SERIALIZE_TAGGED((const uint8_t*)creation_sink, NEM_ADDRESS_SIZE);
  401. SERIALIZE_U64(creation_fee);
  402. return true;
  403. }
  404. bool nem_transaction_create_mosaic_supply_change(
  405. nem_transaction_ctx* ctx,
  406. uint8_t network,
  407. uint32_t timestamp,
  408. const ed25519_public_key signer,
  409. uint64_t fee,
  410. uint32_t deadline,
  411. const char* namespace,
  412. const char* mosaic,
  413. uint32_t type,
  414. uint64_t delta) {
  415. if(!signer) {
  416. signer = ctx->public_key;
  417. }
  418. bool ret = nem_transaction_write_common(
  419. ctx,
  420. NEM_TRANSACTION_TYPE_MOSAIC_SUPPLY_CHANGE,
  421. (uint32_t)network << 24 | 1,
  422. timestamp,
  423. signer,
  424. fee,
  425. deadline);
  426. if(!ret) return false;
  427. size_t namespace_length = strlen(namespace);
  428. size_t mosaic_length = strlen(mosaic);
  429. size_t identifier_length =
  430. sizeof(uint32_t) + namespace_length + sizeof(uint32_t) + mosaic_length;
  431. SERIALIZE_U32(identifier_length);
  432. SERIALIZE_TAGGED((const uint8_t*)namespace, namespace_length);
  433. SERIALIZE_TAGGED((const uint8_t*)mosaic, mosaic_length);
  434. SERIALIZE_U32(type);
  435. SERIALIZE_U64(delta);
  436. return true;
  437. }
  438. bool nem_transaction_create_aggregate_modification(
  439. nem_transaction_ctx* ctx,
  440. uint8_t network,
  441. uint32_t timestamp,
  442. const ed25519_public_key signer,
  443. uint64_t fee,
  444. uint32_t deadline,
  445. uint32_t modifications,
  446. bool relative_change) {
  447. if(!signer) {
  448. signer = ctx->public_key;
  449. }
  450. bool ret = nem_transaction_write_common(
  451. ctx,
  452. NEM_TRANSACTION_TYPE_AGGREGATE_MODIFICATION,
  453. (uint32_t)network << 24 | (relative_change ? 2 : 1),
  454. timestamp,
  455. signer,
  456. fee,
  457. deadline);
  458. if(!ret) return false;
  459. SERIALIZE_U32(modifications);
  460. return true;
  461. }
  462. bool nem_transaction_write_cosignatory_modification(
  463. nem_transaction_ctx* ctx,
  464. uint32_t type,
  465. const ed25519_public_key cosignatory) {
  466. SERIALIZE_U32(sizeof(uint32_t) + sizeof(uint32_t) + sizeof(ed25519_public_key));
  467. SERIALIZE_U32(type);
  468. SERIALIZE_TAGGED(cosignatory, sizeof(ed25519_public_key));
  469. return true;
  470. }
  471. bool nem_transaction_write_minimum_cosignatories(nem_transaction_ctx* ctx, int32_t relative_change) {
  472. SERIALIZE_U32(sizeof(uint32_t));
  473. SERIALIZE_U32((uint32_t)relative_change);
  474. return true;
  475. }
  476. bool nem_transaction_create_importance_transfer(
  477. nem_transaction_ctx* ctx,
  478. uint8_t network,
  479. uint32_t timestamp,
  480. const ed25519_public_key signer,
  481. uint64_t fee,
  482. uint32_t deadline,
  483. uint32_t mode,
  484. const ed25519_public_key remote) {
  485. if(!signer) {
  486. signer = ctx->public_key;
  487. }
  488. bool ret = nem_transaction_write_common(
  489. ctx,
  490. NEM_TRANSACTION_TYPE_IMPORTANCE_TRANSFER,
  491. (uint32_t)network << 24 | 1,
  492. timestamp,
  493. signer,
  494. fee,
  495. deadline);
  496. if(!ret) return false;
  497. SERIALIZE_U32(mode);
  498. SERIALIZE_TAGGED(remote, sizeof(ed25519_public_key));
  499. return true;
  500. }
  501. #endif // USE_NEM