beignet 0.0.1
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/.eslintignore +4 -0
- package/.eslintrc +40 -0
- package/.prettierrc +9 -0
- package/LICENSE +21 -0
- package/README.md +166 -0
- package/dist/electrum/constants.js +31 -0
- package/dist/electrum/constants.js.map +1 -0
- package/dist/electrum/index.js +552 -0
- package/dist/electrum/index.js.map +1 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/shapes/electrum.js +14 -0
- package/dist/shapes/electrum.js.map +1 -0
- package/dist/shapes/index.js +19 -0
- package/dist/shapes/index.js.map +1 -0
- package/dist/shapes/wallet.js +115 -0
- package/dist/shapes/wallet.js.map +1 -0
- package/dist/transaction/index.js +692 -0
- package/dist/transaction/index.js.map +1 -0
- package/dist/types/electrum/constants.d.ts +7 -0
- package/dist/types/electrum/index.d.ts +85 -0
- package/dist/types/electrum.js +9 -0
- package/dist/types/electrum.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.js +20 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/shapes/electrum.d.ts +5 -0
- package/dist/types/shapes/index.d.ts +2 -0
- package/dist/types/shapes/wallet.d.ts +10 -0
- package/dist/types/transaction/index.d.ts +92 -0
- package/dist/types/transaction.js +12 -0
- package/dist/types/transaction.js.map +1 -0
- package/dist/types/types/electrum.d.ts +113 -0
- package/dist/types/types/index.d.ts +3 -0
- package/dist/types/types/transaction.d.ts +51 -0
- package/dist/types/types/wallet.d.ts +400 -0
- package/dist/types/utils/conversion.d.ts +10 -0
- package/dist/types/utils/derivation-path.d.ts +19 -0
- package/dist/types/utils/electrum.d.ts +10 -0
- package/dist/types/utils/exchange-rate.d.ts +30 -0
- package/dist/types/utils/helpers.d.ts +23 -0
- package/dist/types/utils/index.d.ts +7 -0
- package/dist/types/utils/result.d.ts +16 -0
- package/dist/types/utils/transaction.d.ts +21 -0
- package/dist/types/utils/wallet.d.ts +47 -0
- package/dist/types/wallet/constants.d.ts +12 -0
- package/dist/types/wallet/index.d.ts +214 -0
- package/dist/types/wallet.js +36 -0
- package/dist/types/wallet.js.map +1 -0
- package/dist/utils/conversion.js +52 -0
- package/dist/utils/conversion.js.map +1 -0
- package/dist/utils/derivation-path.js +104 -0
- package/dist/utils/derivation-path.js.map +1 -0
- package/dist/utils/electrum.js +149 -0
- package/dist/utils/electrum.js.map +1 -0
- package/dist/utils/exchange-rate.js +49 -0
- package/dist/utils/exchange-rate.js.map +1 -0
- package/dist/utils/helpers.js +137 -0
- package/dist/utils/helpers.js.map +1 -0
- package/dist/utils/index.js +24 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/result.js +36 -0
- package/dist/utils/result.js.map +1 -0
- package/dist/utils/transaction.js +327 -0
- package/dist/utils/transaction.js.map +1 -0
- package/dist/utils/wallet.js +227 -0
- package/dist/utils/wallet.js.map +1 -0
- package/dist/wallet/constants.js +15 -0
- package/dist/wallet/constants.js.map +1 -0
- package/dist/wallet/index.js +2113 -0
- package/dist/wallet/index.js.map +1 -0
- package/docs/html/.nojekyll +1 -0
- package/docs/html/assets/highlight.css +22 -0
- package/docs/html/assets/main.js +58 -0
- package/docs/html/assets/search.js +1 -0
- package/docs/html/assets/style.css +1367 -0
- package/docs/html/classes/Electrum.html +812 -0
- package/docs/html/classes/Transaction.html +687 -0
- package/docs/html/classes/Wallet.html +2030 -0
- package/docs/html/enums/EAddressType.html +227 -0
- package/docs/html/enums/EAvailableNetworks.html +255 -0
- package/docs/html/enums/EBoostType.html +220 -0
- package/docs/html/enums/EFeeId.html +241 -0
- package/docs/html/enums/EPaymentType.html +220 -0
- package/docs/html/enums/EProtocol.html +220 -0
- package/docs/html/enums/EUnit.html +227 -0
- package/docs/html/functions/availableNetworks.html +200 -0
- package/docs/html/functions/constructByteCountParam.html +210 -0
- package/docs/html/functions/decodeOpReturnMessage.html +202 -0
- package/docs/html/functions/decodeRawTransaction.html +210 -0
- package/docs/html/functions/err.html +214 -0
- package/docs/html/functions/formatKeyDerivationPath.html +219 -0
- package/docs/html/functions/formatPeerData.html +207 -0
- package/docs/html/functions/generateMnemonic.html +223 -0
- package/docs/html/functions/generateWalletId.html +202 -0
- package/docs/html/functions/getAddressFromScriptPubKey.html +209 -0
- package/docs/html/functions/getAddressTypeFromPath.html +206 -0
- package/docs/html/functions/getByteCount.html +206 -0
- package/docs/html/functions/getDataFallback.html +207 -0
- package/docs/html/functions/getDefaultPort.html +209 -0
- package/docs/html/functions/getDefaultWalletData.html +200 -0
- package/docs/html/functions/getDefaultWalletDataKeys.html +200 -0
- package/docs/html/functions/getExchangeRates.html +200 -0
- package/docs/html/functions/getHighestUsedIndexFromTxHashes.html +216 -0
- package/docs/html/functions/getKeyDerivationPath.html +211 -0
- package/docs/html/functions/getKeyDerivationPathObject.html +216 -0
- package/docs/html/functions/getKeyDerivationPathString.html +220 -0
- package/docs/html/functions/getKeyValue.html +206 -0
- package/docs/html/functions/getPeers.html +211 -0
- package/docs/html/functions/getProtocolForPort.html +209 -0
- package/docs/html/functions/getScriptHash.html +210 -0
- package/docs/html/functions/getSeed.html +209 -0
- package/docs/html/functions/getSeedHash.html +206 -0
- package/docs/html/functions/getSha256.html +206 -0
- package/docs/html/functions/getStorageKeyValues.html +209 -0
- package/docs/html/functions/getTxFee.html +210 -0
- package/docs/html/functions/getWalletDataStorageKey.html +206 -0
- package/docs/html/functions/isValidBech32mEncodedString.html +211 -0
- package/docs/html/functions/objectKeys-1.html +211 -0
- package/docs/html/functions/objectsMatch.html +210 -0
- package/docs/html/functions/ok.html +214 -0
- package/docs/html/functions/parseOnChainPaymentRequest.html +204 -0
- package/docs/html/functions/reduceValue.html +215 -0
- package/docs/html/functions/removeDustOutputs.html +206 -0
- package/docs/html/functions/setReplaceByFee.html +210 -0
- package/docs/html/functions/shuffleArray.html +211 -0
- package/docs/html/functions/validateAddress.html +216 -0
- package/docs/html/functions/validateMnemonic.html +206 -0
- package/docs/html/functions/validateTransaction.html +206 -0
- package/docs/html/index.html +360 -0
- package/docs/html/interfaces/IAddInput.html +231 -0
- package/docs/html/interfaces/IAddress.html +247 -0
- package/docs/html/interfaces/IAddressData.html +231 -0
- package/docs/html/interfaces/IAddressType.html +199 -0
- package/docs/html/interfaces/IAddressTypeData.html +252 -0
- package/docs/html/interfaces/IAddresses.html +199 -0
- package/docs/html/interfaces/IBoostedTransaction.html +238 -0
- package/docs/html/interfaces/IBoostedTransactions.html +199 -0
- package/docs/html/interfaces/ICreateTransaction.html +224 -0
- package/docs/html/interfaces/ICustomGetAddress.html +231 -0
- package/docs/html/interfaces/ICustomGetScriptHash.html +224 -0
- package/docs/html/interfaces/IElectrumGetAddressBalanceRes.html +235 -0
- package/docs/html/interfaces/IExchangeRates.html +210 -0
- package/docs/html/interfaces/IFormattedPeerData.html +245 -0
- package/docs/html/interfaces/IFormattedTransaction.html +343 -0
- package/docs/html/interfaces/IFormattedTransactions.html +199 -0
- package/docs/html/interfaces/IGenerateAddresses.html +252 -0
- package/docs/html/interfaces/IGenerateAddressesResponse.html +224 -0
- package/docs/html/interfaces/IGetAddress.html +231 -0
- package/docs/html/interfaces/IGetAddressBalanceRes.html +226 -0
- package/docs/html/interfaces/IGetAddressByPath.html +224 -0
- package/docs/html/interfaces/IGetAddressHistoryResponse.html +269 -0
- package/docs/html/interfaces/IGetAddressResponse.html +231 -0
- package/docs/html/interfaces/IGetAddressScriptHashesHistoryResponse.html +245 -0
- package/docs/html/interfaces/IGetDerivationPath.html +259 -0
- package/docs/html/interfaces/IGetFeeEstimatesResponse.html +238 -0
- package/docs/html/interfaces/IGetHeaderResponse.html +245 -0
- package/docs/html/interfaces/IGetNextAvailableAddressResponse.html +238 -0
- package/docs/html/interfaces/IGetTransactions.html +245 -0
- package/docs/html/interfaces/IGetTransactionsFromInputs.html +245 -0
- package/docs/html/interfaces/IGetUtxosResponse.html +224 -0
- package/docs/html/interfaces/IHeader.html +231 -0
- package/docs/html/interfaces/IIndexes.html +238 -0
- package/docs/html/interfaces/IKeyDerivationPath.html +247 -0
- package/docs/html/interfaces/IKeyDerivationPathData.html +224 -0
- package/docs/html/interfaces/INewBlock.html +224 -0
- package/docs/html/interfaces/IOnchainFees.html +245 -0
- package/docs/html/interfaces/IOutput.html +231 -0
- package/docs/html/interfaces/IPeerData.html +231 -0
- package/docs/html/interfaces/IRbfData.html +252 -0
- package/docs/html/interfaces/ISend.html +224 -0
- package/docs/html/interfaces/ISendTransaction.html +322 -0
- package/docs/html/interfaces/ISendTx.html +231 -0
- package/docs/html/interfaces/ISetupTransaction.html +238 -0
- package/docs/html/interfaces/ISubscribeToAddress.html +247 -0
- package/docs/html/interfaces/ISubscribeToHeader.html +245 -0
- package/docs/html/interfaces/ITargets.html +238 -0
- package/docs/html/interfaces/ITransaction.html +281 -0
- package/docs/html/interfaces/ITxHash.html +217 -0
- package/docs/html/interfaces/ITxHashes.html +235 -0
- package/docs/html/interfaces/IUtxo.html +275 -0
- package/docs/html/interfaces/IVin.html +252 -0
- package/docs/html/interfaces/IVout.html +246 -0
- package/docs/html/interfaces/IWallet.html +350 -0
- package/docs/html/interfaces/IWalletData.html +343 -0
- package/docs/html/types/ElectrumConnectionPubSub.html +233 -0
- package/docs/html/types/ElectrumConnectionSubscription.html +204 -0
- package/docs/html/types/InputData.html +203 -0
- package/docs/html/types/ObjectKeys.html +198 -0
- package/docs/html/types/Result.html +201 -0
- package/docs/html/types/TAddressIndexInfo.html +204 -0
- package/docs/html/types/TAddressLabel.html +193 -0
- package/docs/html/types/TAddressType.html +193 -0
- package/docs/html/types/TAddressTypeContent.html +198 -0
- package/docs/html/types/TAddressTypes.html +193 -0
- package/docs/html/types/TAvailableNetworks.html +193 -0
- package/docs/html/types/TConnectToElectrumRes.html +193 -0
- package/docs/html/types/TDecodeRawTx.html +214 -0
- package/docs/html/types/TElectrumNetworks.html +193 -0
- package/docs/html/types/TGetAddressHistory.html +200 -0
- package/docs/html/types/TGetByteCountInput.html +193 -0
- package/docs/html/types/TGetByteCountInputs.html +193 -0
- package/docs/html/types/TGetByteCountOutput.html +193 -0
- package/docs/html/types/TGetByteCountOutputs.html +193 -0
- package/docs/html/types/TGetData.html +211 -0
- package/docs/html/types/TGetTotalFeeObj.html +204 -0
- package/docs/html/types/TKeyDerivationAccount.html +193 -0
- package/docs/html/types/TKeyDerivationChange.html +193 -0
- package/docs/html/types/TKeyDerivationCoinType.html +193 -0
- package/docs/html/types/TKeyDerivationIndex.html +193 -0
- package/docs/html/types/TKeyDerivationPurpose.html +193 -0
- package/docs/html/types/TMessageDataMap.html +210 -0
- package/docs/html/types/TMessageKeys.html +193 -0
- package/docs/html/types/TOnMessage.html +213 -0
- package/docs/html/types/TProcessUnconfirmedTransactions.html +202 -0
- package/docs/html/types/TProtocol.html +193 -0
- package/docs/html/types/TServer.html +204 -0
- package/docs/html/types/TSetData.html +213 -0
- package/docs/html/types/TSetupTransactionResponse.html +193 -0
- package/docs/html/types/TStorage.html +200 -0
- package/docs/html/types/TSubscribedReceive.html +193 -0
- package/docs/html/types/TTransactionMessage.html +198 -0
- package/docs/html/types/TTxResponse.html +206 -0
- package/docs/html/types/TTxResult.html +200 -0
- package/docs/html/types/TUnspentAddressScriptHashData.html +198 -0
- package/docs/html/types/TWalletDataKeys.html +193 -0
- package/docs/html/variables/defaultElectrumPorts.html +193 -0
- package/docs/html/variables/electrumConnection.html +199 -0
- package/docs/html/variables/mostUsedExchangeTickers.html +241 -0
- package/docs/markdown/.nojekyll +1 -0
- package/docs/markdown/README.md +1875 -0
- package/docs/markdown/classes/Electrum.md +718 -0
- package/docs/markdown/classes/Transaction.md +536 -0
- package/docs/markdown/classes/Wallet.md +2189 -0
- package/docs/markdown/enums/EAddressType.md +41 -0
- package/docs/markdown/enums/EAvailableNetworks.md +85 -0
- package/docs/markdown/enums/EBoostType.md +30 -0
- package/docs/markdown/enums/EFeeId.md +63 -0
- package/docs/markdown/enums/EPaymentType.md +30 -0
- package/docs/markdown/enums/EProtocol.md +30 -0
- package/docs/markdown/enums/EUnit.md +41 -0
- package/docs/markdown/interfaces/IAddInput.md +41 -0
- package/docs/markdown/interfaces/IAddress.md +69 -0
- package/docs/markdown/interfaces/IAddressData.md +41 -0
- package/docs/markdown/interfaces/IAddressType.md +7 -0
- package/docs/markdown/interfaces/IAddressTypeData.md +74 -0
- package/docs/markdown/interfaces/IAddresses.md +7 -0
- package/docs/markdown/interfaces/IBoostedTransaction.md +52 -0
- package/docs/markdown/interfaces/IBoostedTransactions.md +7 -0
- package/docs/markdown/interfaces/ICreateTransaction.md +30 -0
- package/docs/markdown/interfaces/ICustomGetAddress.md +41 -0
- package/docs/markdown/interfaces/ICustomGetScriptHash.md +30 -0
- package/docs/markdown/interfaces/IElectrumGetAddressBalanceRes.md +55 -0
- package/docs/markdown/interfaces/IExchangeRates.md +7 -0
- package/docs/markdown/interfaces/IFormattedPeerData.md +63 -0
- package/docs/markdown/interfaces/IFormattedTransaction.md +217 -0
- package/docs/markdown/interfaces/IFormattedTransactions.md +7 -0
- package/docs/markdown/interfaces/IGenerateAddresses.md +74 -0
- package/docs/markdown/interfaces/IGenerateAddressesResponse.md +30 -0
- package/docs/markdown/interfaces/IGetAddress.md +41 -0
- package/docs/markdown/interfaces/IGetAddressBalanceRes.md +36 -0
- package/docs/markdown/interfaces/IGetAddressByPath.md +30 -0
- package/docs/markdown/interfaces/IGetAddressHistoryResponse.md +121 -0
- package/docs/markdown/interfaces/IGetAddressResponse.md +41 -0
- package/docs/markdown/interfaces/IGetAddressScriptHashesHistoryResponse.md +63 -0
- package/docs/markdown/interfaces/IGetDerivationPath.md +100 -0
- package/docs/markdown/interfaces/IGetFeeEstimatesResponse.md +52 -0
- package/docs/markdown/interfaces/IGetHeaderResponse.md +63 -0
- package/docs/markdown/interfaces/IGetNextAvailableAddressResponse.md +52 -0
- package/docs/markdown/interfaces/IGetTransactions.md +63 -0
- package/docs/markdown/interfaces/IGetTransactionsFromInputs.md +63 -0
- package/docs/markdown/interfaces/IGetUtxosResponse.md +30 -0
- package/docs/markdown/interfaces/IHeader.md +41 -0
- package/docs/markdown/interfaces/IIndexes.md +52 -0
- package/docs/markdown/interfaces/IKeyDerivationPath.md +69 -0
- package/docs/markdown/interfaces/IKeyDerivationPathData.md +30 -0
- package/docs/markdown/interfaces/INewBlock.md +30 -0
- package/docs/markdown/interfaces/IOnchainFees.md +63 -0
- package/docs/markdown/interfaces/IOutput.md +41 -0
- package/docs/markdown/interfaces/IPeerData.md +41 -0
- package/docs/markdown/interfaces/IRbfData.md +74 -0
- package/docs/markdown/interfaces/ISend.md +30 -0
- package/docs/markdown/interfaces/ISendTransaction.md +184 -0
- package/docs/markdown/interfaces/ISendTx.md +41 -0
- package/docs/markdown/interfaces/ISetupTransaction.md +52 -0
- package/docs/markdown/interfaces/ISubscribeToAddress.md +60 -0
- package/docs/markdown/interfaces/ISubscribeToHeader.md +59 -0
- package/docs/markdown/interfaces/ITargets.md +52 -0
- package/docs/markdown/interfaces/ITransaction.md +88 -0
- package/docs/markdown/interfaces/ITxHash.md +19 -0
- package/docs/markdown/interfaces/ITxHashes.md +55 -0
- package/docs/markdown/interfaces/IUtxo.md +112 -0
- package/docs/markdown/interfaces/IVin.md +70 -0
- package/docs/markdown/interfaces/IVout.md +52 -0
- package/docs/markdown/interfaces/IWallet.md +209 -0
- package/docs/markdown/interfaces/IWalletData.md +217 -0
- package/example/helpers.ts +76 -0
- package/example/index.ts +77 -0
- package/package.json +69 -0
- package/src/electrum/constants.ts +28 -0
- package/src/electrum/index.ts +838 -0
- package/src/index.ts +6 -0
- package/src/shapes/electrum.ts +13 -0
- package/src/shapes/index.ts +2 -0
- package/src/shapes/wallet.ts +133 -0
- package/src/transaction/index.ts +1078 -0
- package/src/types/electrum.ts +139 -0
- package/src/types/index.ts +3 -0
- package/src/types/transaction.ts +58 -0
- package/src/types/wallet.ts +503 -0
- package/src/utils/conversion.ts +88 -0
- package/src/utils/derivation-path.ts +171 -0
- package/src/utils/electrum.ts +188 -0
- package/src/utils/exchange-rate.ts +70 -0
- package/src/utils/helpers.ts +191 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/result.ts +79 -0
- package/src/utils/transaction.ts +417 -0
- package/src/utils/wallet.ts +351 -0
- package/src/wallet/constants.ts +24 -0
- package/src/wallet/index.ts +3304 -0
- package/tests/constants.ts +9 -0
- package/tests/derivation.test.ts +146 -0
- package/tests/electrum.test.ts +137 -0
- package/tests/storage.test.ts +222 -0
- package/tests/transaction.test.ts +187 -0
- package/tests/wallet.test.ts +307 -0
- package/tsconfig.json +32 -0
- package/typedoc.json +5 -0
|
@@ -0,0 +1,692 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.Transaction = void 0;
|
|
30
|
+
const types_1 = require("../types");
|
|
31
|
+
const shapes_1 = require("../shapes");
|
|
32
|
+
const utils_1 = require("../utils");
|
|
33
|
+
const utils_2 = require("../utils");
|
|
34
|
+
const constants_1 = require("../wallet/constants");
|
|
35
|
+
const utils_3 = require("../utils");
|
|
36
|
+
const bitcoinjs_lib_1 = require("bitcoinjs-lib");
|
|
37
|
+
const secp256k1_1 = __importDefault(require("@bitcoinerlab/secp256k1"));
|
|
38
|
+
const bitcoin = __importStar(require("bitcoinjs-lib"));
|
|
39
|
+
const bitcoin_address_validation_1 = require("bitcoin-address-validation");
|
|
40
|
+
bitcoin.initEccLib(secp256k1_1.default);
|
|
41
|
+
class Transaction {
|
|
42
|
+
constructor({ wallet }) {
|
|
43
|
+
this.getTotalFee = ({ satsPerByte, message = '', transaction = this.data, fundingLightning = false }) => {
|
|
44
|
+
const baseTransactionSize = constants_1.TRANSACTION_DEFAULTS.recommendedBaseFee;
|
|
45
|
+
try {
|
|
46
|
+
const inputs = transaction.inputs || [];
|
|
47
|
+
const outputs = transaction.outputs || [];
|
|
48
|
+
const changeAddress = transaction.changeAddress;
|
|
49
|
+
const inputAddresses = inputs.map((input) => input.address);
|
|
50
|
+
const outputAddresses = outputs.map((output) => output.address);
|
|
51
|
+
if (changeAddress && !transaction.max) {
|
|
52
|
+
outputAddresses.push(changeAddress);
|
|
53
|
+
}
|
|
54
|
+
const inputParam = (0, utils_3.constructByteCountParam)(inputAddresses);
|
|
55
|
+
const outputParam = (0, utils_3.constructByteCountParam)(outputAddresses);
|
|
56
|
+
if (fundingLightning) {
|
|
57
|
+
outputParam.P2WPKH = (outputParam.P2WPKH || 0) + 1;
|
|
58
|
+
}
|
|
59
|
+
const transactionByteCount = (0, utils_3.getByteCount)(inputParam, outputParam, message);
|
|
60
|
+
return transactionByteCount * satsPerByte;
|
|
61
|
+
}
|
|
62
|
+
catch (_a) {
|
|
63
|
+
return baseTransactionSize * satsPerByte;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
this.getTotalFeeObj = ({ satsPerByte = this._wallet.feeEstimates.normal, message = '', transaction = this.data, fundingLightning = false } = {}) => {
|
|
67
|
+
var _a;
|
|
68
|
+
try {
|
|
69
|
+
if (!((_a = transaction.inputs) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
70
|
+
this.setupTransaction({});
|
|
71
|
+
transaction = this.data;
|
|
72
|
+
}
|
|
73
|
+
const inputs = transaction.inputs || [];
|
|
74
|
+
const outputs = transaction.outputs || [];
|
|
75
|
+
const changeAddress = transaction.changeAddress;
|
|
76
|
+
const inputAddresses = inputs.map((input) => input.address);
|
|
77
|
+
const outputAddresses = outputs.map((output) => output.address);
|
|
78
|
+
let increaseAddressCount = 0;
|
|
79
|
+
if (!outputAddresses.length) {
|
|
80
|
+
increaseAddressCount++;
|
|
81
|
+
}
|
|
82
|
+
if (changeAddress && !transaction.max) {
|
|
83
|
+
outputAddresses.push(changeAddress);
|
|
84
|
+
}
|
|
85
|
+
const inputParam = (0, utils_3.constructByteCountParam)(inputAddresses);
|
|
86
|
+
const outputParam = (0, utils_3.constructByteCountParam)(outputAddresses, [
|
|
87
|
+
{ addrType: this._wallet.data.addressType, count: increaseAddressCount }
|
|
88
|
+
]);
|
|
89
|
+
if (fundingLightning) {
|
|
90
|
+
outputParam.P2WPKH = (outputParam.P2WPKH || 0) + 1;
|
|
91
|
+
}
|
|
92
|
+
const transactionByteCount = (0, utils_3.getByteCount)(inputParam, outputParam, message);
|
|
93
|
+
const maxSatPerByte = this.getMaxSatsPerByte({
|
|
94
|
+
transactionByteCount,
|
|
95
|
+
balance: this._wallet.getBalance()
|
|
96
|
+
});
|
|
97
|
+
if (maxSatPerByte < satsPerByte) {
|
|
98
|
+
return (0, utils_1.ok)({
|
|
99
|
+
totalFee: transactionByteCount * maxSatPerByte,
|
|
100
|
+
transactionByteCount: maxSatPerByte ? transactionByteCount : 0,
|
|
101
|
+
satsPerByte: maxSatPerByte,
|
|
102
|
+
maxSatPerByte
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
return (0, utils_1.ok)({
|
|
106
|
+
totalFee: transactionByteCount * satsPerByte,
|
|
107
|
+
transactionByteCount,
|
|
108
|
+
satsPerByte,
|
|
109
|
+
maxSatPerByte
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
catch (e) {
|
|
113
|
+
return (0, utils_1.err)(e);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
this.getMaxSatsPerByte = ({ transactionByteCount, balance = this._wallet.getBalance() }) => {
|
|
117
|
+
return Math.floor(balance / (2 * transactionByteCount));
|
|
118
|
+
};
|
|
119
|
+
this.createTransaction = async ({ transactionData = this.data, shuffleOutputs = true } = {}) => {
|
|
120
|
+
(0, utils_3.removeDustOutputs)(transactionData.outputs);
|
|
121
|
+
const inputValue = this.getTransactionInputValue({
|
|
122
|
+
inputs: transactionData.inputs
|
|
123
|
+
});
|
|
124
|
+
const outputValue = this.getTransactionOutputValue({
|
|
125
|
+
outputs: transactionData.outputs
|
|
126
|
+
});
|
|
127
|
+
if (inputValue === 0) {
|
|
128
|
+
const message = 'No inputs to spend.';
|
|
129
|
+
return (0, utils_1.err)(message);
|
|
130
|
+
}
|
|
131
|
+
const fee = inputValue - outputValue;
|
|
132
|
+
if (fee > inputValue) {
|
|
133
|
+
const message = 'Fee is larger than the intended payment.';
|
|
134
|
+
return (0, utils_1.err)(message);
|
|
135
|
+
}
|
|
136
|
+
const validateRes = (0, utils_1.validateTransaction)(transactionData);
|
|
137
|
+
if (validateRes.isErr())
|
|
138
|
+
return (0, utils_1.err)(validateRes.error.message);
|
|
139
|
+
try {
|
|
140
|
+
const bip32InterfaceRes = await this._wallet.getBip32Interface();
|
|
141
|
+
if (bip32InterfaceRes.isErr()) {
|
|
142
|
+
return (0, utils_1.err)(bip32InterfaceRes.error.message);
|
|
143
|
+
}
|
|
144
|
+
const psbtRes = await this.createPsbtFromTransactionData({
|
|
145
|
+
transactionData,
|
|
146
|
+
bip32Interface: bip32InterfaceRes.value,
|
|
147
|
+
shuffleTargets: shuffleOutputs
|
|
148
|
+
});
|
|
149
|
+
if (psbtRes.isErr()) {
|
|
150
|
+
return (0, utils_1.err)(psbtRes.error);
|
|
151
|
+
}
|
|
152
|
+
const psbt = psbtRes.value;
|
|
153
|
+
const signedPsbtRes = await this.signPsbt({
|
|
154
|
+
psbt,
|
|
155
|
+
bip32Interface: bip32InterfaceRes.value
|
|
156
|
+
});
|
|
157
|
+
if (signedPsbtRes.isErr()) {
|
|
158
|
+
return (0, utils_1.err)(signedPsbtRes.error);
|
|
159
|
+
}
|
|
160
|
+
const tx = signedPsbtRes.value.extractTransaction();
|
|
161
|
+
const id = tx.getId();
|
|
162
|
+
const hex = tx.toHex();
|
|
163
|
+
return (0, utils_1.ok)({ id, hex });
|
|
164
|
+
}
|
|
165
|
+
catch (e) {
|
|
166
|
+
return (0, utils_1.err)(e);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
this.getTransactionInputValue = ({ inputs }) => {
|
|
170
|
+
try {
|
|
171
|
+
if (!inputs) {
|
|
172
|
+
const transaction = this.data;
|
|
173
|
+
inputs = transaction.inputs;
|
|
174
|
+
}
|
|
175
|
+
if (inputs) {
|
|
176
|
+
const response = (0, utils_2.reduceValue)({ arr: inputs, value: 'value' });
|
|
177
|
+
if (response.isOk()) {
|
|
178
|
+
return response.value;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return 0;
|
|
182
|
+
}
|
|
183
|
+
catch (e) {
|
|
184
|
+
return 0;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
this.signPsbt = async ({ psbt, bip32Interface }) => {
|
|
188
|
+
const transactionDataRes = this.data;
|
|
189
|
+
const { inputs } = transactionDataRes;
|
|
190
|
+
for (const [index, input] of inputs.entries()) {
|
|
191
|
+
try {
|
|
192
|
+
const keyPair = bip32Interface.derivePath(input.path);
|
|
193
|
+
psbt.signInput(index, keyPair);
|
|
194
|
+
}
|
|
195
|
+
catch (e) {
|
|
196
|
+
return (0, utils_1.err)(e);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
psbt.finalizeAllInputs();
|
|
200
|
+
return (0, utils_1.ok)(psbt);
|
|
201
|
+
};
|
|
202
|
+
this.createPsbtFromTransactionData = async ({ transactionData, bip32Interface, shuffleTargets = true }) => {
|
|
203
|
+
const { inputs, outputs, fee, rbf } = transactionData;
|
|
204
|
+
let { changeAddress, message } = transactionData;
|
|
205
|
+
const balance = this.getTransactionInputValue({
|
|
206
|
+
inputs
|
|
207
|
+
});
|
|
208
|
+
const outputValue = this.getTransactionOutputValue({
|
|
209
|
+
outputs
|
|
210
|
+
});
|
|
211
|
+
const network = bitcoinjs_lib_1.networks[this._wallet.network];
|
|
212
|
+
let targets = outputs.concat();
|
|
213
|
+
if (changeAddress) {
|
|
214
|
+
const changeAddressValue = balance - (outputValue + fee);
|
|
215
|
+
if (changeAddressValue > constants_1.TRANSACTION_DEFAULTS.dustLimit) {
|
|
216
|
+
targets.push({
|
|
217
|
+
address: changeAddress,
|
|
218
|
+
value: changeAddressValue,
|
|
219
|
+
index: targets.length
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
else if (outputValue + fee < balance) {
|
|
224
|
+
const diffValue = balance - (outputValue + fee);
|
|
225
|
+
if (diffValue > constants_1.TRANSACTION_DEFAULTS.dustLimit) {
|
|
226
|
+
const changeAddressRes = await this._wallet.getChangeAddress();
|
|
227
|
+
if (changeAddressRes.isErr()) {
|
|
228
|
+
return (0, utils_1.err)(changeAddressRes.error.message);
|
|
229
|
+
}
|
|
230
|
+
changeAddress = changeAddressRes.value.address;
|
|
231
|
+
targets.push({
|
|
232
|
+
address: changeAddress,
|
|
233
|
+
value: diffValue,
|
|
234
|
+
index: targets.length
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
if (message.trim() !== '') {
|
|
239
|
+
const messageLength = message.length;
|
|
240
|
+
const lengthMin = 5;
|
|
241
|
+
if (messageLength > 0 && messageLength < lengthMin) {
|
|
242
|
+
message += ' '.repeat(lengthMin - messageLength);
|
|
243
|
+
}
|
|
244
|
+
const data = Buffer.from(message, 'utf8');
|
|
245
|
+
const embed = bitcoin.payments.embed({
|
|
246
|
+
data: [data],
|
|
247
|
+
network
|
|
248
|
+
});
|
|
249
|
+
targets.push({ script: embed.output, value: 0, index: targets.length });
|
|
250
|
+
}
|
|
251
|
+
if (!bip32Interface) {
|
|
252
|
+
const bip32InterfaceRes = await this._wallet.getBip32Interface();
|
|
253
|
+
if (bip32InterfaceRes.isErr()) {
|
|
254
|
+
return (0, utils_1.err)(bip32InterfaceRes.error.message);
|
|
255
|
+
}
|
|
256
|
+
bip32Interface = bip32InterfaceRes.value;
|
|
257
|
+
}
|
|
258
|
+
const root = bip32Interface;
|
|
259
|
+
const psbt = new bitcoin.Psbt({ network });
|
|
260
|
+
try {
|
|
261
|
+
for (const input of inputs) {
|
|
262
|
+
const path = input.path;
|
|
263
|
+
const keyPair = root.derivePath(path);
|
|
264
|
+
await this.addInput({
|
|
265
|
+
psbt,
|
|
266
|
+
keyPair,
|
|
267
|
+
input
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
catch (e) {
|
|
272
|
+
return (0, utils_1.err)(e);
|
|
273
|
+
}
|
|
274
|
+
(0, utils_3.setReplaceByFee)({ psbt, setRbf: !!rbf });
|
|
275
|
+
if (shuffleTargets) {
|
|
276
|
+
targets = (0, utils_2.shuffleArray)(targets);
|
|
277
|
+
}
|
|
278
|
+
targets.forEach((target) => {
|
|
279
|
+
var _a;
|
|
280
|
+
let isOpReturn = false;
|
|
281
|
+
try {
|
|
282
|
+
isOpReturn = !!target.script;
|
|
283
|
+
}
|
|
284
|
+
catch (e) { }
|
|
285
|
+
if (isOpReturn) {
|
|
286
|
+
if (target.script) {
|
|
287
|
+
psbt.addOutput({
|
|
288
|
+
script: target.script,
|
|
289
|
+
value: (_a = target.value) !== null && _a !== void 0 ? _a : 0
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
if (target.address && target.value) {
|
|
295
|
+
psbt.addOutput({
|
|
296
|
+
address: target.address,
|
|
297
|
+
value: target.value
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
return (0, utils_1.ok)(psbt);
|
|
303
|
+
};
|
|
304
|
+
this.addInput = async ({ psbt, keyPair, input }) => {
|
|
305
|
+
try {
|
|
306
|
+
const network = bitcoinjs_lib_1.networks[this._wallet.network];
|
|
307
|
+
const { type } = (0, bitcoin_address_validation_1.getAddressInfo)(input.address);
|
|
308
|
+
if (!input.value) {
|
|
309
|
+
return (0, utils_1.err)('No input provided.');
|
|
310
|
+
}
|
|
311
|
+
if (type === 'p2wpkh') {
|
|
312
|
+
const p2wpkh = bitcoin.payments.p2wpkh({
|
|
313
|
+
pubkey: keyPair.publicKey,
|
|
314
|
+
network
|
|
315
|
+
});
|
|
316
|
+
if (!(p2wpkh === null || p2wpkh === void 0 ? void 0 : p2wpkh.output)) {
|
|
317
|
+
return (0, utils_1.err)('p2wpkh.output is undefined.');
|
|
318
|
+
}
|
|
319
|
+
psbt.addInput({
|
|
320
|
+
hash: input.tx_hash,
|
|
321
|
+
index: input.tx_pos,
|
|
322
|
+
witnessUtxo: {
|
|
323
|
+
script: p2wpkh.output,
|
|
324
|
+
value: input.value
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
if (type === 'p2sh') {
|
|
329
|
+
const p2wpkh = bitcoin.payments.p2wpkh({
|
|
330
|
+
pubkey: keyPair.publicKey,
|
|
331
|
+
network
|
|
332
|
+
});
|
|
333
|
+
const p2sh = bitcoin.payments.p2sh({ redeem: p2wpkh, network });
|
|
334
|
+
if (!(p2sh === null || p2sh === void 0 ? void 0 : p2sh.output)) {
|
|
335
|
+
return (0, utils_1.err)('p2sh.output is undefined.');
|
|
336
|
+
}
|
|
337
|
+
if (!(p2sh === null || p2sh === void 0 ? void 0 : p2sh.redeem)) {
|
|
338
|
+
return (0, utils_1.err)('p2sh.redeem.output is undefined.');
|
|
339
|
+
}
|
|
340
|
+
psbt.addInput({
|
|
341
|
+
hash: input.tx_hash,
|
|
342
|
+
index: input.tx_pos,
|
|
343
|
+
witnessUtxo: {
|
|
344
|
+
script: p2sh.output,
|
|
345
|
+
value: input.value
|
|
346
|
+
},
|
|
347
|
+
redeemScript: p2sh.redeem.output
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
if (type === 'p2pkh') {
|
|
351
|
+
const transaction = await this._wallet.electrum.getTransactions({
|
|
352
|
+
txHashes: [{ tx_hash: input.tx_hash }]
|
|
353
|
+
});
|
|
354
|
+
if (transaction.isErr()) {
|
|
355
|
+
return (0, utils_1.err)(transaction.error.message);
|
|
356
|
+
}
|
|
357
|
+
const hex = transaction.value.data[0].result.hex;
|
|
358
|
+
const nonWitnessUtxo = Buffer.from(hex, 'hex');
|
|
359
|
+
psbt.addInput({
|
|
360
|
+
hash: input.tx_hash,
|
|
361
|
+
index: input.tx_pos,
|
|
362
|
+
nonWitnessUtxo
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
return (0, utils_1.ok)('Success');
|
|
366
|
+
}
|
|
367
|
+
catch (_a) {
|
|
368
|
+
return (0, utils_1.err)('Unable to add input.');
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
this.addOutput = async ({ address, value, index = 0 }) => {
|
|
372
|
+
var _a;
|
|
373
|
+
if (!((_a = this.data.inputs) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
374
|
+
const setupRes = await this.setupTransaction();
|
|
375
|
+
if (setupRes.isErr())
|
|
376
|
+
return (0, utils_1.err)(setupRes.error.message);
|
|
377
|
+
}
|
|
378
|
+
return this.updateSendTransaction({
|
|
379
|
+
transaction: {
|
|
380
|
+
outputs: [{ address, value, index }]
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
};
|
|
384
|
+
this.getTransactionOutputValue = ({ outputs } = {}) => {
|
|
385
|
+
try {
|
|
386
|
+
if (!outputs) {
|
|
387
|
+
const transaction = this.data;
|
|
388
|
+
outputs = transaction.outputs;
|
|
389
|
+
}
|
|
390
|
+
const response = (0, utils_2.reduceValue)({ arr: outputs, value: 'value' });
|
|
391
|
+
if (response.isOk()) {
|
|
392
|
+
return response.value;
|
|
393
|
+
}
|
|
394
|
+
return 0;
|
|
395
|
+
}
|
|
396
|
+
catch (_a) {
|
|
397
|
+
return 0;
|
|
398
|
+
}
|
|
399
|
+
};
|
|
400
|
+
this.updateSendTransaction = ({ transaction }) => {
|
|
401
|
+
try {
|
|
402
|
+
if (transaction.outputs) {
|
|
403
|
+
const currentTransaction = this._wallet.transaction.data;
|
|
404
|
+
const outputs = currentTransaction.outputs.concat();
|
|
405
|
+
transaction.outputs.forEach((output) => {
|
|
406
|
+
outputs[output.index] = output;
|
|
407
|
+
});
|
|
408
|
+
transaction.outputs = outputs;
|
|
409
|
+
}
|
|
410
|
+
this._data = Object.assign(Object.assign({}, this._data), transaction);
|
|
411
|
+
this._wallet.saveWalletData('transaction', this.data).then();
|
|
412
|
+
return (0, utils_1.ok)('Transaction updated');
|
|
413
|
+
}
|
|
414
|
+
catch (e) {
|
|
415
|
+
return (0, utils_1.err)(e);
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
this.sendMax = async ({ address, transaction, index = 0, satsPerByte, rbf = false } = {}) => {
|
|
419
|
+
var _a, _b, _c, _d;
|
|
420
|
+
try {
|
|
421
|
+
if (!transaction) {
|
|
422
|
+
transaction = this.data;
|
|
423
|
+
}
|
|
424
|
+
if (!((_a = transaction.inputs) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
425
|
+
const setupRes = await this.setupTransaction({ rbf });
|
|
426
|
+
if (setupRes.isErr())
|
|
427
|
+
return (0, utils_1.err)(setupRes.error.message);
|
|
428
|
+
}
|
|
429
|
+
const outputs = (_b = transaction.outputs) !== null && _b !== void 0 ? _b : [];
|
|
430
|
+
if (!address) {
|
|
431
|
+
address = (_d = (_c = outputs[index]) === null || _c === void 0 ? void 0 : _c.address) !== null && _d !== void 0 ? _d : '';
|
|
432
|
+
}
|
|
433
|
+
const maxAmountResponse = this.getMaxSendAmount({
|
|
434
|
+
satsPerByte: satsPerByte !== null && satsPerByte !== void 0 ? satsPerByte : 1,
|
|
435
|
+
selectedFeeId: transaction.selectedFeeId,
|
|
436
|
+
transaction
|
|
437
|
+
});
|
|
438
|
+
if (maxAmountResponse.isErr()) {
|
|
439
|
+
return (0, utils_1.err)(maxAmountResponse.error);
|
|
440
|
+
}
|
|
441
|
+
const { amount, fee } = maxAmountResponse.value;
|
|
442
|
+
if (!transaction.max) {
|
|
443
|
+
this.updateSendTransaction({
|
|
444
|
+
transaction: {
|
|
445
|
+
max: true,
|
|
446
|
+
outputs: [{ address, value: amount, index }],
|
|
447
|
+
fee
|
|
448
|
+
}
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
this.updateSendTransaction({
|
|
453
|
+
transaction: {
|
|
454
|
+
max: false
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
return (0, utils_1.ok)('Successfully setup max send transaction.');
|
|
459
|
+
}
|
|
460
|
+
catch (e) {
|
|
461
|
+
return (0, utils_1.err)(e);
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
this.estimateTransactionCosts = ({ transaction, customFeeRate }) => {
|
|
465
|
+
var _a, _b;
|
|
466
|
+
try {
|
|
467
|
+
if (!transaction) {
|
|
468
|
+
transaction = this.data;
|
|
469
|
+
}
|
|
470
|
+
const currentWallet = this._wallet.data;
|
|
471
|
+
const onchainBalance = currentWallet.balance;
|
|
472
|
+
const inputValue = this.getTransactionInputValue({
|
|
473
|
+
inputs: transaction.inputs
|
|
474
|
+
});
|
|
475
|
+
const amount = onchainBalance > inputValue ? onchainBalance : inputValue;
|
|
476
|
+
let utxos = [];
|
|
477
|
+
if (transaction.inputs.length > (currentWallet === null || currentWallet === void 0 ? void 0 : currentWallet.utxos.length)) {
|
|
478
|
+
utxos = transaction.inputs;
|
|
479
|
+
}
|
|
480
|
+
else {
|
|
481
|
+
utxos = (_a = currentWallet === null || currentWallet === void 0 ? void 0 : currentWallet.utxos) !== null && _a !== void 0 ? _a : [];
|
|
482
|
+
}
|
|
483
|
+
const fees = this._wallet.feeEstimates;
|
|
484
|
+
const selectedFeeId = this._wallet.selectedFeeId;
|
|
485
|
+
const satsPerByte = (_b = customFeeRate !== null && customFeeRate !== void 0 ? customFeeRate : fees[selectedFeeId]) !== null && _b !== void 0 ? _b : 1;
|
|
486
|
+
const fee = this.getTotalFee({
|
|
487
|
+
satsPerByte,
|
|
488
|
+
message: transaction.message,
|
|
489
|
+
transaction: Object.assign(Object.assign({}, transaction), { max: true, inputs: utxos, selectedFeeId,
|
|
490
|
+
satsPerByte })
|
|
491
|
+
});
|
|
492
|
+
const maxAmount = {
|
|
493
|
+
amount: amount - fee,
|
|
494
|
+
fee,
|
|
495
|
+
satsPerByte
|
|
496
|
+
};
|
|
497
|
+
if (amount <= fee) {
|
|
498
|
+
return (0, utils_1.err)('Balance is too low to spend.');
|
|
499
|
+
}
|
|
500
|
+
return (0, utils_1.ok)(maxAmount);
|
|
501
|
+
}
|
|
502
|
+
catch (e) {
|
|
503
|
+
return (0, utils_1.err)(e);
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
this._wallet = wallet;
|
|
507
|
+
this._data = (0, shapes_1.getDefaultSendTransaction)();
|
|
508
|
+
}
|
|
509
|
+
get data() {
|
|
510
|
+
return this._data;
|
|
511
|
+
}
|
|
512
|
+
async setupTransaction({ inputTxHashes, utxos, rbf = false, satsPerByte = 1 } = {}) {
|
|
513
|
+
var _a;
|
|
514
|
+
try {
|
|
515
|
+
const addressType = this._wallet.data.addressType;
|
|
516
|
+
const currentWallet = this._wallet.data;
|
|
517
|
+
const transaction = currentWallet.transaction;
|
|
518
|
+
let inputs = [];
|
|
519
|
+
if (inputTxHashes) {
|
|
520
|
+
inputs = currentWallet.utxos.filter((utxo) => {
|
|
521
|
+
return inputTxHashes.includes(utxo.tx_hash);
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
else if (utxos) {
|
|
525
|
+
inputs = utxos;
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
inputs = currentWallet.utxos;
|
|
529
|
+
}
|
|
530
|
+
if (!inputs.length) {
|
|
531
|
+
if (transaction.inputs.length > 0) {
|
|
532
|
+
inputs = transaction.inputs;
|
|
533
|
+
}
|
|
534
|
+
else {
|
|
535
|
+
inputs = this.removeBlackListedUtxos(currentWallet.utxos);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
if (!inputs.length) {
|
|
539
|
+
return (0, utils_1.err)('No inputs specified.');
|
|
540
|
+
}
|
|
541
|
+
const currentChangeAddresses = currentWallet.changeAddresses;
|
|
542
|
+
const addressTypeKeys = Object.values(types_1.EAddressType);
|
|
543
|
+
let changeAddresses = {};
|
|
544
|
+
addressTypeKeys.forEach((key) => {
|
|
545
|
+
changeAddresses = Object.assign(Object.assign({}, changeAddresses), currentChangeAddresses[key]);
|
|
546
|
+
});
|
|
547
|
+
const changeAddressesArr = Object.values(changeAddresses).map(({ address }) => address);
|
|
548
|
+
const changeAddressIndexContent = currentWallet.changeAddressIndex[addressType];
|
|
549
|
+
const changeAddress = changeAddressIndexContent.address;
|
|
550
|
+
if (!changeAddress) {
|
|
551
|
+
return (0, utils_1.err)('Unable to successfully generate a change address.');
|
|
552
|
+
}
|
|
553
|
+
const fee = this.getTotalFee({
|
|
554
|
+
satsPerByte,
|
|
555
|
+
message: ''
|
|
556
|
+
});
|
|
557
|
+
const lightningInvoice = (_a = currentWallet.transaction) === null || _a === void 0 ? void 0 : _a.lightningInvoice;
|
|
558
|
+
let outputs = currentWallet.transaction.outputs || [];
|
|
559
|
+
if (!lightningInvoice) {
|
|
560
|
+
outputs = outputs.filter((output) => {
|
|
561
|
+
if (output.address && !changeAddressesArr.includes(output.address)) {
|
|
562
|
+
return output;
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
}
|
|
566
|
+
const payload = {
|
|
567
|
+
inputs,
|
|
568
|
+
changeAddress,
|
|
569
|
+
fee,
|
|
570
|
+
outputs,
|
|
571
|
+
rbf,
|
|
572
|
+
satsPerByte
|
|
573
|
+
};
|
|
574
|
+
this._data = Object.assign(Object.assign({}, this._data), payload);
|
|
575
|
+
await this._wallet.saveWalletData('transaction', this._data);
|
|
576
|
+
return (0, utils_1.ok)(payload);
|
|
577
|
+
}
|
|
578
|
+
catch (e) {
|
|
579
|
+
return (0, utils_1.err)(e);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
async resetSendTransaction() {
|
|
583
|
+
this._data = (0, shapes_1.getDefaultSendTransaction)();
|
|
584
|
+
await this._wallet.saveWalletData('transaction', this._data);
|
|
585
|
+
return (0, utils_1.ok)('Transaction reset.');
|
|
586
|
+
}
|
|
587
|
+
removeBlackListedUtxos(utxos) {
|
|
588
|
+
if (!utxos) {
|
|
589
|
+
utxos = this._wallet.data.utxos;
|
|
590
|
+
}
|
|
591
|
+
const blacklistedUtxos = this._wallet.data.blacklistedUtxos;
|
|
592
|
+
return utxos.filter((utxo) => {
|
|
593
|
+
return !blacklistedUtxos.find((blacklistedUtxo) => utxo.address === blacklistedUtxo.address &&
|
|
594
|
+
utxo.height === blacklistedUtxo.height &&
|
|
595
|
+
utxo.tx_hash === blacklistedUtxo.tx_hash &&
|
|
596
|
+
utxo.tx_pos === blacklistedUtxo.tx_pos);
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
updateFee({ satsPerByte, selectedFeeId = types_1.EFeeId.custom, index = 0, transaction = this.data }) {
|
|
600
|
+
var _a, _b;
|
|
601
|
+
const inputTotal = this.getTransactionInputValue({
|
|
602
|
+
inputs: transaction.inputs
|
|
603
|
+
});
|
|
604
|
+
const { max, message, outputs } = transaction;
|
|
605
|
+
let address = '';
|
|
606
|
+
if (outputs.length > index) {
|
|
607
|
+
address = (_b = (_a = outputs[index]) === null || _a === void 0 ? void 0 : _a.address) !== null && _b !== void 0 ? _b : '';
|
|
608
|
+
}
|
|
609
|
+
const newFee = this.getTotalFee({ satsPerByte, message });
|
|
610
|
+
if (newFee >= inputTotal / 2) {
|
|
611
|
+
return (0, utils_1.err)('Unable to increase the fee any further. Otherwise, it will exceed half the current balance.');
|
|
612
|
+
}
|
|
613
|
+
const totalTransactionValue = this.getTransactionOutputValue({
|
|
614
|
+
outputs
|
|
615
|
+
});
|
|
616
|
+
const newTotalAmount = totalTransactionValue + newFee;
|
|
617
|
+
const _transaction = {
|
|
618
|
+
satsPerByte,
|
|
619
|
+
fee: newFee,
|
|
620
|
+
selectedFeeId
|
|
621
|
+
};
|
|
622
|
+
if (max) {
|
|
623
|
+
_transaction.outputs = [{ address, value: inputTotal - newFee, index }];
|
|
624
|
+
}
|
|
625
|
+
if (max || newTotalAmount <= inputTotal) {
|
|
626
|
+
this.updateSendTransaction({
|
|
627
|
+
transaction: _transaction
|
|
628
|
+
});
|
|
629
|
+
return (0, utils_1.ok)({ fee: newFee });
|
|
630
|
+
}
|
|
631
|
+
return (0, utils_1.err)('New total amount exceeds the available balance. Unable to update the transaction fee.');
|
|
632
|
+
}
|
|
633
|
+
getMaxSendAmount({ satsPerByte, selectedFeeId = types_1.EFeeId.none, transaction }) {
|
|
634
|
+
var _a;
|
|
635
|
+
try {
|
|
636
|
+
if (!transaction) {
|
|
637
|
+
transaction = this.data;
|
|
638
|
+
}
|
|
639
|
+
const currentWallet = this._wallet.data;
|
|
640
|
+
const onchainBalance = currentWallet.balance;
|
|
641
|
+
const inputValue = this.getTransactionInputValue({
|
|
642
|
+
inputs: transaction.inputs
|
|
643
|
+
});
|
|
644
|
+
const amount = onchainBalance > inputValue ? onchainBalance : inputValue;
|
|
645
|
+
let utxos = [];
|
|
646
|
+
if (transaction.inputs.length > (currentWallet === null || currentWallet === void 0 ? void 0 : currentWallet.utxos.length)) {
|
|
647
|
+
utxos = transaction.inputs;
|
|
648
|
+
}
|
|
649
|
+
else {
|
|
650
|
+
utxos = (_a = currentWallet === null || currentWallet === void 0 ? void 0 : currentWallet.utxos) !== null && _a !== void 0 ? _a : [];
|
|
651
|
+
}
|
|
652
|
+
const fee = this.getTotalFee({
|
|
653
|
+
satsPerByte,
|
|
654
|
+
message: transaction.message,
|
|
655
|
+
transaction: Object.assign(Object.assign({}, transaction), { max: true, inputs: utxos, selectedFeeId,
|
|
656
|
+
satsPerByte })
|
|
657
|
+
});
|
|
658
|
+
const maxAmount = {
|
|
659
|
+
amount: amount - fee,
|
|
660
|
+
fee
|
|
661
|
+
};
|
|
662
|
+
if (amount <= fee) {
|
|
663
|
+
return (0, utils_1.err)('Balance is too low to spend.');
|
|
664
|
+
}
|
|
665
|
+
return (0, utils_1.ok)(maxAmount);
|
|
666
|
+
}
|
|
667
|
+
catch (e) {
|
|
668
|
+
return (0, utils_1.err)(e);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
async setupCpfp({ txid, satsPerByte }) {
|
|
672
|
+
var _a, _b;
|
|
673
|
+
const response = await this.setupTransaction({
|
|
674
|
+
inputTxHashes: txid ? [txid] : undefined,
|
|
675
|
+
rbf: true
|
|
676
|
+
});
|
|
677
|
+
const receiveAddress = await this._wallet.getReceiveAddress({});
|
|
678
|
+
if (receiveAddress.isErr()) {
|
|
679
|
+
return (0, utils_1.err)(receiveAddress.error.message);
|
|
680
|
+
}
|
|
681
|
+
this._data = Object.assign(Object.assign(Object.assign({}, this._data), response), { satsPerByte: (_b = satsPerByte !== null && satsPerByte !== void 0 ? satsPerByte : (_a = this.data) === null || _a === void 0 ? void 0 : _a.satsPerByte) !== null && _b !== void 0 ? _b : 1, boostType: types_1.EBoostType.cpfp });
|
|
682
|
+
const sendMaxResponse = await this.sendMax({
|
|
683
|
+
address: receiveAddress.value
|
|
684
|
+
});
|
|
685
|
+
if (sendMaxResponse.isErr()) {
|
|
686
|
+
return (0, utils_1.err)(sendMaxResponse.error.message);
|
|
687
|
+
}
|
|
688
|
+
return (0, utils_1.ok)(this.data);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
exports.Transaction = Transaction;
|
|
692
|
+
//# sourceMappingURL=index.js.map
|