Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 55db949

Browse files
lidelaschmahmann
andauthored
feat: pubsub http rpc with multibase (#255)
* feat: pubsub with multibase This updates HTTP RPC wire format to one from ipfs/kubo#8183 * use go install instead of make install * ci: switch to go-ipfs master * update README Co-authored-by: Adin Schmahmann <[email protected]>
1 parent 0379ac8 commit 55db949

File tree

7 files changed

+57
-24
lines changed

7 files changed

+57
-24
lines changed

.github/actions/go-test-setup/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ runs:
1212
run: (ipfs init)
1313
- name: Run go-ipfs
1414
shell: bash
15-
run: (ipfs daemon --enable-pubsub-experiment &)
15+
run: (ipfs daemon --enable-pubsub-experiment &)

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# go-ipfs-api
22

3-
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
4-
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
5-
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
3+
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai)
4+
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](https://ipfs.io/)
5+
[![](https://img.shields.io/badge/matrix-%23ipfs-blue.svg?style=flat-square)](https://app.element.io/#/room/#ipfs:matrix.org)
66
[![standard-readme compliant](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
77
[![GoDoc](https://godoc.org/github.com/ipfs/go-ipfs-api?status.svg)](https://godoc.org/github.com/ipfs/go-ipfs-api)
88
[![Build Status](https://travis-ci.org/ipfs/go-ipfs-api.svg)](https://travis-ci.org/ipfs/go-ipfs-api)

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ module github.com/ipfs/go-ipfs-api
44

55
require (
66
github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927
7-
github.com/ipfs/go-ipfs-files v0.0.8
7+
github.com/ipfs/go-ipfs-files v0.0.9
88
github.com/ipfs/go-ipfs-util v0.0.2
99
github.com/libp2p/go-libp2p-core v0.6.1
1010
github.com/mitchellh/go-homedir v1.1.0
1111
github.com/multiformats/go-multiaddr v0.3.0
12+
github.com/multiformats/go-multibase v0.0.3
1213
github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c
1314
)

go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
3333
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
3434
github.com/ipfs/go-cid v0.0.7 h1:ysQJVJA3fNDF1qigJbsSQOdjhVLsOEoPdh0+R97k3jY=
3535
github.com/ipfs/go-cid v0.0.7/go.mod h1:6Ux9z5e+HpkQdckYoX1PG/6xqKspzlEIR5SDmgqgC/I=
36-
github.com/ipfs/go-ipfs-files v0.0.8 h1:8o0oFJkJ8UkO/ABl8T6ac6tKF3+NIpj67aAB6ZpusRg=
37-
github.com/ipfs/go-ipfs-files v0.0.8/go.mod h1:wiN/jSG8FKyk7N0WyctKSvq3ljIa2NNTiZB55kpTdOs=
36+
github.com/ipfs/go-ipfs-files v0.0.9 h1:OFyOfmuVDu9c5YtjSDORmwXzE6fmZikzZpzsnNkgFEg=
37+
github.com/ipfs/go-ipfs-files v0.0.9/go.mod h1:aFv2uQ/qxWpL/6lidWvnSQmaVqCrf0TBGoUr+C1Fo84=
3838
github.com/ipfs/go-ipfs-util v0.0.2 h1:59Sswnk1MFaiq+VcaknX7aYEyGyGDAA73ilhEK2POp8=
3939
github.com/ipfs/go-ipfs-util v0.0.2/go.mod h1:CbPtkWJzjLdEcezDns2XYaehFVNXG9zrdrtMecczcsQ=
4040
github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA=
@@ -89,8 +89,9 @@ github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w
8989
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
9090
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
9191
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
92-
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
9392
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
93+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
94+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
9495
github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c h1:GGsyl0dZ2jJgVT+VvWBf/cNijrHRhkrTjkmp5wg7li0=
9596
github.com/whyrusleeping/tar-utils v0.0.0-20180509141711-8c6c8ba81d5c/go.mod h1:xxcJeBb7SIUl/Wzkz1eVKJE/CB34YNrqX2TQI6jY9zs=
9697
go.opencensus.io v0.22.4 h1:LYy1Hy3MJdrCdMwwzxA/dRok4ejH+RwNGbuoD9fCjto=
@@ -139,6 +140,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
139140
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
140141
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
141142
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
142-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
143143
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
144+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
145+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
144146
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

pubsub.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66

77
"github.com/libp2p/go-libp2p-core/peer"
8+
mbase "github.com/multiformats/go-multibase"
89
)
910

1011
// Message is a pubsub message.
@@ -31,9 +32,9 @@ func newPubSubSubscription(resp io.ReadCloser) *PubSubSubscription {
3132
// Next waits for the next record and returns that.
3233
func (s *PubSubSubscription) Next() (*Message, error) {
3334
var r struct {
34-
From []byte `json:"from,omitempty"`
35-
Data []byte `json:"data,omitempty"`
36-
Seqno []byte `json:"seqno,omitempty"`
35+
From string `json:"from,omitempty"`
36+
Data string `json:"data,omitempty"`
37+
Seqno string `json:"seqno,omitempty"`
3738
TopicIDs []string `json:"topicIDs,omitempty"`
3839
}
3940

@@ -42,15 +43,33 @@ func (s *PubSubSubscription) Next() (*Message, error) {
4243
return nil, err
4344
}
4445

45-
from, err := peer.IDFromBytes(r.From)
46+
// fields are wrapped in multibase when sent over HTTP RPC
47+
// and need to be decoded (https://github.com/ipfs/go-ipfs/pull/8183)
48+
from, err := peer.Decode(r.From)
4649
if err != nil {
4750
return nil, err
4851
}
52+
_, data, err := mbase.Decode(r.Data)
53+
if err != nil {
54+
return nil, err
55+
}
56+
_, seqno, err := mbase.Decode(r.Seqno)
57+
if err != nil {
58+
return nil, err
59+
}
60+
topics := make([]string, len(r.TopicIDs))
61+
for _, mbtopic := range r.TopicIDs {
62+
_, topic, err := mbase.Decode(mbtopic)
63+
if err != nil {
64+
return nil, err
65+
}
66+
topics = append(topics, string(topic))
67+
}
4968
return &Message{
5069
From: from,
51-
Data: r.Data,
52-
Seqno: r.Seqno,
53-
TopicIDs: r.TopicIDs,
70+
Data: data,
71+
Seqno: seqno,
72+
TopicIDs: topics,
5473
}, nil
5574
}
5675

shell.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
homedir "github.com/mitchellh/go-homedir"
2121
ma "github.com/multiformats/go-multiaddr"
2222
manet "github.com/multiformats/go-multiaddr/net"
23+
mbase "github.com/multiformats/go-multibase"
2324
tar "github.com/whyrusleeping/tar-utils"
2425

2526
p2pmetrics "github.com/libp2p/go-libp2p-core/metrics"
@@ -516,7 +517,8 @@ func (s *Shell) ObjectPut(obj *IpfsObject) (string, error) {
516517

517518
func (s *Shell) PubSubSubscribe(topic string) (*PubSubSubscription, error) {
518519
// connect
519-
resp, err := s.Request("pubsub/sub", topic).Send(context.Background())
520+
encoder, _ := mbase.EncoderByName("base64url")
521+
resp, err := s.Request("pubsub/sub", encoder.Encode([]byte(topic))).Send(context.Background())
520522
if err != nil {
521523
return nil, err
522524
}
@@ -528,7 +530,14 @@ func (s *Shell) PubSubSubscribe(topic string) (*PubSubSubscription, error) {
528530
}
529531

530532
func (s *Shell) PubSubPublish(topic, data string) (err error) {
531-
resp, err := s.Request("pubsub/pub", topic, data).Send(context.Background())
533+
534+
fr := files.NewReaderFile(bytes.NewReader([]byte(data)))
535+
slf := files.NewSliceDirectory([]files.DirEntry{files.FileEntry("", fr)})
536+
fileReader := files.NewMultiFileReader(slf, true)
537+
538+
encoder, _ := mbase.EncoderByName("base64url")
539+
resp, err := s.Request("pubsub/pub", encoder.Encode([]byte(topic))).
540+
Body(fileReader).Send(context.Background())
532541
if err != nil {
533542
return err
534543
}

shell_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,9 @@ func TestPubSub(t *testing.T) {
369369
s := NewShell(shellUrl)
370370

371371
var (
372-
topic = "test"
372+
topic = "test\n topic\r\t with unsafe bytes"
373+
payload1 = "Hello\r\nWorld\t!"
374+
payload2 = "Hallo\r\nWelt\t!!11oneonę"
373375

374376
sub *PubSubSubscription
375377
err error
@@ -384,7 +386,7 @@ func TestPubSub(t *testing.T) {
384386
time.Sleep(10 * time.Millisecond)
385387

386388
t.Log("publishing...")
387-
is.Nil(s.PubSubPublish(topic, "Hello World!"))
389+
is.Nil(s.PubSubPublish(topic, payload1))
388390
t.Log("pub: done")
389391

390392
t.Log("next()...")
@@ -393,23 +395,23 @@ func TestPubSub(t *testing.T) {
393395

394396
is.Nil(err)
395397
is.NotNil(r)
396-
is.Equal(r.Data, "Hello World!")
398+
is.Equal(r.Data, payload1)
397399

398400
sub2, err := s.PubSubSubscribe(topic)
399401
is.Nil(err)
400402
is.NotNil(sub2)
401403

402-
is.Nil(s.PubSubPublish(topic, "Hallo Welt!"))
404+
is.Nil(s.PubSubPublish(topic, payload2))
403405

404406
r, err = sub2.Next()
405407
is.Nil(err)
406408
is.NotNil(r)
407-
is.Equal(r.Data, "Hallo Welt!")
409+
is.Equal(r.Data, payload2)
408410

409411
r, err = sub.Next()
410412
is.NotNil(r)
411413
is.Nil(err)
412-
is.Equal(r.Data, "Hallo Welt!")
414+
is.Equal(r.Data, payload2)
413415

414416
is.Nil(sub.Cancel())
415417
}

0 commit comments

Comments
 (0)