mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 22:26:42 +00:00
whisper: minor refactoring (renamed several functions)
This commit is contained in:
parent
5abb7463ad
commit
b28d860ab3
6 changed files with 33 additions and 51 deletions
|
|
@ -111,39 +111,39 @@ func (api *PublicWhisperAPI) NewIdentity() (string, error) {
|
|||
return common.ToHex(crypto.FromECDSAPub(&identity.PublicKey)), nil
|
||||
}
|
||||
|
||||
// GenerateTopicKey generates a random symmetric key and stores it under
|
||||
// GenerateSymKey generates a random symmetric key and stores it under
|
||||
// the 'name' id. Will be used in the future for session key exchange.
|
||||
func (api *PublicWhisperAPI) GenerateTopicKey(name string) error {
|
||||
func (api *PublicWhisperAPI) GenerateSymKey(name string) error {
|
||||
if api.whisper == nil {
|
||||
return whisperOffLineErr
|
||||
}
|
||||
return api.whisper.GenerateTopicKey(name)
|
||||
return api.whisper.GenerateSymKey(name)
|
||||
}
|
||||
|
||||
// AddTopicKey stores the key under the 'name' id.
|
||||
func (api *PublicWhisperAPI) AddTopicKey(name string, key []byte) error {
|
||||
// AddSymKey stores the key under the 'name' id.
|
||||
func (api *PublicWhisperAPI) AddSymKey(name string, key []byte) error {
|
||||
if api.whisper == nil {
|
||||
return whisperOffLineErr
|
||||
}
|
||||
return api.whisper.AddTopicKey(name, key)
|
||||
return api.whisper.AddSymKey(name, key)
|
||||
}
|
||||
|
||||
// HasTopicKey returns true if there is a key associated with the name string.
|
||||
// HasSymKey returns true if there is a key associated with the name string.
|
||||
// Otherwise returns false.
|
||||
func (api *PublicWhisperAPI) HasTopicKey(name string) (bool, error) {
|
||||
func (api *PublicWhisperAPI) HasSymKey(name string) (bool, error) {
|
||||
if api.whisper == nil {
|
||||
return false, whisperOffLineErr
|
||||
}
|
||||
res := api.whisper.HasTopicKey(name)
|
||||
res := api.whisper.HasSymKey(name)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// DeleteTopicKey deletes the key associated with the name string if it exists.
|
||||
func (api *PublicWhisperAPI) DeleteTopicKey(name string) error {
|
||||
// DeleteSymKey deletes the key associated with the name string if it exists.
|
||||
func (api *PublicWhisperAPI) DeleteSymKey(name string) error {
|
||||
if api.whisper == nil {
|
||||
return whisperOffLineErr
|
||||
}
|
||||
api.whisper.DeleteTopicKey(name)
|
||||
api.whisper.DeleteSymKey(name)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -157,7 +157,7 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (*rpc.HexNumber,
|
|||
filter := whisperv5.Filter{
|
||||
Src: crypto.ToECDSAPub(args.From),
|
||||
Dst: crypto.ToECDSAPub(args.To),
|
||||
KeySym: api.whisper.GetTopicKey(args.KeyName),
|
||||
KeySym: api.whisper.GetSymKey(args.KeyName),
|
||||
PoW: args.PoW,
|
||||
Messages: make(map[common.Hash]*whisperv5.ReceivedMessage),
|
||||
AcceptP2P: args.AcceptP2P,
|
||||
|
|
@ -260,7 +260,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
|
|||
params := whisperv5.MessageParams{
|
||||
TTL: args.TTL,
|
||||
Dst: crypto.ToECDSAPub(args.To),
|
||||
KeySym: api.whisper.GetTopicKey(args.KeyName),
|
||||
KeySym: api.whisper.GetSymKey(args.KeyName),
|
||||
Topic: args.Topic,
|
||||
Payload: args.Payload,
|
||||
Padding: args.Padding,
|
||||
|
|
|
|||
|
|
@ -208,7 +208,7 @@ func (e *Envelope) Open(watcher *Filter) (msg *ReceivedMessage) {
|
|||
} else if e.IsSymmetric() {
|
||||
msg, _ = e.OpenSymmetric(watcher.KeySym)
|
||||
if msg != nil {
|
||||
msg.TopicKeyHash = crypto.Keccak256Hash(watcher.KeySym)
|
||||
msg.SymKeyHash = crypto.Keccak256Hash(watcher.KeySym)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ func (f *Filter) MatchMessage(msg *ReceivedMessage) bool {
|
|||
return isEqual(f.Dst, msg.Dst)
|
||||
} else if f.expectsSymmetricEncryption() && msg.isSymmetricEncryption() {
|
||||
// check if that both the key and the topic match
|
||||
if f.SymKeyHash == msg.TopicKeyHash {
|
||||
if f.SymKeyHash == msg.SymKeyHash {
|
||||
for _, t := range f.Topics {
|
||||
if t == msg.Topic {
|
||||
return true
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ type ReceivedMessage struct {
|
|||
Dst *ecdsa.PublicKey // Message recipient (identity used to decode the message)
|
||||
Topic TopicType
|
||||
|
||||
TopicKeyHash common.Hash // The Keccak256Hash of the key, associated with the Topic
|
||||
SymKeyHash common.Hash // The Keccak256Hash of the key, associated with the Topic
|
||||
EnvelopeHash common.Hash // Message envelope hash to act as a unique id
|
||||
EnvelopeVersion uint64
|
||||
}
|
||||
|
|
@ -82,12 +82,8 @@ func isMessageSigned(flags byte) bool {
|
|||
return (flags & signatureFlag) != 0
|
||||
}
|
||||
|
||||
func isMessagePadded(flags byte) bool {
|
||||
return (flags & paddingMask) != 0
|
||||
}
|
||||
|
||||
func (msg *ReceivedMessage) isSymmetricEncryption() bool {
|
||||
return msg.TopicKeyHash != common.Hash{}
|
||||
return msg.SymKeyHash != common.Hash{}
|
||||
}
|
||||
|
||||
func (msg *ReceivedMessage) isAsymmetricEncryption() bool {
|
||||
|
|
@ -105,16 +101,9 @@ func DeriveOneTimeKey(key []byte, salt []byte, version uint64) ([]byte, error) {
|
|||
|
||||
// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
|
||||
func NewSentMessage(params *MessageParams) *SentMessage {
|
||||
// Construct an initial flag set: no signature, no padding, other bits random
|
||||
buf := make([]byte, 1)
|
||||
mrand.Read(buf)
|
||||
flags := buf[0]
|
||||
flags &= ^paddingMask
|
||||
flags &= ^signatureFlag
|
||||
|
||||
msg := SentMessage{}
|
||||
msg.Raw = make([]byte, 1, len(params.Payload)+len(params.Payload)+signatureLength+padSizeLimitUpper)
|
||||
msg.Raw[0] = flags
|
||||
msg.Raw[0] = 0 // set all the flags to zero
|
||||
msg.appendPadding(params)
|
||||
msg.Raw = append(msg.Raw, params.Payload...)
|
||||
return &msg
|
||||
|
|
|
|||
|
|
@ -42,13 +42,6 @@ func BytesToTopic(b []byte) (t TopicType) {
|
|||
return t
|
||||
}
|
||||
|
||||
func HashToTopic(h common.Hash) (t TopicType) {
|
||||
for i := 0; i < TopicLength; i++ {
|
||||
t[i] = h[i]
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// String converts a topic byte array to a string representation.
|
||||
func (topic *TopicType) String() string {
|
||||
return string(common.ToHex(topic[:]))
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ type Whisper struct {
|
|||
filters *Filters
|
||||
|
||||
privateKeys map[string]*ecdsa.PrivateKey
|
||||
topicKeys map[string][]byte
|
||||
symKeys map[string][]byte
|
||||
keyMu sync.RWMutex
|
||||
|
||||
envelopes map[common.Hash]*Envelope // Pool of messages currently tracked by this node
|
||||
|
|
@ -63,7 +63,7 @@ type Whisper struct {
|
|||
func New(server MailServer) *Whisper {
|
||||
whisper := &Whisper{
|
||||
privateKeys: make(map[string]*ecdsa.PrivateKey),
|
||||
topicKeys: make(map[string][]byte),
|
||||
symKeys: make(map[string][]byte),
|
||||
envelopes: make(map[common.Hash]*Envelope),
|
||||
messages: make(map[common.Hash]*ReceivedMessage),
|
||||
expirations: make(map[uint32]*set.SetNonTS),
|
||||
|
|
@ -179,8 +179,8 @@ func (w *Whisper) GetIdentity(key *ecdsa.PublicKey) *ecdsa.PrivateKey {
|
|||
return w.privateKeys[string(crypto.FromECDSAPub(key))]
|
||||
}
|
||||
|
||||
func (w *Whisper) GenerateTopicKey(name string) error {
|
||||
if w.HasTopicKey(name) {
|
||||
func (w *Whisper) GenerateSymKey(name string) error {
|
||||
if w.HasSymKey(name) {
|
||||
return fmt.Errorf("Key with name [%s] already exists", name)
|
||||
}
|
||||
|
||||
|
|
@ -194,12 +194,12 @@ func (w *Whisper) GenerateTopicKey(name string) error {
|
|||
|
||||
w.keyMu.Lock()
|
||||
defer w.keyMu.Unlock()
|
||||
w.topicKeys[name] = key
|
||||
w.symKeys[name] = key
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Whisper) AddTopicKey(name string, key []byte) error {
|
||||
if w.HasTopicKey(name) {
|
||||
func (w *Whisper) AddSymKey(name string, key []byte) error {
|
||||
if w.HasSymKey(name) {
|
||||
return fmt.Errorf("Key with name [%s] already exists", name)
|
||||
}
|
||||
|
||||
|
|
@ -210,26 +210,26 @@ func (w *Whisper) AddTopicKey(name string, key []byte) error {
|
|||
|
||||
w.keyMu.Lock()
|
||||
defer w.keyMu.Unlock()
|
||||
w.topicKeys[name] = derived
|
||||
w.symKeys[name] = derived
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Whisper) HasTopicKey(name string) bool {
|
||||
func (w *Whisper) HasSymKey(name string) bool {
|
||||
w.keyMu.RLock()
|
||||
defer w.keyMu.RUnlock()
|
||||
return w.topicKeys[name] != nil
|
||||
return w.symKeys[name] != nil
|
||||
}
|
||||
|
||||
func (w *Whisper) DeleteTopicKey(name string) {
|
||||
func (w *Whisper) DeleteSymKey(name string) {
|
||||
w.keyMu.Lock()
|
||||
defer w.keyMu.Unlock()
|
||||
delete(w.topicKeys, name)
|
||||
delete(w.symKeys, name)
|
||||
}
|
||||
|
||||
func (w *Whisper) GetTopicKey(name string) []byte {
|
||||
func (w *Whisper) GetSymKey(name string) []byte {
|
||||
w.keyMu.RLock()
|
||||
defer w.keyMu.RUnlock()
|
||||
return w.topicKeys[name]
|
||||
return w.symKeys[name]
|
||||
}
|
||||
|
||||
// Watch installs a new message handler to run in case a matching packet arrives
|
||||
|
|
|
|||
Loading…
Reference in a new issue