Search in sources :

Example 1 with ECDSASignature

use of org.web3j.crypto.ECDSASignature in project signers by ConsenSys.

the class AzureKeyVaultSigner method sign.

@Override
public Signature sign(byte[] data) {
    final AzureKeyVault vault;
    try {
        vault = createUsingClientSecretCredentials(config.getClientId(), config.getClientSecret(), config.getTenantId(), config.getKeyVaultName());
    } catch (final Exception e) {
        LOG.error("Failed to connect to vault", e);
        throw new SignerInitializationException(INACCESSIBLE_KEY_ERROR, e);
    }
    final CryptographyClient cryptoClient = vault.fetchKey(config.getKeyName(), config.getKeyVersion());
    final byte[] dataToSign = needsToHash ? Hash.sha3(data) : data;
    final SignResult result = cryptoClient.sign(signingAlgo, dataToSign);
    final byte[] signature = result.getSignature();
    if (signature.length != 64) {
        throw new RuntimeException("Invalid signature from the key vault signing service, must be 64 bytes long");
    }
    // reference: blog by Tomislav Markovski
    // https://tomislav.tech/2018-02-05-ethereum-keyvault-signing-transactions/
    // The output of this will be a 64 byte array. The first 32 are the value for R and the rest is
    // S.
    final BigInteger R = new BigInteger(1, Arrays.copyOfRange(signature, 0, 32));
    final BigInteger S = new BigInteger(1, Arrays.copyOfRange(signature, 32, 64));
    // The Azure Signature MAY be in the "top" of the curve, which is illegal in Ethereum
    // thus it must be transposed to the lower intersection.
    final ECDSASignature initialSignature = new ECDSASignature(R, S);
    final ECDSASignature canonicalSignature = initialSignature.toCanonicalised();
    // Now we have to work backwards to figure out the recId needed to recover the signature.
    final int recId = recoverKeyIndex(canonicalSignature, dataToSign);
    if (recId == -1) {
        throw new RuntimeException("Could not construct a recoverable key. Are your credentials valid?");
    }
    final int headerByte = recId + 27;
    return new Signature(BigInteger.valueOf(headerByte), canonicalSignature.r, canonicalSignature.s);
}
Also used : SignerInitializationException(tech.pegasys.signers.secp256k1.common.SignerInitializationException) SignResult(com.azure.security.keyvault.keys.cryptography.models.SignResult) Signature(tech.pegasys.signers.secp256k1.api.Signature) ECDSASignature(org.web3j.crypto.ECDSASignature) BigInteger(java.math.BigInteger) ECDSASignature(org.web3j.crypto.ECDSASignature) AzureKeyVault(tech.pegasys.signers.azure.AzureKeyVault) CryptographyClient(com.azure.security.keyvault.keys.cryptography.CryptographyClient) SignerInitializationException(tech.pegasys.signers.secp256k1.common.SignerInitializationException)

Example 2 with ECDSASignature

use of org.web3j.crypto.ECDSASignature in project webapp by elimu-ai.

the class SignOnControllerWeb3 method isSignatureValid.

private boolean isSignatureValid(final String address, final String signature, final String message) {
    logger.info("isSignatureValid");
    boolean match = false;
    // Note: The label prefix is part of the standard
    String label = "\u0019Ethereum Signed Message:\n" + String.valueOf(message.getBytes().length) + message;
    // Get message hash using SHA-3
    final byte[] msgHash = Hash.sha3((label).getBytes());
    // Convert signature HEX string to bytes
    final byte[] signatureBytes = Numeric.hexStringToByteArray(signature);
    byte v = signatureBytes[64];
    if (v < 27) {
        v += 27;
    }
    // Create signature data from the signature bytes
    final SignatureData sd = new SignatureData(v, Arrays.copyOfRange(signatureBytes, 0, 32), Arrays.copyOfRange(signatureBytes, 32, 64));
    for (int i = 0; i < 4; i++) {
        final BigInteger publicKey = Sign.recoverFromSignature((byte) i, new ECDSASignature(new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())), msgHash);
        if (publicKey != null) {
            String recoveredAddress = "0x" + Keys.getAddress(publicKey);
            logger.info("recoveredAddress: " + recoveredAddress);
            if (recoveredAddress.equalsIgnoreCase(address)) {
                match = true;
                break;
            }
        }
    }
    return match;
}
Also used : SignatureData(org.web3j.crypto.Sign.SignatureData) BigInteger(java.math.BigInteger) ECDSASignature(org.web3j.crypto.ECDSASignature)

Example 3 with ECDSASignature

use of org.web3j.crypto.ECDSASignature in project autobahn-java by crossbario.

the class Util method recoverEIP712Signer.

