1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
crate::ix!();
//-------------------------------------------[.cpp/bitcoin/src/script/standard.h]
pub const DEFAULT_ACCEPT_DATACARRIER: bool = true;
/**
| A reference to a Script: the Hash160
| of its serialization (see script.h)
|
*/
pub struct ScriptID {
base: BaseHash<u160>,
}
impl Default for ScriptID {
fn default() -> Self {
todo!();
/*
: base_hash(),
*/
}
}
impl From<&u160> for ScriptID {
fn from(in_: &u160) -> Self {
todo!();
/*
: base_hash(in),
*/
}
}
impl From<&Script> for ScriptID {
fn from(in_: &Script) -> Self {
todo!();
/*
: base_hash(Hash160(in)),
*/
}
}
impl From<&ScriptHash> for ScriptID {
fn from(in_: &ScriptHash) -> Self {
todo!();
/*
: BaseHash(static_cast<u160>(in))
*/
}
}
/**
| Default setting for nMaxDatacarrierBytes.
| 80 bytes of data, +1 for OP_RETURN, +2
| for the pushdata opcodes.
|
*/
pub const MAX_OP_RETURN_RELAY: u32 = 83;
/**
| A data carrying output is an unspendable
| output containing data. The script
| type is designated as TxoutType::NULL_DATA.
|
*/
lazy_static!{
/*
extern bool fAcceptDatacarrier;
*/
}
/**
| Maximum size of TxoutType::NULL_DATA
| scripts that this node considers standard.
|
*/
lazy_static!{
/*
extern unsigned nMaxDatacarrierBytes;
*/
}
#[derive(PartialEq,Eq,PartialOrd,Ord,Clone,Debug)]
pub enum TxoutType {
NONSTANDARD,
/**
| 'standard' transaction types:
|
*/
PUBKEY,
PUBKEYHASH,
SCRIPTHASH,
MULTISIG,
/**
| unspendable OP_RETURN script that
| carries data
|
*/
NULL_DATA,
WITNESS_V0_SCRIPTHASH,
WITNESS_V0_KEYHASH,
WITNESS_V1_TAPROOT,
/**
| Only for Witness versions not already
| defined above
|
*/
WITNESS_UNKNOWN,
}
pub struct NoDestination { }
impl PartialEq<NoDestination> for NoDestination {
#[inline] fn eq(&self, other: &NoDestination) -> bool {
todo!();
/*
return true;
*/
}
}
impl Eq for NoDestination {}
impl Ord for NoDestination {
#[inline] fn cmp(&self, other: &NoDestination) -> Ordering {
todo!();
/*
return true;
*/
}
}
impl PartialOrd<NoDestination> for NoDestination {
#[inline] fn partial_cmp(&self, other: &NoDestination) -> Option<Ordering> {
Some(self.cmp(other))
}
}
///-----------------------
pub struct PKHash {
base: BaseHash<u160>,
}
impl Default for PKHash {
fn default() -> Self {
todo!();
/*
: base_hash(),
*/
}
}
impl From<&u160> for PKHash {
fn from(hash: &u160) -> Self {
todo!();
/*
: base_hash(hash),
*/
}
}
///---------------------
pub struct ScriptHash {
base: BaseHash<u160>,
}
impl Default for ScriptHash {
fn default() -> Self {
todo!();
/*
: base_hash(),
*/
}
}
impl From<&u160> for ScriptHash {
fn from(hash: &u160) -> Self {
todo!();
/*
: base_hash(hash),
*/
}
}
///---------------------
pub struct WitnessV0ScriptHash {
base: BaseHash<u256>,
}
impl Default for WitnessV0ScriptHash {
fn default() -> Self {
todo!();
/*
: base_hash(),
*/
}
}
impl From<&u256> for WitnessV0ScriptHash {
fn from(hash: &u256) -> Self {
todo!();
/*
: base_hash(hash),
*/
}
}
///---------------------
pub struct WitnessV0KeyHash {
base: BaseHash<u160>,
}
impl Default for WitnessV0KeyHash {
fn default() -> Self {
todo!();
/*
: base_hash(),
*/
}
}
impl From<&u160> for WitnessV0KeyHash {
fn from(hash: &u160) -> Self {
todo!();
/*
: base_hash(hash),
*/
}
}
///---------------------
pub struct WitnessV1Taproot {
base: XOnlyPubKey,
}
impl Default for WitnessV1Taproot {
fn default() -> Self {
todo!();
/*
: x_only_pub_key(),
*/
}
}
impl From<&XOnlyPubKey> for WitnessV1Taproot {
fn from(xpk: &XOnlyPubKey) -> Self {
todo!();
/*
: x_only_pub_key(xpk),
*/
}
}
/**
| TxDestination subtype to encode any
| future Witness version
|
*/
pub struct WitnessUnknown {
version: u32,
length: u32,
program: [u8; 40],
}
impl PartialEq<WitnessUnknown> for WitnessUnknown {
#[inline] fn eq(&self, other: &WitnessUnknown) -> bool {
todo!();
/*
if (w1.version != w2.version) return false;
if (w1.length != w2.length) return false;
return std::equal(w1.program, w1.program + w1.length, w2.program);
*/
}
}
impl Eq for WitnessUnknown {}
impl Ord for WitnessUnknown {
#[inline] fn cmp(&self, other: &WitnessUnknown) -> Ordering {
todo!();
/*
if (w1.version < w2.version) return true;
if (w1.version > w2.version) return false;
if (w1.length < w2.length) return true;
if (w1.length > w2.length) return false;
return std::lexicographical_compare(w1.program, w1.program + w1.length, w2.program, w2.program + w2.length);
*/
}
}
impl PartialOrd<WitnessUnknown> for WitnessUnknown {
#[inline] fn partial_cmp(&self, other: &WitnessUnknown) -> Option<Ordering> {
Some(self.cmp(other))
}
}
/**
| A txout script template with a specific
| destination. It is either:
|
| - CNoDestination: no destination set
|
| - PKHash: TxoutType::PUBKEYHASH destination
| (P2PKH)
|
| - ScriptHash: TxoutType::SCRIPTHASH
| destination (P2SH)
|
| - WitnessV0ScriptHash: TxoutType::WITNESS_V0_SCRIPTHASH
| destination (P2WSH)
|
| - WitnessV0KeyHash: TxoutType::WITNESS_V0_KEYHASH
| destination (P2WPKH)
|
| - WitnessV1Taproot: TxoutType::WITNESS_V1_TAPROOT
| destination (P2TR)
|
| - WitnessUnknown: TxoutType::WITNESS_UNKNOWN
| destination (P2W???)
|
| A TxDestination is the internal data
| type encoded in a bitcoin address
|
*/
pub enum TxDestination {
NoDestination(NoDestination),
PKHash(PKHash),
ScriptHash(ScriptHash),
WitnessV0ScriptHash(WitnessV0ScriptHash),
WitnessV0KeyHash(WitnessV0KeyHash),
WitnessV1Taproot(WitnessV1Taproot),
WitnessUnknown(WitnessUnknown)
}
pub struct ShortestVectorFirstComparator { }
impl ShortestVectorFirstComparator {
pub fn invoke(&self,
a: &Vec<u8>,
b: &Vec<u8>) -> bool {
todo!();
/*
if (a.size() < b.size()) return true;
if (a.size() > b.size()) return false;
return a < b;
*/
}
}
#[derive(Default)]
pub struct TaprootSpendData {
/**
| The BIP341 internal key.
|
*/
internal_key: XOnlyPubKey,
/**
| The Merkle root of the script tree (0
| if no scripts).
|
*/
merkle_root: u256,
/**
| Map from (script, leaf_version) to
| (sets of) control blocks.
|
| More than one control block for a given
| script is only possible if it appears
| in multiple branches of the tree. We
| keep them all so that inference can reconstruct
| the full tree. Within each set, the control
| blocks are sorted by size, so that the
| signing logic can easily prefer the
| cheapest one.
|
*/
scripts: HashMap<(Script,i32),HashSet<Vec<u8>,ShortestVectorFirstComparator>>,
}
impl TaprootSpendData {
/**
| Merge other TaprootSpendData (for
| the same scriptPubKey) into this.
|
*/
pub fn merge(&mut self, other: TaprootSpendData) {
todo!();
/*
// TODO: figure out how to better deal with conflicting information
// being merged.
if (internal_key.IsNull() && !other.internal_key.IsNull()) {
internal_key = other.internal_key;
}
if (merkle_root.IsNull() && !other.merkle_root.IsNull()) {
merkle_root = other.merkle_root;
}
for (auto& [key, control_blocks] : other.scripts) {
// Once P0083R3 is supported by all our targeted platforms,
// this loop body can be replaced with:
// scripts[key].merge(std::move(control_blocks));
auto& target = scripts[key];
for (auto& control_block: control_blocks) {
target.insert(std::move(control_block));
}
}
*/
}
}
/**
| Utility class to construct Taproot
| outputs from internal key and script
| tree.
|
*/
pub struct TaprootBuilder {
/**
| Whether the builder is in a valid state
| so far.
|
*/
valid: bool, // default = true
/**
| The current state of the builder.
|
| For each level in the tree, one NodeInfo
| object may be present. m_branch[0] is
| information about the root; further values
| are for deeper subtrees being explored.
|
| For every right branch taken to reach the
| position we're currently working in, there
| will be a (non-nullopt) entry in m_branch
| corresponding to the left branch at that
| level.
|
| For example, imagine this tree:
|
| - N0 -
| / \
| N1 N2
| / \ / \
| A B C N3
| / \
| D E
|
| Initially, m_branch is empty. After
| processing leaf A, it would become
| {nullopt, nullopt, A}. When processing leaf
| B, an entry at level 2 already exists, and
| it would thus be combined with it to
| produce a level 1 one, resulting in
| {nullopt, N1}. Adding C and D takes us to
| {nullopt, N1, C} and {nullopt, N1, C, D}
| respectively. When E is processed, it is
| combined with D, and then C, and then N1,
| to produce the root, resulting in {N0}.
|
| This structure allows processing with just
| O(log n) overhead if the leaves are
| computed on the fly.
|
| As an invariant, there can never be nullopt
| entries at the end. There can also not be
| more than 128 entries (as that would mean
| more than 128 levels in the tree). The
| depth of newly added entries will always be
| at least equal to the current size of
| m_branch (otherwise it does not correspond
| to a depth-first traversal of
| a tree). m_branch is only empty if no
| entries have ever be processed. m_branch
| having length 1 corresponds to being done.
*/
branch: Vec<Option<taproot_builder::NodeInfo>>,
/**
| The internal key, set when finalizing.
|
*/
internal_key: XOnlyPubKey,
/**
| The output key, computed when finalizing.
|
*/
output_key: XOnlyPubKey,
/**
| The tweak parity, computed when finalizing.
|
*/
parity: bool,
}
pub mod taproot_builder {
use super::*;
/**
| Information about a tracked leaf in
| the Merkle tree.
|
*/
pub struct LeafInfo
{
/**
| The script.
|
*/
script: Script,
/**
| The leaf version for that
| script.
|
*/
leaf_version: i32,
/**
| The hashing partners above this
| leaf.
|
*/
merkle_branch: Vec<u256>,
}
/**
| Information associated with a node
| in the Merkle tree.
|
*/
pub struct NodeInfo {
/**
| Merkle hash of this node.
|
*/
hash: u256,
/**
| Tracked leaves underneath this node
| (either from the node itself, or its
| children).
|
| The merkle_branch field of each is the
| partners to get to *this* node.
|
*/
leaves: Vec<LeafInfo>,
}
}
impl TaprootBuilder {
/**
| Return true if so far all input was valid.
|
*/
pub fn is_valid(&self) -> bool {
todo!();
/*
return m_valid;
*/
}
/**
| Return whether there were either no
| leaves, or the leaves form a Huffman
| tree.
|
*/
pub fn is_complete(&self) -> bool {
todo!();
/*
return m_valid && (m_branch.size() == 0 || (m_branch.size() == 1 && m_branch[0].has_value()));
*/
}
}
//-------------------------------------------[.cpp/bitcoin/src/script/standard.cpp]
pub type ValType = Vec<u8>;
lazy_static!{
/*
bool fAcceptDatacarrier = DEFAULT_ACCEPT_DATACARRIER;
unsigned nMaxDatacarrierBytes = MAX_OP_RETURN_RELAY;
*/
}
impl From<&Script> for ScriptHash {
fn from(in_: &Script) -> Self {
todo!();
/*
: base_hash(Hash160(in)),
*/
}
}
impl From<&ScriptID> for ScriptHash {
fn from(in_: &ScriptID) -> Self {
todo!();
/*
: BaseHash(static_cast<u160>(in))
*/
}
}
impl From<&PubKey> for PKHash {
fn from(pubkey: &PubKey) -> Self {
todo!();
/*
: base_hash(pubkey.GetID()),
*/
}
}
impl From<&KeyID> for PKHash {
fn from(pubkey_id: &KeyID) -> Self {
todo!();
/*
: base_hash(pubkey_id),
*/
}
}
impl From<&PubKey> for WitnessV0KeyHash {
fn from(pubkey: &PubKey) -> Self {
todo!();
/*
: base_hash(pubkey.GetID()),
*/
}
}
impl From<&PKHash> for WitnessV0KeyHash {
fn from(pubkey_hash: &PKHash) -> Self {
todo!();
/*
: BaseHash(static_cast<u160>(pubkey_hash))
*/
}
}
impl Into<KeyID> for PKHash {
fn into(self) -> KeyID {
todo!();
/*
return CKeyID{static_cast<u160>(key_hash)};
*/
}
}
impl Into<KeyID> for WitnessV0KeyHash {
fn into(self) -> KeyID {
todo!();
/*
return CKeyID{static_cast<u160>(key_hash)};
*/
}
}
impl From<&Script> for WitnessV0ScriptHash {
fn from(in_: &Script) -> Self {
todo!();
/*
CSHA256().Write(in.data(), in.size()).Finalize(begin());
*/
}
}
/**
| Get the name of a TxoutType as a string
|
*/
pub fn get_txn_output_type(t: TxoutType) -> String {
todo!();
/*
switch (t) {
case TxoutType::NONSTANDARD: return "nonstandard";
case TxoutType::PUBKEY: return "pubkey";
case TxoutType::PUBKEYHASH: return "pubkeyhash";
case TxoutType::SCRIPTHASH: return "scripthash";
case TxoutType::MULTISIG: return "multisig";
case TxoutType::NULL_DATA: return "nulldata";
case TxoutType::WITNESS_V0_KEYHASH: return "witness_v0_keyhash";
case TxoutType::WITNESS_V0_SCRIPTHASH: return "witness_v0_scripthash";
case TxoutType::WITNESS_V1_TAPROOT: return "witness_v1_taproot";
case TxoutType::WITNESS_UNKNOWN: return "witness_unknown";
} // no default case, so the compiler can warn about missing cases
assert(false);
*/
}
pub fn match_pay_to_pubkey(
script: &Script,
pubkey: &mut ValType) -> bool {
todo!();
/*
if (script.size() == CPubKey::SIZE + 2 && script[0] == CPubKey::SIZE && script.back() == OP_CHECKSIG) {
pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::SIZE + 1);
return CPubKey::ValidSize(pubkey);
}
if (script.size() == CPubKey::COMPRESSED_SIZE + 2 && script[0] == CPubKey::COMPRESSED_SIZE && script.back() == OP_CHECKSIG) {
pubkey = valtype(script.begin() + 1, script.begin() + CPubKey::COMPRESSED_SIZE + 1);
return CPubKey::ValidSize(pubkey);
}
return false;
*/
}
pub fn match_pay_to_pubkey_hash(
script: &Script,
pubkeyhash: &mut ValType) -> bool {
todo!();
/*
if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160 && script[2] == 20 && script[23] == OP_EQUALVERIFY && script[24] == OP_CHECKSIG) {
pubkeyhash = valtype(script.begin () + 3, script.begin() + 23);
return true;
}
return false;
*/
}
/**
| Test for "small positive integer" script
| opcodes - OP_1 through OP_16.
|
*/
pub fn is_small_integer(opcode: OpcodeType) -> bool {
todo!();
/*
return opcode >= OP_1 && opcode <= OP_16;
*/
}
pub fn is_pushdata_op(opcode: OpcodeType) -> bool {
todo!();
/*
return opcode > OP_FALSE && opcode <= OP_PUSHDATA4;
*/
}
pub fn is_valid_multisig_key_count(n_keys: i32) -> bool {
todo!();
/*
return n_keys > 0 && n_keys <= MAX_PUBKEYS_PER_MULTISIG;
*/
}
pub fn get_multisig_key_count(
opcode: OpcodeType,
data: ValType,
count: &mut i32) -> bool {
todo!();
/*
if (IsSmallInteger(opcode)) {
count = CScript::DecodeOP_N(opcode);
return IsValidMultisigKeyCount(count);
}
if (IsPushdataOp(opcode)) {
if (!CheckMinimalPush(data, opcode)) return false;
try {
count = CScriptNum(data, /* fRequireMinimal = */ true).getint();
return IsValidMultisigKeyCount(count);
} catch (const scriptnum_error&) {
return false;
}
}
return false;
*/
}
pub fn match_multisig(
script: &Script,
required_sigs: &mut i32,
pubkeys: &mut Vec<ValType>) -> bool {
todo!();
/*
opcodetype opcode;
valtype data;
int num_keys;
CScript::const_iterator it = script.begin();
if (script.size() < 1 || script.back() != OP_CHECKMULTISIG) return false;
if (!script.GetOp(it, opcode, data) || !GetMultisigKeyCount(opcode, data, required_sigs)) return false;
while (script.GetOp(it, opcode, data) && CPubKey::ValidSize(data)) {
pubkeys.emplace_back(std::move(data));
}
if (!GetMultisigKeyCount(opcode, data, num_keys)) return false;
if (pubkeys.size() != static_cast<unsigned long>(num_keys) || num_keys < required_sigs) return false;
return (it + 1 == script.end());
*/
}
/**
| Parse a scriptPubKey and identify script
| type for standard scripts. If successful,
| returns script type and parsed pubkeys
| or hashes, depending on the type. For
| example, for a P2SH script, vSolutionsRet
| will contain the script hash, for P2PKH
| it will contain the key hash, etc.
|
| -----------
| @param[in] scriptPubKey
|
| Script to parse
| ----------
| @param[out] vSolutionsRet
|
| Vector of parsed pubkeys and hashes
|
| -----------
| @return
|
| The script type. TxoutType::NONSTANDARD
| represents a failed solve.
|
*/
pub fn solver(
script_pub_key: &Script,
solutions_ret: &mut Vec<Vec<u8>>) -> TxoutType {
todo!();
/*
vSolutionsRet.clear();
// Shortcut for pay-to-script-hash, which are more constrained than the other types:
// it is always OP_HASH160 20 [20 byte hash] OP_EQUAL
if (scriptPubKey.IsPayToScriptHash())
{
std::vector<unsigned char> hashBytes(scriptPubKey.begin()+2, scriptPubKey.begin()+22);
vSolutionsRet.push_back(hashBytes);
return TxoutType::SCRIPTHASH;
}
int witnessversion;
std::vector<unsigned char> witnessprogram;
if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_KEYHASH_SIZE) {
vSolutionsRet.push_back(std::move(witnessprogram));
return TxoutType::WITNESS_V0_KEYHASH;
}
if (witnessversion == 0 && witnessprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE) {
vSolutionsRet.push_back(std::move(witnessprogram));
return TxoutType::WITNESS_V0_SCRIPTHASH;
}
if (witnessversion == 1 && witnessprogram.size() == WITNESS_V1_TAPROOT_SIZE) {
vSolutionsRet.push_back(std::move(witnessprogram));
return TxoutType::WITNESS_V1_TAPROOT;
}
if (witnessversion != 0) {
vSolutionsRet.push_back(std::vector<unsigned char>{(unsigned char)witnessversion});
vSolutionsRet.push_back(std::move(witnessprogram));
return TxoutType::WITNESS_UNKNOWN;
}
return TxoutType::NONSTANDARD;
}
// Provably prunable, data-carrying output
//
// So long as script passes the IsUnspendable() test and all but the first
// byte passes the IsPushOnly() test we don't care what exactly is in the
// script.
if (scriptPubKey.size() >= 1 && scriptPubKey[0] == OP_RETURN && scriptPubKey.IsPushOnly(scriptPubKey.begin()+1)) {
return TxoutType::NULL_DATA;
}
std::vector<unsigned char> data;
if (MatchPayToPubkey(scriptPubKey, data)) {
vSolutionsRet.push_back(std::move(data));
return TxoutType::PUBKEY;
}
if (MatchPayToPubkeyHash(scriptPubKey, data)) {
vSolutionsRet.push_back(std::move(data));
return TxoutType::PUBKEYHASH;
}
int required;
std::vector<std::vector<unsigned char>> keys;
if (MatchMultisig(scriptPubKey, required, keys)) {
vSolutionsRet.push_back({static_cast<unsigned char>(required)}); // safe as required is in range 1..20
vSolutionsRet.insert(vSolutionsRet.end(), keys.begin(), keys.end());
vSolutionsRet.push_back({static_cast<unsigned char>(keys.size())}); // safe as size is in range 1..20
return TxoutType::MULTISIG;
}
vSolutionsRet.clear();
return TxoutType::NONSTANDARD;
*/
}
/**
| Parse a standard scriptPubKey for the
| destination address. Assigns result
| to the addressRet parameter and returns
| true if successful. Currently only
| works for P2PK, P2PKH, P2SH, P2WPKH,
| and P2WSH scripts.
|
*/
pub fn extract_destination(
script_pub_key: &Script,
address_ret: &mut TxDestination) -> bool {
todo!();
/*
std::vector<valtype> vSolutions;
TxoutType whichType = Solver(scriptPubKey, vSolutions);
switch (whichType) {
case TxoutType::PUBKEY: {
CPubKey pubKey(vSolutions[0]);
if (!pubKey.IsValid())
return false;
addressRet = PKHash(pubKey);
return true;
}
case TxoutType::PUBKEYHASH: {
addressRet = PKHash(u160(vSolutions[0]));
return true;
}
case TxoutType::SCRIPTHASH: {
addressRet = ScriptHash(u160(vSolutions[0]));
return true;
}
case TxoutType::WITNESS_V0_KEYHASH: {
WitnessV0KeyHash hash;
std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
addressRet = hash;
return true;
}
case TxoutType::WITNESS_V0_SCRIPTHASH: {
WitnessV0ScriptHash hash;
std::copy(vSolutions[0].begin(), vSolutions[0].end(), hash.begin());
addressRet = hash;
return true;
}
case TxoutType::WITNESS_V1_TAPROOT: {
WitnessV1Taproot tap;
std::copy(vSolutions[0].begin(), vSolutions[0].end(), tap.begin());
addressRet = tap;
return true;
}
case TxoutType::WITNESS_UNKNOWN: {
WitnessUnknown unk;
unk.version = vSolutions[0][0];
std::copy(vSolutions[1].begin(), vSolutions[1].end(), unk.program);
unk.length = vSolutions[1].size();
addressRet = unk;
return true;
}
case TxoutType::MULTISIG:
case TxoutType::NULL_DATA:
case TxoutType::NONSTANDARD:
return false;
} // no default case, so the compiler can warn about missing cases
assert(false);
*/
}
pub struct ScriptVisitor {
}
pub trait ScriptVisit<T> {
fn script_visit(&self, item: T) -> Script;
}
impl ScriptVisit<&NoDestination> for ScriptVisitor {
fn script_visit(&self, dest: &NoDestination) -> Script {
todo!();
/*
return CScript();
*/
}
}
impl ScriptVisit<&PKHash> for ScriptVisitor {
fn script_visit(&self, keyid: &PKHash) -> Script {
todo!();
/*
return CScript() << OP_DUP << OP_HASH160 << ToByteVector(keyID) << OP_EQUALVERIFY << OP_CHECKSIG;
*/
}
}
impl ScriptVisit<&ScriptHash> for ScriptVisitor {
fn script_visit(&self, scriptid: &ScriptHash) -> Script {
todo!();
/*
return CScript() << OP_HASH160 << ToByteVector(scriptID) << OP_EQUAL;
*/
}
}
impl ScriptVisit<&WitnessV0KeyHash> for ScriptVisitor {
fn script_visit(&self, id: &WitnessV0KeyHash) -> Script {
todo!();
/*
return CScript() << OP_0 << ToByteVector(id);
*/
}
}
impl ScriptVisit<&WitnessV0ScriptHash> for ScriptVisitor {
fn script_visit(&self, id: &WitnessV0ScriptHash) -> Script {
todo!();
/*
return CScript() << OP_0 << ToByteVector(id);
*/
}
}
impl ScriptVisit<&WitnessV1Taproot> for ScriptVisitor {
fn script_visit(&self, tap: &WitnessV1Taproot) -> Script {
todo!();
/*
return CScript() << OP_1 << ToByteVector(tap);
*/
}
}
impl ScriptVisit<&WitnessUnknown> for ScriptVisitor {
fn script_visit(&self, id: &WitnessUnknown) -> Script {
todo!();
/*
return CScript() << CScript::EncodeOP_N(id.version) << std::vector<unsigned char>(id.program, id.program + id.length);
*/
}
}
/**
| Generate a Bitcoin scriptPubKey for the given
| TxDestination.
|
| Returns a P2PKH script for a CKeyID
| destination, a P2SH script for a CScriptID,
| and an empty script for CNoDestination.
|
*/
pub fn get_script_for_destination(dest: &TxDestination) -> Script {
todo!();
/*
return std::visit(CScriptVisitor(), dest);
*/
}
/**
| Generate a P2PK script for the given
| pubkey.
|
*/
pub fn get_script_for_raw_pub_key(pub_key: &PubKey) -> Script {
todo!();
/*
return CScript() << std::vector<unsigned char>(pubKey.begin(), pubKey.end()) << OP_CHECKSIG;
*/
}
/**
| Generate a multisig script.
|
*/
pub fn get_script_for_multisig(
n_required: i32,
keys: &Vec<PubKey>) -> Script {
todo!();
/*
CScript script;
script << nRequired;
for (const CPubKey& key : keys)
script << ToByteVector(key);
script << keys.size() << OP_CHECKMULTISIG;
return script;
*/
}
/**
| Check whether a TxDestination is a
| CNoDestination.
|
*/
pub fn is_valid_destination(dest: &TxDestination) -> bool {
todo!();
/*
return dest.index() != 0;
*/
}
impl TaprootBuilder {
/**
| Combine information about a parent
| Merkle tree node from its child nodes.
|
*/
pub fn combine(&mut self,
a: taproot_builder::NodeInfo,
b: taproot_builder::NodeInfo) -> taproot_builder::NodeInfo {
todo!();
/*
NodeInfo ret;
/* Iterate over all tracked leaves in a, add b's hash to their Merkle branch, and move them to ret. */
for (auto& leaf : a.leaves) {
leaf.merkle_branch.push_back(b.hash);
ret.leaves.emplace_back(std::move(leaf));
}
/* Iterate over all tracked leaves in b, add a's hash to their Merkle branch, and move them to ret. */
for (auto& leaf : b.leaves) {
leaf.merkle_branch.push_back(a.hash);
ret.leaves.emplace_back(std::move(leaf));
}
/* Lexicographically sort a and b's hash, and compute parent hash. */
if (a.hash < b.hash) {
ret.hash = (CHashWriter(HASHER_TAPBRANCH) << a.hash << b.hash).GetSHA256();
} else {
ret.hash = (CHashWriter(HASHER_TAPBRANCH) << b.hash << a.hash).GetSHA256();
}
return ret;
*/
}
/**
| Insert information about a node at a
| certain depth, and propagate information
| up.
|
*/
pub fn insert(&mut self,
node: taproot_builder::NodeInfo,
depth: i32) {
todo!();
/*
assert(depth >= 0 && (size_t)depth <= TAPROOT_CONTROL_MAX_NODE_COUNT);
/* We cannot insert a leaf at a lower depth while a deeper branch is unfinished. Doing
* so would mean the Add() invocations do not correspond to a DFS traversal of a
* binary tree. */
if ((size_t)depth + 1 < m_branch.size()) {
m_valid = false;
return;
}
/* As long as an entry in the branch exists at the specified depth, combine it and propagate up.
* The 'node' variable is overwritten here with the newly combined node. */
while (m_valid && m_branch.size() > (size_t)depth && m_branch[depth].has_value()) {
node = Combine(std::move(node), std::move(*m_branch[depth]));
m_branch.pop_back();
if (depth == 0) m_valid = false; /* Can't propagate further up than the root */
--depth;
}
if (m_valid) {
/* Make sure the branch is big enough to place the new node. */
if (m_branch.size() <= (size_t)depth) m_branch.resize((size_t)depth + 1);
assert(!m_branch[depth].has_value());
m_branch[depth] = std::move(node);
}
*/
}
/**
| Check if a list of depths is legal (will
| lead to IsComplete()).
|
*/
pub fn valid_depths(&mut self, depths: &Vec<i32>) -> bool {
todo!();
/*
std::vector<bool> branch;
for (int depth : depths) {
// This inner loop corresponds to effectively the same logic on branch
// as what Insert() performs on the m_branch variable. Instead of
// storing a NodeInfo object, just remember whether or not there is one
// at that depth.
if (depth < 0 || (size_t)depth > TAPROOT_CONTROL_MAX_NODE_COUNT) return false;
if ((size_t)depth + 1 < branch.size()) return false;
while (branch.size() > (size_t)depth && branch[depth]) {
branch.pop_back();
if (depth == 0) return false;
--depth;
}
if (branch.size() <= (size_t)depth) branch.resize((size_t)depth + 1);
assert(!branch[depth]);
branch[depth] = true;
}
// And this check corresponds to the IsComplete() check on m_branch.
return branch.size() == 0 || (branch.size() == 1 && branch[0]);
*/
}
/**
| Add a new script at a certain depth in
| the tree. Add() operations must be called
| in depth-first traversal order of binary
| tree. If track is true, it will be included
| in the GetSpendData() output.
|
*/
pub fn add(&mut self,
depth: i32,
script: &Script,
leaf_version: i32,
track: Option<bool>) -> &mut TaprootBuilder {
let track: bool = track.unwrap_or(true);
todo!();
/*
assert((leaf_version & ~TAPROOT_LEAF_MASK) == 0);
if (!IsValid()) return *this;
/* Construct NodeInfo object with leaf hash and (if track is true) also leaf information. */
NodeInfo node;
node.hash = (CHashWriter{HASHER_TAPLEAF} << uint8_t(leaf_version) << script).GetSHA256();
if (track) node.leaves.emplace_back(LeafInfo{script, leaf_version, {}});
/* Insert into the branch. */
Insert(std::move(node), depth);
return *this;
*/
}
/**
| Like Add(), but for a Merkle node with
| a given hash to the tree.
|
*/
pub fn add_omitted(&mut self,
depth: i32,
hash: &u256) -> &mut TaprootBuilder {
todo!();
/*
if (!IsValid()) return *this;
/* Construct NodeInfo object with the hash directly, and insert it into the branch. */
NodeInfo node;
node.hash = hash;
Insert(std::move(node), depth);
return *this;
*/
}
/**
| Finalize the construction. Can only
| be called when IsComplete() is true.
| internal_key.IsFullyValid() must
| be true.
|
*/
pub fn finalize(&mut self, internal_key: &XOnlyPubKey) -> &mut TaprootBuilder {
todo!();
/*
/* Can only call this function when IsComplete() is true. */
assert(IsComplete());
m_internal_key = internal_key;
auto ret = m_internal_key.CreateTapTweak(m_branch.size() == 0 ? nullptr : &m_branch[0]->hash);
assert(ret.has_value());
std::tie(m_output_key, m_parity) = *ret;
return *this;
*/
}
/**
| Compute scriptPubKey (after Finalize()).
|
*/
pub fn get_output(&mut self) -> WitnessV1Taproot {
todo!();
/*
return WitnessV1Taproot{m_output_key};
*/
}
/**
| Compute spending data (after Finalize()).
|
*/
pub fn get_spend_data(&self) -> TaprootSpendData {
todo!();
/*
assert(IsComplete());
TaprootSpendData spd;
spd.merkle_root = m_branch.size() == 0 ? uint256() : m_branch[0]->hash;
spd.internal_key = m_internal_key;
if (m_branch.size()) {
// If any script paths exist, they have been combined into the root m_branch[0]
// by now. Compute the control block for each of its tracked leaves, and put them in
// spd.scripts.
for (const auto& leaf : m_branch[0]->leaves) {
std::vector<unsigned char> control_block;
control_block.resize(TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * leaf.merkle_branch.size());
control_block[0] = leaf.leaf_version | (m_parity ? 1 : 0);
std::copy(m_internal_key.begin(), m_internal_key.end(), control_block.begin() + 1);
if (leaf.merkle_branch.size()) {
std::copy(leaf.merkle_branch[0].begin(),
leaf.merkle_branch[0].begin() + TAPROOT_CONTROL_NODE_SIZE * leaf.merkle_branch.size(),
control_block.begin() + TAPROOT_CONTROL_BASE_SIZE);
}
spd.scripts[{leaf.script, leaf.leaf_version}].insert(std::move(control_block));
}
}
return spd;
*/
}
}
/**
| Given a TaprootSpendData and the output
| key, reconstruct its script tree.
|
| If the output doesn't match the spenddata,
| or if the data in spenddata is incomplete,
| std::nullopt is returned. Otherwise,
| a vector of (depth, script, leaf_ver)
| tuples is returned, corresponding
| to a depth-first traversal of the script
| tree.
|
*/
pub fn infer_taproot_tree(
spenddata: &TaprootSpendData,
output: &XOnlyPubKey) -> Option<Vec<(i32,Script,i32)>> {
todo!();
/*
// Verify that the output matches the assumed Merkle root and internal key.
auto tweak = spenddata.internal_key.CreateTapTweak(spenddata.merkle_root.IsNull() ? nullptr : &spenddata.merkle_root);
if (!tweak || tweak->first != output) return std::nullopt;
// If the Merkle root is 0, the tree is empty, and we're done.
std::vector<std::tuple<int, CScript, int>> ret;
if (spenddata.merkle_root.IsNull()) return ret;
/** Data structure to represent the nodes of the tree we're going to build. */
struct TreeNode {
/** Hash of this node, if known; 0 otherwise. */
uint256 hash;
/** The left and right subtrees (note that their order is irrelevant). */
std::unique_ptr<TreeNode> sub[2];
/** If this is known to be a leaf node, a pointer to the (script, leaf_ver) pair.
* nullptr otherwise. */
const std::pair<CScript, int>* leaf = nullptr;
/** Whether or not this node has been explored (is known to be a leaf, or known to have children). */
bool explored = false;
/** Whether or not this node is an inner node (unknown until explored = true). */
bool inner;
/** Whether or not we have produced output for this subtree. */
bool done = false;
};
// Build tree from the provided branches.
TreeNode root;
root.hash = spenddata.merkle_root;
for (const auto& [key, control_blocks] : spenddata.scripts) {
const auto& [script, leaf_ver] = key;
for (const auto& control : control_blocks) {
// Skip script records with nonsensical leaf version.
if (leaf_ver < 0 || leaf_ver >= 0x100 || leaf_ver & 1) continue;
// Skip script records with invalid control block sizes.
if (control.size() < TAPROOT_CONTROL_BASE_SIZE || control.size() > TAPROOT_CONTROL_MAX_SIZE ||
((control.size() - TAPROOT_CONTROL_BASE_SIZE) % TAPROOT_CONTROL_NODE_SIZE) != 0) continue;
// Skip script records that don't match the control block.
if ((control[0] & TAPROOT_LEAF_MASK) != leaf_ver) continue;
// Skip script records that don't match the provided Merkle root.
const uint256 leaf_hash = ComputeTapleafHash(leaf_ver, script);
const uint256 merkle_root = ComputeTaprootMerkleRoot(control, leaf_hash);
if (merkle_root != spenddata.merkle_root) continue;
TreeNode* node = &root;
size_t levels = (control.size() - TAPROOT_CONTROL_BASE_SIZE) / TAPROOT_CONTROL_NODE_SIZE;
for (size_t depth = 0; depth < levels; ++depth) {
// Can't descend into a node which we already know is a leaf.
if (node->explored && !node->inner) return std::nullopt;
// Extract partner hash from Merkle branch in control block.
uint256 hash;
std::copy(control.begin() + TAPROOT_CONTROL_BASE_SIZE + (levels - 1 - depth) * TAPROOT_CONTROL_NODE_SIZE,
control.begin() + TAPROOT_CONTROL_BASE_SIZE + (levels - depth) * TAPROOT_CONTROL_NODE_SIZE,
hash.begin());
if (node->sub[0]) {
// Descend into the existing left or right branch.
bool desc = false;
for (int i = 0; i < 2; ++i) {
if (node->sub[i]->hash == hash || (node->sub[i]->hash.IsNull() && node->sub[1-i]->hash != hash)) {
node->sub[i]->hash = hash;
node = &*node->sub[1-i];
desc = true;
break;
}
}
if (!desc) return std::nullopt; // This probably requires a hash collision to hit.
} else {
// We're in an unexplored node. Create subtrees and descend.
node->explored = true;
node->inner = true;
node->sub[0] = std::make_unique<TreeNode>();
node->sub[1] = std::make_unique<TreeNode>();
node->sub[1]->hash = hash;
node = &*node->sub[0];
}
}
// Cannot turn a known inner node into a leaf.
if (node->sub[0]) return std::nullopt;
node->explored = true;
node->inner = false;
node->leaf = &key;
node->hash = leaf_hash;
}
}
// Recursive processing to turn the tree into flattened output. Use an explicit stack here to avoid
// overflowing the call stack (the tree may be 128 levels deep).
std::vector<TreeNode*> stack{&root};
while (!stack.empty()) {
TreeNode& node = *stack.back();
if (!node.explored) {
// Unexplored node, which means the tree is incomplete.
return std::nullopt;
} else if (!node.inner) {
// Leaf node; produce output.
ret.emplace_back(stack.size() - 1, node.leaf->first, node.leaf->second);
node.done = true;
stack.pop_back();
} else if (node.sub[0]->done && !node.sub[1]->done && !node.sub[1]->explored && !node.sub[1]->hash.IsNull() &&
(CHashWriter{HASHER_TAPBRANCH} << node.sub[1]->hash << node.sub[1]->hash).GetSHA256() == node.hash) {
// Whenever there are nodes with two identical subtrees under it, we run into a problem:
// the control blocks for the leaves underneath those will be identical as well, and thus
// they will all be matched to the same path in the tree. The result is that at the location
// where the duplicate occurred, the left child will contain a normal tree that can be explored
// and processed, but the right one will remain unexplored.
//
// This situation can be detected, by encountering an inner node with unexplored right subtree
// with known hash, and H_TapBranch(hash, hash) is equal to the parent node (this node)'s hash.
//
// To deal with this, simply process the left tree a second time (set its done flag to false;
// noting that the done flag of its children have already been set to false after processing
// those). To avoid ending up in an infinite loop, set the done flag of the right (unexplored)
// subtree to true.
node.sub[0]->done = false;
node.sub[1]->done = true;
} else if (node.sub[0]->done && node.sub[1]->done) {
// An internal node which we're finished with.
node.sub[0]->done = false;
node.sub[1]->done = false;
node.done = true;
stack.pop_back();
} else if (!node.sub[0]->done) {
// An internal node whose left branch hasn't been processed yet. Do so first.
stack.push_back(&*node.sub[0]);
} else if (!node.sub[1]->done) {
// An internal node whose right branch hasn't been processed yet. Do so first.
stack.push_back(&*node.sub[1]);
}
}
return ret;
*/
}