-
Notifications
You must be signed in to change notification settings - Fork 7
/
coin.go
98 lines (90 loc) · 3.09 KB
/
coin.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"errors"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
)
type Coin struct {
Name string `json:"name"`
Symbol string `json:"symbol"`
WIF string `json:"wif,omitempty"`
UncompressedAddress string `json:"uncompressed_address"`
CompressedAddress string `json:"compressed_address"`
}
type Network struct {
name string
symbol string
xpubkey byte
xprivatekey byte
magic wire.BitcoinNet
}
var network = map[string]Network{
"rdd": {name: "reddcoin", symbol: "rdd", xpubkey: 0x3d, xprivatekey: 0xbd, magic: 0xfbc0b6db},
"dgb": {name: "digibyte", symbol: "dgb", xpubkey: 0x1e, xprivatekey: 0x80, magic: 0xfac3b6da},
"btc": {name: "bitcoin", symbol: "btc", xpubkey: 0x00, xprivatekey: 0x80, magic: 0xf9beb4d9},
"ltc": {name: "litecoin", symbol: "ltc", xpubkey: 0x30, xprivatekey: 0xb0, magic: 0xfbc0b6db},
"dash": {name: "dash", symbol: "dash", xpubkey: 0x4c, xprivatekey: 0xcc, magic: 0xd9b4bef9},
}
func (network Network) GetNetworkParams() *chaincfg.Params {
networkParams := &chaincfg.MainNetParams
networkParams.Name = network.name
networkParams.Net = network.magic
networkParams.PubKeyHashAddrID = network.xpubkey
networkParams.PrivateKeyID = network.xprivatekey
return networkParams
}
func (coin *Coin) Generate(network Network) error {
if network == (Network{}) {
return errors.New("Unsupported cryptocurrency symbol provided")
}
secret, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
return err
}
wif, err := btcutil.NewWIF(secret, network.GetNetworkParams(), false)
if err != nil {
return err
}
coin.WIF = wif.String()
uncompressedAddress, err := btcutil.NewAddressPubKey(wif.PrivKey.PubKey().SerializeUncompressed(), network.GetNetworkParams())
if err != nil {
return err
}
compressedAddress, err := btcutil.NewAddressPubKey(wif.PrivKey.PubKey().SerializeCompressed(), network.GetNetworkParams())
if err != nil {
return err
}
coin.UncompressedAddress = uncompressedAddress.EncodeAddress()
coin.CompressedAddress = compressedAddress.EncodeAddress()
coin.Name = network.name
coin.Symbol = network.symbol
return nil
}
func (coin *Coin) Import(network Network) error {
if network == (Network{}) {
return errors.New("Unsupported cryptocurrency symbol provided")
}
wif, err := btcutil.DecodeWIF(coin.WIF)
if err != nil {
return err
}
if !wif.IsForNet(network.GetNetworkParams()) {
return errors.New("The WIF string is not valid for the `" + network.name + "` network")
}
uncompressedAddress, err := btcutil.NewAddressPubKey(wif.PrivKey.PubKey().SerializeUncompressed(), network.GetNetworkParams())
if err != nil {
return err
}
compressedAddress, err := btcutil.NewAddressPubKey(wif.PrivKey.PubKey().SerializeCompressed(), network.GetNetworkParams())
if err != nil {
return err
}
coin.WIF = wif.String()
coin.UncompressedAddress = uncompressedAddress.EncodeAddress()
coin.CompressedAddress = compressedAddress.EncodeAddress()
coin.Name = network.name
coin.Symbol = network.symbol
return nil
}