Skip to content

Commit

Permalink
Allow equals comparison for OutPoints
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewLM committed Oct 27, 2023
1 parent 72aea61 commit 097dc3d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
18 changes: 9 additions & 9 deletions coinlib/lib/src/crypto/ec_public_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ class ECPublicKey {
);
ECPublicKey.fromXOnlyHex(String hex) : this.fromXOnly(hexToBytes(hex));

/// Tweaks the public key with a scalar multiplied by the generator point. In
/// the instance a new key cannot be created (practically impossible for
/// random 32-bit scalars), then null will be returned.
ECPublicKey? tweak(Uint8List scalar) {
checkBytes(scalar, 32, name: "Scalar");
final newKey = secp256k1.pubKeyTweak(_data, scalar, compressed);
return newKey == null ? null : ECPublicKey(newKey);
}

String get hex => bytesToHex(_data);
bool get compressed => _data.length == 33;
Uint8List get data => Uint8List.fromList(_data);
Expand Down Expand Up @@ -66,13 +75,4 @@ class ECPublicKey {
@override
int get hashCode => _data[1] | _data[2] << 8 | _data[3] << 16 | _data[4] << 24;

/// Tweaks the public key with a scalar multiplied by the generator point. In
/// the instance a new key cannot be created (practically impossible for
/// random 32-bit scalars), then null will be returned.
ECPublicKey? tweak(Uint8List scalar) {
checkBytes(scalar, 32, name: "Scalar");
final newKey = secp256k1.pubKeyTweak(_data, scalar, compressed);
return newKey == null ? null : ECPublicKey(newKey);
}

}
11 changes: 11 additions & 0 deletions coinlib/lib/src/tx/outpoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:coinlib/src/common/bytes.dart';
import 'package:coinlib/src/common/checks.dart';
import 'package:coinlib/src/common/hex.dart';
import 'package:coinlib/src/common/serial.dart';
import 'package:collection/collection.dart';

/// Reference to an [Output] by transaction hash and index
class OutPoint with Writable {
Expand Down Expand Up @@ -32,4 +33,14 @@ class OutPoint with Writable {
/// True if this out point is the type found in a coinbase
bool get coinbase => _hash.every((e) => e == 0) && n == 0xffffffff;

@override
bool operator ==(Object other)
=> (other is OutPoint)
&& ListEquality().equals(_hash, other._hash)
&& n == other.n;

@override
int get hashCode
=> _hash[1] | _hash[2] << 8 | _hash[3] << 16 | _hash[4] << 24 | n;

}
2 changes: 1 addition & 1 deletion coinlib/test/crypto/ec_public_key_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void main() {
}
});

test(".equal", () {
test("allows equality comparison", () {
for (int i = 0; i < validPubKeys.length; i++) {
expect(
ECPublicKey.fromHex(validPubKeys[i].hex),
Expand Down
18 changes: 18 additions & 0 deletions coinlib/test/tx/outpoint_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ void main() {
expect(op.hash, Uint8List(32));
});

test("allows equality comparison", () {

final hash = Uint8List(32);
final outp = OutPoint(hash, 0);
final identical = OutPoint(hash, 0);

final diff1 = OutPoint(hash, 1);

hash[0] = 1;
final diff2 = OutPoint(hash, 0);

expect(outp, identical);
expect(outp, outp);
expect(outp, isNot(equals(diff1)));
expect(outp, isNot(equals(diff2)));

});

});

}

0 comments on commit 097dc3d

Please sign in to comment.