mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-11-02 05:27:17 +00:00
Pubkeys are 65 bytes, not 32.
This commit is contained in:
@@ -129,7 +129,10 @@ public class ECKey implements Serializable {
|
|||||||
// Derive public from private.
|
// Derive public from private.
|
||||||
this.pub = publicKeyFromPrivate(privKey);
|
this.pub = publicKeyFromPrivate(privKey);
|
||||||
} else if (pubKey != null) {
|
} else if (pubKey != null) {
|
||||||
this.pub = Utils.bigIntegerTo32Bytes(pubKey);
|
// We expect the pubkey to be in regular encoded form, just as a BigInteger. Therefore the first byte is
|
||||||
|
// a special marker byte.
|
||||||
|
// TODO: This is probably not a useful API and may be confusing.
|
||||||
|
this.pub = Utils.bigIntegerToBytes(pubKey, 65);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,7 +281,7 @@ public class ECKey implements Serializable {
|
|||||||
* Returns a 32 byte array containing the private key.
|
* Returns a 32 byte array containing the private key.
|
||||||
*/
|
*/
|
||||||
public byte[] getPrivKeyBytes() {
|
public byte[] getPrivKeyBytes() {
|
||||||
return Utils.bigIntegerTo32Bytes(priv);
|
return Utils.bigIntegerToBytes(priv, 32);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ECKey fromPrivKeyBytes(byte[] bytes) {
|
public static ECKey fromPrivKeyBytes(byte[] bytes) {
|
||||||
|
|||||||
@@ -64,15 +64,17 @@ public class Utils {
|
|||||||
/**
|
/**
|
||||||
* The regular {@link java.math.BigInteger#toByteArray()} method isn't quite what we often need: it appends a
|
* The regular {@link java.math.BigInteger#toByteArray()} method isn't quite what we often need: it appends a
|
||||||
* leading zero to indicate that the number is positive and may need padding.
|
* leading zero to indicate that the number is positive and may need padding.
|
||||||
* @param b
|
*
|
||||||
|
* @param b the integer to format into a byte array
|
||||||
|
* @param numBytes the desired size of the resulting byte array
|
||||||
* @return 32 byte long array.
|
* @return 32 byte long array.
|
||||||
*/
|
*/
|
||||||
public static byte[] bigIntegerTo32Bytes(BigInteger b) {
|
public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) {
|
||||||
byte[] bytes = new byte[32];
|
byte[] bytes = new byte[numBytes];
|
||||||
byte[] biBytes = b.toByteArray();
|
byte[] biBytes = b.toByteArray();
|
||||||
int start = (biBytes.length == 33) ? 1 : 0;
|
int start = (biBytes.length == numBytes + 1) ? 1 : 0;
|
||||||
int length = Math.min(biBytes.length, 32);
|
int length = Math.min(biBytes.length, numBytes);
|
||||||
System.arraycopy(biBytes, start, bytes, 32 - length, length);
|
System.arraycopy(biBytes, start, bytes, numBytes - length, length);
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user