forked from trezor/trezor-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nem.c
508 lines (409 loc) · 17 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
/**
* 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-donna/ed25519-keccak.h"
#include "memzero.h"
#include "ripemd160.h"
#include "sha3.h"
#define CAN_WRITE(NEEDED) ((ctx->offset + (NEEDED)) <= ctx->size)
#define SERIALIZE_U32(DATA) \
do { \
if (!nem_write_u32(ctx, (DATA))) return false; \
} while (0)
#define SERIALIZE_U64(DATA) \
do { \
if (!nem_write_u64(ctx, (DATA))) return false; \
} while (0)
#define SERIALIZE_TAGGED(DATA, LENGTH) \
do { \
if (!nem_write_tagged(ctx, (DATA), (LENGTH))) return false; \
} while (0)
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 bool nem_write_checked(nem_transaction_ctx *ctx,
const uint8_t *data, uint32_t length) {
if (!CAN_WRITE(length)) {
return false;
}
memcpy(&ctx->buffer[ctx->offset], data, length);
ctx->offset += length;
return true;
}
static inline bool nem_write_u32(nem_transaction_ctx *ctx, uint32_t data) {
if (!CAN_WRITE(4)) {
return false;
}
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;
return true;
}
static inline bool nem_write_u64(nem_transaction_ctx *ctx, uint64_t data) {
SERIALIZE_U32((data >> 0) & 0xffffffff);
SERIALIZE_U32((data >> 32) & 0xffffffff);
return true;
}
static inline bool nem_write_tagged(nem_transaction_ctx *ctx,
const uint8_t *data, uint32_t length) {
SERIALIZE_U32(length);
return nem_write_checked(ctx, data, length);
}
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);
SERIALIZE_U32(sizeof(uint32_t) + name_length + sizeof(uint32_t) +
value_length);
SERIALIZE_TAGGED((const uint8_t *)name, name_length);
SERIALIZE_TAGGED((const uint8_t *)value, value_length);
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] = {0};
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] = {0};
/* 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);
memzero(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] = {0};
nem_get_address_raw(public_key, version, pubkeyhash);
char *ret = base32_encode(pubkeyhash, sizeof(pubkeyhash), address,
NEM_ADDRESS_SIZE + 1, BASE32_ALPHABET_RFC4648);
memzero(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] = {0};
keccak_256(address, 1 + RIPEMD160_DIGEST_LENGTH, hash);
bool valid = (memcmp(&address[1 + RIPEMD160_DIGEST_LENGTH], hash, 4) == 0);
memzero(hash, sizeof(hash));
return valid;
}
bool nem_validate_address(const char *address, uint8_t network) {
uint8_t pubkeyhash[NEM_ADDRESS_SIZE_RAW] = {0};
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);
memzero(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) {
SERIALIZE_U32(type);
SERIALIZE_U32(version);
SERIALIZE_U32(timestamp);
SERIALIZE_TAGGED(signer, sizeof(ed25519_public_key));
SERIALIZE_U64(fee);
SERIALIZE_U32(deadline);
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,
(uint32_t)network << 24 | (mosaics ? 2 : 1),
timestamp, signer, fee, deadline);
if (!ret) return false;
SERIALIZE_TAGGED((const uint8_t *)recipient, NEM_ADDRESS_SIZE);
SERIALIZE_U64(amount);
if (length) {
SERIALIZE_U32(sizeof(uint32_t) + sizeof(uint32_t) + length);
SERIALIZE_U32(encrypted ? 0x02 : 0x01);
SERIALIZE_TAGGED(payload, length);
} else {
SERIALIZE_U32(0);
}
if (mosaics) {
SERIALIZE_U32(mosaics);
}
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;
SERIALIZE_U32(sizeof(uint32_t) + sizeof(uint64_t) + identifier_length);
SERIALIZE_U32(identifier_length);
SERIALIZE_TAGGED((const uint8_t *)namespace, namespace_length);
SERIALIZE_TAGGED((const uint8_t *)mosaic, mosaic_length);
SERIALIZE_U64(quantity);
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,
(uint32_t)network << 24 | 1,
timestamp, signer, fee, deadline);
if (!ret) return false;
SERIALIZE_TAGGED(inner->buffer, inner->offset);
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, (uint32_t)network << 24 | 1,
timestamp, signer, fee, deadline);
if (!ret) return false;
char address[NEM_ADDRESS_SIZE + 1] = {0};
nem_get_address(inner->public_key, network, address);
uint8_t hash[SHA3_256_DIGEST_LENGTH] = {0};
keccak_256(inner->buffer, inner->offset, hash);
SERIALIZE_U32(sizeof(uint32_t) + SHA3_256_DIGEST_LENGTH);
SERIALIZE_TAGGED(hash, SHA3_256_DIGEST_LENGTH);
SERIALIZE_TAGGED((const uint8_t *)address, NEM_ADDRESS_SIZE);
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,
(uint32_t)network << 24 | 1, timestamp, signer, fee, deadline);
if (!ret) return false;
if (parent) {
SERIALIZE_TAGGED((const uint8_t *)rental_sink, NEM_ADDRESS_SIZE);
SERIALIZE_U64(rental_fee);
SERIALIZE_TAGGED((const uint8_t *)namespace, strlen(namespace));
SERIALIZE_TAGGED((const uint8_t *)parent, strlen(parent));
} else {
SERIALIZE_TAGGED((const uint8_t *)rental_sink, NEM_ADDRESS_SIZE);
SERIALIZE_U64(rental_fee);
SERIALIZE_TAGGED((const uint8_t *)namespace, strlen(namespace));
SERIALIZE_U32(0xffffffff);
}
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, (uint32_t)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 = {0};
memcpy(&state, ctx, sizeof(state));
SERIALIZE_U32(0);
SERIALIZE_TAGGED(signer, sizeof(ed25519_public_key));
SERIALIZE_U32(identifier_length);
SERIALIZE_TAGGED((const uint8_t *)namespace, namespace_length);
SERIALIZE_TAGGED((const uint8_t *)mosaic, mosaic_length);
SERIALIZE_TAGGED((const uint8_t *)description, strlen(description));
SERIALIZE_U32(4); // Number of properties
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;
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_TAGGED((const uint8_t *)levy_address, NEM_ADDRESS_SIZE);
SERIALIZE_U32(levy_identifier_length);
SERIALIZE_TAGGED((const uint8_t *)levy_namespace, levy_namespace_length);
SERIALIZE_TAGGED((const uint8_t *)levy_mosaic, levy_mosaic_length);
SERIALIZE_U64(levy_fee);
} else {
SERIALIZE_U32(0);
}
// Rewrite length
nem_write_u32(&state, ctx->offset - state.offset - sizeof(uint32_t));
SERIALIZE_TAGGED((const uint8_t *)creation_sink, NEM_ADDRESS_SIZE);
SERIALIZE_U64(creation_fee);
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,
(uint32_t)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;
SERIALIZE_U32(identifier_length);
SERIALIZE_TAGGED((const uint8_t *)namespace, namespace_length);
SERIALIZE_TAGGED((const uint8_t *)mosaic, mosaic_length);
SERIALIZE_U32(type);
SERIALIZE_U64(delta);
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,
(uint32_t)network << 24 | (relative_change ? 2 : 1), timestamp, signer,
fee, deadline);
if (!ret) return false;
SERIALIZE_U32(modifications);
return true;
}
bool nem_transaction_write_cosignatory_modification(
nem_transaction_ctx *ctx, uint32_t type,
const ed25519_public_key cosignatory) {
SERIALIZE_U32(sizeof(uint32_t) + sizeof(uint32_t) +
sizeof(ed25519_public_key));
SERIALIZE_U32(type);
SERIALIZE_TAGGED(cosignatory, sizeof(ed25519_public_key));
return true;
}
bool nem_transaction_write_minimum_cosignatories(nem_transaction_ctx *ctx,
int32_t relative_change) {
SERIALIZE_U32(sizeof(uint32_t));
SERIALIZE_U32((uint32_t)relative_change);
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,
(uint32_t)network << 24 | 1, timestamp, signer, fee, deadline);
if (!ret) return false;
SERIALIZE_U32(mode);
SERIALIZE_TAGGED(remote, sizeof(ed25519_public_key));
return true;
}