-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathwallet.go
268 lines (231 loc) · 6.74 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/fiatjaf/cli/v3"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip60"
)
func prepareWallet(ctx context.Context, c *cli.Command) (*nip60.WalletStash, func(), error) {
kr, _, err := gatherKeyerFromArguments(ctx, c)
if err != nil {
return nil, nil, err
}
pk, err := kr.GetPublicKey(ctx)
if err != nil {
return nil, nil, err
}
relays := sys.FetchOutboxRelays(ctx, pk, 3)
wl := nip60.LoadStash(ctx, kr, sys.Pool, relays)
if wl == nil {
return nil, nil, fmt.Errorf("error loading wallet stash")
}
wl.Processed = func(evt *nostr.Event, err error) {
if err == nil {
logverbose("processed event %s\n", evt)
} else {
log("error processing event %s: %s\n", evt, err)
}
}
wl.PublishUpdate = func(event nostr.Event, deleted, received, change *nip60.Token, isHistory bool) {
desc := "wallet"
if received != nil {
desc = fmt.Sprintf("received from %s with %d proofs totalling %d",
received.Mint, len(received.Proofs), received.Proofs.Amount())
} else if change != nil {
desc = fmt.Sprintf("change from %s with %d proofs totalling %d",
change.Mint, len(change.Proofs), change.Proofs.Amount())
} else if deleted != nil {
desc = fmt.Sprintf("deleting a used token from %s with %d proofs totalling %d",
deleted.Mint, len(deleted.Proofs), deleted.Proofs.Amount())
} else if isHistory {
desc = "history entry"
}
log("- saving kind:%d event (%s)... ", event.Kind, desc)
first := true
for res := range sys.Pool.PublishMany(ctx, relays, event) {
cleanUrl, ok := strings.CutPrefix(res.RelayURL, "wss://")
if !ok {
cleanUrl = res.RelayURL
}
if !first {
log(", ")
}
first = false
if res.Error != nil {
log("%s: %s", color.RedString(cleanUrl), res.Error)
} else {
log("%s: ok", color.GreenString(cleanUrl))
}
}
log("\n")
}
<-wl.Stable
return wl, func() {
wl.Close()
}, nil
}
var wallet = &cli.Command{
Name: "wallet",
Usage: "manage nip60 cashu wallets (see subcommands)",
Description: "all wallet data is stored on Nostr relays, signed and encrypted with the given key, and reloaded again from relays on every call.\n\nthe same data can be accessed by other compatible nip60 clients.",
DisableSliceFlagSeparator: true,
Flags: defaultKeyFlags,
ArgsUsage: "<wallet-identifier>",
Action: func(ctx context.Context, c *cli.Command) error {
args := c.Args().Slice()
if len(args) != 1 {
return fmt.Errorf("must be called as `nak wallet <wallet-id>")
}
wl, closewl, err := prepareWallet(ctx, c)
if err != nil {
return err
}
for w := range wl.Wallets() {
if w.Identifier == args[0] {
stdout(w.DisplayName(), w.Balance())
break
}
}
closewl()
return nil
},
Commands: []*cli.Command{
{
Name: "list",
Usage: "lists existing wallets with their balances",
DisableSliceFlagSeparator: true,
Action: func(ctx context.Context, c *cli.Command) error {
wl, closewl, err := prepareWallet(ctx, c)
if err != nil {
return err
}
for w := range wl.Wallets() {
stdout(w.DisplayName(), w.Balance())
}
closewl()
return nil
},
},
{
Name: "tokens",
Usage: "lists existing tokens in this wallet with their mints and aggregated amounts",
DisableSliceFlagSeparator: true,
Action: func(ctx context.Context, c *cli.Command) error {
wl, closewl, err := prepareWallet(ctx, c)
if err != nil {
return err
}
args := c.Args().Slice()
if len(args) != 1 {
return fmt.Errorf("must be called as `nak wallet <wallet-id> tokens")
}
w := wl.EnsureWallet(ctx, args[0])
for _, token := range w.Tokens {
stdout(token.ID(), token.Proofs.Amount(), strings.Split(token.Mint, "://")[1])
}
closewl()
return nil
},
},
{
Name: "receive",
Usage: "takes a cashu token string as an argument and adds it to the wallet",
ArgsUsage: "<token>",
DisableSliceFlagSeparator: true,
Action: func(ctx context.Context, c *cli.Command) error {
args := c.Args().Slice()
if len(args) != 2 {
return fmt.Errorf("must be called as `nak wallet <wallet-id> receive <token>")
}
wl, closewl, err := prepareWallet(ctx, c)
if err != nil {
return err
}
w := wl.EnsureWallet(ctx, args[0])
if err := w.ReceiveToken(ctx, args[1]); err != nil {
return err
}
closewl()
return nil
},
},
{
Name: "send",
Usage: "prints a cashu token with the given amount for sending to someone else",
ArgsUsage: "<amount>",
DisableSliceFlagSeparator: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "mint",
Usage: "send from a specific mint",
},
},
Action: func(ctx context.Context, c *cli.Command) error {
args := c.Args().Slice()
if len(args) != 2 {
return fmt.Errorf("must be called as `nak wallet <wallet-id> send <amount>")
}
amount, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return fmt.Errorf("amount '%s' is invalid", args[1])
}
wl, closewl, err := prepareWallet(ctx, c)
if err != nil {
return err
}
w := wl.EnsureWallet(ctx, args[0])
opts := make([]nip60.SendOption, 0, 1)
if mint := c.String("mint"); mint != "" {
mint = "http" + nostr.NormalizeURL(mint)[2:]
opts = append(opts, nip60.WithMint(mint))
}
token, err := w.SendToken(ctx, amount, opts...)
if err != nil {
return err
}
stdout(token)
closewl()
return nil
},
},
{
Name: "pay",
Usage: "pays a bolt11 lightning invoice and outputs the preimage",
ArgsUsage: "<invoice>",
DisableSliceFlagSeparator: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "mint",
Usage: "pay from a specific mint",
},
},
Action: func(ctx context.Context, c *cli.Command) error {
args := c.Args().Slice()
if len(args) != 2 {
return fmt.Errorf("must be called as `nak wallet <wallet-id> pay <invoice>")
}
wl, closewl, err := prepareWallet(ctx, c)
if err != nil {
return err
}
w := wl.EnsureWallet(ctx, args[0])
opts := make([]nip60.SendOption, 0, 1)
if mint := c.String("mint"); mint != "" {
mint = "http" + nostr.NormalizeURL(mint)[2:]
opts = append(opts, nip60.WithMint(mint))
}
preimage, err := w.PayBolt11(ctx, args[1], opts...)
if err != nil {
return err
}
stdout(preimage)
closewl()
return nil
},
},
},
}