Skip to content

Commit

Permalink
Update to Dart 3.2 and fix linter errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewLM committed Nov 22, 2023
1 parent 2331d80 commit 409319f
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 111 deletions.
2 changes: 1 addition & 1 deletion coinlib/lib/src/crypto/hash.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Uint8List hmacSha512(Uint8List key, Uint8List msg) => Uint8List.fromList(
);

Uint8List Function(Uint8List msg) getTaggedHasher(String tag) {
final hashedTag = sha256Hash(utf8.encode(tag) as Uint8List);
final hashedTag = sha256Hash(utf8.encode(tag));
return (Uint8List msg) => sha256Hash(
Uint8List.fromList([...hashedTag, ...hashedTag, ...msg]),
);
Expand Down
32 changes: 11 additions & 21 deletions coinlib/lib/src/crypto/hd_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,11 @@ class HDPrivateKey extends HDKey {

HDPrivateKey({
required this.privateKey,
required Uint8List chaincode,
required int depth,
required int index,
required int parentFingerprint,
}) : super._(
chaincode: chaincode,
depth: depth,
index: index,
parentFingerprint: parentFingerprint,
);
required super.chaincode,
required super.depth,
required super.index,
required super.parentFingerprint,
}) : super._();

/// Creates a master key from an existing private key and chain code.
HDPrivateKey.fromKeyAndChainCode(this.privateKey, Uint8List chaincode)
Expand All @@ -261,7 +256,7 @@ class HDPrivateKey extends HDKey {
throw ArgumentError("Seed should be between 16 and 64 bytes", "seed");
}

final hash = hmacSha512(utf8.encode("Bitcoin seed") as Uint8List, seed);
final hash = hmacSha512(utf8.encode("Bitcoin seed"), seed);
return HDPrivateKey(
privateKey: ECPrivateKey(hash.sublist(0, 32)),
chaincode: hash.sublist(32),
Expand Down Expand Up @@ -308,16 +303,11 @@ class HDPublicKey extends HDKey {

HDPublicKey({
required this.publicKey,
required Uint8List chaincode,
required int depth,
required int index,
required int parentFingerprint,
}) : super._(
chaincode: chaincode,
depth: depth,
index: index,
parentFingerprint: parentFingerprint,
);
required super.chaincode,
required super.depth,
required super.index,
required super.parentFingerprint,
}) : super._();

