github.com/storacha/go-ucanto@v0.7.2/core/delegation/proofs.go (about)

     1  package delegation
     2  
     3  import (
     4  	"github.com/storacha/go-ucanto/core/dag/blockstore"
     5  	"github.com/storacha/go-ucanto/core/ipld"
     6  	"github.com/storacha/go-ucanto/ucan"
     7  )
     8  
     9  type Proof struct {
    10  	delegation Delegation
    11  	link       ucan.Link
    12  }
    13  
    14  func (p Proof) Delegation() (Delegation, bool) {
    15  	return p.delegation, p.delegation != nil
    16  }
    17  
    18  func (p Proof) Link() ucan.Link {
    19  	if p.delegation != nil {
    20  		return p.delegation.Link()
    21  	}
    22  	return p.link
    23  }
    24  
    25  func FromDelegation(delegation Delegation) Proof {
    26  	return Proof{delegation, nil}
    27  }
    28  
    29  func FromLink(link ucan.Link) Proof {
    30  	return Proof{nil, link}
    31  }
    32  
    33  type Proofs []Proof
    34  
    35  func NewProofsView(links []ipld.Link, bs blockstore.BlockReader) Proofs {
    36  	proofs := make(Proofs, 0, len(links))
    37  	for _, link := range links {
    38  		if delegation, err := NewDelegationView(link, bs); err == nil {
    39  			proofs = append(proofs, FromDelegation(delegation))
    40  		} else {
    41  			proofs = append(proofs, FromLink(link))
    42  		}
    43  	}
    44  	return proofs
    45  }
    46  
    47  // WriteInto writes a set of proofs, some of which may be full delegations to a blockstore
    48  func (proofs Proofs) WriteInto(bs blockstore.BlockWriter) ([]ipld.Link, error) {
    49  	links := make([]ucan.Link, 0, len(proofs))
    50  	for _, p := range proofs {
    51  		links = append(links, p.Link())
    52  		if delegation, isDelegation := p.Delegation(); isDelegation {
    53  			err := blockstore.WriteInto(delegation, bs)
    54  			if err != nil {
    55  				return nil, err
    56  			}
    57  		}
    58  	}
    59  	return links, nil
    60  }