Skip to content

Commit e576b55

Browse files
committed
add ProvideRecords and ProvidePeerRecords to allow disseminating already-signed records
1 parent b930d1f commit e576b55

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

Diff for: routing/http/client/client.go

+47-1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ func (c *Client) FindProviders(ctx context.Context, key cid.Cid) (providers iter
236236
return &measuringIter[iter.Result[types.Record]]{Iter: it, ctx: ctx, m: m}, nil
237237
}
238238

239+
// Provide publishes an [types.AnnouncementRecord] to the server, signed by you.
240+
// This [Client] must be configured with [WithIdentity] and [WithProviderInfo]
241+
// in order to be able to provide.
239242
func (c *Client) Provide(ctx context.Context, announcements ...types.AnnouncementRequest) (iter.ResultIter[*types.AnnouncementRecord], error) {
240243
if err := c.canProvide(); err != nil {
241244
return nil, err
@@ -282,7 +285,27 @@ func (c *Client) Provide(ctx context.Context, announcements ...types.Announcemen
282285
req := jsontypes.AnnounceProvidersRequest{
283286
Providers: records,
284287
}
288+
return c.provide(ctx, url, req)
289+
}
290+
291+
// ProvideRecords publishes [types.AnnouncementRecord] to the server with already
292+
// signed records. This can be useful if you've gotten an announcement from somewhere
293+
// else and you wish to spread it through the network.
294+
//
295+
// If the records aren't valid, an error will be returned.
296+
func (c *Client) ProvideRecords(ctx context.Context, records ...*types.AnnouncementRecord) (iter.ResultIter[*types.AnnouncementRecord], error) {
297+
providerRecords := make([]types.Record, len(records))
298+
for i, record := range records {
299+
if err := record.Verify(); err != nil {
300+
return nil, err
301+
}
302+
providerRecords[i] = records[i]
303+
}
285304

305+
url := c.baseURL + "/routing/v1/providers"
306+
req := jsontypes.AnnounceProvidersRequest{
307+
Providers: providerRecords,
308+
}
286309
return c.provide(ctx, url, req)
287310
}
288311

@@ -429,7 +452,9 @@ func (c *Client) FindPeers(ctx context.Context, pid peer.ID) (peers iter.ResultI
429452
return &measuringIter[iter.Result[*types.PeerRecord]]{Iter: it, ctx: ctx, m: m}, nil
430453
}
431454

432-
// ProvidePeer provides information regarding your own peer, setup with [WithProviderInfo].
455+
// ProvidePeer publishes an [types.AnnouncementRecord] to the server with information
456+
// regarding your own peer. This [Client] must be configured with [WithIdentity]
457+
// and [WithProviderInfo] in order to be able to provide.
433458
func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []byte) (iter.ResultIter[*types.AnnouncementRecord], error) {
434459
if err := c.canProvide(); err != nil {
435460
return nil, err
@@ -472,6 +497,27 @@ func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []
472497
return c.provide(ctx, url, req)
473498
}
474499

500+
// ProvidePeerRecords publishes [types.AnnouncementRecord] to the server with already
501+
// signed records. This can be useful if you've gotten an announcement from somewhere
502+
// else and you wish to spread it through the network.
503+
//
504+
// If the records aren't valid, an error will be returned.
505+
func (c *Client) ProvidePeerRecords(ctx context.Context, records ...*types.AnnouncementRecord) (iter.ResultIter[*types.AnnouncementRecord], error) {
506+
providerRecords := make([]types.Record, len(records))
507+
for i, record := range records {
508+
if err := record.Verify(); err != nil {
509+
return nil, err
510+
}
511+
providerRecords[i] = records[i]
512+
}
513+
514+
url := c.baseURL + "/routing/v1/peers"
515+
req := jsontypes.AnnouncePeersRequest{
516+
Peers: providerRecords,
517+
}
518+
return c.provide(ctx, url, req)
519+
}
520+
475521
// GetIPNS tries to retrieve the [ipns.Record] for the given [ipns.Name]. The record is
476522
// validated against the given name. If validation fails, an error is returned, but no
477523
// record.

0 commit comments

Comments
 (0)