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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.0.0

* Bugfix: Taproot sighash now uses actual transaction values instead of hardcoded locktime=0 and version=2 (#14)
* Migrate code from `base` to `core`
* *Breaking:* Remove `toBytes` function in `bitcoin/TxOutput.mo` (use class method instead)
* *Breaking:* Add length assertions inside `Bech32.encode()`
Expand Down
6 changes: 4 additions & 2 deletions src/bitcoin/Transaction.mo
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,12 @@ module {

let sighash_type : [Nat8] = [0x00];
let nVersion_buffer = VarArray.repeat<Nat8>(0, 4);
Common.writeLE32(nVersion_buffer, 0, 2);
Common.writeLE32(nVersion_buffer, 0, version);
let nVersion = nVersion_buffer.toArray();

let nLockTime : [Nat8] = VarArray.repeat<Nat8>(0, 4).toArray();
let nLockTime_buffer = VarArray.repeat<Nat8>(0, 4);
Common.writeLE32(nLockTime_buffer, 0, locktime);
let nLockTime = nLockTime_buffer.toArray();
let sha_prevouts : [Nat8] = Sha256.fromArray(#sha256, prevouts.flatten()).toArray();

let amounts_bytes = amounts.map<Nat64, [Nat8]>(
Expand Down
51 changes: 51 additions & 0 deletions test/bitcoin/p2trKeyPathSigHash.test.mo
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import Blob "mo:core/Blob";
import Nat "mo:core/Nat";
import VarArray "mo:core/VarArray";

import { expect; test } "mo:test";

import Transaction "../../src/bitcoin/Transaction";
import Witness "../../src/bitcoin/Witness";
import TestVectors "p2trTestVectors";

for (testCase in TestVectors.testCases().vals()) {
Expand All @@ -20,3 +23,51 @@ for (testCase in TestVectors.testCases().vals()) {
},
);
};

test(
"non-zero locktime changes sighash",
func() {
let testCase = TestVectors.testCases()[0];
let txLocktime0 = testCase.transaction();
let txLocktime42 = Transaction.Transaction(
TestVectors.version,
testCase.inputs(),
testCase.outputs(),
VarArray.repeat(Witness.EMPTY_WITNESS, testCase.numInputs),
42,
);

let hash0 = txLocktime0.createTaprootKeySpendSignatureHash(
testCase.amounts(), testCase.ownScript(), 0,
);
let hash42 = txLocktime42.createTaprootKeySpendSignatureHash(
testCase.amounts(), testCase.ownScript(), 0,
);

expect.blob(Blob.fromArray(hash0)).notEqual(Blob.fromArray(hash42));
},
);

test(
"different version changes sighash",
func() {
let testCase = TestVectors.testCases()[0];
let txVersion2 = testCase.transaction();
let txVersion1 = Transaction.Transaction(
1,
testCase.inputs(),
testCase.outputs(),
VarArray.repeat(Witness.EMPTY_WITNESS, testCase.numInputs),
0,
);

let hash2 = txVersion2.createTaprootKeySpendSignatureHash(
testCase.amounts(), testCase.ownScript(), 0,
);
let hash1 = txVersion1.createTaprootKeySpendSignatureHash(
testCase.amounts(), testCase.ownScript(), 0,
);

expect.blob(Blob.fromArray(hash2)).notEqual(Blob.fromArray(hash1));
},
);
29 changes: 29 additions & 0 deletions test/bitcoin/p2trScriptPathSigHash.test.mo
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import Blob "mo:core/Blob";
import Nat "mo:core/Nat";
import VarArray "mo:core/VarArray";

import { expect; test } "mo:test";

import P2tr "../../src/bitcoin/P2tr";
import Transaction "../../src/bitcoin/Transaction";
import Witness "../../src/bitcoin/Witness";
import TestVectors "p2trTestVectors";

for (testCase in TestVectors.testCases().vals()) {
Expand All @@ -20,3 +24,28 @@ for (testCase in TestVectors.testCases().vals()) {
},
);
};

test(
"non-zero locktime changes script spend sighash",
func() {
let testCase = TestVectors.testCases()[0];
let txLocktime0 = testCase.transaction();
let txLocktime42 = Transaction.Transaction(
TestVectors.version,
testCase.inputs(),
testCase.outputs(),
VarArray.repeat(Witness.EMPTY_WITNESS, testCase.numInputs),
42,
);

let leafHash = P2tr.leafHash(testCase.leafScript());
let hash0 = txLocktime0.createTaprootScriptSpendSignatureHash(
testCase.amounts(), testCase.ownScript(), 0, leafHash,
);
let hash42 = txLocktime42.createTaprootScriptSpendSignatureHash(
testCase.amounts(), testCase.ownScript(), 0, leafHash,
);

expect.blob(Blob.fromArray(hash0)).notEqual(Blob.fromArray(hash42));
},
);
Loading