Search in sources :

Example 6 with ECDSASignature

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

the class HSMHTTPRequestProcessor method callHSM.

@Override
public Sign.SignatureData callHSM(byte[] dataToSign, HSMHTTPPass pass) {
    Request request = createRequest(dataToSign, pass);
    try (okhttp3.Response response = client.newCall(request).execute()) {
        ResponseBody responseBody = response.body();
        if (response.isSuccessful()) {
            if (responseBody != null) {
                String signHex = readResponse(responseBody.byteStream());
                byte[] signBytes = Numeric.hexStringToByteArray(signHex);
                ECDSASignature signature = CryptoUtils.fromDerFormat(signBytes);
                return Sign.createSignatureData(signature, pass.getPublicKey(), dataToSign);
            } else {
                return null;
            }
        } else {
            int code = response.code();
            String text = responseBody == null ? "N/A" : responseBody.string();
            throw new ClientConnectionException("Invalid response received: " + code + "; " + text);
        }
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }
    return null;
}
Also used : ClientConnectionException(org.web3j.protocol.exceptions.ClientConnectionException) Request(okhttp3.Request) ECDSASignature(org.web3j.crypto.ECDSASignature) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Example 7 with ECDSASignature

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

the class MultiKeyTransactionSigningAcceptanceTestBase method verifySignature.

void verifySignature(String publicKeyHex) {
    ECPublicKey pubKey = EthPublicKeyUtils.createPublicKey(Bytes.fromHexString(publicKeyHex));
    final Optional<Signer> signer = signerProvider.getSigner(new PublicKeySignerIdentifier(pubKey));
    assertThat(signer).isNotEmpty();
    final Signature signature = signer.get().sign(DATA_TO_SIGN);
    final BigInteger messagePublicKey = recoverPublicKey(signature);
    assertThat(EthPublicKeyUtils.createPublicKey(messagePublicKey)).isEqualTo(pubKey);
    final ECDSASignature ecdsaSignature = new ECDSASignature(signature.getR(), signature.getS());
    assertThat(ecdsaSignature.isCanonical()).isTrue();
}
Also used : Signer(tech.pegasys.signers.secp256k1.api.Signer) ECPublicKey(java.security.interfaces.ECPublicKey) Signature(tech.pegasys.signers.secp256k1.api.Signature) ECDSASignature(org.web3j.crypto.ECDSASignature) BigInteger(java.math.BigInteger) ECDSASignature(org.web3j.crypto.ECDSASignature) PublicKeySignerIdentifier(tech.pegasys.signers.secp256k1.common.PublicKeySignerIdentifier)

Example 8 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)

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