Package org.web3j.crypto
Class Sign
java.lang.Object
org.web3j.crypto.Sign
Transaction signing logic.
Adapted from the BitcoinJ ECKey implementation.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final org.bouncycastle.asn1.x9.X9ECParametersstatic final byteEIP-7702 authorization signing magic byte.static final intstatic final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]authorizationHash(BigInteger chainId, String address, BigInteger nonce) Computes the EIP-7702 authorization digest:keccak256(0x05 ‖ rlp([chainId, address, nonce])).static Sign.SignatureDatacreateSignatureData(ECDSASignature sig, BigInteger publicKey, byte[] messageHash) Signature without EIP-155 (Simple replay attack protection) https://eips.ethereum.org/EIPS/eip-155 To add EIP-155 call TransactionEncoder.createEip155SignatureData after that.static byte[]getEthereumMessageHash(byte[] message) static intgetRecId(Sign.SignatureData signatureData, long chainId) Returns recovery ID.static byte[]getVFromRecId(int recId) Returns the header 'v'.static BigIntegerpublicFromPoint(byte[] bits) Returns public key point from the given curve.static BigIntegerpublicKeyFromPrivate(BigInteger privKey) Returns public key from the given private key.static org.bouncycastle.math.ec.ECPointpublicPointFromPrivate(BigInteger privKey) Returns public key point from the given private key.static StringrecoverAuthorizationSigner(AuthorizationTuple authorization) Recovers the authorizing account ("authority") that signed the given EIP-7702 authorization tuple.static 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 Sign.SignatureDatasignatureDataFromHex(String hexSignature) Returns SignatureData from hex signature.static AuthorizationTuplesignAuthorization(long chainId, String address, BigInteger nonce, Credentials credentials) Convenience overload taking alongchainId andCredentials.static AuthorizationTuplesignAuthorization(long chainId, String address, BigInteger nonce, ECKeyPair keyPair) Convenience overload taking alongchainId.static AuthorizationTuplesignAuthorization(BigInteger chainId, String address, BigInteger nonce, Credentials credentials) Convenience overload takingCredentials.static AuthorizationTuplesignAuthorization(BigInteger chainId, String address, BigInteger nonce, ECKeyPair keyPair) Signs an EIP-7702 authorization with the given key pair, returning a fully populated (signed)AuthorizationTuple.static 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 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 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) static Sign.SignatureDatasignTypedData(String jsonData, ECKeyPair keyPair)
-
Field Details
-
CURVE_PARAMS
public static final org.bouncycastle.asn1.x9.X9ECParameters CURVE_PARAMS -
CHAIN_ID_INC
public static final int CHAIN_ID_INC- See Also:
-
LOWER_REAL_V
public static final int LOWER_REAL_V- See Also:
-
REPLAY_PROTECTED_V_MIN
public static final int REPLAY_PROTECTED_V_MIN- See Also:
-
EIP7702_MAGIC
public static final byte EIP7702_MAGICEIP-7702 authorization signing magic byte.- See Also:
-
-
Constructor Details
-
Sign
public Sign()
-
-
Method Details
-
getEthereumMessageHash
public static byte[] getEthereumMessageHash(byte[] message) -
signPrefixedMessage
-
signMessage
-
signTypedData
public static Sign.SignatureData signTypedData(String jsonData, ECKeyPair keyPair) throws IOException - Throws:
IOException
-
signMessage
-
createSignatureData
public static Sign.SignatureData createSignatureData(ECDSASignature sig, BigInteger publicKey, byte[] messageHash) Signature without EIP-155 (Simple replay attack protection) https://eips.ethereum.org/EIPS/eip-155 To add EIP-155 call TransactionEncoder.createEip155SignatureData after that. -
signatureDataFromHex
public static Sign.SignatureData signatureDataFromHex(String hexSignature) throws SignatureException Returns SignatureData from hex signature.- Parameters:
hexSignature- hex representation of signature- Returns:
- SignatureData
- Throws:
RuntimeException- if signature has invalid formatSignatureException
-
recoverFromSignature
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 BigInteger signedMessageToKey(byte[] message, Sign.SignatureData signatureData) throws SignatureException Given 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:
SignatureException- If the public key could not be recovered or if there was a signature format error.
-
signedPrefixedMessageToKey
public static BigInteger signedPrefixedMessageToKey(byte[] message, Sign.SignatureData signatureData) throws SignatureException Given 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:
SignatureException- If the public key could not be recovered or if there was a signature format error.
-
signedMessageHashToKey
public static BigInteger signedMessageHashToKey(byte[] messageHash, Sign.SignatureData signatureData) throws SignatureException Given 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:
SignatureException- If the public key could not be recovered or if there was a signature format error.
-
getRecId
Returns recovery ID.- Parameters:
signatureData- The message signature componentschainId- of the network- Returns:
- int recovery ID
-
getVFromRecId
public static byte[] getVFromRecId(int recId) Returns the header 'v'.- Parameters:
recId- The recovery id.- Returns:
- byte[] header 'v'.
-
publicKeyFromPrivate
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
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
Returns public key point from the given curve.- Parameters:
bits- representing the point on the curve- Returns:
- BigInteger encoded public key
-
authorizationHash
Computes the EIP-7702 authorization digest:keccak256(0x05 ‖ rlp([chainId, address, nonce])). This is the value signed (and recovered against) for a set-code authorization.- Parameters:
chainId- chain id the authorization is bound to (0= valid on any chain)address- 20-byte delegation target address (0x-prefixed)nonce- authority account nonce the authorization is valid for- Returns:
- the 32-byte authorization hash
-
signAuthorization
public static AuthorizationTuple signAuthorization(BigInteger chainId, String address, BigInteger nonce, ECKeyPair keyPair) Signs an EIP-7702 authorization with the given key pair, returning a fully populated (signed)AuthorizationTuple. The signature is canonical (low-s) per EIP-2.- Parameters:
chainId- chain id the authorization is bound to (0= valid on any chain)address- 20-byte delegation target address (0x-prefixed)nonce- authority account nonce the authorization is valid forkeyPair- the authority's key pair- Returns:
- a signed authorization tuple
-
signAuthorization
public static AuthorizationTuple signAuthorization(BigInteger chainId, String address, BigInteger nonce, Credentials credentials) Convenience overload takingCredentials. -
signAuthorization
public static AuthorizationTuple signAuthorization(long chainId, String address, BigInteger nonce, ECKeyPair keyPair) Convenience overload taking alongchainId. -
signAuthorization
public static AuthorizationTuple signAuthorization(long chainId, String address, BigInteger nonce, Credentials credentials) Convenience overload taking alongchainId andCredentials. -
recoverAuthorizationSigner
public static String recoverAuthorizationSigner(AuthorizationTuple authorization) throws SignatureException Recovers the authorizing account ("authority") that signed the given EIP-7702 authorization tuple.- Parameters:
authorization- a signed authorization tuple- Returns:
- the 0x-prefixed authority address
- Throws:
SignatureException- if the signature is malformed or the key cannot be recovered
-