赞
踩
参考goethereumbook,通过go-ethereum包,rpc连接到简单私链上,导入A账户keystore文件,取出私钥,用于转账的签名(注意创世块的chainID),给B账户转账,在终端node1上开启挖矿,查询转账余额。
整个过程中自己有两个疑惑:
第一,Keystores每次操作都要NewKeyStore一个钱包,生成个新的,为啥不用原来那个继续操作?
第二,单位wei转eth的很多,eth转wei的很少,web3.js倒是有,不过用go怎么安全的转呢?
https://gist.github.com/gaoxt/2db081dd2696669c2e3b3807155bf42a
// keyStoreToPrivateKey 根据keystore文件和密码 获取私钥和地址 func keyStoreToPrivateKey(privateKeyFile, password *string) (string, string, error) { keyJSON, err := ioutil.ReadFile(*privateKeyFile) if err != nil { fmt.Println("read keyjson file failed:", err) } unlockedKey, err := keystore.DecryptKey(keyJSON, *password) if err != nil { return "", "", err } privKey := hex.EncodeToString(unlockedKey.PrivateKey.D.Bytes()) addr := crypto.PubkeyToAddress(unlockedKey.PrivateKey.PublicKey) return privKey, addr.String(), nil }
//EthToWei eth单位安全转wei //https://stackoverrun.com/cn/q/13021596 func EthToWei(val float64) *big.Int { bigval := new(big.Float) bigval.SetFloat64(val) // Set precision if required. // bigval.SetPrec(64) coin := new(big.Float) coin.SetInt(big.NewInt(1000000000000000000)) bigval.Mul(bigval, coin) result := new(big.Int) bigval.Int(result) // store converted number in result return result }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。