forked from ipfs/boxo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipfs.go
42 lines (36 loc) · 798 Bytes
/
ipfs.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
package types
import (
"encoding/json"
"github.com/ipfs/go-cid"
"github.com/littlespeechless/boxo/routing/http/internal/drjson"
"github.com/multiformats/go-multiaddr"
)
type CID struct{ cid.Cid }
func (c *CID) MarshalJSON() ([]byte, error) { return drjson.MarshalJSONBytes(c.String()) }
func (c *CID) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
decodedCID, err := cid.Decode(s)
if err != nil {
return err
}
c.Cid = decodedCID
return nil
}
type Multiaddr struct{ multiaddr.Multiaddr }
func (m *Multiaddr) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
ma, err := multiaddr.NewMultiaddr(s)
if err != nil {
return err
}
m.Multiaddr = ma
return nil
}