-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
55 lines (46 loc) · 1.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"os"
"github.com/bcext/cashutil"
"github.com/bcext/gcash/btcec"
"github.com/bcext/gcash/chaincfg"
"github.com/qshuai/tcolor"
)
var net = map[string]*chaincfg.Params{
"mainnet": &chaincfg.MainNetParams,
"testnet": &chaincfg.TestNet3Params,
"regtest": &chaincfg.RegressionNetParams,
}
func main() {
if len(os.Args) != 2 {
fmt.Println(tcolor.WithColor(tcolor.Red, "Usage: addr mainnet/testnet/regtest"))
return
}
var n *chaincfg.Params
n, ok := net[os.Args[1]]
if !ok {
fmt.Println(tcolor.WithColor(tcolor.Red, os.Args[1]+" not existed, should select from mainnet/testnet/regtest"))
return
}
priv, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
fmt.Println(tcolor.WithColor(tcolor.Red, "Create private key failed: "+err.Error()))
return
}
wif, err := cashutil.NewWIF(priv, n, true)
if err != nil {
fmt.Println(tcolor.WithColor(tcolor.Red, "Convert wif format private key failed: "+err.Error()))
return
}
pubKey := priv.PubKey()
pubKeyHash := cashutil.Hash160(pubKey.SerializeCompressed())
addr, err := cashutil.NewAddressPubKeyHash(pubKeyHash, n)
if err != nil {
fmt.Println(tcolor.WithColor(tcolor.Red, "Generate a new bitcoin-cash address failed: "+err.Error()))
return
}
fmt.Println("privkey key: ", tcolor.WithColor(tcolor.Green, wif.String()))
fmt.Println("base58 encoded address:", tcolor.WithColor(tcolor.Green, addr.EncodeAddress(false)))
fmt.Println("bech32 encoded address:", tcolor.WithColor(tcolor.Green, addr.EncodeAddress(true)))
}