3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 14:54:15 +00:00

Wallet: make keychain private and add a remove method.

This commit is contained in:
Mike Hearn 2013-04-22 13:45:38 +02:00
parent 5112a94955
commit f88bdc5fe3
7 changed files with 31 additions and 20 deletions

View File

@ -177,10 +177,8 @@ public class Wallet implements Serializable, BlockChainListener {
*/
final Map<Sha256Hash, Transaction> dead;
/**
* A list of public/private EC keys owned by this user. Access it using addKey[s], hasKey[s] and findPubKeyFromHash.
*/
public ArrayList<ECKey> keychain;
// A list of public/private EC keys owned by this user. Access it using addKey[s], hasKey[s] and findPubKeyFromHash.
private ArrayList<ECKey> 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<ECKey> getKeys() {
public List<ECKey> getKeys() {
lock.lock();
try {
return new ArrayList<ECKey>(keychain);
@ -388,6 +386,20 @@ public class Wallet implements Serializable, BlockChainListener {
}
}
/**
* 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 {
return keychain.remove(key);
} finally {
lock.unlock();
}
}
/**
* Returns the number of keys in the keychain.
*/

View File

@ -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.

View File

@ -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");

View File

@ -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);

View File

@ -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);

View File

@ -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(
"<html>Broadcast transactions appear below. Watch them gain confidence.<br>" +
"Send coins to: <b>" + address + "</b> <i>(click to place on clipboard)</i>");

View File

@ -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 {