diff --git a/bitcoin/script.c b/bitcoin/script.c index 675340471..108e17c09 100644 --- a/bitcoin/script.c +++ b/bitcoin/script.c @@ -368,7 +368,7 @@ u8 *p2wpkh_scriptcode(const tal_t *ctx, const struct pubkey *key) return script; } -bool is_p2pkh(const u8 *script) +bool is_p2pkh(const u8 *script, struct bitcoin_address *addr) { size_t script_len = tal_len(script); @@ -384,10 +384,12 @@ bool is_p2pkh(const u8 *script) return false; if (script[24] != OP_CHECKSIG) return false; + if (addr) + memcpy(addr, script+3, 20); return true; } -bool is_p2sh(const u8 *script) +bool is_p2sh(const u8 *script, struct ripemd160 *addr) { size_t script_len = tal_len(script); @@ -399,10 +401,12 @@ bool is_p2sh(const u8 *script) return false; if (script[22] != OP_EQUAL) return false; + if (addr) + memcpy(addr, script+2, 20); return true; } -bool is_p2wsh(const u8 *script) +bool is_p2wsh(const u8 *script, struct sha256 *addr) { size_t script_len = tal_len(script); @@ -412,10 +416,12 @@ bool is_p2wsh(const u8 *script) return false; if (script[1] != OP_PUSHBYTES(sizeof(struct sha256))) return false; + if (addr) + memcpy(addr, script+2, sizeof(struct sha256)); return true; } -bool is_p2wpkh(const u8 *script) +bool is_p2wpkh(const u8 *script, struct bitcoin_address *addr) { size_t script_len = tal_len(script); @@ -425,6 +431,8 @@ bool is_p2wpkh(const u8 *script) return false; if (script[1] != OP_PUSHBYTES(sizeof(struct ripemd160))) return false; + if (addr) + memcpy(addr, script+2, sizeof(*addr)); return true; } diff --git a/bitcoin/script.h b/bitcoin/script.h index 5752bc9f6..d0cc224ec 100644 --- a/bitcoin/script.h +++ b/bitcoin/script.h @@ -125,17 +125,17 @@ u8 *bitcoin_wscript_htlc_tx(const tal_t *ctx, const struct pubkey *revocation_pubkey, const struct pubkey *local_delayedkey); -/* Is this a pay to pubkey hash? */ -bool is_p2pkh(const u8 *script); +/* Is this a pay to pubkey hash? (extract addr if not NULL) */ +bool is_p2pkh(const u8 *script, struct bitcoin_address *addr); -/* Is this a pay to script hash? */ -bool is_p2sh(const u8 *script); +/* Is this a pay to script hash? (extract addr if not NULL) */ +bool is_p2sh(const u8 *script, struct ripemd160 *addr); -/* Is this (version 0) pay to witness script hash? */ -bool is_p2wsh(const u8 *script); +/* Is this (version 0) pay to witness script hash? (extract addr if not NULL) */ +bool is_p2wsh(const u8 *script, struct sha256 *addr); -/* Is this (version 0) pay to witness pubkey hash? */ -bool is_p2wpkh(const u8 *script); +/* Is this (version 0) pay to witness pubkey hash? (extract addr if not NULL) */ +bool is_p2wpkh(const u8 *script, struct bitcoin_address *addr); /* Are these two scripts equal? */ bool scripteq(const tal_t *s1, const tal_t *s2); diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index fcb4fdc48..c1799ffc3 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1620,8 +1620,8 @@ static void peer_got_shutdown(struct peer *peer, const u8 *msg) * * A receiving node SHOULD fail the connection if the `scriptpubkey` * is not one of those forms. */ - if (!is_p2pkh(scriptpubkey) && !is_p2sh(scriptpubkey) - && !is_p2wpkh(scriptpubkey) && !is_p2wsh(scriptpubkey)) { + if (!is_p2pkh(scriptpubkey, NULL) && !is_p2sh(scriptpubkey, NULL) + && !is_p2wpkh(scriptpubkey, NULL) && !is_p2wsh(scriptpubkey, NULL)) { char *str = tal_fmt(peer, "Bad shutdown scriptpubkey %s", tal_hex(peer, scriptpubkey)); peer_fail_permanent_str(peer, take(str)); diff --git a/onchaind/onchain.c b/onchaind/onchain.c index c6425a479..89277cd33 100644 --- a/onchaind/onchain.c +++ b/onchaind/onchain.c @@ -1086,7 +1086,7 @@ static int match_htlc_output(const struct bitcoin_tx *tx, u8 **htlc_scripts) { /* Must be a p2wsh output */ - if (!is_p2wsh(tx->output[outnum].script)) + if (!is_p2wsh(tx->output[outnum].script, NULL)) return -1; for (size_t i = 0; i < tal_count(htlc_scripts); i++) { diff --git a/wallet/wallet.c b/wallet/wallet.c index a2637d7a7..2df4edfe6 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -195,9 +195,9 @@ bool wallet_can_spend(struct wallet *w, const u8 *script, u32 i; /* If not one of these, can't be for us. */ - if (is_p2sh(script)) + if (is_p2sh(script, NULL)) *output_is_p2sh = true; - else if (is_p2wpkh(script)) + else if (is_p2wpkh(script, NULL)) *output_is_p2sh = false; else return false;