Skip to content

Commit 7f2f2ee

Browse files
committed
routing/http: add ProvideRecords, ProvidePeerRecords for direct publish
1 parent 98f97fa commit 7f2f2ee

File tree

3 files changed

+50
-14
lines changed

3 files changed

+50
-14
lines changed

routing/http/client/client.go

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,6 @@ type httpClient interface {
7777

7878
type Option func(*Client) error
7979

80-
func WithIdentity(identity crypto.PrivKey) Option {
81-
return func(c *Client) error {
82-
c.identity = identity
83-
return nil
84-
}
85-
}
86-
87-
// WithHTTPClient sets a custom HTTP Client to be used with [Client].
8880
func WithHTTPClient(h httpClient) Option {
8981
return func(c *Client) error {
9082
c.httpClient = h
@@ -116,8 +108,15 @@ func WithUserAgent(ua string) Option {
116108
}
117109
}
118110

119-
func WithProviderInfo(peerID peer.ID, addrs []multiaddr.Multiaddr, protocols []string) Option {
111+
// WithProviderInfo configures the [Client] with the given provider information.
112+
// This is used by the methods [Client.Provide] and [Client.ProvidePeer] in order
113+
// to create and sign announcement records.
114+
//
115+
// You can still use [Client.ProvideRecords] and [Client.ProvidePeerRecords]
116+
// without this configuration. Then, you must provide already signed-records.
117+
func WithProviderInfo(identity crypto.PrivKey, peerID peer.ID, addrs []multiaddr.Multiaddr, protocols []string) Option {
120118
return func(c *Client) error {
119+
c.identity = identity
121120
c.peerID = peerID
122121
c.protocols = protocols
123122
for _, a := range addrs {
@@ -256,6 +255,9 @@ func (c *Client) FindProviders(ctx context.Context, key cid.Cid) (providers iter
256255
return &measuringIter[iter.Result[types.Record]]{Iter: it, ctx: ctx, m: m}, nil
257256
}
258257

258+
// Provide publishes [types.AnnouncementRecord]s based on the given [types.AnnouncementRequests].
259+
// This records will be signed by your provided. Therefore, the [Client] must have been configured
260+
// with [WithProviderInfo].
259261
func (c *Client) Provide(ctx context.Context, announcements ...types.AnnouncementRequest) (iter.ResultIter[*types.AnnouncementRecord], error) {
260262
if err := c.canProvide(); err != nil {
261263
return nil, err
@@ -302,7 +304,24 @@ func (c *Client) Provide(ctx context.Context, announcements ...types.Announcemen
302304
req := jsontypes.AnnounceProvidersRequest{
303305
Providers: records,
304306
}
307+
return c.provide(ctx, url, req)
308+
}
309+
310+
// ProvideRecords publishes the given [types.AnnouncementRecord]. An error will
311+
// be returned if the records aren't signed or valid.
312+
func (c *Client) ProvideRecords(ctx context.Context, records ...*types.AnnouncementRecord) (iter.ResultIter[*types.AnnouncementRecord], error) {
313+
providerRecords := make([]types.Record, len(records))
314+
for i, record := range records {
315+
if err := record.Verify(); err != nil {
316+
return nil, err
317+
}
318+
providerRecords[i] = records[i]
319+
}
305320

321+
url := c.baseURL + "/routing/v1/providers"
322+
req := jsontypes.AnnounceProvidersRequest{
323+
Providers: providerRecords,
324+
}
306325
return c.provide(ctx, url, req)
307326
}
308327

@@ -449,7 +468,8 @@ func (c *Client) FindPeers(ctx context.Context, pid peer.ID) (peers iter.ResultI
449468
return &measuringIter[iter.Result[*types.PeerRecord]]{Iter: it, ctx: ctx, m: m}, nil
450469
}
451470

452-
// ProvidePeer provides information regarding your own peer, setup with [WithProviderInfo].
471+
// ProvidePeer publishes an [types.AnnouncementRecord] with the provider
472+
// information from your peer, configured with [WithProviderInfo].
453473
func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []byte) (iter.ResultIter[*types.AnnouncementRecord], error) {
454474
if err := c.canProvide(); err != nil {
455475
return nil, err
@@ -458,7 +478,6 @@ func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []
458478
record := &types.AnnouncementRecord{
459479
Schema: types.SchemaAnnouncement,
460480
Payload: types.AnnouncementPayload{
461-
// TODO: CID, Scope not present for /routing/v1/peers, right?
462481
Timestamp: time.Now(),
463482
TTL: ttl,
464483
ID: &c.peerID,
@@ -492,6 +511,24 @@ func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []
492511
return c.provide(ctx, url, req)
493512
}
494513

514+
// ProvidePeerRecords publishes the given [types.AnnouncementRecord]. An error will
515+
// be returned if the records aren't signed or valid.
516+
func (c *Client) ProvidePeerRecords(ctx context.Context, records ...*types.AnnouncementRecord) (iter.ResultIter[*types.AnnouncementRecord], error) {
517+
providerRecords := make([]types.Record, len(records))
518+
for i, record := range records {
519+
if err := record.Verify(); err != nil {
520+
return nil, err
521+
}
522+
providerRecords[i] = records[i]
523+
}
524+
525+
url := c.baseURL + "/routing/v1/peers"
526+
req := jsontypes.AnnouncePeersRequest{
527+
Peers: providerRecords,
528+
}
529+
return c.provide(ctx, url, req)
530+
}
531+
495532
// GetIPNS tries to retrieve the [ipns.Record] for the given [ipns.Name]. The record is
496533
// validated against the given name. If validation fails, an error is returned, but no
497534
// record.

routing/http/client/client_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ func makeTestDeps(t *testing.T, clientsOpts []Option, serverOpts []server.Option
135135
serverAddr := "http://" + server.Listener.Addr().String()
136136
recordingHTTPClient := &recordingHTTPClient{httpClient: newDefaultHTTPClient(testUserAgent)}
137137
defaultClientOpts := []Option{
138-
WithProviderInfo(peerID, addrs, nil),
139-
WithIdentity(identity),
138+
WithProviderInfo(identity, peerID, addrs, nil),
140139
WithHTTPClient(recordingHTTPClient),
141140
}
142141
c, err := New(serverAddr, append(defaultClientOpts, clientsOpts...)...)

routing/http/types/record_announcement.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (ar AnnouncementRecord) IsSigned() bool {
210210

211211
func (ar *AnnouncementRecord) Sign(peerID peer.ID, key crypto.PrivKey) error {
212212
if ar.IsSigned() {
213-
return errors.New("already signed")
213+
return ar.Verify()
214214
}
215215

216216
if key == nil {

0 commit comments

Comments
 (0)