Skip to content

Commit 40a934e

Browse files
committed
fix conflicts with running bunker connections (client and server apparently) through auth-required relays, clarify "connect-as" and use a special flag for it, let's see.
hopefully fixes #116
1 parent 1200023 commit 40a934e

6 files changed

Lines changed: 76 additions & 37 deletions

File tree

flags.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,45 @@ func getNaturalDate(cmd *cli.Command, name string) nostr.Timestamp {
131131
//
132132
//
133133

134+
type (
135+
SecretKeyFlag = cli.FlagBase[nostr.SecretKey, struct{}, secretkeyValue]
136+
)
137+
138+
type secretkeyValue struct {
139+
secretkey nostr.SecretKey
140+
hasBeenSet bool
141+
}
142+
143+
var _ cli.ValueCreator[nostr.SecretKey, struct{}] = secretkeyValue{}
144+
145+
func (t secretkeyValue) Create(val nostr.SecretKey, p *nostr.SecretKey, c struct{}) cli.Value {
146+
*p = val
147+
return &secretkeyValue{
148+
secretkey: val,
149+
}
150+
}
151+
152+
func (t secretkeyValue) ToString(b nostr.SecretKey) string { return t.secretkey.String() }
153+
154+
func (t *secretkeyValue) Set(value string) error {
155+
secretkey, err := parseSecretKey(value)
156+
t.secretkey = secretkey
157+
t.hasBeenSet = true
158+
return err
159+
}
160+
161+
func (t *secretkeyValue) String() string { return fmt.Sprintf("%#v", t.secretkey) }
162+
func (t *secretkeyValue) Value() nostr.SecretKey { return t.secretkey }
163+
func (t *secretkeyValue) Get() any { return t.secretkey }
164+
165+
func getSecretKey(cmd *cli.Command, name string) nostr.SecretKey {
166+
return cmd.Value(name).(nostr.SecretKey)
167+
}
168+
169+
//
170+
//
171+
//
172+
134173
type (
135174
PubKeyFlag = cli.FlagBase[nostr.PubKey, struct{}, pubkeyValue]
136175
)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/fiatjaf/nak
33
go 1.25
44

55
require (
6-
fiatjaf.com/nostr v0.0.0-20260615112943-0616b30ab35c
6+
fiatjaf.com/nostr v0.0.0-20260620232658-8389bac80c5f
77
github.com/AlecAivazis/survey/v2 v2.3.7
88
github.com/bep/debounce v1.2.1
99
github.com/btcsuite/btcd/btcec/v2 v2.3.6

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fiatjaf.com/lib v0.3.7 h1:mXZOn7NrUcjSdy4oNvwQyAmes7Ueb+Zr5hjqMIe2dxI=
22
fiatjaf.com/lib v0.3.7/go.mod h1:UlHaZvPHj25PtKLh9GjZkUHRmQ2xZ8Jkoa4VRaLeeQ8=
3-
fiatjaf.com/nostr v0.0.0-20260615112943-0616b30ab35c h1:LyUM+6Z8e51OyIRriZooY3+W0ogWBUJe0B/nU1uD04o=
4-
fiatjaf.com/nostr v0.0.0-20260615112943-0616b30ab35c/go.mod h1:b1EIUDnd133Ie8Pg8O/biaKdFyCMz28aD4n64g1GqvM=
3+
fiatjaf.com/nostr v0.0.0-20260620232658-8389bac80c5f h1:dl17ebu+HhtHTS9AdOc27TDUGfSjYVEO4qYLHEwjxOw=
4+
fiatjaf.com/nostr v0.0.0-20260620232658-8389bac80c5f/go.mod h1:b1EIUDnd133Ie8Pg8O/biaKdFyCMz28aD4n64g1GqvM=
55
github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkkhIiSjQ=
66
github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo=
77
github.com/FastFilter/xorfilter v0.2.1 h1:lbdeLG9BdpquK64ZsleBS8B4xO/QW1IM0gMzF7KaBKc=

helpers.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,19 @@ func parsePubKey(value string) (nostr.PubKey, error) {
478478
return nostr.PubKey{}, fmt.Errorf("invalid pubkey (\"%s\"): expected hex, npub, or nprofile", value)
479479
}
480480

481+
func parseSecretKey(input string) (nostr.SecretKey, error) {
482+
if prefix, ski, err := nip19.Decode(input); err == nil && prefix == "nsec" {
483+
return ski.(nostr.SecretKey), nil
484+
}
485+
486+
sk, err := nostr.SecretKeyFromHex(input)
487+
if err != nil {
488+
return nostr.SecretKey{}, fmt.Errorf("invalid secret key: %w", err)
489+
}
490+
491+
return sk, nil
492+
}
493+
481494
func parseEventID(value string) (nostr.ID, error) {
482495
id, err := nostr.IDFromHex(value)
483496
if err == nil {

helpers_key.go

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"fiatjaf.com/nostr"
1111
"fiatjaf.com/nostr/keyer"
12-
"fiatjaf.com/nostr/nip19"
1312
"fiatjaf.com/nostr/nip42"
1413
"fiatjaf.com/nostr/nip46"
1514
"fiatjaf.com/nostr/nip49"
@@ -41,36 +40,26 @@ func gatherKeyerFromArguments(ctx context.Context, c *cli.Command) (nostr.Keyer,
4140
}
4241

4342
var kr nostr.Keyer
44-
if bunker != nil {
45-
kr = keyer.NewBunkerSignerFromBunkerClient(bunker)
46-
} else {
43+
if bunker == nil {
4744
kr = keyer.NewPlainKeySigner(key)
45+
} else {
46+
kr = keyer.NewBunkerSignerFromBunkerClient(bunker)
4847
}
4948

5049
return kr, key, nil
5150
}
5251

5352
func gatherSecretKeyOrBunkerFromArguments(ctx context.Context, c *cli.Command) (nostr.SecretKey, *nip46.BunkerClient, error) {
5453
sec := c.String("sec")
54+
5555
if strings.HasPrefix(sec, "bunker://") {
5656
// it's a bunker
5757
bunkerURL := sec
58-
clientKeyHex := c.String("connect-as")
59-
var clientKey nostr.SecretKey
60-
61-
if clientKeyHex != "" {
62-
var err error
63-
clientKey, err = nostr.SecretKeyFromHex(clientKeyHex)
64-
if err != nil {
65-
return nostr.SecretKey{}, nil, fmt.Errorf("bunker client key '%s' is invalid: %w", clientKeyHex, err)
66-
}
67-
} else {
68-
clientKey = defaultKey()
69-
}
7058

59+
clientKey := getSecretKey(c, "connect-as")
7160
logverbose("[nip46]: connecting to %s with client key %s\n", bunkerURL, clientKey.Hex())
7261

73-
bunker, err := nip46.ConnectBunker(ctx, clientKey, bunkerURL, nil, func(s string) {
62+
bunker, err := nip46.ConnectBunker(ctx, clientKey, bunkerURL, sys.Pool, func(s string) {
7463
log(color.CyanString("[nip46]: open the following URL: %s"), s)
7564
})
7665
if err != nil {
@@ -96,16 +85,8 @@ func gatherSecretKeyOrBunkerFromArguments(ctx context.Context, c *cli.Command) (
9685
return sk, nil, nil
9786
}
9887

99-
if prefix, ski, err := nip19.Decode(sec); err == nil && prefix == "nsec" {
100-
return ski.(nostr.SecretKey), nil, nil
101-
}
102-
103-
sk, err := nostr.SecretKeyFromHex(sec)
104-
if err != nil {
105-
return nostr.SecretKey{}, nil, fmt.Errorf("invalid secret key: %w", err)
106-
}
107-
108-
return sk, nil, nil
88+
sk, err := parseSecretKey(sec)
89+
return sk, nil, err
10990
}
11091

11192
func authSigner(ctx context.Context, c *cli.Command, log func(s string, args ...any), authEvent *nostr.Event) (err error) {
@@ -120,14 +101,19 @@ func authSigner(ctx context.Context, c *cli.Command, log func(s string, args ...
120101
return fmt.Errorf("auth required, but --auth flag not given")
121102
}
122103

123-
kr, _, err := gatherKeyerFromArguments(ctx, c)
124-
if err != nil {
125-
return err
104+
var kr nostr.Keyer
105+
106+
if nip46.IsBunkerClientOperation(ctx) {
107+
kr = keyer.NewPlainKeySigner(getSecretKey(c, "connect-as"))
108+
} else {
109+
kr, _, err = gatherKeyerFromArguments(ctx, c)
110+
if err != nil {
111+
return err
112+
}
126113
}
127114

128115
pk, _ := kr.GetPublicKey(ctx)
129-
npub := nip19.EncodeNpub(pk)
130-
log("authenticating as %s... ", color.YellowString("%s…%s", npub[0:7], npub[58:]))
116+
log("authenticating as %s... ", color.YellowString("%s…", pk.Hex()[0:16]))
131117

132118
return kr.SignEvent(ctx, authEvent)
133119
}

main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ var defaultKeyFlags = []cli.Flag{
4343
Usage: "prompt the user to paste a hex or nsec with which to sign the event",
4444
Category: CATEGORY_SIGNER,
4545
},
46-
&cli.StringFlag{
46+
&SecretKeyFlag{
4747
Name: "connect-as",
4848
Usage: "private key to use when communicating with nip46 bunkers",
49-
DefaultText: "the default key (see `nak key default`)",
5049
Category: CATEGORY_SIGNER,
5150
Sources: cli.EnvVars("NOSTR_CLIENT_KEY"),
51+
Value: defaultKey(),
52+
DefaultText: "the default key (see `nak key default`)",
5253
},
5354
}
5455

0 commit comments

Comments
 (0)