/** * Compute the key that signed the given signature. * * @param messageHash 32-byte hash of message * @param sig - * @return ECKey */ public static ECKey signatureToKey(byte[] messageHash, ECDSASignature sig) throws SignatureException { final byte[] keyBytes = signatureToKeyBytes(messageHash, sig); return ECKey.fromPublicOnly(keyBytes); }
/** * Recover the public key from an encoded node id. * * @param nodeId a 64-byte X,Y point pair */ public static ECKey fromNodeId(byte[] nodeId) { check(nodeId.length == 64, "Expected a 64 byte node id"); byte[] pubBytes = new byte[65]; System.arraycopy(nodeId, 0, pubBytes, 1, nodeId.length); pubBytes[0] = 0x04; // uncompressed return ECKey.fromPublicOnly(pubBytes); }
/** * 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); }
/** * Compute the key that signed the given signature. * * @param messageHash 32-byte hash of message * @param signatureBin Binary encoded signature * @return ECKey */ public static ECKey signatureToKey(byte[] messageHash, byte[] signatureBin) throws SignatureException { final byte[] keyBytes = signatureToKeyBytes(messageHash, signatureBin); return ECKey.fromPublicOnly(keyBytes); }
/** * @param recId Which possible key to recover. * @param sig the R and S components of the signature, wrapped. * @param messageHash Hash of the data that was signed. * @return ECKey */ @Nullable public static ECKey recoverFromSignature(int recId, ECDSASignature sig, byte[] messageHash) { final byte[] pubBytes = recoverPubBytesFromSignature(recId, sig, messageHash); if (pubBytes == null) { return null; } else { return ECKey.fromPublicOnly(pubBytes); } }
@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 public void testVerifySignature1() { ECKey key = ECKey.fromPublicOnly(pubKey); BigInteger r = new BigInteger("95350169487015575001444507567851457309689354890536757640816472151471942911739"); BigInteger s = new BigInteger("53263359985948886716508128220321578640585687230908938312863706887350789467339"); ECDSASignature sig = ECDSASignature.fromComponents(r.toByteArray(), s.toByteArray(), (byte) 28); assertTrue(key.verify(HashUtil.sha3(exampleMessage.getBytes()), sig)); }
@Test(expected = ECKey.MissingPrivateKeyException.class) public void testSignWithPubKeyOnly() { // Expecting an MissingPrivateKeyException for a public only ECKey ECKey key = ECKey.fromPublicOnly(pubKey); String message = "The quick brown fox jumps over the lazy dog."; byte[] input = HashUtil.sha3(message.getBytes()); key.doSign(input); }
@Test public void testGetAddress() { ECKey key = ECKey.fromPublicOnly(pubKey); assertArrayEquals(Hex.decode(address), key.getAddress()); }
@Test public void testNodeId() { ECKey key = ECKey.fromPublicOnly(pubKey); assertEquals(key, ECKey.fromNodeId(key.getNodeId())); } }
@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 testIsPubKeyOnly() { ECKey key = ECKey.fromPublicOnly(pubKey); assertTrue(key.isPubKeyCanonical()); assertTrue(key.isPubKeyOnly()); assertArrayEquals(key.getPubKey(), pubKey); }