diff --git a/bitcoin/script.c b/bitcoin/script.c index eced41e44..f89bde4ff 100644 --- a/bitcoin/script.c +++ b/bitcoin/script.c @@ -476,3 +476,13 @@ u8 **bitcoin_witness_secret(const tal_t *ctx, return witness; } + +bool scripteq(const u8 *s1, size_t s1len, const u8 *s2, size_t s2len) +{ + memcheck(s1, s1len); + memcheck(s2, s2len); + + if (s1len != s2len) + return false; + return memcmp(s1, s2, s1len) == 0; +} diff --git a/bitcoin/script.h b/bitcoin/script.h index 6a918610b..ae78d8cdc 100644 --- a/bitcoin/script.h +++ b/bitcoin/script.h @@ -89,4 +89,7 @@ u8 **bitcoin_witness_secret(const tal_t *ctx, /* Is this a pay to script hash? */ bool is_p2sh(const u8 *script, size_t script_len); +/* Are these two scripts equal? */ +bool scripteq(const u8 *s1, size_t s1len, const u8 *s2, size_t s2len); + #endif /* LIGHTNING_BITCOIN_SCRIPT_H */ diff --git a/find_p2sh_out.c b/find_p2sh_out.c index f03f04843..f3f74a072 100644 --- a/find_p2sh_out.c +++ b/find_p2sh_out.c @@ -10,9 +10,10 @@ static u32 find_output(const struct bitcoin_tx *tx, const u8 *scriptpubkey) u32 i; for (i = 0; i < tx->output_count; i++) { - if (tx->output[i].script_length != tal_count(scriptpubkey)) - continue; - if (memcmp(tx->output[i].script, scriptpubkey, tal_count(scriptpubkey)) == 0) + if (scripteq(tx->output[i].script, + tx->output[i].script_length, + scriptpubkey, + tal_count(scriptpubkey))) break; } /* FIXME: Return failure! */