-
Notifications
You must be signed in to change notification settings - Fork 39
/
signedbase.go
77 lines (67 loc) · 1.52 KB
/
signedbase.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package consensus
import (
"github.com/icon-project/goloop/common"
"github.com/icon-project/goloop/common/crypto"
"github.com/icon-project/goloop/common/errors"
"github.com/icon-project/goloop/module"
)
type byteser interface {
bytes() []byte
}
// base class for signed data
type signedBase struct {
// shall be initialized
_byteser byteser
Signature common.Signature
_hash []byte
_publicKey *crypto.PublicKey
}
func (s *signedBase) hash() []byte {
if s._hash == nil {
bs := s._byteser.bytes()
s._hash = crypto.SHA3Sum256(bs)
}
return s._hash
}
func (s *signedBase) publicKey() *crypto.PublicKey {
if s._publicKey == nil {
publicKey, err := s.Signature.RecoverPublicKey(s.hash())
if err != nil {
return nil
}
s._publicKey = publicKey
}
return s._publicKey
}
func (s *signedBase) address() *common.Address {
publicKey := s.publicKey()
if publicKey == nil {
return nil
}
return common.NewAccountAddressFromPublicKey(publicKey)
}
func (s *signedBase) verify() error {
if s.publicKey() == nil {
return errors.New("bad signature")
}
return nil
}
func (s *signedBase) Sign(wallet module.Wallet) error {
s._hash = nil
s._publicKey = nil
sigBS, err := wallet.Sign(s.hash())
if err != nil {
return errors.Errorf("sendVote : %v", err)
}
sig, err := crypto.ParseSignature(sigBS)
if err != nil {
return errors.Errorf("sendVote : %v", err)
}
s.Signature.Signature = sig
return nil
}
func (s *signedBase) setSignature(sig common.Signature) {
s.Signature = sig
s._hash = nil
s._publicKey = nil
}