当前位置: 首页 > 工具软件 > BitcoinJ > 使用案例 >

bitcoinj 1.连接测试网络

岳杜吟
2023-12-01

比特币提供了一份测试网络让开发者可以测试相关的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)
 

 类似资料: