mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-02-14 11:15:51 +00:00
Pubkeys are 65 bytes, not 32.
This commit is contained in:
parent
ad329d7a34
commit
1a4acc18be
@ -129,7 +129,10 @@ public class ECKey implements Serializable {
|
||||
// Derive public from private.
|
||||
this.pub = publicKeyFromPrivate(privKey);
|
||||
} 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.
|
||||
*/
|
||||
public byte[] getPrivKeyBytes() {
|
||||
return Utils.bigIntegerTo32Bytes(priv);
|
||||
return Utils.bigIntegerToBytes(priv, 32);
|
||||
}
|
||||
|
||||
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
|
||||
* 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.
|
||||
*/
|
||||
public static byte[] bigIntegerTo32Bytes(BigInteger b) {
|
||||
byte[] bytes = new byte[32];
|
||||
public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) {
|
||||
byte[] bytes = new byte[numBytes];
|
||||
byte[] biBytes = b.toByteArray();
|
||||
int start = (biBytes.length == 33) ? 1 : 0;
|
||||
int length = Math.min(biBytes.length, 32);
|
||||
System.arraycopy(biBytes, start, bytes, 32 - length, length);
|
||||
int start = (biBytes.length == numBytes + 1) ? 1 : 0;
|
||||
int length = Math.min(biBytes.length, numBytes);
|
||||
System.arraycopy(biBytes, start, bytes, numBytes - length, length);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user