forked from anyproto/go-threads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
56 lines (48 loc) · 1.21 KB
/
protocol.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
package threads
import (
"fmt"
"github.com/libp2p/go-libp2p-core/protocol"
ma "github.com/multiformats/go-multiaddr"
mb "github.com/multiformats/go-multibase"
"github.com/textileio/go-textile-core/thread"
)
const (
// Thread is the protocol slug.
Thread = "thread"
// ThreadCode is the protocol code.
ThreadCode = 406
// ThreadsVersion is the current protocol version.
ThreadVersion = "0.0.1"
// ThreadProtocol is the threads protocol tag.
ThreadProtocol protocol.ID = "/" + Thread + "/" + ThreadVersion
)
var addrProtocol = ma.Protocol{
Name: Thread,
Code: ThreadCode,
VCode: ma.CodeToVarint(ThreadCode),
Size: ma.LengthPrefixedVarSize,
Transcoder: ma.NewTranscoderFromFunctions(threadStB, threadBtS, threadVal),
}
func threadStB(s string) ([]byte, error) {
_, data, err := mb.Decode(s)
if err != nil {
return nil, fmt.Errorf("failed to parse thread addr: %s %s", s, err)
}
return data, nil
}
func threadVal(b []byte) error {
_, err := thread.Cast(b)
return err
}
func threadBtS(b []byte) (string, error) {
m, err := thread.Cast(b)
if err != nil {
return "", err
}
return m.String(), nil
}
func init() {
if err := ma.AddProtocol(addrProtocol); err != nil {
panic(err)
}
}