Golang 语言版本奇亚(Chia, XCH) 接口之获取主链相关信息(含最新块高)

yufei       4 月, 2 周 前       141

关于如何使用 curl 命令来获取奇亚(Chia, XCH) 接口之获取主链相关信息(含最新块高),请移步 https://www.twle.cn/t/19316#reply0

这里只贴上 Golang 使用 net/http 包来实现的版本

package main

import (
    "bytes"
    "crypto/tls"
    "crypto/x509"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

const url = "https://localhost:8555/get_blockchain_state"

func main() {

    pool := x509.NewCertPool()

    cliCrt, err := tls.LoadX509KeyPair("/Users/yufei/.chia/mainnet/config/ssl/full_node/private_full_node.crt",
        "/Users/yufei/.chia/mainnet/config/ssl/full_node/private_full_node.key")

    if err != nil {

        fmt.Println("Loadx509keypair err:", err)

        return

    }

    tr := &http.Transport{

        TLSClientConfig: &tls.Config{

            RootCAs: pool,

            Certificates:       []tls.Certificate{cliCrt},
            InsecureSkipVerify: true, //跳过验证服务端证书

        },
    }

    client := &http.Client{Transport: tr}

    data := make(map[string]string)
    b, _ := json.Marshal(data)

    resp, err := client.Post(url,
        "application/json",
        bytes.NewBuffer(b))
    if err != nil {
        fmt.Println(err)
    }
    defer resp.Body.Close()
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

重点有两个地方

  1. 设置证书

    pool := x509.NewCertPool()
    
    cliCrt, err := tls.LoadX509KeyPair("/Users/yufei/.chia/mainnet/config/ssl/full_node/private_full_node.crt",
        "/Users/yufei/.chia/mainnet/config/ssl/full_node/private_full_node.key")
    
    if err != nil {
    
        fmt.Println("Loadx509keypair err:", err)
    
        return
    
    }
    
  2. 设置不检查客户端证书和服务器证书

    tr := &http.Transport{
    
        TLSClientConfig: &tls.Config{
    
            RootCAs: pool,
    
            Certificates:       []tls.Certificate{cliCrt},
            InsecureSkipVerify: true, //跳过验证服务端证书
    
        },
    }
    
    client := &http.Client{Transport: tr}
    

输出结果如下

{"blockchain_state": {"difficulty": 1056, "genesis_challenge_initialized": true, "mempool_size": 10, "peak": {"challenge_block_info_hash": "0x9f16cd40f3520633550a8c6b3ed2f1763e4b7524835bc7fce7abdd8e8c3fbf04", "challenge_vdf_output": {"data": "0x00005d62f70f96305e7bf18987b6c2fdd39c54d4df25754cdad364bffc4bd267e4d7e9e799e965c0cdc01845a802da4177f955ce0aca52da0fcf6f1695c148e107052a24d766c33e37edbe8abb9c6195c6d59187674b97a5424144bdf05df0a7a1050100"}, "deficit": 0, "farmer_puzzle_hash": "0xd7fa58bf5fdef6063622e391b1a6e27bba83f4a93388166909fe7e882a7a5e22", "fees": null, "finished_challenge_slot_hashes": null, "finished_infused_challenge_slot_hashes": null, "finished_reward_slot_hashes": null, "header_hash": "0xd9683002e5a3681cad6927187796901b3af6f54befb6d7e5a2f5927fc033b8e3", "height": 371692, "infused_challenge_vdf_output": {"data": "0x030020d0308f0dcf12ea4d6c5d9fd9e18db7b7326875eba14b42ca4b9f72ca112927bb78e99f1eeb50c2679ad0283e9a447c190e5af7ecb58031d95413e191a253462fa3228ad81d57d353982b999b4f11a03edc4922bb3d987d8d7aa9af5e9a46030100"}, "overflow": false, "pool_puzzle_hash": "0xd7fa58bf5fdef6063622e391b1a6e27bba83f4a93388166909fe7e882a7a5e22", "prev_hash": "0x437f692f17a4257a8b878f0e0bb2a80380da0bd7996a5eebf85dec56dd264eed", "prev_transaction_block_hash": null, "prev_transaction_block_height": 371690, "required_iters": 142434, "reward_claims_incorporated": null, "reward_infusion_new_challenge": "0x7bc5bcb109c66e2ec68e4d9bdfb15ba10b2e43de96bff0f67a406900f615e9aa", "signage_point_index": 45, "sub_epoch_summary_included": null, "sub_slot_iters": 111673344, "timestamp": null, "total_iters": 1209271856226, "weight": 77768896}, "space": 17964932862054492160, "sub_slot_iters": 111673344, "sync": {"sync_mode": false, "sync_progress_height": 0, "sync_tip_height": 0, "synced": true}}, "success": true}
目前尚无回复
简单教程 = 简单教程,简单编程
简单教程 是一个关于技术和学习的地方
现在注册
已注册用户请 登入
关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2018 简单教程 twle.cn All Rights Reserved.