interface.go 11 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
// 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.
19
type Path interface {
20
	// String returns the path as a string.
21
	String() string
22
	// Cid returns cid referred to by path
23
	Cid() *cid.Cid
24
	// Root returns cid of root path
25
	Root() *cid.Cid
26
	// Resolved returns whether path has been fully resolved
27 28 29 30
	Resolved() bool
}

// TODO: should we really copy these?
31
//       if we didn't, godoc would generate nice links straight to go-ipld-format
32
type Node ipld.Node
Lars Gierth's avatar
Lars Gierth committed
33
type Link ipld.Link
34 35 36 37 38 39

type Reader interface {
	io.ReadSeeker
	io.Closer
}

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

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

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

61
// CoreAPI defines an unified interface to IPFS for Go programs.
62
type CoreAPI interface {
63
	// Unixfs returns an implementation of Unixfs API.
64
	Unixfs() UnixfsAPI
65

Łukasz Magiera's avatar
Łukasz Magiera committed
66 67
	// Block returns an implementation of Block API.
	Block() BlockAPI
68

69
	// Dag returns an implementation of Dag API.
Łukasz Magiera's avatar
Łukasz Magiera committed
70
	Dag() DagAPI
71

72
	// Name returns an implementation of Name API.
Łukasz Magiera's avatar
Łukasz Magiera committed
73
	Name() NameAPI
74

75
	// Key returns an implementation of Key API.
76
	Key() KeyAPI
77

78 79 80
	// ObjectAPI returns an implementation of Object API
	Object() ObjectAPI

81
	// ResolvePath resolves the path using Unixfs resolver
82
	ResolvePath(context.Context, Path) (Path, error)
83 84 85

	// ResolveNode resolves the path (if not resolved already) using Unixfs
	// resolver, gets and returns the resolved Node
Łukasz Magiera's avatar
Łukasz Magiera committed
86
	ResolveNode(context.Context, Path) (Node, error)
87 88
}

89
// UnixfsAPI is the basic interface to immutable files in IPFS
90
type UnixfsAPI interface {
91
	// Add imports the data from the reader into merkledag file
92
	Add(context.Context, io.Reader) (Path, error)
93 94

	// Cat returns a reader for the file
95
	Cat(context.Context, Path) (Reader, error)
96 97

	// Ls returns the list of links in a directory
98
	Ls(context.Context, Path) ([]*Link, error)
99 100
}

