Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dashtx.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ var DashTx = ("object" === typeof module && exports) || {};
* or the largest available coins until that total is met.
*/
//@ts-ignore - TODO update typedefs
Tx.createLegacyTx = async function (coins, outputs, changeOutput) {
Tx.createLegacyTx = function (coins, outputs, changeOutput) {
// TODO bump to 4 for DIP: enforce tx hygiene
let version = CURRENT_VERSION;

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dashtx",
"version": "0.19.0",
"version": "0.19.1",
"description": "Create DASH Transactions with Vanilla JS (0 deps, cross-platform)",
"main": "dashtx.js",
"module": "dashtx.mjs",
Expand Down
113 changes: 46 additions & 67 deletions tests/legacy-tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,26 @@ let outputs = [

let changeOutput = {};

Zora.test("too few sats", async function (t) {
Zora.test("too few sats", function (t) {
let inputs = [
{
satoshis: 20000 + 190,
},
];
await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
.then(function () {
t.ok(false, "should throw when there aren't enough sats");
})
.catch(function (e) {
let msg = e.message;
let isAboutMemo = /\bcannot pay for\b/.test(e.message);
if (isAboutMemo) {
msg = "throws when there are too few sats";
}
t.ok(isAboutMemo, msg);
});
try {
DashTx.legacyCreateTx(inputs, outputs, changeOutput);
t.ok(false, "should throw when there aren't enough sats");
} catch (e) {
let msg = e.message;
let isAboutMemo = /\bcannot pay for\b/.test(e.message);
if (isAboutMemo) {
msg = "throws when there are too few sats";
}
t.ok(isAboutMemo, msg);
}
});

Zora.test("exactly enough sats", async function (t) {
Zora.test("exactly enough sats", function (t) {
let inputs = [
{
satoshis: 20000 + 190,
Expand All @@ -43,52 +42,37 @@ Zora.test("exactly enough sats", async function (t) {
},
];

await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
.then(function () {
t.ok(true, "sats match exactly");
})
.catch(function (e) {
t.ok(false, e.message);
});
DashTx.legacyCreateTx(inputs, outputs, changeOutput);
t.ok(true, "sats match exactly");
});

Zora.test("donates dust", async function (t) {
Zora.test("donates dust", function (t) {
let satoshis = 20000 + 193 + DashTx.LEGACY_DUST + DashTx.OUTPUT_SIZE + -1;
let inputs = [{ satoshis }];

await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
.then(function (txInfo) {
if (txInfo.length > 1) {
throw new Error("created return change for dust");
}
t.ok(true, "has no return change");
})
.catch(function (e) {
t.ok(false, e.message);
});
let txInfo = DashTx.legacyCreateTx(inputs, outputs, changeOutput);
if (txInfo.length > 1) {
throw new Error("created return change for dust");
}
t.ok(true, "has no return change");
});

Zora.test("returns change", async function (t) {
Zora.test("returns change", function (t) {
let satoshis = 20000 + 193 + DashTx.LEGACY_DUST + DashTx.OUTPUT_SIZE;
let inputs = [{ satoshis }];

await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
.then(function (txInfo) {
let hasChange = txInfo.changeIndex >= 0;
if (!hasChange) {
throw new Error("did not create return change");
}

let change = txInfo.outputs[txInfo.changeIndex];
if (!change) {
throw new Error("did not add change to outputs");
}

t.ok(true, "returned change >= dust");
})
.catch(function (e) {
t.ok(false, e.message);
});
let txInfo = DashTx.legacyCreateTx(inputs, outputs, changeOutput);
let hasChange = txInfo.changeIndex >= 0;
if (!hasChange) {
throw new Error("did not create return change");
}

let change = txInfo.outputs[txInfo.changeIndex];
if (!change) {
throw new Error("did not add change to outputs");
}

t.ok(true, "returned change >= dust");
});

Zora.test("coins selection is better than random", async function (t) {
Expand All @@ -100,21 +84,16 @@ Zora.test("coins selection is better than random", async function (t) {
{ satoshis: 500000 },
];

await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
.then(function (txInfo) {
let tooManyInputs = txInfo.inputs.length >= 0;
if (!tooManyInputs) {
throw new Error("selected more inputs than necessary");
}

let isOptimalInput = txInfo.inputs[0].satoshis === exact;
if (!isOptimalInput) {
throw new Error("did not select clearly optimal input");
}

t.ok(true, "selected closest input");
})
.catch(function (e) {
t.ok(false, e.message);
});
let txInfo = await DashTx.legacyCreateTx(inputs, outputs, changeOutput);
let tooManyInputs = txInfo.inputs.length >= 0;
if (!tooManyInputs) {
throw new Error("selected more inputs than necessary");
}

let isOptimalInput = txInfo.inputs[0].satoshis === exact;
if (!isOptimalInput) {
throw new Error("did not select clearly optimal input");
}

t.ok(true, "selected closest input");
});
11 changes: 3 additions & 8 deletions tests/lex-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,7 @@ Zora.test("legacyCreateTx uses lex sort functions", async function (t) {

let changeOutput = {};

await DashTx.legacyCreateTx(inputs, outputs, changeOutput)
.then(function (txInfo) {
t.deepEqual(sortedIn, txInfo.inputs, "inputs should be sorted");
t.deepEqual(sortedOut, txInfo.outputs, "outputs should be sorted");
})
.catch(function (e) {
t.ok(false, e.message);
});
let txInfo = DashTx.legacyCreateTx(inputs, outputs, changeOutput);
t.deepEqual(sortedIn, txInfo.inputs, "inputs should be sorted");
t.deepEqual(sortedOut, txInfo.outputs, "outputs should be sorted");
});
Loading