forked from OpenBazaar/multiwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiwallet.go
143 lines (132 loc) · 4.31 KB
/
multiwallet.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package multiwallet
import (
"errors"
"github.com/phoreproject/multiwallet/phore"
"strings"
"time"
"github.com/OpenBazaar/wallet-interface"
"github.com/btcsuite/btcd/chaincfg"
"github.com/op/go-logging"
eth "github.com/phoreproject/go-ethwallet/wallet"
"github.com/phoreproject/multiwallet/bitcoin"
"github.com/phoreproject/multiwallet/bitcoincash"
"github.com/phoreproject/multiwallet/client/blockbook"
"github.com/phoreproject/multiwallet/config"
"github.com/phoreproject/multiwallet/litecoin"
"github.com/phoreproject/multiwallet/service"
"github.com/phoreproject/multiwallet/util"
"github.com/phoreproject/multiwallet/zcash"
"github.com/tyler-smith/go-bip39"
)
var log = logging.MustGetLogger("multiwallet")
var UnsuppertedCoinError = errors.New("multiwallet does not contain an implementation for the given coin")
type MultiWallet map[util.ExtCoinType]wallet.Wallet
func NewMultiWallet(cfg *config.Config) (MultiWallet, error) {
log.SetBackend(logging.AddModuleLevel(cfg.Logger))
service.Log = log
blockbook.Log = log
if cfg.Mnemonic == "" {
ent, err := bip39.NewEntropy(256)
if err != nil {
return nil, err
}
mnemonic, err := bip39.NewMnemonic(ent)
if err != nil {
return nil, err
}
cfg.Mnemonic = mnemonic
cfg.CreationDate = time.Now()
}
multiwallet := make(MultiWallet)
var err error
for _, coin := range cfg.Coins {
var w wallet.Wallet
switch coin.CoinType {
case util.CoinTypePhore:
var params chaincfg.Params
if cfg.Params.Name == phore.PhoreMainNetParams.Name {
params = phore.PhoreMainNetParams
} else {
params = phore.PhoreTestNetParams
}
w, err = phore.NewPhoreWallet(coin, cfg.Mnemonic, ¶ms, cfg.Proxy, cfg.Cache, cfg.DisableExchangeRates)
if err != nil {
return nil, err
}
if cfg.Params.Name == phore.PhoreMainNetParams.Name {
multiwallet[util.CoinTypePhore] = w
} else {
multiwallet[util.CoinTypePhoreTest] = w
}
case util.ExtendCoinType(wallet.Bitcoin):
w, err = bitcoin.NewBitcoinWallet(coin, cfg.Mnemonic, cfg.Params, cfg.Proxy, cfg.Cache, cfg.DisableExchangeRates)
if err != nil {
return nil, err
}
if cfg.Params.Name == chaincfg.MainNetParams.Name {
multiwallet[util.ExtendCoinType(wallet.Bitcoin)] = w
} else {
multiwallet[util.ExtendCoinType(wallet.TestnetBitcoin)] = w
}
case util.ExtendCoinType(wallet.BitcoinCash):
w, err = bitcoincash.NewBitcoinCashWallet(coin, cfg.Mnemonic, cfg.Params, cfg.Proxy, cfg.Cache, cfg.DisableExchangeRates)
if err != nil {
return nil, err
}
if cfg.Params.Name == chaincfg.MainNetParams.Name {
multiwallet[util.ExtendCoinType(wallet.BitcoinCash)] = w
} else {
multiwallet[util.ExtendCoinType(wallet.TestnetBitcoinCash)] = w
}
case util.ExtendCoinType(wallet.Zcash):
w, err = zcash.NewZCashWallet(coin, cfg.Mnemonic, cfg.Params, cfg.Proxy, cfg.Cache, cfg.DisableExchangeRates)
if err != nil {
return nil, err
}
if cfg.Params.Name == chaincfg.MainNetParams.Name {
multiwallet[util.ExtendCoinType(wallet.Zcash)] = w
} else {
multiwallet[util.ExtendCoinType(wallet.TestnetZcash)] = w
}
case util.ExtendCoinType(wallet.Litecoin):
w, err = litecoin.NewLitecoinWallet(coin, cfg.Mnemonic, cfg.Params, cfg.Proxy, cfg.Cache, cfg.DisableExchangeRates)
if err != nil {
return nil, err
}
if cfg.Params.Name == chaincfg.MainNetParams.Name {
multiwallet[util.ExtendCoinType(wallet.Litecoin)] = w
} else {
multiwallet[util.ExtendCoinType(wallet.TestnetLitecoin)] = w
}
case util.ExtendCoinType(wallet.Ethereum):
w, err = eth.NewEthereumWallet(coin, cfg.Params, cfg.Mnemonic, cfg.Proxy)
if err != nil {
return nil, err
}
if cfg.Params.Name == chaincfg.MainNetParams.Name {
multiwallet[util.ExtendCoinType(wallet.Ethereum)] = w
} else {
multiwallet[util.ExtendCoinType(wallet.TestnetEthereum)] = w
}
}
}
return multiwallet, nil
}
func (w *MultiWallet) Start() {
for _, wallet := range *w {
wallet.Start()
}
}
func (w *MultiWallet) Close() {
for _, wallet := range *w {
wallet.Close()
}
}
func (w *MultiWallet) WalletForCurrencyCode(currencyCode string) (wallet.Wallet, error) {
for _, wl := range *w {
if strings.EqualFold(wl.CurrencyCode(), currencyCode) || strings.EqualFold(wl.CurrencyCode(), "T"+currencyCode) {
return wl, nil
}
}
return nil, UnsuppertedCoinError
}