congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
ECKey
Code IndexAdd Tabnine to your IDE (free)

How to use
ECKey
in
io.yggdrash.common.crypto

Best Java code snippets using io.yggdrash.common.crypto.ECKey (Showing top 20 results out of 315)

origin: yggdrash/yggdrash

/**
 * Account Constructor.
 * - generate wallet with new key
 */
public Account() {
  this.key = new ECKey();
  this.address = this.key.getAddress();
}
origin: yggdrash/yggdrash

/**
 * Takes the keccak hash (32 bytes) of data and returns the ECDSA signature
 *
 * @param messageHash -
 * @return -
 * @throws IllegalStateException if this ECKey does not have the private part.
 */
public ECDSASignature sign(byte[] messageHash) {
  ECDSASignature sig = doSign(messageHash);
  // Now we have to work backwards to figure out the recId needed to recover the signature.
  int recId = -1;
  byte[] thisKey = this.pub.getEncoded(/* compressed */ false);
  for (int i = 0; i < 4; i++) {
    byte[] k = ECKey.recoverPubBytesFromSignature(i, sig, messageHash);
    if (k != null && Arrays.equals(k, thisKey)) {
      recId = i;
      break;
    }
  }
  if (recId == -1) {
    throw new RuntimeException("Could not construct a recoverable key. This should never happen.");
  }
  sig.v = (byte) (recId + 27);
  return sig;
}
origin: yggdrash/yggdrash

/**
 * Creates an ECKey given the private key only.
 *
 * @param privKeyBytes -
 * @return -
 */
public static ECKey fromPrivate(byte[] privKeyBytes) {
  return fromPrivate(new BigInteger(1, privKeyBytes));
}
origin: yggdrash/yggdrash

/**
 * Compute the key that signed the given signature.
 *
 * @param messageHash     32-byte hash of message
 * @param signatureBase64 Base-64 encoded signature
 * @return ECKey
 */
public static ECKey signatureToKey(byte[] messageHash, String signatureBase64) throws SignatureException {
  final byte[] keyBytes = signatureToKeyBytes(messageHash, signatureBase64);
  return ECKey.fromPublicOnly(keyBytes);
}
origin: yggdrash/yggdrash

/**
 * Verifies the given R/S pair (signature) against a hash using the public key.
 *
 * @param sigHash   -
 * @param signature -
 * @return -
 */
public boolean verify(byte[] sigHash, ECDSASignature signature) {
  return ECKey.verify(sigHash, signature, getPubKey());
}
origin: yggdrash/yggdrash

@Test
public void testVerifySignature2() throws SignatureException {
  BigInteger r = new BigInteger("c52c114d4f5a3ba904a9b3036e5e118fe0dbb987fe3955da20f2cd8f6c21ab9c", 16);
  BigInteger s = new BigInteger("6ba4c2874299a55ad947dbc98a25ee895aabf6b625c26c435e84bfd70edf2f69", 16);
  ECDSASignature sig = ECDSASignature.fromComponents(r.toByteArray(), s.toByteArray(), (byte) 0x1b);
  byte[] rawtx = Hex.decode("f82804881bc16d674ec8000094cd2a3d9f938e13cd947ec05abc7fe734df8dd8268609184e72a0006480");
  byte[] rawHash = HashUtil.sha3(rawtx);
  byte[] address = Hex.decode("cd2a3d9f938e13cd947ec05abc7fe734df8dd826");
  ECKey key = ECKey.signatureToKey(rawHash, sig);
  assertEquals(key, ECKey.signatureToKey(rawHash, sig.toBase64()));
  assertEquals(key, ECKey.recoverFromSignature(0, sig, rawHash));
  assertArrayEquals(key.getPubKey(), ECKey.recoverPubBytesFromSignature(0, sig, rawHash));
  assertArrayEquals(address, key.getAddress());
  assertArrayEquals(address, ECKey.signatureToAddress(rawHash, sig));
  assertArrayEquals(address, ECKey.signatureToAddress(rawHash, sig.toBase64()));
  assertArrayEquals(address, ECKey.recoverAddressFromSignature(0, sig, rawHash));
  assertTrue(key.verify(rawHash, sig));
}
origin: yggdrash/yggdrash

