[英]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.