-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move sign logic to sign() methods in inputs
- Loading branch information
Showing
22 changed files
with
281 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
coinlib/lib/src/tx/input.dart → coinlib/lib/src/tx/inputs/input.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
coinlib/lib/src/tx/input_signature.dart → ...ib/lib/src/tx/inputs/input_signature.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import 'package:coinlib/src/crypto/ec_private_key.dart'; | ||
import 'package:coinlib/src/crypto/ecdsa_signature.dart'; | ||
import 'package:coinlib/src/scripts/script.dart'; | ||
import 'package:coinlib/src/tx/sighash/legacy_signature_hasher.dart'; | ||
import 'package:coinlib/src/tx/sighash/sighash_type.dart'; | ||
import 'package:coinlib/src/tx/transaction.dart'; | ||
import 'input.dart'; | ||
import 'input_signature.dart'; | ||
import 'raw_input.dart'; | ||
|
||
/// Inputs that are not witness inputs: [P2PKHInput] and [P2SHMultisigInput]. | ||
abstract class LegacyInput extends RawInput { | ||
|
||
LegacyInput({ | ||
required super.prevOut, | ||
required super.scriptSig, | ||
super.sequence = Input.sequenceFinal, | ||
}); | ||
|
||
/// Signs the input given the [tx], input number ([inputN]) and a private | ||
/// [key] using the specifified [hashType]. | ||
/// Implemented by specific subclasses. | ||
LegacyInput sign({ | ||
required Transaction tx, | ||
required int inputN, | ||
required ECPrivateKey key, | ||
hashType = const SigHashType.all(), | ||
}); | ||
|
||
/// Creates a signature for the input. Used by subclasses to implement | ||
/// signing. | ||
InputSignature createInputSignature({ | ||
required Transaction tx, | ||
required int inputN, | ||
required ECPrivateKey key, | ||
required Script scriptCode, | ||
hashType = const SigHashType.all(), | ||
}) => InputSignature( | ||
ECDSASignature.sign( | ||
key, | ||
LegacySignatureHasher( | ||
tx: tx, | ||
inputN: inputN, | ||
scriptCode: scriptCode, | ||
hashType: hashType, | ||
).hash, | ||
), | ||
hashType, | ||
); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import 'package:coinlib/src/crypto/ec_private_key.dart'; | ||
import 'package:coinlib/src/crypto/ecdsa_signature.dart'; | ||
import 'package:coinlib/src/scripts/script.dart'; | ||
import 'package:coinlib/src/tx/sighash/sighash_type.dart'; | ||
import 'package:coinlib/src/tx/sighash/witness_signature_hasher.dart'; | ||
import 'package:coinlib/src/tx/transaction.dart'; | ||
import 'input.dart'; | ||
import 'input_signature.dart'; | ||
import 'witness_input.dart'; | ||
|
||
/// Represents v0 witness program inputs | ||
abstract class LegacyWitnessInput extends WitnessInput { | ||
|
||
LegacyWitnessInput({ | ||
required super.prevOut, | ||
required super.witness, | ||
super.sequence = Input.sequenceFinal, | ||
}); | ||
|
||
/// Signs the input given the [tx], input number ([inputN]), private | ||
/// [key] and input [value] using the specifified [hashType]. Should throw | ||
/// [CannotSignInput] if the key cannot sign the input. | ||
/// Implemented by specific subclasses. | ||
LegacyWitnessInput sign({ | ||
required Transaction tx, | ||
required int inputN, | ||
required ECPrivateKey key, | ||
required BigInt value, | ||
hashType = const SigHashType.all(), | ||
}); | ||
|
||
/// Creates a signature for the input. Used by subclasses to implement | ||
/// signing. | ||
InputSignature createInputSignature({ | ||
required Transaction tx, | ||
required int inputN, | ||
required ECPrivateKey key, | ||
required Script scriptCode, | ||
required BigInt value, | ||
hashType = const SigHashType.all(), | ||
}) => InputSignature( | ||
ECDSASignature.sign( | ||
key, | ||
WitnessSignatureHasher( | ||
tx: tx, | ||
inputN: inputN, | ||
scriptCode: scriptCode, | ||
value: value, | ||
hashType: hashType, | ||
).hash, | ||
), | ||
hashType, | ||
); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'package:coinlib/src/crypto/ec_private_key.dart'; | ||
import 'package:coinlib/src/crypto/ec_public_key.dart'; | ||
import 'package:coinlib/src/scripts/programs/p2pkh.dart'; | ||
import 'package:coinlib/src/scripts/script.dart'; | ||
import 'package:coinlib/src/tx/transaction.dart'; | ||
import 'input_signature.dart'; | ||
|
||
/// A mixin for Public Key Hash input types, providing the [ECPublicKey] and | ||
/// [InputSignature] required in these inputs. | ||
abstract mixin class PKHInput { | ||
|
||
ECPublicKey get publicKey; | ||
InputSignature? get insig; | ||
PKHInput addSignature(InputSignature insig); | ||
bool get complete => insig != null; | ||
Script get scriptCode => P2PKH.fromPublicKey(publicKey).script; | ||
|
||
ECPrivateKey checkKey(ECPrivateKey key) { | ||
if (key.pubkey != publicKey) { | ||
throw CannotSignInput("Incorrect key for input"); | ||
} | ||
return key; | ||
} | ||
|
||
} |
4 changes: 2 additions & 2 deletions
4
coinlib/lib/src/tx/raw_input.dart → coinlib/lib/src/tx/inputs/raw_input.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
coinlib/lib/src/tx/witness_input.dart → coinlib/lib/src/tx/inputs/witness_input.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.