|
9 | 9 | * @prop {Uint32} MAX_INPUT_SIZE - 149 each (with padding)
|
10 | 10 | * @prop {Uint32} OUTPUT_SIZE - 34 each
|
11 | 11 | * @prop {TxAppraise} appraise
|
| 12 | + * @prop {TxAppraiseCounts} _appraiseCounts |
| 13 | + * @prop {TxAppraiseMemos} _appraiseMemos |
12 | 14 | * @prop {TxToDash} toDash
|
13 | 15 | * @prop {TxToSats} toSats
|
14 | 16 | * @prop {TxCreate} create
|
@@ -133,34 +135,56 @@ var DashTx = ("object" === typeof module && exports) || {};
|
133 | 135 | 25; // lockscript
|
134 | 136 |
|
135 | 137 | Tx.appraise = function (txInfo) {
|
| 138 | + let extraSize = Tx._appraiseMemos(txInfo.outputs); |
| 139 | + let fees = Tx._appraiseCounts( |
| 140 | + txInfo.inputs.length, |
| 141 | + txInfo.outputs.length, |
| 142 | + extraSize, |
| 143 | + ); |
| 144 | + |
| 145 | + return fees; |
| 146 | + }; |
| 147 | + |
| 148 | + Tx._appraiseCounts = function (numInputs, numOutputs, extraSize) { |
136 | 149 | let min = Tx._HEADER_ONLY_SIZE;
|
137 | 150 |
|
138 |
| - min += Tx.utils.toVarIntSize(txInfo.inputs.length); |
139 |
| - min += Tx.MIN_INPUT_SIZE * txInfo.inputs.length; |
| 151 | + min += Tx.utils.toVarIntSize(numInputs); |
| 152 | + min += Tx.MIN_INPUT_SIZE * numInputs; |
| 153 | + |
| 154 | + min += Tx.utils.toVarIntSize(numOutputs); |
| 155 | + if (extraSize) { |
| 156 | + min += extraSize; // memos, etc |
| 157 | + } |
| 158 | + |
| 159 | + let maxPadding = Tx.MAX_INPUT_PAD * numInputs; |
| 160 | + let max = min + maxPadding; |
| 161 | + |
| 162 | + let spread = max - min; |
| 163 | + let halfSpread = Math.ceil(spread / 2); |
| 164 | + let mid = min + halfSpread; |
| 165 | + |
| 166 | + let fees = { min, mid, max }; |
| 167 | + return fees; |
| 168 | + }; |
| 169 | + |
| 170 | + Tx._appraiseMemos = function (outputs) { |
| 171 | + let size = 0; |
140 | 172 |
|
141 |
| - min += Tx.utils.toVarIntSize(txInfo.outputs.length); |
142 |
| - for (let output of txInfo.outputs) { |
| 173 | + for (let output of outputs) { |
143 | 174 | if (output.memo) {
|
144 | 175 | let memoSize = output.memo.length / 2;
|
145 | 176 | if (memoSize > MAX_U8) {
|
146 |
| - min += 2; |
| 177 | + size += 2; |
147 | 178 | } else if (memoSize >= OP_PUSHDATA1_INT) {
|
148 |
| - min += 1; |
| 179 | + size += 1; |
149 | 180 | }
|
150 |
| - min += OP_RETURN_HEADER_SIZE + memoSize; |
| 181 | + size += OP_RETURN_HEADER_SIZE + memoSize; |
151 | 182 | continue;
|
152 | 183 | }
|
153 |
| - min += Tx.OUTPUT_SIZE; |
| 184 | + size += Tx.OUTPUT_SIZE; |
154 | 185 | }
|
155 | 186 |
|
156 |
| - let maxPadding = Tx.MAX_INPUT_PAD * txInfo.inputs.length; |
157 |
| - let max = min + maxPadding; |
158 |
| - |
159 |
| - let spread = max - min; |
160 |
| - let halfSpread = Math.ceil(spread / 2); |
161 |
| - let mid = min + halfSpread; |
162 |
| - |
163 |
| - return { min: min, mid: mid, max: max }; |
| 187 | + return size; |
164 | 188 | };
|
165 | 189 |
|
166 | 190 | Tx.toDash = function (satoshis) {
|
@@ -1392,6 +1416,20 @@ if ("object" === typeof module) {
|
1392 | 1416 | * @returns {TxFees}
|
1393 | 1417 | */
|
1394 | 1418 |
|
| 1419 | +/** |
| 1420 | + * @callback TxAppraiseCounts |
| 1421 | + * @param {Uint32} numInputs |
| 1422 | + * @param {Uint32} numOutputs |
| 1423 | + * @param {Uint32} [extraSize] - for memos |
| 1424 | + * @returns {TxFees} |
| 1425 | + */ |
| 1426 | + |
| 1427 | +/** |
| 1428 | + * @callback TxAppraiseMemos |
| 1429 | + * @param {Array<TxOutput>} outputs |
| 1430 | + * @returns {Uint32} |
| 1431 | + */ |
| 1432 | + |
1395 | 1433 | /**
|
1396 | 1434 | * @callback TxCreateHashable
|
1397 | 1435 | * @param {TxInfo} txInfo
|
|
0 commit comments