|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/nbd-wtf/go-nostr" |
| 8 | +) |
| 9 | + |
| 10 | +// Business operation types |
| 11 | +const ( |
| 12 | + OpModel = "model" // 5 |
| 13 | + OpData = "data" // 6 |
| 14 | + OpCompute = "compute" // 7 |
| 15 | + OpAlgo = "algo" // 8 |
| 16 | + OpValid = "valid" // 9 |
| 17 | +) |
| 18 | + |
| 19 | +// BusinessOps returns the business operations string |
| 20 | +func BusinessOps() string { |
| 21 | + return "model=5,data=6,compute=7,algo=8,valid=9" |
| 22 | +} |
| 23 | + |
| 24 | +// AllOps returns the combined operations string including both basic and business operations |
| 25 | +func AllOps() string { |
| 26 | + return nostr.DefaultSubspaceOps + "," + BusinessOps() |
| 27 | +} |
| 28 | + |
| 29 | +// ValidateBusinessOp validates a business operation event |
| 30 | +func ValidateBusinessOp(evt *nostr.SubspaceOpEvent) error { |
| 31 | + // Verify operation type |
| 32 | + validOps := map[string]bool{ |
| 33 | + OpModel: true, |
| 34 | + OpData: true, |
| 35 | + OpCompute: true, |
| 36 | + OpAlgo: true, |
| 37 | + OpValid: true, |
| 38 | + } |
| 39 | + if !validOps[evt.Operation] { |
| 40 | + return fmt.Errorf("invalid business operation type: %s", evt.Operation) |
| 41 | + } |
| 42 | + |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +func main() { |
| 47 | + relays := []string{"ws://161.97.129.166:10547"} |
| 48 | + sk := nostr.GeneratePrivateKey() |
| 49 | + pub, _ := nostr.GetPublicKey(sk) |
| 50 | + |
| 51 | + // Create a subspace with all operations (basic + business) |
| 52 | + createEvent := nostr.NewSubspaceCreateEvent( |
| 53 | + "modelgraph", |
| 54 | + AllOps(), // Use combined operations string |
| 55 | + "energy>1000", |
| 56 | + "Desci AI Model collaboration subspace", |
| 57 | + "https://causality-graph.com/images/subspace.png", |
| 58 | + ) |
| 59 | + createEvent.PubKey = pub |
| 60 | + createEvent.Sign(sk) |
| 61 | + |
| 62 | + // Join a subspace |
| 63 | + joinEvent := nostr.NewSubspaceJoinEvent(createEvent.SubspaceID) |
| 64 | + joinEvent.PubKey = pub |
| 65 | + joinEvent.Sign(sk) |
| 66 | + |
| 67 | + // Create a post operation (basic operation) |
| 68 | + postEvent := nostr.NewSubspaceOpEvent(createEvent.SubspaceID, nostr.OpPost) |
| 69 | + postEvent.PubKey = pub |
| 70 | + postEvent.SetContentType("markdown") |
| 71 | + postEvent.SetParent("parent-hash") |
| 72 | + postEvent.Content = "# Subspace update \n We have completed the model optimization!" |
| 73 | + postEvent.Sign(sk) |
| 74 | + |
| 75 | + // Create a proposal (basic operation) |
| 76 | + proposeEvent := nostr.NewSubspaceOpEvent(createEvent.SubspaceID, nostr.OpPropose) |
| 77 | + proposeEvent.PubKey = pub |
| 78 | + proposeEvent.SetProposal("prop_001", "energy>2000") |
| 79 | + proposeEvent.Content = "Increase the energy requirement for subspace addition to 2000" |
| 80 | + proposeEvent.Sign(sk) |
| 81 | + |
| 82 | + // Create a vote (basic operation) |
| 83 | + voteEvent := nostr.NewSubspaceOpEvent(createEvent.SubspaceID, nostr.OpVote) |
| 84 | + voteEvent.PubKey = pub |
| 85 | + voteEvent.SetVote("prop_001", "yes") |
| 86 | + voteEvent.Content = "Agree to increase the energy requirements" |
| 87 | + voteEvent.Sign(sk) |
| 88 | + |
| 89 | + // Create an invite (basic operation) |
| 90 | + inviteEvent := nostr.NewSubspaceOpEvent(createEvent.SubspaceID, nostr.OpInvite) |
| 91 | + inviteEvent.PubKey = pub |
| 92 | + inviteEvent.SetInvite("<Charlie's ETH Address>", "energy>1000") |
| 93 | + inviteEvent.Content = "Invite Charlie join into Desci AI subspace" |
| 94 | + inviteEvent.Sign(sk) |
| 95 | + |
| 96 | + // Create a model operation (business operation) |
| 97 | + modelEvent := nostr.NewSubspaceOpEvent(createEvent.SubspaceID, OpModel) |
| 98 | + modelEvent.PubKey = pub |
| 99 | + modelEvent.SetParent("parent-hash") |
| 100 | + modelEvent.SetContributions("base:0.1,data:0.6,algo:0.3") |
| 101 | + modelEvent.Content = "ipfs://bafy..." |
| 102 | + modelEvent.Sign(sk) |
| 103 | + |
| 104 | + // Validate business operation |
| 105 | + if err := ValidateBusinessOp(modelEvent); err != nil { |
| 106 | + fmt.Printf("Invalid business operation: %v\n", err) |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + // publish the events to relays |
| 111 | + ctx := context.Background() |
| 112 | + for _, url := range relays { |
| 113 | + relay, err := nostr.RelayConnect(ctx, url) |
| 114 | + if err != nil { |
| 115 | + fmt.Println(err) |
| 116 | + continue |
| 117 | + } |
| 118 | + |
| 119 | + // Publish all events |
| 120 | + events := []nostr.Event{ |
| 121 | + createEvent.Event, |
| 122 | + joinEvent.Event, |
| 123 | + postEvent.Event, |
| 124 | + proposeEvent.Event, |
| 125 | + voteEvent.Event, |
| 126 | + inviteEvent.Event, |
| 127 | + modelEvent.Event, |
| 128 | + } |
| 129 | + |
| 130 | + for _, ev := range events { |
| 131 | + if err := relay.Publish(ctx, ev); err != nil { |
| 132 | + fmt.Printf("failed to publish event to %s: %v\n", url, err) |
| 133 | + continue |
| 134 | + } |
| 135 | + fmt.Printf("published event %s to %s\n", ev.ID, url) |
| 136 | + } |
| 137 | + } |
| 138 | + relay, err := nostr.RelayConnect(ctx, relays[0]) |
| 139 | + if err != nil { |
| 140 | + fmt.Println(err) |
| 141 | + return |
| 142 | + } |
| 143 | + var filter nostr.Filter |
| 144 | + filter = nostr.Filter{ |
| 145 | + Kinds: []int{30300}, |
| 146 | + // limit = 3, get the three most recent notes |
| 147 | + Limit: 3, |
| 148 | + } |
| 149 | + events, err := relay.QueryEvents(ctx, filter) |
| 150 | + if err != nil { |
| 151 | + fmt.Printf("failed to query events: %v\n", err) |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + for event := range events { |
| 156 | + fmt.Println("------") |
| 157 | + fmt.Printf("Content: %s\n", event) |
| 158 | + } |
| 159 | +} |
0 commit comments