Remove Bitcoin message signing
This commit is contained in:
parent
348ae7617a
commit
2e6b8769d7
|
@ -366,18 +366,6 @@ public class ECKey implements Serializable {
|
||||||
return new ECDSASignature(components[0], components[1]).toCanonicalised();
|
return new ECDSASignature(components[0], components[1]).toCanonicalised();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Signs a text message using the standard Bitcoin messaging signing format and returns the signature as a base64
|
|
||||||
* encoded string.
|
|
||||||
*
|
|
||||||
* @throws IllegalStateException if this ECKey does not have the private part.
|
|
||||||
*/
|
|
||||||
public String signBitcoinMessage(String message) {
|
|
||||||
byte[] data = ByteUtil.formatForBitcoinSigning(message);
|
|
||||||
byte[] hash = HashUtil.doubleDigest(data);
|
|
||||||
return sign(hash).toBase64();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Takes the sha3 hash (32 bytes) of data and returns the ECDSA signature
|
* Takes the sha3 hash (32 bytes) of data and returns the ECDSA signature
|
||||||
*
|
*
|
||||||
|
@ -402,23 +390,6 @@ public class ECKey implements Serializable {
|
||||||
return sig;
|
return sig;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Given a piece of text and a message signature encoded in base64, returns an ECKey
|
|
||||||
* containing the public key that was used to sign it. This can then be compared to the expected public key to
|
|
||||||
* determine if the signature was correct.
|
|
||||||
*
|
|
||||||
* @param message a piece of human readable text that was signed
|
|
||||||
* @param signatureBase64 The Bitcoin-format message signature in base64
|
|
||||||
* @throws SignatureException If the public key could not be recovered or if there was a signature format error.
|
|
||||||
*/
|
|
||||||
public static ECKey signedBitcoinMessageToKey(String message, String signatureBase64) throws SignatureException {
|
|
||||||
byte[] messageBytes = ByteUtil.formatForBitcoinSigning(message);
|
|
||||||
// Note that the C++ code doesn't actually seem to specify any character encoding. Presumably it's whatever
|
|
||||||
// JSON-SPIRIT hands back. Assume UTF-8 for now.
|
|
||||||
byte[] messageHash = HashUtil.doubleDigest(messageBytes);
|
|
||||||
return signatureToKey(messageHash, signatureBase64);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given a piece of text and a message signature encoded in base64, returns an ECKey
|
* Given a piece of text and a message signature encoded in base64, returns an ECKey
|
||||||
* containing the public key that was used to sign it. This can then be compared to the expected public key to
|
* containing the public key that was used to sign it. This can then be compared to the expected public key to
|
||||||
|
@ -536,16 +507,6 @@ public class ECKey implements Serializable {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Convenience wrapper around {@link ECKey#signedBitcoinMessageToKey(String, String)}. If the key derived from the
|
|
||||||
* signature is not the same as this one, throws a SignatureException.
|
|
||||||
*/
|
|
||||||
public void verifyBitcoinMessage(String message, String signatureBase64) throws SignatureException {
|
|
||||||
ECKey key = ECKey.signedBitcoinMessageToKey(message, signatureBase64);
|
|
||||||
if (!key.pub.equals(pub))
|
|
||||||
throw new SignatureException("Signature did not match for message");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Given the components of a signature and a selector value, recover and return the public key
|
* <p>Given the components of a signature and a selector value, recover and return the public key
|
||||||
* that generated the signature according to the algorithm in SEC1v2 section 4.1.6.</p>
|
* that generated the signature according to the algorithm in SEC1v2 section 4.1.6.</p>
|
||||||
|
|
|
@ -3,7 +3,6 @@ package org.ethereum.util;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import com.google.common.primitives.UnsignedInteger;
|
import com.google.common.primitives.UnsignedInteger;
|
||||||
|
@ -11,10 +10,6 @@ import com.google.common.primitives.UnsignedLongs;
|
||||||
|
|
||||||
public class ByteUtil {
|
public class ByteUtil {
|
||||||
|
|
||||||
/** The string that prefixes all text messages signed using Bitcoin keys. */
|
|
||||||
public static final String BITCOIN_SIGNED_MESSAGE_HEADER = "Bitcoin Signed Message:\n";
|
|
||||||
public static final byte[] BITCOIN_SIGNED_MESSAGE_HEADER_BYTES = BITCOIN_SIGNED_MESSAGE_HEADER.getBytes();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a copy of bytes and appends b to the end of it
|
* Creates a copy of bytes and appends b to the end of it
|
||||||
*/
|
*/
|
||||||
|
@ -24,20 +19,6 @@ public class ByteUtil {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
|
||||||
// * Returns the given byte array hex encoded.
|
|
||||||
// */
|
|
||||||
// public static String bytesToHexString(byte[] bytes) {
|
|
||||||
// StringBuffer buf = new StringBuffer(bytes.length * 2);
|
|
||||||
// for (byte b : bytes) {
|
|
||||||
// String s = Integer.toString(0xFF & b, 16);
|
|
||||||
// if (s.length() < 2)
|
|
||||||
// buf.append('0');
|
|
||||||
// buf.append(s);
|
|
||||||
// }
|
|
||||||
// return buf.toString();
|
|
||||||
// }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
||||||
|
@ -60,27 +41,8 @@ public class ByteUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Given a textual message, returns a byte buffer formatted as follows:</p>
|
* <p>Given a textual message, returns a byte buffer formatted as follows:</p>
|
||||||
*
|
|
||||||
* <tt><p>[24] "Bitcoin Signed Message:\n" [message.length as a varint] message</p></tt>
|
|
||||||
*/
|
*/
|
||||||
public static byte[] formatForBitcoinSigning(String message) {
|
public static byte[] formatForSigning(byte[] message) {
|
||||||
try {
|
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
||||||
bos.write(BITCOIN_SIGNED_MESSAGE_HEADER_BYTES.length);
|
|
||||||
bos.write(BITCOIN_SIGNED_MESSAGE_HEADER_BYTES);
|
|
||||||
byte[] messageBytes = message.getBytes(Charset.forName("UTF-8"));
|
|
||||||
bos.write(encodeInt(messageBytes.length));
|
|
||||||
bos.write(messageBytes);
|
|
||||||
return bos.toByteArray();
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e); // Cannot happen.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Given a textual message, returns a byte buffer formatted as follows:</p>
|
|
||||||
*/
|
|
||||||
public static byte[] formatForEthereumSigning(byte[] message) {
|
|
||||||
try {
|
try {
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
bos.write(encodeInt(message.length));
|
bos.write(encodeInt(message.length));
|
||||||
|
|
Loading…
Reference in New Issue