~eliasnaur/giobtc

giobtc/wallet.go -rw-r--r-- 5.0 KiB
ed79701fElias Naur update gio version 1 year, 8 days ago
                                                                                
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// SPDX-License-Identifier: Unlicense OR MIT
package main

import (
	"bytes"
	"flag"
	"fmt"
	"log"
	"sync/atomic"
	"time"

	"gioui.org/widget/material"
	"github.com/btcsuite/btcd/btcjson"
	"github.com/btcsuite/btcd/chaincfg"
	"github.com/btcsuite/btcd/chaincfg/chainhash"
	"github.com/btcsuite/btcd/rpcclient"
	"github.com/btcsuite/btcd/txscript"
	"github.com/btcsuite/btcutil"
	"golang.org/x/exp/shiny/materialdesign/icons"
)

type Wallet struct {
	addr    btcutil.Address
	disc    chan struct{}
	done    chan struct{}
	events  chan Event
	balance btcutil.Amount
}

type Transaction struct {
	New    bool
	Hash   string
	amount btcutil.Amount
	time   time.Time
	added  time.Time
}

type Event interface {
	isWalletEvent()
}

type TransactionEvent struct {
	Trans Transaction
}

type ErrorEvent struct {
	Err error
}

var qrIcn *material.Icon

func init() {
	icn, err := material.NewIcon(icons.ContentAdd)
	if err != nil {
		log.Fatal(err)
	}
	qrIcn = icn
}

var (
	pubAddr = flag.String("addr", "tb1qy9cem33xwpttzdh7a3nsmqsyz8ytz2jz28w860", "bitcoin address")
	host    = flag.String("host", "localhost:18334", "btcd host")
)

const startHeight = 1582962

var bitcoinNet = &chaincfg.TestNet3Params

const (
	rpcUser = "Mk1Xfdws+n0OI6fARguxpLtZt48="
	rpcPass = "zZT4QgeqwADcEKcrkOJr/2x7760="
	rpcCert = `-----BEGIN CERTIFICATE-----
MIICmTCCAfugAwIBAgIRAJwxVLc1YHY3A35LaTAsS3MwCgYIKoZIzj0EAwQwNDEg
MB4GA1UEChMXYnRjZCBhdXRvZ2VuZXJhdGVkIGNlcnQxEDAOBgNVBAMTB3Rlc3Rt
YWMwHhcNMTkxMDE2MjAyMzU0WhcNMjkxMDE0MjAyMzU0WjA0MSAwHgYDVQQKExdi
dGNkIGF1dG9nZW5lcmF0ZWQgY2VydDEQMA4GA1UEAxMHdGVzdG1hYzCBmzAQBgcq
hkjOPQIBBgUrgQQAIwOBhgAEASBx1IWFOr6/y82v6nJHYB7tGHjHk7WpbEHxqxi2
raovxw4aM2d/gncuPNinMInP6JbRdvV30CYZ5/GrimZjuNRlARihFHbYQ6lkYLAy
wncw4Y7rFOWbmbj9YsFOgnUkhuTkBm3r56UHfeqaO7LbG+zVZo2/mBewkfkhFyEi
SXmpQPXmo4GqMIGnMA4GA1UdDwEB/wQEAwICpDAPBgNVHRMBAf8EBTADAQH/MIGD
BgNVHREEfDB6ggd0ZXN0bWFjgglsb2NhbGhvc3SHBH8AAAGHEAAAAAAAAAAAAAAA
AAAAAAGHEP6AAAAAAAAAAAAAAAAAAAGHEP6AAAAAAAAABMMVBQWbMICHBMCoVraH
EP6AAAAAAAAAhHcl//6k7zSHEP6AAAAAAAAA27SKa9kr6WkwCgYIKoZIzj0EAwQD
gYsAMIGHAkIBrl769uPROZS+KHnBS40kIIdGgwHcA88I8jv4udTphdGRrCOKNFsB
FAu7fv/4YmTAafgmUX0s66Jmg81tKGQRnbUCQQxvDfJYIdPG4nO3piAoihZY7g6U
+zUCmQjcegjk0jY0UpBN3ire+iNEkFMLm2CDtTGTgHZJH7DinyGMgqXb0Kc5
-----END CERTIFICATE-----`
)

