github.com/koko1123/flow-go-1@v0.29.6/engine/access/rest/request/signatures.go (about)

     1  package request
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/koko1123/flow-go-1/engine/access/rest/models"
     7  	"github.com/koko1123/flow-go-1/engine/access/rest/util"
     8  	"github.com/koko1123/flow-go-1/model/flow"
     9  )
    10  
    11  type TransactionSignature flow.TransactionSignature
    12  
    13  func (s *TransactionSignature) Parse(
    14  	rawAddress string,
    15  	rawKeyIndex string,
    16  	rawSignature string,
    17  ) error {
    18  	var address Address
    19  	err := address.Parse(rawAddress)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	keyIndex, err := util.ToUint64(rawKeyIndex)
    25  	if err != nil {
    26  		return fmt.Errorf("invalid key index: %w", err)
    27  	}
    28  
    29  	var signature Signature
    30  	err = signature.Parse(rawSignature)
    31  	if err != nil {
    32  		return fmt.Errorf("invalid signature: %w", err)
    33  	}
    34  
    35  	*s = TransactionSignature(flow.TransactionSignature{
    36  		Address:   address.Flow(),
    37  		KeyIndex:  keyIndex,
    38  		Signature: signature,
    39  	})
    40  
    41  	return nil
    42  }
    43  
    44  func (s TransactionSignature) Flow() flow.TransactionSignature {
    45  	return flow.TransactionSignature(s)
    46  }
    47  
    48  type TransactionSignatures []TransactionSignature
    49  
    50  func (t *TransactionSignatures) Parse(rawSigs []models.TransactionSignature) error {
    51  	signatures := make([]TransactionSignature, len(rawSigs))
    52  	for i, sig := range rawSigs {
    53  		var signature TransactionSignature
    54  		err := signature.Parse(sig.Address, sig.KeyIndex, sig.Signature)
    55  		if err != nil {
    56  			return err
    57  		}
    58  		signatures[i] = signature
    59  	}
    60  
    61  	*t = signatures
    62  	return nil
    63  }
    64  
    65  func (t TransactionSignatures) Flow() []flow.TransactionSignature {
    66  	sigs := make([]flow.TransactionSignature, len(t))
    67  	for i, sig := range t {
    68  		sigs[i] = sig.Flow()
    69  	}
    70  	return sigs
    71  }
    72  
    73  type Signature []byte
    74  
    75  func (s *Signature) Parse(raw string) error {
    76  	if raw == "" {
    77  		return fmt.Errorf("missing value")
    78  	}
    79  
    80  	signatureBytes, err := util.FromBase64(raw)
    81  	if err != nil {
    82  		return fmt.Errorf("invalid encoding")
    83  	}
    84  
    85  	*s = signatureBytes
    86  	return nil
    87  }
    88  
    89  func (s Signature) Flow() []byte {
    90  	return s
    91  }