nip46: auth_url support.

This commit is contained in:
fiatjaf
2024-02-29 20:37:16 -03:00
parent 6d5aef70c5
commit f75501e7ef
2 changed files with 25 additions and 6 deletions

View File

@ -23,7 +23,9 @@ type BunkerClient struct {
relays []string relays []string
sharedSecret []byte sharedSecret []byte
listeners *xsync.MapOf[string, chan Response] listeners *xsync.MapOf[string, chan Response]
expectingAuth *xsync.MapOf[string, struct{}]
idPrefix string idPrefix string
onAuth func(string)
// memoized // memoized
getPublicKeyResponse string getPublicKeyResponse string
@ -36,6 +38,7 @@ func ConnectBunker(
clientSecretKey string, clientSecretKey string,
bunkerURLOrNIP05 string, bunkerURLOrNIP05 string,
pool *nostr.SimplePool, pool *nostr.SimplePool,
onAuth func(string),
) (*BunkerClient, error) { ) (*BunkerClient, error) {
parsed, err := url.Parse(bunkerURLOrNIP05) parsed, err := url.Parse(bunkerURLOrNIP05)
if err != nil { if err != nil {
@ -71,6 +74,7 @@ func ConnectBunker(
targetPublicKey, targetPublicKey,
relays, relays,
pool, pool,
onAuth,
) )
clientPubKey, _ := nostr.GetPublicKey(clientSecretKey) clientPubKey, _ := nostr.GetPublicKey(clientSecretKey)
@ -84,6 +88,7 @@ func NewBunker(
targetPublicKey string, targetPublicKey string,
relays []string, relays []string,
pool *nostr.SimplePool, pool *nostr.SimplePool,
onAuth func(string),
) *BunkerClient { ) *BunkerClient {
if pool == nil { if pool == nil {
pool = nostr.NewSimplePool(ctx) pool = nostr.NewSimplePool(ctx)
@ -98,6 +103,8 @@ func NewBunker(
relays: relays, relays: relays,
sharedSecret: sharedSecret, sharedSecret: sharedSecret,
listeners: xsync.NewMapOf[string, chan Response](), listeners: xsync.NewMapOf[string, chan Response](),
expectingAuth: xsync.NewMapOf[string, struct{}](),
onAuth: onAuth,
idPrefix: "gn-" + strconv.Itoa(rand.Intn(65536)), idPrefix: "gn-" + strconv.Itoa(rand.Intn(65536)),
} }
@ -124,6 +131,15 @@ func NewBunker(
continue 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 { if dispatcher, ok := bunker.listeners.Load(resp.ID); ok {
dispatcher <- resp dispatcher <- resp
} }

View File

@ -29,6 +29,7 @@ func CreateAccount(
domain string, domain string,
pool *nostr.SimplePool, pool *nostr.SimplePool,
extraOpts *CreateAccountOptions, extraOpts *CreateAccountOptions,
onAuth func(string),
) (*BunkerClient, error) { ) (*BunkerClient, error) {
if pool == nil { if pool == nil {
pool = nostr.NewSimplePool(ctx) pool = nostr.NewSimplePool(ctx)
@ -46,6 +47,7 @@ func CreateAccount(
providerPubkey, providerPubkey,
relays, relays,
pool, pool,
onAuth,
) )
clientPubKey, _ := nostr.GetPublicKey(clientSecretKey) 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 // update this bunker instance so it targets the new key now instead of the provider
bunker.target = newlyCreatedPublicKey bunker.target = newlyCreatedPublicKey
bunker.sharedSecret, _ = nip04.ComputeSharedSecret(newlyCreatedPublicKey, clientSecretKey) bunker.sharedSecret, _ = nip04.ComputeSharedSecret(newlyCreatedPublicKey, clientSecretKey)
bunker.getPublicKeyResponse = newlyCreatedPublicKey
// finally try to connect again using the new key as the target // finally try to connect again using the new key as the target
_, err = bunker.RPC(ctx, "connect", []string{newlyCreatedPublicKey, ""}) _, err = bunker.RPC(ctx, "connect", []string{newlyCreatedPublicKey, ""})