|
11 | 11 | #include <script/script.h> |
12 | 12 | #include <serialize.h> |
13 | 13 | #include <span.h> |
| 14 | +#include <cstdint> |
14 | 15 |
|
15 | 16 | /** |
16 | 17 | * This saves us from making many heap allocations when serializing |
@@ -95,6 +96,159 @@ struct ScriptCompression |
95 | 96 | } |
96 | 97 | }; |
97 | 98 |
|
| 99 | +enum class ReconstructableScriptType: uint8_t { |
| 100 | + Unknown = 0x00, |
| 101 | + P2pkh = 0x01, |
| 102 | + P2pkEven = 0x02, |
| 103 | + P2pkOdd = 0x03, |
| 104 | + P2pkUncompressed = 0x04, |
| 105 | + P2sh = 0x06, |
| 106 | + P2wsh = 0x07, |
| 107 | + P2wpkh = 0x08, |
| 108 | + P2tr = 0x09, |
| 109 | +}; |
| 110 | + |
| 111 | +template <typename Stream> |
| 112 | +void Serialize(Stream& s, ReconstructableScriptType type) |
| 113 | +{ |
| 114 | + ::Serialize(s, static_cast<uint8_t>(type)); |
| 115 | +} |
| 116 | + |
| 117 | +struct ReconstructableScript { |
| 118 | + |
| 119 | + template<typename Stream> |
| 120 | + void Ser(Stream& s, const CScript& script) |
| 121 | + { |
| 122 | + if (script.IsPayToTaproot()) { |
| 123 | + ::Serialize(s, ReconstructableScriptType::P2tr); |
| 124 | + ::Serialize(s, MakeByteSpan(script).subspan(2, 32)); |
| 125 | + return; |
| 126 | + } |
| 127 | + if (script.IsPayToWitnessScriptHash()) { |
| 128 | + ::Serialize(s, ReconstructableScriptType::P2wsh); |
| 129 | + ::Serialize(s, MakeByteSpan(script).subspan(2, 32)); |
| 130 | + return; |
| 131 | + } |
| 132 | + if (script.size() == 22 && script[0] == OP_0 && script[1] == 20) { |
| 133 | + ::Serialize(s, ReconstructableScriptType::P2wpkh); |
| 134 | + ::Serialize(s, MakeByteSpan(script).subspan(2, 20)); |
| 135 | + return; |
| 136 | + } |
| 137 | + if (script.IsPayToScriptHash()) { |
| 138 | + ::Serialize(s, ReconstructableScriptType::P2sh); |
| 139 | + ::Serialize(s, MakeByteSpan(script).subspan(2, 20)); |
| 140 | + return; |
| 141 | + } |
| 142 | + if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160 |
| 143 | + && script[2] == 20 && script[23] == OP_EQUALVERIFY |
| 144 | + && script[24] == OP_CHECKSIG) { |
| 145 | + ::Serialize(s, ReconstructableScriptType::P2pkh); |
| 146 | + ::Serialize(s, MakeByteSpan(script).subspan(3, 20)); |
| 147 | + return; |
| 148 | + } |
| 149 | + if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG) { |
| 150 | + if (script[1] == 0x02) { |
| 151 | + ::Serialize(s, ReconstructableScriptType::P2pkEven); |
| 152 | + ::Serialize(s, MakeByteSpan(script).subspan(2, 32)); |
| 153 | + return; |
| 154 | + } |
| 155 | + if (script[1] == 0x03) { |
| 156 | + ::Serialize(s, ReconstructableScriptType::P2pkOdd); |
| 157 | + ::Serialize(s, MakeByteSpan(script).subspan(2, 32)); |
| 158 | + return; |
| 159 | + } |
| 160 | + } |
| 161 | + if (script.size() == 67 && script[0] == 65 && script[66] == OP_CHECKSIG && script[1] == 0x04) { |
| 162 | + ::Serialize(s, ReconstructableScriptType::P2pkUncompressed); |
| 163 | + ::Serialize(s, MakeByteSpan(script).subspan(2, 64)); |
| 164 | + return; |
| 165 | + } |
| 166 | + ::Serialize(s, ReconstructableScriptType::Unknown); |
| 167 | + ::Serialize(s, script); |
| 168 | + } |
| 169 | + |
| 170 | + template<typename Stream> |
| 171 | + void Unser(Stream& s, CScript& script) |
| 172 | + { |
| 173 | + uint8_t type; |
| 174 | + ::Unserialize(s, type); |
| 175 | + ReconstructableScriptType s_type{type}; |
| 176 | + switch (s_type) { |
| 177 | + case ReconstructableScriptType::P2tr: { |
| 178 | + prevector<32, unsigned char> x_only; |
| 179 | + ::Unserialize(s, std::span{x_only}); |
| 180 | + script.resize(34); |
| 181 | + script[0] = OP_1; |
| 182 | + script[1] = 32; |
| 183 | + memcpy(&script[2], x_only.data(), 32); |
| 184 | + break; |
| 185 | + } |
| 186 | + case ReconstructableScriptType::P2wsh: { |
| 187 | + uint256 hash; |
| 188 | + ::Unserialize(s, hash); |
| 189 | + script.resize(34); |
| 190 | + script[0] = OP_0; |
| 191 | + script[1] = 32; |
| 192 | + memcpy(&script[2], hash.data(), 32); |
| 193 | + break; |
| 194 | + } |
| 195 | + case ReconstructableScriptType::P2wpkh: { |
| 196 | + uint160 hash; |
| 197 | + ::Unserialize(s, hash); |
| 198 | + script.resize(22); |
| 199 | + script[0] = OP_0; |
| 200 | + script[1] = 20; |
| 201 | + memcpy(&script[2], hash.data(), 20); |
| 202 | + break; |
| 203 | + } |
| 204 | + case ReconstructableScriptType::P2sh: { |
| 205 | + uint160 hash; |
| 206 | + ::Unserialize(s, hash); |
| 207 | + script.resize(23); |
| 208 | + script[0] = OP_HASH160; |
| 209 | + script[1] = 20; |
| 210 | + memcpy(&script[2], hash.data(), 20); |
| 211 | + script[22] = OP_EQUAL; |
| 212 | + break; |
| 213 | + } |
| 214 | + case ReconstructableScriptType::P2pkh: { |
| 215 | + uint160 hash; |
| 216 | + ::Unserialize(s, hash); |
| 217 | + script.resize(25); |
| 218 | + script[0] = OP_DUP; |
| 219 | + script[1] = OP_HASH160; |
| 220 | + script[2] = 20; |
| 221 | + memcpy(&script[3], hash.data(), 20); |
| 222 | + script[23] = OP_EQUALVERIFY; |
| 223 | + script[24] = OP_CHECKSIG; |
| 224 | + break; |
| 225 | + } |
| 226 | + case ReconstructableScriptType::P2pkEven: |
| 227 | + case ReconstructableScriptType::P2pkOdd: { |
| 228 | + prevector<32, unsigned char> xcoord; |
| 229 | + ::Unserialize(s, std::span{xcoord}); |
| 230 | + script.resize(35); |
| 231 | + script[0] = 33; |
| 232 | + script[1] = type; |
| 233 | + memcpy(&script[2], xcoord.data(), 32); |
| 234 | + script[34] = OP_CHECKSIG; |
| 235 | + break; |
| 236 | + } |
| 237 | + case ReconstructableScriptType::P2pkUncompressed: { |
| 238 | + prevector<64, unsigned char> public_key; |
| 239 | + ::Unserialize(s, std::span{public_key}); |
| 240 | + script.resize(67); |
| 241 | + script[0] = 65; |
| 242 | + script[1] = 0x04; |
| 243 | + memcpy(&script[1], public_key.data(), 64); |
| 244 | + script[66] = OP_CHECKSIG; |
| 245 | + break; |
| 246 | + } |
| 247 | + default: break; |
| 248 | + } |
| 249 | + } |
| 250 | +}; |
| 251 | + |
98 | 252 | struct AmountCompression |
99 | 253 | { |
100 | 254 | template<typename Stream, typename I> void Ser(Stream& s, I val) |
|
0 commit comments