|
| 1 | +# The MIT License (MIT) |
| 2 | + |
| 3 | +# Copyright (c) 2021-2026 Krux contributors |
| 4 | + |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | + |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | + |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | + |
| 23 | +P2PKH_UNCOMPRESSED_HEADER = 27 |
| 24 | +P2PKH_HEADER = 31 |
| 25 | +P2SH_P2WPKH_HEADER = 35 |
| 26 | +P2WPKH_HEADER = 39 |
| 27 | +MESSAGE_MAGIC = b"\x18Bitcoin Signed Message:\n" |
| 28 | +RECID_OFFSET = P2PKH_HEADER - P2PKH_UNCOMPRESSED_HEADER |
| 29 | + |
| 30 | + |
| 31 | +def message_commitment(message): |
| 32 | + """Double-SHA256 commitment over the BIP-137 magic""" |
| 33 | + from embit import compact |
| 34 | + |
| 35 | + try: |
| 36 | + import uhashlib as hashlib |
| 37 | + except ImportError: |
| 38 | + import hashlib |
| 39 | + |
| 40 | + # BIP137 commitment message: |
| 41 | + # `SHA256(SHA256(MAGIC // varint // message))` |
| 42 | + varint = compact.to_bytes(len(message)) |
| 43 | + _message = MESSAGE_MAGIC + varint + message |
| 44 | + return hashlib.sha256(hashlib.sha256(_message).digest()).digest() |
| 45 | + |
| 46 | + |
| 47 | +def build_header(raw_sig, script_type, compressed=True): |
| 48 | + """Build header byte from raw signature and script_type""" |
| 49 | + # Avoid some unexpected header |
| 50 | + rsig = raw_sig[0] |
| 51 | + if not P2PKH_UNCOMPRESSED_HEADER <= rsig <= P2PKH_HEADER + 3: |
| 52 | + raise ValueError("Invalid sig header: %d" % rsig) |
| 53 | + |
| 54 | + # grab the 2 least significant bits as recId |
| 55 | + # and normalize with a minimum p2pkh (uncompressed) flag |
| 56 | + recid = (raw_sig[0] - P2PKH_UNCOMPRESSED_HEADER) & 3 |
| 57 | + |
| 58 | + if script_type == "p2sh-p2wpkh": |
| 59 | + return P2SH_P2WPKH_HEADER + recid |
| 60 | + if script_type == "p2wpkh": |
| 61 | + return P2WPKH_HEADER + recid |
| 62 | + |
| 63 | + # compressed=False is only meaningful for p2pkh |
| 64 | + return ( |
| 65 | + P2PKH_UNCOMPRESSED_HEADER |
| 66 | + + recid |
| 67 | + + (0 if not compressed and script_type == "p2pkh" else RECID_OFFSET) |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +def sign(message, key, derivation, script_type="p2pkh", compressed=True): |
| 72 | + """Sign a BIP137 `message` with `key` at `derivation` for some `script_type`""" |
| 73 | + commitment = message_commitment(message) |
| 74 | + raw_sig = key.sign_at(derivation, commitment) |
| 75 | + header = build_header(raw_sig, script_type, compressed) |
| 76 | + sig = bytes([header]) + raw_sig[1:] |
| 77 | + return (commitment, sig) |
0 commit comments