From f75501e7eff33720c8be582bd561cb894667c5e2 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Thu, 29 Feb 2024 20:37:16 -0300 Subject: [PATCH] nip46: auth_url support. --- nip46/client.go | 28 ++++++++++++++++++++++------ nip46/create_account.go | 3 +++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/nip46/client.go b/nip46/client.go index 223da82..2e0ced9 100644 --- a/nip46/client.go +++ b/nip46/client.go @@ -23,7 +23,9 @@ type BunkerClient struct { relays []string sharedSecret []byte listeners *xsync.MapOf[string, chan Response] + expectingAuth *xsync.MapOf[string, struct{}] idPrefix string + onAuth func(string) // memoized getPublicKeyResponse string @@ -36,6 +38,7 @@ func ConnectBunker( clientSecretKey string, bunkerURLOrNIP05 string, pool *nostr.SimplePool, + onAuth func(string), ) (*BunkerClient, error) { parsed, err := url.Parse(bunkerURLOrNIP05) if err != nil { @@ -71,6 +74,7 @@ func ConnectBunker( targetPublicKey, relays, pool, + onAuth, ) clientPubKey, _ := nostr.GetPublicKey(clientSecretKey) @@ -84,6 +88,7 @@ func NewBunker( targetPublicKey string, relays []string, pool *nostr.SimplePool, + onAuth func(string), ) *BunkerClient { if pool == nil { pool = nostr.NewSimplePool(ctx) @@ -93,12 +98,14 @@ func NewBunker( sharedSecret, _ := nip04.ComputeSharedSecret(targetPublicKey, clientSecretKey) bunker := &BunkerClient{ - pool: pool, - target: targetPublicKey, - relays: relays, - sharedSecret: sharedSecret, - listeners: xsync.NewMapOf[string, chan Response](), - idPrefix: "gn-" + strconv.Itoa(rand.Intn(65536)), + pool: pool, + target: targetPublicKey, + relays: relays, + sharedSecret: sharedSecret, + listeners: xsync.NewMapOf[string, chan Response](), + expectingAuth: xsync.NewMapOf[string, struct{}](), + onAuth: onAuth, + idPrefix: "gn-" + strconv.Itoa(rand.Intn(65536)), } go func() { @@ -124,6 +131,15 @@ func NewBunker( continue } + if resp.Result == "auth_url" { + // special case + authURL := resp.Error + if _, ok := bunker.expectingAuth.Load(resp.ID); ok { + bunker.onAuth(authURL) + } + continue + } + if dispatcher, ok := bunker.listeners.Load(resp.ID); ok { dispatcher <- resp } diff --git a/nip46/create_account.go b/nip46/create_account.go index 0b772d3..d1a9f88 100644 --- a/nip46/create_account.go +++ b/nip46/create_account.go @@ -29,6 +29,7 @@ func CreateAccount( domain string, pool *nostr.SimplePool, extraOpts *CreateAccountOptions, + onAuth func(string), ) (*BunkerClient, error) { if pool == nil { pool = nostr.NewSimplePool(ctx) @@ -46,6 +47,7 @@ func CreateAccount( providerPubkey, relays, pool, + onAuth, ) clientPubKey, _ := nostr.GetPublicKey(clientSecretKey) @@ -69,6 +71,7 @@ func CreateAccount( // update this bunker instance so it targets the new key now instead of the provider bunker.target = newlyCreatedPublicKey bunker.sharedSecret, _ = nip04.ComputeSharedSecret(newlyCreatedPublicKey, clientSecretKey) + bunker.getPublicKeyResponse = newlyCreatedPublicKey // finally try to connect again using the new key as the target _, err = bunker.RPC(ctx, "connect", []string{newlyCreatedPublicKey, ""})