-
Notifications
You must be signed in to change notification settings - Fork 2
/
signer.go
40 lines (31 loc) · 1.09 KB
/
signer.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
package access_ticket
import (
"crypto/sha256"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/sourcenetwork/sourcehub/x/acp/types"
)
// signer is responsible for generating a Ticket signature and veryfing it
type signer struct{}
func (s *signer) Sign(key cryptotypes.PrivKey, ticket *types.AccessTicket) ([]byte, error) {
digest := s.hashTicket(ticket)
return key.Sign(digest)
}
func (s *signer) Verify(key cryptotypes.PubKey, ticket *types.AccessTicket) error {
digest := s.hashTicket(ticket)
ok := key.VerifySignature(digest, ticket.Signature)
if !ok {
return ErrInvalidSignature
}
return nil
}
// hashTicket produces a sha256 which uniquely identifies a Ticket.
func (b *signer) hashTicket(ticket *types.AccessTicket) []byte {
hasher := sha256.New()
hasher.Write([]byte(ticket.VersionDenominator))
// NOTE the Decision ID is produced as a Hash of all fields in it.
// It's paramount that this invariant is not violated to ensure the
// safety of the protocol
hasher.Write([]byte(ticket.DecisionId))
hasher.Write(ticket.DecisionProof)
return hasher.Sum(nil)
}