-
Notifications
You must be signed in to change notification settings - Fork 82
/
coin.go
199 lines (174 loc) · 5.58 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright 2018 Shift Devices AG
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package btc
import (
"fmt"
"math/big"
"os"
"path"
"strings"
"sync"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/digitalbitbox/bitbox-wallet-app/backend/accounts/errors"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/btc/blockchain"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/btc/db/headersdb"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/btc/electrum"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/btc/headers"
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/coin"
"github.com/digitalbitbox/bitbox-wallet-app/util/errp"
"github.com/digitalbitbox/bitbox-wallet-app/util/logging"
"github.com/digitalbitbox/bitbox-wallet-app/util/observable"
"github.com/digitalbitbox/bitbox-wallet-app/util/observable/action"
"github.com/digitalbitbox/bitbox-wallet-app/util/rpc"
"github.com/digitalbitbox/bitbox-wallet-app/util/socksproxy"
"github.com/sirupsen/logrus"
)
// Coin models a Bitcoin-related coin.
type Coin struct {
initOnce sync.Once
code string
unit string
net *chaincfg.Params
dbFolder string
servers []*rpc.ServerInfo
blockExplorerTxPrefix string
socksProxy socksproxy.SocksProxy
observable.Implementation
blockchain blockchain.Interface
headers *headers.Headers
log *logrus.Entry
}
// NewCoin creates a new coin with the given parameters.
func NewCoin(
code string,
unit string,
net *chaincfg.Params,
dbFolder string,
servers []*rpc.ServerInfo,
blockExplorerTxPrefix string,
socksProxy socksproxy.SocksProxy,
) *Coin {
coin := &Coin{
code: code,
unit: unit,
net: net,
dbFolder: dbFolder,
servers: servers,
blockExplorerTxPrefix: blockExplorerTxPrefix,
socksProxy: socksProxy,
log: logging.Get().WithGroup("coin").WithField("code", code),
}
return coin
}
// Initialize implements coin.Coin.
func (coin *Coin) Initialize() {
coin.initOnce.Do(func() {
// Init blockchain
coin.blockchain = electrum.NewElectrumConnection(coin.servers, coin.log, coin.socksProxy)
// Init Headers
// delete old db version (up to v4.10.0, bbolt was used):
oldDBFilename := path.Join(coin.dbFolder, fmt.Sprintf("headers-%s.db", coin.code))
if _, err := os.Stat(oldDBFilename); err == nil {
_ = os.Remove(oldDBFilename)
}
db, err := headersdb.NewDB(
path.Join(coin.dbFolder, fmt.Sprintf("headers-%s.bin", coin.code)))
if err != nil {
coin.log.WithError(err).Panic("Could not open headers DB")
}
coin.headers = headers.NewHeaders(
coin.net,
db,
coin.blockchain,
coin.log)
coin.headers.Initialize()
coin.headers.SubscribeEvent(func(event headers.Event) {
if event == headers.EventSyncing || event == headers.EventSynced {
status, err := coin.headers.Status()
if err != nil {
coin.log.WithError(err).Error("Could not get headers status")
}
coin.Notify(observable.Event{
Subject: fmt.Sprintf("coins/%s/headers/status", coin.code),
Action: action.Replace,
Object: status,
})
}
})
})
}
// Code implements coin.Coin.
func (coin *Coin) Code() string {
return coin.code
}
// Net returns the coin's network params.
func (coin *Coin) Net() *chaincfg.Params {
return coin.net
}
// Unit implements coin.Coin.
func (coin *Coin) Unit(bool) string {
return coin.unit
}
// Decimals implements coin.Coin.
func (coin *Coin) Decimals(isFee bool) uint {
return 8
}
// FormatAmount implements coin.Coin.
func (coin *Coin) FormatAmount(amount coin.Amount, isFee bool) string {
return strings.TrimRight(strings.TrimRight(
new(big.Rat).SetFrac(amount.BigInt(), big.NewInt(unitSatoshi)).FloatString(8),
"0"), ".")
}
// ToUnit implements coin.Coin.
func (coin *Coin) ToUnit(amount coin.Amount, isFee bool) float64 {
result, _ := new(big.Rat).SetFrac(amount.BigInt(), big.NewInt(unitSatoshi)).Float64()
return result
}
// Blockchain connects to a blockchain backend.
func (coin *Coin) Blockchain() blockchain.Interface {
return coin.blockchain
}
// Headers returns the coin headers.
func (coin *Coin) Headers() *headers.Headers {
return coin.headers
}
func (coin *Coin) String() string {
return coin.code
}
// BlockExplorerTransactionURLPrefix implements coin.Coin.
func (coin *Coin) BlockExplorerTransactionURLPrefix() string {
return coin.blockExplorerTxPrefix
}
// SmallestUnit implements coin.Coin.
func (coin *Coin) SmallestUnit() string {
switch coin.code {
case "ltc", "tltc":
return "litoshi"
default:
return "satoshi"
}
}
// DecodeAddress decodes a btc/ltc address, checking that that the format matches the account coin
// type.
func (coin *Coin) DecodeAddress(address string) (btcutil.Address, error) {
btcAddress, err := btcutil.DecodeAddress(address, coin.Net())
if err != nil {
return nil, errp.WithStack(errors.ErrInvalidAddress)
}
if !btcAddress.IsForNet(coin.Net()) {
return nil, errp.WithStack(errors.ErrInvalidAddress)
}
return btcAddress, nil
}