Java Code Examples for org.bitcoinj.wallet.Wallet#fromSeed()
The following examples show how to use
org.bitcoinj.wallet.Wallet#fromSeed() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 6 votes |
public void restoreFromBlock0(String mnemonicCode, Runnable callback) { if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') { mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1); } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); walletFriendlyAddress = wallet.currentReceiveAddress().toString(); callback.run(); RequestorBtc.getUTXOListDgbNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { List<UTXOItemDgb> utxos = (List<UTXOItemDgb>)response; setUTXO(utxos); } @Override public void onFailure(String msg) { } }); }
Example 2
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 6 votes |
public void restoreFromBlock0(String mnemonicCode, Runnable callback) { if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') { mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1); } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); walletFriendlyAddress = wallet.currentReceiveAddress().toString(); callback.run(); RequestorBtc.getUTXOListBtgNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { List<UTXOItem> utxos = (List<UTXOItem>)response; setUTXO(utxos); } @Override public void onFailure(String msg) { } }); }
Example 3
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 6 votes |
public void restoreFromBlock0(String mnemonicCode, Runnable callback) { if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') { mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1); } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); walletFriendlyAddress = wallet.currentReceiveAddress().toString(); callback.run(); RequestorBtc.getUTXOListSbtcNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { List<UTXOItem> utxos = (List<UTXOItem>)response; setUTXO(utxos); } @Override public void onFailure(String msg) { } }); }
Example 4
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 5 votes |
public void restoreFromBlock(String mnemonicCode, WalletCreationCallback callback) { mnemonicCode = mnemonicCode.trim(); if (mnemonicCode.equals("")) { callback.onWalletCreated(null); return; } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); walletFriendlyAddress = wallet.currentReceiveAddress().toString(); ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT); DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber)); xprvKey = de.serializePrivB58(params); wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params); RequestorBtc.getUTXOListDgbNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { List<UTXOItemDgb> utxos = (List<UTXOItemDgb>)response; setUTXO(utxos); callback.onWalletCreated(wallet); } @Override public void onFailure(String msg) { callback.onWalletCreated(wallet); } }); }
Example 5
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 5 votes |
public void restoreFromBlock2(String mnemonicCode, Runnable callback) { if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') { mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1); } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); walletFriendlyAddress = wallet.currentReceiveAddress().toString(); ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT); DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber)); xprvKey = de.serializePrivB58(params); wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params); callback.run(); RequestorBtc.getUTXOListBch(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { List<UTXOItem> utxos = (List<UTXOItem>)response; setUTXO(utxos); } @Override public void onFailure(String msg) { } }); }
Example 6
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 5 votes |
public void restoreFromBlock2(String mnemonicCode, Runnable callback) { if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') { mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1); } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); walletFriendlyAddress = wallet.currentReceiveAddress().toString(); ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT); DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber)); xprvKey = de.serializePrivB58(params); wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params); callback.run(); RequestorBtc.getUTXOListBtgNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { List<UTXOItem> utxos = (List<UTXOItem>)response; setUTXO(utxos); } @Override public void onFailure(String msg) { } }); }
Example 7
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 5 votes |
public void restoreFromBlock2(String mnemonicCode, Runnable callback) { if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') { mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1); } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); walletFriendlyAddress = wallet.currentReceiveAddress().toString(); ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT); DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber)); xprvKey = de.serializePrivB58(params); wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params); callback.run(); RequestorBtc.getUTXOList(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { UTXOListResponse utxoResponse = (UTXOListResponse) response; setUTXO(utxoResponse.getUTXOList()); } @Override public void onFailure(String msg) { } }); }
Example 8
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 5 votes |
public void restoreFromBlock0(String mnemonicCode, Runnable callback) { if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') { mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1); } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); walletFriendlyAddress = wallet.currentReceiveAddress().toString(); callback.run(); RequestorBtc.getUTXOList(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { UTXOListResponse utxoResponse = (UTXOListResponse) response; setUTXO(utxoResponse.getUTXOList()); } @Override public void onFailure(String msg) { } }); }
Example 9
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 5 votes |
public void restoreFromBlock(String mnemonicCode, WalletCreationCallback callback) { mnemonicCode = mnemonicCode.trim(); if (mnemonicCode.equals("")) { callback.onWalletCreated(null); return; } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); walletFriendlyAddress = wallet.currentReceiveAddress().toString(); ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT); DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber)); xprvKey = de.serializePrivB58(params); wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params); callback.onWalletCreated(wallet); RequestorBtc.getUTXOList(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { UTXOListResponse utxoResponse = (UTXOListResponse) response; setUTXO(utxoResponse.getUTXOList()); } @Override public void onFailure(String msg) { } }); }
Example 10
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 5 votes |
public void restoreFromBlock2(String mnemonicCode, Runnable callback) { if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') { mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1); } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); walletFriendlyAddress = wallet.currentReceiveAddress().toString(); ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT); DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber)); xprvKey = de.serializePrivB58(params); wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params); callback.run(); RequestorBtc.getUTXOListLtcNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { List<UTXOItem> utxos = (List<UTXOItem>)response; setUTXO(utxos); } @Override public void onFailure(String msg) { } }); }
Example 11
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 5 votes |
public void restoreFromBlock(String mnemonicCode, WalletCreationCallback callback) { mnemonicCode = mnemonicCode.trim(); if (mnemonicCode.equals("")) { callback.onWalletCreated(null); return; } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); walletFriendlyAddress = wallet.currentReceiveAddress().toString(); ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT); DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber)); xprvKey = de.serializePrivB58(params); wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params); callback.onWalletCreated(wallet); RequestorBtc.getUTXOListLtcNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { List<UTXOItem> utxos = (List<UTXOItem>)response; setUTXO(utxos); } @Override public void onFailure(String msg) { } }); }
Example 12
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 5 votes |
public void restoreFromBlock(String mnemonicCode, WalletCreationCallback callback) { mnemonicCode = mnemonicCode.trim(); if (mnemonicCode.equals("")) { callback.onWalletCreated(null); return; } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); walletFriendlyAddress = wallet.currentReceiveAddress().toString(); ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT); DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber)); xprvKey = de.serializePrivB58(params); wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params); callback.onWalletCreated(wallet); RequestorBtc.getUTXOListSbtcNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { List<UTXOItem> utxos = (List<UTXOItem>)response; setUTXO(utxos); } @Override public void onFailure(String msg) { } }); }
Example 13
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 5 votes |
public void restoreFromBlock2(String mnemonicCode, Runnable callback) { if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') { mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1); } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); walletFriendlyAddress = wallet.currentReceiveAddress().toString(); ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT); DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber)); xprvKey = de.serializePrivB58(params); wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params); callback.run(); RequestorBtc.getUTXOListDgbNew(wallet.currentReceiveAddress().toString(), new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { List<UTXOItemDgb> utxos = (List<UTXOItemDgb>)response; setUTXO(utxos); } @Override public void onFailure(String msg) { } }); }
Example 14
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 4 votes |
public void restoreFromBlock(String mnemonicCode, WalletCreationCallback callback) { mnemonicCode = mnemonicCode.trim(); if (mnemonicCode.equals("")) { callback.onWalletCreated(null); return; } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); //Log.d("flint", "wallet.toString: " + wallet.toString()); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); //sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); updateWalletFriendlyAddress(); // ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT); // DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber)); // xprvKey = de.serializePrivB58(params); // // if (BuildConfig.DEBUG) { // DeterministicKey dk = extractWalletDeterministicKey(); // wifKey = dk.getPrivateKeyAsWiF(params); // } else { // wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params); // } callback.onWalletCreated(wallet); RequestorBtc.getUTXOListQtum(walletFriendlyAddress, new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { List<UTXOItem> utxos = (List<UTXOItem>)response; setUTXO(utxos); } @Override public void onFailure(String msg) { } }); }
Example 15
Source File: RestoreFromSeed.java From green_android with GNU General Public License v3.0 | 4 votes |
public static void main(String[] args) throws Exception { NetworkParameters params = TestNet3Params.get(); // Bitcoinj supports hierarchical deterministic wallets (or "HD Wallets"): https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki // HD wallets allow you to restore your wallet simply from a root seed. This seed can be represented using a short mnemonic sentence as described in BIP 39: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki // Here we restore our wallet from a seed with no passphrase. Also have a look at the BackupToMnemonicSeed.java example that shows how to backup a wallet by creating a mnemonic sentence. String seedCode = "yard impulse luxury drive today throw farm pepper survey wreck glass federal"; String passphrase = ""; Long creationtime = 1409478661L; DeterministicSeed seed = new DeterministicSeed(seedCode, null, passphrase, creationtime); // The wallet class provides a easy fromSeed() function that loads a new wallet from a given seed. Wallet wallet = Wallet.fromSeed(params, seed); // Because we are importing an existing wallet which might already have transactions we must re-download the blockchain to make the wallet picks up these transactions // You can find some information about this in the guides: https://bitcoinj.github.io/working-with-the-wallet#setup // To do this we clear the transactions of the wallet and delete a possible existing blockchain file before we download the blockchain again further down. System.out.println(wallet.toString()); wallet.clearTransactions(0); File chainFile = new File("restore-from-seed.spvchain"); if (chainFile.exists()) { chainFile.delete(); } // Setting up the BlochChain, the BlocksStore and connecting to the network. SPVBlockStore chainStore = new SPVBlockStore(params, chainFile); BlockChain chain = new BlockChain(params, chainStore); PeerGroup peers = new PeerGroup(params, chain); peers.addPeerDiscovery(new DnsDiscovery(params)); // Now we need to hook the wallet up to the blockchain and the peers. This registers event listeners that notify our wallet about new transactions. chain.addWallet(wallet); peers.addWallet(wallet); DownloadProgressTracker bListener = new DownloadProgressTracker() { @Override public void doneDownload() { System.out.println("blockchain downloaded"); } }; // Now we re-download the blockchain. This replays the chain into the wallet. Once this is completed our wallet should know of all its transactions and print the correct balance. peers.start(); peers.startBlockChainDownload(bListener); bListener.await(); // Print a debug message with the details about the wallet. The correct balance should now be displayed. System.out.println(wallet.toString()); // shutting down again peers.stop(); }
Example 16
Source File: WalletManager.java From guarda-android-wallets with GNU General Public License v3.0 | 4 votes |
public void restoreFromBlock2(String mnemonicCode, Runnable callback) { if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') { mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1); } DeterministicSeed seed = new DeterministicSeed(Splitter.on(' ').splitToList(mnemonicCode), null, "", 0); wallet = Wallet.fromSeed(params, seed); mnemonicKey = Joiner.on(" ").join(seed.getMnemonicCode()); sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey)); updateWalletFriendlyAddress(); DeterministicKey deterministicKey = wallet.getActiveKeyChain().getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS); // Log.d("flint", "walletAddress: " + deterministicKey.toAddress(params)); // Log.d("flint", "friendlyWalletAddress: " + deterministicKey.toAddress(params).toBase58()); // ChildNumber childNumber = new ChildNumber(ChildNumber.HARDENED_BIT); // DeterministicKey de = wallet.getKeyByPath(ImmutableList.of(childNumber)); // xprvKey = de.serializePrivB58(params); // // if (BuildConfig.DEBUG) { // DeterministicKey dk = extractWalletDeterministicKey(); // wifKey = dk.getPrivateKeyAsWiF(params); // } else { // wifKey = wallet.currentReceiveKey().getPrivateKeyAsWiF(params); // } callback.run(); RequestorBtc.getUTXOListQtum(walletFriendlyAddress, new ApiMethods.RequestListener() { @Override public void onSuccess(Object response) { List<UTXOItem> utxos = (List<UTXOItem>)response; setUTXO(utxos); } @Override public void onFailure(String msg) { } }); }
Example 17
Source File: PeerGroupTest.java From green_android with GNU General Public License v3.0 | 4 votes |
@Test public void autoRescanOnKeyExhaustion() throws Exception { // Check that if the last key that was inserted into the bloom filter is seen in some requested blocks, // that the exhausting block is discarded, a new filter is calculated and sent, and then the download resumes. final int NUM_KEYS = 9; // First, grab a load of keys from the wallet, and then recreate it so it forgets that those keys were issued. Wallet shadow = Wallet.fromSeed(wallet.getParams(), wallet.getKeyChainSeed()); List<ECKey> keys = new ArrayList<>(NUM_KEYS); for (int i = 0; i < NUM_KEYS; i++) { keys.add(shadow.freshReceiveKey()); } // Reduce the number of keys we need to work with to speed up this test. wallet.setKeyChainGroupLookaheadSize(4); wallet.setKeyChainGroupLookaheadThreshold(2); peerGroup.start(); InboundMessageQueuer p1 = connectPeer(1); assertTrue(p1.lastReceivedFilter.contains(keys.get(0).getPubKey())); assertTrue(p1.lastReceivedFilter.contains(keys.get(5).getPubKeyHash())); assertFalse(p1.lastReceivedFilter.contains(keys.get(keys.size() - 1).getPubKey())); peerGroup.startBlockChainDownload(null); assertNextMessageIs(p1, GetBlocksMessage.class); // Make some transactions and blocks that send money to the wallet thus using up all the keys. List<Block> blocks = Lists.newArrayList(); Coin expectedBalance = Coin.ZERO; Block prev = blockStore.getChainHead().getHeader(); for (ECKey key1 : keys) { Address addr = key1.toAddress(PARAMS); Block next = FakeTxBuilder.makeSolvedTestBlock(prev, FakeTxBuilder.createFakeTx(PARAMS, Coin.FIFTY_COINS, addr)); expectedBalance = expectedBalance.add(next.getTransactions().get(2).getOutput(0).getValue()); blocks.add(next); prev = next; } // Send the chain that doesn't have all the transactions in it. The blocks after the exhaustion point should all // be ignored. int epoch = wallet.getKeyChainGroupCombinedKeyLookaheadEpochs(); BloomFilter filter = new BloomFilter(PARAMS, p1.lastReceivedFilter.bitcoinSerialize()); filterAndSend(p1, blocks, filter); Block exhaustionPoint = blocks.get(3); pingAndWait(p1); assertNotEquals(epoch, wallet.getKeyChainGroupCombinedKeyLookaheadEpochs()); // 4th block was end of the lookahead zone and thus was discarded, so we got 3 blocks worth of money (50 each). assertEquals(Coin.FIFTY_COINS.multiply(3), wallet.getBalance()); assertEquals(exhaustionPoint.getPrevBlockHash(), blockChain.getChainHead().getHeader().getHash()); // Await the new filter. peerGroup.waitForJobQueue(); BloomFilter newFilter = assertNextMessageIs(p1, BloomFilter.class); assertNotEquals(filter, newFilter); assertNextMessageIs(p1, MemoryPoolMessage.class); Ping ping = assertNextMessageIs(p1, Ping.class); inbound(p1, new Pong(ping.getNonce())); // Await restart of the chain download. GetDataMessage getdata = assertNextMessageIs(p1, GetDataMessage.class); assertEquals(exhaustionPoint.getHash(), getdata.getHashOf(0)); assertEquals(InventoryItem.Type.FilteredBlock, getdata.getItems().get(0).type); List<Block> newBlocks = blocks.subList(3, blocks.size()); filterAndSend(p1, newBlocks, newFilter); assertNextMessageIs(p1, Ping.class); // It happened again. peerGroup.waitForJobQueue(); newFilter = assertNextMessageIs(p1, BloomFilter.class); assertNextMessageIs(p1, MemoryPoolMessage.class); inbound(p1, new Pong(assertNextMessageIs(p1, Ping.class).getNonce())); assertNextMessageIs(p1, GetDataMessage.class); newBlocks = blocks.subList(6, blocks.size()); filterAndSend(p1, newBlocks, newFilter); // Send a non-tx message so the peer knows the filtered block is over and force processing. inbound(p1, new Ping()); pingAndWait(p1); assertEquals(expectedBalance, wallet.getBalance()); assertEquals(blocks.get(blocks.size() - 1).getHash(), blockChain.getChainHead().getHeader().getHash()); }
Example 18
Source File: PeerGroupTest.java From GreenBits with GNU General Public License v3.0 | 4 votes |
@Test public void autoRescanOnKeyExhaustion() throws Exception { // Check that if the last key that was inserted into the bloom filter is seen in some requested blocks, // that the exhausting block is discarded, a new filter is calculated and sent, and then the download resumes. final int NUM_KEYS = 9; // First, grab a load of keys from the wallet, and then recreate it so it forgets that those keys were issued. Wallet shadow = Wallet.fromSeed(wallet.getParams(), wallet.getKeyChainSeed()); List<ECKey> keys = new ArrayList<>(NUM_KEYS); for (int i = 0; i < NUM_KEYS; i++) { keys.add(shadow.freshReceiveKey()); } // Reduce the number of keys we need to work with to speed up this test. wallet.setKeyChainGroupLookaheadSize(4); wallet.setKeyChainGroupLookaheadThreshold(2); peerGroup.start(); InboundMessageQueuer p1 = connectPeer(1); assertTrue(p1.lastReceivedFilter.contains(keys.get(0).getPubKey())); assertTrue(p1.lastReceivedFilter.contains(keys.get(5).getPubKeyHash())); assertFalse(p1.lastReceivedFilter.contains(keys.get(keys.size() - 1).getPubKey())); peerGroup.startBlockChainDownload(null); assertNextMessageIs(p1, GetBlocksMessage.class); // Make some transactions and blocks that send money to the wallet thus using up all the keys. List<Block> blocks = Lists.newArrayList(); Coin expectedBalance = Coin.ZERO; Block prev = blockStore.getChainHead().getHeader(); for (ECKey key1 : keys) { Address addr = key1.toAddress(PARAMS); Block next = FakeTxBuilder.makeSolvedTestBlock(prev, FakeTxBuilder.createFakeTx(PARAMS, Coin.FIFTY_COINS, addr)); expectedBalance = expectedBalance.add(next.getTransactions().get(2).getOutput(0).getValue()); blocks.add(next); prev = next; } // Send the chain that doesn't have all the transactions in it. The blocks after the exhaustion point should all // be ignored. int epoch = wallet.getKeyChainGroupCombinedKeyLookaheadEpochs(); BloomFilter filter = new BloomFilter(PARAMS, p1.lastReceivedFilter.bitcoinSerialize()); filterAndSend(p1, blocks, filter); Block exhaustionPoint = blocks.get(3); pingAndWait(p1); assertNotEquals(epoch, wallet.getKeyChainGroupCombinedKeyLookaheadEpochs()); // 4th block was end of the lookahead zone and thus was discarded, so we got 3 blocks worth of money (50 each). assertEquals(Coin.FIFTY_COINS.multiply(3), wallet.getBalance()); assertEquals(exhaustionPoint.getPrevBlockHash(), blockChain.getChainHead().getHeader().getHash()); // Await the new filter. peerGroup.waitForJobQueue(); BloomFilter newFilter = assertNextMessageIs(p1, BloomFilter.class); assertNotEquals(filter, newFilter); assertNextMessageIs(p1, MemoryPoolMessage.class); Ping ping = assertNextMessageIs(p1, Ping.class); inbound(p1, new Pong(ping.getNonce())); // Await restart of the chain download. GetDataMessage getdata = assertNextMessageIs(p1, GetDataMessage.class); assertEquals(exhaustionPoint.getHash(), getdata.getHashOf(0)); assertEquals(InventoryItem.Type.FilteredBlock, getdata.getItems().get(0).type); List<Block> newBlocks = blocks.subList(3, blocks.size()); filterAndSend(p1, newBlocks, newFilter); assertNextMessageIs(p1, Ping.class); // It happened again. peerGroup.waitForJobQueue(); newFilter = assertNextMessageIs(p1, BloomFilter.class); assertNextMessageIs(p1, MemoryPoolMessage.class); inbound(p1, new Pong(assertNextMessageIs(p1, Ping.class).getNonce())); assertNextMessageIs(p1, GetDataMessage.class); newBlocks = blocks.subList(6, blocks.size()); filterAndSend(p1, newBlocks, newFilter); // Send a non-tx message so the peer knows the filtered block is over and force processing. inbound(p1, new Ping()); pingAndWait(p1); assertEquals(expectedBalance, wallet.getBalance()); assertEquals(blocks.get(blocks.size() - 1).getHash(), blockChain.getChainHead().getHeader().getHash()); }
Example 19
Source File: RestoreFromSeed.java From GreenBits with GNU General Public License v3.0 | 4 votes |
public static void main(String[] args) throws Exception { NetworkParameters params = TestNet3Params.get(); // Bitcoinj supports hierarchical deterministic wallets (or "HD Wallets"): https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki // HD wallets allow you to restore your wallet simply from a root seed. This seed can be represented using a short mnemonic sentence as described in BIP 39: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki // Here we restore our wallet from a seed with no passphrase. Also have a look at the BackupToMnemonicSeed.java example that shows how to backup a wallet by creating a mnemonic sentence. String seedCode = "yard impulse luxury drive today throw farm pepper survey wreck glass federal"; String passphrase = ""; Long creationtime = 1409478661L; DeterministicSeed seed = new DeterministicSeed(seedCode, null, passphrase, creationtime); // The wallet class provides a easy fromSeed() function that loads a new wallet from a given seed. Wallet wallet = Wallet.fromSeed(params, seed); // Because we are importing an existing wallet which might already have transactions we must re-download the blockchain to make the wallet picks up these transactions // You can find some information about this in the guides: https://bitcoinj.github.io/working-with-the-wallet#setup // To do this we clear the transactions of the wallet and delete a possible existing blockchain file before we download the blockchain again further down. System.out.println(wallet.toString()); wallet.clearTransactions(0); File chainFile = new File("restore-from-seed.spvchain"); if (chainFile.exists()) { chainFile.delete(); } // Setting up the BlochChain, the BlocksStore and connecting to the network. SPVBlockStore chainStore = new SPVBlockStore(params, chainFile); BlockChain chain = new BlockChain(params, chainStore); PeerGroup peers = new PeerGroup(params, chain); peers.addPeerDiscovery(new DnsDiscovery(params)); // Now we need to hook the wallet up to the blockchain and the peers. This registers event listeners that notify our wallet about new transactions. chain.addWallet(wallet); peers.addWallet(wallet); DownloadProgressTracker bListener = new DownloadProgressTracker() { @Override public void doneDownload() { System.out.println("blockchain downloaded"); } }; // Now we re-download the blockchain. This replays the chain into the wallet. Once this is completed our wallet should know of all its transactions and print the correct balance. peers.start(); peers.startBlockChainDownload(bListener); bListener.await(); // Print a debug message with the details about the wallet. The correct balance should now be displayed. System.out.println(wallet.toString()); // shutting down again peers.stop(); }
Example 20
Source File: PeerGroupTest.java From bcm-android with GNU General Public License v3.0 | 4 votes |
@Test public void autoRescanOnKeyExhaustion() throws Exception { // Check that if the last key that was inserted into the bloom filter is seen in some requested blocks, // that the exhausting block is discarded, a new filter is calculated and sent, and then the download resumes. final int NUM_KEYS = 9; // First, grab a load of keys from the wallet, and then recreate it so it forgets that those keys were issued. Wallet shadow = Wallet.fromSeed(wallet.getParams(), wallet.getKeyChainSeed()); List<ECKey> keys = new ArrayList<>(NUM_KEYS); for (int i = 0; i < NUM_KEYS; i++) { keys.add(shadow.freshReceiveKey()); } // Reduce the number of keys we need to work with to speed up this test. wallet.setKeyChainGroupLookaheadSize(4); wallet.setKeyChainGroupLookaheadThreshold(2); peerGroup.start(); InboundMessageQueuer p1 = connectPeer(1); assertTrue(p1.lastReceivedFilter.contains(keys.get(0).getPubKey())); assertTrue(p1.lastReceivedFilter.contains(keys.get(5).getPubKeyHash())); assertFalse(p1.lastReceivedFilter.contains(keys.get(keys.size() - 1).getPubKey())); peerGroup.startBlockChainDownload(null); assertNextMessageIs(p1, GetBlocksMessage.class); // Make some transactions and blocks that send money to the wallet thus using up all the keys. List<Block> blocks = Lists.newArrayList(); Coin expectedBalance = Coin.ZERO; Block prev = blockStore.getChainHead().getHeader(); for (ECKey key1 : keys) { Address addr = LegacyAddress.fromKey(UNITTEST, key1); Block next = FakeTxBuilder.makeSolvedTestBlock(prev, FakeTxBuilder.createFakeTx(UNITTEST, Coin.FIFTY_COINS, addr)); expectedBalance = expectedBalance.add(next.getTransactions().get(2).getOutput(0).getValue()); blocks.add(next); prev = next; } // Send the chain that doesn't have all the transactions in it. The blocks after the exhaustion point should all // be ignored. int epoch = wallet.getKeyChainGroupCombinedKeyLookaheadEpochs(); BloomFilter filter = new BloomFilter(UNITTEST, p1.lastReceivedFilter.bitcoinSerialize()); filterAndSend(p1, blocks, filter); Block exhaustionPoint = blocks.get(3); pingAndWait(p1); assertNotEquals(epoch, wallet.getKeyChainGroupCombinedKeyLookaheadEpochs()); // 4th block was end of the lookahead zone and thus was discarded, so we got 3 blocks worth of money (50 each). assertEquals(Coin.FIFTY_COINS.multiply(3), wallet.getBalance()); assertEquals(exhaustionPoint.getPrevBlockHash(), blockChain.getChainHead().getHeader().getHash()); // Await the new filter. peerGroup.waitForJobQueue(); BloomFilter newFilter = assertNextMessageIs(p1, BloomFilter.class); assertNotEquals(filter, newFilter); assertNextMessageIs(p1, MemoryPoolMessage.class); Ping ping = assertNextMessageIs(p1, Ping.class); inbound(p1, new Pong(ping.getNonce())); // Await restart of the chain download. GetDataMessage getdata = assertNextMessageIs(p1, GetDataMessage.class); assertEquals(exhaustionPoint.getHash(), getdata.getHashOf(0)); assertEquals(InventoryItem.Type.FilteredBlock, getdata.getItems().get(0).type); List<Block> newBlocks = blocks.subList(3, blocks.size()); filterAndSend(p1, newBlocks, newFilter); assertNextMessageIs(p1, Ping.class); // It happened again. peerGroup.waitForJobQueue(); newFilter = assertNextMessageIs(p1, BloomFilter.class); assertNextMessageIs(p1, MemoryPoolMessage.class); inbound(p1, new Pong(assertNextMessageIs(p1, Ping.class).getNonce())); assertNextMessageIs(p1, GetDataMessage.class); newBlocks = blocks.subList(6, blocks.size()); filterAndSend(p1, newBlocks, newFilter); // Send a non-tx message so the peer knows the filtered block is over and force processing. inbound(p1, new Ping()); pingAndWait(p1); assertEquals(expectedBalance, wallet.getBalance()); assertEquals(blocks.get(blocks.size() - 1).getHash(), blockChain.getChainHead().getHeader().getHash()); }