@@ -77,14 +77,6 @@ type httpClient interface {
77
77
78
78
type Option func (* Client ) error
79
79
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].
88
80
func WithHTTPClient (h httpClient ) Option {
89
81
return func (c * Client ) error {
90
82
c .httpClient = h
@@ -116,8 +108,15 @@ func WithUserAgent(ua string) Option {
116
108
}
117
109
}
118
110
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 {
120
118
return func (c * Client ) error {
119
+ c .identity = identity
121
120
c .peerID = peerID
122
121
c .protocols = protocols
123
122
for _ , a := range addrs {
@@ -256,6 +255,9 @@ func (c *Client) FindProviders(ctx context.Context, key cid.Cid) (providers iter
256
255
return & measuringIter [iter.Result [types.Record ]]{Iter : it , ctx : ctx , m : m }, nil
257
256
}
258
257
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].
259
261
func (c * Client ) Provide (ctx context.Context , announcements ... types.AnnouncementRequest ) (iter.ResultIter [* types.AnnouncementRecord ], error ) {
260
262
if err := c .canProvide (); err != nil {
261
263
return nil , err
@@ -302,7 +304,24 @@ func (c *Client) Provide(ctx context.Context, announcements ...types.Announcemen
302
304
req := jsontypes.AnnounceProvidersRequest {
303
305
Providers : records ,
304
306
}
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
+ }
305
320
321
+ url := c .baseURL + "/routing/v1/providers"
322
+ req := jsontypes.AnnounceProvidersRequest {
323
+ Providers : providerRecords ,
324
+ }
306
325
return c .provide (ctx , url , req )
307
326
}
308
327
@@ -449,7 +468,8 @@ func (c *Client) FindPeers(ctx context.Context, pid peer.ID) (peers iter.ResultI
449
468
return & measuringIter [iter.Result [* types.PeerRecord ]]{Iter : it , ctx : ctx , m : m }, nil
450
469
}
451
470
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].
453
473
func (c * Client ) ProvidePeer (ctx context.Context , ttl time.Duration , metadata []byte ) (iter.ResultIter [* types.AnnouncementRecord ], error ) {
454
474
if err := c .canProvide (); err != nil {
455
475
return nil , err
@@ -458,7 +478,6 @@ func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []
458
478
record := & types.AnnouncementRecord {
459
479
Schema : types .SchemaAnnouncement ,
460
480
Payload : types.AnnouncementPayload {
461
- // TODO: CID, Scope not present for /routing/v1/peers, right?
462
481
Timestamp : time .Now (),
463
482
TTL : ttl ,
464
483
ID : & c .peerID ,
@@ -492,6 +511,24 @@ func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []
492
511
return c .provide (ctx , url , req )
493
512
}
494
513
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
+
495
532
// GetIPNS tries to retrieve the [ipns.Record] for the given [ipns.Name]. The record is
496
533
// validated against the given name. If validation fails, an error is returned, but no
497
534
// record.
0 commit comments