github.com/chain5j/chain5j-pkg@v1.0.7/crypto/signature/types.go (about)

     1  // Package signature
     2  //
     3  // @author: xwc1125
     4  package signature
     5  
     6  import (
     7  	"github.com/chain5j/chain5j-pkg/codec"
     8  	"github.com/chain5j/chain5j-pkg/util/hexutil"
     9  )
    10  
    11  // SignResult 签名结果
    12  type SignResult struct {
    13  	Name      string        `json:"name" mapstructure:"name" validate:"required"`           // 算法名称
    14  	PubKey    hexutil.Bytes `json:"pubKey,omitempty" mapstructure:"pubKey"`                 // 公钥
    15  	Signature hexutil.Bytes `json:"signature" mapstructure:"signature" validate:"required"` // 签名结果
    16  }
    17  
    18  // Serialize 获取signResult的bytes
    19  func (s *SignResult) Serialize() ([]byte, error) {
    20  	return codec.Codecor().Encode(s)
    21  }
    22  
    23  // Deserialize 反解码
    24  func (s *SignResult) Deserialize(data []byte) error {
    25  	var sign SignResult
    26  	err := codec.Codecor().Decode(data, &sign)
    27  	if err != nil {
    28  		return err
    29  	}
    30  	*s = sign
    31  	return nil
    32  }
    33  
    34  func (s *SignResult) Copy() *SignResult {
    35  	cpy := SignResult{
    36  		Name: s.Name,
    37  	}
    38  	if !s.PubKey.Nil() {
    39  		cpy.PubKey = make([]byte, len(s.PubKey))
    40  		copy(cpy.PubKey, s.PubKey)
    41  	}
    42  	if len(s.Signature) > 0 {
    43  		cpy.Signature = make([]byte, len(s.Signature))
    44  		copy(cpy.Signature, s.Signature)
    45  	}
    46  	return &cpy
    47  }