Push an integer the way CLN's add_number() (bitcoin/script.c) does.
(num)
| 104 | |
| 105 | |
| 106 | def _add_number(num): |
| 107 | """Push an integer the way CLN's add_number() (bitcoin/script.c) does.""" |
| 108 | if num == 0: |
| 109 | return bytes([0x00]) # OP_0 |
| 110 | if num <= 16: |
| 111 | return bytes([0x50 + num]) # OP_1 .. OP_16 |
| 112 | # Minimal little-endian, signed (to_self_delay never needs the sign byte). |
| 113 | length = (num.bit_length() + 7) // 8 |
| 114 | if num >> (length * 8 - 1) & 1: |
| 115 | length += 1 # high bit set: pad to stay positive |
| 116 | return bytes([length]) + num.to_bytes(length, 'little') |
| 117 | |
| 118 | |
| 119 | def _push_bytes(data): |
no test coverage detected