| 371 | } |
| 372 | |
| 373 | static void MutateTxAddOutMultiSig(CMutableTransaction& tx, const std::string& strInput) |
| 374 | { |
| 375 | // Separate into VALUE:REQUIRED:NUMKEYS:PUBKEY1:PUBKEY2:....[:FLAGS] |
| 376 | std::vector<std::string> vStrInputParts; |
| 377 | boost::split(vStrInputParts, strInput, boost::is_any_of(":")); |
| 378 | |
| 379 | // Check that there are enough parameters |
| 380 | if (vStrInputParts.size()<3) |
| 381 | throw std::runtime_error("Not enough multisig parameters"); |
| 382 | |
| 383 | // Extract and validate VALUE |
| 384 | CAmount value = ExtractAndValidateValue(vStrInputParts[0]); |
| 385 | |
| 386 | // Extract REQUIRED |
| 387 | const uint32_t required{TrimAndParse<uint32_t>(vStrInputParts.at(1), "invalid multisig required number")}; |
| 388 | |
| 389 | // Extract NUMKEYS |
| 390 | const uint32_t numkeys{TrimAndParse<uint32_t>(vStrInputParts.at(2), "invalid multisig total number")}; |
| 391 | |
| 392 | // Validate there are the correct number of pubkeys |
| 393 | if (vStrInputParts.size() < numkeys + 3) |
| 394 | throw std::runtime_error("incorrect number of multisig pubkeys"); |
| 395 | |
| 396 | if (required < 1 || required > MAX_PUBKEYS_PER_MULTISIG || numkeys < 1 || numkeys > MAX_PUBKEYS_PER_MULTISIG || numkeys < required) |
| 397 | throw std::runtime_error("multisig parameter mismatch. Required " \ |
| 398 | + ToString(required) + " of " + ToString(numkeys) + "signatures."); |
| 399 | |
| 400 | // extract and validate PUBKEYs |
| 401 | std::vector<CPubKey> pubkeys; |
| 402 | for(int pos = 1; pos <= int(numkeys); pos++) { |
| 403 | CPubKey pubkey(ParseHex(vStrInputParts[pos + 2])); |
| 404 | if (!pubkey.IsFullyValid()) |
| 405 | throw std::runtime_error("invalid TX output pubkey"); |
| 406 | pubkeys.push_back(pubkey); |
| 407 | } |
| 408 | |
| 409 | // Extract FLAGS |
| 410 | bool bSegWit = false; |
| 411 | bool bScriptHash = false; |
| 412 | if (vStrInputParts.size() == numkeys + 4) { |
| 413 | std::string flags = vStrInputParts.back(); |
| 414 | bSegWit = (flags.find('W') != std::string::npos); |
| 415 | bScriptHash = (flags.find('S') != std::string::npos); |
| 416 | } |
| 417 | else if (vStrInputParts.size() > numkeys + 4) { |
| 418 | // Validate that there were no more parameters passed |
| 419 | throw std::runtime_error("Too many parameters"); |
| 420 | } |
| 421 | |
| 422 | CScript scriptPubKey = GetScriptForMultisig(required, pubkeys); |
| 423 | |
| 424 | if (bSegWit) { |
| 425 | for (const CPubKey& pubkey : pubkeys) { |
| 426 | if (!pubkey.IsCompressed()) { |
| 427 | throw std::runtime_error("Uncompressed pubkeys are not useable for SegWit outputs"); |
| 428 | } |
| 429 | } |
| 430 | // Build a P2WSH with the multisig script |
no test coverage detected