Skip to content

Commit 49f6750

Browse files
committed
wip: move client to new drclient
1 parent 970f871 commit 49f6750

File tree

6 files changed

+518
-1520
lines changed

6 files changed

+518
-1520
lines changed

client.go

+97-120
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,154 @@
11
package main
22

33
import (
4-
"bytes"
54
"context"
5+
"encoding/json"
6+
"errors"
67
"fmt"
8+
"io"
79
"log"
8-
"strings"
10+
"os"
911
"time"
1012

11-
pbuf "github.com/gogo/protobuf/proto"
12-
13+
"github.com/ipfs/boxo/ipns"
14+
"github.com/ipfs/boxo/routing/http/client"
15+
"github.com/ipfs/boxo/routing/http/types"
16+
"github.com/ipfs/boxo/routing/http/types/iter"
1317
"github.com/ipfs/go-cid"
14-
"github.com/ipfs/go-delegated-routing/client"
15-
"github.com/ipfs/go-ipns"
16-
ipns_pb "github.com/ipfs/go-ipns/pb"
17-
"github.com/ipld/go-ipld-prime/codec/dagjson"
18-
"github.com/libp2p/go-libp2p-core/peer"
19-
"github.com/multiformats/go-multiaddr"
20-
21-
drp "github.com/ipfs/go-delegated-routing/gen/proto"
18+
"github.com/libp2p/go-libp2p/core/peer"
2219
)
2320

