Skip to content

Commit 453da0b

Browse files
committed
P256K1XOnlyPubKey: add toString() method
1 parent 42391bf commit 453da0b

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

secp-api/src/main/java/org/bitcoinj/secp/api/P256K1XOnlyPubKey.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.bitcoinj.secp.api;
1717

1818
import java.math.BigInteger;
19+
import java.util.HexFormat;
1920

2021
/**
2122
*
@@ -55,6 +56,7 @@ static P256K1XOnlyPubKey of(BigInteger x) {
5556
* Default implementation. Currently used by all known implementations
5657
*/
5758
class P256K1XOnlyPubKeyImpl implements P256K1XOnlyPubKey {
59+
private static final HexFormat formatter = HexFormat.of();
5860
private final BigInteger x;
5961

6062
public P256K1XOnlyPubKeyImpl(P256k1PubKey pubKey) {
@@ -78,5 +80,13 @@ public BigInteger getX() {
7880
public byte[] getSerialized() {
7981
return P256k1PubKey.integerTo32Bytes(x);
8082
}
83+
84+
/**
85+
* @return A hex string representing the default binary serialization format
86+
*/
87+
@Override
88+
public String toString() {
89+
return formatter.formatHex(getSerialized());
90+
}
8191
}
8292
}

secp-examples-java/src/main/java/org/bitcoinj/secp/examples/Schnorr.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static void main(String[] args) {
6565

6666
System.out.printf("Is the signature valid? %s\n", is_signature_valid);
6767
System.out.printf("Secret Key: %s\n", keyPair.getS().toString(16));
68-
System.out.printf("Public Key (as ECPoint): %s\n", formatter.formatHex(xOnly2.getSerialized()));
68+
System.out.printf("Public Key (as ECPoint): %s\n", xOnly2);
6969
System.out.printf("Signature: %s\n", formatter.formatHex(signature));
7070

7171
/* It's best practice to try to clear secrets from memory after using them.

secp-examples-kotlin/src/main/java/org/bitcoinj/secp/kotlin/examples/Schnorr.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ object Schnorr {
6060

6161
System.out.printf("Is the signature valid? %s\n", is_signature_valid)
6262
System.out.printf("Secret Key: %s\n", keyPair.s.toString(16))
63-
System.out.printf("Public Key (as ECPoint): %s\n", formatter.formatHex(xOnly2.serialized))
63+
System.out.printf("Public Key (as ECPoint): %s\n", xOnly2)
6464
System.out.printf("Signature: %s\n", formatter.formatHex(signature))
6565

6666
/* It's best practice to try to clear secrets from memory after using them.

0 commit comments

Comments
 (0)