nip46: auth_url support.

This commit is contained in:
fiatjaf 2024-02-29 20:37:16 -03:00
parent 6d5aef70c5
commit f75501e7ef
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
2 changed files with 25 additions and 6 deletions

View File

@ -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
}

View File

@ -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, ""})