Java Code Examples for org.bitcoinj.wallet.Wallet#importKey()

The following examples show how to use org.bitcoinj.wallet.Wallet#importKey() . 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: PeerGroupTest.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testWalletCatchupTime() throws Exception {
    // Check the fast catchup time was initialized to something around the current runtime minus a week.
    // The wallet was already added to the peer in setup.
    final int WEEK = 86400 * 7;
    final long now = Utils.currentTimeSeconds();
    peerGroup.start();
    assertTrue(peerGroup.getFastCatchupTimeSecs() > now - WEEK - 10000);
    Wallet w2 = new Wallet(UNITTEST);
    ECKey key1 = new ECKey();
    key1.setCreationTimeSeconds(now - 86400);  // One day ago.
    w2.importKey(key1);
    peerGroup.addWallet(w2);
    peerGroup.waitForJobQueue();
    assertEquals(peerGroup.getFastCatchupTimeSecs(), now - 86400 - WEEK);
    // Adding a key to the wallet should update the fast catchup time, but asynchronously and in the background
    // due to the need to avoid complicated lock inversions.
    ECKey key2 = new ECKey();
    key2.setCreationTimeSeconds(now - 100000);
    w2.importKey(key2);
    peerGroup.waitForJobQueue();
    assertEquals(peerGroup.getFastCatchupTimeSecs(), now - WEEK - 100000);
}
 
Example 2
Source File: PeerGroupTest.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testWalletCatchupTime() throws Exception {
    // Check the fast catchup time was initialized to something around the current runtime minus a week.
    // The wallet was already added to the peer in setup.
    final int WEEK = 86400 * 7;
    final long now = Utils.currentTimeSeconds();
    peerGroup.start();
    assertTrue(peerGroup.getFastCatchupTimeSecs() > now - WEEK - 10000);
    Wallet w2 = new Wallet(PARAMS);
    ECKey key1 = new ECKey();
    key1.setCreationTimeSeconds(now - 86400);  // One day ago.
    w2.importKey(key1);
    peerGroup.addWallet(w2);
    peerGroup.waitForJobQueue();
    assertEquals(peerGroup.getFastCatchupTimeSecs(), now - 86400 - WEEK);
    // Adding a key to the wallet should update the fast catchup time, but asynchronously and in the background
    // due to the need to avoid complicated lock inversions.
    ECKey key2 = new ECKey();
    key2.setCreationTimeSeconds(now - 100000);
    w2.importKey(key2);
    peerGroup.waitForJobQueue();
    assertEquals(peerGroup.getFastCatchupTimeSecs(), now - WEEK - 100000);
}
 
Example 3
Source File: PeerGroupTest.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testWalletCatchupTime() throws Exception {
    // Check the fast catchup time was initialized to something around the current runtime minus a week.
    // The wallet was already added to the peer in setup.
    final int WEEK = 86400 * 7;
    final long now = Utils.currentTimeSeconds();
    peerGroup.start();
    assertTrue(peerGroup.getFastCatchupTimeSecs() > now - WEEK - 10000);
    Wallet w2 = new Wallet(PARAMS);
    ECKey key1 = new ECKey();
    key1.setCreationTimeSeconds(now - 86400);  // One day ago.
    w2.importKey(key1);
    peerGroup.addWallet(w2);
    peerGroup.waitForJobQueue();
    assertEquals(peerGroup.getFastCatchupTimeSecs(), now - 86400 - WEEK);
    // Adding a key to the wallet should update the fast catchup time, but asynchronously and in the background
    // due to the need to avoid complicated lock inversions.
    ECKey key2 = new ECKey();
    key2.setCreationTimeSeconds(now - 100000);
    w2.importKey(key2);
    peerGroup.waitForJobQueue();
    assertEquals(peerGroup.getFastCatchupTimeSecs(), now - WEEK - 100000);
}
 
