3
0
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:
Mike Hearn 2012-01-26 18:40:24 +01:00
parent ad329d7a34
commit 1a4acc18be
2 changed files with 13 additions and 8 deletions

View File

@ -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) {

View File

@ -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;
}