我正在尝试学习bitcoinj,并编写了下面的测试代码。我在以下网站上创建了一个帐户:
http://tpfaucet.appspot.com/
所以我可以用假硬币和测试发送/接收。我的帐户有14个假BTC显示时,我登录的网址。但是,下面的代码显示我有0枚硬币。有人能帮我理解我错过了什么吗?我使用getBalance和getWatchedBalance都没有运气。
public class CheckBalance {
public static void main(String[] args) throws Exception {
// This line makes the log output more compact and easily read, especially when using the JDK log adapter.
BriefLogFormatter.init();
// Figure out which network we should connect to. Each one gets its own set of files.
final NetworkParameters params = TestNet3Params.get();
final String filePrefix = "forwarding-service-testnet";
// Parse the address given as the first parameter.
final Address forwardingAddress = new Address(params, "bogusHash"); //note, I replace bogusHash when I really run
// Start up a basic app using a class that automates some boilerplate.
final WalletAppKit kit = new WalletAppKit(params, new File("."), filePrefix);
// Download the block chain and wait until it's done.
kit.startAndWait();
System.out.println("You have : " +kit.wallet().getWatchedBalance() + " bitcoins");
System.exit(0);
}
}发布于 2014-03-22 22:02:33
在分配forwardingAdress之后,您不会使用它。我想这不是钱包里一个酒吧的入口吧?
要获得钱包中还没有的入口的余额,您可以执行以下操作:
kit.wallet().addKey(new ECKey(null, Hex.decode("0210fdade86b268597e9aa4f2adc314fe459837be831aeb532f04b32c160b4e50a")));kit.setAutoSave(true);现在你有了你想知道钱包里余额的公钥匙。删除Blockchain文件:forwarding-service-testnet.spv链
您有一个公钥,没有创建日期在您的钱包和Blockchain下载将花费更长的时间。但最终你应该看到一个适当的平衡。
WalletAppKit不是用来检查它没有生成或添加的键盘余额。这就是为什么这是更复杂,因为它应该是。
您可以创建一个普通钱包,添加键,将其添加到PeerGroup和SPVBlockschain中,然后开始下载块以获取计算余额所需的信息。每当您将密钥添加到钱包中时,如果密钥比最后一个块要老,则必须重新下载SPVBlockchain。
发布于 2014-07-09 08:13:56
在您的代码中,您正在检查钱包的余额(它还没有包含任何地址),而不是您创建的地址的余额。您应该尝试将您从网络创建的地址的密钥导入您在本地创建的钱包,然后再将该地址的余额显示在您的钱包中。
发布于 2017-09-04 14:55:46
您可以使用
WalletAppKit kit = new WalletAppKit(params, new File("."), "walletappkit2");
System.out.println("you have got :" + MonetaryFormat.BTC.noCode().format(kit.wallet().getBalance()).toString());https://stackoverflow.com/questions/22186273
复制相似问题