diff --git a/core/src/main/java/com/google/bitcoin/core/Wallet.java b/core/src/main/java/com/google/bitcoin/core/Wallet.java index 42035332..a8680e69 100644 --- a/core/src/main/java/com/google/bitcoin/core/Wallet.java +++ b/core/src/main/java/com/google/bitcoin/core/Wallet.java @@ -177,10 +177,8 @@ public class Wallet implements Serializable, BlockChainListener { */ final Map dead; - /** - * A list of public/private EC keys owned by this user. Access it using addKey[s], hasKey[s] and findPubKeyFromHash. - */ - public ArrayList keychain; + // A list of public/private EC keys owned by this user. Access it using addKey[s], hasKey[s] and findPubKeyFromHash. + private ArrayList keychain; private final NetworkParameters params; @@ -379,7 +377,7 @@ public class Wallet implements Serializable, BlockChainListener { /** * Returns a snapshot of the keychain. This view is not live. */ - public Iterable getKeys() { + public List getKeys() { lock.lock(); try { return new ArrayList(keychain); @@ -387,6 +385,20 @@ public class Wallet implements Serializable, BlockChainListener { lock.unlock(); } } + + /** + * Removes the given key from the keychain. Be very careful with this - losing a private key destroys the + * money associated with it. + * @return Whether the key was removed or not. + */ + public boolean removeKey(ECKey key) { + lock.lock(); + try { + return keychain.remove(key); + } finally { + lock.unlock(); + } + } /** * Returns the number of keys in the keychain. diff --git a/core/src/test/java/com/google/bitcoin/core/BlockChainTest.java b/core/src/test/java/com/google/bitcoin/core/BlockChainTest.java index 6877877d..d33e6486 100644 --- a/core/src/test/java/com/google/bitcoin/core/BlockChainTest.java +++ b/core/src/test/java/com/google/bitcoin/core/BlockChainTest.java @@ -69,7 +69,7 @@ public class BlockChainTest { resetBlockStore(); chain = new BlockChain(unitTestParams, wallet, blockStore); - coinbaseTo = wallet.keychain.get(0).toAddress(unitTestParams); + coinbaseTo = wallet.getKeys().get(0).toAddress(unitTestParams); } @Test @@ -100,7 +100,7 @@ public class BlockChainTest { // Quick check that we can actually receive coins. Transaction tx1 = createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), - wallet.keychain.get(0).toAddress(unitTestParams)); + wallet.getKeys().get(0).toAddress(unitTestParams)); Block b1 = createFakeBlock(blockStore, tx1).block; chain.add(b1); assertTrue(wallet.getBalance().compareTo(BigInteger.ZERO) > 0); @@ -112,7 +112,7 @@ public class BlockChainTest { // there isn't any such tx present (as an optimization). Transaction tx1 = createFakeTx(unitTestParams, Utils.toNanoCoins(1, 0), - wallet.keychain.get(0).toAddress(unitTestParams)); + wallet.getKeys().get(0).toAddress(unitTestParams)); Block b1 = createFakeBlock(blockStore, tx1).block; chain.add(b1); resetBlockStore(); @@ -272,7 +272,7 @@ public class BlockChainTest { Address addressToSendTo = receiveKey.toAddress(unitTestParams); // Create a block, sending the coinbase to the coinbaseTo address (which is in the wallet). - Block b1 = unitTestParams.genesisBlock.createNextBlockWithCoinbase(wallet.keychain.get(0).getPubKey()); + Block b1 = unitTestParams.genesisBlock.createNextBlockWithCoinbase(wallet.getKeys().get(0).getPubKey()); chain.add(b1); // Check a transaction has been received. diff --git a/core/src/test/java/com/google/bitcoin/core/ChainSplitTest.java b/core/src/test/java/com/google/bitcoin/core/ChainSplitTest.java index 18795bbd..4826f158 100644 --- a/core/src/test/java/com/google/bitcoin/core/ChainSplitTest.java +++ b/core/src/test/java/com/google/bitcoin/core/ChainSplitTest.java @@ -47,8 +47,8 @@ public class ChainSplitTest { wallet.addKey(new ECKey()); wallet.addKey(new ECKey()); chain = new BlockChain(unitTestParams, wallet, new MemoryBlockStore(unitTestParams)); - coinsTo = wallet.keychain.get(0).toAddress(unitTestParams); - coinsTo2 = wallet.keychain.get(1).toAddress(unitTestParams); + coinsTo = wallet.getKeys().get(0).toAddress(unitTestParams); + coinsTo2 = wallet.getKeys().get(1).toAddress(unitTestParams); someOtherGuy = new ECKey().toAddress(unitTestParams); } @@ -496,7 +496,7 @@ public class ChainSplitTest { // The second block contains a coinbase transaction that spends to coinTo2. // The third block contains a normal transaction that spends to coinTo. Block b1 = unitTestParams.genesisBlock.createNextBlock(coinsTo); - Block b2 = b1.createNextBlockWithCoinbase(wallet.keychain.get(1).getPubKey()); + Block b2 = b1.createNextBlockWithCoinbase(wallet.getKeys().get(1).getPubKey()); Block b3 = b2.createNextBlock(coinsTo); log.debug("Adding block b1"); diff --git a/core/src/test/java/com/google/bitcoin/core/LazyParseByteCacheTest.java b/core/src/test/java/com/google/bitcoin/core/LazyParseByteCacheTest.java index 61f5c41f..ee88466d 100644 --- a/core/src/test/java/com/google/bitcoin/core/LazyParseByteCacheTest.java +++ b/core/src/test/java/com/google/bitcoin/core/LazyParseByteCacheTest.java @@ -85,11 +85,11 @@ public class LazyParseByteCacheTest { Transaction tx1 = createFakeTx(unitTestParams, Utils.toNanoCoins(2, 0), - wallet.keychain.get(0).toAddress(unitTestParams)); + wallet.getKeys().get(0).toAddress(unitTestParams)); //add a second input so can test granularity of byte cache. Transaction prevTx = new Transaction(unitTestParams); - TransactionOutput prevOut = new TransactionOutput(unitTestParams, prevTx, Utils.toNanoCoins(1, 0), wallet.keychain.get(0).toAddress(unitTestParams)); + TransactionOutput prevOut = new TransactionOutput(unitTestParams, prevTx, Utils.toNanoCoins(1, 0), wallet.getKeys().get(0).toAddress(unitTestParams)); prevTx.addOutput(prevOut); // Connect it. tx1.addInput(prevOut); diff --git a/examples/src/main/java/com/google/bitcoin/examples/PingService.java b/examples/src/main/java/com/google/bitcoin/examples/PingService.java index 2d84667a..f7568cec 100644 --- a/examples/src/main/java/com/google/bitcoin/examples/PingService.java +++ b/examples/src/main/java/com/google/bitcoin/examples/PingService.java @@ -57,7 +57,6 @@ import static com.google.common.base.Preconditions.checkNotNull; */ public class PingService { private final PeerGroup peerGroup; - private final BlockChain chain; private final BlockStore blockStore; private final File walletFile; @@ -77,7 +76,7 @@ public class PingService { w = Wallet.loadFromFile(walletFile); } catch (IOException e) { w = new Wallet(params); - w.keychain.add(new ECKey()); + w.addKey(new ECKey()); w.saveToFile(walletFile); } final Wallet wallet = w; @@ -95,7 +94,7 @@ public class PingService { CheckpointManager.checkpoint(params, stream, blockStore, key.getCreationTimeSeconds()); } } - chain = new BlockChain(params, wallet, blockStore); + BlockChain chain = new BlockChain(params, wallet, blockStore); // Connect to the localhost node. One minute timeout since we won't try any other peers System.out.println("Connecting ..."); peerGroup = new PeerGroup(params, chain); diff --git a/examples/src/main/java/com/google/bitcoin/examples/toywallet/ToyWallet.java b/examples/src/main/java/com/google/bitcoin/examples/toywallet/ToyWallet.java index 43ae4b7d..6ad66f17 100644 --- a/examples/src/main/java/com/google/bitcoin/examples/toywallet/ToyWallet.java +++ b/examples/src/main/java/com/google/bitcoin/examples/toywallet/ToyWallet.java @@ -151,7 +151,7 @@ public class ToyWallet { wallet.saveToFile(walletFile); freshWallet = true; } - System.out.println("Send to: " + wallet.keychain.get(0).toAddress(params)); + System.out.println("Send to: " + wallet.getKeys().get(0).toAddress(params)); System.out.println(wallet); wallet.autosaveToFile(walletFile, 500, TimeUnit.MILLISECONDS, null); @@ -238,7 +238,7 @@ public class ToyWallet { } private void setupWindow(JFrame window) { - final Address address = wallet.keychain.get(0).toAddress(params); + final Address address = wallet.getKeys().get(0).toAddress(params); JLabel instructions = new JLabel( "Broadcast transactions appear below. Watch them gain confidence.
" + "Send coins to: " + address + " (click to place on clipboard)"); diff --git a/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java b/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java index 00b6c24e..9ce1dfc3 100644 --- a/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java +++ b/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java @@ -739,7 +739,7 @@ public class WalletTool { System.err.println("Wallet does not seem to contain that key."); return; } - wallet.keychain.remove(key); + wallet.removeKey(key); } private static void dumpWallet() throws BlockStoreException {