1

我正在尝试使用 BitcoinJ 获取钱包余额,这是我正在尝试的代码,但它总是返回 0

BriefLogFormatter.init();
    NetworkParameters params ;
    String net = "testnet"; //choosing network
    if (net.equals("testnet")) {params = TestNet3Params.get();
    } else if (net.equals("regtest")) {params = RegTestParams.get();
    } else {params = MainNetParams.get();}

    String privKey = "key-here";
     BigInteger test = new BigInteger(privKey);
    ECKey key = ECKey.fromPrivate(test);//initializing key
     Wallet wallet = new Wallet(params);
     wallet.importKey(key); //puting key in wallet

     BlockStore blockStore = new MemoryBlockStore(params);
     BlockChain chain = new BlockChain(params, wallet, blockStore);
     PeerGroup peerGroup = new PeerGroup(params, chain);
     peerGroup.addWallet(wallet);
     peerGroup.startAsync(); //syncing with the blockchain

     
    System.out.println( wallet.getBalance().toString());//getting balance
4

2 回答 2

0

使用WalletAppKit类来处理所有样板:

WalletAppKit walletAppKit = WalletAppKit(params, ScriptType, FileDirectory)

walletAppKit.startAsync()
walletAppKit.awaitRunning()
walletAppKit.wallet().getBalance().toPlainString()
于 2021-02-28T06:18:45.070 回答
0

你应该在 startAsync 之后下载BlockChain

peerGroup.startAsync();
peerGroup.downloadBlockChain();
于 2021-01-16T10:37:10.957 回答