Skip to content

Commit 813b59f

Browse files
committed
routing/http: add ProvideRecords, ProvidePeerRecords for direct publish
1 parent e1aa41c commit 813b59f

File tree

3 files changed

+50
-12
lines changed

3 files changed

+50
-12
lines changed

routing/http/client/client.go

+48-9
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ type httpClient interface {
7272

7373
type Option func(*Client)
7474

75-
func WithIdentity(identity crypto.PrivKey) Option {
76-
return func(c *Client) {
77-
c.identity = identity
78-
}
79-
}
80-
8175
func WithHTTPClient(h httpClient) Option {
8276
return func(c *Client) {
8377
c.httpClient = h
@@ -101,8 +95,15 @@ func WithUserAgent(ua string) Option {
10195
}
10296
}
10397

104-
func WithProviderInfo(peerID peer.ID, addrs []multiaddr.Multiaddr, protocols []string) Option {
98+
// WithProviderInfo configures the [Client] with the given provider information.
99+
// This is used by the methods [Client.Provide] and [Client.ProvidePeer] in order
100+
// to create and sign announcement records.
101+
//
102+
// You can still use [Client.ProvideRecords] and [Client.ProvidePeerRecords]
103+
// without this configuration. Then, you must provide already signed-records.
104+
func WithProviderInfo(identity crypto.PrivKey, peerID peer.ID, addrs []multiaddr.Multiaddr, protocols []string) Option {
105105
return func(c *Client) {
106+
c.identity = identity
106107
c.peerID = peerID
107108
c.protocols = protocols
108109
for _, a := range addrs {
@@ -236,6 +237,9 @@ func (c *Client) FindProviders(ctx context.Context, key cid.Cid) (providers iter
236237
return &measuringIter[iter.Result[types.Record]]{Iter: it, ctx: ctx, m: m}, nil
237238
}
238239

240+
// Provide publishes [types.AnnouncementRecord]s based on the given [types.AnnouncementRequests].
241+
// This records will be signed by your provided. Therefore, the [Client] must have been configured
242+
// with [WithProviderInfo].
239243
func (c *Client) Provide(ctx context.Context, announcements ...types.AnnouncementRequest) (iter.ResultIter[*types.AnnouncementRecord], error) {
240244
if err := c.canProvide(); err != nil {
241245
return nil, err
@@ -282,7 +286,24 @@ func (c *Client) Provide(ctx context.Context, announcements ...types.Announcemen
282286
req := jsontypes.AnnounceProvidersRequest{
283287
Providers: records,
284288
}
289+
return c.provide(ctx, url, req)
290+
}
291+
292+
// ProvideRecords publishes the given [types.AnnouncementRecord]. An error will
293+
// be returned if the records aren't signed or valid.
294+
func (c *Client) ProvideRecords(ctx context.Context, records ...*types.AnnouncementRecord) (iter.ResultIter[*types.AnnouncementRecord], error) {
295+
providerRecords := make([]types.Record, len(records))
296+
for i, record := range records {
297+
if err := record.Verify(); err != nil {
298+
return nil, err
299+
}
300+
providerRecords[i] = records[i]
301+
}
285302

303+
url := c.baseURL + "/routing/v1/providers"
304+
req := jsontypes.AnnounceProvidersRequest{
305+
Providers: providerRecords,
306+
}
286307
return c.provide(ctx, url, req)
287308
}
288309

@@ -429,7 +450,8 @@ func (c *Client) FindPeers(ctx context.Context, pid peer.ID) (peers iter.ResultI
429450
return &measuringIter[iter.Result[*types.PeerRecord]]{Iter: it, ctx: ctx, m: m}, nil
430451
}
431452

432-
// ProvidePeer provides information regarding your own peer, setup with [WithProviderInfo].
453+
// ProvidePeer publishes an [types.AnnouncementRecord] with the provider
454+
// information from your peer, configured with [WithProviderInfo].
433455
func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []byte) (iter.ResultIter[*types.AnnouncementRecord], error) {
434456
if err := c.canProvide(); err != nil {
435457
return nil, err
@@ -438,7 +460,6 @@ func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []
438460
record := &types.AnnouncementRecord{
439461
Schema: types.SchemaAnnouncement,
440462
Payload: types.AnnouncementPayload{
441-
// TODO: CID, Scope not present for /routing/v1/peers, right?
442463
Timestamp: time.Now(),
443464
TTL: ttl,
444465
ID: &c.peerID,
@@ -472,6 +493,24 @@ func (c *Client) ProvidePeer(ctx context.Context, ttl time.Duration, metadata []
472493
return c.provide(ctx, url, req)
473494
}
474495

496+
// ProvidePeerRecords publishes the given [types.AnnouncementRecord]. An error will
497+
// be returned if the records aren't signed or valid.
498+
func (c *Client) ProvidePeerRecords(ctx context.Context, records ...*types.AnnouncementRecord) (iter.ResultIter[*types.AnnouncementRecord], error) {
499+
providerRecords := make([]types.Record, len(records))
500+
for i, record := range records {
501+
if err := record.Verify(); err != nil {
502+
return nil, err
503+
}
504+
providerRecords[i] = records[i]
505+
}
506+
507+
url := c.baseURL + "/routing/v1/peers"
508+
req := jsontypes.AnnouncePeersRequest{
509+
Peers: providerRecords,
510+
}
511+
return c.provide(ctx, url, req)
512+
}
513+
475514
// GetIPNS tries to retrieve the [ipns.Record] for the given [ipns.Name]. The record is
476515
// validated against the given name. If validation fails, an error is returned, but no
477516
// record.

routing/http/client/client_test.go

+1-2
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: defaultHTTPClient}
137137
defaultClientOpts := []Option{
138-
WithProviderInfo(peerID, addrs, nil),
139-
WithIdentity(identity),
138+
WithProviderInfo(identity, peerID, addrs, nil),
140139
WithUserAgent(testUserAgent),
141140
WithHTTPClient(recordingHTTPClient),
142141
}

routing/http/types/record_announcement.go

+1-1
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)