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 = hex.EncodeToString(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 signature, err := hex.DecodeString(v.Signature)
51 message, err := v.encodeMessage()
58 if !xPub.Verify(message, signature) {
59 return errVerifySignature
65 // encodeMessage encode the verification for the validators to sign or verify
66 func (v *Verification) encodeMessage() ([]byte, error) {
67 buff := new(bytes.Buffer)
68 if _, err := v.SourceHash.WriteTo(buff); err != nil {
72 if _, err := v.TargetHash.WriteTo(buff); err != nil {
76 uint64Buff := make([]byte, 8)
78 binary.LittleEndian.PutUint64(uint64Buff, v.SourceHeight)
79 if _, err := buff.Write(uint64Buff); err != nil {
83 binary.LittleEndian.PutUint64(uint64Buff, v.TargetHeight)
84 if _, err := buff.Write(uint64Buff); err != nil {
88 return sha3Hash(buff.Bytes())
91 func sha3Hash(message []byte) ([]byte, error) {
92 sha3 := sha3pool.Get256()
93 defer sha3pool.Put256(sha3)
95 if _, err := sha3.Write(message); err != nil {
100 if _, err := hash.ReadFrom(sha3); err != nil {
104 return hash.Bytes(), nil