首页 > 解决方案 > 如何使用 bitcoinj 库离线或在本地主机上发送比特币

问题描述

我正在开发一个 POC 应用程序,我将拥有多个不同用户的比特币钱包。我正在寻找任何方式将比特币从一个钱包发送到本地主机本身的另一个钱包,而不使用网络来避免网络费用。为了更好地理解;看起来好像我在本地系统上有 2 个或更多比特币钱包文件,其中包含访问它的钱包的所有详细信息。有没有办法将比特币从一个钱包转移到另一个离线(不使用在线网络)。我尝试使用创建 2 个钱包钱包

WalletAppKit kit = new WalletAppKit(params, new File("./wallet"), filePrefix);
kit.startAsync();
kit.awaitRunning();

当我习惯不同时,这个创建的钱包filePrefix或者我可以创建它

Wallet wallet = new Wallet(params);
wallet.autoSaveFile(true);

现在我的机器上有 2 个钱包。我试图创建PeerGroup并尝试connectToLocalhost()但每次它的节目(下面是日志的一部分)

16:34:55.301 [bitcoinj user thread] DEBUG org.bitcoinj.core.BitcoinSerializer - Sending tx message: 0b110907747800000000000000000000e1000000cf9e6d5001000000010816112030be2164acba755cea8f9ea76acd405e3414518efbf04ce498955583000000006a473044022042177736c663fdc7dafe155999c4b4a6cdb63f1c6213c2a24a690da9f77a35c00220506f7238c534983c8d3d279cda4e9578c6036730f3ac043b68467fcf28597e1c0121037f86b44d10a225a21f6c3eddf918e4817ac240b7c3748c0cf631b9629bbc1fecffffffff02a0860100000000001976a914ea382d5c3e75cb3d73248e9eec0ab166782af95e88acc0280d00000000001976a914cb089faa36b2230bf0ec2e5d7052e529db6ce0be88ac00000000
16:34:55.303 [bitcoinj user thread] DEBUG org.bitcoinj.wallet.Wallet - Received tx we already saw in a block or created ourselves: 5df3592f352825b51cf3891e54e739bc6ce2f335570cf285b9fde594506d9ecf
16:34:55.303 [BlockingClient network thread for seed.testnet.bitcoin.sprovoost.nl/190.85.201.38:18333] INFO org.bitcoinj.core.Peer - Announcing to seed.testnet.bitcoin.sprovoost.nl/190.85.201.38:18333 as: /bitcoinj:0.15.6/earnography-server:1.0/
Peer connected
16:34:55.303 [BlockingClient network thread for seed.testnet.bitcoin.sprovoost.nl/190.85.201.38:18333] DEBUG org.bitcoinj.core.BitcoinSerializer - Sending version message: 0b11090776657273696f6e00000000007e000000effbf8657c1101000000000000000000d67c0c5e00000000000000000000000000000000000000000000ffffbe55c926479d000000000000000000000000000000000000ffff7f000001479d0000000000000000282f626974636f696e6a3a302e31352e362f6561726e6f6772617068792d7365727665723a312e302f0000000000
16:34:55.303 [bitcoinj user thread] INFO org.bitcoinj.core.PeerGroup - Attempting connection to [127.0.0.1]:18333     (1 connected, 29 pending, 28 max)
Hostname of peer is 128.199.150.233
16:34:55.304 [bitcoinj user thread] INFO org.bitcoinj.core.Peer - Request to fetch block 5df3592f352825b51cf3891e54e739bc6ce2f335570cf285b9fde594506d9ecf
16:34:55.305 [bitcoinj user thread] DEBUG org.bitcoinj.core.BitcoinSerializer - Sending getdata message: 0b11090767657464617461000000000025000000d92f1b100102000040cf9e6d5094e5fdb985f20c5735f3e26cbc39e7541e89f31cb52528352f59f35d
Waiting for node to send us the requested block: 5df3592f352825b51cf3891e54e739bc6ce2f335570cf285b9fde594506d9ecf
16:34:55.306 [BlockingClient network thread for localhost/127.0.0.1:18333] ERROR org.bitcoinj.net.BlockingClient - Error trying to open/read from connection: localhost/127.0.0.1:18333: Connection refused (Connection refused)
16:34:55.306 [BlockingClient network thread for localhost/127.0.0.1:18333] INFO org.bitcoinj.core.PeerGroup - [127.0.0.1]:18333: Peer died      (1 connected, 28 pending, 29 max)
16:34:55.333 [PeerGroup Thread] INFO org.bitcoinj.core.PeerGroup - Waiting 973 ms before next connect attempt to [2620:6e:a000:1:43:43:43:43]:18333
16:34:55.339 [BlockingClient network thread for testnet-seed.bitcoin.jonasschnelli.ch/18.194.127.176:18333] DEBUG org.bitcoinj.core.BitcoinSerializer - Received 102 byte 'version' message: 7f1101000c04000000000000d77c0c5e00000000000000000000000000000000000000000000ffff9d22b767c4bc0c0400000000000000000000000000000000000000000000000081254911a30bb465102f5361746f7368693a302e31372e312f8ffc180001
16:34:55.340 [BlockingClient network thread for testnet-seed.bitcoin.jonasschnelli.ch/18.194.127.176:18333] INFO org.bitcoinj.core.Peer - Peer{[18.194.127.176]:18333, version=70015, subVer=/Satoshi:0.17.1/, services=1036 (BLOOM, WITNESS, NETWORK_LIMITED), time=2020-01-01 16:34:55, height=1637519}

这是我的主类文件代码:

BriefLogFormatter.init();
        NetworkParameters params = TestNet3Params.get();
        try {
            String walletFileName = "wallet/forwarding-service-testnet.wallet";
            final File walletFile = new File(walletFileName);
            Wallet wallet = Wallet.loadFromFile(walletFile, null);

            WalletAppKit kit = new WalletAppKit(params, new File("./wallet"), "filePrefix");
        kit.startAsync();
        kit.awaitRunning();
        LegacyAddress to = LegacyAddress.fromBase58(params, "n2sPiCfFwjDmakuUDEPiTR9WKQkdkFAcaF");
        SendRequest req = SendRequest.to(to, Coin.parseCoin("0.001"));
        req.feePerKb = Coin.valueOf(0);
        req.ensureMinRequiredFee = false;
        try {

            Wallet.SendResult result = kit.wallet().sendCoins(kit.peerGroup(), req);
            System.out.println("Coins sent >> Transaction fee: " + result.tx.getFee());
        } catch (InsufficientMoneyException e) {
            System.out.println("Not enough coins in your wallet. Missing " + e.missing.getValue()
                    + " satoshis are missing (including fees)");
            System.out.println("Send money to: " + kit.wallet().currentReceiveAddress().toString());
        }
            peerGroup.stopAsync();

        } catch (Exception e) {
            e.printStackTrace();
        }

我可以在我的 localhost peergroup 上的 2 个或更多钱包之间转移比特币吗?

标签: javablockchainpeerbitcoinj

解决方案


推荐阅读