interface.go 6.68 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
	ipld "gx/ipfs/QmNwUEK7QbwSqyKBu3mMtToo8SUc6wQJ7gdZq4gGGJqfnf/go-ipld-format"
	cid "gx/ipfs/QmeSrf6pzut73u6zLQkRFQ3ygt3k6XFT2kjdYP8Tnkwwyg/go-cid"
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 20 21 22 23 24 25 26
type Path interface {
	String() string
	Cid() *cid.Cid
	Root() *cid.Cid
	Resolved() bool
}

// TODO: should we really copy these?
27
//       if we didn't, godoc would generate nice links straight to go-ipld-format
28
type Node ipld.Node
Lars Gierth's avatar
Lars Gierth committed
29
type Link ipld.Link
30

Łukasz Magiera's avatar
Łukasz Magiera committed
31 32 33 34 35
type IpnsEntry struct {
	Name  string
	Value Path
}

36 37 38 39 40
type Reader interface {
	io.ReadSeeker
	io.Closer
}

41
// CoreAPI defines an unified interface to IPFS for Go programs.
42
type CoreAPI interface {
43
	// Unixfs returns an implementation of Unixfs API
44
	Unixfs() UnixfsAPI
Łukasz Magiera's avatar
Łukasz Magiera committed
45
	Dag() DagAPI
Łukasz Magiera's avatar
Łukasz Magiera committed
46
	Name() NameAPI
47
	Key() KeyAPI
48 49

	// ResolvePath resolves the path using Unixfs resolver
50
	ResolvePath(context.Context, Path) (Path, error)
51 52 53

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

57
// UnixfsAPI is the basic interface to immutable files in IPFS
58
type UnixfsAPI interface {
59
	// Add imports the data from the reader into merkledag file
60
	Add(context.Context, io.Reader) (Path, error)
61 62

	// Cat returns a reader for the file
63
	Cat(context.Context, Path) (Reader, error)
64 65

	// Ls returns the list of links in a directory
66
	Ls(context.Context, Path) ([]*Link, error)
67 68
}

Łukasz Magiera's avatar
Łukasz Magiera committed
69
// DagAPI specifies the interface to IPLD
Łukasz Magiera's avatar
Łukasz Magiera committed
70
type DagAPI interface {
Łukasz Magiera's avatar
Łukasz Magiera committed
71
	// Put inserts data using specified format and input encoding.
Łukasz Magiera's avatar
Łukasz Magiera committed
72 73 74
	// 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)
75 76 77 78 79 80 81 82 83 84 85 86 87 88

	// 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
89 90

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

	// Tree returns list of paths within a node specified by the path.
94 95 96 97 98
	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
99 100
}

101 102 103 104 105 106 107 108
// 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
109
type NameAPI interface {
110
	// Publish announces new IPNS name
111
	Publish(ctx context.Context, path Path, opts ...options.NamePublishOption) (*IpnsEntry, error)
112 113 114

	// WithValidTime is an option for Publish which specifies for how long the
	// entry will remain valid. Default value is 24h
115
	WithValidTime(validTime time.Duration) options.NamePublishOption
116 117 118 119 120

	// 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.
	//
	// You can use .Key API to list and generate more names and their respective keys.
121 122
	WithKey(key string) options.NamePublishOption

123
	// Resolve attempts to resolve the newest version of the specified name
124
	Resolve(ctx context.Context, name string, opts ...options.NameResolveOption) (Path, error)
125 126 127

	// WithRecursive is an option for Resolve which specifies whether to perform a
	// recursive lookup. Default value is false
128
	WithRecursive(recursive bool) options.NameResolveOption
129 130 131

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

	// WithNoCache is an option for Resolve which specifies when set to true
	// disables the use of local name cache. Default value is false
136
	WithNoCache(nocache bool) options.NameResolveOption
Łukasz Magiera's avatar
Łukasz Magiera committed
137 138
}

139
// KeyAPI specifies the interface to Keystore
140
type KeyAPI interface {
141 142
	// Generate generates new key, stores it in the keystore under the specified
	// name and returns a base58 encoded multihash of it's public key
143
	Generate(ctx context.Context, name string, opts ...options.KeyGenerateOption) (string, error)
144 145 146 147 148 149 150

	// WithAlgorithm is an option for Generate which specifies which algorithm
	// should be used for the key. Default is "rsa"
	//
	// Supported algorithms:
	// * rsa
	// * ed25519
151
	WithAlgorithm(algorithm string) options.KeyGenerateOption
152 153 154

	// WithSize is an option for Generate which specifies the size of the key to
	// generated. Default is 0
155 156
	WithSize(size int) options.KeyGenerateOption

157
	// Rename renames oldName key to newName.
158
	Rename(ctx context.Context, oldName string, newName string, opts ...options.KeyRenameOption) (string, bool, error)
159 160 161

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

164
	// List lists keys stored in keystore
Łukasz Magiera's avatar
Łukasz Magiera committed
165
	List(ctx context.Context) (map[string]string, error) //TODO: better key type?
166 167

	// Remove removes keys from keystore
168
	Remove(ctx context.Context, name string) (string, error)
Łukasz Magiera's avatar
Łukasz Magiera committed
169 170
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
// type ObjectAPI interface {
// 	New() (cid.Cid, Object)
// 	Get(string) (Object, error)
// 	Links(string) ([]*Link, error)
// 	Data(string) (Reader, error)
// 	Stat(string) (ObjectStat, error)
// 	Put(Object) (cid.Cid, error)
// 	SetData(string, Reader) (cid.Cid, error)
// 	AppendData(string, Data) (cid.Cid, error)
// 	AddLink(string, string, string) (cid.Cid, error)
// 	RmLink(string, string) (cid.Cid, error)
// }

// type ObjectStat struct {
// 	Cid            cid.Cid
// 	NumLinks       int
// 	BlockSize      int
// 	LinksSize      int
// 	DataSize       int
// 	CumulativeSize int
// }

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