Skip to content

Commit

Permalink
First cut of a gateway.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Jul 28, 2014
1 parent 67cc7b9 commit 1708a1f
Show file tree
Hide file tree
Showing 15 changed files with 727 additions and 74 deletions.
13 changes: 8 additions & 5 deletions Makefile
@@ -1,13 +1,14 @@
PETTYCOIN_OBJS := block.o check_block.o check_tx.o difficulty.o shadouble.o timestamp.o gateways.o hash_tx.o pettycoin.o merkle_txs.o merkle_recurse.o tx_cmp.o genesis.o marshal.o hash_block.o prev_txhashes.o state.o tal_packet.o dns.o netaddr.o peer.o peer_cache.o pseudorand.o welcome.o log.o generating.o blockfile.o pending.o log_helper.o txhash.o signature.o proof.o chain.o features.o todo.o base58.o sync.o create_refs.o shard.o packet_io.o tx.o complain.o block_shard.o recv_block.o input_refs.o peer_wants.o inputhash.o tx_in_hashes.o merkle_hashes.o recv_tx.o reward.o recv_complain.o json.o jsonrpc.o getinfo.o ecode_names.o sendrawtransaction.c pettycoin_dir.o pkt_names.o
PETTYCOIN_GENERATE_OBJS := pettycoin-generate.o merkle_hashes.o merkle_recurse.o hash_tx.o tx_cmp.o shadouble.o marshal.o minimal_log.o timestamp.o tal_packet.o
PETTYCOIN_OBJS := block.o check_block.o check_tx.o difficulty.o shadouble.o timestamp.o gateways.o hash_tx.o pettycoin.o merkle_txs.o merkle_recurse.o tx_cmp.o genesis.o marshal.o hash_block.o prev_txhashes.o state.o tal_packet.o dns.o netaddr.o peer.o peer_cache.o pseudorand.o welcome.o log.o generating.o blockfile.o pending.o log_helper.o txhash.o signature.o proof.o chain.o features.o todo.o base58.o sync.o create_refs.o shard.o packet_io.o tx.o complain.o block_shard.o recv_block.o input_refs.o peer_wants.o inputhash.o tx_in_hashes.o merkle_hashes.o recv_tx.o reward.o recv_complain.o json.o jsonrpc.o getinfo.o ecode_names.o sendrawtransaction.c pettycoin_dir.o pkt_names.o hex.o
PETTYCOIN_GENERATE_OBJS := pettycoin-generate.o merkle_hashes.o merkle_recurse.o hash_tx.o tx_cmp.o shadouble.o marshal.o minimal_log.o timestamp.o tal_packet.o hex.o
MKGENESIS_OBJS := mkgenesis.o shadouble.o hash_block.o merkle_hashes.o merkle_recurse.o minimal_log.o
SIZES_OBJS := sizes.o
MKPRIV_OBJS := mkpriv.o
PETTYCOIN_TX_OBJS := pettycoin-tx.o base58.o create_tx.o marshal.o hash_tx.o minimal_log.o shadouble.o signature.o hash_block.o merkle_recurse.o json.o pettycoin_dir.o
PETTYCOIN_QUERY_OBJS := pettycoin-query.o json.o base58.o pettycoin_dir.o
PETTYCOIN_TX_OBJS := pettycoin-tx.o base58.o create_tx.o marshal.o hash_tx.o minimal_log.o shadouble.o signature.o hash_block.o merkle_recurse.o json.o pettycoin_dir.o hex.o
PETTYCOIN_QUERY_OBJS := pettycoin-query.o json.o base58.o pettycoin_dir.o hex.o
PETTY_ADDR_OBJS := petty-addr.o base58.o
PETTYCOIN_GATEWAY_OBJS := pettycoin-gateway.o hex.o json.o pettycoin_dir.o base58.o

