Package org.web3j.crypto
Class Sign
- java.lang.Object
-
- org.web3j.crypto.Sign
-
public class Sign extends java.lang.ObjectTransaction signing logic.Adapted from the BitcoinJ ECKey implementation.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSign.SignatureData
-
Field Summary
Fields Modifier and Type Field Description static org.bouncycastle.asn1.x9.X9ECParametersCURVE_PARAMS
-
Constructor Summary
Constructors Constructor Description Sign()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]getEthereumMessageHash(byte[] message)static java.math.BigIntegerpublicFromPoint(byte[] bits)Returns public key point from the given curve.static java.math.BigIntegerpublicKeyFromPrivate(java.math.BigInteger privKey)Returns public key from the given private key.static org.bouncycastle.math.ec.ECPointpublicPointFromPrivate(java.math.BigInteger privKey)Returns public key point from the given private key.static java.math.BigIntegerrecoverFromSignature(int recId, ECDSASignature sig, byte[] message)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.static java.math.BigIntegersignedMessageHashToKey(byte[] messageHash, Sign.SignatureData signatureData)Given an arbitrary message hash and an Ethereum message signature encoded in bytes, returns the public key that was used to sign it.static java.math.BigIntegersignedMessageToKey(byte[] message, Sign.SignatureData signatureData)Given an arbitrary piece of text and an Ethereum message signature encoded in bytes, returns the public key that was used to sign it.static java.math.BigIntegersignedPrefixedMessageToKey(byte[] message, Sign.SignatureData signatureData)Given an arbitrary message and an Ethereum message signature encoded in bytes, returns the public key that was used to sign it.static Sign.SignatureDatasignMessage(byte[] message, ECKeyPair keyPair)static Sign.SignatureDatasignMessage(byte[] message, ECKeyPair keyPair, boolean needToHash)static Sign.SignatureDatasignPrefixedMessage(byte[] message, ECKeyPair keyPair)
-
-
-
Method Detail
-
getEthereumMessageHash
public static byte[] getEthereumMessageHash(byte[] message)
-
signPrefixedMessage
public static Sign.SignatureData signPrefixedMessage(byte[] message, ECKeyPair keyPair)
-
signMessage
public static Sign.SignatureData signMessage(byte[] message, ECKeyPair keyPair)
-
signMessage
public static Sign.SignatureData signMessage(byte[] message, ECKeyPair keyPair, boolean needToHash)
-
recoverFromSignature
public static java.math.BigInteger recoverFromSignature(int recId, ECDSASignature sig, byte[] message)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.The recId is an index from 0 to 3 which indicates which of the 4 possible keys is the correct one. Because the key recovery operation yields multiple potential keys, the correct key must either be stored alongside the signature, or you must be willing to try each recId in turn until you find one that outputs the key you are expecting.
If this method returns null it means recovery was not possible and recId should be iterated.
Given the above two points, a correct usage of this method is inside a for loop from 0 to 3, and if the output is null OR a key that is not the one you expect, you try again with the next recId.
- Parameters:
recId- Which possible key to recover.sig- the R and S components of the signature, wrapped.message- Hash of the data that was signed.- Returns:
- An ECKey containing only the public part, or null if recovery wasn't possible.
-
signedMessageToKey
public static java.math.BigInteger signedMessageToKey(byte[] message, Sign.SignatureData signatureData) throws java.security.SignatureExceptionGiven an arbitrary piece of text and an Ethereum message signature encoded in bytes, returns 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.- Parameters:
message- RLP encoded message.signatureData- The message signature components- Returns:
- the public key used to sign the message
- Throws:
java.security.SignatureException- If the public key could not be recovered or if there was a signature format error.
-
signedPrefixedMessageToKey
public static java.math.BigInteger signedPrefixedMessageToKey(byte[] message, Sign.SignatureData signatureData) throws java.security.SignatureExceptionGiven an arbitrary message and an Ethereum message signature encoded in bytes, returns 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.- Parameters:
message- The message.signatureData- The message signature components- Returns:
- the public key used to sign the message
- Throws:
java.security.SignatureException- If the public key could not be recovered or if there was a signature format error.
-
signedMessageHashToKey
public static java.math.BigInteger signedMessageHashToKey(byte[] messageHash, Sign.SignatureData signatureData) throws java.security.SignatureExceptionGiven an arbitrary message hash and an Ethereum message signature encoded in bytes, returns 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.- Parameters:
messageHash- The message hash.signatureData- The message signature components- Returns:
- the public key used to sign the message
- Throws:
java.security.SignatureException- If the public key could not be recovered or if there was a signature format error.
-
publicKeyFromPrivate
public static java.math.BigInteger publicKeyFromPrivate(java.math.BigInteger privKey)
Returns public key from the given private key.- Parameters:
privKey- the private key to derive the public key from- Returns:
- BigInteger encoded public key
-
publicPointFromPrivate
public static org.bouncycastle.math.ec.ECPoint publicPointFromPrivate(java.math.BigInteger privKey)
Returns public key point from the given private key.- Parameters:
privKey- the private key to derive the public key from- Returns:
- ECPoint public key
-
publicFromPoint
public static java.math.BigInteger publicFromPoint(byte[] bits)
Returns public key point from the given curve.- Parameters:
bits- representing the point on the curve- Returns:
- BigInteger encoded public key
-
-