Notice/Hinweis: this article is available in German as well, please follow this link to BitcoinJ 05 Watching Wallet.
One aspect regarding Bitcoin wallets is interesting – some wallets are „watching wallets“. They do works as regular ones do – they are showing transactions and have balances. Just one (important) part is missing and that are the Private Keys. For that reason we cannot make any payments with these wallets so they are perfect for demonstrating purposes.
On the official BitcoinJ Website you find some information about the process of converting a wallet but in detail you will find that the created wallet does not word as expected.
The following program converts an existing wallet without any more input of the user. The converted file(name) is created by adding of a „W“ at the end of the name (for example: filenameFullWallet = „bitcoinj02createwallet.wallet“ will be converted to filenameWatchingWallet = „bitcoinj02createwalletW.wallet“).
Just two important notices: you have to delete existing Watching Wallet files by hand (you will get a notice) and you need an online access during converting – this step lasts between 1 and 3 minutes so please be patient with the software.
To use this programs you need several external libraries – they are all accessible from my Github-Repository https://github.com/java-crypto/BitcoinJ_Libraries).
This is the shortened output on the console; the full outputs are available on my Github-Repository for BitcoinJ: Github-Repository.
Errormessage:
1 2 3 4 |
BitcoinJ 05 Create a watching wallet from a regular one Existing file needs to get deleted manually: bitcoinj02createwalletW.wallet Existing file needs to get deleted manually: bitcoinj02createwalletW.spvchain Please delete the files above manually and restart the program. The program ends now. |
Console (shortened):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
Error: BitcoinJ 05 Create a watching wallet from a regular one Existing file needs to get deleted manually: bitcoinj02createwalletW.wallet Existing file needs to get deleted manually: bitcoinj02createwalletW.spvchain Please delete the files above manually and restart the program. The program ends now. BitcoinJ 05 Create a watching wallet from a regular one We are using the Bitcoin Testnet and not the Mainnet Load the Full wallet: bitcoinj02createwallet.wallet Loading successfull : bitcoinj02createwallet.wallet ********************************************************************************** Dump the full wallet: bitcoinj02createwallet.wallet Wallet Balances: 0.01626622 BTC ESTIMATED 0.01626622 BTC AVAILABLE 0.01626622 BTC ESTIMATED_SPENDABLE 0.01626622 BTC AVAILABLE_SPENDABLE Transactions: 0 pending 2 unspent 2 spent 0 dead Last seen best block: 1669600 (2020-03-12T10:22:01Z): 00000000bfdc0927b5c5ccd2b6d6e7c77002b6207a2bf57d2ca7897c93b44506 Keys: Earliest creation time: 2020-02-29T09:44:25Z addr:mjLMNLydrmt2JerdCSc8sZCTMuujMXpero,tb1q980nq50prlpulx3t9r46jxa9csd8qh4ltz0en6 hash160:29df3051e11fc3cf9a2b28eba91ba5c41a705ebf creationTimeSeconds:1582969466 [2020-02-29T09:44:26Z] (imported) ... Seed birthday: 1582969465 [2020-02-29T09:44:25Z] Ouput script type: P2PKH Key to watch: tpubD8WRr3vpDwybtseRYzbP739wJw4vSMcRAvw6QifMh5ZNJTKwp5QRmymsjjLHp6kZqMPAVEqsZcAfqSApUS8mtLbwzyxAM5XFQMh3eZ73PRy Lookahead siz/thr: 100/33 addr:miaLUm4yeBv7XEfx9u9LXCeCvS9Y7GL3gE hash160:218c24dd87b2b87a3405518512cbd6e6dfa1c9f6 (M, root) ... >>> UNSPENT: -0.001425 BTC total value (sends 0.00575738 BTC and receives 0.00433238 BTC) confidence: Seen by 10 peers (most recently: 2020-03-12T10:17:14Z). Appeared in best chain at height 1669600, depth 1. Source: NETWORK 3b04ff0742625ad5123e3a6bc74b407fde68e098c97244bd6a413b9550effe34 updated: 2020-03-12T10:07:00Z purpose: USER_PAYMENT in PUSHDATA(72)[3045022100f873e0f5d4d3b9302aaa2cb374443e000a5c2e17cdd44bb4389056a36ab935880220632dd6451bcf235f20393f3545eaec98a0159fda9281031eb1851bf5e5f90cc301] PUSHDATA(33)[0325eab72b88d818f9b61456586151eeeafed64687c0ea666e8b9c64d57074a435] 0.00575738 BTC P2PKH addr:myA5v3sQo4VdhFGNJ9Gv22aRLnw7q7KhDw outpoint:905277665a3eb6cd1cf0af30b00f15db3ec6fc7ae86b3c150aa2fdcdb589d0b1:0 out HASH160 PUSHDATA(20)[41a37afc980ac078df37b24f2a687102f79b0ab3] EQUAL 0.0012 BTC P2SH addr:2MyEHka6Whdt87LyK3VuUGGfuWkNTtSD3Hj out DUP HASH160 PUSHDATA(20)[446f865811cad957a865e4514e83f1234bf708d8] EQUALVERIFY CHECKSIG 0.00433238 BTC P2PKH addr:mmkoqyZzzP9fPS6LD6i1xPy7UtAkX1YLi7 fee 0.00100446 BTC/kB, 0.000225 BTC for 224 bytes ... >>> SPENT: -0.005225 BTC total value (sends 0.01098238 BTC and receives 0.00575738 BTC) confidence: Seen by 5 peers (most recently: 2020-03-11T13:55:19Z). Appeared in best chain at height 1669539, depth 62. Source: NETWORK 905277665a3eb6cd1cf0af30b00f15db3ec6fc7ae86b3c150aa2fdcdb589d0b1 updated: 2020-03-11T13:41:17Z purpose: USER_PAYMENT in PUSHDATA(71)[304402201c175385ce3dd9ca411060949b3f5bfc16840623236aee73bb88002ab5cdb0fc0220797f97d2fedf72de4493e7c745cd7b0f55ee416a4f84adb95815d418545dfead01] PUSHDATA(33)[02c8508a80ac5c29b891a443b599e827155a71cb9ab702c3745a083bbbdc98155a] 0.01098238 BTC P2PKH addr:n3YE2rPKNhefnjrSHvEScXazNaSyeK52ML outpoint:18866d7e03a0b8958769382afe24ff7671fbc911cbdf3006fd1fbb3a3c49c4cf:0 out DUP HASH160 PUSHDATA(20)[c17fe3e9ea4561d70dc255a266d5eab4c8e10997] EQUALVERIFY CHECKSIG 0.00575738 BTC P2PKH addr:myA5v3sQo4VdhFGNJ9Gv22aRLnw7q7KhDw spent by:3b04ff0742625ad5123e3a6bc74b407fde68e098c97244bd6a413b9550effe34:0 out HASH160 PUSHDATA(20)[41a37afc980ac078df37b24f2a687102f79b0ab3] EQUAL 0.005 BTC P2SH addr:2MyEHka6Whdt87LyK3VuUGGfuWkNTtSD3Hj fee 0.00100896 BTC/kB, 0.000225 BTC for 223 bytes ... We created the watching wallet: bitcoinj02createwalletW.wallet Determistic watchingKey serializePubB58 (full) : tpubD8WRr3vpDwybtseRYzbP739wJw4vSMcRAvw6QifMh5ZNJTKwp5QRmymsjjLHp6kZqMPAVEqsZcAfqSApUS8mtLbwzyxAM5XFQMh3eZ73PRy Determistic watchingKey serializePubB58 (watching): tpubD8WRr3vpDwybtseRYzbP739wJw4vSMcRAvw6QifMh5ZNJTKwp5QRmymsjjLHp6kZqMPAVEqsZcAfqSApUS8mtLbwzyxAM5XFQMh3eZ73PRy ********************************************************************************** Dump the watching wallet before reorganisation bitcoinj02createwalletW.wallet Wallet Balances: 0.00 BTC ESTIMATED ... Transactions: 0 pending ... Last seen best block: 0 (time unknown): null Wallet is watching. Keys: Earliest creation time: 2020-02-29T09:44:25Z Key birthday: 1582969465 [2020-02-29T09:44:25Z] Ouput script type: P2PKH Key to watch: tpubD8WRr3vpDwybtseRYzbP739wJw4vSMcRAvw6QifMh5ZNJTKwp5QRmymsjjLHp6kZqMPAVEqsZcAfqSApUS8mtLbwzyxAM5XFQMh3eZ73PRy Lookahead siz/thr: 100/33 addr:mogznRmPEcfXBJimuGM8CojsWUtPVfQ3VM hash160:59a73e67ba416d45b46da3be18b9f3f0004ee364 (M/0H, account) ... Load the wallet in WalletAppKit: bitcoinj02createwalletW.wallet I'm going online to reimport the transactions, please be patient (maybe 1 to 2 minutes)... ********************************************************************************** Dump the watching wallet after reorganiation: bitcoinj02createwalletW.wallet Wallet Balances: 0.01646622 BTC ESTIMATED 0.01646622 BTC AVAILABLE 0.00 BTC ESTIMATED_SPENDABLE 0.00 BTC AVAILABLE_SPENDABLE Transactions: 0 pending 4 unspent 2 spent 0 dead Last seen best block: 1669830 (2020-03-15T12:40:32Z): 000000000000005cbc5daaf9e32636f46d1c16bfe166d9ec7640880a4f61dacf Wallet is watching. Keys: Earliest creation time: 2020-02-29T09:44:25Z Key birthday: 1582969465 [2020-02-29T09:44:25Z] Ouput script type: P2PKH Key to watch: tpubD6NzVbkztN62gkBwS81yzkMy1AZnVSpZbFnW1N19z86T7EXV9rs15Ln5Ym1M1Utk4BzH6q5vSDvGNQ9K1H72j3LTqw2JBbH6792WyMst7gc Lookahead siz/thr: 100/33 addr:mogznRmPEcfXBJimuGM8CojsWUtPVfQ3VM hash160:59a73e67ba416d45b46da3be18b9f3f0004ee364 (M/0H, account) ... >>> UNSPENT: 0.0001 BTC total value (sends 0.00 BTC and receives 0.0001 BTC) confidence: Appeared in best chain at height 1669682, depth 149. Source: NETWORK 147ceb133fb04577e6e724e46e8792042ba02bdd01086792505aec6583ffb9f5 updated: 2020-03-13T14:03:06Z version 2 time locked until block 1669680 purpose: UNKNOWN in PUSHDATA(22)[00148843535b110ab5f67b1d6388b350009ce2aaa934] unconnected outpoint:207cb72e6db8188ceb0d376bd0ae7327e7963ec2ddf52b96a42b6c268f16c8ea:1 sequence:fffffffe out DUP HASH160 PUSHDATA(20)[51fd9d4bf19749a00f87b5166bd4ebbd93e0661c] EQUALVERIFY CHECKSIG 0.0001 BTC P2PKH addr:mnzUr2qbm8gDQQoMjz3k9UhiPuNq6QDFA5 out HASH160 PUSHDATA(20)[7d987bfed51da64f7c621f3c38803f21835dca1a] EQUAL 0.08772392 BTC P2SH addr:2N4hK84XafPwXBi7tq21PBySQ2dp4umdiqc ... >>> SPENT: -0.005225 BTC total value (sends 0.01098238 BTC and receives 0.00575738 BTC) confidence: Appeared in best chain at height 1669539, depth 292. Source: NETWORK 905277665a3eb6cd1cf0af30b00f15db3ec6fc7ae86b3c150aa2fdcdb589d0b1 updated: 2020-03-11T13:55:45Z purpose: UNKNOWN in PUSHDATA(71)[304402201c175385ce3dd9ca411060949b3f5bfc16840623236aee73bb88002ab5cdb0fc0220797f97d2fedf72de4493e7c745cd7b0f55ee416a4f84adb95815d418545dfead01] PUSHDATA(33)[02c8508a80ac5c29b891a443b599e827155a71cb9ab702c3745a083bbbdc98155a] 0.01098238 BTC P2PKH addr:n3YE2rPKNhefnjrSHvEScXazNaSyeK52ML outpoint:18866d7e03a0b8958769382afe24ff7671fbc911cbdf3006fd1fbb3a3c49c4cf:0 out DUP HASH160 PUSHDATA(20)[c17fe3e9ea4561d70dc255a266d5eab4c8e10997] EQUALVERIFY CHECKSIG 0.00575738 BTC P2PKH addr:myA5v3sQo4VdhFGNJ9Gv22aRLnw7q7KhDw spent by:3b04ff0742625ad5123e3a6bc74b407fde68e098c97244bd6a413b9550effe34:0 out HASH160 PUSHDATA(20)[41a37afc980ac078df37b24f2a687102f79b0ab3] EQUAL 0.005 BTC P2SH addr:2MyEHka6Whdt87LyK3VuUGGfuWkNTtSD3Hj fee 0.00100896 BTC/kB, 0.000225 BTC for 223 bytes ... Converting was successfull |
Sourcecode:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
/* * Herkunft/Origin: http://javacrypto.bplaced.net/ * Programmierer/Programmer: Michael Fehr * Copyright/Copyright: Michael Fehr * Lizenttext/Licence: verschiedene Lizenzen / several licenses * getestet mit/tested with: Java Runtime Environment 11.0.5 x64 * verwendete IDE/used IDE: intelliJ IDEA 2019.3.1 * Datum/Date (dd.mm.jjjj): 15.03.2020 * Funktion: 05 wandelt das Wallet in ein Watching Wallet um * Function: 05 converts a wallet to a watching wallet * * Sicherheitshinweis/Security notice * Die Programmroutinen dienen nur der Darstellung und haben keinen Anspruch auf eine korrekte Funktion, * insbesondere mit Blick auf die Sicherheit ! * Prüfen Sie die Sicherheit bevor das Programm in der echten Welt eingesetzt wird. * The program routines just show the function but please be aware of the security part - * check yourself before using in the real world ! * * Sie benötigen diverse Bibliotheken (alle im Github-Archiv im Unterordner "libs") * You need a lot of libraries (see my Github-repository in subfolder "libs") * verwendete BitcoinJ-Bibliothek / used BitcoinJ Library: bitcoinj-core-0.15.6.jar * my Github-Repository: https://github.com/java-crypto/BitcoinJ * libs in my Github-Repo: https://github.com/java-crypto/BitcoinJ_Libraries * */ import org.bitcoinj.core.ECKey; import org.bitcoinj.core.NetworkParameters; import org.bitcoinj.crypto.DeterministicKey; import org.bitcoinj.kits.WalletAppKit; import org.bitcoinj.params.TestNet3Params; import org.bitcoinj.wallet.UnreadableWalletException; import org.bitcoinj.wallet.Wallet; import java.io.File; import java.io.IOException; public class BitcoinJ05ConvertWatchingWalletEn { static WalletAppKit kit; public static void main(String[] args) throws UnreadableWalletException, IOException { System.out.println("BitcoinJ 05 Create a watching wallet from a regular one"); String filenameFullWallet = "bitcoinj02createwallet.wallet"; String filenameWatchingWallet = "bitcoinj02createwalletW.wallet"; // check for existing filenameWatchingWallet files // files are .wallet and .spvchain boolean foundOldWatchingFiles = false; if (new File(filenameWatchingWallet).exists()) { System.out.println("Existing file needs to get deleted manually: " + filenameWatchingWallet); foundOldWatchingFiles = true; } if (new File(filenameWatchingWallet.replace(".wallet", ".spvchain")).exists()) { System.out.println("Existing file needs to get deleted manually: " + filenameWatchingWallet.replace(".wallet", ".spvchain")); foundOldWatchingFiles = true; } if (foundOldWatchingFiles == true) { System.out.println("Please delete the files above manually and restart the program. The program ends now."); System.exit(0); } System.out.println("\nWe are using the Bitcoin Testnet and not the Mainnet"); NetworkParameters netParams = TestNet3Params.get(); //NetworkParameters netParams = MainNetParams.get(); System.out.println("\nLoad the Full wallet: " + filenameFullWallet); final File fullWalletFile = new File(filenameFullWallet); Wallet fullWallet = null; if (fullWalletFile.exists()) { System.out.println("Loading successfull : " + filenameFullWallet); fullWallet = Wallet.loadFromFile(fullWalletFile); } else { System.out.println("File not found: " + filenameFullWallet); System.exit(0); } System.out.println("**********************************************************************************"); System.out.println("Dump the full wallet: " + filenameFullWallet); System.out.println(fullWallet.toString()); // create the watching wallet System.out.println("We created the watching wallet: " + filenameWatchingWallet); DeterministicKey fullWatchingKey = fullWallet.getWatchingKey(); System.out.println("Determistic watchingKey serializePubB58 (full) : " + fullWatchingKey.serializePubB58(netParams)); long fullKeyBirthday = fullWatchingKey.getCreationTimeSeconds(); DeterministicKey watchingWatchingKey = DeterministicKey.deserializeB58(fullWatchingKey.serializePubB58(netParams), netParams); System.out.println("Determistic watchingKey serializePubB58 (watching): " + watchingWatchingKey.serializePubB58(netParams)); Wallet watchingWallet = Wallet.fromWatchingKeyB58(netParams, watchingWatchingKey.serializePubB58(netParams), fullKeyBirthday); System.out.println("**********************************************************************************"); System.out.println("Dump the watching wallet before reorganisation " + filenameWatchingWallet); System.out.println(watchingWallet.toString()); watchingWallet.saveToFile(new File(filenameWatchingWallet)); watchingWallet.reset(); // force to reimport the spv-file // need an online access to reorg the spv-file and re-import the transactions System.out.println("Load the wallet in WalletAppKit: " + filenameWatchingWallet); System.out.println("I'm going online to reimport the transactions, please be patient (maybe 1 to 2 minutes)..."); kit = new WalletAppKit(netParams, new File("."), filenameWatchingWallet.replace(".wallet", "")) { @Override protected void onSetupCompleted() { if (wallet().getKeyChainGroupSize() < 1) { wallet().importKey(new ECKey()); } } }; kit.startAsync(); // start the communication kit.awaitRunning(); // wait for completion System.out.println("**********************************************************************************"); System.out.println("Dump the watching wallet after reorganiation: " + filenameWatchingWallet); System.out.println(kit.wallet().toString()); System.out.println("Converting was successfull"); } } |
You can find all codes to Bitcoin in my Github-Repository for download, please follow the link: https://github.com/java-crypto/BitcoinJ. All programs are runnable with Java 11 (eventually with Java 8) as well and got developed with intelliJ IDEA.
Last edit: 15.03.2020