static CompletableFuture<String> recoverEIP712Signer(int chainId, String verifyingContract, int closeAt, String marketId, String channelId, int channelSeq, BigInteger balance, boolean isFinal, byte[] signature) {
    CompletableFuture<String> future = new CompletableFuture<>();
    try {
        JSONObject data = createEIP712Data(chainId, verifyingContract, closeAt, marketId, channelId, channelSeq, balance, isFinal);
        StructuredDataEncoder encoder = new StructuredDataEncoder(data.toString());
        byte[] message = encoder.hashStructuredData();
        byte v = signature[64];
        if (v < 27) {
            v += 27;
        }
        byte[] r = Arrays.copyOfRange(signature, 0, 32);
        byte[] s = Arrays.copyOfRange(signature, 32, 64);
        Sign.SignatureData sd = new Sign.SignatureData(v, r, s);
        int recID = v - 27;
        BigInteger publicKey = Sign.recoverFromSignature((byte) recID, new ECDSASignature(new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())), message);
        if (publicKey != null) {
            future.complete("0x" + Keys.getAddress(publicKey));
        } else {
            future.complete(null);
        }
    } catch (Exception e) {
        future.completeExceptionally(e);
    }
    return future;
}
Also used : JSONException(org.json.JSONException) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) StructuredDataEncoder(org.web3j.crypto.StructuredDataEncoder) BigInteger(java.math.BigInteger) ECDSASignature(org.web3j.crypto.ECDSASignature) Sign(org.web3j.crypto.Sign)

Example 4 with ECDSASignature

use of org.web3j.crypto.ECDSASignature in project web3signer by ConsenSys.

the class FilecoinVerify method recoverSignature.

private static BigInteger recoverSignature(final SecpArtifactSignature artifactSignature, final byte[] digest) {
    final tech.pegasys.signers.secp256k1.api.Signature signatureData = artifactSignature.getSignatureData();
    final ECDSASignature signature = new ECDSASignature(signatureData.getR(), signatureData.getS());
    final ECDSASignature canonicalSignature = signature.toCanonicalised();
    final int recId = signatureData.getV().intValue();
    return Sign.recoverFromSignature(recId, canonicalSignature, digest);
}
Also used : ECDSASignature(org.web3j.crypto.ECDSASignature)

Example 5 with ECDSASignature

use of org.web3j.crypto.ECDSASignature in project autobahn-java by crossbario.

the class Util method recoverySigner.

static CompletableFuture<String> recoverySigner(JSONObject data, byte[] signature) {
    CompletableFuture<String> future = new CompletableFuture<>();
    try {
        StructuredDataEncoder encoder = new StructuredDataEncoder(data.toString());
        byte[] message = encoder.hashStructuredData();
        byte v = signature[64];
        if (v < 27) {
            v += 27;
        }
        byte[] r = Arrays.copyOfRange(signature, 0, 32);
        byte[] s = Arrays.copyOfRange(signature, 32, 64);
        Sign.SignatureData sd = new Sign.SignatureData(v, r, s);
        int recID = v - 27;
        BigInteger publicKey = Sign.recoverFromSignature((byte) recID, new ECDSASignature(new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())), message);
        if (publicKey != null) {
            future.complete("0x" + Keys.getAddress(publicKey));
        } else {
            future.complete(null);
        }
    } catch (Exception e) {
        future.completeExceptionally(e);
    }
    return future;
}
Also used : IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) StructuredDataEncoder(org.web3j.crypto.StructuredDataEncoder) BigInteger(java.math.BigInteger) ECDSASignature(org.web3j.crypto.ECDSASignature) Sign(org.web3j.crypto.Sign)

Aggregations

ECDSASignature (org.web3j.crypto.ECDSASignature)8 BigInteger (java.math.BigInteger)5 IOException (java.io.IOException)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 Sign (org.web3j.crypto.Sign)2 StructuredDataEncoder (org.web3j.crypto.StructuredDataEncoder)2 Signature (tech.pegasys.signers.secp256k1.api.Signature)2 CryptographyClient (com.azure.security.keyvault.keys.cryptography.CryptographyClient)1 SignResult (com.azure.security.keyvault.keys.cryptography.models.SignResult)1 ECPublicKey (java.security.interfaces.ECPublicKey)1 Request (okhttp3.Request)1 ResponseBody (okhttp3.ResponseBody)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 SignatureData (org.web3j.crypto.Sign.SignatureData)1 ClientConnectionException (org.web3j.protocol.exceptions.ClientConnectionException)1 AzureKeyVault (tech.pegasys.signers.azure.AzureKeyVault)1 Signer (tech.pegasys.signers.secp256k1.api.Signer)1 PublicKeySignerIdentifier (tech.pegasys.signers.secp256k1.common.PublicKeySignerIdentifier)1 SignerInitializationException (tech.pegasys.signers.secp256k1.common.SignerInitializationException)1