interface.go 12.7 KB
Newer Older
1 2
// Package iface defines IPFS Core API which is a set of interfaces used to
// interact with IPFS nodes.
3 4 5 6 7 8
package iface

import (
	"context"
	"errors"
	"io"
Łukasz Magiera's avatar
Łukasz Magiera committed
9
	"time"
10

11 12
	options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"

Steven Allen's avatar
Steven Allen committed
13 14
	cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
	ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
15 16
)

17 18 19
var ErrIsDir = errors.New("object is a directory")
var ErrOffline = errors.New("can't resolve, ipfs node is offline")

20 21
// Path is a generic wrapper for paths used in the API. A path can be resolved
// to a CID using one of Resolve functions in the API.
22
type Path interface {
23
	// String returns the path as a string.
24
	String() string
25
	// Cid returns cid referred to by path
26
	Cid() *cid.Cid
27
	// Root returns cid of root path
28
	Root() *cid.Cid
29
	// Resolved returns whether path has been fully resolved
30 31 32
	Resolved() bool
}

33 34 35 36 37
type Reader interface {
	io.ReadSeeker
	io.Closer
}

38
// IpnsEntry specifies the interface to IpnsEntries
39
type IpnsEntry interface {
40
	// Name returns IpnsEntry name
41
	Name() string
42
	// Value returns IpnsEntry value
43 44 45
	Value() Path
}

46
// Key specifies the interface to Keys in KeyAPI Keystore
47
type Key interface {
48
	// Key returns key name
49
	Name() string
50
	// Path returns key path
51 52 53
	Path() Path
}

Łukasz Magiera's avatar
Łukasz Magiera committed
54 55 56 57 58
type BlockStat interface {
	Size() int
	Path() Path
}

Łukasz Magiera's avatar
Łukasz Magiera committed
59 60 61 62 63 64 65 66 67
// Pin holds information about pinned resource
type Pin interface {
	// Path to the pinned object
	Path() Path

	// Type of the pin
	Type() string
}

Łukasz Magiera's avatar
Łukasz Magiera committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
// PinStatus holds information about pin health
type PinStatus interface {
	// Ok indicates whether the pin has been verified to be correct
	Ok() bool

	// BadNodes returns any bad (usually missing) nodes from the pin
	BadNodes() []BadPinNode
}

// BadPinNode is a node that has been marked as bad by Pin.Verify
type BadPinNode interface {
	// Path is the path of the node
	Path() Path

	// Err is the reason why the node has been marked as bad
	Err() error
}

86
// CoreAPI defines an unified interface to IPFS for Go programs.
87
type CoreAPI interface {
88
	// Unixfs returns an implementation of Unixfs API.
89
	Unixfs() UnixfsAPI
90

Łukasz Magiera's avatar
Łukasz Magiera committed
91 92
	// Block returns an implementation of Block API.
	Block() BlockAPI
93

94
	// Dag returns an implementation of Dag API.
Łukasz Magiera's avatar
Łukasz Magiera committed
95
	Dag() DagAPI
96

97
	// Name returns an implementation of Name API.
Łukasz Magiera's avatar
Łukasz Magiera committed
98
	Name() NameAPI
99

100
	// Key returns an implementation of Key API.
101
	Key() KeyAPI
Łukasz Magiera's avatar
Łukasz Magiera committed
102
	Pin() PinAPI
103

104 105 106
	// ObjectAPI returns an implementation of Object API
	Object() ObjectAPI

107
	// ResolvePath resolves the path using Unixfs resolver
108
	ResolvePath(context.Context, Path) (Path, error)
109 110 111

	// ResolveNode resolves the path (if not resolved already) using Unixfs
	// resolver, gets and returns the resolved Node
112
	ResolveNode(context.Context, Path) (ipld.Node, error)
113 114
}

115
// UnixfsAPI is the basic interface to immutable files in IPFS
116
type UnixfsAPI interface {
117
	// Add imports the data from the reader into merkledag file
118
	Add(context.Context, io.Reader) (Path, error)
119 120

	// Cat returns a reader for the file
121
	Cat(context.Context, Path) (Reader, error)
122 123

	// Ls returns the list of links in a directory
124
	Ls(context.Context, Path) ([]*ipld.Link, error)
125 126
}

