简体   繁体   English

如何通过 go-ethereum 创建 eth 帐户?

[英]How can I create eth account via go-ethereum?

I'm running local ethereum node on my localhost on http://127.0.0.1:7545 (using ganache).我在http://127.0.0.1:7545的本地主机上运行本地以太坊节点(使用 ganache)。 I create a new account with keystore as below snippet.我使用密钥库创建了一个新帐户,如下所示。 But, how can my local ethereum node can be aware of that new account?但是,我的本地以太坊节点如何知道这个新帐户? Normally, I can get balances, transactions etc... But I couldn't achieve to awareness of new account and managing them over my network via go-ethereum SDK.通常,我可以获得余额、交易等……但我无法通过go-ethereum SDK 通过我的网络了解新账户并管理它们。

func CreateAccount() {
    password := "secret"

    ks := keystore.NewKeyStore("./wallets", keystore.StandardScryptN, keystore.StandardScryptP)

    account, err := ks.NewAccount(password)

    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(account.Address.Hex())
}

In order to make go-ethereum talk to your Ganache client, you need to call Dial , which accepts a provider URL.为了让go-ethereum与您的 Ganache 客户端对话,您需要调用Dial ,它接受提供者 URL。 In the case described, that would be done as follows:在所描述的情况下,将按如下方式进行:

client, err := ethclient.Dial("http://localhost:7545")
if err != nil {
  log.Fatal(err)
}

So all together, you would have something like this along with what you are trying to accomplish in creating a new account and having Ganache see it:因此,总的来说,您将拥有类似的东西以及您在创建新帐户并让 Ganache 看到它时要完成的工作:

func main() {
    client, err := ethclient.Dial("http://localhost:7545")
    if err != nil {
        log.fatal(err)
    }

    fmt.Println("we have a connection")
}

func CreateAccount() {
    ks := keystore.NewKeyStore("./wallets", keystore.StandardScryptN, keystore.StandardScryptP)
    password := "secret"
    account, err := ks.NewAccount(password)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(account.Address.Hex())
}

A really great reference for all things go-ethereum is https://goethereumbook.org which walks through this and more step-by-step with full code examples. https://goethereumbook.org是关于所有go-ethereum的一个非常好的参考,它通过完整的代码示例逐步完成了这个和更多的步骤。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 AWS 区块链服务与 go-ethereum 一起使用? - How to use the AWS blockchain service with go-ethereum? 为什么命令“ go clean -n -r -igithub.com/ethereum/go-ethereum…”不起作用? - Why command “go clean -n -r -i github.com/ethereum/go-ethereum…” does not work? 在 go-ethereum 安装中创建命令? - Make command in go-ethereum installation? Go-Ethereum 中的两个未知参数 function - two unknow parameters in Go-Ethereum function 来自 go-ethereum 的智能合约解析问题的事件数据 - Event data from Smart Contract parsing issue with go-ethereum 没有 abi 的 go-ethereum 编码或解码输入数据 - go-ethereum encoding or decoding input data without abi 使用 glide 安装 go-ethereum 依赖项和tendermint 依赖项 - install go-ethereum dependencies and tendermint dependencies with glide 从 go-ethereum 实现 Ethereum Personal_sign (EIP-191) 给出了与 ethers.js 不同的签名 - Implementing Ethereum personal_sign (EIP-191) from go-ethereum gives different signature from ethers.js “不支持交易类型”尝试使用 Go-Ethereum、Solidity、Go 部署简单合约时。 不会发生在 Remix 中 - "transaction type not supported" When trying to deploy a simple contract using Go-Ethereum, Solidity, Go. Doesn't happen in Remix 如何直接从 Go 中的 GCP 服务帐户 JSON 密钥文件创建 kube.netes.Clientset? - How can I create a kubernetes.Clientset directly from a GCP service account JSON key file in Go?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM