-
Notifications
You must be signed in to change notification settings - Fork 178
/
me.go
55 lines (44 loc) · 1.5 KB
/
me.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
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package local
import (
"fmt"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/crypto/hash"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/flow/filter"
)
type Local struct {
me *flow.Identity
sk crypto.PrivateKey // instance of the node's private staking key
}
func New(id *flow.Identity, sk crypto.PrivateKey) (*Local, error) {
if !sk.PublicKey().Equals(id.StakingPubKey) {
return nil, fmt.Errorf("cannot initialize with mismatching keys, expect %v, but got %v",
id.StakingPubKey, sk.PublicKey())
}
l := &Local{
me: id,
sk: sk,
}
return l, nil
}
func (l *Local) NodeID() flow.Identifier {
return l.me.NodeID
}
func (l *Local) Address() string {
return l.me.Address
}
func (l *Local) Sign(msg []byte, hasher hash.Hasher) (crypto.Signature, error) {
return l.sk.Sign(msg, hasher)
}
func (l *Local) NotMeFilter() flow.IdentityFilter {
return filter.Not(filter.HasNodeID(l.NodeID()))
}
// SignFunc provides a signature oracle that given a message, a hasher, and a signing function, it
// generates and returns a signature over the message using the node's private key
// as well as the input hasher by invoking the given signing function. The overall idea of this function
// is to not expose the private key to the caller.
func (l *Local) SignFunc(data []byte, hasher hash.Hasher, f func(crypto.PrivateKey, []byte, hash.Hasher) (crypto.Signature,
error)) (crypto.Signature, error) {
return f(l.sk, data, hasher)
}