# golang eth wallat https://zhuanlan.zhihu.com/p/367285757 https://www.cnblogs.com/chris-cp/p/9046830.html https://github.com/ethereum/go-ethereum https://geth.ethereum.org/docs/rpc/server https://gist.github.com/gaoxt/2db081dd2696669c2e3b3807155bf42a 我們ip wsl go 連線到 windows 所以必須透過cat 拿到 對外ip ```bash= root@HOME-X213212:~/golang/mvc# cat /etc/resolv.conf # This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf: # [network] # generateResolvConf = false nameserver 172.20.64.1 ``` 至於外部 geth server 必須要將 ip設為--rpcaddr 172.20.64.1 wsl 內部 golang 就可以連線到外部 windows geth server # geth 連線到兩年前的geth server ```go= ./geth --datadir data --networkid 123 --rpc --rpccorsdomain "*" --nodiscover --rpcapi="db,eth,net,web3,personal" --rpcaddr 172.20.64.1 --rpccorsdomain "*" --allow-insecure-unlock console --ipcpath geth.ipc --ws --wsapi "db,eth,net,web3,personal" ``` ![](https://i.imgur.com/mhti1vM.png) 創世塊 ```json= { "config": { "chainId": 123, "homesteadBlock": 0, "eip150Block": 0, "eip155Block": 0, "eip158Block": 0 }, "nonce": "0x0000000000000042", "difficulty": "0x010", "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", "coinbase": "0x0000000000000000000000000000000000000000", "timestamp": "0x00", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "extraData": "0x0000000000000000000000000000000000000000000000000000000000000000", "gasLimit": "0xffffffff", "alloc": { "0x21d10c5e114a959e6e92c6f5f0ffcbe7df7785f1": {"balance": "99999999999999999999999999999999"}, "0x0000000000000000000000000000000000000002": {"balance": "222222222"} } } ``` ``` eth.accounts ``` ``` ["0x21d10c5e114a959e6e92c6f5f0ffcbe7df7785f1", "0x4d545c6c4f6e56c1defd223a8ff202dde9845b54", "0x0ad5f82c818842fc7d71d5f3016a4be55cd59ac7", "0x1c3f3c25928a0522c5c521e1c033f9d3ac409980", "0x80a900292c87bd532a00f09880148904b1534644", "0x997e7ed4a3c9fe6b46b09ccf85ea0e1716713231"] ``` # getblance 以前java 還要引入ipc的檔案現在我們直接透過 外部開放的port 進行連線並查詢 ```go= client, err := ethclient.Dial("http://172.20.64.1:8545") if err != nil { log.Fatal(err) } fmt.Println("we have a connection") // _ = client // we'll use this in the upcoming sections account := common.HexToAddress("0x0000000000000000000000000000000000000002") balance, err := client.BalanceAt(context.Background(), account, nil) if err != nil { log.Fatal(err) } ``` 查詢創世塊 # geth transfer ![](https://i.imgur.com/iHkux1L.png) ``` personal.unlockAccount(eth.accounts[0]) var txHash = eth.sendTransaction({ from: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(5, "ether") }) eth.getTransaction(txHash) > miner.start(1) true // 等待交易塊被處理 > miner.stop() true ``` 我以為交易塊可能會影響挖礦速度 一共送了三個轉帳請求 ![](https://i.imgur.com/RDAQkj1.png) # golang transfer https://www.gushiciku.cn/pl/pETX/zh-tw ```go= func keyStoreToPrivateKey(privateKeyFile, password 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) fmt.Println(addr) return privKey, nil } func main(){ pk, err := keyStoreToPrivateKey("/root/golang/mvc/UTC--2020-01-03T05-58-33.219268500Z--21d10c5e114a959e6e92c6f5f0ffcbe7df7785f1", "654321") if err != nil { log.Fatal(err) } privateKey, err := crypto.HexToECDSA(pk) if err != nil { log.Fatal("privateKey ", err) } publicKey := privateKey.Public() publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) if !ok { log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey") } fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA) nonce, err := client.PendingNonceAt(context.Background(), fromAddress) if err != nil { log.Fatal(err) } value := big.NewInt(200) // in wei (1 eth) gasLimit := uint64(21000) // in units gasPrice, err := client.SuggestGasPrice(context.Background()) if err != nil { log.Fatal(err) } toAddress := common.HexToAddress("0x997e7ed4a3c9fe6b46b09ccf85ea0e1716713231") var data []byte tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data) chainID, err := client.NetworkID(context.Background()) if err != nil { log.Fatal(err) } signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey) if err != nil { log.Fatal(err) } err = client.SendTransaction(context.Background(), signedTx) if err != nil { log.Fatal(err) } fmt.Println("tx sent: %s", signedTx.Hash().Hex()) } ``` ![](https://i.imgur.com/eVPu1Nf.png) 交易id已經被收到 ![](https://i.imgur.com/qYiobsh.png) miner.start(4) ![](https://i.imgur.com/lGLD950.png) 已經收到eth 轉帳。