forked from OpenBazaar/spvwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.go
executable file
·169 lines (146 loc) · 3.67 KB
/
wallet.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package spvclient
import (
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
btc "github.com/btcsuite/btcutil"
hd "github.com/btcsuite/btcutil/hdkeychain"
"github.com/btcsuite/btcwallet/wallet/txrules"
"github.com/ontio/spvclient/chain"
"github.com/ontio/spvclient/log"
"github.com/ontio/spvclient/netserv"
"time"
)
type SPVWallet struct {
params *chaincfg.Params
repoPath string
Blockchain *chain.Blockchain
peerManager *netserv.PeerManager
wireService *netserv.WireService
running bool
config *netserv.PeerManagerConfig
}
const WALLET_VERSION = "0.1.0"
func NewSPVWallet(config *Config) (*SPVWallet, error) {
w := &SPVWallet{
repoPath: config.RepoPath,
params: config.Params,
}
var err error
if err != nil {
return nil, err
}
w.Blockchain, err = chain.NewBlockchain(w.repoPath, w.params, config.IsVote)
if err != nil {
return nil, err
}
minSync := 5
if config.TrustedPeer != nil {
minSync = 1
}
wireConfig := &netserv.WireServiceConfig{
Chain: w.Blockchain,
MinPeersForSync: minSync,
Params: w.params,
}
ws := netserv.NewWireService(wireConfig)
w.wireService = ws
getNewestBlock := func() (*chainhash.Hash, int32, error) {
sh, err := w.Blockchain.BestBlock()
if err != nil {
return nil, 0, err
}
h := sh.Header.BlockHash()
return &h, int32(sh.Height), nil
}
w.config = &netserv.PeerManagerConfig{
UserAgentName: config.UserAgent,
UserAgentVersion: WALLET_VERSION,
Params: w.params,
AddressCacheDir: config.RepoPath,
Proxy: config.Proxy,
GetNewestBlock: getNewestBlock,
MsgChan: ws.MsgChan(),
}
if config.TrustedPeer != nil {
w.config.TrustedPeer = config.TrustedPeer
}
w.peerManager, err = netserv.NewPeerManager(w.config)
if err != nil {
return nil, err
}
return w, nil
}
func (w *SPVWallet) Start() {
w.running = true
go w.wireService.Start()
go w.peerManager.Start()
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// API
//
//////////////
func (w *SPVWallet) CurrencyCode() string {
if w.params.Name == chaincfg.MainNetParams.Name {
return "btc"
} else {
return "tbtc"
}
}
func (w *SPVWallet) IsDust(amount int64) bool {
return txrules.IsDustAmount(btc.Amount(amount), 25, txrules.DefaultRelayFeePerKb)
}
func (w *SPVWallet) ChildKey(keyBytes []byte, chaincode []byte, isPrivateKey bool) (*hd.ExtendedKey, error) {
parentFP := []byte{0x00, 0x00, 0x00, 0x00}
var id []byte
if isPrivateKey {
id = w.params.HDPrivateKeyID[:]
} else {
id = w.params.HDPublicKeyID[:]
}
hdKey := hd.NewExtendedKey(
id,
keyBytes,
chaincode,
parentFP,
0,
0,
isPrivateKey)
return hdKey.Child(0)
}
func (w *SPVWallet) Params() *chaincfg.Params {
return w.params
}
func (w *SPVWallet) ChainTip() (uint32, chainhash.Hash) {
var ch chainhash.Hash
sh, err := w.Blockchain.BestBlock()
if err != nil {
return 0, ch
}
return sh.Height, sh.Header.BlockHash()
}
func (w *SPVWallet) Close() {
if w.running {
log.Info("Disconnecting from peers and shutting down")
w.peerManager.Stop()
w.Blockchain.Close()
w.wireService.Stop()
w.running = false
}
}
func (w *SPVWallet) ReSyncBlockchain(fromDate time.Time) {
w.Blockchain.Rollback(fromDate)
//w.txstore.PopulateAdrs()
w.wireService.Resync()
}
func (w *SPVWallet) ReSync() {
w.wireService.ResyncWithNil()
}
func (s *SPVWallet) Broadcast(tx *wire.MsgTx) error {
log.Debugf("Broadcasting tx %s to peers", tx.TxHash().String())
for _, p := range s.peerManager.ConnectedPeers() {
p.QueueMessageWithEncoding(tx, nil, wire.LatestEncoding)
}
return nil
}