@btc-vision/transaction 1.0.96 → 1.0.99
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +4 -7
- package/.prettierrc.json +11 -11
- package/CONTRIBUTING.md +71 -71
- package/LICENSE.md +62 -62
- package/README.md +76 -76
- package/browser/_version.d.ts +1 -1
- package/browser/bytecode/Compressor.d.ts +0 -1
- package/browser/crypto/crypto-browser.d.ts +1 -30
- package/browser/crypto/crypto.d.ts +0 -1
- package/browser/generators/AddressGenerator.d.ts +0 -1
- package/browser/generators/Generator.d.ts +0 -1
- package/browser/generators/builders/CalldataGenerator.d.ts +0 -1
- package/browser/generators/builders/DeploymentGenerator.d.ts +0 -1
- package/browser/generators/builders/MultiSignGenerator.d.ts +0 -1
- package/browser/index.js +1 -1
- package/browser/keypair/EcKeyPair.d.ts +0 -1
- package/browser/keypair/Wallet.d.ts +0 -1
- package/browser/signer/TweakedSigner.d.ts +0 -1
- package/browser/transaction/browser/BrowserSignerBase.d.ts +0 -1
- package/browser/transaction/browser/extensions/UnisatSigner.d.ts +0 -1
- package/browser/transaction/builders/DeploymentTransaction.d.ts +0 -1
- package/browser/transaction/builders/InteractionTransaction.d.ts +0 -1
- package/browser/transaction/builders/MultiSignTransaction.d.ts +0 -1
- package/browser/transaction/builders/SharedInteractionTransaction.d.ts +0 -1
- package/browser/transaction/builders/TransactionBuilder.d.ts +0 -1
- package/browser/transaction/builders/UnwrapSegwitTransaction.d.ts +0 -1
- package/browser/transaction/builders/UnwrapTransaction.d.ts +0 -1
- package/browser/transaction/builders/WrapTransaction.d.ts +0 -1
- package/browser/transaction/interfaces/ITransactionParameters.d.ts +0 -1
- package/browser/transaction/interfaces/Tap.d.ts +0 -1
- package/browser/transaction/shared/P2TR_MS.d.ts +0 -1
- package/browser/transaction/shared/TweakedTransaction.d.ts +0 -1
- package/browser/utils/BitcoinUtils.d.ts +0 -1
- package/browser/verification/TapscriptVerificator.d.ts +0 -1
- package/browser/wbtc/WrappedGenerationParameters.d.ts +0 -1
- package/build/bytecode/Compressor.d.ts +0 -1
- package/build/bytecode/Compressor.js +4 -4
- package/build/crypto/crypto.d.ts +0 -1
- package/build/generators/AddressGenerator.d.ts +0 -1
- package/build/generators/Generator.d.ts +0 -1
- package/build/generators/Generator.js +3 -5
- package/build/generators/builders/CalldataGenerator.d.ts +0 -1
- package/build/generators/builders/DeploymentGenerator.d.ts +0 -1
- package/build/generators/builders/{MultisignGenerator.d.ts → MultiSignGenerator.d.ts} +0 -1
- package/build/generators/builders/{MultisignGenerator.js → MultiSignGenerator.js} +1 -1
- package/build/keypair/EcKeyPair.d.ts +0 -1
- package/build/keypair/EcKeyPair.js +2 -2
- package/build/keypair/Wallet.d.ts +0 -1
- package/build/keypair/Wallet.js +0 -5
- package/build/metadata/ContractBaseMetadata.js +0 -1
- package/build/metadata/contracts/wBTC.js +3 -5
- package/build/metadata/tokens.js +27 -25
- package/build/signer/TweakedSigner.d.ts +0 -1
- package/build/transaction/browser/BrowserSignerBase.d.ts +0 -1
- package/build/transaction/browser/extensions/UnisatSigner.d.ts +0 -1
- package/build/transaction/browser/extensions/UnisatSigner.js +1 -6
- package/build/transaction/builders/DeploymentTransaction.d.ts +0 -1
- package/build/transaction/builders/DeploymentTransaction.js +25 -33
- package/build/transaction/builders/FundingTransaction.js +1 -3
- package/build/transaction/builders/InteractionTransaction.d.ts +0 -1
- package/build/transaction/builders/InteractionTransaction.js +2 -5
- package/build/transaction/builders/MultiSignTransaction.d.ts +0 -1
- package/build/transaction/builders/MultiSignTransaction.js +66 -73
- package/build/transaction/builders/SharedInteractionTransaction.d.ts +0 -1
- package/build/transaction/builders/SharedInteractionTransaction.js +20 -25
- package/build/transaction/builders/TransactionBuilder.d.ts +0 -1
- package/build/transaction/builders/TransactionBuilder.js +12 -21
- package/build/transaction/builders/UnwrapSegwitTransaction.d.ts +0 -1
- package/build/transaction/builders/UnwrapSegwitTransaction.js +4 -10
- package/build/transaction/builders/UnwrapTransaction.d.ts +0 -1
- package/build/transaction/builders/UnwrapTransaction.js +5 -11
- package/build/transaction/builders/WrapTransaction.d.ts +0 -1
- package/build/transaction/builders/WrapTransaction.js +5 -12
- package/build/transaction/interfaces/ITransactionParameters.d.ts +0 -1
- package/build/transaction/interfaces/Tap.d.ts +0 -1
- package/build/transaction/processor/PsbtTransaction.js +3 -4
- package/build/transaction/shared/P2TR_MS.d.ts +0 -1
- package/build/transaction/shared/TweakedTransaction.d.ts +0 -1
- package/build/transaction/shared/TweakedTransaction.js +11 -16
- package/build/utils/BitcoinUtils.d.ts +0 -1
- package/build/utxo/OPNetLimitedProvider.js +2 -3
- package/build/verification/TapscriptVerificator.d.ts +0 -1
- package/build/verification/TapscriptVerificator.js +1 -1
- package/build/wbtc/UnwrapGeneration.js +0 -2
- package/build/wbtc/WrappedGenerationParameters.d.ts +0 -1
- package/build/wbtc/WrappedGenerationParameters.js +0 -6
- package/cjs/package.json +3 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +43 -0
- package/docs/assets/icons.js +15 -0
- package/docs/assets/icons.svg +1 -0
- package/docs/assets/main.js +59 -0
- package/docs/assets/material-style.css +247 -0
- package/docs/assets/navigation.js +1 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1412 -0
- package/docs/classes/AddressGenerator.html +177 -0
- package/docs/classes/AddressVerificator.html +177 -0
- package/docs/classes/BitcoinUtils.html +196 -0
- package/docs/classes/CalldataGenerator.html +213 -0
- package/docs/classes/Compressor.html +185 -0
- package/docs/classes/ContractBaseMetadata.html +182 -0
- package/docs/classes/CustomKeypair.html +189 -0
- package/docs/classes/DeploymentGenerator.html +201 -0
- package/docs/classes/DeploymentTransaction.html +465 -0
- package/docs/classes/EcKeyPair.html +280 -0
- package/docs/classes/FundingTransaction.html +425 -0
- package/docs/classes/Generator.html +199 -0
- package/docs/classes/InteractionTransaction.html +467 -0
- package/docs/classes/MultiSignGenerator.html +178 -0
- package/docs/classes/MultiSignTransaction.html +490 -0
- package/docs/classes/OPNetLimitedProvider.html +225 -0
- package/docs/classes/OPNetTokenAddressManager.html +182 -0
- package/docs/classes/P2TR_MS.html +186 -0
- package/docs/classes/PsbtTransaction.html +320 -0
- package/docs/classes/SharedInteractionTransaction.html +477 -0
- package/docs/classes/TapscriptVerificator.html +180 -0
- package/docs/classes/TransactionBuilder.html +423 -0
- package/docs/classes/TransactionFactory.html +230 -0
- package/docs/classes/TweakedSigner.html +181 -0
- package/docs/classes/TweakedTransaction.html +301 -0
- package/docs/classes/UnisatSigner.html +199 -0
- package/docs/classes/UnwrapGeneration.html +177 -0
- package/docs/classes/UnwrapTransaction.html +518 -0
- package/docs/classes/Wallet.html +204 -0
- package/docs/classes/WrapTransaction.html +509 -0
- package/docs/classes/WrappedGeneration.html +187 -0
- package/docs/classes/wBTC.html +190 -0
- package/docs/enums/ChainId.html +176 -0
- package/docs/enums/Consensus.html +204 -0
- package/docs/enums/Features.html +175 -0
- package/docs/enums/MessageType.html +176 -0
- package/docs/enums/OPNetNetwork.html +177 -0
- package/docs/enums/PSBTTypes.html +175 -0
- package/docs/enums/TransactionSequence.html +177 -0
- package/docs/enums/TransactionType.html +181 -0
- package/docs/enums/UnisatChainType.html +181 -0
- package/docs/enums/UnisatNetwork.html +177 -0
- package/docs/hierarchy.html +174 -0
- package/docs/index.html +215 -0
- package/docs/interfaces/Balance.html +177 -0
- package/docs/interfaces/BitcoinTransferResponse.html +178 -0
- package/docs/interfaces/BroadcastResponse.html +181 -0
- package/docs/interfaces/BroadcastTransactionOptions.html +176 -0
- package/docs/interfaces/BroadcastedTransaction.html +184 -0
- package/docs/interfaces/ConsensusConfig.html +182 -0
- package/docs/interfaces/ContractAddressVerificationParams.html +179 -0
- package/docs/interfaces/DeploymentResult.html +178 -0
- package/docs/interfaces/FetchUTXOParams.html +178 -0
- package/docs/interfaces/FetchUTXOParamsMultiAddress.html +178 -0
- package/docs/interfaces/FundingTransactionResponse.html +178 -0
- package/docs/interfaces/GenerationConstraints.html +182 -0
- package/docs/interfaces/IDeploymentParameters.html +186 -0
- package/docs/interfaces/IFundingTransactionParameters.html +186 -0
- package/docs/interfaces/IInteractionParameters.html +187 -0
- package/docs/interfaces/ITransactionParameters.html +184 -0
- package/docs/interfaces/ITweakedTransactionData.html +178 -0
- package/docs/interfaces/IUnwrapParameters.html +189 -0
- package/docs/interfaces/IWBTCUTXODocument.html +180 -0
- package/docs/interfaces/IWallet.html +181 -0
- package/docs/interfaces/IWrapParameters.html +190 -0
- package/docs/interfaces/MultiSignFromBase64Params.html +188 -0
- package/docs/interfaces/MultiSignParameters.html +188 -0
- package/docs/interfaces/OPNetTokenMetadata.html +179 -0
- package/docs/interfaces/PsbtInputExtended.html +194 -0
- package/docs/interfaces/PsbtOutputExtendedAddress.html +183 -0
- package/docs/interfaces/PsbtOutputExtendedScript.html +183 -0
- package/docs/interfaces/PsbtSignatureOptions.html +176 -0
- package/docs/interfaces/PsbtTransactionData.html +179 -0
- package/docs/interfaces/RawUTXOResponse.html +178 -0
- package/docs/interfaces/SharedInteractionParameters.html +187 -0
- package/docs/interfaces/TapLeafScript.html +177 -0
- package/docs/interfaces/ToSignInputAddress.html +178 -0
- package/docs/interfaces/ToSignInputPublicKey.html +178 -0
- package/docs/interfaces/TweakSettings.html +179 -0
- package/docs/interfaces/UTXO.html +178 -0
- package/docs/interfaces/Unisat.html +190 -0
- package/docs/interfaces/UnisatChainInfo.html +177 -0
- package/docs/interfaces/UnwrapResult.html +181 -0
- package/docs/interfaces/UnwrappedGenerationParameters.html +178 -0
- package/docs/interfaces/UpdateInput.html +175 -0
- package/docs/interfaces/VaultUTXOs.html +178 -0
- package/docs/interfaces/Web3Provider.html +180 -0
- package/docs/interfaces/WrapResult.html +179 -0
- package/docs/interfaces/WrappedGenerationParameters.html +184 -0
- package/docs/modules.html +289 -0
- package/docs/types/FromBase64Params.html +174 -0
- package/docs/types/IDeploymentParametersWithoutSigner.html +174 -0
- package/docs/types/IUnwrapParametersWithoutSigner.html +174 -0
- package/docs/types/IWrapParametersWithoutSigner.html +174 -0
- package/docs/types/InteractionParametersWithoutSigner.html +174 -0
- package/docs/types/PsbtOutputExtended.html +174 -0
- package/docs/types/ToSignInput.html +174 -0
- package/docs/variables/FACTORY_ADDRESS_FRACTAL.html +174 -0
- package/docs/variables/FACTORY_ADDRESS_REGTEST.html +174 -0
- package/docs/variables/FACTORY_ADDRESS_TESTNET.html +174 -0
- package/docs/variables/MOTO_ADDRESS_FRACTAL.html +174 -0
- package/docs/variables/MOTO_ADDRESS_REGTEST.html +174 -0
- package/docs/variables/MOTO_ADDRESS_TESTNET.html +174 -0
- package/docs/variables/OPNetConsensusConfig.html +174 -0
- package/docs/variables/OPNetMetadata.html +174 -0
- package/docs/variables/POOL_ADDRESS_FRACTAL.html +174 -0
- package/docs/variables/POOL_ADDRESS_REGTEST.html +174 -0
- package/docs/variables/POOL_ADDRESS_TESTNET.html +174 -0
- package/docs/variables/ROUTER_ADDRESS_FRACTAL.html +174 -0
- package/docs/variables/ROUTER_ADDRESS_REGTEST.html +174 -0
- package/docs/variables/ROUTER_ADDRESS_TESTNET.html +174 -0
- package/docs/variables/RoswellConsensus.html +174 -0
- package/docs/variables/WBTC_ADDRESS_FRACTAL.html +174 -0
- package/docs/variables/WBTC_ADDRESS_REGTEST.html +174 -0
- package/docs/variables/WBTC_ADDRESS_TESTNET.html +174 -0
- package/docs/variables/currentConsensus.html +174 -0
- package/docs/variables/currentConsensusConfig.html +174 -0
- package/docs/variables/version.html +174 -0
- package/gulpfile.js +49 -133
- package/package.json +112 -113
- package/src/bytecode/Compressor.ts +27 -27
- package/src/consensus/metadata/RoswellConsensus.ts +26 -26
- package/src/crypto/crypto.ts +1 -1
- package/src/generators/Generator.ts +75 -75
- package/src/generators/builders/MultiSignGenerator.ts +73 -73
- package/src/metadata/contracts/wBTC.ts +60 -60
- package/src/metadata/tokens.ts +135 -135
- package/src/network/ChainId.ts +6 -6
- package/src/opnet.ts +82 -82
- package/src/signer/TweakedSigner.ts +56 -56
- package/src/transaction/builders/DeploymentTransaction.ts +356 -356
- package/src/transaction/builders/MultiSignTransaction.ts +659 -659
- package/src/transaction/builders/UnwrapSegwitTransaction.ts +365 -365
- package/src/transaction/builders/UnwrapTransaction.ts +507 -507
- package/src/transaction/enums/TransactionType.ts +9 -9
- package/src/transaction/processor/PsbtTransaction.ts +173 -173
- package/src/transaction/shared/P2TR_MS.ts +42 -42
- package/src/transaction/shared/TweakedTransaction.ts +539 -539
- package/src/utils/BitcoinUtils.ts +96 -96
- package/tsconfig.base.json +27 -43
- package/tsconfig.json +15 -8
- package/tsconfig.webpack.json +22 -22
- package/EPOCH.md +0 -250
- package/VAULT_TARGET_CONSOLIDATION.md +0 -111
- package/browser/network/NetworkInformation.d.ts +0 -6
- package/browser/transaction/browser/BrowserSigner.d.ts +0 -9
- package/browser/transaction/builders/TapUnwarpTransaction.d.ts +0 -40
- package/browser/transaction/builders/UnwarpTransaction.d.ts +0 -34
- package/browser/utxo/UTXOManager.d.ts +0 -7
- package/build/Utils.d.ts +0 -0
- package/build/Utils.js +0 -1
- package/build/consensus/metadata/RoswsellConsensus.d.ts +0 -2
- package/build/consensus/metadata/RoswsellConsensus.js +0 -4
- package/build/contracts/ContractMetadataManager.d.ts +0 -0
- package/build/contracts/ContractMetadataManager.js +0 -1
- package/build/generators/OPNetAddressGenerator.d.ts +0 -0
- package/build/generators/OPNetAddressGenerator.js +0 -1
- package/build/generators/builders/DeploymentGeneratorV2.d.ts +0 -13
- package/build/generators/builders/DeploymentGeneratorV2.js +0 -57
- package/build/generators/builders/UnwrapGenerator.d.ts +0 -8
- package/build/generators/builders/UnwrapGenerator.js +0 -79
- package/build/keypair/interfaces/GeneratedWallet.d.ts +0 -5
- package/build/keypair/interfaces/GeneratedWallet.js +0 -1
- package/build/metadata/CommonContracts.d.ts +0 -6
- package/build/metadata/CommonContracts.js +0 -5
- package/build/metadata/ContractMetadataManager.d.ts +0 -1
- package/build/metadata/ContractMetadataManager.js +0 -9
- package/build/metadata/contracts/ContractBase.d.ts +0 -9
- package/build/metadata/contracts/ContractBase.js +0 -13
- package/build/metadata/contracts/ContractBaseMetadata.d.ts +0 -9
- package/build/metadata/contracts/ContractBaseMetadata.js +0 -13
- package/build/metadata/contracts/ContractMetadataManager.d.ts +0 -0
- package/build/metadata/contracts/ContractMetadataManager.js +0 -1
- package/build/network/NetworkConverter.d.ts +0 -0
- package/build/network/NetworkConverter.js +0 -14
- package/build/network/NetworkInformation.d.ts +0 -6
- package/build/network/NetworkInformation.js +0 -1
- package/build/scripts/Regtest.d.ts +0 -2
- package/build/scripts/Regtest.js +0 -15
- package/build/scripts/test.d.ts +0 -1
- package/build/scripts/test.js +0 -74
- package/build/signer/Regtest.d.ts +0 -2
- package/build/signer/Regtest.js +0 -15
- package/build/tests/Regtest.d.ts +0 -3
- package/build/tests/Regtest.js +0 -29
- package/build/tests/adaptPSBT.d.ts +0 -1
- package/build/tests/adaptPSBT.js +0 -44
- package/build/tests/btc/send.d.ts +0 -1
- package/build/tests/btc/send.js +0 -35
- package/build/tests/btc/transfer.d.ts +0 -1
- package/build/tests/btc/transfer.js +0 -35
- package/build/tests/createPairReg.d.ts +0 -1
- package/build/tests/createPairReg.js +0 -73
- package/build/tests/deploy/deployMoto.d.ts +0 -4
- package/build/tests/deploy/deployMoto.js +0 -89
- package/build/tests/deploy/deployPool.d.ts +0 -1
- package/build/tests/deploy/deployPool.js +0 -5
- package/build/tests/deploy/deployStep1.d.ts +0 -1
- package/build/tests/deploy/deployStep1.js +0 -5
- package/build/tests/deploy/deployStep2.d.ts +0 -1
- package/build/tests/deploy/deployStep2.js +0 -5
- package/build/tests/deploy/deployStep3.d.ts +0 -1
- package/build/tests/deploy/deployStep3.js +0 -5
- package/build/tests/deploy.d.ts +0 -1
- package/build/tests/deploy.js +0 -41
- package/build/tests/deployMotoRegStep1.d.ts +0 -1
- package/build/tests/deployMotoRegStep1.js +0 -85
- package/build/tests/deployReg.d.ts +0 -1
- package/build/tests/deployReg.js +0 -85
- package/build/tests/factory/createPairReg.d.ts +0 -1
- package/build/tests/factory/createPairReg.js +0 -13
- package/build/tests/gen.d.ts +0 -1
- package/build/tests/gen.js +0 -19
- package/build/tests/interaction.d.ts +0 -5
- package/build/tests/interaction.js +0 -62
- package/build/tests/massWrapReg.d.ts +0 -1
- package/build/tests/massWrapReg.js +0 -105
- package/build/tests/mineReg.d.ts +0 -1
- package/build/tests/mineReg.js +0 -19
- package/build/tests/moto/airdropToken.d.ts +0 -1
- package/build/tests/moto/airdropToken.js +0 -21
- package/build/tests/moto/airdropTokens.d.ts +0 -1
- package/build/tests/moto/airdropTokens.js +0 -60
- package/build/tests/moto/allowance.d.ts +0 -1
- package/build/tests/moto/allowance.js +0 -6
- package/build/tests/moto/approve.d.ts +0 -1
- package/build/tests/moto/approve.js +0 -10
- package/build/tests/moto/approveWBTC.d.ts +0 -1
- package/build/tests/moto/approveWBTC.js +0 -12
- package/build/tests/moto/balanceOf.d.ts +0 -1
- package/build/tests/moto/balanceOf.js +0 -12
- package/build/tests/moto/transfer.d.ts +0 -1
- package/build/tests/moto/transfer.js +0 -16
- package/build/tests/motoswap/airdropToken.d.ts +0 -11
- package/build/tests/motoswap/airdropToken.js +0 -36
- package/build/tests/motoswap/deployMoto.d.ts +0 -4
- package/build/tests/motoswap/deployMoto.js +0 -89
- package/build/tests/motoswap/deployMotoRegStep1.d.ts +0 -1
- package/build/tests/motoswap/deployMotoRegStep1.js +0 -91
- package/build/tests/motoswap/deployMotoRegStep2.d.ts +0 -1
- package/build/tests/motoswap/deployMotoRegStep2.js +0 -91
- package/build/tests/motoswap/deployPool.d.ts +0 -1
- package/build/tests/motoswap/deployPool.js +0 -5
- package/build/tests/motoswap/deployStep1.d.ts +0 -1
- package/build/tests/motoswap/deployStep1.js +0 -5
- package/build/tests/motoswap/deployStep2.d.ts +0 -1
- package/build/tests/motoswap/deployStep2.js +0 -5
- package/build/tests/motoswap/deployStep3.d.ts +0 -1
- package/build/tests/motoswap/deployStep3.js +0 -5
- package/build/tests/motoswap/interaction.d.ts +0 -3
- package/build/tests/motoswap/interaction.js +0 -63
- package/build/tests/motoswap/routerAddLiquidity.d.ts +0 -11
- package/build/tests/motoswap/routerAddLiquidity.js +0 -35
- package/build/tests/motoswap-router/addLiquidity.d.ts +0 -11
- package/build/tests/motoswap-router/addLiquidity.js +0 -36
- package/build/tests/motoswap-router/deployMoto.d.ts +0 -4
- package/build/tests/motoswap-router/deployMoto.js +0 -89
- package/build/tests/motoswap-router/deployPool.d.ts +0 -1
- package/build/tests/motoswap-router/deployPool.js +0 -5
- package/build/tests/motoswap-router/deployStep1.d.ts +0 -1
- package/build/tests/motoswap-router/deployStep1.js +0 -5
- package/build/tests/motoswap-router/deployStep2.d.ts +0 -1
- package/build/tests/motoswap-router/deployStep2.js +0 -5
- package/build/tests/motoswap-router/deployStep3.d.ts +0 -1
- package/build/tests/motoswap-router/deployStep3.js +0 -5
- package/build/tests/motoswap-router/getAmountsOut.d.ts +0 -5
- package/build/tests/motoswap-router/getAmountsOut.js +0 -34
- package/build/tests/motoswap-router/routerAddLiquidity.d.ts +0 -11
- package/build/tests/motoswap-router/routerAddLiquidity.js +0 -35
- package/build/tests/motoswap-router/swap.d.ts +0 -8
- package/build/tests/motoswap-router/swap.js +0 -24
- package/build/tests/multisign.d.ts +0 -1
- package/build/tests/multisign.js +0 -47
- package/build/tests/multisign2.d.ts +0 -1
- package/build/tests/multisign2.js +0 -27
- package/build/tests/pool/DecodePoolAddress.d.ts +0 -6
- package/build/tests/pool/DecodePoolAddress.js +0 -12
- package/build/tests/pool/decodeReserves.d.ts +0 -5
- package/build/tests/pool/decodeReserves.js +0 -13
- package/build/tests/pool/reserves.d.ts +0 -1
- package/build/tests/pool/reserves.js +0 -18
- package/build/tests/shared/Utils.d.ts +0 -2
- package/build/tests/shared/Utils.js +0 -14
- package/build/tests/shared/interaction.d.ts +0 -7
- package/build/tests/shared/interaction.js +0 -85
- package/build/tests/shared/tokens.d.ts +0 -6
- package/build/tests/shared/tokens.js +0 -5
- package/build/tests/stakeReg.d.ts +0 -1
- package/build/tests/stakeReg.js +0 -73
- package/build/tests/stakedReg.d.ts +0 -1
- package/build/tests/stakedReg.js +0 -28
- package/build/tests/test.d.ts +0 -1
- package/build/tests/test.js +0 -51
- package/build/tests/test2.d.ts +0 -1
- package/build/tests/test2.js +0 -73
- package/build/tests/testReg.d.ts +0 -1
- package/build/tests/testReg.js +0 -91
- package/build/tests/tokens.d.ts +0 -6
- package/build/tests/tokens.js +0 -5
- package/build/tests/totalRewardReg.d.ts +0 -1
- package/build/tests/totalRewardReg.js +0 -28
- package/build/tests/transfer.d.ts +0 -1
- package/build/tests/transfer.js +0 -74
- package/build/tests/transferReg.d.ts +0 -1
- package/build/tests/transferReg.js +0 -74
- package/build/tests/unStakeReg.d.ts +0 -1
- package/build/tests/unStakeReg.js +0 -72
- package/build/tests/unwrapReg.d.ts +0 -1
- package/build/tests/unwrapReg.js +0 -61
- package/build/tests/unwrapReg2.d.ts +0 -1
- package/build/tests/unwrapReg2.js +0 -56
- package/build/tests/unwrapRegSegwit.d.ts +0 -1
- package/build/tests/unwrapRegSegwit.js +0 -83
- package/build/tests/wbtc/approve.d.ts +0 -1
- package/build/tests/wbtc/approve.js +0 -6
- package/build/tests/wbtc/approveWBTC.d.ts +0 -1
- package/build/tests/wbtc/approveWBTC.js +0 -12
- package/build/tests/wbtc/massWrapReg.d.ts +0 -1
- package/build/tests/wbtc/massWrapReg.js +0 -105
- package/build/tests/wbtc/transfer.d.ts +0 -1
- package/build/tests/wbtc/transfer.js +0 -16
- package/build/tests/wbtc/transferReg.d.ts +0 -1
- package/build/tests/wbtc/transferReg.js +0 -16
- package/build/tests/wbtc/unStakeReg.d.ts +0 -1
- package/build/tests/wbtc/unStakeReg.js +0 -72
- package/build/tests/wbtc/unwrapReg.d.ts +0 -1
- package/build/tests/wbtc/unwrapReg.js +0 -60
- package/build/tests/wbtc/unwrapRegSegwit.d.ts +0 -1
- package/build/tests/wbtc/unwrapRegSegwit.js +0 -83
- package/build/tests/wbtc/withdrawalRequestReg.d.ts +0 -1
- package/build/tests/wbtc/withdrawalRequestReg.js +0 -71
- package/build/tests/wbtc/wrapReg.d.ts +0 -1
- package/build/tests/wbtc/wrapReg.js +0 -65
- package/build/tests/wbtc/wrapTest.d.ts +0 -1
- package/build/tests/wbtc/wrapTest.js +0 -66
- package/build/tests/withdrawalRequestReg.d.ts +0 -1
- package/build/tests/withdrawalRequestReg.js +0 -71
- package/build/tests/wrap.d.ts +0 -1
- package/build/tests/wrap.js +0 -65
- package/build/tests/wrapReg.d.ts +0 -1
- package/build/tests/wrapReg.js +0 -68
- package/build/tests/wrapTest.d.ts +0 -1
- package/build/tests/wrapTest.js +0 -66
- package/build/tests/wrapTestg.d.ts +0 -1
- package/build/tests/wrapTestg.js +0 -66
- package/build/tests/writers/allowance.d.ts +0 -3
- package/build/tests/writers/allowance.js +0 -10
- package/build/tests/writers/approve.d.ts +0 -4
- package/build/tests/writers/approve.js +0 -11
- package/build/transaction/TransactionBuilder.d.ts +0 -60
- package/build/transaction/TransactionBuilder.js +0 -244
- package/build/transaction/browser/BrowserSigner.d.ts +0 -11
- package/build/transaction/browser/BrowserSigner.js +0 -10
- package/build/transaction/browser/extensions/Unisat.d.ts +0 -54
- package/build/transaction/browser/extensions/Unisat.js +0 -11
- package/build/transaction/builders/DeploymentTransactionV2.d.ts +0 -36
- package/build/transaction/builders/DeploymentTransactionV2.js +0 -192
- package/build/transaction/builders/GenericTransaction.d.ts +0 -11
- package/build/transaction/builders/GenericTransaction.js +0 -23
- package/build/transaction/builders/TapUnwarpTransaction.d.ts +0 -37
- package/build/transaction/builders/TapUnwarpTransaction.js +0 -201
- package/build/transaction/builders/UnwarpSegwitTransaction.d.ts +0 -34
- package/build/transaction/builders/UnwarpSegwitTransaction.js +0 -184
- package/build/transaction/builders/UnwarpTransaction.d.ts +0 -35
- package/build/transaction/builders/UnwarpTransaction.js +0 -199
- package/build/transaction/interfaces/ITransactions.d.ts +0 -32
- package/build/transaction/interfaces/ITransactions.js +0 -1
- package/build/utxo/IUTXO.d.ts +0 -0
- package/build/utxo/IUTXO.js +0 -1
- package/build/utxo/OPNetUtils.d.ts +0 -7
- package/build/utxo/OPNetUtils.js +0 -47
- package/build/utxo/UTXOManager.d.ts +0 -7
- package/build/utxo/UTXOManager.js +0 -47
- package/build/wbtc/BroadcastResponse.d.ts +0 -0
- package/build/wbtc/BroadcastResponse.js +0 -1
- package/examples/Deploy.md +0 -188
- package/examples/Unwrap.md +0 -165
- package/examples/Wrap.md +0 -194
- package/tsconfig.cjs.json +0 -13
- /package/build/generators/{features.d.ts → Features.d.ts} +0 -0
- /package/build/generators/{features.js → Features.js} +0 -0
|
@@ -1,539 +1,539 @@
|
|
|
1
|
-
import { Logger } from '@btc-vision/logger';
|
|
2
|
-
import { Network, Payment, payments, Psbt, Signer, Transaction } from 'bitcoinjs-lib';
|
|
3
|
-
import { TweakedSigner, TweakSettings } from '../../signer/TweakedSigner.js';
|
|
4
|
-
import { ECPairInterface } from 'ecpair';
|
|
5
|
-
import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371.js';
|
|
6
|
-
import { PsbtInput } from 'bip174/src/lib/interfaces.js';
|
|
7
|
-
import { UTXO } from '../../utxo/interfaces/IUTXO.js';
|
|
8
|
-
import { PsbtInputExtended, TapLeafScript } from '../interfaces/Tap.js';
|
|
9
|
-
import { AddressVerificator } from '../../keypair/AddressVerificator.js';
|
|
10
|
-
import { varuint } from 'bitcoinjs-lib/src/bufferutils.js';
|
|
11
|
-
import { ChainId } from '../../network/ChainId.js';
|
|
12
|
-
|
|
13
|
-
export interface ITweakedTransactionData {
|
|
14
|
-
readonly signer: Signer;
|
|
15
|
-
readonly network: Network;
|
|
16
|
-
readonly chainId?: ChainId;
|
|
17
|
-
readonly nonWitnessUtxo?: Buffer;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* The transaction sequence
|
|
22
|
-
*/
|
|
23
|
-
export enum TransactionSequence {
|
|
24
|
-
REPLACE_BY_FEE = 0xfffffffd,
|
|
25
|
-
FINAL = 0xffffffff,
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @description PSBT Transaction processor.
|
|
30
|
-
* */
|
|
31
|
-
export abstract class TweakedTransaction extends Logger {
|
|
32
|
-
public readonly logColor: string = '#00ffe1';
|
|
33
|
-
public finalized: boolean = false;
|
|
34
|
-
/**
|
|
35
|
-
* @description Was the transaction signed?
|
|
36
|
-
*/
|
|
37
|
-
protected signer: Signer;
|
|
38
|
-
/**
|
|
39
|
-
* @description Tweaked signer
|
|
40
|
-
*/
|
|
41
|
-
protected tweakedSigner?: Signer;
|
|
42
|
-
/**
|
|
43
|
-
* @description The network of the transaction
|
|
44
|
-
*/
|
|
45
|
-
protected network: Network;
|
|
46
|
-
/**
|
|
47
|
-
* @description Was the transaction signed?
|
|
48
|
-
*/
|
|
49
|
-
protected signed: boolean = false;
|
|
50
|
-
/**
|
|
51
|
-
* @description The transaction
|
|
52
|
-
* @protected
|
|
53
|
-
*/
|
|
54
|
-
protected abstract readonly transaction: Psbt;
|
|
55
|
-
/**
|
|
56
|
-
* @description The sighash types of the transaction
|
|
57
|
-
* @protected
|
|
58
|
-
*/
|
|
59
|
-
protected sighashTypes: number[] | undefined;
|
|
60
|
-
/**
|
|
61
|
-
* @description The script data of the transaction
|
|
62
|
-
*/
|
|
63
|
-
protected scriptData: Payment | null = null;
|
|
64
|
-
/**
|
|
65
|
-
* @description The tap data of the transaction
|
|
66
|
-
*/
|
|
67
|
-
protected tapData: Payment | null = null;
|
|
68
|
-
/**
|
|
69
|
-
* @description The inputs of the transaction
|
|
70
|
-
*/
|
|
71
|
-
protected readonly inputs: PsbtInputExtended[] = [];
|
|
72
|
-
/**
|
|
73
|
-
* @description The sequence of the transaction
|
|
74
|
-
* @protected
|
|
75
|
-
*/
|
|
76
|
-
protected sequence: number = TransactionSequence.REPLACE_BY_FEE;
|
|
77
|
-
/**
|
|
78
|
-
* The tap leaf script
|
|
79
|
-
* @protected
|
|
80
|
-
*/
|
|
81
|
-
protected tapLeafScript: TapLeafScript | null = null;
|
|
82
|
-
/**
|
|
83
|
-
* Add a non-witness utxo to the transaction
|
|
84
|
-
* @protected
|
|
85
|
-
*/
|
|
86
|
-
protected nonWitnessUtxo?: Buffer;
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Is the transaction being generated inside a browser?
|
|
90
|
-
* @protected
|
|
91
|
-
*/
|
|
92
|
-
protected readonly isBrowser: boolean = false;
|
|
93
|
-
|
|
94
|
-
protected regenerated: boolean = false;
|
|
95
|
-
protected ignoreSignatureErrors: boolean = false;
|
|
96
|
-
|
|
97
|
-
protected constructor(data: ITweakedTransactionData) {
|
|
98
|
-
super();
|
|
99
|
-
|
|
100
|
-
this.signer = data.signer;
|
|
101
|
-
this.network = data.network;
|
|
102
|
-
|
|
103
|
-
this.nonWitnessUtxo = data.nonWitnessUtxo;
|
|
104
|
-
|
|
105
|
-
this.isBrowser = typeof window !== 'undefined';
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Read witnesses
|
|
110
|
-
* @protected
|
|
111
|
-
*/
|
|
112
|
-
public static readScriptWitnessToWitnessStack(buffer: Buffer): Buffer[] {
|
|
113
|
-
let offset = 0;
|
|
114
|
-
|
|
115
|
-
function readSlice(n: number): Buffer {
|
|
116
|
-
const slice = buffer.subarray(offset, offset + n);
|
|
117
|
-
offset += n;
|
|
118
|
-
return slice;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
function readVarInt(): number {
|
|
122
|
-
const varint = varuint.decode(buffer, offset);
|
|
123
|
-
offset += varuint.decode.bytes;
|
|
124
|
-
return varint;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function readVarSlice(): Buffer {
|
|
128
|
-
const len = readVarInt();
|
|
129
|
-
return readSlice(len);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
function readVector(): Buffer[] {
|
|
133
|
-
const count = readVarInt();
|
|
134
|
-
const vector = [];
|
|
135
|
-
for (let i = 0; i < count; i++) {
|
|
136
|
-
vector.push(readVarSlice());
|
|
137
|
-
}
|
|
138
|
-
return vector;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return readVector();
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Pre-estimate the transaction fees for a Taproot transaction
|
|
146
|
-
* @param {bigint} feeRate - The fee rate in satoshis per virtual byte
|
|
147
|
-
* @param {bigint} numInputs - The number of inputs
|
|
148
|
-
* @param {bigint} numOutputs - The number of outputs
|
|
149
|
-
* @param {bigint} numWitnessElements - The number of witness elements (e.g., number of control blocks and witnesses)
|
|
150
|
-
* @param {bigint} witnessElementSize - The average size of each witness element in bytes
|
|
151
|
-
* @param {bigint} emptyWitness - The amount of empty witnesses
|
|
152
|
-
* @param {bigint} [taprootControlWitnessSize=139n] - The size of the control block witness in bytes
|
|
153
|
-
* @param {bigint} [taprootScriptSize=32n] - The size of the taproot script in bytes
|
|
154
|
-
* @returns {bigint} - The estimated transaction fees
|
|
155
|
-
*/
|
|
156
|
-
public static preEstimateTaprootTransactionFees(
|
|
157
|
-
feeRate: bigint, // satoshis per virtual byte
|
|
158
|
-
numInputs: bigint,
|
|
159
|
-
numOutputs: bigint,
|
|
160
|
-
numWitnessElements: bigint,
|
|
161
|
-
witnessElementSize: bigint,
|
|
162
|
-
emptyWitness: bigint,
|
|
163
|
-
taprootControlWitnessSize: bigint = 32n,
|
|
164
|
-
taprootScriptSize: bigint = 139n,
|
|
165
|
-
): bigint {
|
|
166
|
-
const txHeaderSize = 10n;
|
|
167
|
-
const inputBaseSize = 41n;
|
|
168
|
-
const outputSize = 68n;
|
|
169
|
-
const taprootWitnessBaseSize = 1n; // Base witness size per input (without signatures and control blocks)
|
|
170
|
-
|
|
171
|
-
// Base transaction size (excluding witness data)
|
|
172
|
-
const baseTxSize = txHeaderSize + inputBaseSize * numInputs + outputSize * numOutputs;
|
|
173
|
-
|
|
174
|
-
// Witness data size for Taproot
|
|
175
|
-
const witnessSize =
|
|
176
|
-
numInputs * taprootWitnessBaseSize +
|
|
177
|
-
numWitnessElements * witnessElementSize +
|
|
178
|
-
taprootControlWitnessSize * numInputs +
|
|
179
|
-
taprootScriptSize * numInputs +
|
|
180
|
-
emptyWitness;
|
|
181
|
-
|
|
182
|
-
// Total weight and virtual size
|
|
183
|
-
const weight = baseTxSize * 3n + (baseTxSize + witnessSize);
|
|
184
|
-
const vSize = weight / 4n;
|
|
185
|
-
|
|
186
|
-
return vSize * feeRate;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
protected static signInput(
|
|
190
|
-
transaction: Psbt,
|
|
191
|
-
input: PsbtInput,
|
|
192
|
-
i: number,
|
|
193
|
-
signer: Signer,
|
|
194
|
-
sighashTypes: number[],
|
|
195
|
-
): void {
|
|
196
|
-
if (sighashTypes && sighashTypes[0]) input.sighashType = sighashTypes[0];
|
|
197
|
-
|
|
198
|
-
transaction.signInput(i, signer, sighashTypes.length ? sighashTypes : undefined);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Calculate the sign hash number
|
|
203
|
-
* @description Calculates the sign hash
|
|
204
|
-
* @protected
|
|
205
|
-
* @returns {number}
|
|
206
|
-
*/
|
|
207
|
-
protected static calculateSignHash(sighashTypes: number[]): number {
|
|
208
|
-
if (!sighashTypes) {
|
|
209
|
-
throw new Error('Sighash types are required');
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
let signHash: number = 0;
|
|
213
|
-
for (let sighashType of sighashTypes) {
|
|
214
|
-
signHash |= sighashType;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
return signHash || 0;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
public ignoreSignatureError(): void {
|
|
221
|
-
this.ignoreSignatureErrors = true;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* @description Returns the script address
|
|
226
|
-
* @returns {string}
|
|
227
|
-
*/
|
|
228
|
-
public getScriptAddress(): string {
|
|
229
|
-
if (!this.scriptData || !this.scriptData.address) {
|
|
230
|
-
throw new Error('Tap data is required');
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
return this.scriptData.address;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* @description Returns the transaction
|
|
238
|
-
* @returns {Transaction}
|
|
239
|
-
*/
|
|
240
|
-
public getTransaction(): Transaction {
|
|
241
|
-
return this.transaction.extractTransaction(false);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* @description Returns the tap address
|
|
246
|
-
* @returns {string}
|
|
247
|
-
* @throws {Error} - If tap data is not set
|
|
248
|
-
*/
|
|
249
|
-
public getTapAddress(): string {
|
|
250
|
-
if (!this.tapData || !this.tapData.address) {
|
|
251
|
-
throw new Error('Tap data is required');
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
return this.tapData.address;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* @description Disables replace by fee on the transaction
|
|
259
|
-
*/
|
|
260
|
-
public disableRBF(): void {
|
|
261
|
-
if (this.signed) throw new Error('Transaction is already signed');
|
|
262
|
-
|
|
263
|
-
this.sequence = TransactionSequence.FINAL;
|
|
264
|
-
|
|
265
|
-
for (let input of this.inputs) {
|
|
266
|
-
input.sequence = TransactionSequence.FINAL;
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* Get the tweaked hash
|
|
272
|
-
* @private
|
|
273
|
-
*
|
|
274
|
-
* @returns {Buffer | undefined} The tweaked hash
|
|
275
|
-
*/
|
|
276
|
-
public getTweakerHash(): Buffer | undefined {
|
|
277
|
-
return this.tapData?.hash;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
/**
|
|
281
|
-
* Pre-estimate the transaction fees
|
|
282
|
-
* @param {bigint} feeRate - The fee rate
|
|
283
|
-
* @param {bigint} numInputs - The number of inputs
|
|
284
|
-
* @param {bigint} numOutputs - The number of outputs
|
|
285
|
-
* @param {bigint} numSignatures - The number of signatures
|
|
286
|
-
* @param {bigint} numPubkeys - The number of public keys
|
|
287
|
-
* @returns {bigint} - The estimated transaction fees
|
|
288
|
-
*/
|
|
289
|
-
public preEstimateTransactionFees(
|
|
290
|
-
feeRate: bigint, // satoshis per byte
|
|
291
|
-
numInputs: bigint,
|
|
292
|
-
numOutputs: bigint,
|
|
293
|
-
numSignatures: bigint,
|
|
294
|
-
numPubkeys: bigint,
|
|
295
|
-
): bigint {
|
|
296
|
-
const txHeaderSize = 10n;
|
|
297
|
-
const inputBaseSize = 41n;
|
|
298
|
-
const outputSize = 68n;
|
|
299
|
-
const signatureSize = 144n;
|
|
300
|
-
const pubkeySize = 34n;
|
|
301
|
-
|
|
302
|
-
// Base transaction size (excluding witness data)
|
|
303
|
-
const baseTxSize = txHeaderSize + inputBaseSize * numInputs + outputSize * numOutputs;
|
|
304
|
-
|
|
305
|
-
// Witness data size
|
|
306
|
-
const redeemScriptSize = 1n + numPubkeys * (1n + pubkeySize) + 1n + numSignatures;
|
|
307
|
-
const witnessSize =
|
|
308
|
-
numSignatures * signatureSize + numPubkeys * pubkeySize + redeemScriptSize;
|
|
309
|
-
|
|
310
|
-
// Total weight and virtual size
|
|
311
|
-
const weight = baseTxSize * 3n + (baseTxSize + witnessSize);
|
|
312
|
-
const vSize = weight / 4n;
|
|
313
|
-
|
|
314
|
-
return vSize * feeRate;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
protected generateTapData(): Payment {
|
|
318
|
-
return {
|
|
319
|
-
internalPubkey: this.internalPubKeyToXOnly(),
|
|
320
|
-
network: this.network,
|
|
321
|
-
};
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* Generates the script address.
|
|
326
|
-
* @protected
|
|
327
|
-
* @returns {Payment}
|
|
328
|
-
*/
|
|
329
|
-
protected generateScriptAddress(): Payment {
|
|
330
|
-
return {
|
|
331
|
-
internalPubkey: this.internalPubKeyToXOnly(),
|
|
332
|
-
network: this.network,
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* Returns the signer key.
|
|
338
|
-
* @protected
|
|
339
|
-
* @returns {Signer}
|
|
340
|
-
*/
|
|
341
|
-
protected getSignerKey(): Signer {
|
|
342
|
-
return this.signer;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* Signs an input of the transaction.
|
|
347
|
-
* @param {Psbt} transaction - The transaction to sign
|
|
348
|
-
* @param {PsbtInput} input - The input to sign
|
|
349
|
-
* @param {number} i - The index of the input
|
|
350
|
-
* @param {Signer} [signer] - The signer to use
|
|
351
|
-
* @protected
|
|
352
|
-
*/
|
|
353
|
-
protected async signInput(
|
|
354
|
-
transaction: Psbt,
|
|
355
|
-
input: PsbtInput,
|
|
356
|
-
i: number,
|
|
357
|
-
signer?: Signer,
|
|
358
|
-
): Promise<void> {
|
|
359
|
-
const signHash =
|
|
360
|
-
this.sighashTypes && this.sighashTypes.length
|
|
361
|
-
? [TweakedTransaction.calculateSignHash(this.sighashTypes)]
|
|
362
|
-
: undefined;
|
|
363
|
-
|
|
364
|
-
signer = signer || this.getSignerKey();
|
|
365
|
-
|
|
366
|
-
let testedTap: boolean = false;
|
|
367
|
-
if (input.tapInternalKey) {
|
|
368
|
-
if (!this.tweakedSigner) this.tweakSigner();
|
|
369
|
-
|
|
370
|
-
let tweakedSigner: Signer | undefined;
|
|
371
|
-
if (signer !== this.signer) {
|
|
372
|
-
tweakedSigner = this.getTweakedSigner(true, signer);
|
|
373
|
-
} else {
|
|
374
|
-
tweakedSigner = this.tweakedSigner;
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
if (tweakedSigner) {
|
|
378
|
-
testedTap = true;
|
|
379
|
-
|
|
380
|
-
try {
|
|
381
|
-
if ('signTaprootInput' in signer) {
|
|
382
|
-
// @ts-ignore
|
|
383
|
-
return await signer.signTaprootInput(transaction, i, signHash);
|
|
384
|
-
} else {
|
|
385
|
-
transaction.signTaprootInput(i, tweakedSigner, undefined, signHash);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
return;
|
|
389
|
-
} catch (e) {}
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
try {
|
|
394
|
-
if ('signInput' in signer) {
|
|
395
|
-
// @ts-ignore
|
|
396
|
-
return await signer.signInput(transaction, i, signHash);
|
|
397
|
-
} else {
|
|
398
|
-
transaction.signInput(i, signer, signHash);
|
|
399
|
-
}
|
|
400
|
-
} catch (e) {
|
|
401
|
-
if (!testedTap) {
|
|
402
|
-
// and we try again taproot...
|
|
403
|
-
|
|
404
|
-
if ('signTaprootInput' in signer) {
|
|
405
|
-
// @ts-ignore
|
|
406
|
-
return await signer.signTaprootInput(transaction, i, signHash);
|
|
407
|
-
} else if (this.tweakedSigner) {
|
|
408
|
-
transaction.signTaprootInput(i, this.tweakedSigner, undefined, signHash);
|
|
409
|
-
} else {
|
|
410
|
-
throw e;
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
/**
|
|
417
|
-
* Signs all the inputs of the transaction.
|
|
418
|
-
* @param {Psbt} transaction - The transaction to sign
|
|
419
|
-
* @protected
|
|
420
|
-
* @returns {Promise<void>}
|
|
421
|
-
*/
|
|
422
|
-
protected async signInputs(transaction: Psbt): Promise<void> {
|
|
423
|
-
for (let i = 0; i < transaction.data.inputs.length; i++) {
|
|
424
|
-
let input: PsbtInput = transaction.data.inputs[i];
|
|
425
|
-
|
|
426
|
-
try {
|
|
427
|
-
await this.signInput(transaction, input, i);
|
|
428
|
-
} catch (e) {
|
|
429
|
-
this.log(`Failed to sign input ${i}: ${(e as Error).stack}`);
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
try {
|
|
434
|
-
transaction.finalizeAllInputs();
|
|
435
|
-
|
|
436
|
-
this.finalized = true;
|
|
437
|
-
} catch (e) {
|
|
438
|
-
this.finalized = false;
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
/**
|
|
443
|
-
* Converts the public key to x-only.
|
|
444
|
-
* @protected
|
|
445
|
-
* @returns {Buffer}
|
|
446
|
-
*/
|
|
447
|
-
protected internalPubKeyToXOnly(): Buffer {
|
|
448
|
-
return toXOnly(this.signer.publicKey);
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
/**
|
|
452
|
-
* Internal init.
|
|
453
|
-
* @protected
|
|
454
|
-
*/
|
|
455
|
-
protected internalInit(): void {
|
|
456
|
-
this.scriptData = payments.p2tr(this.generateScriptAddress());
|
|
457
|
-
this.tapData = payments.p2tr(this.generateTapData());
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
/**
|
|
461
|
-
* Tweak the signer for the interaction
|
|
462
|
-
* @protected
|
|
463
|
-
*/
|
|
464
|
-
protected tweakSigner(): void {
|
|
465
|
-
if (this.tweakedSigner) return;
|
|
466
|
-
|
|
467
|
-
// tweaked p2tr signer.
|
|
468
|
-
this.tweakedSigner = this.getTweakedSigner(true);
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
/**
|
|
472
|
-
* Get the tweaked signer
|
|
473
|
-
* @private
|
|
474
|
-
* @returns {Signer} The tweaked signer
|
|
475
|
-
*/
|
|
476
|
-
protected getTweakedSigner(
|
|
477
|
-
useTweakedHash: boolean = false,
|
|
478
|
-
signer: Signer = this.signer,
|
|
479
|
-
): Signer | undefined {
|
|
480
|
-
const settings: TweakSettings = {
|
|
481
|
-
network: this.network,
|
|
482
|
-
};
|
|
483
|
-
|
|
484
|
-
if (useTweakedHash) {
|
|
485
|
-
settings.tweakHash = this.getTweakerHash();
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
if (!('privateKey' in signer)) {
|
|
489
|
-
return;
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
return TweakedSigner.tweakSigner(signer as unknown as ECPairInterface, settings) as Signer;
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
/**
|
|
496
|
-
* Generate the PSBT input extended
|
|
497
|
-
* @param {UTXO} utxo The UTXO
|
|
498
|
-
* @param {number} i The index of the input
|
|
499
|
-
* @protected
|
|
500
|
-
* @returns {PsbtInputExtended} The PSBT input extended
|
|
501
|
-
*/
|
|
502
|
-
protected generatePsbtInputExtended(utxo: UTXO, i: number): PsbtInputExtended {
|
|
503
|
-
const input: PsbtInputExtended = {
|
|
504
|
-
hash: utxo.transactionId,
|
|
505
|
-
index: utxo.outputIndex,
|
|
506
|
-
witnessUtxo: {
|
|
507
|
-
value: Number(utxo.value),
|
|
508
|
-
script: Buffer.from(utxo.scriptPubKey.hex, 'hex'),
|
|
509
|
-
},
|
|
510
|
-
sequence: this.sequence,
|
|
511
|
-
};
|
|
512
|
-
|
|
513
|
-
if (this.sighashTypes) {
|
|
514
|
-
const inputSign = TweakedTransaction.calculateSignHash(this.sighashTypes);
|
|
515
|
-
if (inputSign) input.sighashType = inputSign;
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
if (this.tapLeafScript) {
|
|
519
|
-
input.tapLeafScript = [this.tapLeafScript];
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
if (i === 0 && this.nonWitnessUtxo) {
|
|
523
|
-
input.nonWitnessUtxo = this.nonWitnessUtxo;
|
|
524
|
-
this.log(`Using non-witness utxo for input ${i}`);
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
// automatically detect p2tr inputs.
|
|
528
|
-
if (
|
|
529
|
-
utxo.scriptPubKey.address &&
|
|
530
|
-
AddressVerificator.isValidP2TRAddress(utxo.scriptPubKey.address, this.network)
|
|
531
|
-
) {
|
|
532
|
-
this.tweakSigner();
|
|
533
|
-
|
|
534
|
-
input.tapInternalKey = this.internalPubKeyToXOnly();
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
return input;
|
|
538
|
-
}
|
|
539
|
-
}
|
|
1
|
+
import { Logger } from '@btc-vision/logger';
|
|
2
|
+
import { Network, Payment, payments, Psbt, Signer, Transaction } from 'bitcoinjs-lib';
|
|
3
|
+
import { TweakedSigner, TweakSettings } from '../../signer/TweakedSigner.js';
|
|
4
|
+
import { ECPairInterface } from 'ecpair';
|
|
5
|
+
import { toXOnly } from 'bitcoinjs-lib/src/psbt/bip371.js';
|
|
6
|
+
import { PsbtInput } from 'bip174/src/lib/interfaces.js';
|
|
7
|
+
import { UTXO } from '../../utxo/interfaces/IUTXO.js';
|
|
8
|
+
import { PsbtInputExtended, TapLeafScript } from '../interfaces/Tap.js';
|
|
9
|
+
import { AddressVerificator } from '../../keypair/AddressVerificator.js';
|
|
10
|
+
import { varuint } from 'bitcoinjs-lib/src/bufferutils.js';
|
|
11
|
+
import { ChainId } from '../../network/ChainId.js';
|
|
12
|
+
|
|
13
|
+
export interface ITweakedTransactionData {
|
|
14
|
+
readonly signer: Signer;
|
|
15
|
+
readonly network: Network;
|
|
16
|
+
readonly chainId?: ChainId;
|
|
17
|
+
readonly nonWitnessUtxo?: Buffer;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The transaction sequence
|
|
22
|
+
*/
|
|
23
|
+
export enum TransactionSequence {
|
|
24
|
+
REPLACE_BY_FEE = 0xfffffffd,
|
|
25
|
+
FINAL = 0xffffffff,
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @description PSBT Transaction processor.
|
|
30
|
+
* */
|
|
31
|
+
export abstract class TweakedTransaction extends Logger {
|
|
32
|
+
public readonly logColor: string = '#00ffe1';
|
|
33
|
+
public finalized: boolean = false;
|
|
34
|
+
/**
|
|
35
|
+
* @description Was the transaction signed?
|
|
36
|
+
*/
|
|
37
|
+
protected signer: Signer;
|
|
38
|
+
/**
|
|
39
|
+
* @description Tweaked signer
|
|
40
|
+
*/
|
|
41
|
+
protected tweakedSigner?: Signer;
|
|
42
|
+
/**
|
|
43
|
+
* @description The network of the transaction
|
|
44
|
+
*/
|
|
45
|
+
protected network: Network;
|
|
46
|
+
/**
|
|
47
|
+
* @description Was the transaction signed?
|
|
48
|
+
*/
|
|
49
|
+
protected signed: boolean = false;
|
|
50
|
+
/**
|
|
51
|
+
* @description The transaction
|
|
52
|
+
* @protected
|
|
53
|
+
*/
|
|
54
|
+
protected abstract readonly transaction: Psbt;
|
|
55
|
+
/**
|
|
56
|
+
* @description The sighash types of the transaction
|
|
57
|
+
* @protected
|
|
58
|
+
*/
|
|
59
|
+
protected sighashTypes: number[] | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* @description The script data of the transaction
|
|
62
|
+
*/
|
|
63
|
+
protected scriptData: Payment | null = null;
|
|
64
|
+
/**
|
|
65
|
+
* @description The tap data of the transaction
|
|
66
|
+
*/
|
|
67
|
+
protected tapData: Payment | null = null;
|
|
68
|
+
/**
|
|
69
|
+
* @description The inputs of the transaction
|
|
70
|
+
*/
|
|
71
|
+
protected readonly inputs: PsbtInputExtended[] = [];
|
|
72
|
+
/**
|
|
73
|
+
* @description The sequence of the transaction
|
|
74
|
+
* @protected
|
|
75
|
+
*/
|
|
76
|
+
protected sequence: number = TransactionSequence.REPLACE_BY_FEE;
|
|
77
|
+
/**
|
|
78
|
+
* The tap leaf script
|
|
79
|
+
* @protected
|
|
80
|
+
*/
|
|
81
|
+
protected tapLeafScript: TapLeafScript | null = null;
|
|
82
|
+
/**
|
|
83
|
+
* Add a non-witness utxo to the transaction
|
|
84
|
+
* @protected
|
|
85
|
+
*/
|
|
86
|
+
protected nonWitnessUtxo?: Buffer;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Is the transaction being generated inside a browser?
|
|
90
|
+
* @protected
|
|
91
|
+
*/
|
|
92
|
+
protected readonly isBrowser: boolean = false;
|
|
93
|
+
|
|
94
|
+
protected regenerated: boolean = false;
|
|
95
|
+
protected ignoreSignatureErrors: boolean = false;
|
|
96
|
+
|
|
97
|
+
protected constructor(data: ITweakedTransactionData) {
|
|
98
|
+
super();
|
|
99
|
+
|
|
100
|
+
this.signer = data.signer;
|
|
101
|
+
this.network = data.network;
|
|
102
|
+
|
|
103
|
+
this.nonWitnessUtxo = data.nonWitnessUtxo;
|
|
104
|
+
|
|
105
|
+
this.isBrowser = typeof window !== 'undefined';
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Read witnesses
|
|
110
|
+
* @protected
|
|
111
|
+
*/
|
|
112
|
+
public static readScriptWitnessToWitnessStack(buffer: Buffer): Buffer[] {
|
|
113
|
+
let offset = 0;
|
|
114
|
+
|
|
115
|
+
function readSlice(n: number): Buffer {
|
|
116
|
+
const slice = buffer.subarray(offset, offset + n);
|
|
117
|
+
offset += n;
|
|
118
|
+
return slice;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function readVarInt(): number {
|
|
122
|
+
const varint = varuint.decode(buffer, offset);
|
|
123
|
+
offset += varuint.decode.bytes;
|
|
124
|
+
return varint;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function readVarSlice(): Buffer {
|
|
128
|
+
const len = readVarInt();
|
|
129
|
+
return readSlice(len);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function readVector(): Buffer[] {
|
|
133
|
+
const count = readVarInt();
|
|
134
|
+
const vector = [];
|
|
135
|
+
for (let i = 0; i < count; i++) {
|
|
136
|
+
vector.push(readVarSlice());
|
|
137
|
+
}
|
|
138
|
+
return vector;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return readVector();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Pre-estimate the transaction fees for a Taproot transaction
|
|
146
|
+
* @param {bigint} feeRate - The fee rate in satoshis per virtual byte
|
|
147
|
+
* @param {bigint} numInputs - The number of inputs
|
|
148
|
+
* @param {bigint} numOutputs - The number of outputs
|
|
149
|
+
* @param {bigint} numWitnessElements - The number of witness elements (e.g., number of control blocks and witnesses)
|
|
150
|
+
* @param {bigint} witnessElementSize - The average size of each witness element in bytes
|
|
151
|
+
* @param {bigint} emptyWitness - The amount of empty witnesses
|
|
152
|
+
* @param {bigint} [taprootControlWitnessSize=139n] - The size of the control block witness in bytes
|
|
153
|
+
* @param {bigint} [taprootScriptSize=32n] - The size of the taproot script in bytes
|
|
154
|
+
* @returns {bigint} - The estimated transaction fees
|
|
155
|
+
*/
|
|
156
|
+
public static preEstimateTaprootTransactionFees(
|
|
157
|
+
feeRate: bigint, // satoshis per virtual byte
|
|
158
|
+
numInputs: bigint,
|
|
159
|
+
numOutputs: bigint,
|
|
160
|
+
numWitnessElements: bigint,
|
|
161
|
+
witnessElementSize: bigint,
|
|
162
|
+
emptyWitness: bigint,
|
|
163
|
+
taprootControlWitnessSize: bigint = 32n,
|
|
164
|
+
taprootScriptSize: bigint = 139n,
|
|
165
|
+
): bigint {
|
|
166
|
+
const txHeaderSize = 10n;
|
|
167
|
+
const inputBaseSize = 41n;
|
|
168
|
+
const outputSize = 68n;
|
|
169
|
+
const taprootWitnessBaseSize = 1n; // Base witness size per input (without signatures and control blocks)
|
|
170
|
+
|
|
171
|
+
// Base transaction size (excluding witness data)
|
|
172
|
+
const baseTxSize = txHeaderSize + inputBaseSize * numInputs + outputSize * numOutputs;
|
|
173
|
+
|
|
174
|
+
// Witness data size for Taproot
|
|
175
|
+
const witnessSize =
|
|
176
|
+
numInputs * taprootWitnessBaseSize +
|
|
177
|
+
numWitnessElements * witnessElementSize +
|
|
178
|
+
taprootControlWitnessSize * numInputs +
|
|
179
|
+
taprootScriptSize * numInputs +
|
|
180
|
+
emptyWitness;
|
|
181
|
+
|
|
182
|
+
// Total weight and virtual size
|
|
183
|
+
const weight = baseTxSize * 3n + (baseTxSize + witnessSize);
|
|
184
|
+
const vSize = weight / 4n;
|
|
185
|
+
|
|
186
|
+
return vSize * feeRate;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
protected static signInput(
|
|
190
|
+
transaction: Psbt,
|
|
191
|
+
input: PsbtInput,
|
|
192
|
+
i: number,
|
|
193
|
+
signer: Signer,
|
|
194
|
+
sighashTypes: number[],
|
|
195
|
+
): void {
|
|
196
|
+
if (sighashTypes && sighashTypes[0]) input.sighashType = sighashTypes[0];
|
|
197
|
+
|
|
198
|
+
transaction.signInput(i, signer, sighashTypes.length ? sighashTypes : undefined);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Calculate the sign hash number
|
|
203
|
+
* @description Calculates the sign hash
|
|
204
|
+
* @protected
|
|
205
|
+
* @returns {number}
|
|
206
|
+
*/
|
|
207
|
+
protected static calculateSignHash(sighashTypes: number[]): number {
|
|
208
|
+
if (!sighashTypes) {
|
|
209
|
+
throw new Error('Sighash types are required');
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
let signHash: number = 0;
|
|
213
|
+
for (let sighashType of sighashTypes) {
|
|
214
|
+
signHash |= sighashType;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return signHash || 0;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
public ignoreSignatureError(): void {
|
|
221
|
+
this.ignoreSignatureErrors = true;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* @description Returns the script address
|
|
226
|
+
* @returns {string}
|
|
227
|
+
*/
|
|
228
|
+
public getScriptAddress(): string {
|
|
229
|
+
if (!this.scriptData || !this.scriptData.address) {
|
|
230
|
+
throw new Error('Tap data is required');
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return this.scriptData.address;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* @description Returns the transaction
|
|
238
|
+
* @returns {Transaction}
|
|
239
|
+
*/
|
|
240
|
+
public getTransaction(): Transaction {
|
|
241
|
+
return this.transaction.extractTransaction(false);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* @description Returns the tap address
|
|
246
|
+
* @returns {string}
|
|
247
|
+
* @throws {Error} - If tap data is not set
|
|
248
|
+
*/
|
|
249
|
+
public getTapAddress(): string {
|
|
250
|
+
if (!this.tapData || !this.tapData.address) {
|
|
251
|
+
throw new Error('Tap data is required');
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return this.tapData.address;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* @description Disables replace by fee on the transaction
|
|
259
|
+
*/
|
|
260
|
+
public disableRBF(): void {
|
|
261
|
+
if (this.signed) throw new Error('Transaction is already signed');
|
|
262
|
+
|
|
263
|
+
this.sequence = TransactionSequence.FINAL;
|
|
264
|
+
|
|
265
|
+
for (let input of this.inputs) {
|
|
266
|
+
input.sequence = TransactionSequence.FINAL;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Get the tweaked hash
|
|
272
|
+
* @private
|
|
273
|
+
*
|
|
274
|
+
* @returns {Buffer | undefined} The tweaked hash
|
|
275
|
+
*/
|
|
276
|
+
public getTweakerHash(): Buffer | undefined {
|
|
277
|
+
return this.tapData?.hash;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Pre-estimate the transaction fees
|
|
282
|
+
* @param {bigint} feeRate - The fee rate
|
|
283
|
+
* @param {bigint} numInputs - The number of inputs
|
|
284
|
+
* @param {bigint} numOutputs - The number of outputs
|
|
285
|
+
* @param {bigint} numSignatures - The number of signatures
|
|
286
|
+
* @param {bigint} numPubkeys - The number of public keys
|
|
287
|
+
* @returns {bigint} - The estimated transaction fees
|
|
288
|
+
*/
|
|
289
|
+
public preEstimateTransactionFees(
|
|
290
|
+
feeRate: bigint, // satoshis per byte
|
|
291
|
+
numInputs: bigint,
|
|
292
|
+
numOutputs: bigint,
|
|
293
|
+
numSignatures: bigint,
|
|
294
|
+
numPubkeys: bigint,
|
|
295
|
+
): bigint {
|
|
296
|
+
const txHeaderSize = 10n;
|
|
297
|
+
const inputBaseSize = 41n;
|
|
298
|
+
const outputSize = 68n;
|
|
299
|
+
const signatureSize = 144n;
|
|
300
|
+
const pubkeySize = 34n;
|
|
301
|
+
|
|
302
|
+
// Base transaction size (excluding witness data)
|
|
303
|
+
const baseTxSize = txHeaderSize + inputBaseSize * numInputs + outputSize * numOutputs;
|
|
304
|
+
|
|
305
|
+
// Witness data size
|
|
306
|
+
const redeemScriptSize = 1n + numPubkeys * (1n + pubkeySize) + 1n + numSignatures;
|
|
307
|
+
const witnessSize =
|
|
308
|
+
numSignatures * signatureSize + numPubkeys * pubkeySize + redeemScriptSize;
|
|
309
|
+
|
|
310
|
+
// Total weight and virtual size
|
|
311
|
+
const weight = baseTxSize * 3n + (baseTxSize + witnessSize);
|
|
312
|
+
const vSize = weight / 4n;
|
|
313
|
+
|
|
314
|
+
return vSize * feeRate;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
protected generateTapData(): Payment {
|
|
318
|
+
return {
|
|
319
|
+
internalPubkey: this.internalPubKeyToXOnly(),
|
|
320
|
+
network: this.network,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Generates the script address.
|
|
326
|
+
* @protected
|
|
327
|
+
* @returns {Payment}
|
|
328
|
+
*/
|
|
329
|
+
protected generateScriptAddress(): Payment {
|
|
330
|
+
return {
|
|
331
|
+
internalPubkey: this.internalPubKeyToXOnly(),
|
|
332
|
+
network: this.network,
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Returns the signer key.
|
|
338
|
+
* @protected
|
|
339
|
+
* @returns {Signer}
|
|
340
|
+
*/
|
|
341
|
+
protected getSignerKey(): Signer {
|
|
342
|
+
return this.signer;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Signs an input of the transaction.
|
|
347
|
+
* @param {Psbt} transaction - The transaction to sign
|
|
348
|
+
* @param {PsbtInput} input - The input to sign
|
|
349
|
+
* @param {number} i - The index of the input
|
|
350
|
+
* @param {Signer} [signer] - The signer to use
|
|
351
|
+
* @protected
|
|
352
|
+
*/
|
|
353
|
+
protected async signInput(
|
|
354
|
+
transaction: Psbt,
|
|
355
|
+
input: PsbtInput,
|
|
356
|
+
i: number,
|
|
357
|
+
signer?: Signer,
|
|
358
|
+
): Promise<void> {
|
|
359
|
+
const signHash =
|
|
360
|
+
this.sighashTypes && this.sighashTypes.length
|
|
361
|
+
? [TweakedTransaction.calculateSignHash(this.sighashTypes)]
|
|
362
|
+
: undefined;
|
|
363
|
+
|
|
364
|
+
signer = signer || this.getSignerKey();
|
|
365
|
+
|
|
366
|
+
let testedTap: boolean = false;
|
|
367
|
+
if (input.tapInternalKey) {
|
|
368
|
+
if (!this.tweakedSigner) this.tweakSigner();
|
|
369
|
+
|
|
370
|
+
let tweakedSigner: Signer | undefined;
|
|
371
|
+
if (signer !== this.signer) {
|
|
372
|
+
tweakedSigner = this.getTweakedSigner(true, signer);
|
|
373
|
+
} else {
|
|
374
|
+
tweakedSigner = this.tweakedSigner;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (tweakedSigner) {
|
|
378
|
+
testedTap = true;
|
|
379
|
+
|
|
380
|
+
try {
|
|
381
|
+
if ('signTaprootInput' in signer) {
|
|
382
|
+
// @ts-ignore
|
|
383
|
+
return await signer.signTaprootInput(transaction, i, signHash);
|
|
384
|
+
} else {
|
|
385
|
+
transaction.signTaprootInput(i, tweakedSigner, undefined, signHash);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
return;
|
|
389
|
+
} catch (e) {}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
try {
|
|
394
|
+
if ('signInput' in signer) {
|
|
395
|
+
// @ts-ignore
|
|
396
|
+
return await signer.signInput(transaction, i, signHash);
|
|
397
|
+
} else {
|
|
398
|
+
transaction.signInput(i, signer, signHash);
|
|
399
|
+
}
|
|
400
|
+
} catch (e) {
|
|
401
|
+
if (!testedTap) {
|
|
402
|
+
// and we try again taproot...
|
|
403
|
+
|
|
404
|
+
if ('signTaprootInput' in signer) {
|
|
405
|
+
// @ts-ignore
|
|
406
|
+
return await signer.signTaprootInput(transaction, i, signHash);
|
|
407
|
+
} else if (this.tweakedSigner) {
|
|
408
|
+
transaction.signTaprootInput(i, this.tweakedSigner, undefined, signHash);
|
|
409
|
+
} else {
|
|
410
|
+
throw e;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Signs all the inputs of the transaction.
|
|
418
|
+
* @param {Psbt} transaction - The transaction to sign
|
|
419
|
+
* @protected
|
|
420
|
+
* @returns {Promise<void>}
|
|
421
|
+
*/
|
|
422
|
+
protected async signInputs(transaction: Psbt): Promise<void> {
|
|
423
|
+
for (let i = 0; i < transaction.data.inputs.length; i++) {
|
|
424
|
+
let input: PsbtInput = transaction.data.inputs[i];
|
|
425
|
+
|
|
426
|
+
try {
|
|
427
|
+
await this.signInput(transaction, input, i);
|
|
428
|
+
} catch (e) {
|
|
429
|
+
this.log(`Failed to sign input ${i}: ${(e as Error).stack}`);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
try {
|
|
434
|
+
transaction.finalizeAllInputs();
|
|
435
|
+
|
|
436
|
+
this.finalized = true;
|
|
437
|
+
} catch (e) {
|
|
438
|
+
this.finalized = false;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Converts the public key to x-only.
|
|
444
|
+
* @protected
|
|
445
|
+
* @returns {Buffer}
|
|
446
|
+
*/
|
|
447
|
+
protected internalPubKeyToXOnly(): Buffer {
|
|
448
|
+
return toXOnly(this.signer.publicKey);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Internal init.
|
|
453
|
+
* @protected
|
|
454
|
+
*/
|
|
455
|
+
protected internalInit(): void {
|
|
456
|
+
this.scriptData = payments.p2tr(this.generateScriptAddress());
|
|
457
|
+
this.tapData = payments.p2tr(this.generateTapData());
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Tweak the signer for the interaction
|
|
462
|
+
* @protected
|
|
463
|
+
*/
|
|
464
|
+
protected tweakSigner(): void {
|
|
465
|
+
if (this.tweakedSigner) return;
|
|
466
|
+
|
|
467
|
+
// tweaked p2tr signer.
|
|
468
|
+
this.tweakedSigner = this.getTweakedSigner(true);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Get the tweaked signer
|
|
473
|
+
* @private
|
|
474
|
+
* @returns {Signer} The tweaked signer
|
|
475
|
+
*/
|
|
476
|
+
protected getTweakedSigner(
|
|
477
|
+
useTweakedHash: boolean = false,
|
|
478
|
+
signer: Signer = this.signer,
|
|
479
|
+
): Signer | undefined {
|
|
480
|
+
const settings: TweakSettings = {
|
|
481
|
+
network: this.network,
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
if (useTweakedHash) {
|
|
485
|
+
settings.tweakHash = this.getTweakerHash();
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (!('privateKey' in signer)) {
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
return TweakedSigner.tweakSigner(signer as unknown as ECPairInterface, settings) as Signer;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Generate the PSBT input extended
|
|
497
|
+
* @param {UTXO} utxo The UTXO
|
|
498
|
+
* @param {number} i The index of the input
|
|
499
|
+
* @protected
|
|
500
|
+
* @returns {PsbtInputExtended} The PSBT input extended
|
|
501
|
+
*/
|
|
502
|
+
protected generatePsbtInputExtended(utxo: UTXO, i: number): PsbtInputExtended {
|
|
503
|
+
const input: PsbtInputExtended = {
|
|
504
|
+
hash: utxo.transactionId,
|
|
505
|
+
index: utxo.outputIndex,
|
|
506
|
+
witnessUtxo: {
|
|
507
|
+
value: Number(utxo.value),
|
|
508
|
+
script: Buffer.from(utxo.scriptPubKey.hex, 'hex'),
|
|
509
|
+
},
|
|
510
|
+
sequence: this.sequence,
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
if (this.sighashTypes) {
|
|
514
|
+
const inputSign = TweakedTransaction.calculateSignHash(this.sighashTypes);
|
|
515
|
+
if (inputSign) input.sighashType = inputSign;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
if (this.tapLeafScript) {
|
|
519
|
+
input.tapLeafScript = [this.tapLeafScript];
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (i === 0 && this.nonWitnessUtxo) {
|
|
523
|
+
input.nonWitnessUtxo = this.nonWitnessUtxo;
|
|
524
|
+
this.log(`Using non-witness utxo for input ${i}`);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// automatically detect p2tr inputs.
|
|
528
|
+
if (
|
|
529
|
+
utxo.scriptPubKey.address &&
|
|
530
|
+
AddressVerificator.isValidP2TRAddress(utxo.scriptPubKey.address, this.network)
|
|
531
|
+
) {
|
|
532
|
+
this.tweakSigner();
|
|
533
|
+
|
|
534
|
+
input.tapInternalKey = this.internalPubKeyToXOnly();
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
return input;
|
|
538
|
+
}
|
|
539
|
+
}
|