@Test
public void testVerifySignature6() throws SignatureException {
  // generate ECkey object with privateKey
  ECKey key = ECKey.fromPrivate(privateKey);
  // check public key with pubKey
  assertArrayEquals(pubKey, key.getPubKey());
  // generate messageHash with exampleMessage
  byte[] messageHash = HashUtil.sha3(exampleMessage.getBytes());
  // generate ECDSASignature with ECKey, messageHash
  ECDSASignature signature = key.sign(messageHash);
  // verify the sign message
  assertTrue(key.verify(messageHash, signature));
  // get public key with messageHash, ECDSASignature
  ECKey keyFromSig = ECKey.signatureToKey(messageHash, signature);
  byte[] pubKeyFromSig = keyFromSig.getPubKey();
  assertArrayEquals(pubKey, pubKeyFromSig);
  // verify the sign message
  assertTrue(keyFromSig.verify(messageHash, signature));
}
origin: yggdrash/yggdrash

@Test
public void keyRecovery() {
  ECKey key = new ECKey();
  String message = "Hello World!";
  byte[] hash = HashUtil.sha256(message.getBytes());
  ECKey.ECDSASignature sig = key.doSign(hash);
  key = ECKey.fromPublicOnly(key.getPubKeyPoint());
  boolean found = false;
  for (int i = 0; i < 4; i++) {
    ECKey key2 = ECKey.recoverFromSignature(i, sig, hash);
    checkNotNull(key2);
    if (key.equals(key2)) {
      found = true;
      break;
    }
  }
  assertTrue(found);
}
origin: yggdrash/yggdrash

@Test
public void testECKey() {
  ECKey key = new ECKey();
  assertTrue(key.isPubKeyCanonical());
  assertNotNull(key.getPubKey());
  assertNotNull(key.getPrivKeyBytes());
  log.debug(Hex.toHexString(key.getPrivKeyBytes()) + " :Generated privkey");
  log.debug(Hex.toHexString(key.getPubKey()) + " :Generated pubkey");
}
origin: yggdrash/yggdrash