Example 4
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlockByWif(String wif, WalletCreationCallback callback) {
    wif = wif.trim();
    try {
        DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, wif);
        ECKey key = dumpedPrivateKey.getKey();
        wallet = new Wallet(params);
        wallet.importKey(key);
        restFromWif = true;
        walletFriendlyAddress = wallet.getImportedKeys().get(0).toAddress(params).toString();
        wifKey = wif;
    } catch (IllegalArgumentException iae) {
        FirebaseCrash.report(iae);
        Log.e("psd", "restoreFromBlockByWif: " + iae.toString());
        callback.onWalletCreated(wallet);
        return;
    }

    callback.onWalletCreated(wallet);

    RequestorBtc.getUTXOListBch(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 5
Source File: BlockTest.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testReceiveCoinbaseTransaction() throws Exception {
    // Block 169482 (hash 0000000000000756935f1ee9d5987857b604046f846d3df56d024cdb5f368665)
    // contains coinbase transactions that are mining pool shares.
    // The private key MINERS_KEY is used to check transactions are received by a wallet correctly.

    // The address for this private key is 1GqtGtn4fctXuKxsVzRPSLmYWN1YioLi9y.
    final String MINING_PRIVATE_KEY = "5JDxPrBRghF1EvSBjDigywqfmAjpHPmTJxYtQTYJxJRHLLQA4mG";

    final long BLOCK_NONCE = 3973947400L;
    final Coin BALANCE_AFTER_BLOCK = Coin.valueOf(22223642);
    Block block169482 = MAINNET.getDefaultSerializer().makeBlock(ByteStreams.toByteArray(getClass().getResourceAsStream("block169482.dat")));

    // Check block.
    assertNotNull(block169482);
    block169482.verify(169482, EnumSet.noneOf(Block.VerifyFlag.class));
    assertEquals(BLOCK_NONCE, block169482.getNonce());

    StoredBlock storedBlock = new StoredBlock(block169482, BigInteger.ONE, 169482); // Nonsense work - not used in test.

    // Create a wallet contain the miner's key that receives a spend from a coinbase.
    ECKey miningKey = DumpedPrivateKey.fromBase58(MAINNET, MINING_PRIVATE_KEY).getKey();
    assertNotNull(miningKey);
    Context context = new Context(MAINNET);
    Wallet wallet = new Wallet(context);
    wallet.importKey(miningKey);

    // Initial balance should be zero by construction.
    assertEquals(Coin.ZERO, wallet.getBalance());

    // Give the wallet the first transaction in the block - this is the coinbase tx.
    List<Transaction> transactions = block169482.getTransactions();
    assertNotNull(transactions);
    wallet.receiveFromBlock(transactions.get(0), storedBlock, NewBlockType.BEST_CHAIN, 0);

    // Coinbase transaction should have been received successfully but be unavailable to spend (too young).
    assertEquals(BALANCE_AFTER_BLOCK, wallet.getBalance(BalanceType.ESTIMATED));
    assertEquals(Coin.ZERO, wallet.getBalance(BalanceType.AVAILABLE));
}
 
Example 6
Source File: WalletProtobufSerializerTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@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());
    }
}
 
