Skip to content

Commit

Permalink
ECKey: Move compressPoint()/decompressPoint() helpers to LazyECPoint.…
Browse files Browse the repository at this point in the history
…compress()/decompress().

Deprecate the old methods.
  • Loading branch information
schildbach authored and Andreas Schildbach committed Sep 29, 2021
1 parent 98bc701 commit 4dc4cf7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
12 changes: 6 additions & 6 deletions core/src/main/java/org/bitcoinj/core/ECKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,19 @@ protected ECKey(@Nullable BigInteger priv, LazyECPoint pub) {
}

/**
* Utility for compressing an elliptic curve point. Returns the same point if it's already compressed.
* See the ECKey class docs for a discussion of point compression.
* @deprecated Use {@link LazyECPoint#compress()}
*/
@Deprecated
public static LazyECPoint compressPoint(LazyECPoint point) {
return point.isCompressed() ? point : new LazyECPoint(point.get(), true);
return point.compress();
}

/**
* Utility for decompressing an elliptic curve point. Returns the same point if it's already uncompressed.
* See the ECKey class docs for a discussion of point compression.
* @deprecated Use {@link LazyECPoint#decompress()}
*/
@Deprecated
public static LazyECPoint decompressPoint(LazyECPoint point) {
return !point.isCompressed() ? point : new LazyECPoint(point.get(), false);
return point.decompress();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/bitcoinj/crypto/DeterministicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public DeterministicKey(List<ChildNumber> childNumberPath,
LazyECPoint publicAsPoint,
@Nullable BigInteger priv,
@Nullable DeterministicKey parent) {
super(priv, compressPoint(checkNotNull(publicAsPoint)));
super(priv, publicAsPoint.compress());
checkArgument(chainCode.length == 32);
this.parent = parent;
this.childNumberPath = HDPath.M(checkNotNull(childNumberPath));
Expand Down Expand Up @@ -138,7 +138,7 @@ public DeterministicKey(List<ChildNumber> childNumberPath,
@Nullable DeterministicKey parent,
int depth,
int parentFingerprint) {
super(null, compressPoint(checkNotNull(publicAsPoint)));
super(null, publicAsPoint.compress());
checkArgument(chainCode.length == 32);
this.parent = parent;
this.childNumberPath = HDPath.M(checkNotNull(childNumberPath));
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/java/org/bitcoinj/crypto/LazyECPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@ public LazyECPoint(ECPoint point, boolean compressed) {
this.bits = null;
}

/**
* Returns a compressed version of this elliptic curve point. Returns the same point if it's already compressed.
* See the {@link ECKey} class docs for a discussion of point compression.
*/
public LazyECPoint compress() {
return compressed ? this : new LazyECPoint(get(), true);
}

/**
* Returns a decompressed version of this elliptic curve point. Returns the same point if it's already compressed.
* See the {@link ECKey} class docs for a discussion of point compression.
*/
public LazyECPoint decompress() {
return !compressed ? this : new LazyECPoint(get(), false);
}

public ECPoint get() {
if (point == null)
point = curve.decodePoint(bits);
Expand Down

0 comments on commit 4dc4cf7

Please sign in to comment.