github.com/amazechain/amc@v0.1.3/common/types/signature.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The AmazeChain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package types
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"github.com/amazechain/amc/common/hexutil"
    24  	"reflect"
    25  )
    26  
    27  const SignatureLength = 96
    28  const PublicKeyLength = 48
    29  
    30  var (
    31  	signatureT = reflect.TypeOf(Signature{})
    32  	publicKeyT = reflect.TypeOf(PublicKey{})
    33  )
    34  
    35  type Signature [SignatureLength]byte
    36  
    37  func (h Signature) Hex() string { return hexutil.Encode(h[:]) }
    38  func (h Signature) String() string {
    39  	return h.Hex()
    40  }
    41  
    42  func (h Signature) Size() int {
    43  	return SignatureLength
    44  }
    45  
    46  func (h Signature) Bytes() []byte { return h[:] }
    47  
    48  func (h *Signature) SetBytes(data []byte) error {
    49  	if len(data) != SignatureLength {
    50  		return fmt.Errorf("invalid bytes len %d", len(data))
    51  	}
    52  	copy(h[:], data[:SignatureLength])
    53  	return nil
    54  }
    55  
    56  func (h Signature) Marshal() ([]byte, error) {
    57  	return h.Bytes(), nil
    58  }
    59  
    60  func (h *Signature) Unmarshal(data []byte) error {
    61  	return h.SetBytes(data)
    62  }
    63  
    64  // Hash supports the %v, %s, %q, %x, %X and %d format verbs.
    65  func (h Signature) Format(s fmt.State, c rune) {
    66  	hexb := make([]byte, 2+len(h)*2)
    67  	copy(hexb, "0x")
    68  	hex.Encode(hexb[2:], h[:])
    69  
    70  	switch c {
    71  	case 'x', 'X':
    72  		if !s.Flag('#') {
    73  			hexb = hexb[2:]
    74  		}
    75  		if c == 'X' {
    76  			hexb = bytes.ToUpper(hexb)
    77  		}
    78  		fallthrough
    79  	case 'v', 's':
    80  		s.Write(hexb)
    81  	case 'q':
    82  		q := []byte{'"'}
    83  		s.Write(q)
    84  		s.Write(hexb)
    85  		s.Write(q)
    86  	case 'd':
    87  		fmt.Fprint(s, ([len(h)]byte)(h))
    88  	default:
    89  		fmt.Fprintf(s, "%%!%c(signature=%x)", c, h)
    90  	}
    91  }
    92  
    93  // UnmarshalText parses a hash in hex syntax.
    94  func (h *Signature) UnmarshalText(input []byte) error {
    95  	return hexutil.UnmarshalFixedText("Signature", input, h[:])
    96  }
    97  
    98  // UnmarshalJSON parses a hash in hex syntax.
    99  func (h *Signature) UnmarshalJSON(input []byte) error {
   100  	return hexutil.UnmarshalFixedJSON(signatureT, input, h[:])
   101  }
   102  
   103  // MarshalText returns the hex representation of h.
   104  func (h Signature) MarshalText() ([]byte, error) {
   105  	return hexutil.Bytes(h[:]).MarshalText()
   106  }
   107  
   108  type PublicKey [PublicKeyLength]byte
   109  
   110  func (h PublicKey) Hex() string { return hexutil.Encode(h[:]) }
   111  func (h PublicKey) String() string {
   112  	return h.Hex()
   113  }
   114  
   115  func (h PublicKey) Bytes() []byte {
   116  	return h[:]
   117  }
   118  
   119  func (h *PublicKey) SetBytes(b []byte) error {
   120  	if len(b) != PublicKeyLength {
   121  		return fmt.Errorf("invalid bytes len %d", len(b))
   122  	}
   123  
   124  	copy(h[:], b[:PublicKeyLength])
   125  	return nil
   126  }
   127  
   128  func (h PublicKey) Size() int {
   129  	return PublicKeyLength
   130  }
   131  
   132  func (h PublicKey) Marshal() ([]byte, error) {
   133  	return h.Bytes(), nil
   134  }
   135  
   136  func (h *PublicKey) Unmarshal(data []byte) error {
   137  	return h.SetBytes(data)
   138  }
   139  
   140  // MarshalText returns the hex representation of a.
   141  func (a PublicKey) MarshalText() ([]byte, error) {
   142  	return hexutil.Bytes(a[:]).MarshalText()
   143  }
   144  
   145  // UnmarshalText parses a hash in hex syntax.
   146  func (a *PublicKey) UnmarshalText(input []byte) error {
   147  	return hexutil.UnmarshalFixedText("PublicKey", input, a[:])
   148  }
   149  
   150  // UnmarshalJSON parses a hash in hex syntax.
   151  func (a *PublicKey) UnmarshalJSON(input []byte) error {
   152  	return hexutil.UnmarshalFixedJSON(publicKeyT, input, a[:])
   153  }
   154  
   155  // Format implements fmt.Formatter.
   156  // supports the %v, %s, %q, %x, %X and %d format verbs.
   157  func (h PublicKey) Format(s fmt.State, c rune) {
   158  	hexb := make([]byte, 2+len(h)*2)
   159  	copy(hexb, "0x")
   160  	hex.Encode(hexb[2:], h[:])
   161  
   162  	switch c {
   163  	case 'x', 'X':
   164  		if !s.Flag('#') {
   165  			hexb = hexb[2:]
   166  		}
   167  		if c == 'X' {
   168  			hexb = bytes.ToUpper(hexb)
   169  		}
   170  		fallthrough
   171  	case 'v', 's':
   172  		s.Write(hexb)
   173  	case 'q':
   174  		q := []byte{'"'}
   175  		s.Write(q)
   176  		s.Write(hexb)
   177  		s.Write(q)
   178  	case 'd':
   179  		fmt.Fprint(s, ([len(h)]byte)(h))
   180  	default:
   181  		fmt.Fprintf(s, "%%!%c(publickey=%x)", c, h)
   182  	}
   183  }