Browse Source

script: enhance is_p2sh/is_p2pkh/is_p2wsh/is_p2wpkh to extract addr.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
parent
commit
9ec5cb7ba2
  1. 16
      bitcoin/script.c
  2. 16
      bitcoin/script.h
  3. 4
      lightningd/peer_control.c
  4. 2
      onchaind/onchain.c
  5. 4
      wallet/wallet.c

16
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;
}

16
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);

4
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));

2
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++) {

4
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;

Loading…
Cancel
Save