/
client_onchain.go
68 lines (61 loc) · 1.93 KB
/
client_onchain.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
package client
import (
"fmt"
"github.com/companyzero/bisonrelay/client/clientdb"
)
// OnchainRecvAddrForUser returns the on-chain receive address of the local
// wallet associated with the specified user. If acct is specified, addresses
// are generated from that account.
func (c *Client) OnchainRecvAddrForUser(uid UserID, acct string) (string, error) {
var addr string
err := c.dbView(func(tx clientdb.ReadTx) error {
var err error
addr, err = c.db.OnchainRecvAddrForUser(tx, uid)
return err
})
if err != nil {
return "", err
}
if addr != "" {
return addr, nil
}
if pc, ok := c.cfg.PayClient.(*DcrlnPaymentClient); ok {
newAddr, err := pc.NewReceiveAddress(c.ctx, acct)
if err != nil {
return "", fmt.Errorf("unable to generate new on-chain address: %v", err)
}
err = c.dbUpdate(func(tx clientdb.ReadWriteTx) error {
return c.db.UpdateOnchainRecvAddrForUser(tx, uid, newAddr.String())
})
if err != nil {
return "", err
}
return newAddr.String(), nil
} else {
return "", fmt.Errorf("payment client is not able to generate " +
"new receive addresses")
}
}
// UpdateOnchainRecvAddrForUser updates the on-chain receive address of the local
// wallet associated with the specified user. If addr is nil, then the current
// address is removed.
func (c *Client) UpdateOnchainRecvAddrForUser(uid UserID, addr string) error {
return c.dbUpdate(func(tx clientdb.ReadWriteTx) error {
return c.db.UpdateOnchainRecvAddrForUser(tx, uid, addr)
})
}
// UserWithOnchainRecvAddr returns the user that has the specified onchain
// receive address. If there is no user for the specified address, then this
// returns nil.
func (c *Client) UserWithOnchainRecvAddr(addr string) *UserID {
var uid *UserID
err := c.dbView(func(tx clientdb.ReadTx) error {
uid = c.db.UserWithOnchainRecvAddr(tx, addr)
return nil
})
if err != nil {
c.log.Warn("Unable to find user with onchain addr %s: %v", addr,
err)
}
return uid
}