github.com/storacha/go-ucanto@v0.7.2/principal/verifier/verifier.go (about)

     1  package verifier
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/storacha/go-ucanto/did"
     8  	"github.com/storacha/go-ucanto/principal"
     9  	"github.com/storacha/go-ucanto/ucan/crypto/signature"
    10  )
    11  
    12  type Unwrapper interface {
    13  	// Unwrap returns the unwrapped did:key of this signer.
    14  	Unwrap() principal.Verifier
    15  }
    16  
    17  type WrappedVerifier interface {
    18  	principal.Verifier
    19  	Unwrapper
    20  }
    21  
    22  type wrapvf struct {
    23  	id  did.DID
    24  	key principal.Verifier
    25  }
    26  
    27  func (w wrapvf) Code() uint64 {
    28  	return w.key.Code()
    29  }
    30  
    31  func (w wrapvf) DID() did.DID {
    32  	return w.id
    33  }
    34  
    35  func (w wrapvf) Encode() []byte {
    36  	return w.key.Encode()
    37  }
    38  
    39  func (w wrapvf) Raw() []byte {
    40  	return w.key.Raw()
    41  }
    42  
    43  func (w wrapvf) Verify(msg []byte, sig signature.Signature) bool {
    44  	return w.key.Verify(msg, sig)
    45  }
    46  
    47  func (w wrapvf) Unwrap() principal.Verifier {
    48  	return w.key
    49  }
    50  
    51  // Wrap the key of this verifier into a verifier with a different DID. This is
    52  // primarily used to wrap a did:key verifier with a verifier that has a DID of
    53  // a different method.
    54  func Wrap(key principal.Verifier, id did.DID) (WrappedVerifier, error) {
    55  	if !strings.HasPrefix(key.DID().String(), "did:key:") {
    56  		return nil, fmt.Errorf("verifier is not a did:key")
    57  	}
    58  	return wrapvf{id, key}, nil
    59  }