github.com/storacha/go-ucanto@v0.7.2/ucan/crypto/signature/signature.go (about)

     1  package signature
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/multiformats/go-varint"
     8  )
     9  
    10  const NON_STANDARD = 0xd000
    11  const ES256K = 0xd0e7
    12  const BLS12381G1 = 0xd0ea
    13  const BLS12381G2 = 0xd0eb
    14  const EdDSA = 0xd0ed
    15  const ES256 = 0xd01200
    16  const ES384 = 0xd01201
    17  const ES512 = 0xd01202
    18  const RS256 = 0xd01205
    19  const EIP191 = 0xd191
    20  
    21  func CodeName(code uint64) (string, error) {
    22  	switch code {
    23  	case ES256K:
    24  		return "ES256K", nil
    25  	case BLS12381G1:
    26  		return "BLS12381G1", nil
    27  	case BLS12381G2:
    28  		return "BLS12381G2", nil
    29  	case EdDSA:
    30  		return "EdDSA", nil
    31  	case ES256:
    32  		return "ES256", nil
    33  	case ES384:
    34  		return "ES384", nil
    35  	case ES512:
    36  		return "ES512", nil
    37  	case RS256:
    38  		return "RS256", nil
    39  	case EIP191:
    40  		return "EIP191", nil
    41  	default:
    42  		return "", fmt.Errorf("unknown signature algorithm code 0x%x", code)
    43  	}
    44  }
    45  
    46  func NameCode(name string) (uint64, error) {
    47  	switch name {
    48  	case "ES256K":
    49  		return ES256K, nil
    50  	case "BLS12381G1":
    51  		return BLS12381G1, nil
    52  	case "BLS12381G2":
    53  		return BLS12381G2, nil
    54  	case "EdDSA":
    55  		return EdDSA, nil
    56  	case "ES256":
    57  		return ES256, nil
    58  	case "ES384":
    59  		return ES384, nil
    60  	case "ES512":
    61  		return ES512, nil
    62  	case "RS256":
    63  		return RS256, nil
    64  	case "EIP191":
    65  		return EIP191, nil
    66  	default:
    67  		return NON_STANDARD, nil
    68  	}
    69  }
    70  
    71  type Signature interface {
    72  	Code() uint64
    73  	Size() uint64
    74  	Bytes() []byte
    75  	// Raw signature (without signature algorithm info).
    76  	Raw() []byte
    77  }
    78  
    79  func NewSignature(code uint64, raw []byte) Signature {
    80  	cl := varint.UvarintSize(code)
    81  	rl := varint.UvarintSize(uint64(len(raw)))
    82  	sig := make(signature, cl+rl+len(raw))
    83  	varint.PutUvarint(sig, code)
    84  	varint.PutUvarint(sig[cl:], uint64(len(raw)))
    85  	copy(sig[cl+rl:], raw)
    86  	return sig
    87  }
    88  
    89  func NewNonStandard(name string, raw []byte) Signature {
    90  	code := uint64(NON_STANDARD)
    91  	cl := varint.UvarintSize(code)
    92  	rl := varint.UvarintSize(uint64(len(raw)))
    93  	sig := make(signature, cl+rl+len(raw)+len(name))
    94  	varint.PutUvarint(sig, code)
    95  	varint.PutUvarint(sig[cl:], uint64(len(raw)))
    96  	copy(sig[cl+rl:], raw)
    97  	copy(sig[cl+rl+len(raw):], name)
    98  	return sig
    99  }
   100  
   101  func Encode(s Signature) []byte {
   102  	return s.Bytes()
   103  }
   104  
   105  func Decode(b []byte) Signature {
   106  	return signature(b)
   107  }
   108  
   109  type signature []byte
   110  
   111  func (s signature) Code() uint64 {
   112  	c, _ := varint.ReadUvarint(bytes.NewReader(s))
   113  	return c
   114  }
   115  
   116  func (s signature) Size() uint64 {
   117  	n, _ := varint.ReadUvarint(bytes.NewReader(s[varint.UvarintSize(s.Code()):]))
   118  	return n
   119  }
   120  
   121  func (s signature) Raw() []byte {
   122  	cl := varint.UvarintSize(s.Code())
   123  	rl := varint.UvarintSize(s.Size())
   124  	return s[cl+rl:]
   125  }
   126  
   127  func (s signature) Bytes() []byte {
   128  	return s
   129  }
   130  
   131  type SignatureView interface {
   132  	Signature
   133  	// Verify that the signature was produced by the given message.
   134  	Verify(msg []byte, signer Verifier) bool
   135  }
   136  
   137  func NewSignatureView(s Signature) SignatureView {
   138  	return signatureView(signature(s.Bytes()))
   139  }
   140  
   141  type signatureView signature
   142  
   143  func (v signatureView) Bytes() []byte {
   144  	return signature(v).Bytes()
   145  }
   146  
   147  func (v signatureView) Code() uint64 {
   148  	return signature(v).Code()
   149  }
   150  
   151  func (v signatureView) Raw() []byte {
   152  	return signature(v).Raw()
   153  }
   154  
   155  func (v signatureView) Size() uint64 {
   156  	return signature(v).Size()
   157  }
   158  
   159  func (v signatureView) Verify(msg []byte, signer Verifier) bool {
   160  	return signer.Verify(msg, v)
   161  }