nem.c 17 KB

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