7_load_keystore

command
v0.0.0-...-6e38dd8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2023 License: MIT Imports: 5 Imported by: 0

README

导入Keystore文件

导入Keystore文件的教程网络上较少,但它其实更安全,而且Go处理起来也非常方便。

具体的步骤和导入私钥差不多,只是多了个密码而已。为了模拟更真实的使用场景,我们这次直接从文件中读入。

假设Keystore文件用环境变量KEYSTORE_FILE指定,先打开文件。

f, err := os.Open(os.Getenv("KEYSTORE_FILE"))
if err != nil {
    log.Fatalf("open keystore file error: %s", err)
}
defer f.Close()

然后使用密码解锁文件,并生成可签名发送交易的transactor对象,这次使用的是NewTransactorWithChainID函数

auth, err := bind.NewTransactorWithChainID(f, os.Getenv("PASSWORD"), chainId)
if err != nil {
    log.Fatalf("failed to create authorized transactor: %v", err)
}

完整代码

package main

import (
	"context"
	"github.com/ethereum/go-ethereum/accounts/abi/bind"
	"github.com/ethereum/go-ethereum/ethclient"
	"log"
	"os"
)

func main() {
	apiKey := os.Getenv("INFURA_API_KEY")
	url := "https://mainnet.infura.io/v3/" + apiKey
	client, err := ethclient.Dial(url)
	if err != nil {
		log.Fatalf("could not connect to Infura with ethclient: %s", err)
	}
	ctx := context.Background()
	chainId, err := client.ChainID(ctx)
	if err != nil {
		log.Fatalf("get chainID error: %s", err)
	}

	f, err := os.Open(os.Getenv("KEYSTORE_FILE"))
	if err != nil {
		log.Fatalf("open keystore file error: %s", err)
	}
	defer f.Close()

	auth, err := bind.NewTransactorWithChainID(f, os.Getenv("PASSWORD"), chainId)
	if err != nil {
		log.Fatalf("failed to create authorized transactor: %v", err)
	}
	log.Printf("account load success, address: %s", auth.From)

	_ = auth
}

Keystore文件因为不明文存储私钥,所以更安全。没有密码光有文件解锁不了,而光有密码没有文件也不可能拿到私钥,所以给了我们分开存储的便利性。

如果有一天你怀疑密码或者Keystore文件可能被盗或者泄露了,还可以通过老密码和老文件,导入后重新生成新的Keystore文件和新密码,然后把老文件老密码彻底废弃不用。

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL