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