1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 22:38:08 +00:00

nem: Add transaction serialization methods

This commit is contained in:
Saleem Rashid 2017-05-30 13:21:58 +01:00 committed by Pavol Rusnak
parent 9888aab352
commit e1a511092a
3 changed files with 633 additions and 1 deletions

484
nem.c
View File

@ -22,10 +22,10 @@
#include "nem.h"
#include <stddef.h>
#include <string.h>
#include "base32.h"
#include "ed25519-keccak.h"
#include "macros.h"
#include "ripemd160.h"
#include "sha3.h"
@ -43,6 +43,57 @@ const char *nem_network_name(uint8_t network) {
}
}
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];
@ -102,3 +153,434 @@ bool nem_validate_address(const char *address, uint8_t 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;
}

127
nem.h
View File

@ -24,8 +24,16 @@
#define __NEM_H__
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "bip32.h"
#include "ed25519.h"
#define NEM_LEVY_PERCENTILE_DIVISOR 4
#define NEM_MAX_DIVISIBILITY 6
#define NEM_MAX_SUPPLY 9000000000
#define NEM_NETWORK_MAINNET 0x68
#define NEM_NETWORK_TESTNET 0x98
#define NEM_NETWORK_MIJIN 0x60
@ -33,6 +41,21 @@
#define NEM_ADDRESS_SIZE 40
#define NEM_ADDRESS_SIZE_RAW 25
#define NEM_TRANSACTION_TYPE_TRANSFER 0x0101
#define NEM_TRANSACTION_TYPE_AGGREGATE_MODIFICATION 0x1001
#define NEM_TRANSACTION_TYPE_MULTISIG_SIGNATURE 0x1002
#define NEM_TRANSACTION_TYPE_MULTISIG 0x1004
#define NEM_TRANSACTION_TYPE_PROVISION_NAMESPACE 0x2001
#define NEM_TRANSACTION_TYPE_MOSAIC_CREATION 0x4001
#define NEM_TRANSACTION_TYPE_MOSAIC_SUPPLY_CHANGE 0x4002
typedef struct {
ed25519_public_key public_key;
uint8_t *buffer;
size_t offset;
size_t size;
} nem_transaction_ctx;
const char *nem_network_name(uint8_t network);
void nem_get_address_raw(const ed25519_public_key public_key, uint8_t version, uint8_t *address);
@ -41,4 +64,108 @@ bool nem_get_address(const ed25519_public_key public_key, uint8_t version, char
bool nem_validate_address_raw(const uint8_t *address, uint8_t network);
bool nem_validate_address(const char *address, uint8_t network);
void nem_transaction_start(nem_transaction_ctx *ctx, const ed25519_public_key public_key, uint8_t *buffer, size_t size);
size_t nem_transaction_end(nem_transaction_ctx *ctx, const ed25519_secret_key private_key, ed25519_signature signature);
bool nem_transaction_write_common(nem_transaction_ctx *context,
uint32_t type,
uint32_t version,
uint32_t timestamp,
const ed25519_public_key signer,
uint64_t fee,
uint32_t deadline);
bool nem_transaction_create_transfer(nem_transaction_ctx *context,
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);
bool nem_transaction_write_mosaic(nem_transaction_ctx *ctx,
const char *namespace,
const char *mosaic,
uint64_t quantity);
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);
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);
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);
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);
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);
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);
bool nem_transaction_write_cosignatory_modification(nem_transaction_ctx *ctx,
uint32_t type,
const ed25519_public_key cosignatory);
bool nem_transaction_write_minimum_cosignatories(nem_transaction_ctx *ctx,
int32_t relative_change);
#endif

23
nem_serialize.h Normal file
View File

@ -0,0 +1,23 @@
#define serialize_u32(data) + sizeof(uint32_t)
#define serialize_u64(data) + sizeof(uint64_t)
#define serialize_write(data, length) + (length)
if (!nem_can_write(ctx, NEM_SERIALIZE)) {
return false;
}
#undef serialize_u32
#undef serialize_u64
#undef serialize_write
#define serialize_u32(data) nem_write_u32(ctx, (data));
#define serialize_u64(data) nem_write_u64(ctx, (data));
#define serialize_write(data, length) nem_write(ctx, (data), (length));
NEM_SERIALIZE
#undef serialize_u32
#undef serialize_u64
#undef serialize_write
#undef NEM_SERIALIZE