github.com/annchain/OG@v0.0.9/og_interface/hash.go (about)

     1  package og_interface
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  
     7  	"github.com/annchain/commongo/marshaller"
     8  )
     9  
    10  const (
    11  	FlagHashNone byte = iota
    12  	FlagHash32
    13  )
    14  
    15  func MarshalHash(hash Hash) ([]byte, error) {
    16  	return hash.MarshalMsg()
    17  }
    18  
    19  func UnmarshalHash(b []byte) (Hash, []byte, error) {
    20  	var h Hash
    21  
    22  	// get lead
    23  	if len(b) == 0 {
    24  		return nil, nil, marshaller.ErrorNotEnoughBytes(1, 0)
    25  	}
    26  	lead := b[0]
    27  	switch lead {
    28  	case FlagHash32:
    29  		h = &Hash32{}
    30  	default:
    31  		return nil, nil, fmt.Errorf("unknown Hash lead: %x", lead)
    32  	}
    33  
    34  	b, err := h.UnmarshalMsg(b)
    35  	return h, b, err
    36  }
    37  
    38  func BigToHash(b *big.Int, hashFlag byte) Hash {
    39  	switch hashFlag {
    40  	case FlagHash32:
    41  		return BigToHash32(b)
    42  	default:
    43  		return BigToHash32(b)
    44  	}
    45  }