/** * Creates an ECKey that cannot be used for signing, only verifying signatures, from the given point. The * compression state of pub will be preserved. * * @param pub - * @return - */ public static ECKey fromPublicOnly(ECPoint pub) { return new ECKey(null, pub); }
/** * Account Constructor. * - generate wallet with new key */ public Account() { this.key = new ECKey(); this.address = this.key.getAddress(); }
/** * Creates an ECKey that cannot be used for signing, only verifying signatures, from the given encoded point. * The compression state of pub will be preserved. * * @param pub - * @return - */ public static ECKey fromPublicOnly(byte[] pub) { return new ECKey(null, CURVE.getCurve().decodePoint(pub)); }
/** * Creates an ECKey given the private key only. * * @param privKey - * @return - */ public static ECKey fromPrivate(BigInteger privKey) { return new ECKey(privKey, CURVE.getG().multiply(privKey)); }
@Test(expected = IllegalArgumentException.class) public void testPrivatePublicKeyBytesNoArg() { // Expecting an IllegalArgumentException for using only null-parameters new ECKey((BigInteger) null, null); }
@Test(expected = IllegalArgumentException.class) public void testInvalidPrivateKey() throws Exception { // Expecting an IllegalArgumentException for using an non EC private key PrivateKey privateKey = KeyPairGenerator.getInstance("DSA").generateKeyPair().getPrivate(); new ECKey( Security.getProvider("SunEC"), privateKey, ECKey.fromPublicOnly(pubKey).getPubKeyPoint()); }
@Test(expected = IllegalArgumentException.class) public void testSignIncorrectInputSize() { // Expecting an IllegalArgumentException for a non 32-byte input ECKey key = new ECKey(); String message = "The quick brown fox jumps over the lazy dog."; key.doSign(message.getBytes()); }
private void encryptKeyFileInit(ECKey key, String keyPath, String keyName, String password) throws IOException, InvalidCipherTextException { if (key == null) { key = new ECKey();
@Test @SuppressWarnings("UnstableApiUsage") public void testSValue() throws Exception { // Check that we never generate an S value that is larger than half the curve order. This avoids a malleability // issue that can allow someone to change a transaction [hash] without invalidating the signature. final int ITERATIONS = 10; ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(ITERATIONS)); List<ListenableFuture<ECKey.ECDSASignature>> sigFutures = Lists.newArrayList(); final ECKey key = new ECKey(); for (byte i = 0; i < ITERATIONS; i++) { final byte[] hash = HashUtil.sha3(new byte[] {i}); sigFutures.add(executor.submit(() -> key.doSign(hash))); } List<ECKey.ECDSASignature> sigs = Futures.allAsList(sigFutures).get(); for (ECKey.ECDSASignature signature : sigs) { assertTrue(signature.s.compareTo(ECKey.HALF_CURVE_ORDER) <= 0); } final ECKey.ECDSASignature duplicate = new ECKey.ECDSASignature(sigs.get(0).r, sigs.get(0).s); assertEquals(sigs.get(0), duplicate); assertEquals(sigs.get(0).hashCode(), duplicate.hashCode()); }
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)); }
@Test public void testGetPrivKeyBytes() { ECKey key = new ECKey(); assertNotNull(key.getPrivKeyBytes()); assertEquals(32, key.getPrivKeyBytes().length); }
@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); }
@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"); }
@Test public void testEqualsObject() { ECKey key0 = new ECKey(); ECKey key1 = ECKey.fromPrivate(privateKey); ECKey key2 = ECKey.fromPrivate(privateKey); assertNotEquals(key0, key1); assertEquals(key1, key1); assertEquals(key1, key2); }