Łukasz Magiera's avatar
Łukasz Magiera committed
127
// BlockAPI specifies the interface to the block layer
Łukasz Magiera's avatar
Łukasz Magiera committed
128
type BlockAPI interface {
Łukasz Magiera's avatar
Łukasz Magiera committed
129
	// Put imports raw block data, hashing it using specified settings.
Łukasz Magiera's avatar
Łukasz Magiera committed
130
	Put(context.Context, io.Reader, ...options.BlockPutOption) (Path, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
131 132 133

	// WithFormat is an option for Put which specifies the multicodec to use to
	// serialize the object. Default is "v0"
Łukasz Magiera's avatar
Łukasz Magiera committed
134
	WithFormat(codec string) options.BlockPutOption
Łukasz Magiera's avatar
Łukasz Magiera committed
135 136 137 138

	// WithHash is an option for Put which specifies the multihash settings to use
	// when hashing the object. Default is mh.SHA2_256 (0x12).
	// If mhLen is set to -1, default length for the hash will be used
Łukasz Magiera's avatar
Łukasz Magiera committed
139 140
	WithHash(mhType uint64, mhLen int) options.BlockPutOption

Łukasz Magiera's avatar
Łukasz Magiera committed
141
	// Get attempts to resolve the path and return a reader for data in the block
Łukasz Magiera's avatar
Łukasz Magiera committed
142
	Get(context.Context, Path) (io.Reader, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
143

Łukasz Magiera's avatar
Łukasz Magiera committed
144 145 146 147 148
	// Rm removes the block specified by the path from local blockstore.
	// By default an error will be returned if the block can't be found locally.
	//
	// NOTE: If the specified block is pinned it won't be removed and no error
	// will be returned
Łukasz Magiera's avatar
Łukasz Magiera committed
149
	Rm(context.Context, Path, ...options.BlockRmOption) error
Łukasz Magiera's avatar
Łukasz Magiera committed
150 151 152

	// WithForce is an option for Rm which, when set to true, will ignore
	// non-existing blocks
Łukasz Magiera's avatar
Łukasz Magiera committed
153 154
	WithForce(force bool) options.BlockRmOption

Łukasz Magiera's avatar
Łukasz Magiera committed
155
	// Stat returns information on
Łukasz Magiera's avatar
Łukasz Magiera committed
156
	Stat(context.Context, Path) (BlockStat, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
157 158
}

Łukasz Magiera's avatar
Łukasz Magiera committed
159
// DagAPI specifies the interface to IPLD
Łukasz Magiera's avatar
Łukasz Magiera committed
160
type DagAPI interface {
Łukasz Magiera's avatar
Łukasz Magiera committed
161
	// Put inserts data using specified format and input encoding.
Łukasz Magiera's avatar
Łukasz Magiera committed
162 163 164
	// Unless used with WithCodec or WithHash, the defaults "dag-cbor" and
	// "sha256" are used.
	Put(ctx context.Context, src io.Reader, opts ...options.DagPutOption) (Path, error)
165 166 167 168 169 170 171 172 173 174 175 176 177 178

	// WithInputEnc is an option for Put which specifies the input encoding of the
	// data. Default is "json", most formats/codecs support "raw"
	WithInputEnc(enc string) options.DagPutOption

	// WithCodec is an option for Put which specifies the multicodec to use to
	// serialize the object. Default is cid.DagCBOR (0x71)
	WithCodec(codec uint64) options.DagPutOption

	// WithHash is an option for Put which specifies the multihash settings to use
	// when hashing the object. Default is based on the codec used
	// (mh.SHA2_256 (0x12) for DagCBOR). If mhLen is set to -1, default length for
	// the hash will be used
	WithHash(mhType uint64, mhLen int) options.DagPutOption
Łukasz Magiera's avatar
Łukasz Magiera committed
179 180

	// Get attempts to resolve and get the node specified by the path
181
	Get(ctx context.Context, path Path) (ipld.Node, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
182 183

	// Tree returns list of paths within a node specified by the path.
184 185 186 187 188
	Tree(ctx context.Context, path Path, opts ...options.DagTreeOption) ([]Path, error)

	// WithDepth is an option for Tree which specifies maximum depth of the
	// returned tree. Default is -1 (no depth limit)
	WithDepth(depth int) options.DagTreeOption
Łukasz Magiera's avatar
Łukasz Magiera committed
189 190
}

191 192 193 194 195 196 197 198
// NameAPI specifies the interface to IPNS.
//
// IPNS is a PKI namespace, where names are the hashes of public keys, and the
// private key enables publishing new (signed) values. In both publish and
// resolve, the default name used is the node's own PeerID, which is the hash of
// its public key.
//
// You can use .Key API to list and generate more names and their respective keys.
Łukasz Magiera's avatar
Łukasz Magiera committed
199
type NameAPI interface {
200
	// Publish announces new IPNS name
201
	Publish(ctx context.Context, path Path, opts ...options.NamePublishOption) (IpnsEntry, error)
202 203 204

	// WithValidTime is an option for Publish which specifies for how long the
	// entry will remain valid. Default value is 24h
205
	WithValidTime(validTime time.Duration) options.NamePublishOption
206 207 208

	// WithKey is an option for Publish which specifies the key to use for
	// publishing. Default value is "self" which is the node's own PeerID.
209
	// The key parameter must be either PeerID or keystore key alias.
210
	//
211
	// You can use KeyAPI to list and generate more names and their respective keys.
212 213
	WithKey(key string) options.NamePublishOption

214
	// Resolve attempts to resolve the newest version of the specified name
215
	Resolve(ctx context.Context, name string, opts ...options.NameResolveOption) (Path, error)
216 217 218

	// WithRecursive is an option for Resolve which specifies whether to perform a
	// recursive lookup. Default value is false
219
	WithRecursive(recursive bool) options.NameResolveOption
220 221 222

	// WithLocal is an option for Resolve which specifies if the lookup should be
	// offline. Default value is false
223
	WithLocal(local bool) options.NameResolveOption
224

225 226 227
	// WithCache is an option for Resolve which specifies if cache should be used.
	// Default value is true
	WithCache(cache bool) options.NameResolveOption
Łukasz Magiera's avatar
Łukasz Magiera committed
228 229
}

230
// KeyAPI specifies the interface to Keystore
231
type KeyAPI interface {
232 233
	// Generate generates new key, stores it in the keystore under the specified
	// name and returns a base58 encoded multihash of it's public key
234
	Generate(ctx context.Context, name string, opts ...options.KeyGenerateOption) (Key, error)
235

Łukasz Magiera's avatar
Łukasz Magiera committed
236
	// WithType is an option for Generate which specifies which algorithm
237
	// should be used for the key. Default is options.RSAKey
238
	//
Łukasz Magiera's avatar
Łukasz Magiera committed
239
	// Supported key types:
240 241
	// * options.RSAKey
	// * options.Ed25519Key
Łukasz Magiera's avatar
Łukasz Magiera committed
242
	WithType(algorithm string) options.KeyGenerateOption
243 244

	// WithSize is an option for Generate which specifies the size of the key to
Łukasz Magiera's avatar
Łukasz Magiera committed
245 246 247 248
	// generated. Default is -1
	//
	// value of -1 means 'use default size for key type':
	//  * 2048 for RSA
249 250
	WithSize(size int) options.KeyGenerateOption

251 252 253
	// Rename renames oldName key to newName. Returns the key and whether another
	// key was overwritten, or an error
	Rename(ctx context.Context, oldName string, newName string, opts ...options.KeyRenameOption) (Key, bool, error)
254 255 256

	// WithForce is an option for Rename which specifies whether to allow to
	// replace existing keys.
257 258
	WithForce(force bool) options.KeyRenameOption

259
	// List lists keys stored in keystore
260
	List(ctx context.Context) ([]Key, error)
261

262 263
	// Remove removes keys from keystore. Returns ipns path of the removed key
	Remove(ctx context.Context, name string) (Path, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
264 265
}

Łukasz Magiera's avatar
Łukasz Magiera committed
266 267
// ObjectAPI specifies the interface to MerkleDAG and contains useful utilities
// for manipulating MerkleDAG data structures.
268
type ObjectAPI interface {
Łukasz Magiera's avatar
Łukasz Magiera committed
269
	// New creates new, empty (by default) dag-node.
270
	New(context.Context, ...options.ObjectNewOption) (ipld.Node, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
271 272 273 274 275 276 277

	// WithType is an option for New which allows to change the type of created
	// dag node.
	//
	// Supported types:
	// * 'empty' - Empty node
	// * 'unixfs-dir' - Empty UnixFS directory
Łukasz Magiera's avatar
Łukasz Magiera committed
278 279
	WithType(string) options.ObjectNewOption

280 281 282 283 284 285 286 287 288 289
	// Put imports the data into merkledag
	Put(context.Context, io.Reader, ...options.ObjectPutOption) (Path, error)

	// WithInputEnc is an option for Put which specifies the input encoding of the
	// data. Default is "json".
	//
	// Supported encodings:
	// * "protobuf"
	// * "json"
	WithInputEnc(e string) options.ObjectPutOption
Łukasz Magiera's avatar
Łukasz Magiera committed
290

Łukasz Magiera's avatar
Łukasz Magiera committed
291 292 293 294 295 296 297 298
	// WithDataType specifies the encoding of data field when using Josn or XML
	// input encoding.
	//
	// Supported types:
	// * "text" (default)
	// * "base64"
	WithDataType(t string) options.ObjectPutOption

Łukasz Magiera's avatar
Łukasz Magiera committed
299
	// Get returns the node for the path
300
	Get(context.Context, Path) (ipld.Node, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
301 302

	// Data returns reader for data of the node
303
	Data(context.Context, Path) (io.Reader, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
304 305

	// Links returns lint or links the node contains
306
	Links(context.Context, Path) ([]*ipld.Link, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
307 308

	// Stat returns information about the node
309 310
	Stat(context.Context, Path) (*ObjectStat, error)

Łukasz Magiera's avatar
Łukasz Magiera committed
311 312 313
	// AddLink adds a link under the specified path. child path can point to a
	// subdirectory within the patent which must be present (can be overridden
	// with WithCreate option).
Łukasz Magiera's avatar
Łukasz Magiera committed
314
	AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (Path, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
315 316 317

	// WithCreate is an option for AddLink which specifies whether create required
	// directories for the child
Łukasz Magiera's avatar
Łukasz Magiera committed
318 319
	WithCreate(create bool) options.ObjectAddLinkOption

Łukasz Magiera's avatar
Łukasz Magiera committed
320
	// RmLink removes a link from the node
Łukasz Magiera's avatar
Łukasz Magiera committed
321
	RmLink(ctx context.Context, base Path, link string) (Path, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
322 323

	// AppendData appends data to the node
Łukasz Magiera's avatar
Łukasz Magiera committed
324
	AppendData(context.Context, Path, io.Reader) (Path, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
325 326

	// SetData sets the data contained in the node
Łukasz Magiera's avatar
Łukasz Magiera committed
327
	SetData(context.Context, Path, io.Reader) (Path, error)
328 329
}

Łukasz Magiera's avatar
Łukasz Magiera committed
330
// ObjectStat provides information about dag nodes
331
type ObjectStat struct {
Łukasz Magiera's avatar
Łukasz Magiera committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
	// Cid is the CID of the node
	Cid *cid.Cid

	// NumLinks is number of links the node contains
	NumLinks int

	// BlockSize is size of the raw serialized node
	BlockSize int

	// LinksSize is size of the links block section
	LinksSize int

	// DataSize is the size of data block section
	DataSize int

Łukasz Magiera's avatar
Łukasz Magiera committed
347
	// CumulativeSize is size of the tree (BlockSize + link sizes)
348 349
	CumulativeSize int
}
350

Łukasz Magiera's avatar
Łukasz Magiera committed
351 352 353 354 355 356 357 358 359 360 361
// PinAPI specifies the interface to pining
type PinAPI interface {
	// Add creates new pin, be default recursive - pinning the whole referenced
	// tree
	Add(context.Context, Path, ...options.PinAddOption) error

	// WithRecursive is an option for Add which specifies whether to pin an entire
	// object tree or just one object. Default: true
	WithRecursive(bool) options.PinAddOption

	// Ls returns list of pinned objects on this node
Łukasz Magiera's avatar
Łukasz Magiera committed
362
	Ls(context.Context, ...options.PinLsOption) ([]Pin, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

	// WithType is an option for Ls which allows to specify which pin types should
	// be returned
	//
	// Supported values:
	// * "direct" - directly pinned objects
	// * "recursive" - roots of recursive pins
	// * "indirect" - indirectly pinned objects (referenced by recursively pinned
	//    objects)
	// * "all" - all pinned objects (default)
	WithType(string) options.PinLsOption

	// Rm removes pin for object specified by the path
	Rm(context.Context, Path) error

	// Update changes one pin to another, skipping checks for matching paths in
	// the old tree
Łukasz Magiera's avatar
Łukasz Magiera committed
380
	Update(ctx context.Context, from Path, to Path, opts ...options.PinUpdateOption) error
Łukasz Magiera's avatar
Łukasz Magiera committed
381 382

	// Verify verifies the integrity of pinned objects
Łukasz Magiera's avatar
Łukasz Magiera committed
383
	Verify(context.Context) (<-chan PinStatus, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
384
}