private void encryptKeyFileInit(ECKey key, String keyPath, String keyName, String password)
    throws IOException, InvalidCipherTextException {
  if (key == null) {
    key = new ECKey();
  this.keyPath = keyPath;
  this.keyName = keyName;
  this.address = key.getAddress();
  this.publicKey = key.getPubKey();
      WALLET_PBKDF2_ALGORITHM);
  byte[] encData = AESEncrypt.encrypt(
      key.getPrivKeyBytes(),
      ByteUtil.parseBytes(kdfPass, 0, 16),
      iv);
origin: yggdrash/yggdrash

@Test
public void testEthereumSign() {
  ECKey key = ECKey.fromPrivate(privateKey);
  log.debug("Secret\t: " + Hex.toHexString(key.getPrivKeyBytes()));
  log.debug("Pubkey\t: " + Hex.toHexString(key.getPubKey()));
  log.debug("Data\t: " + exampleMessage);
  byte[] messageHash = HashUtil.sha3(exampleMessage.getBytes());
  ECDSASignature signature = key.sign(messageHash);
  String output = signature.toBase64();
  log.debug("Sign\t: " + output + " (Base64, length: " + output.length() + ")");
  assertEquals(sigBase64, output);
}
origin: yggdrash/yggdrash

@Test
public void testVerifySignature_Static() throws SignatureException {
  byte[] messageHash = Hex.decode("92e0d4290bba01aa0abbb4705360c751af13fdb1131b8f6f1e632c4621adac75");
  byte[] signature = Hex.decode("1cca588a8eb84d5bf6741bc6e0ccfbe1fdb05b6c624b5fe72199fa1f2e501f876c5b5f11863323a998b79a0d27714fcc8825cf357903e863396f2e2e281220de31");
  // get public key with messageHash, ECDSASignature
  ECKey.ECDSASignature sig = new ECKey.ECDSASignature(signature);
  ECKey keyFromSig = ECKey.signatureToKey(messageHash, sig);
  log.debug("address=" + Hex.toHexString(keyFromSig.getAddress()));
  byte[] pubKeyFromSig = keyFromSig.getPubKey();
  log.debug("pubKey=" + Hex.toHexString(pubKeyFromSig));
  assertArrayEquals(pubKeyFromSig, Hex.decode("0493fe448d38c77c212cce10c07ed37984c59bedac51219b70847429153063cfae0d2f42ba394ffe9d5d2d11b0c0f400ac04997c584c0ef6f2041cf20f8c2c446b"));
  // verify the sign message
  assertTrue(keyFromSig.verify(messageHash, sig));
}
origin: yggdrash/yggdrash

@Test
public void testVerifySignature5() {
  ECKey key = ECKey.fromPrivate(privateKey);
  byte[] messageHash = HashUtil.sha3(exampleMessage.getBytes());
  ECDSASignature signature = key.sign(messageHash);
  assertTrue(key.verify(messageHash, signature));
}
origin: yggdrash/yggdrash

@Test
public void test3() {
  BigInteger privKey = new BigInteger("cd244b3015703ddf545595da06ada5516628c5feadbf49dc66049c4b370cc5d8", 16);
  byte[] addr = ECKey.fromPrivate(privKey).getAddress();
  assertEquals("89b44e4d3c81ede05d0f5de8d1a68f754d73d997", Hex.toHexString(addr));
}
origin: yggdrash/yggdrash

/**
 * Get the public key.
 *
 * @return public key
 */
byte[] getPubKey() throws SignatureException {
  ECKey.ECDSASignature ecdsaSignature = new ECKey.ECDSASignature(this.signature);
  ECKey ecKeyPub = ECKey.signatureToKey(this.header.getHashForSigning(), ecdsaSignature);
  return ecKeyPub.getPubKey();
}
origin: yggdrash/yggdrash

@Test
public void testFromPrivateKey() {
  ECKey key = ECKey.fromPrivate(privateKey);
  assertTrue(key.isPubKeyCanonical());
  assertTrue(key.hasPrivKey());
  assertArrayEquals(pubKey, key.getPubKey());
}
origin: yggdrash/yggdrash

@Test
public void testIsPubKeyOnly() {
  ECKey key = ECKey.fromPublicOnly(pubKey);
  assertTrue(key.isPubKeyCanonical());
  assertTrue(key.isPubKeyOnly());
  assertArrayEquals(key.getPubKey(), pubKey);
}
origin: yggdrash/yggdrash

/**
 * Verify the signature with public key
 *
 * @param data signed data
 * @param signature  signature
 * @param hashed whether hashed data or not
 * @param pubKey public key for verifying
 * @return verification result
 */
public static boolean verify(byte[] data, byte[] signature, boolean hashed, byte[] pubKey) {
  ECKey.ECDSASignature ecdsaSignature = new ECKey.ECDSASignature(signature);
  byte[] hashedData = hashed ? data : HashUtil.sha3(data);
  ECKey ecKeyPub;
  try {
    ecKeyPub = ECKey.signatureToKey(hashedData, ecdsaSignature);
  } catch (SignatureException e) {
    logger.debug("Invalid signature" + e.getMessage());
    return false;
  }
  if (pubKey != null && !Arrays.equals(ecKeyPub.getPubKey(), pubKey)) {
    logger.debug("Invalid signature");
    return false;
  }
  return ecKeyPub.verify(hashedData, ecdsaSignature);
}
origin: yggdrash/yggdrash

public boolean verify() {
  ECKey.ECDSASignature ecdsaSignature = new ECKey.ECDSASignature(signature.getBytes());
  try {
    ECKey ecKeyPub = ECKey.signatureToKey(rawForSign.getBytes(), ecdsaSignature);
    if (ecKeyPub.verify(rawForSign.getBytes(), ecdsaSignature)) {
      return new Address(ecKeyPub.getAddress()).equals(owner);
    } else {
      return false;
    }
  } catch (SignatureException e) {
    throw new InvalidSignatureException(e);
  }
}
origin: yggdrash/yggdrash

private void testProviderRoundTrip(Provider provider) {
  ECKey key = new ECKey(provider, secureRandom);
  String message = "The quick brown fox jumps over the lazy dog.";
  byte[] input = HashUtil.sha3(message.getBytes());
  ECDSASignature sig = key.sign(input);
  assertTrue(sig.validateComponents());
  assertTrue(key.verify(input, sig));
}
io.yggdrash.common.cryptoECKey

Javadoc

Represents an elliptic curve public and (optionally) private key, usable for digital signatures but not encryption. Creating a new ECKey with the empty constructor will generate a new random keypair. Other static methods can be used when you already have the public or private parts. If you create a key with only the public part, you can check signatures but not create them.

The ECDSA algorithm supports key recovery in which a signature plus a couple of discriminator bits can be reversed to find the public key used to calculate it. This can be convenient when you have a message and a signature and want to find out who signed it, rather than requiring the user to provide the expected identity.

This code is borrowed from the bitcoinj project and altered to fit Ethereum.
See bitcoinj on GitHub.

Most used methods

  • <init>
    Generates an entirely new keypair with the given SecureRandom object. BouncyCastle will be used as
  • doSign
    Signs the given hash and returns the R and S components as BigIntegers and put them in ECDSASignatur
  • fromPrivate
    Creates an ECKey given the private key only.
  • fromPublicOnly
    Creates an ECKey that cannot be used for signing, only verifying signatures, from the given encoded
  • getAddress
    Gets the address form of the public key.
  • getNodeId
    Generates the NodeID based on this key, that is the public key without first format byte
  • getPrivKeyBytes
    Returns a 32 byte array containing the private key, or null if the key is encrypted or public only
  • getPubKey
    Gets the encoded public key value.
  • isPubKeyCanonical
    Returns true if the given pubkey is canonical, i.e. the correct length taking into wallet compressio
  • recoverPubBytesFromSignature
    Given the components of a signature and a selector value, recover and return the public key that gen
  • sign
    Takes the keccak hash (32 bytes) of data and returns the ECDSA signature
  • signatureToKey
    Compute the key that signed the given signature.
  • sign,
  • signatureToKey,
  • verify,
  • check,
  • computeAddress,
  • decompressKey,
  • equals,
  • extractPublicKey,
  • fromNodeId,
  • getPubKeyPoint

Popular in Java

  • Finding current android device location
  • compareTo (BigDecimal)
  • setRequestProperty (URLConnection)
  • scheduleAtFixedRate (ScheduledExecutorService)
  • Container (java.awt)
    A generic Abstract Window Toolkit(AWT) container object is a component that can contain other AWT co
  • Date (java.sql)
    A class which can consume and produce dates in SQL Date format. Dates are represented in SQL as yyyy
  • Arrays (java.util)
    This class contains various methods for manipulating arrays (such as sorting and searching). This cl
  • Response (javax.ws.rs.core)
    Defines the contract between a returned instance and the runtime when an application needs to provid
  • Options (org.apache.commons.cli)
    Main entry-point into the library. Options represents a collection of Option objects, which describ
  • BasicDataSource (org.apache.commons.dbcp)
    Basic implementation of javax.sql.DataSource that is configured via JavaBeans properties. This is no
  • Top 12 Jupyter Notebook extensions
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now