-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsend.go
More file actions
75 lines (63 loc) · 1.64 KB
/
send.go
File metadata and controls
75 lines (63 loc) · 1.64 KB
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
package arksdk
import (
"context"
client "github.com/arkade-os/arkd/pkg/client-lib"
clientTypes "github.com/arkade-os/arkd/pkg/client-lib/types"
)
func (a *arkClient) SendOffChain(
ctx context.Context, receivers []clientTypes.Receiver,
) (string, error) {
if err := a.safeCheck(); err != nil {
return "", err
}
vtxos, err := a.getSpendableVtxos(ctx, false)
if err != nil {
return "", err
}
res, err := a.ArkClient.SendOffChain(ctx, receivers, client.WithVtxos(vtxos))
if err != nil {
return "", err
}
// mark vtxos as spent and add transaction to DB before unlocking the mutex
if err := a.saveSendTransaction(ctx, *res); err != nil {
return "", err
}
return res.Txid, nil
}
func (a *arkClient) getSpendableVtxos(
ctx context.Context, withRecoverable bool,
) ([]clientTypes.VtxoWithTapTree, error) {
a.dbMu.Lock()
spendableVtxos, err := a.store.VtxoStore().GetSpendableVtxos(ctx)
a.dbMu.Unlock()
if err != nil {
return nil, err
}
_, offchainAddrs, _, _, err := a.Wallet().GetAddresses(ctx)
if err != nil {
return nil, err
}
cfg, err := a.GetConfigData(ctx)
if err != nil {
return nil, err
}
vtxos := make([]clientTypes.VtxoWithTapTree, 0, len(spendableVtxos))
for _, offchainAddr := range offchainAddrs {
for _, v := range spendableVtxos {
if v.Unrolled || (!withRecoverable && v.IsRecoverable()) {
continue
}
vtxoAddr, err := v.Address(cfg.SignerPubKey, cfg.Network)
if err != nil {
return nil, err
}
if vtxoAddr == offchainAddr.Address {
vtxos = append(vtxos, clientTypes.VtxoWithTapTree{
Vtxo: v,
Tapscripts: offchainAddr.Tapscripts,
})
}
}
}
return vtxos, nil
}