find_p2wsh_out: new helper to find a particular output.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-04-24 19:56:35 +09:30
parent bd081d219d
commit 6a54716b56
2 changed files with 28 additions and 6 deletions

View File

@ -5,21 +5,40 @@
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
#include <string.h> #include <string.h>
u32 find_p2sh_out(const struct bitcoin_tx *tx, u8 *redeemscript) static u32 find_output(const struct bitcoin_tx *tx, const u8 *scriptpubkey)
{ {
/* This is the scriptPubKey commit tx will have */
u8 *p2sh = scriptpubkey_p2sh(NULL, redeemscript);
u32 i; u32 i;
for (i = 0; i < tx->output_count; i++) { for (i = 0; i < tx->output_count; i++) {
if (tx->output[i].script_length != tal_count(p2sh)) if (tx->output[i].script_length != tal_count(scriptpubkey))
continue; continue;
if (memcmp(tx->output[i].script, p2sh, tal_count(p2sh)) == 0) if (memcmp(tx->output[i].script, scriptpubkey, tal_count(scriptpubkey)) == 0)
break; break;
} }
/* FIXME: Return failure! */ /* FIXME: Return failure! */
if (i == tx->output_count) if (i == tx->output_count)
errx(1, "No matching output in tx"); errx(1, "No matching output in tx");
return i;
}
u32 find_p2sh_out(const struct bitcoin_tx *tx, const u8 *redeemscript)
{
/* This is the scriptPubKey commit tx will have */
u8 *p2sh = scriptpubkey_p2sh(NULL, redeemscript);
u32 i;
i = find_output(tx, p2sh);
tal_free(p2sh); tal_free(p2sh);
return i; return i;
} }
u32 find_p2wsh_out(const struct bitcoin_tx *tx, const u8 *witnessscript)
{
/* This is the scriptPubKey commit tx will have */
u8 *p2wsh = scriptpubkey_p2wsh(NULL, witnessscript);
u32 i;
i = find_output(tx, p2wsh);
tal_free(p2wsh);
return i;
}

View File

@ -8,5 +8,8 @@ struct bitcoin_tx;
/* Normally we'd simply remember which output of the anchor or commit /* Normally we'd simply remember which output of the anchor or commit
* tx is the one which pays to this script. But for these examples, * tx is the one which pays to this script. But for these examples,
* we have to figure it out by recreating the output and matching. */ * we have to figure it out by recreating the output and matching. */
u32 find_p2sh_out(const struct bitcoin_tx *tx, u8 *redeemscript); u32 find_p2sh_out(const struct bitcoin_tx *tx, const u8 *redeemscript);
/* Similar routine for finding a specific p2wsh output. */
u32 find_p2wsh_out(const struct bitcoin_tx *tx, const u8 *witnessscript);
#endif /* LIGHTNING_FIND_P2SH_OUT_H */ #endif /* LIGHTNING_FIND_P2SH_OUT_H */