Example 7
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlockByWif2(String wif, Runnable callback) {
    wif = wif.trim();
    try {
        DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, wif);
        ECKey key = dumpedPrivateKey.getKey();
        wallet = new Wallet(params);
        wallet.importKey(key);
        restFromWif = true;
        sharedManager.setLastSyncedBlock(Coders.encodeBase64(wif));
        walletFriendlyAddress = wallet.getImportedKeys().get(0).toAddress(params).toString();
        wifKey = wif;
    } catch (WrongNetworkException wne) {
        Log.e("psd", "restoreFromBlockByWif2: " + wne.toString());
        callback.run();
        return;
    } catch (IllegalArgumentException iae) {
        FirebaseCrash.report(iae);
        Log.e("psd", "restoreFromBlockByWif2: " + iae.toString());
        callback.run();
        return;
    }

    callback.run();

    RequestorBtc.getUTXOListBch(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 8
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlockByWif(String wif, WalletCreationCallback callback) {
    wif = wif.trim();
    try {
        DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, wif);
        ECKey key = dumpedPrivateKey.getKey();
        wallet = new Wallet(params);
        wallet.importKey(key);
        restFromWif = true;
        walletFriendlyAddress = wallet.getImportedKeys().get(0).toAddress(params).toString();
        wifKey = wif;
    } catch (IllegalArgumentException iae) {
        FirebaseCrash.report(iae);
        Log.e("psd", "restoreFromBlockByWif: " + iae.toString());
        callback.onWalletCreated(wallet);
        return;
    }

    callback.onWalletCreated(wallet);

    RequestorBtc.getUTXOList(walletFriendlyAddress, 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 vote down vote up
public void restoreFromBlockByWif2(String wif, Runnable callback) {
    wif = wif.trim();
    try {
        DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, wif);
        ECKey key = dumpedPrivateKey.getKey();
        wallet = new Wallet(params);
        wallet.importKey(key);
        restFromWif = true;
        sharedManager.setLastSyncedBlock(Coders.encodeBase64(wif));
        walletFriendlyAddress = wallet.getImportedKeys().get(0).toAddress(params).toString();
        wifKey = wif;
    } catch (WrongNetworkException wne) {
        Log.e("psd", "restoreFromBlockByWif2: " + wne.toString());
        callback.run();
        return;
    } catch (IllegalArgumentException iae) {
        FirebaseCrash.report(iae);
        Log.e("psd", "restoreFromBlockByWif2: " + iae.toString());
        callback.run();
        return;
    }

    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 10
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlockByWif(String wif, WalletCreationCallback callback) {
    wif = wif.trim();
    try {
        DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, wif);
        ECKey key = dumpedPrivateKey.getKey();
        wallet = new Wallet(params);
        wallet.importKey(key);
        restFromWif = true;
        walletFriendlyAddress = wallet.getImportedKeys().get(0).toAddress(params).toString();
        wifKey = wif;
    } catch (IllegalArgumentException iae) {
        FirebaseCrash.report(iae);
        Log.e("psd", "restoreFromBlockByWif: " + iae.toString());
        callback.onWalletCreated(wallet);
        return;
    }

    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 11
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlockByWif2(String wif, Runnable callback) {
    wif = wif.trim();
    try {
        DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, wif);
        ECKey key = dumpedPrivateKey.getKey();
        wallet = new Wallet(params);
        wallet.importKey(key);
        restFromWif = true;
        sharedManager.setLastSyncedBlock(Coders.encodeBase64(wif));
        walletFriendlyAddress = wallet.getImportedKeys().get(0).toAddress(params).toString();
        wifKey = wif;
    } catch (WrongNetworkException wne) {
        Log.e("psd", "restoreFromBlockByWif2: " + wne.toString());
        callback.run();
        return;
    } catch (IllegalArgumentException iae) {
        FirebaseCrash.report(iae);
        Log.e("psd", "restoreFromBlockByWif2: " + iae.toString());
        callback.run();
        return;
    }

    callback.run();

    RequestorBtc.getUTXOListLtcNew(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 12
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlockByWif(String wif, WalletCreationCallback callback) {
    wif = wif.trim();
    try {
        DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, wif);
        ECKey key = dumpedPrivateKey.getKey();
        wallet = new Wallet(params);
        wallet.importKey(key);
        restFromWif = true;
        walletFriendlyAddress = wallet.getImportedKeys().get(0).toAddress(params).toString();
        wifKey = wif;
    } catch (IllegalArgumentException iae) {
        FirebaseCrash.report(iae);
        Log.e("psd", "restoreFromBlockByWif: " + iae.toString());
        callback.onWalletCreated(wallet);
        return;
    }

    callback.onWalletCreated(wallet);

    RequestorBtc.getUTXOListLtcNew(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 13
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlock2(String mnemonicCode, Runnable callback) {
    if (mnemonicCode.charAt(mnemonicCode.length() - 1) == ' ') {
        mnemonicCode = mnemonicCode.substring(0, mnemonicCode.length() - 1);
    }

    DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, anyToWif(mnemonicCode));
    ECKey key = dumpedPrivateKey.getKey();
    wallet = new Wallet(params);
    wallet.importKey(key);
    mnemonicKey = mnemonicCode;
    sharedManager.setLastSyncedBlock(Coders.encodeBase64(mnemonicKey));

    walletFriendlyAddress = wallet.getImportedKeys().get(0).toAddress(params).toBase58();
    walletFriendlyAddress = testReadonlyAddress == null ? walletFriendlyAddress : testReadonlyAddress;

    callback.run();

    RequestorBtc.getUTXOListKmdNew(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 14
Source File: WalletProtobufSerializerTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@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());
    }
}
 
Example 15
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 5 votes vote down vote up
public void restoreFromBlockByWif(String wif, WalletCreationCallback callback) {
    wif = wif.trim();
    try {
        DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, wif);
        ECKey key = dumpedPrivateKey.getKey();
        wallet = new Wallet(params);
        wallet.importKey(key);
        restFromWif = true;
        walletFriendlyAddress = wallet.getImportedKeys().get(0).toAddress(params).toString();
        wifKey = wif;
    } catch (IllegalArgumentException iae) {
        FirebaseCrash.report(iae);
        Log.e("psd", "restoreFromBlockByWif: " + iae.toString());
        callback.onWalletCreated(wallet);
        return;
    }

    callback.onWalletCreated(wallet);

    RequestorBtc.getUTXOListDgbNew(walletFriendlyAddress, new ApiMethods.RequestListener() {
        @Override
        public void onSuccess(Object response) {
            List<UTXOItemDgb> utxos = (List<UTXOItemDgb>)response;
            setUTXO(utxos);
        }

        @Override
        public void onFailure(String msg) {

        }
    });
}
 
Example 16
Source File: BlockTest.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testReceiveCoinbaseTransaction() throws Exception {
    // Block 169482 (hash 0000000000000756935f1ee9d5987857b604046f846d3df56d024cdb5f368665)
    // contains coinbase transactions that are mining pool shares.
    // The private key MINERS_KEY is used to check transactions are received by a wallet correctly.

    // The address for this private key is 1GqtGtn4fctXuKxsVzRPSLmYWN1YioLi9y.
    final String MINING_PRIVATE_KEY = "5JDxPrBRghF1EvSBjDigywqfmAjpHPmTJxYtQTYJxJRHLLQA4mG";

    final long BLOCK_NONCE = 3973947400L;
    final Coin BALANCE_AFTER_BLOCK = Coin.valueOf(22223642);
    final NetworkParameters PARAMS = MainNetParams.get();

    Block block169482 = PARAMS.getDefaultSerializer().makeBlock(ByteStreams.toByteArray(getClass().getResourceAsStream("block169482.dat")));

    // Check block.
    assertNotNull(block169482);
    block169482.verify(169482, EnumSet.noneOf(Block.VerifyFlag.class));
    assertEquals(BLOCK_NONCE, block169482.getNonce());

    StoredBlock storedBlock = new StoredBlock(block169482, BigInteger.ONE, 169482); // Nonsense work - not used in test.

    // Create a wallet contain the miner's key that receives a spend from a coinbase.
    ECKey miningKey = DumpedPrivateKey.fromBase58(PARAMS, MINING_PRIVATE_KEY).getKey();
    assertNotNull(miningKey);
    Context context = new Context(PARAMS);
    Wallet wallet = new Wallet(context);
    wallet.importKey(miningKey);

    // Initial balance should be zero by construction.
    assertEquals(Coin.ZERO, wallet.getBalance());

    // Give the wallet the first transaction in the block - this is the coinbase tx.
    List<Transaction> transactions = block169482.getTransactions();
    assertNotNull(transactions);
    wallet.receiveFromBlock(transactions.get(0), storedBlock, NewBlockType.BEST_CHAIN, 0);

    // Coinbase transaction should have been received successfully but be unavailable to spend (too young).
    assertEquals(BALANCE_AFTER_BLOCK, wallet.getBalance(BalanceType.ESTIMATED));
    assertEquals(Coin.ZERO, wallet.getBalance(BalanceType.AVAILABLE));
}
 
Example 17
Source File: BlockTest.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testReceiveCoinbaseTransaction() throws Exception {
    // Block 169482 (hash 0000000000000756935f1ee9d5987857b604046f846d3df56d024cdb5f368665)
    // contains coinbase transactions that are mining pool shares.
    // The private key MINERS_KEY is used to check transactions are received by a wallet correctly.

    // The address for this private key is 1GqtGtn4fctXuKxsVzRPSLmYWN1YioLi9y.
    final String MINING_PRIVATE_KEY = "5JDxPrBRghF1EvSBjDigywqfmAjpHPmTJxYtQTYJxJRHLLQA4mG";

    final long BLOCK_NONCE = 3973947400L;
    final Coin BALANCE_AFTER_BLOCK = Coin.valueOf(22223642);
    final NetworkParameters PARAMS = MainNetParams.get();

    Block block169482 = PARAMS.getDefaultSerializer().makeBlock(ByteStreams.toByteArray(getClass().getResourceAsStream("block169482.dat")));

    // Check block.
    assertNotNull(block169482);
    block169482.verify(169482, EnumSet.noneOf(Block.VerifyFlag.class));
    assertEquals(BLOCK_NONCE, block169482.getNonce());

    StoredBlock storedBlock = new StoredBlock(block169482, BigInteger.ONE, 169482); // Nonsense work - not used in test.

    // Create a wallet contain the miner's key that receives a spend from a coinbase.
    ECKey miningKey = DumpedPrivateKey.fromBase58(PARAMS, MINING_PRIVATE_KEY).getKey();
    assertNotNull(miningKey);
    Context context = new Context(PARAMS);
    Wallet wallet = new Wallet(context);
    wallet.importKey(miningKey);

    // Initial balance should be zero by construction.
    assertEquals(Coin.ZERO, wallet.getBalance());

    // Give the wallet the first transaction in the block - this is the coinbase tx.
    List<Transaction> transactions = block169482.getTransactions();
    assertNotNull(transactions);
    wallet.receiveFromBlock(transactions.get(0), storedBlock, NewBlockType.BEST_CHAIN, 0);

    // Coinbase transaction should have been received successfully but be unavailable to spend (too young).
    assertEquals(BALANCE_AFTER_BLOCK, wallet.getBalance(BalanceType.ESTIMATED));
    assertEquals(Coin.ZERO, wallet.getBalance(BalanceType.AVAILABLE));
}
 
Example 18
Source File: WalletManager.java    From guarda-android-wallets with GNU General Public License v3.0 4 votes vote down vote up
public void restoreFromBlockByWif2(String wif, Runnable callback) {
    wif = wif.trim();
    try {
        DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, wif);
        ECKey key = dumpedPrivateKey.getKey();
        wallet = new Wallet(params);
        wallet.importKey(key);
        restFromWif = true;
        sharedManager.setLastSyncedBlock(Coders.encodeBase64(wif));
        walletFriendlyAddress = wallet.getImportedKeys().get(0).toAddress(params).toString();
        wifKey = wif;
    } catch (WrongNetworkException wne) {
        Log.e("psd", "restoreFromBlockByWif2: " + wne.toString());
        callback.run();
        return;
    } catch (AddressFormatException afe) {
        Log.e("psd", "restoreFromBlockByWif2: " + afe.toString());
        callback.run();
        return;
    } catch (IllegalArgumentException iae) {
        FirebaseCrash.report(iae);
        Log.e("psd", "restoreFromBlockByWif2: " + iae.toString());
        callback.run();
        return;
    }

    callback.run();

    RequestorBtc.getUTXOListDgbNew(walletFriendlyAddress, new ApiMethods.RequestListener() {
        @Override
        public void onSuccess(Object response) {
            List<UTXOItemDgb> utxos = (List<UTXOItemDgb>)response;
            setUTXO(utxos);
        }

        @Override
        public void onFailure(String msg) {

        }
    });
}
 
