-
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.
- Loading branch information
Showing
5 changed files
with
104 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import 'dart:typed_data'; | ||
|
||
import 'package:coinlib/src/common/hex.dart'; | ||
import 'ec_public_key.dart'; | ||
|
||
/// Represents an [ECPublicKey] that must be in a compressed format, or else a | ||
/// [InvalidPublicKey] will be thrown. | ||
class ECCompressedPublicKey extends ECPublicKey { | ||
|
||
ECCompressedPublicKey(super.data) { | ||
if (data.length != 33) throw InvalidPublicKey(); | ||
} | ||
ECCompressedPublicKey.fromHex(String hex) : this(hexToBytes(hex)); | ||
ECCompressedPublicKey.fromXOnly(super.xcoord) : super.fromXOnly(); | ||
ECCompressedPublicKey.fromXOnlyHex(super.hex) : super.fromXOnlyHex(); | ||
ECCompressedPublicKey.fromPubkey(ECPublicKey key) : this( | ||
key.compressed | ||
? key.data | ||
: Uint8List.fromList([key.yIsEven ? 2 : 3, ...key.x]), | ||
); | ||
|
||
@override | ||
ECCompressedPublicKey? tweak(Uint8List scalar) { | ||
final tweaked = super.tweak(scalar); | ||
return tweaked == null ? null : ECCompressedPublicKey.fromPubkey(tweaked); | ||
} | ||
|
||
@override | ||
ECCompressedPublicKey get xonly => ECCompressedPublicKey.fromXOnly(x); | ||
|
||
} |
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,65 @@ | ||
import 'package:coinlib/coinlib.dart'; | ||
import 'package:test/test.dart'; | ||
import '../vectors/keys.dart'; | ||
|
||
void main() { | ||
|
||
group("ECCompressedPublicKey", () { | ||
|
||
setUpAll(loadCoinlib); | ||
|
||
test("requires 33 bytes", () { | ||
|
||
for (final failing in [ | ||
// Too small | ||
pubkeyVec.substring(0, 32*2), | ||
// Too large | ||
longPubkeyVec, | ||
"${pubkeyVec}ff", | ||
]) { | ||
expect( | ||
() => ECCompressedPublicKey.fromHex(failing), | ||
throwsA(isA<InvalidPublicKey>()), | ||
); | ||
} | ||
|
||
}); | ||
|
||
test("accepts compressed types", () { | ||
for (final vec in validPubKeys) { | ||
if (!vec.compressed) continue; | ||
final pk = ECCompressedPublicKey.fromHex(vec.hex); | ||
expect(pk.hex, vec.hex); | ||
expect(pk.compressed, true); | ||
expect(pk.yIsEven, vec.evenY); | ||
} | ||
}); | ||
|
||
test(".fromXOnly", () => expect( | ||
ECCompressedPublicKey.fromXOnlyHex(xOnlyPubkeyVec).hex, | ||
"02$xOnlyPubkeyVec", | ||
),); | ||
|
||
test(".fromPubkey", () { | ||
|
||
void expectCompressedKey(String pubkey, String compressed) => expect( | ||
ECCompressedPublicKey.fromPubkey(ECPublicKey.fromHex(pubkey)).hex, | ||
compressed, | ||
); | ||
|
||
expectCompressedKey(longPubkeyVec, pubkeyVec); | ||
expectCompressedKey(pubkeyVec, pubkeyVec); | ||
expectCompressedKey( | ||
"06ef164284e2c3abc32b310eb62904af0d49196c51087bdf4038998f8818787c882433ae83422904f48ad36dcf351ac9a37e6b00e57cf40b469b650ec850640efe", | ||
"02ef164284e2c3abc32b310eb62904af0d49196c51087bdf4038998f8818787c88", | ||
); | ||
expectCompressedKey( | ||
"07576168b540f6f80e4d2a325f8cbd420ceb170ff42cd07e96bffc5e6a4a4ea04b1208f618306fd629cd2972cea45aa81ae7b24a64bf2e86704d7a63d82fd97a8f", | ||
"03576168b540f6f80e4d2a325f8cbd420ceb170ff42cd07e96bffc5e6a4a4ea04b", | ||
); | ||
|
||
}); | ||
|
||
}); | ||
|
||
} |
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