以太坊生成私钥并获取其地址查询余额

偶然间突然想碰碰运气,生成eth私钥,然后拿它的地址去获取主网上的余额,如果有就保存私钥,虽然这种概率非常小,但也并不是不可能,嘻嘻

相信很多人都有过这个想法,废话不多说,直接上代码:

package main

import (
	"myproject/eth_utils"
	"strings"
	"net/http"
	"log"
	"io/ioutil"
	"encoding/json"
	"fmt"

	"sync"
	"crypto/tls"
	"time"
	"net/url"
)

var priv_and_addr_chan = make(chan map[string]string,1000)


//查看api,可容addr数量
func main() {




	var wg sync.WaitGroup
	//for i:=0;i<10;i++  {
	//
		wg.Add(1)
		go func() {

			for v:=range priv_and_addr_chan{
	THERE:

				var  addrs string
				for addr,_:= range v  {
					addrs=addrs+addr+","
				}
				addrs = strings.TrimRight(addrs, ",")


				ethurl:="https://api.etherscan.io/api?module=account&action=balancemulti&address=" + addrs + "&tag=latest"

				proxyUrl := "http://114.249.115.89:9000"
				proxy, _ := url.Parse(proxyUrl)
				tr := &http.Transport{
					Proxy:           http.ProxyURL(proxy),
					TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
				}
				client := &http.Client{
					Transport: tr,
					Timeout:   time.Second * 10, //超时时间
				}
				resp, err := client.Get(ethurl)
				if err != nil {
					fmt.Println("出错了", err)
					return
				}


				if err!=nil{
					log.Fatal(err)
					goto THERE
				}
				defer resp.Body.Close()
				body, err := ioutil.ReadAll(resp.Body)
				if err != nil {
					log.Fatal(err)
					goto THERE
				}
				var i []interface{}

				unmarshal := json.Unmarshal(body, &i)
				fmt.Println(unmarshal)

			}
			wg.Done()
		}()





wg.Add(1)
	go func() {

		for  {

			priv_addr_map := make(map[string]string)
			for i:=0;i<20 ;i++  {
				priv, addr := eth_utils.GetEthPrivAndAddr()
				priv_addr_map[addr]=priv

			}

			priv_and_addr_chan <- priv_addr_map
		}
		wg.Done()
	}()
	wg.Wait()

}

package eth_utils

import (
	"crypto/ecdsa"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/crypto"
	"log"
	"math/rand"
	"fmt"
	"time"
	crand "crypto/rand"
)





func GetEthAddrFromPriv(priv string) ( addr common.Address) {


	privateKey, err := crypto.HexToECDSA(priv)
	if err != nil {
		log.Fatal(err)
	}

	publicKey := privateKey.Public()
	publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
	if !ok {
		log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey")
	}
	addr = crypto.PubkeyToAddress(*publicKeyECDSA)

	return
}


func GetEthPrivsAndAddrs(num int)(privs ,addrs []string) {

	rand.Seed(time.Now().Unix())
	for i := 0; i < num; i++ {

		priv, addr := GetEthPrivAndAddr()

		privs= append(privs,priv)

		addrs = append(addrs, addr)

	}

	return
}

func GetEthPrivAndAddr()(priv,addr string){
HERE:
	privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), crand.Reader)

	if err != nil {
		return
	}
	priv = fmt.Sprintf("%x", privateKeyECDSA.D.Bytes())

	if len(priv)!=64{
		goto HERE
	}
	addr =  GetEthAddrFromPriv(priv).String()
	return
}

 

你可能感兴趣的:(以太坊笔记整理)