9
9
iface "github.com/ipfs/interface-go-ipfs-core"
10
10
caopts "github.com/ipfs/interface-go-ipfs-core/options"
11
11
"github.com/libp2p/go-libp2p-core/peer"
12
+ mbase "github.com/multiformats/go-multibase"
12
13
)
13
14
14
15
type PubsubAPI HttpApi
@@ -21,8 +22,15 @@ func (api *PubsubAPI) Ls(ctx context.Context) ([]string, error) {
21
22
if err := api .core ().Request ("pubsub/ls" ).Exec (ctx , & out ); err != nil {
22
23
return nil , err
23
24
}
24
-
25
- return out .Strings , nil
25
+ topics := make ([]string , len (out .Strings ))
26
+ for n , mb := range out .Strings {
27
+ _ , topic , err := mbase .Decode (mb )
28
+ if err != nil {
29
+ return nil , err
30
+ }
31
+ topics [n ] = string (topic )
32
+ }
33
+ return topics , nil
26
34
}
27
35
28
36
func (api * PubsubAPI ) Peers (ctx context.Context , opts ... caopts.PubSubPeersOption ) ([]peer.ID , error ) {
@@ -35,7 +43,11 @@ func (api *PubsubAPI) Peers(ctx context.Context, opts ...caopts.PubSubPeersOptio
35
43
Strings []string
36
44
}
37
45
38
- if err := api .core ().Request ("pubsub/peers" , options .Topic ).Exec (ctx , & out ); err != nil {
46
+ var optionalTopic string
47
+ if len (options .Topic ) > 0 {
48
+ optionalTopic = toMultibase ([]byte (options .Topic ))
49
+ }
50
+ if err := api .core ().Request ("pubsub/peers" , optionalTopic ).Exec (ctx , & out ); err != nil {
39
51
return nil , err
40
52
}
41
53
@@ -51,7 +63,7 @@ func (api *PubsubAPI) Peers(ctx context.Context, opts ...caopts.PubSubPeersOptio
51
63
}
52
64
53
65
func (api * PubsubAPI ) Publish (ctx context.Context , topic string , message []byte ) error {
54
- return api .core ().Request ("pubsub/pub" , topic ).
66
+ return api .core ().Request ("pubsub/pub" , toMultibase ([] byte ( topic )) ).
55
67
FileBody (bytes .NewReader (message )).
56
68
Exec (ctx , nil )
57
69
}
@@ -64,29 +76,36 @@ type pubsubSub struct {
64
76
}
65
77
66
78
type pubsubMessage struct {
67
- JFrom [] byte `json:"from,omitempty"`
68
- JData [] byte `json:"data,omitempty"`
69
- JSeqno [] byte `json:"seqno,omitempty"`
79
+ JFrom string `json:"from,omitempty"`
80
+ JData string `json:"data,omitempty"`
81
+ JSeqno string `json:"seqno,omitempty"`
70
82
JTopicIDs []string `json:"topicIDs,omitempty"`
71
83
72
- from peer.ID
73
- err error
84
+ // real values after unpacking from text/multibase envelopes
85
+ from peer.ID
86
+ data []byte
87
+ seqno []byte
88
+ topics []string
89
+
90
+ err error
74
91
}
75
92
76
93
func (msg * pubsubMessage ) From () peer.ID {
77
94
return msg .from
78
95
}
79
96
80
97
func (msg * pubsubMessage ) Data () []byte {
81
- return msg .JData
98
+ return msg .data
82
99
}
83
100
84
101
func (msg * pubsubMessage ) Seq () []byte {
85
- return msg .JSeqno
102
+ return msg .seqno
86
103
}
87
104
105
+ // TODO: do we want to keep this interface as []string,
106
+ // or change to more correct [][]byte?
88
107
func (msg * pubsubMessage ) Topics () []string {
89
- return msg .JTopicIDs
108
+ return msg .topics
90
109
}
91
110
92
111
func (s * pubsubSub ) Next (ctx context.Context ) (iface.PubSubMessage , error ) {
@@ -98,22 +117,41 @@ func (s *pubsubSub) Next(ctx context.Context) (iface.PubSubMessage, error) {
98
117
if msg .err != nil {
99
118
return nil , msg .err
100
119
}
120
+ // unpack values from text/multibase envelopes
101
121
var err error
102
- msg .from , err = peer .IDFromBytes (msg .JFrom )
103
- return & msg , err
122
+ msg .from , err = peer .Decode (msg .JFrom )
123
+ if err != nil {
124
+ return nil , err
125
+ }
126
+ _ , msg .data , err = mbase .Decode (msg .JData )
127
+ if err != nil {
128
+ return nil , err
129
+ }
130
+ _ , msg .seqno , err = mbase .Decode (msg .JSeqno )
131
+ if err != nil {
132
+ return nil , err
133
+ }
134
+ for _ , mbt := range msg .JTopicIDs {
135
+ _ , topic , err := mbase .Decode (mbt )
136
+ if err != nil {
137
+ return nil , err
138
+ }
139
+ msg .topics = append (msg .topics , string (topic ))
140
+ }
141
+ return & msg , nil
104
142
case <- ctx .Done ():
105
143
return nil , ctx .Err ()
106
144
}
107
145
}
108
146
109
147
func (api * PubsubAPI ) Subscribe (ctx context.Context , topic string , opts ... caopts.PubSubSubscribeOption ) (iface.PubSubSubscription , error ) {
148
+ /* right now we have no options (discover got deprecated)
110
149
options, err := caopts.PubSubSubscribeOptions(opts...)
111
150
if err != nil {
112
151
return nil, err
113
152
}
114
-
115
- resp , err := api .core ().Request ("pubsub/sub" , topic ).
116
- Option ("discover" , options .Discover ).Send (ctx )
153
+ */
154
+ resp , err := api .core ().Request ("pubsub/sub" , toMultibase ([]byte (topic ))).Send (ctx )
117
155
118
156
if err != nil {
119
157
return nil , err
@@ -168,3 +206,8 @@ func (s *pubsubSub) Close() error {
168
206
func (api * PubsubAPI ) core () * HttpApi {
169
207
return (* HttpApi )(api )
170
208
}
209
+
210
+ // Encodes bytes into URL-safe multibase that can be sent over HTTP RPC (URL or body)
211
+ func toMultibase (data []byte ) string {
212
+ return mbase .Encode (mbase .Base64url , data )
213
+ }
0 commit comments