From 39cde2007ac26f048666f1ce06150075efd0967d Mon Sep 17 00:00:00 2001 From: rms rob Date: Mon, 4 Jul 2022 18:30:36 +0200 Subject: [PATCH] init w3w --- README.md | 30 ++++++++++++++++++++++++++++ go.mod | 18 +++++++++++++++++ go.sum | 23 ++++++++++++++++++++++ main.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..a22af7b --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +[![Go Reference](https://pkg.go.dev/badge/github.com/rrobrms/w3w.svg)](https://pkg.go.dev/github.com/rrobrms/w3w) +[![Go Report Card](https://goreportcard.com/badge/github.com/rrobrms/w3w)](https://goreportcard.com/report/github.com/rrobrms/w3w) +[![Coverage Status](https://coveralls.io/repos/github/rrobrms/w3w/badge.svg?branch=main)](https://coveralls.io/github/rrobrms/w3w?branch=main) + + +# Eth wallet CLI + +> Simple CLI to generate ether wallets from `go-ethereum` + +## Install + +> `go get github.com/rrobrms/w3w` + + +## usage + +```sh +$ w3w +$ 23ed07206ce891e433a0193879caa12b6f50b69qbe374ecrd5fed1c7658a90b6 0xC3c7902A11f79184D5138B047FD9a993633b642d +``` + +```sh +$ w3w -v +$ ╭─────────────┬──────────────────────────────────────────────────────────────────╮ +$ │ private key │ 23ed07206ce891e433a0193879caa12b6f50b69qbe374ecrd5fed1c7658a90b6 │ +$ ├─────────────├──────────────────────────────────────────────────────────────────┤ +$ │ address │ 0xC3c7902A11f79184D5138B047FD9a993633b642d │ +$ ╰─────────────┴──────────────────────────────────────────────────────────────────╯ +$ valid https://etherscan.io/address/0xC3c7902A11f79184D5138B047FD9a993633b642d +``` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8681cd4 --- /dev/null +++ b/go.mod @@ -0,0 +1,18 @@ +module github.com/rrobrms/w3w + +go 1.18 + +require golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d + +require ( + github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect + github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect + github.com/mattn/go-colorable v0.1.9 // indirect + github.com/mattn/go-isatty v0.0.14 // indirect +) + +require ( + github.com/ethereum/go-ethereum v1.10.20 + github.com/fatih/color v1.13.0 + golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..dd63caa --- /dev/null +++ b/go.sum @@ -0,0 +1,23 @@ +github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k= +github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU= +github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 h1:YLtO71vCjJRCBcrPMtQ9nqBsqpA1m5sE92cU+pd5Mcc= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeCxkaw7y45JueMRL4DIyJDKs= +github.com/ethereum/go-ethereum v1.10.20 h1:75IW830ClSS40yrQC1ZCMZCt5I+zU16oqId2SiQwdQ4= +github.com/ethereum/go-ethereum v1.10.20/go.mod h1:LWUN82TCHGpxB3En5HVmLLzPD7YSrEUFmFfN1nKkVN0= +github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= +github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= +github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U= +github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go new file mode 100644 index 0000000..64bd941 --- /dev/null +++ b/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "crypto/ecdsa" + "flag" + "fmt" + "log" + "regexp" + + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/crypto" + "github.com/fatih/color" +) + +func main() { + + privateKey, err := crypto.GenerateKey() + if err != nil { + log.Fatal(err) + } + privateKeyBytes := crypto.FromECDSA(privateKey) + privateKeyHex := hexutil.Encode(privateKeyBytes)[2:] + + publicKey := privateKey.Public() + publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) + if !ok { + log.Fatal("cannot assert type: publicKey is not of type *ecdsa.PublicKey") + } + address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex() + + re := regexp.MustCompile("^0x[0-9a-fA-F]{40}$") + var isValid string + if re.MatchString(address) { + isValid = color.GreenString("valid") + } else { + isValid = color.RedString("invalid") + } + + t := color.New(color.FgCyan,color.Bold) + g := color.New(color.FgHiWhite) + + var ( + verbose bool = false + verbose_usage string = "Get nice output" + ) + flag.BoolVar(&verbose, "v", verbose, verbose_usage) + flag.Parse() + + if verbose { + t.Println("╭─────────────┬──────────────────────────────────────────────────────────────────╮") + t.Printf("│ private key │ %v │\n", privateKeyHex) + t.Println("├─────────────├──────────────────────────────────────────────────────────────────┤") + t.Printf("│ address │ %v │\n", address) + t.Println("╰─────────────┴──────────────────────────────────────────────────────────────────╯") + g.Printf(" %s https://etherscan.io/address/%v \n", isValid, address) + } else { + fmt.Println(privateKeyHex, address) + } +}