mirror of
https://github.com/fiatjaf/nak.git
synced 2025-08-10 21:20:47 -04:00
wallet pay and fix missing tokens because the program was exiting before they were saved.
This commit is contained in:
2
go.mod
2
go.mod
@@ -14,7 +14,7 @@ require (
|
||||
github.com/json-iterator/go v1.1.12
|
||||
github.com/mailru/easyjson v0.9.0
|
||||
github.com/markusmobius/go-dateparser v1.2.3
|
||||
github.com/nbd-wtf/go-nostr v0.49.0
|
||||
github.com/nbd-wtf/go-nostr v0.49.2
|
||||
)
|
||||
|
||||
require (
|
||||
|
4
go.sum
4
go.sum
@@ -124,8 +124,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/nbd-wtf/go-nostr v0.49.0 h1:/oDdOaZf2oFP0NDh48MFNMjiEO99hNxS+P+OrvIDRsc=
|
||||
github.com/nbd-wtf/go-nostr v0.49.0/go.mod h1:M50QnhkraC5Ol93v3jqxSMm1aGxUQm5mlmkYw5DJzh8=
|
||||
github.com/nbd-wtf/go-nostr v0.49.2 h1:nOiwo/M20bYczU8pwLtosR+hGcSIaCdgoqiy6DTYJ+M=
|
||||
github.com/nbd-wtf/go-nostr v0.49.2/go.mod h1:M50QnhkraC5Ol93v3jqxSMm1aGxUQm5mlmkYw5DJzh8=
|
||||
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
|
178
wallet.go
178
wallet.go
@@ -4,68 +4,118 @@ 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, error) {
|
||||
func prepareWallet(ctx context.Context, c *cli.Command) (*nip60.WalletStash, func(), error) {
|
||||
kr, _, err := gatherKeyerFromArguments(ctx, c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
pk, err := kr.GetPublicKey(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
relays := sys.FetchOutboxRelays(ctx, pk, 3)
|
||||
wl := nip60.LoadStash(ctx, kr, sys.Pool, relays)
|
||||
if wl == nil {
|
||||
return nil, fmt.Errorf("error loading wallet stash")
|
||||
return nil, nil, fmt.Errorf("error loading wallet stash")
|
||||
}
|
||||
|
||||
go func() {
|
||||
for err := range wl.Processed {
|
||||
if err == nil {
|
||||
// event processed ok
|
||||
} else {
|
||||
log("processing error: %s\n", err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
go func() {
|
||||
for evt := range wl.Changes {
|
||||
for res := range sys.Pool.PublishMany(ctx, relays, evt) {
|
||||
if res.Error != nil {
|
||||
log("error saving kind:%d event to %s: %s\n", evt.Kind, res.RelayURL, err)
|
||||
} else {
|
||||
log("saved kind:%d event to %s\n", evt.Kind, res.RelayURL)
|
||||
}
|
||||
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, nil
|
||||
return wl, func() {
|
||||
wl.Close()
|
||||
}, nil
|
||||
}
|
||||
|
||||
var wallet = &cli.Command{
|
||||
Name: "wallet",
|
||||
Usage: "manage nip60 Cashu wallets",
|
||||
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: "list existing Cashu wallets",
|
||||
Usage: "lists existing wallets with their balances",
|
||||
DisableSliceFlagSeparator: true,
|
||||
Action: func(ctx context.Context, c *cli.Command) error {
|
||||
wl, err := prepareWallet(ctx, c)
|
||||
wl, closewl, err := prepareWallet(ctx, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -74,15 +124,16 @@ var wallet = &cli.Command{
|
||||
stdout(w.DisplayName(), w.Balance())
|
||||
}
|
||||
|
||||
closewl()
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "tokens",
|
||||
Usage: "list existing tokens in this wallet",
|
||||
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, err := prepareWallet(ctx, c)
|
||||
wl, closewl, err := prepareWallet(ctx, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -92,18 +143,19 @@ var wallet = &cli.Command{
|
||||
return fmt.Errorf("must be called as `nak wallet <wallet-id> tokens")
|
||||
}
|
||||
|
||||
w := wl.EnsureWallet(args[0])
|
||||
w := wl.EnsureWallet(ctx, args[0])
|
||||
|
||||
for _, token := range w.Tokens {
|
||||
stdout(token.ID(), token.Proofs.Amount(), token.Mint)
|
||||
stdout(token.ID(), token.Proofs.Amount(), strings.Split(token.Mint, "://")[1])
|
||||
}
|
||||
|
||||
closewl()
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "receive",
|
||||
Usage: "receive a cashu token",
|
||||
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 {
|
||||
@@ -112,25 +164,32 @@ var wallet = &cli.Command{
|
||||
return fmt.Errorf("must be called as `nak wallet <wallet-id> receive <token>")
|
||||
}
|
||||
|
||||
wl, err := prepareWallet(ctx, c)
|
||||
wl, closewl, err := prepareWallet(ctx, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w := wl.EnsureWallet(args[0])
|
||||
w := wl.EnsureWallet(ctx, args[0])
|
||||
|
||||
if err := w.ReceiveToken(ctx, args[1]); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
closewl()
|
||||
return nil
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "send",
|
||||
Usage: "send a cashu token",
|
||||
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 {
|
||||
@@ -141,20 +200,67 @@ var wallet = &cli.Command{
|
||||
return fmt.Errorf("amount '%s' is invalid", args[1])
|
||||
}
|
||||
|
||||
wl, err := prepareWallet(ctx, c)
|
||||
wl, closewl, err := prepareWallet(ctx, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w := wl.EnsureWallet(args[0])
|
||||
w := wl.EnsureWallet(ctx, args[0])
|
||||
|
||||
token, err := w.SendToken(ctx, amount)
|
||||
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
|
||||
},
|
||||
},
|
||||
|
Reference in New Issue
Block a user