smite: add is_standard_shutdown_script helper - #186
Conversation
f33051b to
22217d9
Compare
NishantBansal2003
left a comment
There was a problem hiding this comment.
Thanks! I was about to add this as a follow-up to #185, but it looks like I don’t have to now
22217d9 to
220b1bf
Compare
220b1bf to
847c2f1
Compare
NishantBansal2003
left a comment
There was a problem hiding this comment.
Mostly, it looks good. Just a few small comments. I'm not sure how this follows the merge with #192, but if it's merged before, I'll add the updated feature check there
| /// Returns `true` if `spk` is a standard `shutdown` scriptpubkey per BOLT 2: P2WPKH or P2WSH. | ||
| /// Negotiated `features` widen the accepted set. | ||
| /// | ||
| /// Legacy P2PKH/P2SH are rejected. A receiver may accept them for backward compatibility, but this |
There was a problem hiding this comment.
In the oracle, we verify what the peer accepted (and whether it was valid), and then we check our side to ensure that the target is actually sending that.
So, when verifying a target-accepted open_channel, I need it to pass even if it contains the legacy script. However, for our received accept_channel, I need to ensure that it does not contain legacy scripts
There was a problem hiding this comment.
I didn't address this yet in 63fed11. I think we want to handle this in a similar way to enum Side.
|
|
||
| #[test] | ||
| #[allow(clippy::similar_names)] | ||
| fn is_standard_shutdown_script_rejects_legacy_accepts_witness_v0() { |
There was a problem hiding this comment.
nit: I think for testing semantic script types, we should use the bitcoin crate to create the scripts, eg: ScriptBuf::new_p2wpkh(&WPubkeyHash::all_zeros()).into_bytes();, but for testing non-standard cases, we can construct the raw bytes manually
There was a problem hiding this comment.
Mhh. If we used bitcoin::ScriptBuf to construct standard scripts via ScriptBuf::new_p2wpkh etc., I think we'd mostly be testing the bitcoin crate against itself, since is_standard_shutdown_script is implemented with bitcoin::Script::is_p2wpkh().
It would also make it less clear how we are testing. I think raw opcodes make it explicit which bytes we are testing. I think tests should be optimized for clear understanding of what we are testing how. This is why I think it's ok to use bitcoin::opcodes.
I think @morehouse mentioned something similar in a review, but I didn't find it.
dd42b6b to
63fed11
Compare
|
I rebased this PR on top of #192 since I don't expect it to change much. I didn't address all feedback yet, see #186 (comment) and TODO in the code + commit message. |
63fed11 to
6b8d367
Compare
|
TODO:
|
6b8d367 to
c62c850
Compare
|
c62c850: rebased on master (87ebab6), added f024116: fixed wrong usage of backticks in |
BOLT-02 specifies sender requirements for shutdown scripts. They must be witness v0 (P2WPKH, P2WSH) or following features must be negotiated: * `option_shutdown_anysegwit`: witness v1-v16 with a 2..=40 byte program * `option_simple_close`: `OP_RETURN` with a single minimal data push of 6..=80 bytes This applies to the `shutdown` and `closing_complete` messages, and the `upfront_shutdown_script` TLV in the `open_channel`, `open_channel2`, `accept_channel` and `accept_channel2` messages. This commit adds two helpers to catch targets that don't comply with the spec: is_standard_shutdown_script and is_acceptable_shutdown_script.
c62c850 to
f024116
Compare
| pub fn is_acceptable_shutdown_script(spk: &[u8], features: &Features) -> bool { | ||
| let script = Script::from_bytes(spk); | ||
| is_standard_shutdown_script(spk, features) || script.is_p2pkh() || script.is_p2sh() | ||
| } |
There was a problem hiding this comment.
We should add tests for is_acceptable_shutdown_script.
| // | ||
| // TODO: Oracle verification depends on if we're verifying a target's message or our own | ||
| // message. Legacy scripts MAY be accepted by receivers, but MUST NOT be sent. |
There was a problem hiding this comment.
| // | |
| // TODO: Oracle verification depends on if we're verifying a target's message or our own | |
| // message. Legacy scripts MAY be accepted by receivers, but MUST NOT be sent. |
Depends on #192.
From the commit message:
As per the note I added to the code, I'm not sure if the fuzzer should also reject legacy scripts, since a target must not send them.update: decided to reject them, see discussionI haven't wired this into existing code or #163 yet, but I thought the introduction of the helper might be worthwile to review itself, especially considering the question wrt legacy scripts.