github.com/ethersphere/bee/v2@v2.2.0/pkg/resolver/cidv1/cidv1.go (about)

     1  // Copyright 2022 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package cidv1
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/ethersphere/bee/v2/pkg/resolver"
    11  	"github.com/ethersphere/bee/v2/pkg/swarm"
    12  	"github.com/ipfs/go-cid"
    13  	"github.com/multiformats/go-multihash"
    14  )
    15  
    16  // https://github.com/multiformats/multicodec/blob/master/table.csv
    17  const (
    18  	SwarmNsCodec       uint64 = 0xe4
    19  	SwarmManifestCodec uint64 = 0xfa
    20  	SwarmFeedCodec     uint64 = 0xfb
    21  )
    22  
    23  type Resolver struct{}
    24  
    25  func (Resolver) Resolve(name string) (swarm.Address, error) {
    26  	id, err := cid.Parse(name)
    27  	if err != nil {
    28  		return swarm.ZeroAddress, fmt.Errorf("failed parsing CID %s err %w: %w", name, err, resolver.ErrParse)
    29  	}
    30  
    31  	switch id.Prefix().GetCodec() {
    32  	case SwarmNsCodec:
    33  	case SwarmManifestCodec:
    34  	case SwarmFeedCodec:
    35  	default:
    36  		return swarm.ZeroAddress, fmt.Errorf("unsupported codec for CID %d: %w", id.Prefix().GetCodec(), resolver.ErrParse)
    37  	}
    38  
    39  	dh, err := multihash.Decode(id.Hash())
    40  	if err != nil {
    41  		return swarm.ZeroAddress, fmt.Errorf("unable to decode hash %w: %w", err, resolver.ErrInvalidContentHash)
    42  	}
    43  
    44  	addr := swarm.NewAddress(dh.Digest)
    45  
    46  	return addr, nil
    47  }
    48  
    49  func (Resolver) Close() error {
    50  	return nil
    51  }