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