Last active
February 18, 2020 02:42
-
-
Save ysqi/cc02e7944e040cc1bd124904f7c8b043 to your computer and use it in GitHub Desktop.
以太坊获得账户地址过程演示,https://learnblockchain.cn/article/563
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/ecdsa" | |
"crypto/rand" | |
"fmt" | |
"os" | |
"github.com/ethereum/go-ethereum/crypto" | |
) | |
func main() { | |
for i := 0; i < 3; i++ { | |
showflow() | |
} | |
} | |
func showflow() { | |
p, err := ecdsa.GenerateKey(crypto.S256(), rand.Reader) | |
ck(err) | |
privateKey := crypto.FromECDSA(p) | |
publicKey := crypto.FromECDSAPub(&p.PublicKey) | |
addr := crypto.PubkeyToAddress(p.PublicKey) | |
fmt.Printf(` | |
私钥:%x | |
公钥: %x | |
Keccak256(公钥): %x | |
地址:%v | |
-----------------------------`, | |
privateKey, publicKey, crypto.Keccak256(publicKey[1:]), addr.String()) | |
//output | |
//----------------------------- | |
//私钥:25bb6b5586ada2636884f25b88406b8b38e7f494227972fdb43e503ee6e2f42a | |
//公钥: 041a85a9f5e5b07f8c5608171c6639d08a75c4f1c720d74fd31c75b9192b9d4e32ce8e4333ab0162afd00e8c95428e29c49f4799585e2310219ad40bad27b25a7b | |
//Keccak256(公钥): e3bd0a720c5b80f1de31c087d6802ca679d98183362ef646eed71283f912e181 | |
//地址:0xD6802Ca679D98183362EF646EeD71283F912e181 | |
//----------------------------- | |
} | |
func ck(err error) { | |
if err == nil { | |
return | |
} | |
fmt.Println(err) | |
os.Exit(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment