use of org.web3j.crypto.Sign.SignatureData in project XChange by knowm.
the class IdexTradeService method createNormalizedLimitOrderReq.
public final OrderReq createNormalizedLimitOrderReq(Currency baseCurrency, Currency counterCurrency, OrderType type, BigDecimal limitPrice, BigDecimal originalAmount, String contractAddress, BigInteger nonce, BigInteger expires) {
nonce = nonce == null ? BigInteger.valueOf(exchange.getNonceFactory().createValue()) : nonce;
contractAddress = contractAddress == null ? contractAddress().getAddress() : contractAddress;
expires = expires == null ? BigInteger.valueOf(100000) : expires;
exchange.getExchangeMetaData().getCurrencies().get(baseCurrency);
exchange.getExchangeMetaData().getCurrencies().get(counterCurrency);
OrderReq orderReq = null;
boolean untested = true;
if (untested) {
List<Currency> listOfCurrencies = asList(baseCurrency, /*OMG*/
counterCurrency);
if (type == BID)
Collections.reverse(listOfCurrencies);
IdexCurrencyMeta buy_currency = (IdexCurrencyMeta) exchange.getExchangeMetaData().getCurrencies().get(listOfCurrencies.get(0));
IdexCurrencyMeta sell_currency = (IdexCurrencyMeta) exchange.getExchangeMetaData().getCurrencies().get(listOfCurrencies.get(1));
BigDecimal divide = originalAmount.divide(limitPrice, MathContext.DECIMAL128);
BigDecimal amount_buy = divide.multiply(new BigDecimal("1e" + buy_currency.getDecimals()), MathContext.DECIMAL128);
BigDecimal amount_sell = originalAmount.multiply(new BigDecimal("1e" + sell_currency.getDecimals()), MathContext.DECIMAL128);
String buyc = buy_currency.getAddress();
String sellc = sell_currency.getAddress();
List<List<String>> hash_data = asList(asList("contractAddress", contractAddress, "address"), asList("tokenBuy", buyc, "address"), asList("amountBuy", amount_buy.toString(), "uint256"), asList("tokenSell", sellc, "address"), asList("amountSell", amount_sell.toString(), "uint256"), asList("expires", "" + expires, "uint256"), asList("nonce", "" + nonce, "uint256"), asList("address", "" + getApiKey(), "address"));
SignatureData sig = generateSignature(exchange.getExchangeSpecification().getSecretKey(), hash_data);
byte[] v = sig.getV();
byte[] r = sig.getR();
byte[] s = sig.getS();
orderReq = new OrderReq().address(getApiKey()).nonce(nonce).tokenBuy(buyc).amountBuy(amount_buy.toString()).tokenSell(sellc).amountSell(amount_sell.toString()).expires(expires).r("0x" + new String(Hex.toHexString(r))).s("0x" + new String(Hex.toHexString(s))).v(BigInteger.valueOf(v[0] & 0xffl));
}
return orderReq;
}
use of org.web3j.crypto.Sign.SignatureData in project signers by ConsenSys.
the class AzureKeyVaultSignerTest method azureWithoutHashingDoesntHashData.
@Test
void azureWithoutHashingDoesntHashData() throws SignatureException {
final AzureConfig config = new AzureConfig(keyVaultName, KEY_NAME, "", clientId, clientSecret, tenantId);
final Signer azureNonHashedDataSigner = new AzureKeyVaultSignerFactory(false).createSigner(config);
final BigInteger publicKey = Numeric.toBigInt(EthPublicKeyUtils.toByteArray(azureNonHashedDataSigner.getPublicKey()));
final byte[] dataToSign = "Hello World".getBytes(UTF_8);
// manual hash before sending to remote signing
final byte[] hashedData = Hash.sha3(dataToSign);
final Signature signature = azureNonHashedDataSigner.sign(hashedData);
// Determine if Web3j thinks the signature comes from the public key used (really proves
// that the hashedData isn't hashed a second time).
final SignatureData sigData = new SignatureData(signature.getV().toByteArray(), Numeric.toBytesPadded(signature.getR(), 32), Numeric.toBytesPadded(signature.getS(), 32));
final BigInteger recoveredPublicKey = Sign.signedMessageHashToKey(hashedData, sigData);
assertThat(recoveredPublicKey).isEqualTo(publicKey);
}
use of org.web3j.crypto.Sign.SignatureData 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.Sign.SignatureData in project web3signer by ConsenSys.
the class EthSecpArtifactSignerTest method signsData.
@Test
void signsData() {
final ECKeyPair ecKeyPair = new ECKeyPair(Numeric.toBigInt(PRIVATE_KEY), Numeric.toBigInt(PUBLIC_KEY));
final Credentials credentials = Credentials.create(ecKeyPair);
final EthSecpArtifactSigner ethSecpArtifactSigner = new EthSecpArtifactSigner(new CredentialSigner(credentials));
final Bytes message = Bytes.wrap("Hello, world!".getBytes(UTF_8));
final SecpArtifactSignature signature = ethSecpArtifactSigner.sign(message);
final SignatureData expectedSignature = Sign.signMessage(message.toArrayUnsafe(), ecKeyPair);
final Signature signatureData = signature.getSignatureData();
assertThat(signatureData.getR()).isEqualTo(Numeric.toBigInt(expectedSignature.getR()));
assertThat(signatureData.getS()).isEqualTo(Numeric.toBigInt(expectedSignature.getS()));
assertThat(signatureData.getV()).isEqualTo(Numeric.toBigInt(expectedSignature.getV()));
}
use of org.web3j.crypto.Sign.SignatureData in project web3j by web3j.
the class ECRecoverTest method testRecoverAddressFromSignature.
@Test
public void testRecoverAddressFromSignature() {
String signature = "0x2c6401216c9031b9a6fb8cbfccab4fcec6c951cdf40e2320108d1856eb532250576865fbcd452bcdc4c57321b619ed7a9cfd38bd973c3e1e0243ac2777fe9d5b1b";
String address = "0x31b26e43651e9371c88af3d36c14cfd938baf4fd";
String message = "v0G9u7huK4mJb2K1";
String prefix = PERSONAL_MESSAGE_PREFIX + message.length();
byte[] msgHash = Hash.sha3((prefix + message).getBytes());
byte[] signatureBytes = Numeric.hexStringToByteArray(signature);
byte v = signatureBytes[64];
if (v < 27) {
v += 27;
}
SignatureData sd = new SignatureData(v, (byte[]) Arrays.copyOfRange(signatureBytes, 0, 32), (byte[]) Arrays.copyOfRange(signatureBytes, 32, 64));
String addressRecovered = null;
boolean match = false;
// Iterate for each possible key to recover
for (int i = 0; i < 4; i++) {
BigInteger publicKey = Sign.recoverFromSignature((byte) i, new ECDSASignature(new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())), msgHash);
if (publicKey != null) {
addressRecovered = "0x" + Keys.getAddress(publicKey);
if (addressRecovered.equals(address)) {
match = true;
break;
}
}
}
assertEquals(addressRecovered, (address));
assertTrue(match);
}
Aggregations