mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +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
|
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.
|
// 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 {
|
if api.whisper == nil {
|
||||||
return whisperOffLineErr
|
return whisperOffLineErr
|
||||||
}
|
}
|
||||||
return api.whisper.GenerateTopicKey(name)
|
return api.whisper.GenerateSymKey(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddTopicKey stores the key under the 'name' id.
|
// AddSymKey stores the key under the 'name' id.
|
||||||
func (api *PublicWhisperAPI) AddTopicKey(name string, key []byte) error {
|
func (api *PublicWhisperAPI) AddSymKey(name string, key []byte) error {
|
||||||
if api.whisper == nil {
|
if api.whisper == nil {
|
||||||
return whisperOffLineErr
|
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.
|
// Otherwise returns false.
|
||||||
func (api *PublicWhisperAPI) HasTopicKey(name string) (bool, error) {
|
func (api *PublicWhisperAPI) HasSymKey(name string) (bool, error) {
|
||||||
if api.whisper == nil {
|
if api.whisper == nil {
|
||||||
return false, whisperOffLineErr
|
return false, whisperOffLineErr
|
||||||
}
|
}
|
||||||
res := api.whisper.HasTopicKey(name)
|
res := api.whisper.HasSymKey(name)
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteTopicKey deletes the key associated with the name string if it exists.
|
// DeleteSymKey deletes the key associated with the name string if it exists.
|
||||||
func (api *PublicWhisperAPI) DeleteTopicKey(name string) error {
|
func (api *PublicWhisperAPI) DeleteSymKey(name string) error {
|
||||||
if api.whisper == nil {
|
if api.whisper == nil {
|
||||||
return whisperOffLineErr
|
return whisperOffLineErr
|
||||||
}
|
}
|
||||||
api.whisper.DeleteTopicKey(name)
|
api.whisper.DeleteSymKey(name)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -157,7 +157,7 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (*rpc.HexNumber,
|
||||||
filter := whisperv5.Filter{
|
filter := whisperv5.Filter{
|
||||||
Src: crypto.ToECDSAPub(args.From),
|
Src: crypto.ToECDSAPub(args.From),
|
||||||
Dst: crypto.ToECDSAPub(args.To),
|
Dst: crypto.ToECDSAPub(args.To),
|
||||||
KeySym: api.whisper.GetTopicKey(args.KeyName),
|
KeySym: api.whisper.GetSymKey(args.KeyName),
|
||||||
PoW: args.PoW,
|
PoW: args.PoW,
|
||||||
Messages: make(map[common.Hash]*whisperv5.ReceivedMessage),
|
Messages: make(map[common.Hash]*whisperv5.ReceivedMessage),
|
||||||
AcceptP2P: args.AcceptP2P,
|
AcceptP2P: args.AcceptP2P,
|
||||||
|
|
@ -260,7 +260,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
|
||||||
params := whisperv5.MessageParams{
|
params := whisperv5.MessageParams{
|
||||||
TTL: args.TTL,
|
TTL: args.TTL,
|
||||||
Dst: crypto.ToECDSAPub(args.To),
|
Dst: crypto.ToECDSAPub(args.To),
|
||||||
KeySym: api.whisper.GetTopicKey(args.KeyName),
|
KeySym: api.whisper.GetSymKey(args.KeyName),
|
||||||
Topic: args.Topic,
|
Topic: args.Topic,
|
||||||
Payload: args.Payload,
|
Payload: args.Payload,
|
||||||
Padding: args.Padding,
|
Padding: args.Padding,
|
||||||
|
|
|
||||||
|
|
@ -208,7 +208,7 @@ func (e *Envelope) Open(watcher *Filter) (msg *ReceivedMessage) {
|
||||||
} else if e.IsSymmetric() {
|
} else if e.IsSymmetric() {
|
||||||
msg, _ = e.OpenSymmetric(watcher.KeySym)
|
msg, _ = e.OpenSymmetric(watcher.KeySym)
|
||||||
if msg != nil {
|
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)
|
return isEqual(f.Dst, msg.Dst)
|
||||||
} else if f.expectsSymmetricEncryption() && msg.isSymmetricEncryption() {
|
} else if f.expectsSymmetricEncryption() && msg.isSymmetricEncryption() {
|
||||||
// check if that both the key and the topic match
|
// 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 {
|
for _, t := range f.Topics {
|
||||||
if t == msg.Topic {
|
if t == msg.Topic {
|
||||||
return true
|
return true
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ type ReceivedMessage struct {
|
||||||
Dst *ecdsa.PublicKey // Message recipient (identity used to decode the message)
|
Dst *ecdsa.PublicKey // Message recipient (identity used to decode the message)
|
||||||
Topic TopicType
|
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
|
EnvelopeHash common.Hash // Message envelope hash to act as a unique id
|
||||||
EnvelopeVersion uint64
|
EnvelopeVersion uint64
|
||||||
}
|
}
|
||||||
|
|
@ -82,12 +82,8 @@ func isMessageSigned(flags byte) bool {
|
||||||
return (flags & signatureFlag) != 0
|
return (flags & signatureFlag) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func isMessagePadded(flags byte) bool {
|
|
||||||
return (flags & paddingMask) != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (msg *ReceivedMessage) isSymmetricEncryption() bool {
|
func (msg *ReceivedMessage) isSymmetricEncryption() bool {
|
||||||
return msg.TopicKeyHash != common.Hash{}
|
return msg.SymKeyHash != common.Hash{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (msg *ReceivedMessage) isAsymmetricEncryption() bool {
|
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.
|
// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
|
||||||
func NewSentMessage(params *MessageParams) *SentMessage {
|
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 := SentMessage{}
|
||||||
msg.Raw = make([]byte, 1, len(params.Payload)+len(params.Payload)+signatureLength+padSizeLimitUpper)
|
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.appendPadding(params)
|
||||||
msg.Raw = append(msg.Raw, params.Payload...)
|
msg.Raw = append(msg.Raw, params.Payload...)
|
||||||
return &msg
|
return &msg
|
||||||
|
|
|
||||||
|
|
@ -42,13 +42,6 @@ func BytesToTopic(b []byte) (t TopicType) {
|
||||||
return t
|
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.
|
// String converts a topic byte array to a string representation.
|
||||||
func (topic *TopicType) String() string {
|
func (topic *TopicType) String() string {
|
||||||
return string(common.ToHex(topic[:]))
|
return string(common.ToHex(topic[:]))
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ type Whisper struct {
|
||||||
filters *Filters
|
filters *Filters
|
||||||
|
|
||||||
privateKeys map[string]*ecdsa.PrivateKey
|
privateKeys map[string]*ecdsa.PrivateKey
|
||||||
topicKeys map[string][]byte
|
symKeys map[string][]byte
|
||||||
keyMu sync.RWMutex
|
keyMu sync.RWMutex
|
||||||
|
|
||||||
envelopes map[common.Hash]*Envelope // Pool of messages currently tracked by this node
|
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 {
|
func New(server MailServer) *Whisper {
|
||||||
whisper := &Whisper{
|
whisper := &Whisper{
|
||||||
privateKeys: make(map[string]*ecdsa.PrivateKey),
|
privateKeys: make(map[string]*ecdsa.PrivateKey),
|
||||||
topicKeys: make(map[string][]byte),
|
symKeys: make(map[string][]byte),
|
||||||
envelopes: make(map[common.Hash]*Envelope),
|
envelopes: make(map[common.Hash]*Envelope),
|
||||||
messages: make(map[common.Hash]*ReceivedMessage),
|
messages: make(map[common.Hash]*ReceivedMessage),
|
||||||
expirations: make(map[uint32]*set.SetNonTS),
|
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))]
|
return w.privateKeys[string(crypto.FromECDSAPub(key))]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Whisper) GenerateTopicKey(name string) error {
|
func (w *Whisper) GenerateSymKey(name string) error {
|
||||||
if w.HasTopicKey(name) {
|
if w.HasSymKey(name) {
|
||||||
return fmt.Errorf("Key with name [%s] already exists", 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()
|
w.keyMu.Lock()
|
||||||
defer w.keyMu.Unlock()
|
defer w.keyMu.Unlock()
|
||||||
w.topicKeys[name] = key
|
w.symKeys[name] = key
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Whisper) AddTopicKey(name string, key []byte) error {
|
func (w *Whisper) AddSymKey(name string, key []byte) error {
|
||||||
if w.HasTopicKey(name) {
|
if w.HasSymKey(name) {
|
||||||
return fmt.Errorf("Key with name [%s] already exists", 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()
|
w.keyMu.Lock()
|
||||||
defer w.keyMu.Unlock()
|
defer w.keyMu.Unlock()
|
||||||
w.topicKeys[name] = derived
|
w.symKeys[name] = derived
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Whisper) HasTopicKey(name string) bool {
|
func (w *Whisper) HasSymKey(name string) bool {
|
||||||
w.keyMu.RLock()
|
w.keyMu.RLock()
|
||||||
defer w.keyMu.RUnlock()
|
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()
|
w.keyMu.Lock()
|
||||||
defer w.keyMu.Unlock()
|
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()
|
w.keyMu.RLock()
|
||||||
defer w.keyMu.RUnlock()
|
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
|
// Watch installs a new message handler to run in case a matching packet arrives
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue