github.com/MetalBlockchain/metalgo@v1.11.9/vms/secp256k1fx/credential.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package secp256k1fx
     5  
     6  import (
     7  	"encoding/json"
     8  	"errors"
     9  	"fmt"
    10  
    11  	"github.com/MetalBlockchain/metalgo/utils/crypto/secp256k1"
    12  	"github.com/MetalBlockchain/metalgo/utils/formatting"
    13  )
    14  
    15  var ErrNilCredential = errors.New("nil credential")
    16  
    17  type Credential struct {
    18  	Sigs [][secp256k1.SignatureLen]byte `serialize:"true" json:"signatures"`
    19  }
    20  
    21  // MarshalJSON marshals [cr] to JSON
    22  // The string representation of each signature is created using the hex formatter
    23  func (cr *Credential) MarshalJSON() ([]byte, error) {
    24  	signatures := make([]string, len(cr.Sigs))
    25  	for i, sig := range cr.Sigs {
    26  		sigStr, err := formatting.Encode(formatting.HexNC, sig[:])
    27  		if err != nil {
    28  			return nil, fmt.Errorf("couldn't convert signature to string: %w", err)
    29  		}
    30  		signatures[i] = sigStr
    31  	}
    32  	jsonFieldMap := map[string]interface{}{
    33  		"signatures": signatures,
    34  	}
    35  	return json.Marshal(jsonFieldMap)
    36  }
    37  
    38  func (cr *Credential) Verify() error {
    39  	switch {
    40  	case cr == nil:
    41  		return ErrNilCredential
    42  	default:
    43  		return nil
    44  	}
    45  }