Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tests for maxWeightToSatisfy #43

Merged
merged 1 commit into from
Sep 17, 2024
Merged
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
74 changes: 64 additions & 10 deletions packages/wasm-miniscript/test/fixedScriptToDescriptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,48 @@ const scriptTypes = ["p2sh", "p2shP2wsh", "p2wsh"] as const;
const scope = ["external", "internal"] as const;
const index = [0, 1, 2];

/** Get the expected max weight to satisfy the descriptor */
function getExpectedMaxWeightToSatisfy(scriptType: utxolib.bitgo.outputScripts.ScriptType2Of3) {
switch (scriptType) {
case "p2sh":
return 256;
case "p2shP2wsh":
return 99;
case "p2wsh":
return 64;
default:
throw new Error("unexpected script type");
}
}

/** Compute the total size of the input, including overhead */
function getTotalInputSize(vSize: number) {
const sizeOpPushData1 = 1;
const sizeOpPushData2 = 2;
return (
32 /* txid */ +
4 /* vout */ +
4 /* nSequence */ +
(vSize < 255 ? sizeOpPushData1 : sizeOpPushData2) +
vSize
);
}

/** Get the full expected vSize of the input including overhead */
function getExpectedVSize(scriptType: utxolib.bitgo.outputScripts.ScriptType2Of3) {
// https://github.com/BitGo/BitGoJS/blob/master/modules/unspents/docs/input-costs.md
switch (scriptType) {
case "p2sh":
return 298;
case "p2shP2wsh":
return 140;
case "p2wsh":
return 105;
default:
throw new Error("unexpected script type");
}
}

function runTest(
scriptType: utxolib.bitgo.outputScripts.ScriptType2Of3,
index: number,
Expand All @@ -24,11 +66,16 @@ function runTest(
scriptType,
).scriptPubKey;

it("descriptor should have expected format", function () {
const descriptor = Descriptor.fromString(
let descriptor: Descriptor;

before(function () {
descriptor = Descriptor.fromString(
getDescriptorForScriptType(rootWalletKeys, scriptType, scope),
"derivable",
);
});

it("descriptor should have expected format", function () {
const [x1, x2, x3] = rootWalletKeys.triple.map((xpub) => xpub.neutered().toBase58());
if (scriptType === "p2sh" && scope === "external") {
// spot check
Expand All @@ -49,16 +96,23 @@ function runTest(
});

it("address should match descriptor", function () {
const scriptFromDescriptor = Buffer.from(
Descriptor.fromString(
getDescriptorForScriptType(rootWalletKeys, scriptType, scope),
"derivable",
)
.atDerivationIndex(index)
.scriptPubkey(),
);
const scriptFromDescriptor = Buffer.from(descriptor.atDerivationIndex(index).scriptPubkey());
assert.deepStrictEqual(scriptUtxolib.toString("hex"), scriptFromDescriptor.toString("hex"));
});

it("should have expected weights", function () {
assert.ok(Number.isInteger(descriptor.maxWeightToSatisfy()));
const vSize = Math.ceil(descriptor.maxWeightToSatisfy() / 4);
console.log(
scriptType,
"scriptLength",
descriptor.atDerivationIndex(0).encode().length,
"vSize",
vSize,
);
assert.equal(vSize, getExpectedMaxWeightToSatisfy(scriptType));
assert.equal(getTotalInputSize(vSize), getExpectedVSize(scriptType));
});
});
}

Expand Down
Loading