@@ -12,6 +12,7 @@ import (
1212 ipns "github.com/ipfs/go-ipns"
1313 pb "github.com/ipfs/go-ipns/pb"
1414 path "github.com/ipfs/go-path"
15+ opts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
1516 "github.com/libp2p/go-libp2p-core/crypto"
1617 peer "github.com/libp2p/go-libp2p-core/peer"
1718 routing "github.com/libp2p/go-libp2p-core/routing"
@@ -22,11 +23,6 @@ import (
2223
2324const ipnsPrefix = "/ipns/"
2425
25- // DefaultRecordEOL specifies the time that the network will cache IPNS
26- // records after being publihsed. Records should be re-published before this
27- // interval expires.
28- const DefaultRecordEOL = 24 * time .Hour
29-
3026// IpnsPublisher is capable of publishing and resolving names to the IPFS
3127// routing system.
3228type IpnsPublisher struct {
@@ -47,9 +43,18 @@ func NewIpnsPublisher(route routing.ValueStore, ds ds.Datastore) *IpnsPublisher
4743
4844// Publish implements Publisher. Accepts a keypair and a value,
4945// and publishes it out to the routing system
50- func (p * IpnsPublisher ) Publish (ctx context.Context , k crypto.PrivKey , value path.Path ) error {
46+ func (p * IpnsPublisher ) Publish (ctx context.Context , k crypto.PrivKey , value path.Path , options ... opts. PublishOption ) error {
5147 log .Debugf ("Publish %s" , value )
52- return p .PublishWithEOL (ctx , k , value , time .Now ().Add (DefaultRecordEOL ))
48+
49+ ctx , span := StartSpan (ctx , "IpnsPublisher.Publish" , trace .WithAttributes (attribute .String ("Value" , value .String ())))
50+ defer span .End ()
51+
52+ record , err := p .updateRecord (ctx , k , value , options ... )
53+ if err != nil {
54+ return err
55+ }
56+
57+ return PutRecordToRouting (ctx , p .routing , k .GetPublic (), record )
5358}
5459
5560// IpnsDsKey returns a datastore key given an IPNS identifier (peer
@@ -142,7 +147,7 @@ func (p *IpnsPublisher) GetPublished(ctx context.Context, id peer.ID, checkRouti
142147 return e , nil
143148}
144149
145- func (p * IpnsPublisher ) updateRecord (ctx context.Context , k crypto.PrivKey , value path.Path , eol time. Time ) (* pb.IpnsEntry , error ) {
150+ func (p * IpnsPublisher ) updateRecord (ctx context.Context , k crypto.PrivKey , value path.Path , options ... opts. PublishOption ) (* pb.IpnsEntry , error ) {
146151 id , err := peer .IDFromPrivateKey (k )
147152 if err != nil {
148153 return nil , err
@@ -164,12 +169,10 @@ func (p *IpnsPublisher) updateRecord(ctx context.Context, k crypto.PrivKey, valu
164169 seqno ++
165170 }
166171
167- // Set the TTL
168- // TODO: Make this less hacky.
169- ttl , _ := checkCtxTTL (ctx )
172+ opts := opts .ProcessPublishOptions (options )
170173
171174 // Create record
172- entry , err := ipns .Create (k , []byte (value ), seqno , eol , ttl )
175+ entry , err := ipns .Create (k , []byte (value ), seqno , opts . EOL , opts . TTL )
173176 if err != nil {
174177 return nil , err
175178 }
@@ -190,33 +193,6 @@ func (p *IpnsPublisher) updateRecord(ctx context.Context, k crypto.PrivKey, valu
190193 return entry , nil
191194}
192195
193- // PublishWithEOL is a temporary stand in for the ipns records implementation
194- // see here for more details: https://github.com/ipfs/specs/tree/master/records
195- func (p * IpnsPublisher ) PublishWithEOL (ctx context.Context , k crypto.PrivKey , value path.Path , eol time.Time ) error {
196- ctx , span := StartSpan (ctx , "IpnsPublisher.PublishWithEOL" , trace .WithAttributes (attribute .String ("Value" , value .String ())))
197- defer span .End ()
198-
199- record , err := p .updateRecord (ctx , k , value , eol )
200- if err != nil {
201- return err
202- }
203-
204- return PutRecordToRouting (ctx , p .routing , k .GetPublic (), record )
205- }
206-
207- // setting the TTL on published records is an experimental feature.
208- // as such, i'm using the context to wire it through to avoid changing too
209- // much code along the way.
210- func checkCtxTTL (ctx context.Context ) (time.Duration , bool ) {
211- v := ctx .Value (ttlContextKey )
212- if v == nil {
213- return 0 , false
214- }
215-
216- d , ok := v .(time.Duration )
217- return d , ok
218- }
219-
220196// PutRecordToRouting publishes the given entry using the provided ValueStore,
221197// keyed on the ID associated with the provided public key. The public key is
222198// also made available to the routing system so that entries can be verified.
@@ -307,13 +283,3 @@ func PublishEntry(ctx context.Context, r routing.ValueStore, ipnskey string, rec
307283func PkKeyForID (id peer.ID ) string {
308284 return "/pk/" + string (id )
309285}
310-
311- // contextKey is a private comparable type used to hold value keys in contexts
312- type contextKey string
313-
314- var ttlContextKey contextKey = "ipns-publish-ttl"
315-
316- // ContextWithTTL returns a copy of the parent context with an added value representing the TTL
317- func ContextWithTTL (ctx context.Context , ttl time.Duration ) context.Context {
318- return context .WithValue (context .Background (), ttlContextKey , ttl )
319- }
0 commit comments