Skip to content

Commit

Permalink
P256K1XOnlyPubKey: add toString() method
Browse files Browse the repository at this point in the history
  • Loading branch information
msgilligan committed Aug 20, 2024
1 parent 42391bf commit 453da0b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.bitcoinj.secp.api;

import java.math.BigInteger;
import java.util.HexFormat;

/**
*
Expand Down Expand Up @@ -55,6 +56,7 @@ static P256K1XOnlyPubKey of(BigInteger x) {
* Default implementation. Currently used by all known implementations
*/
class P256K1XOnlyPubKeyImpl implements P256K1XOnlyPubKey {
private static final HexFormat formatter = HexFormat.of();
private final BigInteger x;

public P256K1XOnlyPubKeyImpl(P256k1PubKey pubKey) {
Expand All @@ -78,5 +80,13 @@ public BigInteger getX() {
public byte[] getSerialized() {
return P256k1PubKey.integerTo32Bytes(x);
}

/**
* @return A hex string representing the default binary serialization format
*/
@Override
public String toString() {
return formatter.formatHex(getSerialized());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static void main(String[] args) {

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

/* It's best practice to try to clear secrets from memory after using them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ object Schnorr {

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

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

0 comments on commit 453da0b

Please sign in to comment.