Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 | 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x | import { TransactionBuilder, address as bjsAddress, TxOutput } from 'bitcoinjs-lib' import BN from 'bn.js' import { addUTXOsToFund, DUST_MINIMUM, estimateTXBytes, sumOutputValues, hash160, signInputs, getTransactionInsideBuilder } from './utils' import { makePreorderSkeleton, makeRegisterSkeleton, makeUpdateSkeleton, makeTransferSkeleton, makeRenewalSkeleton, makeRevokeSkeleton, makeNamespacePreorderSkeleton, makeNamespaceRevealSkeleton, makeNamespaceReadySkeleton, makeNameImportSkeleton, makeAnnounceSkeleton, makeTokenTransferSkeleton, BlockstackNamespace } from './skeletons' import { config } from '../config' import { InvalidAmountError, InvalidParameterError } from '../errors' import { TransactionSigner, PubkeyHashSigner } from './signers' import { UTXO } from '../network' const dummyConsensusHash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' const dummyZonefileHash = 'ffffffffffffffffffffffffffffffffffffffff' /** * @ignore */ function addOwnerInput(utxos: UTXO[], ownerAddress: string, txB: TransactionBuilder, addChangeOut: boolean = true ) { // add an owner UTXO and a change out. if (utxos.length <= 0) { throw new Error('Owner has no UTXOs for UPDATE.') } utxos.sort((a, b) => a.value - b.value) const ownerUTXO = utxos[0] const ownerInput = txB.addInput(ownerUTXO.tx_hash, ownerUTXO.tx_output_n) if (addChangeOut) { txB.addOutput(ownerAddress, ownerUTXO.value) } return { index: ownerInput, value: ownerUTXO.value } } /** * @ignore */ function fundTransaction(txB: TransactionBuilder, paymentAddress: string, utxos: UTXO[], feeRate: number, inAmounts: number, changeIndex: number | null = null ) { // change index for the payer. if (changeIndex === null) { changeIndex = txB.addOutput(paymentAddress, DUST_MINIMUM) } // fund the transaction fee. const txFee = estimateTXBytes(txB, 0, 0) * feeRate const outAmounts = sumOutputValues(txB) const change = addUTXOsToFund(txB, utxos, txFee + outAmounts - inAmounts, feeRate) const txInner = getTransactionInsideBuilder(txB) const txOut = txInner.outs[changeIndex] as TxOutput txOut.value += change return txB } /** * @ignore */ function returnTransactionHex(txB: TransactionBuilder, buildIncomplete: boolean = false ) { if (buildIncomplete) { return txB.buildIncomplete().toHex() } else { return txB.build().toHex() } } /** * @ignore */ function getTransactionSigner(input: string | TransactionSigner): TransactionSigner { if (typeof input === 'string') { return PubkeyHashSigner.fromHexString(input) } else { return input } } /** * Estimates cost of a preorder transaction for a domain name. * @param {String} fullyQualifiedName - the name to preorder * @param {String} destinationAddress - the address to receive the name (this * must be passed as the 'registrationAddress' in the register transaction) * @param {String} paymentAddress - the address funding the preorder * @param {Number} paymentUtxos - the number of UTXOs we expect will be required * from the payment address. * @returns {Promise} - a promise which resolves to the satoshi cost to fund * the preorder. This includes a 5500 satoshi dust output for the preorder. * Even though this is a change output, the payer must supply enough funds * to generate this output, so we include it in the cost. * @private */ function estimatePreorder(fullyQualifiedName: string, destinationAddress: string, paymentAddress: string, paymentUtxos: number = 1 ): Promise<number> { const network = config.network const preorderPromise = network.getNamePrice(fullyQualifiedName) .then(namePrice => makePreorderSkeleton( fullyQualifiedName, dummyConsensusHash, paymentAddress, network.getDefaultBurnAddress(), namePrice, destinationAddress )) return Promise.all([network.getFeeRate(), preorderPromise]) .then(([feeRate, preorderTX]) => { const outputsValue = sumOutputValues(preorderTX) const txFee = feeRate * estimateTXBytes(preorderTX, paymentUtxos, 0) return txFee + outputsValue }) } /** * Estimates cost of a register transaction for a domain name. * @param {String} fullyQualifiedName - the name to register * @param {String} registerAddress - the address to receive the name * @param {String} paymentAddress - the address funding the register * @param {Boolean} includingZonefile - whether or not we will broadcast * a zonefile hash as part of the register * @param {Number} paymentUtxos - the number of UTXOs we expect will be required * from the payment address. * @returns {Promise} - a promise which resolves to the satoshi cost to fund * the register. * @private */ function estimateRegister(fullyQualifiedName: string, registerAddress: string, paymentAddress: string, includingZonefile: boolean = false, paymentUtxos: number = 1 ): Promise<number> { const network = config.network let valueHash if (includingZonefile) { valueHash = dummyZonefileHash } const registerTX = makeRegisterSkeleton( fullyQualifiedName, registerAddress, valueHash ) return network.getFeeRate() .then((feeRate) => { const outputsValue = sumOutputValues(registerTX) // 1 additional output for payer change const txFee = feeRate * estimateTXBytes(registerTX, paymentUtxos, 1) return txFee + outputsValue }) } /** * Estimates cost of an update transaction for a domain name. * @param {String} fullyQualifiedName - the name to update * @param {String} ownerAddress - the owner of the name * @param {String} paymentAddress - the address funding the update * @param {Number} paymentUtxos - the number of UTXOs we expect will be required * from the payment address. * @returns {Promise} - a promise which resolves to the satoshi cost to fund * the update. * @private */ function estimateUpdate(fullyQualifiedName: string, ownerAddress: string, paymentAddress: string, paymentUtxos: number = 1 ): Promise<number> { const network = config.network const updateTX = makeUpdateSkeleton( fullyQualifiedName, dummyConsensusHash, dummyZonefileHash ) return network.getFeeRate() .then((feeRate) => { const outputsValue = sumOutputValues(updateTX) // 1 additional input for the owner // 2 additional outputs for owner / payer change const txFee = feeRate * estimateTXBytes(updateTX, 1 + paymentUtxos, 2) return txFee + outputsValue }) } /** * Estimates cost of an transfer transaction for a domain name. * @param {String} fullyQualifiedName - the name to transfer * @param {String} destinationAddress - the next owner of the name * @param {String} ownerAddress - the current owner of the name * @param {String} paymentAddress - the address funding the transfer * @param {Number} paymentUtxos - the number of UTXOs we expect will be required * from the payment address. * @returns {Promise} - a promise which resolves to the satoshi cost to fund * the transfer. * @private */ function estimateTransfer(fullyQualifiedName: string, destinationAddress: string, ownerAddress: string, paymentAddress: string, paymentUtxos: number = 1 ): Promise<number> { const network = config.network const transferTX = makeTransferSkeleton(fullyQualifiedName, dummyConsensusHash, destinationAddress) return network.getFeeRate() .then((feeRate) => { const outputsValue = sumOutputValues(transferTX) // 1 additional input for the owner // 2 additional outputs for owner / payer change const txFee = feeRate * estimateTXBytes(transferTX, 1 + paymentUtxos, 2) return txFee + outputsValue }) } /** * Estimates cost of an transfer transaction for a domain name. * @param {String} fullyQualifiedName - the name to renew * @param {String} destinationAddress - the next owner of the name * @param {String} ownerAddress - the current owner of the name * @param {String} paymentAddress - the address funding the transfer * @param {Boolean} includingZonefile - whether or not we will broadcast a zonefile hash in the renewal operation * @param {Number} paymentUtxos - the number of UTXOs we expect will be required * from the payment address. * @returns {Promise} - a promise which resolves to the satoshi cost to fund * the transfer. * @private */ function estimateRenewal(fullyQualifiedName: string, destinationAddress: string, ownerAddress: string, paymentAddress: string, includingZonefile: boolean = false, paymentUtxos: number = 1 ): Promise<number> { const network = config.network let valueHash: string if (includingZonefile) { valueHash = dummyZonefileHash } const renewalPromise = network.getNamePrice(fullyQualifiedName) .then(namePrice => makeRenewalSkeleton( fullyQualifiedName, destinationAddress, ownerAddress, network.getDefaultBurnAddress(), namePrice, valueHash )) return Promise.all([network.getFeeRate(), renewalPromise]) .then(([feeRate, renewalTX]) => { const outputsValue = sumOutputValues(renewalTX) // 1 additional input for the owner // and renewal skeleton includes all outputs for owner change, but not for payer change. const txFee = feeRate * estimateTXBytes(renewalTX, 1 + paymentUtxos, 1) return txFee + outputsValue - 5500 // don't count the dust change for old owner. }) } /** * Estimates cost of a revoke transaction for a domain name. * @param {String} fullyQualifiedName - the name to revoke * @param {String} ownerAddress - the current owner of the name * @param {String} paymentAddress the address funding the revoke * @param {Number} paymentUtxos - the number of UTXOs we expect will be required * from the payment address. * @returns {Promise} - a promise which resolves to the satoshi cost to fund the * revoke. * @private */ function estimateRevoke(fullyQualifiedName: string, ownerAddress: string, paymentAddress: string, paymentUtxos: number = 1 ): Promise<number> { const network = config.network const revokeTX = makeRevokeSkeleton(fullyQualifiedName) return Promise.all([network.getFeeRate()]) .then(([feeRate]) => { const outputsValue = sumOutputValues(revokeTX) // 1 additional input for owner // 1 additional output for payer change const txFee = feeRate * estimateTXBytes(revokeTX, 1 + paymentUtxos, 2) return txFee + outputsValue }) } /** * Estimates cost of a namespace preorder transaction for a namespace * @param {String} namespaceID - the namespace to preorder * @param {String} revealAddress - the address to receive the namespace (this * must be passed as the 'revealAddress' in the namespace-reveal transaction) * @param {String} paymentAddress - the address funding the preorder * @param {Number} paymentUtxos - the number of UTXOs we expect will be required * from the payment address. * @returns {Promise} - a promise which resolves to the satoshi cost to fund * the preorder. This includes a 5500 satoshi dust output for the preorder. * Even though this is a change output, the payer must supply enough funds * to generate this output, so we include it in the cost. * @private */ function estimateNamespacePreorder(namespaceID: string, revealAddress: string, paymentAddress: string, paymentUtxos: number = 1 ): Promise<number> { const network = config.network const preorderPromise = network.getNamespacePrice(namespaceID) .then(namespacePrice => makeNamespacePreorderSkeleton( namespaceID, dummyConsensusHash, paymentAddress, revealAddress, namespacePrice )) return Promise.all([network.getFeeRate(), preorderPromise]) .then(([feeRate, preorderTX]) => { const outputsValue = sumOutputValues(preorderTX) const txFee = feeRate * estimateTXBytes(preorderTX, paymentUtxos, 0) return txFee + outputsValue }) } /** * Estimates cost of a namesapce reveal transaction for a namespace * @param {BlockstackNamespace} namespace - the namespace to reveal * @param {String} revealAddress - the address to receive the namespace * (this must have been passed as 'revealAddress' to a prior namespace * preorder) * @param {String} paymentAddress - the address that pays for this transaction * @param {Number} paymentUtxos - the number of UTXOs we expect will be required * from the payment address * @returns {Promise} - a promise which resolves to the satoshi cost to * fund the reveal. This includes a 5500 satoshi dust output for the * preorder. Even though this is a change output, the payer must have * enough funds to generate this output, so we include it in the cost. * @private */ function estimateNamespaceReveal(namespace: BlockstackNamespace, revealAddress: string, paymentAddress: string, paymentUtxos: number = 1 ): Promise<number> { const network = config.network const revealTX = makeNamespaceRevealSkeleton(namespace, revealAddress) return network.getFeeRate() .then((feeRate) => { const outputsValue = sumOutputValues(revealTX) // 1 additional output for payer change const txFee = feeRate * estimateTXBytes(revealTX, paymentUtxos, 1) return txFee + outputsValue }) } /** * Estimates the cost of a namespace-ready transaction for a namespace * @param {String} namespaceID - the namespace to ready * @param {Number} revealUtxos - the number of UTXOs we expect will * be required from the reveal address * @returns {Promise} - a promise which resolves to the satoshi cost to * fund this namespacey-ready transaction. * @private */ function estimateNamespaceReady(namespaceID: string, revealUtxos: number = 1 ): Promise<number> { const network = config.network const readyTX = makeNamespaceReadySkeleton(namespaceID) return network.getFeeRate() .then((feeRate) => { const outputsValue = sumOutputValues(readyTX) const txFee = feeRate * estimateTXBytes(readyTX, revealUtxos, 1) return txFee + outputsValue }) } /** * Estimates the cost of a name-import transaction * @param {String} name - the fully-qualified name * @param {String} recipientAddr - the recipient * @param {String} zonefileHash - the zone file hash * @param {Number} importUtxos - the number of UTXOs we expect will * be required from the importer address * @returns {Promise} - a promise which resolves to the satoshi cost * to fund this name-import transaction * @private */ function estimateNameImport(name: string, recipientAddr: string, zonefileHash: string, importUtxos: number = 1 ): Promise<number> { const network = config.network const importTX = makeNameImportSkeleton(name, recipientAddr, zonefileHash) return network.getFeeRate() .then((feeRate) => { const outputsValue = sumOutputValues(importTX) const txFee = feeRate * estimateTXBytes(importTX, importUtxos, 1) return txFee + outputsValue }) } /** * Estimates the cost of an announce transaction * @param {String} messageHash - the hash of the message * @param {Number} senderUtxos - the number of utxos we expect will * be required from the importer address * @returns {Promise} - a promise which resolves to the satoshi cost * to fund this announce transaction * @private */ function estimateAnnounce(messageHash: string, senderUtxos: number = 1 ): Promise<number> { const network = config.network const announceTX = makeAnnounceSkeleton(messageHash) return network.getFeeRate() .then((feeRate) => { const outputsValue = sumOutputValues(announceTX) const txFee = feeRate * estimateTXBytes(announceTX, senderUtxos, 1) return txFee + outputsValue }) } /** * Estimates the cost of a token-transfer transaction * @param {String} recipientAddress - the recipient of the tokens * @param {String} tokenType - the type of token to spend * @param {Object} tokenAmount - a 64-bit unsigned BigInteger encoding the number of tokens * to spend * @param {String} scratchArea - an arbitrary string to store with the transaction * @param {Number} senderUtxos - the number of utxos we expect will * be required from the importer address * @param {Number} additionalOutputs - the number of outputs we expect to add beyond * just the recipient output (default = 1, if the token owner is also the bitcoin funder) * @returns {Promise} - a promise which resolves to the satoshi cost to * fund this token-transfer transaction * @private */ function estimateTokenTransfer(recipientAddress: string, tokenType: string, tokenAmount: BN, scratchArea: string, senderUtxos: number = 1, additionalOutputs: number = 1 ) { const network = config.network const tokenTransferTX = makeTokenTransferSkeleton( recipientAddress, dummyConsensusHash, tokenType, tokenAmount, scratchArea) return network.getFeeRate() .then((feeRate) => { const outputsValue = sumOutputValues(tokenTransferTX) const txFee = feeRate * estimateTXBytes(tokenTransferTX, senderUtxos, additionalOutputs) return txFee + outputsValue }) } /** * Generates a preorder transaction for a domain name. * @param {String} fullyQualifiedName - the name to pre-order * @param {String} destinationAddress - the address to receive the name (this * must be passed as the 'registrationAddress' in the register transaction) * @param {String | TransactionSigner} paymentKeyIn - a hex string of * the private key used to fund the transaction or a transaction signer object * @param {boolean} buildIncomplete - optional boolean, defaults to false, * indicating whether the function should attempt to return an unsigned (or not fully signed) * transaction. Useful for passing around a TX for multi-sig input signing. * @returns {Promise} - a promise which resolves to the hex-encoded transaction. * this function *does not* perform the requisite safety checks -- please see * the safety module for those. * @private */ function makePreorder(fullyQualifiedName: string, destinationAddress: string, paymentKeyIn: string | TransactionSigner, buildIncomplete: boolean = false ) { const network = config.network const namespace = fullyQualifiedName.split('.').pop() const paymentKey = getTransactionSigner(paymentKeyIn) return paymentKey.getAddress().then((preorderAddress) => { const preorderPromise = Promise.all([network.getConsensusHash(), network.getNamePrice(fullyQualifiedName), network.getNamespaceBurnAddress(namespace)]) .then(([consensusHash, namePrice, burnAddress]) => makePreorderSkeleton( fullyQualifiedName, consensusHash, preorderAddress, burnAddress, namePrice, destinationAddress )) return Promise.all([network.getUTXOs(preorderAddress), network.getFeeRate(), preorderPromise]) .then(([utxos, feeRate, preorderSkeleton]) => { const txB = TransactionBuilder.fromTransaction(preorderSkeleton, network.layer1) txB.setVersion(1) const changeIndex = 1 // preorder skeleton always creates a change output at index = 1 const signingTxB = fundTransaction(txB, preorderAddress, utxos, feeRate, 0, changeIndex) return signInputs(signingTxB, paymentKey) }) .then(signingTxB => returnTransactionHex(signingTxB, buildIncomplete)) }) } /** * Generates an update transaction for a domain name. * @param {String} fullyQualifiedName - the name to update * @param {String | TransactionSigner} ownerKeyIn - a hex string of the * owner key, or a transaction signer object. This will provide one * UTXO input, and also recieve a dust output. * @param {String | TransactionSigner} paymentKeyIn - a hex string, or a * transaction signer object, of the private key used to fund the * transaction's txfees * @param {String} zonefile - the zonefile data to update (this will be hashed * to include in the transaction), the zonefile itself must be published * after the UPDATE propagates. * @param {String} valueHash - if given, this is the hash to store (instead of * zonefile). zonefile will be ignored if this is given. * @param {boolean} buildIncomplete - optional boolean, defaults to false, * indicating whether the function should attempt to return an unsigned (or not fully signed) * transaction. Useful for passing around a TX for multi-sig input signing. * @returns {Promise} - a promise which resolves to the hex-encoded transaction. * this function *does not* perform the requisite safety checks -- please see * the safety module for those. * @private */ function makeUpdate(fullyQualifiedName: string, ownerKeyIn: string | TransactionSigner, paymentKeyIn: string | TransactionSigner, zonefile: string, valueHash: string = '', buildIncomplete: boolean = false ) { const network = config.network if (!valueHash && !zonefile) { return Promise.reject( new Error('Need zonefile or valueHash arguments') ) } if (valueHash.length === 0) { if (!zonefile) { return Promise.reject( new Error('Need zonefile or valueHash arguments') ) } valueHash = hash160(Buffer.from(zonefile)).toString('hex') } else if (valueHash.length !== 40) { return Promise.reject( new Error(`Invalid valueHash ${valueHash}`) ) } const paymentKey = getTransactionSigner(paymentKeyIn) const ownerKey = getTransactionSigner(ownerKeyIn) return Promise.all([ownerKey.getAddress(), paymentKey.getAddress()]) .then(([ownerAddress, paymentAddress]) => { const txPromise = network.getConsensusHash() .then(consensusHash => makeUpdateSkeleton(fullyQualifiedName, consensusHash, valueHash)) .then((updateTX) => { const txB = TransactionBuilder.fromTransaction(updateTX, network.layer1) txB.setVersion(1) return txB }) return Promise.all([txPromise, network.getUTXOs(paymentAddress), network.getUTXOs(ownerAddress), network.getFeeRate()]) .then(([txB, payerUtxos, ownerUtxos, feeRate]) => { const ownerInput = addOwnerInput(ownerUtxos, ownerAddress, txB) const signingTxB = fundTransaction(txB, paymentAddress, payerUtxos, feeRate, ownerInput.value) return signInputs(signingTxB, paymentKey, [{ index: ownerInput.index, signer: ownerKey }]) }) }) .then(signingTxB => returnTransactionHex(signingTxB, buildIncomplete)) } /** * Generates a register transaction for a domain name. * @param {String} fullyQualifiedName - the name to register * @param {String} registerAddress - the address to receive the name (this * must have been passed as the 'destinationAddress' in the preorder transaction) * this address will receive a dust UTXO * @param {String | TransactionSigner} paymentKeyIn - a hex string of * the private key (or a TransactionSigner object) used to fund the * transaction (this *must* be the same as the payment address used * to fund the preorder) * @param {String} zonefile - the zonefile data to include (this will be hashed * to include in the transaction), the zonefile itself must be published * after the UPDATE propagates. * @param {String} valueHash - the hash of the zone file data to include. * It will be used instead of zonefile, if given * @param {boolean} buildIncomplete - optional boolean, defaults to false, * indicating whether the function should attempt to return an unsigned (or not fully signed) * transaction. Useful for passing around a TX for multi-sig input signing. * @returns {Promise} - a promise which resolves to the hex-encoded transaction. * this function *does not* perform the requisite safety checks -- please see * the safety module for those. * @private */ function makeRegister(fullyQualifiedName: string, registerAddress: string, paymentKeyIn: string | TransactionSigner, zonefile: string = null, valueHash: string = null, buildIncomplete: boolean = false ) { const network = config.network if (!valueHash && !!zonefile) { valueHash = hash160(Buffer.from(zonefile)).toString('hex') } else if (!!valueHash && valueHash.length !== 40) { return Promise.reject( new Error(`Invalid zonefile hash ${valueHash}`) ) } const registerSkeleton = makeRegisterSkeleton( fullyQualifiedName, registerAddress, valueHash ) const txB = TransactionBuilder.fromTransaction(registerSkeleton, network.layer1) txB.setVersion(1) const paymentKey = getTransactionSigner(paymentKeyIn) return paymentKey.getAddress().then( paymentAddress => Promise.all([network.getUTXOs(paymentAddress), network.getFeeRate()]) .then(([utxos, feeRate]) => { const signingTxB = fundTransaction(txB, paymentAddress, utxos, feeRate, 0) return signInputs(signingTxB, paymentKey) }) ) .then(signingTxB => returnTransactionHex(signingTxB, buildIncomplete)) } /** * Generates a transfer transaction for a domain name. * @param {String} fullyQualifiedName - the name to transfer * @param {String} destinationAddress - the address to receive the name. * this address will receive a dust UTXO * @param {String | TransactionSigner} ownerKeyIn - a hex string of * the current owner's private key (or a TransactionSigner object) * @param {String | TransactionSigner} paymentKeyIn - a hex string of * the private key used to fund the transaction (or a * TransactionSigner object) * @param {Boolean} keepZonefile - if true, then preserve the name's zone file * @param {boolean} buildIncomplete - optional boolean, defaults to false, * indicating whether the function should attempt to return an unsigned (or not fully signed) * transaction. Useful for passing around a TX for multi-sig input signing. * @returns {Promise} - a promise which resolves to the hex-encoded transaction. * this function *does not* perform the requisite safety checks -- please see * the safety module for those. * @private */ function makeTransfer(fullyQualifiedName: string, destinationAddress: string, ownerKeyIn: string | TransactionSigner, paymentKeyIn: string | TransactionSigner, keepZonefile: boolean = false, buildIncomplete: boolean = false ) { const network = config.network const paymentKey = getTransactionSigner(paymentKeyIn) const ownerKey = getTransactionSigner(ownerKeyIn) return Promise.all([ownerKey.getAddress(), paymentKey.getAddress()]) .then(([ownerAddress, paymentAddress]) => { const txPromise = network.getConsensusHash() .then(consensusHash => makeTransferSkeleton( fullyQualifiedName, consensusHash, destinationAddress, keepZonefile )) .then((transferTX) => { const txB = TransactionBuilder .fromTransaction(transferTX, network.layer1) txB.setVersion(1) return txB }) return Promise.all([txPromise, network.getUTXOs(paymentAddress), network.getUTXOs(ownerAddress), network.getFeeRate()]) .then(([txB, payerUtxos, ownerUtxos, feeRate]) => { const ownerInput = addOwnerInput(ownerUtxos, ownerAddress, txB) const signingTxB = fundTransaction(txB, paymentAddress, payerUtxos, feeRate, ownerInput.value) return signInputs(signingTxB, paymentKey, [{ index: ownerInput.index, signer: ownerKey }]) }) }) .then(signingTxB => returnTransactionHex(signingTxB, buildIncomplete)) } /** * Generates a revoke transaction for a domain name. * @param {String} fullyQualifiedName - the name to revoke * @param {String | TransactionSigner} ownerKeyIn - a hex string of * the current owner's private key (or a TransactionSigner object) * @param {String | TransactionSigner} paymentKeyIn - a hex string of * the private key used to fund the transaction (or a * TransactionSigner object) * @param {boolean} buildIncomplete - optional boolean, defaults to false, * indicating whether the function should attempt to return an unsigned (or not fully signed) * transaction. Useful for passing around a TX for multi-sig input signing. * @returns {Promise} - a promise which resolves to the hex-encoded transaction. * this function *does not* perform the requisite safety checks -- please see * the safety module for those. * @private */ function makeRevoke(fullyQualifiedName: string, ownerKeyIn: string | TransactionSigner, paymentKeyIn: string | TransactionSigner, buildIncomplete: boolean = false ) { const network = config.network const paymentKey = getTransactionSigner(paymentKeyIn) const ownerKey = getTransactionSigner(ownerKeyIn) return Promise.all([ownerKey.getAddress(), paymentKey.getAddress()]) .then(([ownerAddress, paymentAddress]) => { const revokeTX = makeRevokeSkeleton(fullyQualifiedName) const txPromise = TransactionBuilder.fromTransaction(revokeTX, network.layer1) txPromise.setVersion(1) return Promise.all([txPromise, network.getUTXOs(paymentAddress), network.getUTXOs(ownerAddress), network.getFeeRate()]) .then(([txB, payerUtxos, ownerUtxos, feeRate]) => { const ownerInput = addOwnerInput(ownerUtxos, ownerAddress, txB) const signingTxB = fundTransaction(txB, paymentAddress, payerUtxos, feeRate, ownerInput.value) return signInputs(signingTxB, paymentKey, [{ index: ownerInput.index, signer: ownerKey }]) }) }) .then(signingTxB => returnTransactionHex(signingTxB, buildIncomplete)) } /** * Generates a renewal transaction for a domain name. * @param {String} fullyQualifiedName - the name to transfer * @param {String} destinationAddress - the address to receive the name after renewal * this address will receive a dust UTXO * @param {String | TransactionSigner} ownerKeyIn - a hex string of * the current owner's private key (or a TransactionSigner object) * @param {String | TransactionSigner} paymentKeyIn - a hex string of * the private key used to fund the renewal (or a TransactionSigner * object) * @param {String} zonefile - the zonefile data to include, if given (this will be hashed * to include in the transaction), the zonefile itself must be published * after the RENEWAL propagates. * @param {String} valueHash - the raw zone file hash to include (this will be used * instead of zonefile, if given). * @param {boolean} buildIncomplete - optional boolean, defaults to false, * indicating whether the function should attempt to return an unsigned (or not fully signed) * transaction. Useful for passing around a TX for multi-sig input signing. * @returns {Promise} - a promise which resolves to the hex-encoded transaction. * this function *does not* perform the requisite safety checks -- please see * the safety module for those. * @private */ function makeRenewal(fullyQualifiedName: string, destinationAddress: string, ownerKeyIn: string | TransactionSigner, paymentKeyIn: string | TransactionSigner, zonefile: string = null, valueHash: string = null, buildIncomplete: boolean = false ) { const network = config.network if (!valueHash && !!zonefile) { valueHash = hash160(Buffer.from(zonefile)).toString('hex') } const namespace = fullyQualifiedName.split('.').pop() const paymentKey = getTransactionSigner(paymentKeyIn) const ownerKey = getTransactionSigner(ownerKeyIn) return Promise.all([ownerKey.getAddress(), paymentKey.getAddress()]) .then(([ownerAddress, paymentAddress]) => { const txPromise = Promise.all([network.getNamePrice(fullyQualifiedName), network.getNamespaceBurnAddress(namespace)]) .then(([namePrice, burnAddress]) => makeRenewalSkeleton( fullyQualifiedName, destinationAddress, ownerAddress, burnAddress, namePrice, valueHash )) .then((tx) => { const txB = TransactionBuilder.fromTransaction(tx, network.layer1) txB.setVersion(1) return txB }) return Promise.all([txPromise, network.getUTXOs(paymentAddress), network.getUTXOs(ownerAddress), network.getFeeRate()]) .then(([txB, payerUtxos, ownerUtxos, feeRate]) => { const ownerInput = addOwnerInput(ownerUtxos, ownerAddress, txB, false) const txInner = getTransactionInsideBuilder(txB) const ownerOutput = txInner.outs[2] as TxOutput const ownerOutputAddr = bjsAddress.fromOutputScript( ownerOutput.script, network.layer1 ) if (ownerOutputAddr !== ownerAddress) { return Promise.reject( new Error(`Original owner ${ownerAddress} should have an output at ` + `index 2 in transaction was ${ownerOutputAddr}`) ) } ownerOutput.value = ownerInput.value const signingTxB = fundTransaction(txB, paymentAddress, payerUtxos, feeRate, ownerInput.value) return signInputs(signingTxB, paymentKey, [{ index: ownerInput.index, signer: ownerKey }]) }) }) .then(signingTxB => returnTransactionHex(signingTxB, buildIncomplete)) } /** * Generates a namespace preorder transaction for a namespace * @param {String} namespaceID - the namespace to pre-order * @param {String} revealAddress - the address to receive the namespace (this * must be passed as the 'revealAddress' in the namespace-reveal transaction) * @param {String | TransactionSigner} paymentKeyIn - a hex string of * the private key used to fund the transaction (or a * TransactionSigner object) * @param {boolean} buildIncomplete - optional boolean, defaults to false, * indicating whether the function should attempt to return an unsigned (or not fully signed) * transaction. Useful for passing around a TX for multi-sig input signing. * @returns {Promise} - a promise which resolves to the hex-encoded transaction. * this function *does not* perform the requisite safety checks -- please see * the safety module for those. * @private * * @ignore */ function makeNamespacePreorder(namespaceID: string, revealAddress: string, paymentKeyIn: string | TransactionSigner, buildIncomplete: boolean = false ) { const network = config.network const paymentKey = getTransactionSigner(paymentKeyIn) return paymentKey.getAddress().then((preorderAddress) => { const preorderPromise = Promise.all([network.getConsensusHash(), network.getNamespacePrice(namespaceID)]) .then(([consensusHash, namespacePrice]) => makeNamespacePreorderSkeleton( namespaceID, consensusHash, preorderAddress, revealAddress, namespacePrice)) return Promise.all([network.getUTXOs(preorderAddress), network.getFeeRate(), preorderPromise]) .then(([utxos, feeRate, preorderSkeleton]) => { const txB = TransactionBuilder.fromTransaction(preorderSkeleton, network.layer1) txB.setVersion(1) const changeIndex = 1 // preorder skeleton always creates a change output at index = 1 const signingTxB = fundTransaction(txB, preorderAddress, utxos, feeRate, 0, changeIndex) return signInputs(signingTxB, paymentKey) }) .then(signingTxB => returnTransactionHex(signingTxB, buildIncomplete)) }) } /** * Generates a namespace reveal transaction for a namespace * @param {BlockstackNamespace} namespace - the namespace to reveal * @param {String} revealAddress - the address to receive the namespace (this * must be passed as the 'revealAddress' in the namespace-reveal transaction) * @param {String | TransactionSigner} paymentKeyIn - a hex string (or * a TransactionSigner object) of the private key used to fund the * transaction * @param {boolean} buildIncomplete - optional boolean, defaults to false, * indicating whether the function should attempt to return an unsigned (or not fully signed) * transaction. Useful for passing around a TX for multi-sig input signing. * @returns {Promise} - a promise which resolves to the hex-encoded transaction. * this function *does not* perform the requisite safety checks -- please see * the safety module for those. * @private */ function makeNamespaceReveal(namespace: BlockstackNamespace, revealAddress: string, paymentKeyIn: string | TransactionSigner, buildIncomplete: boolean = false ) { const network = config.network if (!namespace.check()) { return Promise.reject(new Error('Invalid namespace')) } const namespaceRevealTX = makeNamespaceRevealSkeleton(namespace, revealAddress) const paymentKey = getTransactionSigner(paymentKeyIn) return paymentKey.getAddress().then( preorderAddress => Promise.all([network.getUTXOs(preorderAddress), network.getFeeRate()]) .then(([utxos, feeRate]) => { const txB = TransactionBuilder .fromTransaction(namespaceRevealTX, network.layer1) txB.setVersion(1) const signingTxB = fundTransaction(txB, preorderAddress, utxos, feeRate, 0) return signInputs(signingTxB, paymentKey) }) ) .then(signingTxB => returnTransactionHex(signingTxB, buildIncomplete)) } /** * Generates a namespace ready transaction for a namespace * @param {String} namespaceID - the namespace to launch * @param {String | TransactionSigner} revealKeyIn - the private key * of the 'revealAddress' used to reveal the namespace * @param {boolean} buildIncomplete - optional boolean, defaults to false, * indicating whether the function should attempt to return an unsigned (or not fully signed) * transaction. Useful for passing around a TX for multi-sig input signing. * @returns {Promise} - a promise which resolves to the hex-encoded transaction. * this function *does not* perform the requisite safety checks -- please see * the safety module for those. * @private */ function makeNamespaceReady(namespaceID: string, revealKeyIn: string | TransactionSigner, buildIncomplete: boolean = false ) { const network = config.network const namespaceReadyTX = makeNamespaceReadySkeleton(namespaceID) const revealKey = getTransactionSigner(revealKeyIn) return revealKey.getAddress().then( revealAddress => Promise.all([network.getUTXOs(revealAddress), network.getFeeRate()]) .then(([utxos, feeRate]) => { const txB = TransactionBuilder.fromTransaction(namespaceReadyTX, network.layer1) txB.setVersion(1) const signingTxB = fundTransaction(txB, revealAddress, utxos, feeRate, 0) return signInputs(signingTxB, revealKey) }) ) .then(signingTxB => returnTransactionHex(signingTxB, buildIncomplete)) } /** * Generates a name import transaction for a namespace * @param {String} name - the name to import * @param {String} recipientAddr - the address to receive the name * @param {String} zonefileHash - the hash of the zonefile to give this name * @param {String | TransactionSigner} importerKeyIn - the private key * that pays for the import * @param {boolean} buildIncomplete - optional boolean, defaults to false, * indicating whether the function should attempt to return an unsigned (or not fully signed) * transaction. Useful for passing around a TX for multi-sig input signing. * @returns {Promise} - a promise which resolves to the hex-encoded transaction. * this function does not perform the requisite safety checks -- please see * the safety module for those. * @private */ function makeNameImport(name: string, recipientAddr: string, zonefileHash: string, importerKeyIn: string | TransactionSigner, buildIncomplete: boolean = false ) { const network = config.network const nameImportTX = makeNameImportSkeleton(name, recipientAddr, zonefileHash) const importerKey = getTransactionSigner(importerKeyIn) return importerKey.getAddress().then( importerAddress => Promise.all([network.getUTXOs(importerAddress), network.getFeeRate()]) .then(([utxos, feeRate]) => { const txB = TransactionBuilder.fromTransaction(nameImportTX, network.layer1) const signingTxB = fundTransaction(txB, importerAddress, utxos, feeRate, 0) return signInputs(signingTxB, importerKey) }) ) .then(signingTxB => returnTransactionHex(signingTxB, buildIncomplete)) } /** * Generates an announce transaction * @param {String} messageHash - the hash of the message to send. Should be * an already-announced zone file hash * @param {String | TransactionSigner} senderKeyIn - the private key * that pays for the transaction. Should be the key that owns the * name that the message recipients subscribe to * @param {boolean} buildIncomplete - optional boolean, defaults to false, * indicating whether the function should attempt to return an unsigned (or not fully signed) * transaction. Useful for passing around a TX for multi-sig input signing. * @returns {Promise} - a promise which resolves to the hex-encoded transaction. * this function does not perform the requisite safety checks -- please see the * safety module for those. * @private */ function makeAnnounce(messageHash: string, senderKeyIn: string | TransactionSigner, buildIncomplete: boolean = false ) { const network = config.network const announceTX = makeAnnounceSkeleton(messageHash) const senderKey = getTransactionSigner(senderKeyIn) return senderKey.getAddress().then( senderAddress => Promise.all([network.getUTXOs(senderAddress), network.getFeeRate()]) .then(([utxos, feeRate]) => { const txB = TransactionBuilder.fromTransaction(announceTX, network.layer1) const signingTxB = fundTransaction(txB, senderAddress, utxos, feeRate, 0) return signInputs(signingTxB, senderKey) }) ) .then(signingTxB => returnTransactionHex(signingTxB, buildIncomplete)) } /** * Generates a token-transfer transaction * @param {String} recipientAddress - the address to receive the tokens * @param {String} tokenType - the type of tokens to send * @param {Object} tokenAmount - the BigInteger encoding of an unsigned 64-bit number of * tokens to send * @param {String} scratchArea - an arbitrary string to include with the transaction * @param {String | TransactionSigner} senderKeyIn - the hex-encoded private key to send * the tokens * @param {String | TransactionSigner} btcFunderKeyIn - the hex-encoded private key to fund * the bitcoin fees for the transaction. Optional -- if not passed, will attempt to * fund with sender key. * @param {boolean} buildIncomplete - optional boolean, defaults to false, * indicating whether the function should attempt to return an unsigned (or not fully signed) * transaction. Useful for passing around a TX for multi-sig input signing. * @returns {Promise} - a promise which resolves to the hex-encoded transaction. * This function does not perform the requisite safety checks -- please see the * safety module for those. * @private */ function makeTokenTransfer(recipientAddress: string, tokenType: string, tokenAmount: BN, scratchArea: string, senderKeyIn: string | TransactionSigner, btcFunderKeyIn?: string | TransactionSigner, buildIncomplete: boolean = false ) { const network = config.network const separateFunder = !!btcFunderKeyIn const senderKey = getTransactionSigner(senderKeyIn) const btcKey = btcFunderKeyIn ? getTransactionSigner(btcFunderKeyIn) : senderKey const txPromise = network.getConsensusHash() .then(consensusHash => makeTokenTransferSkeleton( recipientAddress, '00000000000000000000000000000000', tokenType, tokenAmount, scratchArea)) return Promise.all([senderKey.getAddress(), btcKey.getAddress()]) .then(([senderAddress, btcAddress]) => { const btcUTXOsPromise = separateFunder ? network.getUTXOs(btcAddress) : Promise.resolve<UTXO[]>([]) return Promise.all([ network.getUTXOs(senderAddress), btcUTXOsPromise, network.getFeeRate(), txPromise ]).then(([senderUTXOs, btcUTXOs, feeRate, tokenTransferTX]) => { const txB = TransactionBuilder.fromTransaction(tokenTransferTX, network.layer1) if (separateFunder) { const payerInput = addOwnerInput(senderUTXOs, senderAddress, txB) const signingTxB = fundTransaction(txB, btcAddress, btcUTXOs, feeRate, payerInput.value) return signInputs(signingTxB, btcKey, [{ index: payerInput.index, signer: senderKey }]) } else { const signingTxB = fundTransaction(txB, senderAddress, senderUTXOs, feeRate, 0) return signInputs(signingTxB, senderKey) } }) }) .then(signingTxB => returnTransactionHex(signingTxB, buildIncomplete)) } /** * Generates a bitcoin spend to a specified address. This will fund up to `amount` * of satoshis from the payer's UTXOs. It will generate a change output if and only * if the amount of leftover change is *greater* than the additional fees associated * with the extra output. If the requested amount is not enough to fund the transaction's * associated fees, then this will reject with a InvalidAmountError * * UTXOs are selected largest to smallest, and UTXOs which cannot fund the fees associated * with their own input will not be included. * * If you specify an amount > the total balance of the payer address, then this will * generate a maximum spend transaction * * @param {String} destinationAddress - the address to receive the bitcoin payment * @param {String | TransactionSigner} paymentKeyIn - the private key * used to fund the bitcoin spend * @param {number} amount - the amount in satoshis for the payment address to * spend in this transaction * @param {boolean} buildIncomplete - optional boolean, defaults to false, * indicating whether the function should attempt to return an unsigned (or not fully signed) * transaction. Useful for passing around a TX for multi-sig input signing. * @returns {Promise} - a promise which resolves to the hex-encoded transaction. * @private */ function makeBitcoinSpend(destinationAddress: string, paymentKeyIn: string | TransactionSigner, amount: number, buildIncomplete: boolean = false ) { if (amount <= 0) { return Promise.reject(new InvalidParameterError('amount', 'amount must be greater than zero')) } const network = config.network const paymentKey = getTransactionSigner(paymentKeyIn) return paymentKey.getAddress().then( paymentAddress => Promise.all([network.getUTXOs(paymentAddress), network.getFeeRate()]) .then(([utxos, feeRate]) => { const txB = new TransactionBuilder(network.layer1) txB.setVersion(1) const destinationIndex = txB.addOutput(destinationAddress, 0) // will add utxos up to _amount_ and return the amount of leftover _change_ let change try { change = addUTXOsToFund(txB, utxos, amount, feeRate, false) } catch (err) { if (err.name === 'NotEnoughFundsError') { // actual amount funded = amount requested - remainder amount -= err.leftToFund change = 0 } else { throw err } } let feesToPay = feeRate * estimateTXBytes(txB, 0, 0) const feeForChange = feeRate * (estimateTXBytes(txB, 0, 1)) - feesToPay // it's worthwhile to add a change output if (change > feeForChange) { feesToPay += feeForChange txB.addOutput(paymentAddress, change) } // now let's compute how much output is leftover once we pay the fees. const outputAmount = amount - feesToPay if (outputAmount < DUST_MINIMUM) { throw new InvalidAmountError(feesToPay, amount) } // we need to manually set the output values now const txInner = getTransactionInsideBuilder(txB) const txOut = txInner.outs[destinationIndex] as TxOutput txOut.value = outputAmount // ready to sign. return signInputs(txB, paymentKey) }) ) .then(signingTxB => returnTransactionHex(signingTxB, buildIncomplete)) } export const transactions = { makeRenewal, makeUpdate, makePreorder, makeRegister, makeTransfer, makeRevoke, makeNamespacePreorder, makeNamespaceReveal, makeNamespaceReady, makeBitcoinSpend, makeNameImport, makeAnnounce, makeTokenTransfer, BlockstackNamespace, estimatePreorder, estimateRegister, estimateTransfer, estimateUpdate, estimateRenewal, estimateRevoke, estimateNamespacePreorder, estimateNamespaceReveal, estimateNamespaceReady, estimateNameImport, estimateAnnounce, estimateTokenTransfer } |