-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.go
88 lines (65 loc) · 1.63 KB
/
connect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"errors"
"fmt"
"strconv"
nkcli "github.com/mdzz-club/nkcli/internal"
"github.com/urfave/cli/v2"
)
func connectAction(c *cli.Context) error {
if c.Args().Len() == 0 {
fmt.Printf("You need pass a nostrconnect:// arg")
return nil
}
cu, err := nkcli.ParseURL(c.Args().Get(0))
if err != nil {
return err
}
fmt.Printf("App name: %v\n", cu.Metadata.Name)
if len(cu.Metadata.Url) != 0 {
fmt.Printf("App URL: %v\n", cu.Metadata.Url)
}
if len(cu.Metadata.Description) != 0 {
fmt.Printf("App description: %v\n", cu.Metadata.Description)
}
db, err := nkcli.Open(c.String("db"))
if err != nil {
return err
}
keys, err := db.List()
if err != nil {
return err
}
fmt.Printf("\nYou have %v keys:\n\n", len(keys))
nkcli.PrintKeyList(keys)
fmt.Print(" 🔗 Choose your key: ")
line := nkcli.Scanline()
n, err := strconv.Atoi(line)
if err != nil {
return err
}
if n < 1 || n > len(keys) {
return errors.New("Invalid key No.")
}
usedPub := keys[n-1]
allows := []string{}
if c.Bool("allow-all") {
fmt.Println("\nNOTICE: This connection will allow all requests by default.")
allows = []string{"get_public_key", "sign_event", "delegate", "get_relays", "nip04_encrypt", "nip04_decrypt"}
}
conn := &nkcli.Connection{
AppID: cu.Pubkey,
Relay: cu.Relay,
PubKey: usedPub.Pubkey,
Acked: false,
Allows: allows,
Metadata: &nkcli.ConnMetadata{
Name: cu.Metadata.Name,
Description: cu.Metadata.Description,
Url: cu.Metadata.Url,
},
}
db.SetConnection(conn)
fmt.Print("\nConnection info saved!\nRun nkcli without subcommand to serve it.\n")
return nil
}