24-
func identify(ctx context.Context, endpoint string, prettyOutput bool) error {
25-
ic, err := drp.New_DelegatedRouting_Client(endpoint)
21+
func findProviders(ctx context.Context, key cid.Cid, endpoint string, prettyOutput bool) error {
22+
drc, err := client.New(endpoint)
2623
if err != nil {
2724
return err
2825
}
2926

30-
respCh, err := ic.Identify_Async(ctx, &drp.DelegatedRouting_IdentifyArg{})
31-
for r := range respCh {
32-
if r.Err != nil {
33-
log.Println(r.Err)
34-
continue
35-
}
36-
37-
if !prettyOutput {
38-
var buf bytes.Buffer
39-
if err := dagjson.Encode(r.Resp, &buf); err != nil {
40-
return err
41-
}
42-
fmt.Println(buf.String())
43-
} else {
44-
var methods []string
45-
for _, m := range r.Resp.Methods {
46-
methods = append(methods, string(m))
47-
}
48-
fmt.Println(strings.Join(methods, ","))
49-
}
27+
recordsIter, err := drc.FindProviders(ctx, key)
28+
if err != nil {
29+
return err
5030
}
51-
return nil
31+
defer recordsIter.Close()
32+
33+
return printIter(os.Stdout, prettyOutput, recordsIter)
5234
}
5335

54-
func findprovs(ctx context.Context, c cid.Cid, endpoint string, prettyOutput bool) error {
55-
ic, err := drp.New_DelegatedRouting_Client(endpoint)
36+
func findPeers(ctx context.Context, pid peer.ID, endpoint string, prettyOutput bool) error {
37+
drc, err := client.New(endpoint)
5638
if err != nil {
5739
return err
5840
}
5941

60-
respCh, err := ic.FindProviders_Async(ctx, &drp.FindProvidersRequest{
61-
Key: drp.LinkToAny(c),
62-
})
42+
recordsIter, err := drc.FindPeers(ctx, pid)
6343
if err != nil {
6444
return err
6545
}
66-
for r := range respCh {
67-
if r.Err != nil {
68-
log.Println(r.Err)
69-
continue
46+
defer recordsIter.Close()
47+
48+
return printIter(os.Stdout, prettyOutput, recordsIter)
49+
}
50+
51+
func printIter(w io.Writer, prettyOutput bool, iter iter.ResultIter[types.Record]) error {
52+
for iter.Next() {
53+
res := iter.Val()
54+
55+
// Check for error, but do not complain if we exceeded the timeout. We are
56+
// expecting that to happen: we explicitly defined a timeout.
57+
if res.Err != nil {
58+
if !errors.Is(res.Err, context.DeadlineExceeded) {
59+
return res.Err
60+
}
61+
62+
return nil
7063
}
7164

72-
if !prettyOutput {
73-
var buf bytes.Buffer
74-
if err := dagjson.Encode(r.Resp, &buf); err != nil {
75-
return err
65+
if prettyOutput {
66+
switch res.Val.GetSchema() {
67+
case types.SchemaPeer:
68+
record := res.Val.(*types.PeerRecord)
69+
fmt.Fprintln(w, record.ID)
70+
fmt.Fprintln(w, "\tProtocols:", record.Protocols)
71+
fmt.Fprintln(w, "\tAddresses:", record.Addrs)
72+
default:
73+
// You may not want to fail here, it's up to you. You can just handle
74+
// the schemas you want, or that you know, but not fail.
75+
log.Printf("unrecognized schema: %s", res.Val.GetSchema())
7676
}
77-
fmt.Println(buf.String())
7877
} else {
79-
for _, prov := range r.Resp.Providers {
80-
if prov.ProviderNode.Peer != nil {
81-
ai := &peer.AddrInfo{}
82-
ai.ID = peer.ID(prov.ProviderNode.Peer.ID)
83-
for _, bma := range prov.ProviderNode.Peer.Multiaddresses {
84-
ma, err := multiaddr.NewMultiaddrBytes(bma)
85-
if err != nil {
86-
return err
87-
}
88-
ai.Addrs = append(ai.Addrs, ma)
89-
}
90-
fmt.Println(ai)
91-
}
92-
for _, proto := range prov.ProviderProto {
93-
if proto.Bitswap != nil {
94-
fmt.Println("\t Bitswap")
95-
} else if proto.GraphSyncFILv1 != nil {
96-
fmt.Println("\t GraphSyncFILv1")
97-
var buf bytes.Buffer
98-
if err := dagjson.Encode(proto.GraphSyncFILv1, &buf); err != nil {
99-
return err
100-
}
101-
fmt.Println("\t\t" + buf.String())
102-
} else {
103-
var buf bytes.Buffer
104-
if err := dagjson.Encode(proto, &buf); err != nil {
105-
return err
106-
}
107-
fmt.Println("\t" + buf.String())
108-
}
109-
}
78+
err := json.NewEncoder(os.Stdout).Encode(res.Val)
79+
if err != nil {
80+
return err
11081
}
11182
}
11283
}
84+
11385
return nil
11486
}
11587

116-
func getIPNS(ctx context.Context, p peer.ID, endpoint string, prettyOutput bool) error {
117-
ic, err := drp.New_DelegatedRouting_Client(endpoint)
88+
func getIPNS(ctx context.Context, name ipns.Name, endpoint string, prettyOutput bool) error {
89+
drc, err := client.New(endpoint)
90+
if err != nil {
91+
return err
92+
}
93+
94+
rec, err := drc.GetIPNS(ctx, name)
11895
if err != nil {
11996
return err
12097
}
12198

12299
if prettyOutput {
123-
c := client.NewClient(ic)
124-
respCh, err := c.GetIPNSAsync(ctx, []byte(p))
100+
v, err := rec.Value()
125101
if err != nil {
126102
return err
127103
}
128-
for r := range respCh {
129-
if r.Err != nil {
130-
log.Println(r.Err)
131-
continue
132-
}
133-
rec := new(ipns_pb.IpnsEntry)
134-
if err := pbuf.Unmarshal(r.Record, rec); err != nil {
135-
return err
136-
}
137-
seqno := rec.GetSequence()
138-
ttl := time.Duration(rec.GetTtl())
139-
eol, err := ipns.GetEOL(rec)
140-
if err != nil {
141-
return err
142-
}
143-
value := string(rec.GetValue())
144-
fmt.Printf("Sequence: %d, TTL: %v, EOL: %v, Value: %s\n", seqno, ttl, eol, value)
104+
105+
seq, err := rec.Sequence()
106+
if err != nil {
107+
return err
108+
}
109+
110+
eol, err := rec.Validity()
111+
if err != nil {
112+
return err
113+
}
114+
115+
fmt.Printf("/ipns/%s\n", name)
116+
117+
// Since [client.Client.GetIPNS] verifies if the retrieved record is valid, we
118+
// do not need to verify it again. However, if you were not using this specific
119+
// client, but using some other tool, you should always validate the IPNS Record
120+
// using the [ipns.Validate] or [ipns.ValidateWithName] functions.
121+
fmt.Println("\tSignature: VALID")
122+
fmt.Println("\tValue:", v.String())
123+
fmt.Println("\tSequence:", seq)
124+
fmt.Println("\tValidity:", eol.Format(time.RFC3339))
125+
ttl, err := rec.TTL()
126+
if err == nil {
127+
fmt.Println("\tTTL:", ttl.String())
145128
}
129+
146130
return nil
147131
}
148132

149-
respCh, err := ic.GetIPNS_Async(ctx, &drp.GetIPNSRequest{
150-
ID: []byte(p),
151-
})
133+
raw, err := ipns.MarshalRecord(rec)
152134
if err != nil {
153135
return err
154136
}
155-
for r := range respCh {
156-
if r.Err != nil {
157-
log.Println(r.Err)
158-
continue
159-
}
160-
var buf bytes.Buffer
161-
if err := dagjson.Encode(r.Resp, &buf); err != nil {
162-
return err
163-
}
164-
fmt.Println(buf.String())
165-
}
166-
return nil
137+
138+
_, err = os.Stdout.Write(raw)
139+
return err
167140
}
168141

169-
func putIPNS(ctx context.Context, key peer.ID, record []byte, endpoint string) error {
170-
ic, err := drp.New_DelegatedRouting_Client(endpoint)
142+
func putIPNS(ctx context.Context, name ipns.Name, record []byte, endpoint string) error {
143+
drc, err := client.New(endpoint)
144+
if err != nil {
145+
return err
146+
}
147+
148+
rec, err := ipns.UnmarshalRecord(record)
171149
if err != nil {
172150
return err
173151
}
174152

175-
c := client.NewClient(ic)
176-
return c.PutIPNS(ctx, []byte(key), record)
153+
return drc.PutIPNS(ctx, name, rec)
177154
}

0 commit comments

Comments
 (0)