Example 19
Source File: PrivateKeys.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    // TODO: Assumes main network not testnet. Make it selectable.
    NetworkParameters params = MainNetParams.get();
    try {
        // Decode the private key from Satoshis Base58 variant. If 51 characters long then it's from Bitcoins
        // dumpprivkey command and includes a version byte and checksum, or if 52 characters long then it has 
        // compressed pub key. Otherwise assume it's a raw key.
        ECKey key;
        if (args[0].length() == 51 || args[0].length() == 52) {
            DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, args[0]);
            key = dumpedPrivateKey.getKey();
        } else {
            BigInteger privKey = Base58.decodeToBigInteger(args[0]);
            key = ECKey.fromPrivate(privKey);
        }
        System.out.println("Address from private key is: " + key.toAddress(params).toString());
        // And the address ...
        Address destination = Address.fromBase58(params, args[1]);

        // Import the private key to a fresh wallet.
        Wallet wallet = new Wallet(params);
        wallet.importKey(key);

        // Find the transactions that involve those coins.
        final MemoryBlockStore blockStore = new MemoryBlockStore(params);
        BlockChain chain = new BlockChain(params, wallet, blockStore);

        final PeerGroup peerGroup = new PeerGroup(params, chain);
        peerGroup.addAddress(new PeerAddress(params, InetAddress.getLocalHost()));
        peerGroup.startAsync();
        peerGroup.downloadBlockChain();
        peerGroup.stopAsync();

        // And take them!
        System.out.println("Claiming " + wallet.getBalance().toFriendlyString());
        wallet.sendCoins(peerGroup, destination, wallet.getBalance());
        // Wait a few seconds to let the packets flush out to the network (ugly).
        Thread.sleep(5000);
        System.exit(0);
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("First arg should be private key in Base58 format. Second argument should be address " +
                "to send to.");
    }
}
 