/// Creates a HD public key from a base58 encoded representation ([b58]). May
/// throw [InvalidBase58], [InvalidBase58Checksum] or [InvalidHDKey].
Expand Down
2 changes: 1 addition & 1 deletion coinlib/lib/src/crypto/message_signature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MagicHash with Writable {
MagicHash(this.message, this.prefix);

static _writeUtf8(Writer writer, String msg)
=> writer.writeVarSlice(utf8.encode(msg) as Uint8List);
=> writer.writeVarSlice(utf8.encode(msg));

@override
void write(Writer writer) {
Expand Down
4 changes: 2 additions & 2 deletions coinlib/lib/src/crypto/nums_public_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class NUMSPublicKey extends ECPublicKey {

final Uint8List _rTweak;

NUMSPublicKey._(Uint8List rTweak, Uint8List data)
: _rTweak = Uint8List.fromList(rTweak), super(data);
NUMSPublicKey._(Uint8List rTweak, super.data)
: _rTweak = Uint8List.fromList(rTweak), super();

/// Constructs a NUMS key from a given [rTweak].
/// Throws [ArgumentError] if [rTweak] cannot produce a valid public key.
Expand Down
56 changes: 36 additions & 20 deletions coinlib/lib/src/generated/secp256k1.ffi.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2189,15 +2189,22 @@ typedef secp256k1_scratch_space = secp256k1_scratch_space_struct;
///
/// Except for test cases, this function should compute some cryptographic hash of
/// the message, the algorithm, the key and the attempt.
typedef secp256k1_nonce_function = ffi.Pointer<
ffi.NativeFunction<
ffi.Int Function(
ffi.Pointer<ffi.UnsignedChar> nonce32,
ffi.Pointer<ffi.UnsignedChar> msg32,
ffi.Pointer<ffi.UnsignedChar> key32,
ffi.Pointer<ffi.UnsignedChar> algo16,
ffi.Pointer<ffi.Void> data,
ffi.UnsignedInt attempt)>>;
typedef secp256k1_nonce_function
= ffi.Pointer<ffi.NativeFunction<secp256k1_nonce_functionFunction>>;
typedef secp256k1_nonce_functionFunction = ffi.Int Function(
ffi.Pointer<ffi.UnsignedChar> nonce32,
ffi.Pointer<ffi.UnsignedChar> msg32,
ffi.Pointer<ffi.UnsignedChar> key32,
ffi.Pointer<ffi.UnsignedChar> algo16,
ffi.Pointer<ffi.Void> data,
ffi.UnsignedInt attempt);
typedef Dartsecp256k1_nonce_functionFunction = int Function(
ffi.Pointer<ffi.UnsignedChar> nonce32,
ffi.Pointer<ffi.UnsignedChar> msg32,
ffi.Pointer<ffi.UnsignedChar> key32,
ffi.Pointer<ffi.UnsignedChar> algo16,
ffi.Pointer<ffi.Void> data,
int attempt);

/// Opaque data structured that holds a parsed ECDSA signature,
/// supporting pubkey recovery.
Expand Down Expand Up @@ -2268,17 +2275,26 @@ final class secp256k1_keypair extends ffi.Struct {
///
/// Except for test cases, this function should compute some cryptographic hash of
/// the message, the key, the pubkey, the algorithm description, and data.
typedef secp256k1_nonce_function_hardened = ffi.Pointer<
ffi.NativeFunction<
ffi.Int Function(
ffi.Pointer<ffi.UnsignedChar> nonce32,
ffi.Pointer<ffi.UnsignedChar> msg,
ffi.Size msglen,
ffi.Pointer<ffi.UnsignedChar> key32,
ffi.Pointer<ffi.UnsignedChar> xonly_pk32,
ffi.Pointer<ffi.UnsignedChar> algo,
ffi.Size algolen,
ffi.Pointer<ffi.Void> data)>>;
typedef secp256k1_nonce_function_hardened = ffi
.Pointer<ffi.NativeFunction<secp256k1_nonce_function_hardenedFunction>>;
typedef secp256k1_nonce_function_hardenedFunction = ffi.Int Function(
ffi.Pointer<ffi.UnsignedChar> nonce32,
ffi.Pointer<ffi.UnsignedChar> msg,
ffi.Size msglen,
ffi.Pointer<ffi.UnsignedChar> key32,
ffi.Pointer<ffi.UnsignedChar> xonly_pk32,
ffi.Pointer<ffi.UnsignedChar> algo,
ffi.Size algolen,
ffi.Pointer<ffi.Void> data);
typedef Dartsecp256k1_nonce_function_hardenedFunction = int Function(
ffi.Pointer<ffi.UnsignedChar> nonce32,
ffi.Pointer<ffi.UnsignedChar> msg,
int msglen,
ffi.Pointer<ffi.UnsignedChar> key32,
ffi.Pointer<ffi.UnsignedChar> xonly_pk32,
ffi.Pointer<ffi.UnsignedChar> algo,
int algolen,
ffi.Pointer<ffi.Void> data);

/// Data structure that contains additional arguments for schnorrsig_sign_custom.
///
Expand Down
2 changes: 1 addition & 1 deletion coinlib/lib/src/scripts/programs/p2tr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:coinlib/src/taproot.dart';
class P2TR extends P2Witness {

/// Construct using an output script.
P2TR.fromScript(Script script) : super.fromScript(script) {
P2TR.fromScript(super.script) : super.fromScript() {
if (data.length != 32 || version != 1) throw NoProgramMatch();
}

Expand Down
2 changes: 1 addition & 1 deletion coinlib/lib/src/scripts/programs/p2wpkh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:coinlib/src/scripts/script.dart';
/// can satisfy this script with a signature provided as witness data.
class P2WPKH extends P2Witness {

P2WPKH.fromScript(Script script) : super.fromScript(script) {
P2WPKH.fromScript(super.script) : super.fromScript() {
if (data.length != 20 || version != 0) throw NoProgramMatch();
}

Expand Down
2 changes: 1 addition & 1 deletion coinlib/lib/src/scripts/programs/p2wsh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class P2WSH extends P2Witness {

/// Construct using an output script, not to be confused with the redeem
/// script. For that use [fromRedeemScript].
P2WSH.fromScript(Script script) : super.fromScript(script) {
P2WSH.fromScript(super.script) : super.fromScript() {
if (data.length != 32 || version != 0) throw NoProgramMatch();
}

Expand Down
7 changes: 2 additions & 5 deletions coinlib/lib/src/tx/inputs/p2pkh_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:coinlib/src/crypto/ec_public_key.dart';
import 'package:coinlib/src/scripts/operations.dart';
import 'package:coinlib/src/scripts/programs/p2pkh.dart';
import 'package:coinlib/src/scripts/script.dart';
import 'package:coinlib/src/tx/outpoint.dart';
import 'package:coinlib/src/tx/sighash/sighash_type.dart';
import 'package:coinlib/src/tx/transaction.dart';
import 'input.dart';
Expand All @@ -27,17 +26,15 @@ class P2PKHInput extends LegacyInput with PKHInput {
final int? signedSize = 147;

P2PKHInput({
required OutPoint prevOut,
required super.prevOut,
required this.publicKey,
this.insig,
int sequence = Input.sequenceFinal,
super.sequence = Input.sequenceFinal,
}) : super(
prevOut: prevOut,
scriptSig: Script([
if (insig != null) ScriptPushData(insig.bytes),
ScriptPushData(publicKey.data),
]).compiled,
sequence: sequence,
);

/// Checks if the [RawInput] matches the expected format for a [P2PKHInput],
Expand Down
7 changes: 2 additions & 5 deletions coinlib/lib/src/tx/inputs/p2sh_multisig_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:coinlib/src/scripts/operations.dart';
import 'package:coinlib/src/scripts/program.dart';
import 'package:coinlib/src/scripts/programs/multisig.dart';
import 'package:coinlib/src/scripts/script.dart';
import 'package:coinlib/src/tx/outpoint.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';
Expand All @@ -25,18 +24,16 @@ class P2SHMultisigInput extends LegacyInput {
final List<ECDSAInputSignature> sigs;

P2SHMultisigInput({
required OutPoint prevOut,
required super.prevOut,
required this.program,
Iterable<ECDSAInputSignature> sigs = const [],
int sequence = Input.sequenceFinal,
super.sequence = Input.sequenceFinal,
}) : sigs = List.unmodifiable(sigs), super(
prevOut: prevOut,
scriptSig: Script([
ScriptOp.fromNumber(0),
...sigs.map((sig) => ScriptPushData(sig.bytes)),
ScriptPushData(program.script.compiled),
]).compiled,
sequence: sequence,
) {
if (sigs.length > program.threshold) {
throw ArgumentError(
Expand Down
7 changes: 2 additions & 5 deletions coinlib/lib/src/tx/inputs/p2wpkh_input.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:typed_data';
import 'package:coinlib/src/crypto/ec_private_key.dart';
import 'package:coinlib/src/crypto/ec_public_key.dart';
import 'package:coinlib/src/tx/outpoint.dart';
import 'package:coinlib/src/tx/sighash/sighash_type.dart';
import 'package:coinlib/src/tx/transaction.dart';
import 'input.dart';
Expand All @@ -26,13 +25,11 @@ class P2WPKHInput extends LegacyWitnessInput with PKHInput {
final int? signedSize = 147;

P2WPKHInput({
required OutPoint prevOut,
required super.prevOut,
required this.publicKey,
this.insig,
int sequence = Input.sequenceFinal,
super.sequence = Input.sequenceFinal,
}) : super(
prevOut: prevOut,
sequence: sequence,
witness: [
if (insig != null) insig.bytes,
publicKey.data,
Expand Down
11 changes: 3 additions & 8 deletions coinlib/lib/src/tx/inputs/taproot_key_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:coinlib/src/crypto/ec_private_key.dart';
import 'package:coinlib/src/scripts/programs/p2tr.dart';
import 'package:coinlib/src/taproot.dart';
import 'package:coinlib/src/tx/inputs/taproot_input.dart';
import 'package:coinlib/src/tx/outpoint.dart';
import 'package:coinlib/src/tx/output.dart';
import 'package:coinlib/src/tx/sighash/sighash_type.dart';
import 'package:coinlib/src/tx/transaction.dart';
Expand All @@ -21,14 +20,10 @@ class TaprootKeyInput extends TaprootInput {
final int? signedSize = 41 + 65;

TaprootKeyInput({
required OutPoint prevOut,
required super.prevOut,
this.insig,
int sequence = Input.sequenceFinal,
}) : super(
prevOut: prevOut,
sequence: sequence,
witness: [if (insig != null) insig.bytes],
);
super.sequence = Input.sequenceFinal,
}) : super(witness: [if (insig != null) insig.bytes]);

/// Checks if the [raw] input and [witness] data match the expected format for
/// a [TaprootKeyInput], with a signature. If it does it returns a
Expand Down
6 changes: 2 additions & 4 deletions coinlib/lib/src/tx/inputs/taproot_script_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@ class TaprootScriptInput extends TaprootInput {
final Script tapscript;

TaprootScriptInput({
required OutPoint prevOut,
required super.prevOut,
required Uint8List controlBlock,
required this.tapscript,
List<Uint8List>? stack,
int sequence = Input.sequenceFinal,
super.sequence = Input.sequenceFinal,
}) : super(
prevOut: prevOut,
sequence: sequence,
witness: [if (stack != null) ...stack, tapscript.compiled, controlBlock],
);

Expand Down
11 changes: 3 additions & 8 deletions coinlib/lib/src/tx/inputs/witness_input.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:typed_data';
import 'package:coinlib/src/tx/outpoint.dart';
import 'input.dart';
import 'raw_input.dart';
import 'p2wpkh_input.dart';
Expand All @@ -12,14 +11,10 @@ class WitnessInput extends RawInput {
final List<Uint8List> witness;

WitnessInput({
required OutPoint prevOut,
required super.prevOut,
required List<Uint8List> witness,
int sequence = Input.sequenceFinal,
}) : witness = List.unmodifiable(witness), super(
prevOut: prevOut,
scriptSig: Uint8List(0),
sequence: sequence,
);
super.sequence = Input.sequenceFinal,
}) : witness = List.unmodifiable(witness), super(scriptSig: Uint8List(0));

/// Matches a [raw] input with witness data to a corresponding [WitnessInput]
/// or specialised sub-class object. If this is not a witness input, null is
Expand Down
6 changes: 3 additions & 3 deletions coinlib/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ version: 2.0.0-rc.1
repository: https://github.com/peercoin/coinlib

environment:
sdk: '>=3.0.0 <4.0.0'
sdk: '>=3.2.0 <4.0.0'

dev_dependencies:
ffigen: ^9.0.1
lints: ^2.0.0
ffigen: ^10.0.0
lints: ^3.0.0
test: ^1.21.0

dependencies:
Expand Down
Loading

0 comments on commit 409319f

Please sign in to comment.