Skip to content

Commit

Permalink
ref: simplify _signToTarget(txDraft, keys)
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Mar 25, 2024
1 parent 2185113 commit 0a8aff0
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions dashwallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1917,19 +1917,17 @@
*/
wallet.legacy.finalizeAndSignTx = async function (txDraft, keys) {
/** @type {import('dashtx').TxInfoSigned} */
let txSigned = await _signToTarget(
txDraft,
txDraft.feeEstimate,
keys,
).catch(async function (e) {
//@ts-ignore
if ("E_NO_ENTROPY" !== e.code) {
throw e;
}
let txSigned = await _signToTarget(txDraft, keys).catch(
async function (e) {
//@ts-ignore
if ("E_NO_ENTROPY" !== e.code) {
throw e;
}

let _txSigned = await _signFeeWalk(txDraft, keys);
return _txSigned;
});
let _txSigned = await _signFeeWalk(txDraft, keys);
return _txSigned;
},
);

let txSummary = _summarizeLegacyTx(txSigned);
return txSummary;
Expand Down Expand Up @@ -2088,41 +2086,39 @@
}

/**
* @param {import('dashtx').TxInfo} txInfoRaw
* @param {Number} feeEstimate
* @param {TxDraft} txDraft
* @param {Array<Uint8Array>} keys
* @returns {Promise<TxInfoSigned>}
*/
async function _signToTarget(txInfoRaw, feeEstimate, keys) {
async function _signToTarget(txDraft, keys) {
let limit = 128;
let lastTx = "";
let hasEntropy = true;

/** @type {import('dashtx').TxInfoSigned} */
let txInfo;
let txSigned;

for (let n = 0; true; n += 1) {
txInfo = await dashTx.hashAndSignAll(txInfoRaw, keys);
//console.log("DEBUG txInfoRaw (entropy):");
//console.log(txInfoRaw);
txSigned = await dashTx.hashAndSignAll(txDraft, keys);

lastTx = txInfo.transaction;
let fee = txInfo.transaction.length / 2;
if (fee <= feeEstimate) {
lastTx = txSigned.transaction;
let fee = txSigned.transaction.length / 2;
if (fee <= txDraft.feeEstimate) {
break;
}

if (txInfo.transaction === lastTx) {
if (txSigned.transaction === lastTx) {
hasEntropy = false;
break;
}
if (n >= limit) {
throw new Error(
`(near-)infinite loop: fee is ${fee} trying to hit target fee of ${feeEstimate}`,
`(near-)infinite loop: fee is ${fee} trying to hit target fee of ${txDraft.feeEstimate}`,
);
}
}

return txInfo;
return txSigned;
}

/**
Expand Down

0 comments on commit 0a8aff0

Please sign in to comment.