Commit 8bf438d0 authored by xpetrak2's avatar xpetrak2
Browse files

Declaration of order, trade and first trade proto

New message type 'encrypted'
Encrypted payloads and their types
Order internal structure definition
Trade internal structure definition
Declaration of the first trading protocol 'basic'
parent f1c49e7f
......@@ -14,11 +14,14 @@ src_coincerd_SOURCES = \
src/json_parser.c src/json_parser.h \
src/linkedlist.c src/linkedlist.h \
src/log.c src/log.h \
src/market.h \
src/neighbours.c src/neighbours.h \
src/p2p.c src/p2p.h \
src/paths.c src/paths.h \
src/peers.c src/peers.h \
src/routing.c src/routing.h
src/routing.c src/routing.h \
src/trade.c src/trade.h \
src/trade_basic.c src/trade_basic.h
src_coincerd_CFLAGS = $(AM_CFLAGS) $(JANSSON_CFLAGS) $(LIBEVENT_CFLAGS) $(LIBSODIUM_CFLAGS)
src_coincerd_LDADD = $(AM_LDADD) $(JANSSON_LIBS) $(LIBEVENT_LIBS) $(LIBSODIUM_LIBS)
......
......@@ -24,6 +24,46 @@
#include "daemon_messages.h"
#include "hosts.h"
#include "log.h"
#include "trade.h"
#include "trade_basic.h"
static int create_trade_execution_basic(trade_execution_basic_t **basic,
const trade_t *trade);
/**
* Create encrypted and store it in a message.
*
* @param message Store encrypted in this message.
* @param encrypted_payload Encrypted data.
*
* @return 0 Successfully created.
* @return 1 Failure.
*/
int create_encrypted(message_t *message, const char *encrypted_payload)
{
encrypted_t *encrypted;
char *payload;
encrypted = (encrypted_t *) malloc(sizeof(encrypted_t));
if (!encrypted) {
log_error("Creating encrypted");
return 1;
}
payload = (char *) malloc(strlen(encrypted_payload + 1) * sizeof(char));
if (!payload) {
log_error("Creating encrypted payload");
free(encrypted);
return 1;
}
strcpy(payload, encrypted_payload);
encrypted->payload = payload;
message->body.type = ENCRYPTED;
message->body.data = encrypted;
return 0;
}
/**
* Create a p2p.bye and store it in a message.
......@@ -196,6 +236,171 @@ int create_p2p_route_sol(message_t *message, const unsigned char *target)
return 0;
}
/**
* Create trade.execution payload.
*
* @param execution Store data to this trade.execution.
* @param trade Get data from this trade.
*
* @return 0 Successfully created.
* @return 1 Failure.
*/
int create_trade_execution(trade_execution_t **execution, const trade_t *trade)
{
int ret = 1;
*execution = malloc(sizeof(trade_execution_t));
if (!*execution) {
log_error("Creating trade.execution");
return 1;
}
memcpy((*execution)->order, trade->order->id, SHA3_256_SIZE);
switch (trade->type) {
case TT_BASIC:
ret = create_trade_execution_basic(&(*execution)->data,
trade);
}
if (ret) {
free(execution);
}
return ret;
}
/**
* Create trade.execution of type basic.
*
* @param basic Basic execution data holder to be dynamically
* allocated and initialized in here.
* @param trade Trade to create trade.execution for.
*
* @return 0 Successfully created.
* @return 1 Failure.
*/
static int create_trade_execution_basic(trade_execution_basic_t **basic,
const trade_t *trade)
{
trade_execution_basic_t *exec_data;
char *script;
trade_basic_t *trade_data;
trade_data = (trade_basic_t *) trade->data;
exec_data = (trade_execution_basic_t *) malloc(
sizeof(trade_execution_basic_t));
if (!exec_data) {
log_error("Creating trade execution data");
return 1;
}
*basic = exec_data;
switch (trade->step) {
case TS_COMMITMENT:
memcpy(exec_data->commitment,
trade_data->my_commitment,
SHA3_256_SIZE);
break;
case TS_KEY_AND_COMMITTED_EXCHANGE:
memcpy(exec_data->pubkey,
trade->my_keypair.public_key,
PUBLIC_KEY_SIZE);
memcpy(exec_data->committed,
trade_data->my_committed,
TRADE_BASIC_COMMITTED_SIZE);
if (!trade_data->my_script) {
break;
}
case TS_SCRIPT_ORIGIN:
script = (char *) malloc(
(strlen(trade_data->my_script) + 1) *
sizeof(char));
if (!script) {
log_error("Script for trade.execution message");
return 1;
}
strcpy(script, trade_data->my_script);
exec_data->script = script;
memcpy(exec_data->hx, trade_data->hx, RIPEMD_160_SIZE);
break;
case TS_SCRIPT_RESPONSE:
script = (char *) malloc(
(strlen(trade_data->my_script) + 1) *
sizeof(char));
if (!script) {
log_error("Script for trade.execution message");
return 1;
}
strcpy(script, trade_data->my_script);
exec_data->script = script;
break;
default:
log_error("Non-existent step for trade.execution "
"creation");
return 1;
}
return 0;
}
/**
* Create trade.proposal payload.
*
* @param trade_proposal trade.proposal to be created.
* @param trade trade.proposal of this trade.
*
* @return 0 Successfully created.
* @return 1 Failure.
*/
int create_trade_proposal(trade_proposal_t **trade_proposal,
const trade_t *trade)
{
trade_proposal_t *proposal;
proposal = (trade_proposal_t *) malloc(sizeof(trade_proposal_t));
if (!proposal) {
log_error("Creating trade.proposal");
return 1;
}
*trade_proposal = proposal;
trade_proposal_init(proposal, trade);
return 0;
}
/**
* Create trade.reject payload.
*
* @param trade_reject trade.reject to be created.
* @param order_id trade.reject's order ID.
*
* @return 0 Successfully created.
* @return 1 Failure.
*/
int create_trade_reject(trade_reject_t **trade_reject,
const unsigned char *order_id)
{
trade_reject_t *reject;
reject = (trade_reject_t *) malloc(sizeof(trade_reject_t));
if (!reject) {
log_error("Creating trade.reject");
return 1;
}
*trade_reject = reject;
memcpy(reject->order, order_id, SHA3_256_SIZE);
return 0;
}
/**
* Safely delete a message.
*
......@@ -203,18 +408,25 @@ int create_p2p_route_sol(message_t *message, const unsigned char *target)
*/
void message_delete(message_t *message)
{
message_body_t *body = &message->body;
void *data = message->body.data;
if (!data) {
return;
}
switch (body->type) {
switch (message->body.type) {
case ENCRYPTED:
free(((encrypted_t *) data)->payload);
free(data);
case P2P_BYE:
break;
case P2P_HELLO:
free(((p2p_hello_t *) body->data)->client);
free(body->data);
free(((p2p_hello_t *) data)->client);
free(data);
break;
case P2P_PEERS_ADV:
free(((p2p_peers_adv_t *) body->data)->addresses);
free(body->data);
free(((p2p_peers_adv_t *) data)->addresses);
free(data);
break;
case P2P_PEERS_SOL:
break;
......@@ -225,9 +437,27 @@ void message_delete(message_t *message)
case P2P_ROUTE_ADV:
break;
case P2P_ROUTE_SOL:
free(body->data);
free(data);
break;
default:
break;
}
}
/**
* Safely delete a message payload.
*
* @param type Type of the message payload.
* @param data Payload's data.
*/
void payload_delete(enum payload_type type, void *data)
{
switch (type) {
case TRADE_PROPOSAL:
free(data);
break;
case TRADE_REJECT:
free(data);
break;
}
}
......@@ -24,12 +24,16 @@
#include "crypto.h"
#include "linkedlist.h"
#include "trade.h"
/** Current version of coincer daemon protocol. */
#define PROTOCOL_VERSION 1
/** Types of daemon messages. */
enum message_type {
/* encrypted */
ENCRYPTED, /**< Encrypted message inside. */
/* p2p */
P2P_BYE, /**< Peer departure announcement. */
P2P_HELLO, /**< Initial message between two neighbours. */
P2P_PEERS_ADV, /**< Known hosts addresses advertisement.*/
......@@ -40,6 +44,22 @@ enum message_type {
P2P_ROUTE_SOL /**< Peer reachability solicitation. */
};
/**
* Types of encrypted payloads.
*/
enum payload_type {
TRADE_EXECUTION, /**< Trade execution of a certain trade step. */
TRADE_PROPOSAL, /**< Trade proposal. */
TRADE_REJECT /**< Rejection of a trading proposal. */
};
/**
* encrypted message data holder.
*/
typedef struct s_encrypted {
char *payload; /**< Encrypted message content. */
} encrypted_t;
/**
* p2p.hello data holder.
*/
......@@ -86,6 +106,8 @@ typedef struct s_message {
unsigned char sig[SIGNATURE_SIZE]; /**< Signature. */
} message_t;
int create_encrypted(message_t *message, const char *encrypted_payload);
int create_p2p_bye(message_t *message);
int create_p2p_hello(message_t *message, unsigned short port);
int create_p2p_peers_adv(message_t *message, const linkedlist_t *hosts);
......@@ -95,6 +117,14 @@ int create_p2p_pong(message_t *message);
int create_p2p_route_adv(message_t *message);
int create_p2p_route_sol(message_t *message, const unsigned char *target);
int create_trade_execution(trade_execution_t **execution,
const trade_t *trade);
int create_trade_proposal(trade_proposal_t **proposal,
const trade_t *trade);
int create_trade_reject(trade_reject_t **trade_reject,
const unsigned char *order_id);
void message_delete(message_t *message);
void payload_delete(enum payload_type type, void *data);
#endif /* DAEMON_MESSAGES_H */
/*
* Coincer
* Copyright (C) 2017-2018 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MARKET_H
#define MARKET_H
#include "crypto.h"
#include "linkedlist.h"
#include "peers.h"
/** Order that does not belong to us. */
#define ORDER_FOREIGN 0x01
/** Order that is being traded */
#define ORDER_TRADING 0x02
typedef struct s_order {
unsigned char id[SHA3_256_SIZE];
int flags;
union owner {
peer_t *cp;
identity_t *me;
} owner;
linkedlist_t blacklist;
linkedlist_node_t *node;
} order_t;
#endif /* MARKET_H */
/*
* Coincer
* Copyright (C) 2017-2018 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "crypto.h"
#include "trade.h"
#include "trade_basic.h"
/**
* Delete trade.execution message.
*
* @param trade_execution The message.
* @param type Trading type of the trade.execution.
* @param step trade.execution's step.
*/
void trade_execution_delete(trade_execution_t *trade_execution,
enum trade_type type,
enum trade_step step)
{
switch (type) {
case TT_BASIC:
trade_execution_basic_delete(trade_execution->data,
step);
break;
}
free(trade_execution);
}
/**
* Initialize trade.proposal of a trade.
*
* @param trade_proposal Initialize this trade.proposal.
* @param trade Get data from this trade.
*/
void trade_proposal_init(trade_proposal_t *trade_proposal, const trade_t *trade)
{
trade_proposal->protocol = trade->type;
memcpy(trade_proposal->order, trade->order->id, SHA3_256_SIZE);
memset(trade_proposal->commitment, 0x0, SHA3_256_SIZE);
switch (trade->type) {
case TT_BASIC:
trade_proposal_basic_init(trade_proposal, trade->data);
}
}
/*
* Coincer
* Copyright (C) 2017-2018 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TRADE_H
#define TRADE_H
#include "crypto.h"
#include "linkedlist.h"
#include "market.h"
#include "peers.h"
/**
* Trading protocol type.
*/
enum trade_type {
TT_BASIC
};
/**
* Step of a trade.
*/
enum trade_step {
TS_PROPOSAL, /**< Received or sent a trade.proposal. */
TS_COMMITMENT, /**< Received or sent hashed 'committed'. */
TS_KEY_AND_COMMITTED_EXCHANGE, /**< Pubkey and 'committed' exchange. */
TS_SCRIPT_ORIGIN, /**< Received or sent the first script. */
TS_SCRIPT_RESPONSE, /**< Received or sent the second script. */
TS_COINS_COMMITMENT, /**< Committing coins into a blackchain. */
/** Waiting for the counterparty to commit their coins. */
TS_COINS_CP_COMMITMENT,
TS_COINS_CLAIM, /** Coins claim. */
TS_DONE /** Trade successfully completed. */
};
/**
* Trade data holder.
*/
typedef struct s_trade {
order_t *order; /**< Trade refering to this order. */
enum trade_type type; /**< Type of the trade. */
enum trade_step step; /**< Current step of the trade. */
identity_t *my_identity; /**< Process the trade under this identity. */
keypair_t my_keypair; /**< Our trading keypair. */
/** Counterparty's message sending identifier. */
unsigned char cp_identifier[PUBLIC_KEY_SIZE];
/** Counterparty's trading public key. */
unsigned char cp_pubkey[PUBLIC_KEY_SIZE];
void *data; /**< Trade data. */
linkedlist_node_t *node; /**< Trade's node in the LL of trades. */
} trade_t;
/**
* trade.execution data holder.
*/
typedef struct s_trade_execution {
/** trade.execution related to this order. */
unsigned char order[SHA3_256_SIZE];
/** trade.execution's data. */
void *data;
} trade_execution_t;
/**
* trade.proposal data holder.
*/
typedef struct s_trade_proposal {
enum trade_type protocol; /**< Type of the trading protocol. */
unsigned char order[SHA3_256_SIZE]; /**< Order id. */
unsigned char commitment[SHA3_256_SIZE]; /**< Optional commitment. */
} trade_proposal_t;
/**
* trade.reject data holder.
*/
typedef struct s_trade_reject {
unsigned char order[SHA3_256_SIZE]; /**< Order id. */
} trade_reject_t;
void trade_execution_delete(trade_execution_t *data,
enum trade_type type,
enum trade_step step);
void trade_proposal_init(trade_proposal_t *trade_proposal,
const trade_t *trade);
#endif /* TRADE_H */
/*
* Coincer
* Copyright (C) 2017-2018 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "crypto.h"
#include "global_state.h"
#include "json_parser.h"
#include "trade.h"
#include "trade_basic.h"
/**
* Delete trade.execution of a basic trade.
*
* @param execution The execution to be deleted.
* @param step The execution's step.
*/
void trade_execution_basic_delete(trade_execution_basic_t *execution,
enum trade_step step)
{
if (step == TS_SCRIPT_ORIGIN || step == TS_SCRIPT_RESPONSE ||
step == TS_KEY_AND_COMMITTED_EXCHANGE) {
if (execution->script) {
free(execution->script);
}
}
}
/**
* Initialize trade.proposal of a basic trade.
*
* @param trade_proposal Initialize this trade.proposal.
* @param trade_data Get trade data from this.
*/
void trade_proposal_basic_init(trade_proposal_t *trade_proposal,
const trade_basic_t *trade_data)
{
memcpy(trade_proposal->commitment,
trade_data->my_commitment,
SHA3_256_SIZE);
}
/*
* Coincer
* Copyright (C) 2017-2018 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,