2024-02-29 20:29:08 -03:00
|
|
|
package nip46
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
|
|
"github.com/nbd-wtf/go-nostr/nip04"
|
|
|
|
"github.com/nbd-wtf/go-nostr/nip05"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CheckNameAvailability(ctx context.Context, name, domain string) bool {
|
|
|
|
result, _, err := nip05.Fetch(ctx, name+"@"+domain)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, ok := result.Names[name]
|
|
|
|
return !ok
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateAccountOptions struct {
|
|
|
|
Email string
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateAccount(
|
|
|
|
ctx context.Context,
|
|
|
|
clientSecretKey string,
|
|
|
|
name string,
|
|
|
|
domain string,
|
|
|
|
pool *nostr.SimplePool,
|
|
|
|
extraOpts *CreateAccountOptions,
|
2024-02-29 20:37:16 -03:00
|
|
|
onAuth func(string),
|
2024-05-15 16:13:12 -03:00
|
|
|
) (*BunkerClient, []string, error) {
|
2024-02-29 20:29:08 -03:00
|
|
|
if pool == nil {
|
|
|
|
pool = nostr.NewSimplePool(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a bunker that targets the provider directly
|
2024-05-15 16:13:12 -03:00
|
|
|
providerPubkey, relays, err := queryWellKnownNostrJson(ctx, "_@"+domain)
|
2024-02-29 20:29:08 -03:00
|
|
|
if err != nil {
|
2024-05-15 16:13:12 -03:00
|
|
|
return nil, nil, err
|
2024-02-29 20:29:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bunker := NewBunker(
|
|
|
|
ctx,
|
|
|
|
clientSecretKey,
|
|
|
|
providerPubkey,
|
|
|
|
relays,
|
|
|
|
pool,
|
2024-02-29 20:37:16 -03:00
|
|
|
onAuth,
|
2024-02-29 20:29:08 -03:00
|
|
|
)
|
|
|
|
|
2024-03-04 09:30:45 -03:00
|
|
|
_, err = bunker.RPC(ctx, "connect", []string{providerPubkey, ""})
|
2024-02-29 20:29:08 -03:00
|
|
|
if err != nil {
|
2024-05-15 16:13:12 -03:00
|
|
|
return nil, relays, fmt.Errorf("initial connect error: %w", err)
|
2024-02-29 20:29:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// call create_account on it, it should return the value of the public key that will be created
|
|
|
|
email := ""
|
|
|
|
if extraOpts != nil {
|
|
|
|
email = extraOpts.Email
|
|
|
|
}
|
|
|
|
resp, err := bunker.RPC(ctx, "create_account", []string{name, domain, email})
|
|
|
|
if err != nil {
|
2024-05-15 16:13:12 -03:00
|
|
|
return nil, relays, fmt.Errorf("error on create_account: %w", err)
|
2024-02-29 20:29:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
newlyCreatedPublicKey := resp
|
|
|
|
|
|
|
|
// update this bunker instance so it targets the new key now instead of the provider
|
|
|
|
bunker.target = newlyCreatedPublicKey
|
|
|
|
bunker.sharedSecret, _ = nip04.ComputeSharedSecret(newlyCreatedPublicKey, clientSecretKey)
|
2024-02-29 20:37:16 -03:00
|
|
|
bunker.getPublicKeyResponse = newlyCreatedPublicKey
|
2024-02-29 20:29:08 -03:00
|
|
|
|
|
|
|
// finally try to connect again using the new key as the target
|
|
|
|
_, err = bunker.RPC(ctx, "connect", []string{newlyCreatedPublicKey, ""})
|
|
|
|
if err != nil {
|
2024-05-15 16:13:12 -03:00
|
|
|
return bunker, relays, fmt.Errorf("newly-created public key connect error: %w", err)
|
2024-02-29 20:29:08 -03:00
|
|
|
}
|
|
|
|
|
2024-05-15 16:13:12 -03:00
|
|
|
return bunker, relays, err
|
2024-02-29 20:29:08 -03:00
|
|
|
}
|