0

我正在尝试编写简单的程序,该程序将成为比特币支付的网络客户端。我使用 bitcoinj 作为库;但是出现错误。

2017-04-12 15:07:35.099 ERROR 14507 --- [inj user thread] 
org.bitcoinj.core.Context                : Performing thread fixup: 
you are accessing bitcoinj via a thread that has not had any context 
set on it.
2017-04-12 15:07:35.099 ERROR 14507 --- [inj user thread]     
org.bitcoinj.core.Context                : This error has been 
corrected for, but doing this makes your app less robust.
2017-04-12 15:07:35.099 ERROR 14507 --- [inj user thread] 
org.bitcoinj.core.Context                : You should use 
Context.propagate() or a ContextPropagatingThreadFactory.
2017-04-12 15:07:35.099 ERROR 14507 --- [inj user thread] 
org.bitcoinj.core.Context                : Please refer to the user 
guide for more information about this.
2017-04-12 15:07:35.099 ERROR 14507 --- [inj user thread] 
org.bitcoinj.core.Context                : Thread name is bitcoinj 
user thread.

我只想知道如何在 Spring 框架上的网页上实现比特币支付

@Component 公共类 BitcoinjService {

private final NetworkParameters params;
private final WalletAppKit kit;

@PreDestroy
private void close(){
    System.out.println("shutting down again");
    kit.stopAsync();
    kit.awaitTerminated();
}

public BitcoinjService() {
    params = TestNet3Params.get();
    kit = new WalletAppKit(params, new File("."), "walletappkit-example");
    kit.startAsync();
    kit.awaitRunning();

    kit.wallet().addCoinsReceivedEventListener((wallet, tx, prevBalance, newBalance) -> {
        System.out.println("-----> coins resceived: " + tx.getHashAsString());
        System.out.println("received: " + tx.getValue(wallet));
    });

    kit.wallet().addCoinsSentEventListener((wallet, tx, prevBalance, newBalance) -> System.out.println("coins sent"));

    kit.wallet().addKeyChainEventListener(keys -> System.out.println("new key added"));

    kit.wallet().addScriptsChangeEventListener((wallet, scripts, isAddingScripts) -> System.out.println("new script added"));

    kit.wallet().addTransactionConfidenceEventListener((wallet, tx) -> {
        System.out.println("-----> confidence changed: " + tx.getHashAsString());
        TransactionConfidence confidence = tx.getConfidence();
        System.out.println("new block depth: " + confidence.getDepthInBlocks());
    });

    // Ready to run. The kit syncs the blockchain and our wallet event listener gets notified when something happens.
    // To test everything we create and print a fresh receiving address. Send some coins to that address and see if everything works.
    System.out.println("send money to: " + kit.wallet().freshReceiveAddress().toString());

}


public String  getFreshReciveAddress() {
    Context.propagate(new Context(params));
    return kit.wallet().currentReceiveAddress().toString();
}

public String getBalance(){
    Context.propagate(new Context(params));
    return kit.wallet().getBalance().toFriendlyString();
}

public void sendTo(String address) {
    Context.propagate(new Context(params));
    // Get the address 1RbxbA1yP2Lebauuef3cBiBho853f7jxs in object form.
    Address targetAddress = Address.fromBase58(params, address);
    // Do the send of 1 BTC in the background. This could throw InsufficientMoneyException.
    Wallet.SendResult result = null;
    try {
        result = kit.wallet().sendCoins(kit.peerGroup(), targetAddress, Coin.parseCoin(kit.wallet().getBalance().toPlainString()).minus(Coin.parseCoin("0.0005")));//TODO: calulating fee
        kit.wallet().saveToFile(new File("TestFile"));
        result.broadcastComplete.get();
    } catch (InsufficientMoneyException | IOException | InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }

}
4

0 回答 0