9 "github.com/bytom/bytom/crypto/ed25519/chainkd"
10 "github.com/bytom/bytom/crypto/sha3pool"
11 "github.com/bytom/bytom/protocol/bc"
14 var errVerifySignature = errors.New("signature of verification message is invalid")
16 // Verification represent a verification message for the block
17 // source hash and target hash point to the checkpoint, and the source checkpoint is the target checkpoint's parent(not be directly)
18 // the vector <sourceHash, targetHash, sourceHeight, targetHeight, pubKey> as the message of signature
19 type Verification struct {
28 // Sign used to sign the verification by specified xPrv
29 func (v *Verification) Sign(xPrv chainkd.XPrv) error {
30 message, err := v.encodeMessage()
35 v.Signature = xPrv.Sign(message)
39 // VerifySignature verify the signature of encode message of verification
40 func (v *Verification) VerifySignature() error {
41 pubKey, err := hex.DecodeString(v.PubKey)
46 message, err := v.encodeMessage()
53 if !xPub.Verify(message, v.Signature) {
54 return errVerifySignature
60 // encodeMessage encode the verification for the validators to sign or verify
61 func (v *Verification) encodeMessage() ([]byte, error) {
62 buff := new(bytes.Buffer)
63 if _, err := v.SourceHash.WriteTo(buff); err != nil {
67 if _, err := v.TargetHash.WriteTo(buff); err != nil {
71 uint64Buff := make([]byte, 8)
73 binary.LittleEndian.PutUint64(uint64Buff, v.SourceHeight)
74 if _, err := buff.Write(uint64Buff); err != nil {
78 binary.LittleEndian.PutUint64(uint64Buff, v.TargetHeight)
79 if _, err := buff.Write(uint64Buff); err != nil {
83 return sha3Hash(buff.Bytes())
86 func sha3Hash(message []byte) ([]byte, error) {
87 sha3 := sha3pool.Get256()
88 defer sha3pool.Put256(sha3)
90 if _, err := sha3.Write(message); err != nil {
95 if _, err := hash.ReadFrom(sha3); err != nil {
99 return hash.Bytes(), nil