拨打 jsonrpc 连接到比特币

发布于 2024-11-19 00:13:27 字数 454 浏览 4 评论 0原文

package main

import "rpc/jsonrpc"
import "fmt"

func main() {
    rc, e := jsonrpc.Dial("tcp", "user:pass@localhost:8332")
    if e != nil {fmt.Print(e);return;}

    var blocks float64
    rc.Call("getblockcount", "", &blocks)
    if e != nil {fmt.Print(e); return;}
    fmt.Print("%f blocks", blocks)
}

给我以下错误: 拨号 tcp user:pass@localhost:8332: 地址 user:pass@localhost:8332 中冒号太多

如何验证我的 rpc 连接?

package main

import "rpc/jsonrpc"
import "fmt"

func main() {
    rc, e := jsonrpc.Dial("tcp", "user:pass@localhost:8332")
    if e != nil {fmt.Print(e);return;}

    var blocks float64
    rc.Call("getblockcount", "", &blocks)
    if e != nil {fmt.Print(e); return;}
    fmt.Print("%f blocks", blocks)
}

Gives me the following error:
dial tcp user:pass@localhost:8332: too many colons in address user:pass@localhost:8332

How do I authenticate my rpc connection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ㄖ落Θ余辉 2024-11-26 00:13:27

Go rpc/jsonrpc 包(更一般地说,rpc 包)不支持身份验证。 jsonrpc.Dial 的有效字符串可以在底层 net 的第二个参数的文档中找到.拨号功能。

但我认为你还做出了一个很大的假设,即无论你尝试连接到什么系统(也许是比特币?)都支持 Go jsonrpc 协议,除非它是用 Go 编写的,否则几乎肯定不会。

The Go rpc/jsonrpc package (and more generally, the rpc package) does not support authentication. A valid string for the jsonrpc.Dial can be found in the documentation for the second argument of the underlying net.Dial function.

But I think you're also making a big assumption that whatever system you're trying to connect to (bitcoin maybe?) supports the Go jsonrpc protocol, which--unless it's written in Go--it almost assuredly does not.

仲春光 2024-11-26 00:13:27
testRequest := `{"jsonrpc": "1.0", "id":"", "method": "help", "params": []}`

request, e := http.NewRequest("POST", brpc.addr, strings.NewReader(testRequest))
request.SetBasicAuth(brpc.user, brpc.pass)

responce, e := brpc.c.Do(request)
// responce.Body has the result
testRequest := `{"jsonrpc": "1.0", "id":"", "method": "help", "params": []}`

request, e := http.NewRequest("POST", brpc.addr, strings.NewReader(testRequest))
request.SetBasicAuth(brpc.user, brpc.pass)

responce, e := brpc.c.Do(request)
// responce.Body has the result
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文