|
9 | 9 | #include <prevector.h> |
10 | 10 | #include <primitives/transaction.h> |
11 | 11 | #include <script/script.h> |
| 12 | +#include <addresstype.h> |
12 | 13 | #include <serialize.h> |
13 | 14 | #include <span.h> |
14 | 15 |
|
@@ -95,6 +96,136 @@ 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 | + XOnlyPubKey pk; |
| 179 | + s >> pk; |
| 180 | + script = GetScriptForDestination(WitnessV1Taproot(pk)); |
| 181 | + break; |
| 182 | + } |
| 183 | + case ReconstructableScriptType::P2wsh: { |
| 184 | + uint256 hash; |
| 185 | + s >> hash; |
| 186 | + script = GetScriptForDestination(WitnessV0ScriptHash(hash)); |
| 187 | + break; |
| 188 | + } |
| 189 | + case ReconstructableScriptType::P2wpkh: { |
| 190 | + uint160 hash; |
| 191 | + s >> hash; |
| 192 | + script = GetScriptForDestination(WitnessV0KeyHash(hash)); |
| 193 | + break; |
| 194 | + } |
| 195 | + case ReconstructableScriptType::P2sh: { |
| 196 | + uint160 hash; |
| 197 | + s >> hash; |
| 198 | + script = GetScriptForDestination(ScriptHash(hash)); |
| 199 | + break; |
| 200 | + } |
| 201 | + case ReconstructableScriptType::P2pkh: { |
| 202 | + uint160 hash; |
| 203 | + s >> hash; |
| 204 | + script = GetScriptForDestination(PKHash(hash)); |
| 205 | + } |
| 206 | + case ReconstructableScriptType::P2pkEven: |
| 207 | + case ReconstructableScriptType::P2pkOdd: { |
| 208 | + unsigned char xcoord[32]; |
| 209 | + s >> xcoord; |
| 210 | + script.resize(35); |
| 211 | + script[0] = 33; |
| 212 | + script[1] = type; |
| 213 | + memcpy(&script[2], xcoord, 32); |
| 214 | + script[34] = OP_CHECKSIG; |
| 215 | + } |
| 216 | + case ReconstructableScriptType::P2pkUncompressed: { |
| 217 | + unsigned char public_key[65]; |
| 218 | + s >> public_key; |
| 219 | + script.resize(67); |
| 220 | + script[0] = 65; |
| 221 | + memcpy(&script[1], public_key, 65); |
| 222 | + script[66] = OP_CHECKSIG; |
| 223 | + } |
| 224 | + default: break; |
| 225 | + } |
| 226 | + } |
| 227 | +}; |
| 228 | + |
98 | 229 | struct AmountCompression |
99 | 230 | { |
100 | 231 | template<typename Stream, typename I> void Ser(Stream& s, I val) |
|
0 commit comments