Example 20
Source File: PrivateKeys.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    // TODO: Assumes main network not testnet. Make it selectable.
    NetworkParameters params = MainNetParams.get();
    try {
        // Decode the private key from Satoshis Base58 variant. If 51 characters long then it's from Bitcoins
        // dumpprivkey command and includes a version byte and checksum, or if 52 characters long then it has 
        // compressed pub key. Otherwise assume it's a raw key.
        ECKey key;
        if (args[0].length() == 51 || args[0].length() == 52) {
            DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, args[0]);
            key = dumpedPrivateKey.getKey();
        } else {
            BigInteger privKey = Base58.decodeToBigInteger(args[0]);
            key = ECKey.fromPrivate(privKey);
        }
        System.out.println("Address from private key is: " + key.toAddress(params).toString());
        // And the address ...
        Address destination = Address.fromBase58(params, args[1]);

        // Import the private key to a fresh wallet.
        Wallet wallet = new Wallet(params);
        wallet.importKey(key);

        // Find the transactions that involve those coins.
        final MemoryBlockStore blockStore = new MemoryBlockStore(params);
        BlockChain chain = new BlockChain(params, wallet, blockStore);

        final PeerGroup peerGroup = new PeerGroup(params, chain);
        peerGroup.addAddress(new PeerAddress(params, InetAddress.getLocalHost()));
        peerGroup.startAsync();
        peerGroup.downloadBlockChain();
        peerGroup.stopAsync();

        // And take them!
        System.out.println("Claiming " + wallet.getBalance().toFriendlyString());
        wallet.sendCoins(peerGroup, destination, wallet.getBalance());
        // Wait a few seconds to let the packets flush out to the network (ugly).
        Thread.sleep(5000);
        System.exit(0);
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("First arg should be private key in Base58 format. Second argument should be address " +
                "to send to.");
    }
}