Skip to content

add convert sha256 to string functions and update docs #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ You will need several development libraries:
To Build on Ubuntu 15.10
------------------------
Build protobuf-c dependency (>= 1.1.0):

```
sudo apt-get install libprotoc-dev
git clone https://github.com/protobuf-c/protobuf-c.git
Expand All @@ -21,7 +22,20 @@ make install
cd ../
```

Build libsodium:

```
wget https://download.libsodium.org/libsodium/releases/LATEST.tar.gz
tar zxvf LATEST.tar.gz
cd libsodium-1.0.10
./configure
make && make check
make install
cd ../
```

Clone lightning and initialize submodules:

```
git clone https://github.com/ElementsProject/lightning.git
cd lighting
Expand Down
18 changes: 18 additions & 0 deletions bitcoin/shadouble.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "shadouble.h"
#include <ccan/mem/mem.h>
#include <stdio.h>

void sha256_double(struct sha256_double *shadouble, const void *p, size_t len)
{
Expand All @@ -12,3 +13,20 @@ void sha256_double_done(struct sha256_ctx *shactx, struct sha256_double *res)
sha256_done(shactx, &res->sha);
sha256(&res->sha, &res->sha, sizeof(res->sha));
}

char *sha256_str(const tal_t *ctx, struct sha256 *sha)
{
const size_t size = sizeof(struct sha256);
char *hex = tal_arr(ctx, char, size * 2 + 1);
size_t i = 0;
for (; i < size; i++) {
sprintf(hex + i*2, "%02x", sha->u.u8[size - i - 1]);
}
*(hex + size*2) = '\0';
return hex;
}

char *sha256_double_str(const tal_t *ctx, struct sha256_double *shadouble)
{
return sha256_str(ctx, &shadouble->sha);
}
5 changes: 5 additions & 0 deletions bitcoin/shadouble.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define LIGHTNING_BITCOIN_SHADOUBLE_H
#include "config.h"
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/tal/tal.h>

/* To explicitly distinguish between single sha and bitcoin's standard double */
struct sha256_double {
Expand All @@ -11,4 +12,8 @@ struct sha256_double {
void sha256_double(struct sha256_double *shadouble, const void *p, size_t len);

void sha256_double_done(struct sha256_ctx *sha256, struct sha256_double *res);

char *sha256_str(const tal_t *ctx, struct sha256 *sha);
char *sha256_double_str(const tal_t *ctx, struct sha256_double *shadouble);

#endif /* LIGHTNING_BITCOIN_SHADOUBLE_H */
9 changes: 4 additions & 5 deletions daemon/bitcoind.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,11 @@ static void process_transactions(struct bitcoin_cli *bcli)
bcli->output + blktok->start);
}
}
/* FIXME: log txid, blkid */

log_debug(bcli->dstate->base_log,
"txid %02x%02x%02x%02x..., conf %u, coinbase %u",
txid.sha.u.u8[0], txid.sha.u.u8[1],
txid.sha.u.u8[2], txid.sha.u.u8[3],
conf, is_coinbase);
"txid %s, conf %u, coinbase %u, blkhash %s",
sha256_double_str(bcli, &txid), conf, is_coinbase,
conf ? sha256_double_str(bcli, &blkhash) : "null");

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you allocate the strings off "bcli" (which will be freed when the command is done), you don't need to free the strings! In fact, you can just do:

log_debug(bcli->dstate->base_log,
              "txid %s, conf %u, coinbase %u, blkhash %s",
              sha256_double_str(bcli, &txid),
                          conf, is_coinbase, conf ? sha256_double_str(bcli, &blkhash) : "null");

Thanks!

cb(bcli->dstate, &txid, conf, is_coinbase,
conf ? &blkhash : NULL);
Expand Down