-
Notifications
You must be signed in to change notification settings - Fork 41
/
multiwallet.go
125 lines (114 loc) · 3.39 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
package multiwallet
import (
"errors"
"strings"
"time"
eth "github.com/OpenBazaar/go-ethwallet/wallet"
"github.com/OpenBazaar/multiwallet/bitcoin"
"github.com/OpenBazaar/multiwallet/bitcoincash"
"github.com/OpenBazaar/multiwallet/client/blockbook"
"github.com/OpenBazaar/multiwallet/config"
"github.com/OpenBazaar/multiwallet/litecoin"
"github.com/OpenBazaar/multiwallet/service"
"github.com/OpenBazaar/multiwallet/zcash"
"github.com/OpenBazaar/wallet-interface"
"github.com/btcsuite/btcd/chaincfg"
"github.com/op/go-logging"
"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[wallet.CoinType]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(128)
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 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[wallet.Bitcoin] = w
} else {
multiwallet[wallet.TestnetBitcoin] = w
}
case 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[wallet.BitcoinCash] = w
} else {
multiwallet[wallet.TestnetBitcoinCash] = w
}
case 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[wallet.Zcash] = w
} else {
multiwallet[wallet.TestnetZcash] = w
}
case 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[wallet.Litecoin] = w
} else {
multiwallet[wallet.TestnetLitecoin] = w
}
case 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[wallet.Ethereum] = w
} else {
multiwallet[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
}