github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/go-ethereum-master/swarm/api/api.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package api 18 19 import ( 20 "context" 21 "fmt" 22 "io" 23 "math/big" 24 "net/http" 25 "path" 26 "strings" 27 28 "bytes" 29 "mime" 30 "path/filepath" 31 "time" 32 33 "github.com/ethereum/go-ethereum/common" 34 "github.com/ethereum/go-ethereum/contracts/ens" 35 "github.com/ethereum/go-ethereum/core/types" 36 "github.com/ethereum/go-ethereum/metrics" 37 "github.com/ethereum/go-ethereum/swarm/log" 38 "github.com/ethereum/go-ethereum/swarm/multihash" 39 "github.com/ethereum/go-ethereum/swarm/storage" 40 "github.com/ethereum/go-ethereum/swarm/storage/mru" 41 ) 42 43 var ( 44 apiResolveCount = metrics.NewRegisteredCounter("api.resolve.count", nil) 45 apiResolveFail = metrics.NewRegisteredCounter("api.resolve.fail", nil) 46 apiPutCount = metrics.NewRegisteredCounter("api.put.count", nil) 47 apiPutFail = metrics.NewRegisteredCounter("api.put.fail", nil) 48 apiGetCount = metrics.NewRegisteredCounter("api.get.count", nil) 49 apiGetNotFound = metrics.NewRegisteredCounter("api.get.notfound", nil) 50 apiGetHTTP300 = metrics.NewRegisteredCounter("api.get.http.300", nil) 51 apiModifyCount = metrics.NewRegisteredCounter("api.modify.count", nil) 52 apiModifyFail = metrics.NewRegisteredCounter("api.modify.fail", nil) 53 apiAddFileCount = metrics.NewRegisteredCounter("api.addfile.count", nil) 54 apiAddFileFail = metrics.NewRegisteredCounter("api.addfile.fail", nil) 55 apiRmFileCount = metrics.NewRegisteredCounter("api.removefile.count", nil) 56 apiRmFileFail = metrics.NewRegisteredCounter("api.removefile.fail", nil) 57 apiAppendFileCount = metrics.NewRegisteredCounter("api.appendfile.count", nil) 58 apiAppendFileFail = metrics.NewRegisteredCounter("api.appendfile.fail", nil) 59 apiGetInvalid = metrics.NewRegisteredCounter("api.get.invalid", nil) 60 ) 61 62 // Resolver interface resolve a domain name to a hash using ENS 63 type Resolver interface { 64 Resolve(string) (common.Hash, error) 65 } 66 67 // ResolveValidator is used to validate the contained Resolver 68 type ResolveValidator interface { 69 Resolver 70 Owner(node [32]byte) (common.Address, error) 71 HeaderByNumber(context.Context, *big.Int) (*types.Header, error) 72 } 73 74 // NoResolverError is returned by MultiResolver.Resolve if no resolver 75 // can be found for the address. 76 type NoResolverError struct { 77 TLD string 78 } 79 80 // NewNoResolverError creates a NoResolverError for the given top level domain 81 func NewNoResolverError(tld string) *NoResolverError { 82 return &NoResolverError{TLD: tld} 83 } 84 85 // Error NoResolverError implements error 86 func (e *NoResolverError) Error() string { 87 if e.TLD == "" { 88 return "no ENS resolver" 89 } 90 return fmt.Sprintf("no ENS endpoint configured to resolve .%s TLD names", e.TLD) 91 } 92 93 // MultiResolver is used to resolve URL addresses based on their TLDs. 94 // Each TLD can have multiple resolvers, and the resoluton from the 95 // first one in the sequence will be returned. 96 type MultiResolver struct { 97 resolvers map[string][]ResolveValidator 98 nameHash func(string) common.Hash 99 } 100 101 // MultiResolverOption sets options for MultiResolver and is used as 102 // arguments for its constructor. 103 type MultiResolverOption func(*MultiResolver) 104 105 // MultiResolverOptionWithResolver adds a Resolver to a list of resolvers 106 // for a specific TLD. If TLD is an empty string, the resolver will be added 107 // to the list of default resolver, the ones that will be used for resolution 108 // of addresses which do not have their TLD resolver specified. 109 func MultiResolverOptionWithResolver(r ResolveValidator, tld string) MultiResolverOption { 110 return func(m *MultiResolver) { 111 m.resolvers[tld] = append(m.resolvers[tld], r) 112 } 113 } 114 115 // MultiResolverOptionWithNameHash is unused at the time of this writing 116 func MultiResolverOptionWithNameHash(nameHash func(string) common.Hash) MultiResolverOption { 117 return func(m *MultiResolver) { 118 m.nameHash = nameHash 119 } 120 } 121 122 // NewMultiResolver creates a new instance of MultiResolver. 123 func NewMultiResolver(opts ...MultiResolverOption) (m *MultiResolver) { 124 m = &MultiResolver{ 125 resolvers: make(map[string][]ResolveValidator), 126 nameHash: ens.EnsNode, 127 } 128 for _, o := range opts { 129 o(m) 130 } 131 return m 132 } 133 134 // Resolve resolves address by choosing a Resolver by TLD. 135 // If there are more default Resolvers, or for a specific TLD, 136 // the Hash from the the first one which does not return error 137 // will be returned. 138 func (m *MultiResolver) Resolve(addr string) (h common.Hash, err error) { 139 rs, err := m.getResolveValidator(addr) 140 if err != nil { 141 return h, err 142 } 143 for _, r := range rs { 144 h, err = r.Resolve(addr) 145 if err == nil { 146 return 147 } 148 } 149 return 150 } 151 152 // ValidateOwner checks the ENS to validate that the owner of the given domain is the given eth address 153 func (m *MultiResolver) ValidateOwner(name string, address common.Address) (bool, error) { 154 rs, err := m.getResolveValidator(name) 155 if err != nil { 156 return false, err 157 } 158 var addr common.Address 159 for _, r := range rs { 160 addr, err = r.Owner(m.nameHash(name)) 161 // we hide the error if it is not for the last resolver we check 162 if err == nil { 163 return addr == address, nil 164 } 165 } 166 return false, err 167 } 168 169 // HeaderByNumber uses the validator of the given domainname and retrieves the header for the given block number 170 func (m *MultiResolver) HeaderByNumber(ctx context.Context, name string, blockNr *big.Int) (*types.Header, error) { 171 rs, err := m.getResolveValidator(name) 172 if err != nil { 173 return nil, err 174 } 175 for _, r := range rs { 176 var header *types.Header 177 header, err = r.HeaderByNumber(ctx, blockNr) 178 // we hide the error if it is not for the last resolver we check 179 if err == nil { 180 return header, nil 181 } 182 } 183 return nil, err 184 } 185 186 // getResolveValidator uses the hostname to retrieve the resolver associated with the top level domain 187 func (m *MultiResolver) getResolveValidator(name string) ([]ResolveValidator, error) { 188 rs := m.resolvers[""] 189 tld := path.Ext(name) 190 if tld != "" { 191 tld = tld[1:] 192 rstld, ok := m.resolvers[tld] 193 if ok { 194 return rstld, nil 195 } 196 } 197 if len(rs) == 0 { 198 return rs, NewNoResolverError(tld) 199 } 200 return rs, nil 201 } 202 203 // SetNameHash sets the hasher function that hashes the domain into a name hash that ENS uses 204 func (m *MultiResolver) SetNameHash(nameHash func(string) common.Hash) { 205 m.nameHash = nameHash 206 } 207 208 /* 209 API implements webserver/file system related content storage and retrieval 210 on top of the FileStore 211 it is the public interface of the FileStore which is included in the ethereum stack 212 */ 213 type API struct { 214 resource *mru.Handler 215 fileStore *storage.FileStore 216 dns Resolver 217 } 218 219 // NewAPI the api constructor initialises a new API instance. 220 func NewAPI(fileStore *storage.FileStore, dns Resolver, resourceHandler *mru.Handler) (self *API) { 221 self = &API{ 222 fileStore: fileStore, 223 dns: dns, 224 resource: resourceHandler, 225 } 226 return 227 } 228 229 // Upload to be used only in TEST 230 func (a *API) Upload(ctx context.Context, uploadDir, index string, toEncrypt bool) (hash string, err error) { 231 fs := NewFileSystem(a) 232 hash, err = fs.Upload(uploadDir, index, toEncrypt) 233 return hash, err 234 } 235 236 // Retrieve FileStore reader API 237 func (a *API) Retrieve(ctx context.Context, addr storage.Address) (reader storage.LazySectionReader, isEncrypted bool) { 238 return a.fileStore.Retrieve(ctx, addr) 239 } 240 241 // Store wraps the Store API call of the embedded FileStore 242 func (a *API) Store(ctx context.Context, data io.Reader, size int64, toEncrypt bool) (addr storage.Address, wait func(ctx context.Context) error, err error) { 243 log.Debug("api.store", "size", size) 244 return a.fileStore.Store(ctx, data, size, toEncrypt) 245 } 246 247 // ErrResolve is returned when an URI cannot be resolved from ENS. 248 type ErrResolve error 249 250 // Resolve resolves a URI to an Address using the MultiResolver. 251 func (a *API) Resolve(ctx context.Context, uri *URI) (storage.Address, error) { 252 apiResolveCount.Inc(1) 253 log.Trace("resolving", "uri", uri.Addr) 254 255 // if the URI is immutable, check if the address looks like a hash 256 if uri.Immutable() { 257 key := uri.Address() 258 if key == nil { 259 return nil, fmt.Errorf("immutable address not a content hash: %q", uri.Addr) 260 } 261 return key, nil 262 } 263 264 // if DNS is not configured, check if the address is a hash 265 if a.dns == nil { 266 key := uri.Address() 267 if key == nil { 268 apiResolveFail.Inc(1) 269 return nil, fmt.Errorf("no DNS to resolve name: %q", uri.Addr) 270 } 271 return key, nil 272 } 273 274 // try and resolve the address 275 resolved, err := a.dns.Resolve(uri.Addr) 276 if err == nil { 277 return resolved[:], nil 278 } 279 280 key := uri.Address() 281 if key == nil { 282 apiResolveFail.Inc(1) 283 return nil, err 284 } 285 return key, nil 286 } 287 288 // Put provides singleton manifest creation on top of FileStore store 289 func (a *API) Put(ctx context.Context, content string, contentType string, toEncrypt bool) (k storage.Address, wait func(context.Context) error, err error) { 290 apiPutCount.Inc(1) 291 r := strings.NewReader(content) 292 key, waitContent, err := a.fileStore.Store(ctx, r, int64(len(content)), toEncrypt) 293 if err != nil { 294 apiPutFail.Inc(1) 295 return nil, nil, err 296 } 297 manifest := fmt.Sprintf(`{"entries":[{"hash":"%v","contentType":"%s"}]}`, key, contentType) 298 r = strings.NewReader(manifest) 299 key, waitManifest, err := a.fileStore.Store(ctx, r, int64(len(manifest)), toEncrypt) 300 if err != nil { 301 apiPutFail.Inc(1) 302 return nil, nil, err 303 } 304 return key, func(ctx context.Context) error { 305 err := waitContent(ctx) 306 if err != nil { 307 return err 308 } 309 return waitManifest(ctx) 310 }, nil 311 } 312 313 // Get uses iterative manifest retrieval and prefix matching 314 // to resolve basePath to content using FileStore retrieve 315 // it returns a section reader, mimeType, status, the key of the actual content and an error 316 func (a *API) Get(ctx context.Context, manifestAddr storage.Address, path string) (reader storage.LazySectionReader, mimeType string, status int, contentAddr storage.Address, err error) { 317 log.Debug("api.get", "key", manifestAddr, "path", path) 318 apiGetCount.Inc(1) 319 trie, err := loadManifest(ctx, a.fileStore, manifestAddr, nil) 320 if err != nil { 321 apiGetNotFound.Inc(1) 322 status = http.StatusNotFound 323 log.Warn(fmt.Sprintf("loadManifestTrie error: %v", err)) 324 return 325 } 326 327 log.Debug("trie getting entry", "key", manifestAddr, "path", path) 328 entry, _ := trie.getEntry(path) 329 330 if entry != nil { 331 log.Debug("trie got entry", "key", manifestAddr, "path", path, "entry.Hash", entry.Hash) 332 // we need to do some extra work if this is a mutable resource manifest 333 if entry.ContentType == ResourceContentType { 334 335 // get the resource root chunk key 336 log.Trace("resource type", "key", manifestAddr, "hash", entry.Hash) 337 ctx, cancel := context.WithCancel(context.Background()) 338 defer cancel() 339 rsrc, err := a.resource.Load(storage.Address(common.FromHex(entry.Hash))) 340 if err != nil { 341 apiGetNotFound.Inc(1) 342 status = http.StatusNotFound 343 log.Debug(fmt.Sprintf("get resource content error: %v", err)) 344 return reader, mimeType, status, nil, err 345 } 346 347 // use this key to retrieve the latest update 348 rsrc, err = a.resource.LookupLatest(ctx, rsrc.NameHash(), true, &mru.LookupParams{}) 349 if err != nil { 350 apiGetNotFound.Inc(1) 351 status = http.StatusNotFound 352 log.Debug(fmt.Sprintf("get resource content error: %v", err)) 353 return reader, mimeType, status, nil, err 354 } 355 356 // if it's multihash, we will transparently serve the content this multihash points to 357 // \TODO this resolve is rather expensive all in all, review to see if it can be achieved cheaper 358 if rsrc.Multihash { 359 360 // get the data of the update 361 _, rsrcData, err := a.resource.GetContent(rsrc.NameHash().Hex()) 362 if err != nil { 363 apiGetNotFound.Inc(1) 364 status = http.StatusNotFound 365 log.Warn(fmt.Sprintf("get resource content error: %v", err)) 366 return reader, mimeType, status, nil, err 367 } 368 369 // validate that data as multihash 370 decodedMultihash, err := multihash.FromMultihash(rsrcData) 371 if err != nil { 372 apiGetInvalid.Inc(1) 373 status = http.StatusUnprocessableEntity 374 log.Warn("invalid resource multihash", "err", err) 375 return reader, mimeType, status, nil, err 376 } 377 manifestAddr = storage.Address(decodedMultihash) 378 log.Trace("resource is multihash", "key", manifestAddr) 379 380 // get the manifest the multihash digest points to 381 trie, err := loadManifest(ctx, a.fileStore, manifestAddr, nil) 382 if err != nil { 383 apiGetNotFound.Inc(1) 384 status = http.StatusNotFound 385 log.Warn(fmt.Sprintf("loadManifestTrie (resource multihash) error: %v", err)) 386 return reader, mimeType, status, nil, err 387 } 388 389 // finally, get the manifest entry 390 // it will always be the entry on path "" 391 entry, _ = trie.getEntry(path) 392 if entry == nil { 393 status = http.StatusNotFound 394 apiGetNotFound.Inc(1) 395 err = fmt.Errorf("manifest (resource multihash) entry for '%s' not found", path) 396 log.Trace("manifest (resource multihash) entry not found", "key", manifestAddr, "path", path) 397 return reader, mimeType, status, nil, err 398 } 399 400 } else { 401 // data is returned verbatim since it's not a multihash 402 return rsrc, "application/octet-stream", http.StatusOK, nil, nil 403 } 404 } 405 406 // regardless of resource update manifests or normal manifests we will converge at this point 407 // get the key the manifest entry points to and serve it if it's unambiguous 408 contentAddr = common.Hex2Bytes(entry.Hash) 409 status = entry.Status 410 if status == http.StatusMultipleChoices { 411 apiGetHTTP300.Inc(1) 412 return nil, entry.ContentType, status, contentAddr, err 413 } 414 mimeType = entry.ContentType 415 log.Debug("content lookup key", "key", contentAddr, "mimetype", mimeType) 416 reader, _ = a.fileStore.Retrieve(ctx, contentAddr) 417 } else { 418 // no entry found 419 status = http.StatusNotFound 420 apiGetNotFound.Inc(1) 421 err = fmt.Errorf("manifest entry for '%s' not found", path) 422 log.Trace("manifest entry not found", "key", contentAddr, "path", path) 423 } 424 return 425 } 426 427 // Modify loads manifest and checks the content hash before recalculating and storing the manifest. 428 func (a *API) Modify(ctx context.Context, addr storage.Address, path, contentHash, contentType string) (storage.Address, error) { 429 apiModifyCount.Inc(1) 430 quitC := make(chan bool) 431 trie, err := loadManifest(ctx, a.fileStore, addr, quitC) 432 if err != nil { 433 apiModifyFail.Inc(1) 434 return nil, err 435 } 436 if contentHash != "" { 437 entry := newManifestTrieEntry(&ManifestEntry{ 438 Path: path, 439 ContentType: contentType, 440 }, nil) 441 entry.Hash = contentHash 442 trie.addEntry(entry, quitC) 443 } else { 444 trie.deleteEntry(path, quitC) 445 } 446 447 if err := trie.recalcAndStore(); err != nil { 448 apiModifyFail.Inc(1) 449 return nil, err 450 } 451 return trie.ref, nil 452 } 453 454 // AddFile creates a new manifest entry, adds it to swarm, then adds a file to swarm. 455 func (a *API) AddFile(ctx context.Context, mhash, path, fname string, content []byte, nameresolver bool) (storage.Address, string, error) { 456 apiAddFileCount.Inc(1) 457 458 uri, err := Parse("bzz:/" + mhash) 459 if err != nil { 460 apiAddFileFail.Inc(1) 461 return nil, "", err 462 } 463 mkey, err := a.Resolve(ctx, uri) 464 if err != nil { 465 apiAddFileFail.Inc(1) 466 return nil, "", err 467 } 468 469 // trim the root dir we added 470 if path[:1] == "/" { 471 path = path[1:] 472 } 473 474 entry := &ManifestEntry{ 475 Path: filepath.Join(path, fname), 476 ContentType: mime.TypeByExtension(filepath.Ext(fname)), 477 Mode: 0700, 478 Size: int64(len(content)), 479 ModTime: time.Now(), 480 } 481 482 mw, err := a.NewManifestWriter(ctx, mkey, nil) 483 if err != nil { 484 apiAddFileFail.Inc(1) 485 return nil, "", err 486 } 487 488 fkey, err := mw.AddEntry(ctx, bytes.NewReader(content), entry) 489 if err != nil { 490 apiAddFileFail.Inc(1) 491 return nil, "", err 492 } 493 494 newMkey, err := mw.Store() 495 if err != nil { 496 apiAddFileFail.Inc(1) 497 return nil, "", err 498 499 } 500 501 return fkey, newMkey.String(), nil 502 } 503 504 // RemoveFile removes a file entry in a manifest. 505 func (a *API) RemoveFile(ctx context.Context, mhash string, path string, fname string, nameresolver bool) (string, error) { 506 apiRmFileCount.Inc(1) 507 508 uri, err := Parse("bzz:/" + mhash) 509 if err != nil { 510 apiRmFileFail.Inc(1) 511 return "", err 512 } 513 mkey, err := a.Resolve(ctx, uri) 514 if err != nil { 515 apiRmFileFail.Inc(1) 516 return "", err 517 } 518 519 // trim the root dir we added 520 if path[:1] == "/" { 521 path = path[1:] 522 } 523 524 mw, err := a.NewManifestWriter(ctx, mkey, nil) 525 if err != nil { 526 apiRmFileFail.Inc(1) 527 return "", err 528 } 529 530 err = mw.RemoveEntry(filepath.Join(path, fname)) 531 if err != nil { 532 apiRmFileFail.Inc(1) 533 return "", err 534 } 535 536 newMkey, err := mw.Store() 537 if err != nil { 538 apiRmFileFail.Inc(1) 539 return "", err 540 541 } 542 543 return newMkey.String(), nil 544 } 545 546 // AppendFile removes old manifest, appends file entry to new manifest and adds it to Swarm. 547 func (a *API) AppendFile(ctx context.Context, mhash, path, fname string, existingSize int64, content []byte, oldAddr storage.Address, offset int64, addSize int64, nameresolver bool) (storage.Address, string, error) { 548 apiAppendFileCount.Inc(1) 549 550 buffSize := offset + addSize 551 if buffSize < existingSize { 552 buffSize = existingSize 553 } 554 555 buf := make([]byte, buffSize) 556 557 oldReader, _ := a.Retrieve(ctx, oldAddr) 558 io.ReadAtLeast(oldReader, buf, int(offset)) 559 560 newReader := bytes.NewReader(content) 561 io.ReadAtLeast(newReader, buf[offset:], int(addSize)) 562 563 if buffSize < existingSize { 564 io.ReadAtLeast(oldReader, buf[addSize:], int(buffSize)) 565 } 566 567 combinedReader := bytes.NewReader(buf) 568 totalSize := int64(len(buf)) 569 570 // TODO(jmozah): to append using pyramid chunker when it is ready 571 //oldReader := a.Retrieve(oldKey) 572 //newReader := bytes.NewReader(content) 573 //combinedReader := io.MultiReader(oldReader, newReader) 574 575 uri, err := Parse("bzz:/" + mhash) 576 if err != nil { 577 apiAppendFileFail.Inc(1) 578 return nil, "", err 579 } 580 mkey, err := a.Resolve(ctx, uri) 581 if err != nil { 582 apiAppendFileFail.Inc(1) 583 return nil, "", err 584 } 585 586 // trim the root dir we added 587 if path[:1] == "/" { 588 path = path[1:] 589 } 590 591 mw, err := a.NewManifestWriter(ctx, mkey, nil) 592 if err != nil { 593 apiAppendFileFail.Inc(1) 594 return nil, "", err 595 } 596 597 err = mw.RemoveEntry(filepath.Join(path, fname)) 598 if err != nil { 599 apiAppendFileFail.Inc(1) 600 return nil, "", err 601 } 602 603 entry := &ManifestEntry{ 604 Path: filepath.Join(path, fname), 605 ContentType: mime.TypeByExtension(filepath.Ext(fname)), 606 Mode: 0700, 607 Size: totalSize, 608 ModTime: time.Now(), 609 } 610 611 fkey, err := mw.AddEntry(ctx, io.Reader(combinedReader), entry) 612 if err != nil { 613 apiAppendFileFail.Inc(1) 614 return nil, "", err 615 } 616 617 newMkey, err := mw.Store() 618 if err != nil { 619 apiAppendFileFail.Inc(1) 620 return nil, "", err 621 622 } 623 624 return fkey, newMkey.String(), nil 625 } 626 627 // BuildDirectoryTree used by swarmfs_unix 628 func (a *API) BuildDirectoryTree(ctx context.Context, mhash string, nameresolver bool) (addr storage.Address, manifestEntryMap map[string]*manifestTrieEntry, err error) { 629 630 uri, err := Parse("bzz:/" + mhash) 631 if err != nil { 632 return nil, nil, err 633 } 634 addr, err = a.Resolve(ctx, uri) 635 if err != nil { 636 return nil, nil, err 637 } 638 639 quitC := make(chan bool) 640 rootTrie, err := loadManifest(ctx, a.fileStore, addr, quitC) 641 if err != nil { 642 return nil, nil, fmt.Errorf("can't load manifest %v: %v", addr.String(), err) 643 } 644 645 manifestEntryMap = map[string]*manifestTrieEntry{} 646 err = rootTrie.listWithPrefix(uri.Path, quitC, func(entry *manifestTrieEntry, suffix string) { 647 manifestEntryMap[suffix] = entry 648 }) 649 650 if err != nil { 651 return nil, nil, fmt.Errorf("list with prefix failed %v: %v", addr.String(), err) 652 } 653 return addr, manifestEntryMap, nil 654 } 655 656 // ResourceLookup Looks up mutable resource updates at specific periods and versions 657 func (a *API) ResourceLookup(ctx context.Context, addr storage.Address, period uint32, version uint32, maxLookup *mru.LookupParams) (string, []byte, error) { 658 var err error 659 rsrc, err := a.resource.Load(addr) 660 if err != nil { 661 return "", nil, err 662 } 663 if version != 0 { 664 if period == 0 { 665 return "", nil, mru.NewError(mru.ErrInvalidValue, "Period can't be 0") 666 } 667 _, err = a.resource.LookupVersion(ctx, rsrc.NameHash(), period, version, true, maxLookup) 668 } else if period != 0 { 669 _, err = a.resource.LookupHistorical(ctx, rsrc.NameHash(), period, true, maxLookup) 670 } else { 671 _, err = a.resource.LookupLatest(ctx, rsrc.NameHash(), true, maxLookup) 672 } 673 if err != nil { 674 return "", nil, err 675 } 676 var data []byte 677 _, data, err = a.resource.GetContent(rsrc.NameHash().Hex()) 678 if err != nil { 679 return "", nil, err 680 } 681 return rsrc.Name(), data, nil 682 } 683 684 // ResourceCreate creates Resource and returns its key 685 func (a *API) ResourceCreate(ctx context.Context, name string, frequency uint64) (storage.Address, error) { 686 key, _, err := a.resource.New(ctx, name, frequency) 687 if err != nil { 688 return nil, err 689 } 690 return key, nil 691 } 692 693 // ResourceUpdateMultihash updates a Mutable Resource and marks the update's content to be of multihash type, which will be recognized upon retrieval. 694 // It will fail if the data is not a valid multihash. 695 func (a *API) ResourceUpdateMultihash(ctx context.Context, name string, data []byte) (storage.Address, uint32, uint32, error) { 696 return a.resourceUpdate(ctx, name, data, true) 697 } 698 699 // ResourceUpdate updates a Mutable Resource with arbitrary data. 700 // Upon retrieval the update will be retrieved verbatim as bytes. 701 func (a *API) ResourceUpdate(ctx context.Context, name string, data []byte) (storage.Address, uint32, uint32, error) { 702 return a.resourceUpdate(ctx, name, data, false) 703 } 704 705 func (a *API) resourceUpdate(ctx context.Context, name string, data []byte, multihash bool) (storage.Address, uint32, uint32, error) { 706 var addr storage.Address 707 var err error 708 if multihash { 709 addr, err = a.resource.UpdateMultihash(ctx, name, data) 710 } else { 711 addr, err = a.resource.Update(ctx, name, data) 712 } 713 period, _ := a.resource.GetLastPeriod(name) 714 version, _ := a.resource.GetVersion(name) 715 return addr, period, version, err 716 } 717 718 // ResourceHashSize returned the size of the digest produced by the Mutable Resource hashing function 719 func (a *API) ResourceHashSize() int { 720 return a.resource.HashSize 721 } 722 723 // ResourceIsValidated checks if the Mutable Resource has an active content validator. 724 func (a *API) ResourceIsValidated() bool { 725 return a.resource.IsValidated() 726 } 727 728 // ResolveResourceManifest retrieves the Mutable Resource manifest for the given address, and returns the address of the metadata chunk. 729 func (a *API) ResolveResourceManifest(ctx context.Context, addr storage.Address) (storage.Address, error) { 730 trie, err := loadManifest(ctx, a.fileStore, addr, nil) 731 if err != nil { 732 return nil, fmt.Errorf("cannot load resource manifest: %v", err) 733 } 734 735 entry, _ := trie.getEntry("") 736 if entry.ContentType != ResourceContentType { 737 return nil, fmt.Errorf("not a resource manifest: %s", addr) 738 } 739 740 return storage.Address(common.FromHex(entry.Hash)), nil 741 }