-
Notifications
You must be signed in to change notification settings - Fork 15
/
wot.go
120 lines (97 loc) · 2.42 KB
/
wot.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"context"
"log"
"time"
"github.com/nbd-wtf/go-nostr"
)
var (
pubkeyFollowerCount = make(map[string]int)
oneHopNetwork []string
wot []string
wotRelays []string
wotMap map[string]bool
)
func refreshTrustNetwork() {
ctx := context.Background()
timeoutCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
ownerPubkey := nPubToPubkey(config.OwnerNpub)
filters := []nostr.Filter{{
Authors: []string{ownerPubkey},
Kinds: []int{nostr.KindFollowList},
}}
for ev := range pool.SubManyEose(timeoutCtx, config.ImportSeedRelays, filters) {
for _, contact := range ev.Event.Tags.GetAll([]string{"p"}) {
pubkeyFollowerCount[contact[1]]++
appendOneHopNetwork(contact[1])
}
}
log.Println("🌐 building web of trust graph")
for i := 0; i < len(oneHopNetwork); i += 100 {
timeout, cancel := context.WithTimeout(ctx, 4*time.Second)
defer cancel()
end := i + 100
if end > len(oneHopNetwork) {
end = len(oneHopNetwork)
}
filters = []nostr.Filter{{
Authors: oneHopNetwork[i:end],
Kinds: []int{nostr.KindFollowList, nostr.KindRelayListMetadata},
}}
for ev := range pool.SubManyEose(timeout, config.ImportSeedRelays, filters) {
for _, contact := range ev.Event.Tags.GetAll([]string{"p"}) {
if len(contact) > 1 {
pubkeyFollowerCount[contact[1]]++
}
}
for _, relay := range ev.Event.Tags.GetAll([]string{"r"}) {
appendRelay(relay[1])
}
}
}
log.Println("🫂 total network size:", len(pubkeyFollowerCount))
log.Println("🔗 relays discovered:", len(wotRelays))
updateWoTMap()
}
func appendRelay(relay string) {
for _, r := range wotRelays {
if r == relay {
return
}
}
wotRelays = append(wotRelays, relay)
}
func appendPubkeyToWoT(pubkey string) {
for _, pk := range wot {
if pk == pubkey {
return
}
}
if len(pubkey) != 64 {
return
}
wot = append(wot, pubkey)
}
func appendOneHopNetwork(pubkey string) {
for _, pk := range oneHopNetwork {
if pk == pubkey {
return
}
}
if len(pubkey) != 64 {
return
}
oneHopNetwork = append(oneHopNetwork, pubkey)
}
func updateWoTMap() {
wotMapTmp := make(map[string]bool)
for pubkey, count := range pubkeyFollowerCount {
if count >= config.ChatRelayMinimumFollowers {
wotMapTmp[pubkey] = true
appendPubkeyToWoT(pubkey)
}
}
wotMap = wotMapTmp
log.Println("🌐 pubkeys with minimum followers: ", len(wotMap), "keys")
}