public VerificationKeyImpl(byte[] ecKey, NetworkParameters params) { this.ecKey = ECKey.fromPublicOnly(ecKey); this.vKey = this.ecKey.getPubKey(); this.params = params; this.address = new AddressImpl(this.ecKey.toAddress(params)); }
/** * Creates an ECKey that simply trusts the caller to ensure that point is really the result of multiplying the * generator point by the private key. This is used to speed things up when you know you have the right values * already. The compression state of pub will be preserved. */ public static ECKey fromPrivateAndPrecalculatedPublic(BigInteger priv, ECPoint pub) { return new ECKey(priv, pub); }
private TypedKey getFromMiniKey() { final ECKey key = ECKey.fromPrivate(content).decompress(); return new TypedKey(CoinID.getSupportedCoins(), key); } }
/** * A wrapper for {@link #getPrivKeyBytes()} that returns null if the private key bytes are missing or would have * to be derived (for the HD key case). */ @Override @Nullable public byte[] getSecretBytes() { if (hasPrivKey()) return getPrivKeyBytes(); else return null; }
/** * Exports the private key in the form used by Bitcoin Core's "dumpprivkey" and "importprivkey" commands. Use * the {@link org.bitcoinj.core.DumpedPrivateKey#toString()} method to get the string. * * @param params The network this key is intended for use on. * @return Private key bytes as a {@link DumpedPrivateKey}. * @throws IllegalStateException if the private key is not available. */ public DumpedPrivateKey getPrivateKeyEncoded(NetworkParameters params) { return new DumpedPrivateKey(params, getPrivKeyBytes(), isCompressed()); }
public Keys getKeys() { ECKey key = new ECKey(); byte[] address = key.getPubKey(); String addressAsString = key.toAddress(getNetworkParameters()).toString(); byte[] publicKey = key.getPubKey(); String publicKeyAsHexString = key.getPublicKeyAsHex(); byte[] privateKey = key.getPrivKeyBytes(); String privateKeyAsHexString = key.getPrivateKeyAsHex(); String addressAsStringWithPrivate = key.toStringWithPrivate(getNetworkParameters()).toString(); return new Keys() .setAddress(address) .setAddressAsString(addressAsString) .setPublicKey(publicKey) .setPublicKeyAsHexString(publicKeyAsHexString) .setPrivateKey(privateKey) .setPrivateKeyAsHexString(privateKeyAsHexString) .setAddressAsStringWithPrivate(addressAsStringWithPrivate); }
private String toString(boolean includePrivate, @Nullable KeyParameter aesKey, NetworkParameters params) { final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(this).omitNullValues(); helper.add("pub HEX", getPublicKeyAsHex()); if (includePrivate) { ECKey decryptedKey = isEncrypted() ? decrypt(checkNotNull(aesKey)) : this; try { helper.add("priv HEX", decryptedKey.getPrivateKeyAsHex()); helper.add("priv WIF", decryptedKey.getPrivateKeyAsWiF(params)); } catch (IllegalStateException e) { // TODO: Make hasPrivKey() work for deterministic keys and fix this. } catch (Exception e) { final String message = e.getMessage(); helper.add("priv EXCEPTION", e.getClass().getName() + (message != null ? ": " + message : "")); } } if (creationTimeSeconds > 0) helper.add("creationTimeSeconds", creationTimeSeconds); helper.add("keyCrypter", keyCrypter); if (includePrivate) helper.add("encryptedPrivateKey", encryptedPrivateKey); helper.add("isEncrypted", isEncrypted()); helper.add("isPubKeyOnly", isPubKeyOnly()); return helper.toString(); }
@Before public void setUp() throws Exception { ecKey = new ECKey(); pub = ECKey.fromPublicOnly(ecKey.getPubKey()); //ek = new EncryptionKeyImpl(pub); address = new AddressImpl("myGgn8UojMsyqn6KGQLEbVbpYSePcKfawG"); System.out.println("ecKey pubHex " + ecKey.getPublicKeyAsHex()); System.out.println("ecKey privHex " + ecKey.getPrivateKeyAsHex()); System.out.println("pub from ecKey " + pub); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ECIES",new BouncyCastleProvider()); keyPairGenerator.initialize(new ECGenParameterSpec("secp256k1")); KeyPair recipientKeyPair = keyPairGenerator.generateKeyPair(); pubKey = recipientKeyPair.getPublic(); privKey = recipientKeyPair.getPrivate(); ek = new EncryptionKeyImpl(pubKey); System.out.println("privKey " + privKey); //testkey creation byte[] privbytes = Hex.decodeHex("076edbacad6ba3572be68131900da4e2a3b72f273bb2184c304282bcac117838".toCharArray()); tkey = ECKey.fromPrivate(privbytes, false); System.out.println("\nhardcoded testKey (tkey) " + tkey); // PrivateKey privateKey = BitcoinCrypto.loadPrivateKey(tkey.getPrivateKeyAsHex()); System.out.println("testKey (tkey) priv: " + tkey.getPrivateKeyAsHex()); // System.out.println("testKey priv loaded: " + privateKey.toString()); }
@Test public void testKeyPairRoundtrip() throws Exception { byte[] privkeyASN1 = HEX.decode( "3082011302010104205c0b98e524ad188ddef35dc6abba13c34a351a05409e5d285403718b93336a4aa081a53081a2020101302c06072a8648ce3d0101022100fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f300604010004010704410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8022100fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141020101a144034200042af7a2aafe8dafd7dc7f9cfb58ce09bda7dce28653ab229b98d1d3d759660c672dd0db18c8c2d76aa470448e876fc2089ab1354c01a6e72cefc50915f4a963ee"); ECKey decodedKey = ECKey.fromASN1(privkeyASN1); // Now re-encode and decode the ASN.1 to see if it is equivalent (it does not produce the exact same byte // sequence, some integers are padded now). ECKey roundtripKey = ECKey.fromPrivateAndPrecalculatedPublic(decodedKey.getPrivKey(), decodedKey.getPubKeyPoint()); for (ECKey key : new ECKey[] {decodedKey, roundtripKey}) { byte[] message = reverseBytes(HEX.decode( "11da3761e86431e4a54c176789e41f1651b324d240d599a7067bee23d328ec2a")); byte[] output = key.sign(Sha256Hash.wrap(message)).encodeToDER(); assertTrue(key.verify(message, output)); output = HEX.decode( "304502206faa2ebc614bf4a0b31f0ce4ed9012eb193302ec2bcaccc7ae8bb40577f47549022100c73a1a1acc209f3f860bf9b9f5e13e9433db6f8b7bd527a088a0e0cd0a4c83e9"); assertTrue(key.verify(message, output)); } // Try to sign with one key and verify with the other. byte[] message = reverseBytes(HEX.decode( "11da3761e86431e4a54c176789e41f1651b324d240d599a7067bee23d328ec2a")); assertTrue(roundtripKey.verify(message, decodedKey.sign(Sha256Hash.wrap(message)).encodeToDER())); assertTrue(decodedKey.verify(message, roundtripKey.sign(Sha256Hash.wrap(message)).encodeToDER())); // Verify bytewise equivalence of public keys (i.e. compression state is preserved) ECKey key = new ECKey(); ECKey key2 = ECKey.fromASN1(key.toASN1()); assertArrayEquals(key.getPubKey(), key2.getPubKey()); }
@Test public void testKeys() throws Exception { for (int i = 0 ; i < 20 ; i++) { myKey = new ECKey(); myAddress = myKey.toAddress(PARAMS); myWallet = new Wallet(PARAMS); myWallet.importKey(myKey); Wallet wallet1 = roundTrip(myWallet); assertArrayEquals(myKey.getPubKey(), wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPubKey()); assertArrayEquals(myKey.getPrivKeyBytes(), wallet1.findKeyFromPubHash(myKey.getPubKeyHash()).getPrivKeyBytes()); } }
@Test public void testSomething() { String msg = "hello world"; Sha256Hash hash = Sha256Hash.of(msg.getBytes()); ECKey signingKey = new ECKey(); ECKey verificationKey = ECKey.fromPublicOnly(signingKey.getPubKeyPoint()); ECKey.ECDSASignature sig = signingKey.sign(hash); boolean isVerified = verificationKey.verify(hash, sig); assertTrue("msg " + msg + " can not be signed with " + Arrays.toString(sig.encodeToDER()), isVerified); }
@Test public void roundTripDumpedPrivKey() throws Exception { ECKey key = new ECKey(); assertTrue(key.isCompressed()); NetworkParameters params = UnitTestParams.get(); String base58 = key.getPrivateKeyEncoded(params).toString(); ECKey key2 = DumpedPrivateKey.fromBase58(params, base58).getKey(); assertTrue(key2.isCompressed()); assertTrue(Arrays.equals(key.getPrivKeyBytes(), key2.getPrivKeyBytes())); assertTrue(Arrays.equals(key.getPubKey(), key2.getPubKey())); }
public void checkAuthenticationMessage (AuthenticationMessage authentication, ClientObject node) throws NoSuchProviderException, NoSuchAlgorithmException { ECKey ecKey = ECKey.fromPublicOnly(authentication.pubKeyServer); if (node.pubKeyClient != null) { //Must be an outgoing connection, check if the nodeKey is what we expect it to be if (!Arrays.equals(ecKey.getPubKey(), node.pubKeyClient.getPubKey())) { //We connected to the wrong node? System.out.println("Connected to wrong node? Expected: " + node.pubKeyClient.getPublicKeyAsHex() + ". Is: " + ecKey.getPublicKeyAsHex()); authenticationFailed(); } } node.pubKeyClient = ecKey; ECKey pubKeyClient = node.pubKeyClient; ECKey pubKeyTempServer = node.ephemeralKeyServer; byte[] data = new byte[pubKeyClient.getPubKey().length + pubKeyTempServer.getPubKey().length]; System.arraycopy(pubKeyClient.getPubKey(), 0, data, 0, pubKeyClient.getPubKey().length); System.arraycopy(pubKeyTempServer.getPubKey(), 0, data, pubKeyClient.getPubKey().length, pubKeyTempServer.getPubKey().length); if (!CryptoTools.verifySignature(pubKeyClient, data, authentication.signature)) { System.out.println("Node was not able to authenticate.."); authenticationFailed(); } }
@Test public void clear() throws Exception { ECKey unencryptedKey = new ECKey(); ECKey encryptedKey = (new ECKey()).encrypt(keyCrypter, keyCrypter.deriveKey(PASSWORD1)); checkSomeBytesAreNonZero(unencryptedKey.getPrivKeyBytes()); // The encryptedPrivateKey should be null in an unencrypted ECKey anyhow but check all the same. assertTrue(unencryptedKey.getEncryptedPrivateKey() == null); checkSomeBytesAreNonZero(encryptedKey.getSecretBytes()); checkSomeBytesAreNonZero(encryptedKey.getEncryptedPrivateKey().encryptedBytes); checkSomeBytesAreNonZero(encryptedKey.getEncryptedPrivateKey().initialisationVector); }
@Override public Address freshReceiveAddress () { return keyList.get(0).toAddress(Constants.getNetwork()); } }
/** Create a fake transaction, without change. */ public static Transaction createFakeTxWithoutChange(final NetworkParameters params, final TransactionOutput output) { Transaction prevTx = FakeTxBuilder.createFakeTx(params, Coin.COIN, new ECKey().toAddress(params)); Transaction tx = new Transaction(params); tx.addOutput(output); tx.addInput(prevTx.getOutput(0)); return tx; }
@Test public void testToString() throws Exception { ECKey key = ECKey.fromPrivate(BigInteger.TEN).decompress(); // An example private key. NetworkParameters params = MainNetParams.get(); assertEquals("ECKey{pub HEX=04a0434d9e47f3c86235477c7b1ae6ae5d3442d49b1943c2b752a68e2a47e247c7893aba425419bc27a3b6c7e693a24c696f794c2ed877a1593cbee53b037368d7, isEncrypted=false, isPubKeyOnly=false}", key.toString()); assertEquals("ECKey{pub HEX=04a0434d9e47f3c86235477c7b1ae6ae5d3442d49b1943c2b752a68e2a47e247c7893aba425419bc27a3b6c7e693a24c696f794c2ed877a1593cbee53b037368d7, priv HEX=000000000000000000000000000000000000000000000000000000000000000a, priv WIF=5HpHagT65TZzG1PH3CSu63k8DbpvD8s5ip4nEB3kEsreBoNWTw6, isEncrypted=false, isPubKeyOnly=false}", key.toStringWithPrivate(params)); }
/** * Constructs a key that has an encrypted private component. The given object wraps encrypted bytes and an * initialization vector. Note that the key will not be decrypted during this call: the returned ECKey is * unusable for signing unless a decryption key is supplied. */ public static ECKey fromEncrypted(EncryptedData encryptedPrivateKey, KeyCrypter crypter, byte[] pubKey) { ECKey key = fromPublicOnly(pubKey); key.encryptedPrivateKey = checkNotNull(encryptedPrivateKey); key.keyCrypter = checkNotNull(crypter); return key; }
/** * Signs the given hash and returns the R and S components as BigIntegers. In the Bitcoin protocol, they are * usually encoded using ASN.1 format, so you want {@link org.bitcoinj.core.ECKey.ECDSASignature#toASN1()} * instead. However sometimes the independent components can be useful, for instance, if you're going to do * further EC maths on them. * @throws KeyCrypterException if this ECKey doesn't have a private part. */ public ECDSASignature sign(Sha256Hash input) throws KeyCrypterException { return sign(input, null); }