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);
}
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;
}
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;
}
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);
}
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;
}
Aggregations