弹簧网页与比特币支付

时间:2017-04-12 12:12:28

标签: java spring spring-mvc bitcoinj

我正在尝试编写一个简单的程序,它将成为比特币支付的Web客户端。我使用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();
    }

}

0 个答案:

没有答案