| 243 | } |
| 244 | |
| 245 | bool IsValidPeginWitness(const CScriptWitness& pegin_witness, const std::vector<std::pair<CScript, CScript>>& fedpegscripts, const COutPoint& prevout, std::string& err_msg, bool check_depth, bool* depth_failed) { |
| 246 | if (depth_failed) { |
| 247 | *depth_failed = false; |
| 248 | } |
| 249 | |
| 250 | // 0) Return false if !consensus.has_parent_chain |
| 251 | if (!Params().GetConsensus().has_parent_chain) { |
| 252 | err_msg = "Parent chain is not enabled on this network."; |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | // Format on stack is as follows: |
| 257 | // 1) value - the value of the pegin output |
| 258 | // 2) asset type - the asset type being pegged in |
| 259 | // 3) genesis blockhash - genesis block of the parent chain |
| 260 | // 4) claim script - script to be evaluated for spend authorization |
| 261 | // 5) serialized transaction - serialized bitcoin transaction |
| 262 | // 6) txout proof - merkle proof connecting transaction to header |
| 263 | // |
| 264 | // First 4 values(plus prevout) are enough to validate a peg-in without any internal knowledge |
| 265 | // of Bitcoin serialization. This is useful for further abstraction by outsourcing |
| 266 | // the other validity checks to RPC calls. |
| 267 | |
| 268 | const std::vector<std::vector<unsigned char> >& stack = pegin_witness.stack; |
| 269 | // Must include all elements |
| 270 | if (stack.size() != 6) { |
| 271 | err_msg = "Not enough stack items."; |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | CDataStream stream(stack[0], SER_NETWORK, PROTOCOL_VERSION); |
| 276 | CAmount value; |
| 277 | try { |
| 278 | stream >> value; |
| 279 | } catch (...) { |
| 280 | err_msg = "Could not deserialize value."; |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | if (!MoneyRange(value)) { |
| 285 | err_msg = "Value was not in valid value range."; |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | // Get asset type |
| 290 | if (stack[1].size() != 32) { |
| 291 | err_msg = "Asset type was not 32 bytes."; |
| 292 | return false; |
| 293 | } |
| 294 | CAsset asset(stack[1]); |
| 295 | |
| 296 | // Get genesis blockhash |
| 297 | if (stack[2].size() != 32) { |
| 298 | err_msg = "Parent genesis blockchaash was not 32 bytes."; |
| 299 | return false; |
| 300 | } |
| 301 | uint256 gen_hash(stack[2]); |
| 302 | |