Skip to content

Commit

Permalink
Ensure Taproot signatures use SIGHASH_DEFAULT by default
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewLM committed Feb 3, 2025
1 parent f4eb080 commit 9999f6c
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 45 deletions.
4 changes: 2 additions & 2 deletions coinlib/lib/src/tx/inputs/taproot_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class TaprootInput extends WitnessInput {
required int inputN,
required ECPrivateKey key,
required List<Output> prevOuts,
SigHashType hashType = const SigHashType.all(),
SigHashType hashType = const SigHashType.schnorrDefault(),
}) => throw CannotSignInput("Unimplemented sign() for {this.runtimeType}");

/// Creates a signature for the input. Used by subclasses to implement
Expand All @@ -37,7 +37,7 @@ abstract class TaprootInput extends WitnessInput {
required int inputN,
required ECPrivateKey key,
required List<Output> prevOuts,
SigHashType hashType = const SigHashType.all(),
SigHashType hashType = const SigHashType.schnorrDefault(),
Uint8List? leafHash,
int codeSeperatorPos = 0xFFFFFFFF,
}) => SchnorrInputSignature(
Expand Down
2 changes: 1 addition & 1 deletion coinlib/lib/src/tx/inputs/taproot_key_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class TaprootKeyInput extends TaprootInput {
required int inputN,
required ECPrivateKey key,
required List<Output> prevOuts,
SigHashType hashType = const SigHashType.all(),
SigHashType hashType = const SigHashType.schnorrDefault(),
}) {

if (inputN >= prevOuts.length) {
Expand Down
2 changes: 1 addition & 1 deletion coinlib/lib/src/tx/inputs/taproot_script_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class TaprootScriptInput extends TaprootInput {
required int inputN,
required ECPrivateKey key,
required List<Output> prevOuts,
SigHashType hashType = const SigHashType.all(),
SigHashType hashType = const SigHashType.schnorrDefault(),
int codeSeperatorPos = 0xFFFFFFFF,
}) => createInputSignature(
tx: tx,
Expand Down
2 changes: 1 addition & 1 deletion coinlib/lib/src/tx/sighash/taproot_signature_hasher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class TaprootSignatureHasher with Writable implements SignatureHasher {
required this.tx,
required this.inputN,
required this.prevOuts,
required this.hashType,
this.hashType = const SigHashType.schnorrDefault(),
this.leafHash,
this.codeSeperatorPos = 0xFFFFFFFF,
}) : txHashes = TransactionSignatureHashes(tx),
Expand Down
2 changes: 1 addition & 1 deletion coinlib/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: coinlib
description:
A straight-forward, modular library for Peercoin and other Satoshi-based UTXO
blockchains
version: 3.0-dev
version: 3.0.0-dev
repository: https://github.com/peercoin/coinlib

environment:
Expand Down
23 changes: 22 additions & 1 deletion coinlib/test/tx/inputs/taproot_key_input_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:typed_data';
import 'package:coinlib/coinlib.dart';
import 'package:test/test.dart';
import '../../vectors/keys.dart';
import '../../vectors/signatures.dart';
import '../../vectors/inputs.dart';
import '../../vectors/tx.dart';

void main() {

Expand Down Expand Up @@ -97,12 +99,31 @@ void main() {

});

test("filterSignatures", () {
test(".filterSignatures()", () {
final input = TaprootKeyInput(prevOut: prevOut, insig: insig);
expect(input.filterSignatures((insig) => false).insig, isNull);
expect(input.filterSignatures((insig) => true).insig, isNotNull);
});

test(".sign() should sign as SIGHASH_DEFAULT by default", () {
final input = TaprootKeyInput(prevOut: prevOut);
final signedInput = input.sign(
tx: Transaction(
inputs: [input],
outputs: [exampleOutput],
),
inputN: 0,
key: keyPairVectors[0].privateObj,
prevOuts: [
Output.fromProgram(
BigInt.from(10000),
P2TR.fromTweakedKey(keyPairVectors[0].publicObj),
),
],
);
expect(signedInput.insig!.hashType.schnorrDefault, true);
});

});

}
33 changes: 19 additions & 14 deletions coinlib/test/tx/transaction_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,9 @@ void main() {

test("sign script-path P2TR input with NUMS key", () {
// Sent on testnet:
// 7353bd0fd3c2f572b45f144b1c8ad17b555b52eee6493b27d41b168783bec0f2
// d035cbb954abade234a186870d8ff2cdf5f663930f4954e13bfcd3e9fee6cbd0
// Includes 12ppc input via:
// 980d55a017d166d4b26e45c81e958f2c751bc0abb8e3116bd3354158be44c53c
// 63ea0ecff27d9bffd00b09b92b682900e6093a2729f9f0a36746be32dbdeb074

TapLeaf checkSigLeafForVector(KeyTestVector vec) => TapLeaf(
Script([
Expand Down Expand Up @@ -538,7 +538,7 @@ void main() {
inputs: [
TaprootScriptInput.fromTaprootLeaf(
prevOut: OutPoint.fromHex(
"980d55a017d166d4b26e45c81e958f2c751bc0abb8e3116bd3354158be44c53c",
"63ea0ecff27d9bffd00b09b92b682900e6093a2729f9f0a36746be32dbdeb074",
1,
),
taproot: taproot,
Expand All @@ -560,21 +560,26 @@ void main() {
P2TR.fromTaproot(taproot),
);

final solvedTx = tx.replaceInput(
inputToSign.updateStack([
inputToSign.createScriptSignature(
tx: tx,
inputN: 0,
key: keyPairVectors[2].privateObj,
prevOuts: [prevOut],
).bytes,
]),
0,
final solvedInput = inputToSign.updateStack([
inputToSign.createScriptSignature(
tx: tx,
inputN: 0,
key: keyPairVectors[2].privateObj,
prevOuts: [prevOut],
).bytes,
]);
final solvedTx = tx.replaceInput(solvedInput, 0);

// Should've created a default schnorr siganture
expect(
SchnorrInputSignature.fromBytes(solvedInput.witness.first)
.hashType.schnorrDefault,
true,
);

expect(
solvedTx.toHex(),
"030000000001013cc544be584135d36b11e3b8abc01b752c8f951ec8456eb2d466d117a0550d980100000000ffffffff01a0860100000000001976a914c42e7ef92fdb603af844d064faad95db9bcdfd3d88ac034194435688998751de388e3a240d488f68f86d70a631a90355472673ccfa14e323a147eedea0b4da14a73195abe22be29fcafeb60cf3bb9de94891969f2c546088012220b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340ac61c1b33ff3fab0fd16daef5f4916bfbd83244bf3b9f446eb0f1b7b5b1f97a9e99065763e9da064b9dc0471fb0f3c8fa2c84b4b84d2ca992497c12d2274386795aa8ef91bcc8ea862a20c20ecb36adc4a8c29ca24475f9685d07e76e19379328e847e00000000",
"0300000000010174b0dedb32be4667a3f0f929273a09e60029682bb9090bd0ff9b7df2cf0eea630100000000ffffffff01a0860100000000001976a914c42e7ef92fdb603af844d064faad95db9bcdfd3d88ac0340e41ed624484087c25a210f34ade142ad706dded5fbca1787092531bb47a4251b8187e789ee1f1d41f9f22431eb43c979f009afe00e25aaaedf68a5db606c08b32220b80011a883a0fd621ad46dfc405df1e74bf075cbaf700fd4aebef6e96f848340ac61c1b33ff3fab0fd16daef5f4916bfbd83244bf3b9f446eb0f1b7b5b1f97a9e99065763e9da064b9dc0471fb0f3c8fa2c84b4b84d2ca992497c12d2274386795aa8ef91bcc8ea862a20c20ecb36adc4a8c29ca24475f9685d07e76e19379328e847e00000000",
);

});
Expand Down
48 changes: 24 additions & 24 deletions coinlib_flutter/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,26 @@ packages:
dependency: transitive
description:
name: coinlib
sha256: "7de0ddee701ebc97d68e6e0d177beb9bc8afd4c8f857f5b24fd771bb39047f6c"
sha256: cd0b36d2230d602e00ba24cfe06b70ef67deac9e03a4044fb4ddfbcaa4d992e3
url: "https://pub.dev"
source: hosted
version: "2.1.0"
version: "2.2.0"
coinlib_flutter:
dependency: "direct main"
description:
name: coinlib_flutter
sha256: d67c160392ab283cc76ee81bbf7731f3bd30d3db8a6b652063cdd05ae83857dc
sha256: e144c726dd117653fa065778deee5fea6686c3448749bd6c695be8024dd120df
url: "https://pub.dev"
source: hosted
version: "2.1.0"
version: "2.2.0"
collection:
dependency: transitive
description:
name: collection
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
sha256: a1ace0a119f20aabc852d165077c036cd864315bd99b7eaa10a60100341941bf
url: "https://pub.dev"
source: hosted
version: "1.18.0"
version: "1.19.0"
convert:
dependency: transitive
description:
Expand Down Expand Up @@ -143,18 +143,18 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
sha256: "7bb2830ebd849694d1ec25bf1f44582d6ac531a57a365a803a6034ff751d2d06"
url: "https://pub.dev"
source: hosted
version: "10.0.4"
version: "10.0.7"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
sha256: "9491a714cca3667b60b5c420da8217e6de0d1ba7a5ec322fab01758f6998f379"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "3.0.8"
leak_tracker_testing:
dependency: transitive
description:
Expand Down Expand Up @@ -183,18 +183,18 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
url: "https://pub.dev"
source: hosted
version: "0.8.0"
version: "0.11.1"
meta:
dependency: transitive
description:
name: meta
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
url: "https://pub.dev"
source: hosted
version: "1.12.0"
version: "1.15.0"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -223,7 +223,7 @@ packages:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
version: "0.0.0"
source_span:
dependency: transitive
description:
Expand All @@ -236,10 +236,10 @@ packages:
dependency: transitive
description:
name: stack_trace
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
sha256: "9f47fd3630d76be3ab26f0ee06d213679aa425996925ff3feffdec504931c377"
url: "https://pub.dev"
source: hosted
version: "1.11.1"
version: "1.12.0"
stream_channel:
dependency: transitive
description:
Expand All @@ -252,10 +252,10 @@ packages:
dependency: transitive
description:
name: string_scanner
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
sha256: "688af5ed3402a4bde5b3a6c15fd768dbf2621a614950b17f04626c431ab3c4c3"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
version: "1.3.0"
term_glyph:
dependency: transitive
description:
Expand All @@ -268,10 +268,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
sha256: "664d3a9a64782fcdeb83ce9c6b39e78fd2971d4e37827b9b06c3aa1edc5e760c"
url: "https://pub.dev"
source: hosted
version: "0.7.0"
version: "0.7.3"
typed_data:
dependency: transitive
description:
Expand All @@ -292,10 +292,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
sha256: f6be3ed8bd01289b34d679c2b62226f63c0e69f9fd2e50a6b3c1c729a961041b
url: "https://pub.dev"
source: hosted
version: "14.2.1"
version: "14.3.0"
wasm_interop:
dependency: transitive
description:
Expand All @@ -305,5 +305,5 @@ packages:
source: hosted
version: "2.0.1"
sdks:
dart: ">=3.3.0 <4.0.0"
dart: ">=3.4.0 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"

0 comments on commit 9999f6c

Please sign in to comment.