Łukasz Magiera's avatar
Łukasz Magiera committed
101
// BlockAPI specifies the interface to the block layer
Łukasz Magiera's avatar
Łukasz Magiera committed
102
type BlockAPI interface {
Łukasz Magiera's avatar
Łukasz Magiera committed
103
	// Put imports raw block data, hashing it using specified settings.
Łukasz Magiera's avatar
Łukasz Magiera committed
104
	Put(context.Context, io.Reader, ...options.BlockPutOption) (Path, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
105 106 107

	// 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
108
	WithFormat(codec string) options.BlockPutOption
Łukasz Magiera's avatar
Łukasz Magiera committed
109 110 111 112

	// 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
113 114
	WithHash(mhType uint64, mhLen int) options.BlockPutOption

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

Łukasz Magiera's avatar
Łukasz Magiera committed
118 119 120 121 122
	// 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
123
	Rm(context.Context, Path, ...options.BlockRmOption) error
Łukasz Magiera's avatar
Łukasz Magiera committed
124 125 126

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

Łukasz Magiera's avatar
Łukasz Magiera committed
129
	// Stat returns information on
Łukasz Magiera's avatar
Łukasz Magiera committed
130
	Stat(context.Context, Path) (BlockStat, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
131 132
}

Łukasz Magiera's avatar
Łukasz Magiera committed
133
// DagAPI specifies the interface to IPLD
Łukasz Magiera's avatar
Łukasz Magiera committed
134
type DagAPI interface {
Łukasz Magiera's avatar
Łukasz Magiera committed
135
	// Put inserts data using specified format and input encoding.
Łukasz Magiera's avatar
Łukasz Magiera committed
136 137 138
	// 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)
139 140 141 142 143 144 145 146 147 148 149 150 151 152

	// 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
153 154

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

	// Tree returns list of paths within a node specified by the path.
158 159 160 161 162
	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
163 164
}

165 166 167 168 169 170 171 172
// 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
173
type NameAPI interface {
174
	// Publish announces new IPNS name
175
	Publish(ctx context.Context, path Path, opts ...options.NamePublishOption) (IpnsEntry, error)
176 177 178

	// WithValidTime is an option for Publish which specifies for how long the
	// entry will remain valid. Default value is 24h
179
	WithValidTime(validTime time.Duration) options.NamePublishOption
180 181 182

	// 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.
183
	// The key parameter must be either PeerID or keystore key alias.
184
	//
185
	// You can use KeyAPI to list and generate more names and their respective keys.
186 187
	WithKey(key string) options.NamePublishOption

188
	// Resolve attempts to resolve the newest version of the specified name
189
	Resolve(ctx context.Context, name string, opts ...options.NameResolveOption) (Path, error)
190 191 192

	// WithRecursive is an option for Resolve which specifies whether to perform a
	// recursive lookup. Default value is false
193
	WithRecursive(recursive bool) options.NameResolveOption
194 195 196

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

199 200 201
	// 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
202 203
}

204
// KeyAPI specifies the interface to Keystore
205
type KeyAPI interface {
206 207
	// Generate generates new key, stores it in the keystore under the specified
	// name and returns a base58 encoded multihash of it's public key
208
	Generate(ctx context.Context, name string, opts ...options.KeyGenerateOption) (Key, error)
209

Łukasz Magiera's avatar
Łukasz Magiera committed
210
	// WithType is an option for Generate which specifies which algorithm
211
	// should be used for the key. Default is options.RSAKey
212
	//
Łukasz Magiera's avatar
Łukasz Magiera committed
213
	// Supported key types:
214 215
	// * options.RSAKey
	// * options.Ed25519Key
Łukasz Magiera's avatar
Łukasz Magiera committed
216
	WithType(algorithm string) options.KeyGenerateOption
217 218

	// WithSize is an option for Generate which specifies the size of the key to
Łukasz Magiera's avatar
Łukasz Magiera committed
219 220 221 222
	// generated. Default is -1
	//
	// value of -1 means 'use default size for key type':
	//  * 2048 for RSA
223 224
	WithSize(size int) options.KeyGenerateOption

225 226 227
	// 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)
228 229 230

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

233
	// List lists keys stored in keystore
234
	List(ctx context.Context) ([]Key, error)
235

236 237
	// 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
238 239
}

Łukasz Magiera's avatar
Łukasz Magiera committed
240 241
// ObjectAPI specifies the interface to MerkleDAG and contains useful utilities
// for manipulating MerkleDAG data structures.
242
type ObjectAPI interface {
Łukasz Magiera's avatar
Łukasz Magiera committed
243
	// New creates new, empty (by default) dag-node.
Łukasz Magiera's avatar
Łukasz Magiera committed
244
	New(context.Context, ...options.ObjectNewOption) (Node, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
245 246 247 248 249 250 251

	// 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
252 253
	WithType(string) options.ObjectNewOption

254 255 256 257 258 259 260 261 262 263
	// 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
264

Łukasz Magiera's avatar
Łukasz Magiera committed
265 266 267 268 269 270 271 272
	// 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
273
	// Get returns the node for the path
274
	Get(context.Context, Path) (Node, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
275 276

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

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

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

Łukasz Magiera's avatar
Łukasz Magiera committed
285 286 287
	// 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
288
	AddLink(ctx context.Context, base Path, name string, child Path, opts ...options.ObjectAddLinkOption) (Path, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
289 290 291

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

Łukasz Magiera's avatar
Łukasz Magiera committed
294
	// RmLink removes a link from the node
Łukasz Magiera's avatar
Łukasz Magiera committed
295
	RmLink(ctx context.Context, base Path, link string) (Path, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
296 297

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

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

Łukasz Magiera's avatar
Łukasz Magiera committed
304
// ObjectStat provides information about dag nodes
305
type ObjectStat struct {
Łukasz Magiera's avatar
Łukasz Magiera committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	// 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
321
	// CumulativeSize is size of the tree (BlockSize + link sizes)
322 323
	CumulativeSize int
}
324 325 326

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