BINS := pettycoin-generate mkgenesis pettycoin sizes mkpriv pettycoin-tx pettycoin-query petty-addr
BINS := pettycoin-generate mkgenesis pettycoin sizes mkpriv pettycoin-tx pettycoin-query petty-addr pettycoin-gateway
CCAN_OBJS := ccan-asort.o ccan-breakpoint.o ccan-tal.o ccan-tal-path.o ccan-tal-str.o ccan-take.o ccan-list.o ccan-str.o ccan-opt-helpers.o ccan-opt.o ccan-opt-parse.o ccan-opt-usage.o ccan-read_write_all.o ccan-htable.o ccan-io-io.o ccan-io-poll.o ccan-timer.o ccan-time.o ccan-noerr.o ccan-hash.o ccan-isaac64.o ccan-net.o ccan-err.o ccan-tal-grab_file.o
CCANDIR=ccan/
VERSION:=$(shell git describe --dirty --always 2>/dev/null || echo Unknown)
Expand Down Expand Up @@ -47,6 +48,8 @@ sizes: $(SIZES_OBJS) $(CCAN_OBJS)

petty-addr: $(PETTY_ADDR_OBJS) $(CCAN_OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(PETTY_ADDR_OBJS) $(CCAN_OBJS) $(LDLIBS)
pettycoin-gateway: $(PETTYCOIN_GATEWAY_OBJS) $(CCAN_OBJS)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(PETTYCOIN_GATEWAY_OBJS) $(CCAN_OBJS) $(LDLIBS)

genesis.c: mkgenesis
./mkgenesis $(TEST_GENESIS_DIFFICULTY) $(TEST_GENESIS_TIMESTAMP) $(TEST_GENESIS_NONCE) > $@.tmp; STATUS=$$?; if [ $$STATUS = 0 ]; then mv $@.tmp $@; else rm -f $@.tmp; exit $$STATUS; fi
Expand Down
50 changes: 50 additions & 0 deletions hex.c
@@ -0,0 +1,50 @@
#include "hex.h"
#include <ccan/short_types/short_types.h>
#include <stdio.h>

static bool char_to_hex(u8 *val, char c)
{
if (c >= '0' && c <= '9') {
*val = c - '0';
return true;
}
if (c >= 'a' && c <= 'f') {
*val = c - 'a' + 10;
return true;
}
if (c >= 'A' && c <= 'F') {
*val = c - 'A' + 10;
return true;
}
return false;
}

bool from_hex(const char *str, size_t slen, void *buf, size_t bufsize)
{
u8 v1, v2;
u8 *p = buf;

while (slen > 1) {
if (!char_to_hex(&v1, str[0]) || !char_to_hex(&v2, str[1]))
return false;
if (!bufsize)
return false;
*(p++) = (v1 << 4) | v2;
str += 2;
slen -= 2;
bufsize--;
}
return slen == 0 && bufsize == 0;
}

char *to_hex(const tal_t *ctx, const void *buf, size_t bufsize)
{
const u8 *p = buf;
size_t i;
char *hex = tal_arr(ctx, char, bufsize * 2 + 1);

for (i = 0; i < bufsize; i++)
sprintf(hex + i*2, "%02x", p[i]);

return hex;
}
13 changes: 13 additions & 0 deletions hex.h
@@ -0,0 +1,13 @@
#ifndef PETTYCOIN_HEX_H
#define PETTYCOIN_HEX_H
#include "config.h"
#include <ccan/tal/tal.h>
#include <stdbool.h>

/* Unpack slen hex digits into buf; fail on bad char or not exact length */
bool from_hex(const char *str, size_t slen, void *buf, size_t bufsize);

/* Allocate hex string off ctx */
char *to_hex(const tal_t *ctx, const void *buf, size_t bufsize);

#endif /* PETTYCOIN_HEX_H */
8 changes: 2 additions & 6 deletions json.c
@@ -1,5 +1,6 @@
/* JSON core and helpers */
#include "base58.h"
#include "hex.h"
#include "json.h"
#include "protocol.h"
#include <assert.h>
Expand Down Expand Up @@ -276,12 +277,7 @@ void json_add_null(char **result, const char *fieldname)
void json_add_hex(char **result, const char *fieldname, const void *data,
size_t len)
{
char *hex = tal_arr(*result, char, len * 2 + 1);
const u8 *p = data;
size_t i;

for (i = 0; i < len; i++)
sprintf(hex + i*2, "%02x", p[i]);
char *hex = to_hex(*result, data, len);

json_add_string(result, fieldname, hex);
tal_free(hex);
Expand Down

0 comments on commit 1708a1f

Please sign in to comment.