github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/module/local/me.go (about) 1 package local 2 3 import ( 4 "fmt" 5 6 "github.com/onflow/crypto" 7 "github.com/onflow/crypto/hash" 8 9 "github.com/onflow/flow-go/model/flow" 10 "github.com/onflow/flow-go/model/flow/filter" 11 ) 12 13 type Local struct { 14 me flow.IdentitySkeleton 15 sk crypto.PrivateKey // instance of the node's private staking key 16 } 17 18 func New(id flow.IdentitySkeleton, sk crypto.PrivateKey) (*Local, error) { 19 if !sk.PublicKey().Equals(id.StakingPubKey) { 20 return nil, fmt.Errorf("cannot initialize with mismatching keys, expect %v, but got %v", 21 id.StakingPubKey, sk.PublicKey()) 22 } 23 24 l := &Local{ 25 me: id, 26 sk: sk, 27 } 28 return l, nil 29 } 30 31 func (l *Local) NodeID() flow.Identifier { 32 return l.me.NodeID 33 } 34 35 func (l *Local) Address() string { 36 return l.me.Address 37 } 38 39 func (l *Local) Sign(msg []byte, hasher hash.Hasher) (crypto.Signature, error) { 40 return l.sk.Sign(msg, hasher) 41 } 42 43 func (l *Local) NotMeFilter() flow.IdentityFilter[flow.Identity] { 44 return filter.Not(filter.HasNodeID[flow.Identity](l.NodeID())) 45 } 46 47 // SignFunc provides a signature oracle that given a message, a hasher, and a signing function, it 48 // generates and returns a signature over the message using the node's private key 49 // as well as the input hasher by invoking the given signing function. The overall idea of this function 50 // is to not expose the private key to the caller. 51 func (l *Local) SignFunc(data []byte, hasher hash.Hasher, f func(crypto.PrivateKey, []byte, hash.Hasher) (crypto.Signature, 52 error)) (crypto.Signature, error) { 53 return f(l.sk, data, hasher) 54 }