Browse Source

bitcoin_script: wean entirely off protobuf types.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 10 years ago
parent
commit
316f29cb69
  1. 21
      bitcoin_script.c
  2. 6
      bitcoin_script.h
  3. 5
      commit_tx.c
  4. 2
      open-anchor-sig.c

21
bitcoin_script.c

@ -166,19 +166,19 @@ u8 *scriptsig_pay_to_pubkeyhash(const tal_t *ctx,
}
/* Is this a normal pay to pubkey hash? */
bool is_pay_to_pubkey_hash(const ProtobufCBinaryData *script)
bool is_pay_to_pubkey_hash(const u8 *script, size_t script_len)
{
if (script->len != 25)
if (script_len != 25)
return false;
if (script->data[0] != OP_DUP)
if (script[0] != OP_DUP)
return false;
if (script->data[1] != OP_HASH160)
if (script[1] != OP_HASH160)
return false;
if (script->data[2] != OP_PUSHBYTES(20))
if (script[2] != OP_PUSHBYTES(20))
return false;
if (script->data[23] != OP_EQUALVERIFY)
if (script[23] != OP_EQUALVERIFY)
return false;
if (script->data[24] != OP_CHECKSIG)
if (script[24] != OP_CHECKSIG)
return false;
return true;
}
@ -191,15 +191,12 @@ u8 *bitcoin_redeem_revocable(const tal_t *ctx,
const struct pubkey *mykey,
u32 locktime,
const struct pubkey *theirkey,
const Sha256Hash *revocation_hash)
const struct sha256 *rhash)
{
u8 *script = tal_arr(ctx, u8, 0);
struct sha256 rhash;
u8 rhash_ripemd[RIPEMD160_DIGEST_LENGTH];
le32 locktime_le = cpu_to_le32(locktime);
proto_to_sha256(revocation_hash, &rhash);
/* If there are two args: */
add_op(&script, OP_DEPTH);
add_op(&script, OP_1SUB);
@ -212,7 +209,7 @@ u8 *bitcoin_redeem_revocable(const tal_t *ctx,
add_op(&script, OP_IF);
/* Must hash to revocation_hash, and be signed by them. */
RIPEMD160(rhash.u.u8, sizeof(rhash.u), rhash_ripemd);
RIPEMD160(rhash->u.u8, sizeof(rhash->u), rhash_ripemd);
add_op(&script, OP_HASH160);
add_push_bytes(&script, rhash_ripemd, sizeof(rhash_ripemd));
add_op(&script, OP_EQUALVERIFY);

6
bitcoin_script.h

@ -2,11 +2,11 @@
#define LIGHTNING_BITCOIN_SCRIPT_H
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include "lightning.pb-c.h"
struct bitcoin_address;
struct pubkey;
struct signature;
struct sha256;
/* tal_count() gives the length of the script. */
u8 *bitcoin_redeem_2of2(const tal_t *ctx,
@ -24,7 +24,7 @@ u8 *bitcoin_redeem_revocable(const tal_t *ctx,
const struct pubkey *mykey,
u32 locktime,
const struct pubkey *theirkey,
const Sha256Hash *revocation_hash);
const struct sha256 *revocation_hash);
/* Create an output script using p2sh for this redeem script. */
u8 *scriptpubkey_p2sh(const tal_t *ctx, const u8 *redeemscript);
@ -39,6 +39,6 @@ u8 *scriptsig_pay_to_pubkeyhash(const tal_t *ctx,
const struct signature *sig);
/* Is this a normal pay to pubkey hash? */
bool is_pay_to_pubkey_hash(const ProtobufCBinaryData *script);
bool is_pay_to_pubkey_hash(const u8 *script, size_t script_len);
#endif /* LIGHTNING_BITCOIN_SCRIPT_H */

5
commit_tx.c

@ -4,6 +4,7 @@
#include "bitcoin_script.h"
#include "permute_tx.h"
#include "pubkey.h"
#include "pkt.h"
struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
OpenChannel *ours,
@ -14,6 +15,7 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
struct bitcoin_tx *tx;
const u8 *redeemscript;
struct pubkey ourkey, theirkey;
struct sha256 redeem;
/* Now create commitment tx: one input, two outputs. */
tx = bitcoin_tx(ctx, 1, 2);
@ -26,12 +28,13 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
return tal_free(tx);
if (!proto_to_pubkey(theirs->anchor->pubkey, &theirkey))
return tal_free(tx);
proto_to_sha256(ours->revocation_hash, &redeem);
/* First output is a P2SH to a complex redeem script (usu. for me) */
redeemscript = bitcoin_redeem_revocable(tx, &ourkey,
ours->locktime_seconds,
&theirkey,
ours->revocation_hash);
&redeem);
tx->output[0].script = scriptpubkey_p2sh(tx, redeemscript);
tx->output[0].script_length = tal_count(tx->output[0].script);

2
open-anchor-sig.c

@ -35,7 +35,7 @@ static u8 *tx_scriptsig(const tal_t *ctx,
if (!sig)
return NULL;
if (!is_pay_to_pubkey_hash(&input->subscript))
if (!is_pay_to_pubkey_hash(input->subscript.data, input->subscript.len))
errx(1, "FIXME: Don't know how to handle input");
bitcoin_address(pubkey, &addr);
return scriptsig_pay_to_pubkeyhash(ctx, &addr, sig);

Loading…
Cancel
Save