func NewWallet(pubaddr string, host string) (*Wallet, error) {
	addr, err := btcutil.DecodeAddress(pubaddr, bitcoinNet)
	if err != nil {
		return nil, err
	}
	w := &Wallet{
		addr: addr,
	}
	ccfg := &rpcclient.ConnConfig{
		Host:         host,
		Endpoint:     "ws",
		User:         rpcUser,
		Pass:         rpcPass,
		Certificates: []byte(rpcCert),
	}
	w.Connect(ccfg)
	return w, nil
}

func (w *Wallet) Balance() btcutil.Amount {
	return btcutil.Amount(atomic.LoadInt64((*int64)(&w.balance)))
}

func (w *Wallet) Connect(cfg *rpcclient.ConnConfig) {
	w.disc = make(chan struct{})
	w.done = make(chan struct{})
	w.events = make(chan Event)
	go w.run(cfg)
}

func (w *Wallet) run(cfg *rpcclient.ConnConfig) {
	var cl *rpcclient.Client
	onerr := func(err error) {
		if w.disc == nil {
			return
		}
		log.Printf("wallet error: %v", err)
		w.events <- ErrorEvent{err}
	}
	rescanned := false
	connected := make(chan struct{})
	handlers := &rpcclient.NotificationHandlers{
		OnClientConnected: func() {
			connected <- struct{}{}
		},
		OnRescanFinished: func(hash *chainhash.Hash, height int32, blkTime time.Time) {
			rescanned = true
		},
		OnRecvTx: func(trans *btcutil.Tx, details *btcjson.BlockDetails) {
			for _, out := range trans.MsgTx().TxOut {
				// Extract and print details from the script.
				_, addresses, _, err := txscript.ExtractPkScriptAddrs(out.PkScript, bitcoinNet)
				if err != nil {
					onerr(err)
					return
				}
				if len(addresses) != 1 {
					continue
				}
				addr := addresses[0]
				if !bytes.Equal(addr.ScriptAddress(), w.addr.ScriptAddress()) {
					continue
				}
				atomic.AddInt64((*int64)(&w.balance), out.Value)
				var t time.Time
				if details != nil {
					t = time.Unix(details.Time, 0)
				} else {
					t = time.Now()
				}
				w.events <- TransactionEvent{
					Trans: Transaction{
						New:    rescanned,
						Hash:   trans.Hash().String(),
						amount: btcutil.Amount(out.Value),
						time:   t,
						added:  time.Now(),
					},
				}
			}
		},
	}
	var err error
	cl, err = rpcclient.New(cfg, handlers)
	if err != nil {
		onerr(err)
	}
	go func() {
		<-connected
		block, err := cl.GetBlockHash(startHeight)
		if err != nil {
			onerr(err)
			return
		}
		addrs := []btcutil.Address{w.addr}
		if err := cl.NotifyReceived(addrs); err != nil {
			log.Fatal(err)
		}
		if err := cl.Rescan(block, addrs, nil); err != nil {
			onerr(err)
			return
		}
	}()
	for {
		select {
		case <-w.disc:
			cl.Shutdown()
			w.done <- struct{}{}
			return
		}
	}
}

func (w *Wallet) disconnect() {
	w.disc <- struct{}{}
	<-w.done
}

func (t *Transaction) FormatAmount() string {
	return fmt.Sprintf("%d", t.amount)
	//return fmt.Sprintf("%d", t.amount)
}

func (t *Transaction) FormatTime() string {
	return t.time.Local().Format("2006-01-02 15:04:05")
}

func (e TransactionEvent) isWalletEvent() {}
func (e ErrorEvent) isWalletEvent()       {}