nem.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 nem_write_checked(nem_transaction_ctx *ctx,
  57. 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 nem_write_tagged(nem_transaction_ctx *ctx,
  81. const uint8_t *data, uint32_t length) {
  82. SERIALIZE_U32(length);
  83. return nem_write_checked(ctx, data, length);
  84. }
  85. static inline bool nem_write_mosaic_str(nem_transaction_ctx *ctx,
  86. 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) +
  90. value_length);
  91. SERIALIZE_TAGGED((const uint8_t *)name, name_length);
  92. SERIALIZE_TAGGED((const uint8_t *)value, value_length);
  93. return true;
  94. }
  95. static inline bool nem_write_mosaic_bool(nem_transaction_ctx *ctx,
  96. const char *name, bool value) {
  97. return nem_write_mosaic_str(ctx, name, value ? "true" : "false");
  98. }
  99. static inline bool nem_write_mosaic_u64(nem_transaction_ctx *ctx,
  100. const char *name, uint64_t value) {
  101. char buffer[21] = {0};
  102. if (bn_format_uint64(value, NULL, NULL, 0, 0, false, 0, buffer,
  103. sizeof(buffer)) == 0) {
  104. return false;
  105. }
  106. return nem_write_mosaic_str(ctx, name, buffer);
  107. }
  108. void nem_get_address_raw(const ed25519_public_key public_key, uint8_t version,
  109. uint8_t *address) {
  110. uint8_t hash[SHA3_256_DIGEST_LENGTH] = {0};
  111. /* 1. Perform 256-bit Sha3 on the public key */
  112. keccak_256(public_key, sizeof(ed25519_public_key), hash);
  113. /* 2. Perform 160-bit Ripemd of hash resulting from step 1. */
  114. ripemd160(hash, SHA3_256_DIGEST_LENGTH, &address[1]);
  115. /* 3. Prepend version byte to Ripemd hash (either 0x68 or 0x98) */
  116. address[0] = version;
  117. /* 4. Perform 256-bit Sha3 on the result, take the first four bytes as a
  118. * checksum */
  119. keccak_256(address, 1 + RIPEMD160_DIGEST_LENGTH, hash);
  120. /* 5. Concatenate output of step 3 and the checksum from step 4 */
  121. memcpy(&address[1 + RIPEMD160_DIGEST_LENGTH], hash, 4);
  122. memzero(hash, sizeof(hash));
  123. }
  124. bool nem_get_address(const ed25519_public_key public_key, uint8_t version,
  125. char *address) {
  126. uint8_t pubkeyhash[NEM_ADDRESS_SIZE_RAW] = {0};
  127. nem_get_address_raw(public_key, version, pubkeyhash);
  128. char *ret = base32_encode(pubkeyhash, sizeof(pubkeyhash), address,
  129. NEM_ADDRESS_SIZE + 1, BASE32_ALPHABET_RFC4648);
  130. memzero(pubkeyhash, sizeof(pubkeyhash));
  131. return (ret != NULL);
  132. }
  133. bool nem_validate_address_raw(const uint8_t *address, uint8_t network) {
  134. if (!nem_network_name(network) || address[0] != network) {
  135. return false;
  136. }
  137. uint8_t hash[SHA3_256_DIGEST_LENGTH] = {0};
  138. keccak_256(address, 1 + RIPEMD160_DIGEST_LENGTH, hash);
  139. bool valid = (memcmp(&address[1 + RIPEMD160_DIGEST_LENGTH], hash, 4) == 0);
  140. memzero(hash, sizeof(hash));
  141. return valid;
  142. }
  143. bool nem_validate_address(const char *address, uint8_t network) {
  144. uint8_t pubkeyhash[NEM_ADDRESS_SIZE_RAW] = {0};
  145. if (strlen(address) != NEM_ADDRESS_SIZE) {
  146. return false;
  147. }
  148. uint8_t *ret = base32_decode(address, NEM_ADDRESS_SIZE, pubkeyhash,
  149. sizeof(pubkeyhash), BASE32_ALPHABET_RFC4648);
  150. bool valid = (ret != NULL) && nem_validate_address_raw(pubkeyhash, network);
  151. memzero(pubkeyhash, sizeof(pubkeyhash));
  152. return valid;
  153. }
  154. void nem_transaction_start(nem_transaction_ctx *ctx,
  155. const ed25519_public_key public_key, uint8_t *buffer,
  156. size_t size) {
  157. memcpy(ctx->public_key, public_key, sizeof(ctx->public_key));
  158. ctx->buffer = buffer;
  159. ctx->offset = 0;
  160. ctx->size = size;
  161. }
  162. size_t nem_transaction_end(nem_transaction_ctx *ctx,
  163. const ed25519_secret_key private_key,
  164. ed25519_signature signature) {
  165. if (private_key != NULL && signature != NULL) {
  166. ed25519_sign_keccak(ctx->buffer, ctx->offset, private_key, signature);
  167. }
  168. return ctx->offset;
  169. }
  170. bool nem_transaction_write_common(nem_transaction_ctx *ctx, uint32_t type,
  171. uint32_t version, uint32_t timestamp,
  172. const ed25519_public_key signer, uint64_t fee,
  173. uint32_t deadline) {
  174. SERIALIZE_U32(type);
  175. SERIALIZE_U32(version);
  176. SERIALIZE_U32(timestamp);
  177. SERIALIZE_TAGGED(signer, sizeof(ed25519_public_key));
  178. SERIALIZE_U64(fee);
  179. SERIALIZE_U32(deadline);
  180. return true;
  181. }
  182. bool nem_transaction_create_transfer(nem_transaction_ctx *ctx, uint8_t network,
  183. uint32_t timestamp,
  184. const ed25519_public_key signer,
  185. uint64_t fee, uint32_t deadline,
  186. const char *recipient, uint64_t amount,
  187. const uint8_t *payload, uint32_t length,
  188. bool encrypted, uint32_t mosaics) {
  189. if (!signer) {
  190. signer = ctx->public_key;
  191. }
  192. if (!payload) {
  193. length = 0;
  194. }
  195. bool ret =
  196. nem_transaction_write_common(ctx, NEM_TRANSACTION_TYPE_TRANSFER,
  197. (uint32_t)network << 24 | (mosaics ? 2 : 1),
  198. timestamp, signer, fee, deadline);
  199. if (!ret) return false;
  200. SERIALIZE_TAGGED((const uint8_t *)recipient, NEM_ADDRESS_SIZE);
  201. SERIALIZE_U64(amount);
  202. if (length) {
  203. SERIALIZE_U32(sizeof(uint32_t) + sizeof(uint32_t) + length);
  204. SERIALIZE_U32(encrypted ? 0x02 : 0x01);
  205. SERIALIZE_TAGGED(payload, length);
  206. } else {
  207. SERIALIZE_U32(0);
  208. }
  209. if (mosaics) {
  210. SERIALIZE_U32(mosaics);
  211. }
  212. return true;
  213. }
  214. bool nem_transaction_write_mosaic(nem_transaction_ctx *ctx,
  215. const char *namespace, const char *mosaic,
  216. uint64_t quantity) {
  217. size_t namespace_length = strlen(namespace);
  218. size_t mosaic_length = strlen(mosaic);
  219. size_t identifier_length =
  220. sizeof(uint32_t) + namespace_length + sizeof(uint32_t) + mosaic_length;
  221. SERIALIZE_U32(sizeof(uint32_t) + sizeof(uint64_t) + identifier_length);
  222. SERIALIZE_U32(identifier_length);
  223. SERIALIZE_TAGGED((const uint8_t *)namespace, namespace_length);
  224. SERIALIZE_TAGGED((const uint8_t *)mosaic, mosaic_length);
  225. SERIALIZE_U64(quantity);
  226. return true;
  227. }
  228. bool nem_transaction_create_multisig(nem_transaction_ctx *ctx, uint8_t network,
  229. uint32_t timestamp,
  230. const ed25519_public_key signer,
  231. uint64_t fee, uint32_t deadline,
  232. const nem_transaction_ctx *inner) {
  233. if (!signer) {
  234. signer = ctx->public_key;
  235. }
  236. bool ret = nem_transaction_write_common(ctx, NEM_TRANSACTION_TYPE_MULTISIG,
  237. (uint32_t)network << 24 | 1,
  238. timestamp, signer, fee, deadline);
  239. if (!ret) return false;
  240. SERIALIZE_TAGGED(inner->buffer, inner->offset);
  241. return true;
  242. }
  243. bool nem_transaction_create_multisig_signature(
  244. nem_transaction_ctx *ctx, uint8_t network, uint32_t timestamp,
  245. const ed25519_public_key signer, uint64_t fee, uint32_t deadline,
  246. const nem_transaction_ctx *inner) {
  247. if (!signer) {
  248. signer = ctx->public_key;
  249. }
  250. bool ret = nem_transaction_write_common(
  251. ctx, NEM_TRANSACTION_TYPE_MULTISIG_SIGNATURE, (uint32_t)network << 24 | 1,
  252. timestamp, signer, fee, deadline);
  253. if (!ret) return false;
  254. char address[NEM_ADDRESS_SIZE + 1] = {0};
  255. nem_get_address(inner->public_key, network, address);
  256. uint8_t hash[SHA3_256_DIGEST_LENGTH] = {0};
  257. keccak_256(inner->buffer, inner->offset, hash);
  258. SERIALIZE_U32(sizeof(uint32_t) + SHA3_256_DIGEST_LENGTH);
  259. SERIALIZE_TAGGED(hash, SHA3_256_DIGEST_LENGTH);
  260. SERIALIZE_TAGGED((const uint8_t *)address, NEM_ADDRESS_SIZE);
  261. return true;
  262. }
  263. bool nem_transaction_create_provision_namespace(
  264. nem_transaction_ctx *ctx, uint8_t network, uint32_t timestamp,
  265. const ed25519_public_key signer, uint64_t fee, uint32_t deadline,
  266. const char *namespace, const char *parent, const char *rental_sink,
  267. uint64_t rental_fee) {
  268. if (!signer) {
  269. signer = ctx->public_key;
  270. }
  271. bool ret = nem_transaction_write_common(
  272. ctx, NEM_TRANSACTION_TYPE_PROVISION_NAMESPACE,
  273. (uint32_t)network << 24 | 1, timestamp, signer, fee, deadline);
  274. if (!ret) return false;
  275. if (parent) {
  276. SERIALIZE_TAGGED((const uint8_t *)rental_sink, NEM_ADDRESS_SIZE);
  277. SERIALIZE_U64(rental_fee);
  278. SERIALIZE_TAGGED((const uint8_t *)namespace, strlen(namespace));
  279. SERIALIZE_TAGGED((const uint8_t *)parent, strlen(parent));
  280. } else {
  281. SERIALIZE_TAGGED((const uint8_t *)rental_sink, NEM_ADDRESS_SIZE);
  282. SERIALIZE_U64(rental_fee);
  283. SERIALIZE_TAGGED((const uint8_t *)namespace, strlen(namespace));
  284. SERIALIZE_U32(0xffffffff);
  285. }
  286. return true;
  287. }
  288. bool nem_transaction_create_mosaic_creation(
  289. nem_transaction_ctx *ctx, uint8_t network, uint32_t timestamp,
  290. const ed25519_public_key signer, uint64_t fee, uint32_t deadline,
  291. const char *namespace, const char *mosaic, const char *description,
  292. uint32_t divisibility, uint64_t supply, bool mutable_supply,
  293. bool transferable, uint32_t levy_type, uint64_t levy_fee,
  294. const char *levy_address, const char *levy_namespace,
  295. const char *levy_mosaic, const char *creation_sink, uint64_t creation_fee) {
  296. if (!signer) {
  297. signer = ctx->public_key;
  298. }
  299. bool ret = nem_transaction_write_common(
  300. ctx, NEM_TRANSACTION_TYPE_MOSAIC_CREATION, (uint32_t)network << 24 | 1,
  301. timestamp, signer, fee, deadline);
  302. if (!ret) return false;
  303. size_t namespace_length = strlen(namespace);
  304. size_t mosaic_length = strlen(mosaic);
  305. size_t identifier_length =
  306. sizeof(uint32_t) + namespace_length + sizeof(uint32_t) + mosaic_length;
  307. // This length will be rewritten later on
  308. nem_transaction_ctx state = {0};
  309. memcpy(&state, ctx, sizeof(state));
  310. SERIALIZE_U32(0);
  311. SERIALIZE_TAGGED(signer, sizeof(ed25519_public_key));
  312. SERIALIZE_U32(identifier_length);
  313. SERIALIZE_TAGGED((const uint8_t *)namespace, namespace_length);
  314. SERIALIZE_TAGGED((const uint8_t *)mosaic, mosaic_length);
  315. SERIALIZE_TAGGED((const uint8_t *)description, strlen(description));
  316. SERIALIZE_U32(4); // Number of properties
  317. if (!nem_write_mosaic_u64(ctx, "divisibility", divisibility)) return false;
  318. if (!nem_write_mosaic_u64(ctx, "initialSupply", supply)) return false;
  319. if (!nem_write_mosaic_bool(ctx, "supplyMutable", mutable_supply))
  320. return false;
  321. if (!nem_write_mosaic_bool(ctx, "transferable", transferable)) return false;
  322. if (levy_type) {
  323. size_t levy_namespace_length = strlen(levy_namespace);
  324. size_t levy_mosaic_length = strlen(levy_mosaic);
  325. size_t levy_identifier_length = sizeof(uint32_t) + levy_namespace_length +
  326. sizeof(uint32_t) + levy_mosaic_length;
  327. SERIALIZE_U32(sizeof(uint32_t) + sizeof(uint32_t) + NEM_ADDRESS_SIZE +
  328. sizeof(uint32_t) + levy_identifier_length + sizeof(uint64_t));
  329. SERIALIZE_U32(levy_type);
  330. SERIALIZE_TAGGED((const uint8_t *)levy_address, NEM_ADDRESS_SIZE);
  331. SERIALIZE_U32(levy_identifier_length);
  332. SERIALIZE_TAGGED((const uint8_t *)levy_namespace, levy_namespace_length);
  333. SERIALIZE_TAGGED((const uint8_t *)levy_mosaic, levy_mosaic_length);
  334. SERIALIZE_U64(levy_fee);
  335. } else {
  336. SERIALIZE_U32(0);
  337. }
  338. // Rewrite length
  339. nem_write_u32(&state, ctx->offset - state.offset - sizeof(uint32_t));
  340. SERIALIZE_TAGGED((const uint8_t *)creation_sink, NEM_ADDRESS_SIZE);
  341. SERIALIZE_U64(creation_fee);
  342. return true;
  343. }
  344. bool nem_transaction_create_mosaic_supply_change(
  345. nem_transaction_ctx *ctx, uint8_t network, uint32_t timestamp,
  346. const ed25519_public_key signer, uint64_t fee, uint32_t deadline,
  347. const char *namespace, const char *mosaic, uint32_t type, uint64_t delta) {
  348. if (!signer) {
  349. signer = ctx->public_key;
  350. }
  351. bool ret = nem_transaction_write_common(
  352. ctx, NEM_TRANSACTION_TYPE_MOSAIC_SUPPLY_CHANGE,
  353. (uint32_t)network << 24 | 1, timestamp, signer, fee, deadline);
  354. if (!ret) return false;
  355. size_t namespace_length = strlen(namespace);
  356. size_t mosaic_length = strlen(mosaic);
  357. size_t identifier_length =
  358. sizeof(uint32_t) + namespace_length + sizeof(uint32_t) + mosaic_length;
  359. SERIALIZE_U32(identifier_length);
  360. SERIALIZE_TAGGED((const uint8_t *)namespace, namespace_length);
  361. SERIALIZE_TAGGED((const uint8_t *)mosaic, mosaic_length);
  362. SERIALIZE_U32(type);
  363. SERIALIZE_U64(delta);
  364. return true;
  365. }
  366. bool nem_transaction_create_aggregate_modification(
  367. nem_transaction_ctx *ctx, uint8_t network, uint32_t timestamp,
  368. const ed25519_public_key signer, uint64_t fee, uint32_t deadline,
  369. uint32_t modifications, bool relative_change) {
  370. if (!signer) {
  371. signer = ctx->public_key;
  372. }
  373. bool ret = nem_transaction_write_common(
  374. ctx, NEM_TRANSACTION_TYPE_AGGREGATE_MODIFICATION,
  375. (uint32_t)network << 24 | (relative_change ? 2 : 1), timestamp, signer,
  376. fee, deadline);
  377. if (!ret) return false;
  378. SERIALIZE_U32(modifications);
  379. return true;
  380. }
  381. bool nem_transaction_write_cosignatory_modification(
  382. nem_transaction_ctx *ctx, uint32_t type,
  383. const ed25519_public_key cosignatory) {
  384. SERIALIZE_U32(sizeof(uint32_t) + sizeof(uint32_t) +
  385. sizeof(ed25519_public_key));
  386. SERIALIZE_U32(type);
  387. SERIALIZE_TAGGED(cosignatory, sizeof(ed25519_public_key));
  388. return true;
  389. }
  390. bool nem_transaction_write_minimum_cosignatories(nem_transaction_ctx *ctx,
  391. int32_t relative_change) {
  392. SERIALIZE_U32(sizeof(uint32_t));
  393. SERIALIZE_U32((uint32_t)relative_change);
  394. return true;
  395. }
  396. bool nem_transaction_create_importance_transfer(
  397. nem_transaction_ctx *ctx, uint8_t network, uint32_t timestamp,
  398. const ed25519_public_key signer, uint64_t fee, uint32_t deadline,
  399. uint32_t mode, const ed25519_public_key remote) {
  400. if (!signer) {
  401. signer = ctx->public_key;
  402. }
  403. bool ret = nem_transaction_write_common(
  404. ctx, NEM_TRANSACTION_TYPE_IMPORTANCE_TRANSFER,
  405. (uint32_t)network << 24 | 1, timestamp, signer, fee, deadline);
  406. if (!ret) return false;
  407. SERIALIZE_U32(mode);
  408. SERIALIZE_TAGGED(remote, sizeof(ed25519_public_key));
  409. return true;
  410. }
  411. #endif // USE_NEM