bitcoinj 1.连接测试网络

本文介绍如何使用比特币测试网络进行API测试,提供了获取测试比特币的方法及连接测试节点的Java代码示例,展示了通过测试网络获取区块信息的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

比特币提供了一份测试网络让开发者可以测试相关的api,我们可以通过  http://bitcoinfaucet.uo1.net/send.php 这个链接获得一些测试比特币。

测试网络:

1、https://testnet.blockchain.info

2、https://chain.so/

3、https://testnet.smartbit.com.au/

测试代码:

 System.out.println("Connecting to node");
        final NetworkParameters params = TestNet3Params.get();
        BlockStore blockStore = new MemoryBlockStore(params);
        BlockChain chain = new BlockChain(params, blockStore);
        PeerGroup peerGroup = new PeerGroup(params, chain);
//        if (!opts.has("localhost")) {
            peerGroup.addPeerDiscovery(new DnsDiscovery(params));
//        } else {
//            PeerAddress addr = new PeerAddress(params, InetAddress.getLocalHost());
//            peerGroup.addAddress(addr);
//        }
        peerGroup.start();
        peerGroup.waitForPeers(1).get();
        Peer peer = peerGroup.getConnectedPeers().get(0);

        // Retrieve a block through a peer
        Sha256Hash blockHash = Sha256Hash.wrap("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943");
        Future<Block> future = peer.getBlock(blockHash);
        System.out.println("Waiting for node to send us the requested block: " + blockHash);
        Block block = future.get();
        System.out.println(block);
        peerGroup.stopAsync();

运行结果如下:

block: 
   hash: 000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943
   version: 1
   previous block: 0000000000000000000000000000000000000000000000000000000000000000
   merkle root: 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
   time: 1296688602 (2011-02-02T23:16:42Z)
   difficulty target (nBits): 486604799
   nonce: 414098458
   with 1 transaction(s):
  4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
     == COINBASE TXN (scriptSig PUSHDATA(4)[ffff001d] PUSHDATA(1)[04] PUSHDATA(69)[5468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73])  (scriptPubKey PUSHDATA(65)[04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f] CHECKSIG)
 

bitcoinj项目富含完整demo 此项目使用maven构建,不会使用maven的同学,查看项目pom.xml文件,并在http://mvnrepository.com/下载相应的依赖jar包. demo:bitcoinj签名交易 /** * @param unSpentBTCList 未花费utxo集合 * @param from 发送者地址 * @param to 接收者地址 * @param privateKey 私钥 * @param value 发送金额.单位:聪 * @param fee 旷工费.单位:聪 * @return 签名之后未广播的原生交易字符串 * @throws Exception */ public static String signBTCTransactionData(List unSpentBTCList, String from, String to, String privateKey, long value, long fee) throws Exception { NetworkParameters networkParameters = null; // networkParameters = MainNetParams.get(); //测试网络 networkParameters = TestNet3Params.get(); Transaction transaction = new Transaction(networkParameters); DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(networkParameters, privateKey); ECKey ecKey = dumpedPrivateKey.getKey(); long totalMoney = 0; List utxos = new ArrayList(); //遍历未花费列表,组装合适的item for (UnSpentBTC us : unSpentBTCList) { if (totalMoney >= (value + fee)) break; UTXO utxo = new UTXO(Sha256Hash.wrap(us.getTxid()), us.getVout(), Coin.valueOf(us.getSatoshis()), us.getHeight(), false, new Script(Hex.decode(us.getScriptPubKey()))); utxos.add(utxo); totalMoney += us.getSatoshis(); } transaction.addOutput(Coin.valueOf(value), Address.fromBase58(networkParameters, to)); // transaction. //消费列表总金额 - 已经转账的金额 - 手续费 就等于需要返回给自己的金额了 long balance = totalMoney - value - fee; //输出-转给自己 if (balance > 0) { transaction.addOutput(Coin.valueOf(balance), Address.fromBase58(networkParameters, from)); } //输入未消费列表项 for (UTXO utxo : utxos) { TransactionOutPoint outPoint = new TransactionOutPoint(networkParameters, utxo.getIndex(), utxo.getHash());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

le4

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值