/** * Creates a segwit scriptPubKey that sends to the given public key. */ public static Script createP2WPKHOutputScript(ECKey key) { checkArgument(key.isCompressed()); return createP2WPKHOutputScript(key.getPubKeyHash()); }
/** * Creates a segwit scriptPubKey that sends to the given public key. */ public static Script createP2WPKHOutputScript(ECKey key) { checkArgument(key.isCompressed()); return createP2WPKHOutputScript(key.getPubKeyHash()); }
/** * Removes the given key from the keychain. Be very careful with this - losing a private key <b>destroys the * money associated with it</b>. * @return Whether the key was removed or not. */ public boolean removeKey(ECKey key) { lock.lock(); try { boolean a = hashToKeys.remove(ByteString.copyFrom(key.getPubKeyHash())) != null; boolean b = pubkeyToKeys.remove(ByteString.copyFrom(key.getPubKey())) != null; boolean c = scriptHashToKeys.remove(ByteString.copyFrom(key.getSegwitHash())) != null; checkState((useSegwit && !key.isCompressed()) || a == b); // Should be in both maps or neither. return a; } finally { lock.unlock(); } }
/** * 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()); }
/** * 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()); }
/** * 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()); }
/** * 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()); }
private void importKeyLocked(ECKey key) { if (hashToKeys.isEmpty()) { isWatching = key.isWatching(); } else { if (key.isWatching() && !isWatching) throw new IllegalArgumentException("Key is watching but chain is not"); if (!key.isWatching() && isWatching) throw new IllegalArgumentException("Key is not watching but chain is"); } ECKey previousKey = pubkeyToKeys.put(ByteString.copyFrom(key.getPubKey()), key); hashToKeys.put(ByteString.copyFrom(key.getPubKeyHash()), key); if (useSegwit) { if (key.isCompressed()) { // only compressed public keys are accepted in P2WPKH and P2WSH, see BIP 143 scriptHashToKeys.put(ByteString.copyFrom(key.getSegwitHash()), key); } } checkState(previousKey == null); }
/** * Create a decrypted private key with the keyCrypter and AES key supplied. Note that if the aesKey is wrong, this * has some chance of throwing KeyCrypterException due to the corrupted padding that will result, but it can also * just yield a garbage key. * * @param keyCrypter The keyCrypter that specifies exactly how the decrypted bytes are created. * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached). */ public ECKey decrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(keyCrypter); // Check that the keyCrypter matches the one used to encrypt the keys, if set. if (this.keyCrypter != null && !this.keyCrypter.equals(keyCrypter)) throw new KeyCrypterException("The keyCrypter being used to decrypt the key is different to the one that was used to encrypt it"); checkState(encryptedPrivateKey != null, "This key is not encrypted"); byte[] unencryptedPrivateKey = keyCrypter.decrypt(encryptedPrivateKey, aesKey); ECKey key = ECKey.fromPrivate(unencryptedPrivateKey); if (!isCompressed()) key = key.decompress(); if (!Arrays.equals(key.getPubKey(), getPubKey())) throw new KeyCrypterException("Provided AES key is wrong"); key.setCreationTimeSeconds(creationTimeSeconds); return key; }
/** * Create a decrypted private key with the keyCrypter and AES key supplied. Note that if the aesKey is wrong, this * has some chance of throwing KeyCrypterException due to the corrupted padding that will result, but it can also * just yield a garbage key. * * @param keyCrypter The keyCrypter that specifies exactly how the decrypted bytes are created. * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached). */ public ECKey decrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(keyCrypter); // Check that the keyCrypter matches the one used to encrypt the keys, if set. if (this.keyCrypter != null && !this.keyCrypter.equals(keyCrypter)) throw new KeyCrypterException("The keyCrypter being used to decrypt the key is different to the one that was used to encrypt it"); checkState(encryptedPrivateKey != null, "This key is not encrypted"); byte[] unencryptedPrivateKey = keyCrypter.decrypt(encryptedPrivateKey, aesKey); ECKey key = ECKey.fromPrivate(unencryptedPrivateKey); if (!isCompressed()) key = key.decompress(); if (!Arrays.equals(key.getPubKey(), getPubKey())) throw new KeyCrypterException("Provided AES key is wrong"); key.setCreationTimeSeconds(creationTimeSeconds); return key; }
/** * Create a decrypted private key with the keyCrypter and AES key supplied. Note that if the aesKey is wrong, this * has some chance of throwing KeyCrypterException due to the corrupted padding that will result, but it can also * just yield a garbage key. * * @param keyCrypter The keyCrypter that specifies exactly how the decrypted bytes are created. * @param aesKey The KeyParameter with the AES encryption key (usually constructed with keyCrypter#deriveKey and cached). */ public ECKey decrypt(KeyCrypter keyCrypter, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(keyCrypter); // Check that the keyCrypter matches the one used to encrypt the keys, if set. if (this.keyCrypter != null && !this.keyCrypter.equals(keyCrypter)) throw new KeyCrypterException("The keyCrypter being used to decrypt the key is different to the one that was used to encrypt it"); checkState(encryptedPrivateKey != null, "This key is not encrypted"); byte[] unencryptedPrivateKey = keyCrypter.decrypt(encryptedPrivateKey, aesKey); ECKey key = ECKey.fromPrivate(unencryptedPrivateKey); if (!isCompressed()) key = key.decompress(); if (!Arrays.equals(key.getPubKey(), getPubKey())) throw new KeyCrypterException("Provided AES key is wrong"); key.setCreationTimeSeconds(creationTimeSeconds); return key; }
/** Inserts the given key and equivalent hashed form (for the address). */ public synchronized void insert(ECKey key, boolean useSegwit) { insert(key.getPubKey()); if (useSegwit) { if (key.isCompressed()) { Script p2sh = key.getSegwitScript(); for (ScriptChunk chunk : p2sh.getChunks()) { if (!chunk.isOpCode() && chunk.data != null && chunk.data.length >= 8) { insert(chunk.data); } } } } else { insert(key.getPubKeyHash()); } } public synchronized void insert(ECKey key) {
@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())); }
@Test public void test_Mini_KeyFormat_shouldReturnCompressed_byDefault() throws Exception { //Arrange String compressedByDefault = String.format(balanceApiResponse, miniUncompressedAddress, 0, miniCompressedAddress,0); mockInterceptor.setResponseString(compressedByDefault); //Act String format = privateKeyFactory.getFormat(miniKey); ECKey ecKey = privateKeyFactory.getKey(format, miniKey); Address address = ecKey.toAddress(MainNetParams.get()); //Assert Assert.assertEquals(miniCompressedAddress, address.toString()); Assert.assertTrue(ecKey.isCompressed()); }
@Test public void test_Mini_KeyFormat_shouldReturnCompressed_ifBothHaveFunds() throws Exception { //Arrange String compressedWithBalance = String.format(balanceApiResponse, miniUncompressedAddress, 0, miniCompressedAddress ,1000); mockInterceptor.setResponseString(compressedWithBalance); //Act String format = privateKeyFactory.getFormat(miniKey); ECKey ecKey = privateKeyFactory.getKey(format, miniKey); Address address = ecKey.toAddress(MainNetParams.get()); //Assert Assert.assertEquals(miniCompressedAddress, address.toString()); Assert.assertTrue(ecKey.isCompressed()); }
@Test public void test_HEX_KeyFormat_shouldReturnCompressed_byDefault() throws Exception { //Arrange String compressedByDefault = String.format(balanceApiResponse, hexUncompressedAddress, 0, hexCompressedAddress ,0); mockInterceptor.setResponseString(compressedByDefault); //Act String format = privateKeyFactory.getFormat(hexKey); ECKey ecKey = privateKeyFactory.getKey(format, hexKey); Address address = ecKey.toAddress(MainNetParams.get()); //Assert Assert.assertEquals(hexCompressedAddress, address.toString()); Assert.assertTrue(ecKey.isCompressed()); Assert.assertEquals(PrivateKeyFactory.HEX, format); }
@Test public void test_HEX_KeyFormat_shouldReturnCompressed_ifBothHaveFunds() throws Exception { //Arrange String compressedWithBalance = String.format(balanceApiResponse, hexUncompressedAddress, 0, hexCompressedAddress ,1000); mockInterceptor.setResponseString(compressedWithBalance); //Act String format = privateKeyFactory.getFormat(hexKey); ECKey ecKey = privateKeyFactory.getKey(format, hexKey); Address address = ecKey.toAddress(MainNetParams.get()); //Assert Assert.assertEquals(hexCompressedAddress, address.toString()); Assert.assertTrue(ecKey.isCompressed()); Assert.assertEquals(PrivateKeyFactory.HEX, format); }
@Test public void test_Mini_KeyFormat_shouldReturnUncompressed_ifHasBalance() throws Exception { //Arrange String uncompressedWithBalance = String.format(balanceApiResponse, miniUncompressedAddress, 1000, miniCompressedAddress ,0); mockInterceptor.setResponseString(uncompressedWithBalance); //Act String format = privateKeyFactory.getFormat(miniKey); ECKey ecKey = privateKeyFactory.getKey(format, miniKey); Address address = ecKey.toAddress(MainNetParams.get()); //Assert if(PersistentUrls.getInstance().getCurrentNetworkParams() instanceof MainNetParams) { Assert.assertEquals(miniUncompressedAddress, address.toString()); Assert.assertTrue(!ecKey.isCompressed()); } }
@Test public void test_HEX_KeyFormat_shouldReturnUncompressed_ifHasBalance() throws Exception { //Arrange String uncompressedWithBalance = String.format(balanceApiResponse, hexUncompressedAddress, 1000 ,hexCompressedAddress, 0); mockInterceptor.setResponseString(uncompressedWithBalance); //Act String format = privateKeyFactory.getFormat(hexKey); ECKey ecKey = privateKeyFactory.getKey(format, hexKey); Address address = ecKey.toAddress(MainNetParams.get()); //Assert Assert.assertEquals(PrivateKeyFactory.HEX, format); if(PersistentUrls.getInstance().getCurrentNetworkParams() instanceof MainNetParams) { Assert.assertEquals(hexUncompressedAddress, address.toString()); Assert.assertTrue(!ecKey.isCompressed()); } }
@Test public void testPublicKeysAreEqual() { ECKey key = new ECKey(); ECKey pubKey1 = ECKey.fromPublicOnly(key.getPubKeyPoint()); assertTrue(pubKey1.isCompressed()); ECKey pubKey2 = pubKey1.decompress(); assertEquals(pubKey1, pubKey2); assertEquals(pubKey1.hashCode(), pubKey2.hashCode()); } }