From c03d403437c20584bcbf3cf3fa9d79ac7a0a8ca7 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 30 Jan 2015 13:25:12 +0100 Subject: Added whisper interface for xeth, added examples, updated RPC * Added RPC methods for whisper * Added whisper example --- xeth/whisper.go | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 xeth/whisper.go (limited to 'xeth/whisper.go') diff --git a/xeth/whisper.go b/xeth/whisper.go new file mode 100644 index 000000000..31201271b --- /dev/null +++ b/xeth/whisper.go @@ -0,0 +1,116 @@ +package xeth + +import ( + "errors" + "fmt" + "time" + + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/whisper" +) + +var qlogger = logger.NewLogger("XSHH") + +type Whisper struct { + *whisper.Whisper +} + +func NewWhisper(w *whisper.Whisper) *Whisper { + return &Whisper{w} +} + +func (self *Whisper) Post(payload string, to, from string, topics []string, priority, ttl uint32) error { + if priority == 0 { + priority = 1000 + } + + if ttl == 0 { + ttl = 100 + } + + pk := crypto.ToECDSAPub(fromHex(from)) + if key := self.Whisper.GetIdentity(pk); key != nil || len(from) == 0 { + msg := whisper.NewMessage(fromHex(payload)) + envelope, err := msg.Seal(time.Duration(priority*100000), whisper.Opts{ + Ttl: time.Duration(ttl) * time.Second, + To: crypto.ToECDSAPub(fromHex(to)), + From: key, + Topics: whisper.TopicsFromString(topics...), + }) + + if err != nil { + return err + } + + if err := self.Whisper.Send(envelope); err != nil { + return err + } + } else { + return errors.New("unmatched pub / priv for seal") + } + + return nil +} + +func (self *Whisper) NewIdentity() string { + key := self.Whisper.NewIdentity() + + return toHex(crypto.FromECDSAPub(&key.PublicKey)) +} + +func (self *Whisper) HasIdentity(key string) bool { + return self.Whisper.HasIdentity(crypto.ToECDSAPub(fromHex(key))) +} + +func (self *Whisper) Watch(opts *Options) int { + filter := whisper.Filter{ + To: crypto.ToECDSA(fromHex(opts.To)), + From: crypto.ToECDSAPub(fromHex(opts.From)), + Topics: whisper.TopicsFromString(opts.Topics...), + } + + var i int + filter.Fn = func(msg *whisper.Message) { + opts.Fn(NewWhisperMessage(msg)) + } + fmt.Println("new filter", filter) + + i = self.Whisper.Watch(filter) + + return i +} + +func (self *Whisper) Messages(id int) (messages []WhisperMessage) { + msgs := self.Whisper.Messages(id) + messages = make([]WhisperMessage, len(msgs)) + for i, message := range msgs { + messages[i] = NewWhisperMessage(message) + } + + return +} + +type Options struct { + To string + From string + Topics []string + Fn func(msg WhisperMessage) +} + +type WhisperMessage struct { + ref *whisper.Message + Flags int32 `json:"flags"` + Payload string `json:"payload"` + From string `json:"from"` +} + +func NewWhisperMessage(msg *whisper.Message) WhisperMessage { + return WhisperMessage{ + ref: msg, + Flags: int32(msg.Flags), + Payload: "0x" + ethutil.Bytes2Hex(msg.Payload), + From: "0x" + ethutil.Bytes2Hex(crypto.FromECDSAPub(msg.Recover())), + } +} -- cgit v1.2.3 From 2d9b3aa537a901d9b5b3aa951d71d9977c6e4703 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 30 Jan 2015 17:00:33 +0100 Subject: Removed debug log --- xeth/whisper.go | 2 -- 1 file changed, 2 deletions(-) (limited to 'xeth/whisper.go') diff --git a/xeth/whisper.go b/xeth/whisper.go index 31201271b..32ad1332b 100644 --- a/xeth/whisper.go +++ b/xeth/whisper.go @@ -2,7 +2,6 @@ package xeth import ( "errors" - "fmt" "time" "github.com/ethereum/go-ethereum/crypto" @@ -75,7 +74,6 @@ func (self *Whisper) Watch(opts *Options) int { filter.Fn = func(msg *whisper.Message) { opts.Fn(NewWhisperMessage(msg)) } - fmt.Println("new filter", filter) i = self.Whisper.Watch(filter) -- cgit v1.2.3 From 623469cb6c6cdb6ff84dc2cb7d4409e9d7cf3f65 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 3 Feb 2015 06:56:19 -0800 Subject: Added missing whisper timestamp. Closes #284 --- xeth/whisper.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'xeth/whisper.go') diff --git a/xeth/whisper.go b/xeth/whisper.go index 32ad1332b..d537f8c54 100644 --- a/xeth/whisper.go +++ b/xeth/whisper.go @@ -99,16 +99,16 @@ type Options struct { type WhisperMessage struct { ref *whisper.Message - Flags int32 `json:"flags"` Payload string `json:"payload"` From string `json:"from"` + Sent uint64 `json:"time"` } func NewWhisperMessage(msg *whisper.Message) WhisperMessage { return WhisperMessage{ ref: msg, - Flags: int32(msg.Flags), Payload: "0x" + ethutil.Bytes2Hex(msg.Payload), From: "0x" + ethutil.Bytes2Hex(crypto.FromECDSAPub(msg.Recover())), + Sent: msg.Sent, } } -- cgit v1.2.3 From 7bd2fbe2b1445c26190008d21ad52dc5c364765c Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 3 Feb 2015 07:16:05 -0800 Subject: Fixed whisper "to" filtering. Closes #283 --- xeth/whisper.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'xeth/whisper.go') diff --git a/xeth/whisper.go b/xeth/whisper.go index d537f8c54..42d7c44bc 100644 --- a/xeth/whisper.go +++ b/xeth/whisper.go @@ -2,6 +2,7 @@ package xeth import ( "errors" + "fmt" "time" "github.com/ethereum/go-ethereum/crypto" @@ -31,6 +32,7 @@ func (self *Whisper) Post(payload string, to, from string, topics []string, prio pk := crypto.ToECDSAPub(fromHex(from)) if key := self.Whisper.GetIdentity(pk); key != nil || len(from) == 0 { + fmt.Println("POST:", to) msg := whisper.NewMessage(fromHex(payload)) envelope, err := msg.Seal(time.Duration(priority*100000), whisper.Opts{ Ttl: time.Duration(ttl) * time.Second, @@ -101,7 +103,7 @@ type WhisperMessage struct { ref *whisper.Message Payload string `json:"payload"` From string `json:"from"` - Sent uint64 `json:"time"` + Sent int64 `json:"time"` } func NewWhisperMessage(msg *whisper.Message) WhisperMessage { -- cgit v1.2.3 From b1870631a4829e075eae77064973ef94aa2166b3 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 4 Feb 2015 05:52:59 -0800 Subject: WIP miner --- xeth/whisper.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'xeth/whisper.go') diff --git a/xeth/whisper.go b/xeth/whisper.go index 42d7c44bc..52f4593e4 100644 --- a/xeth/whisper.go +++ b/xeth/whisper.go @@ -2,11 +2,9 @@ package xeth import ( "errors" - "fmt" "time" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/whisper" ) @@ -32,7 +30,6 @@ func (self *Whisper) Post(payload string, to, from string, topics []string, prio pk := crypto.ToECDSAPub(fromHex(from)) if key := self.Whisper.GetIdentity(pk); key != nil || len(from) == 0 { - fmt.Println("POST:", to) msg := whisper.NewMessage(fromHex(payload)) envelope, err := msg.Seal(time.Duration(priority*100000), whisper.Opts{ Ttl: time.Duration(ttl) * time.Second, @@ -109,8 +106,8 @@ type WhisperMessage struct { func NewWhisperMessage(msg *whisper.Message) WhisperMessage { return WhisperMessage{ ref: msg, - Payload: "0x" + ethutil.Bytes2Hex(msg.Payload), - From: "0x" + ethutil.Bytes2Hex(crypto.FromECDSAPub(msg.Recover())), + Payload: toHex(msg.Payload), + From: toHex(crypto.FromECDSAPub(msg.Recover())), Sent: msg.Sent, } } -- cgit v1.2.3 From e40c1c62ce0c2d9567066d84ea74fd24b424a81a Mon Sep 17 00:00:00 2001 From: obscuren Date: Thu, 5 Feb 2015 15:00:59 -0800 Subject: API changed to use Pubkey only. Reflected that change in the rest of the api --- xeth/whisper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xeth/whisper.go') diff --git a/xeth/whisper.go b/xeth/whisper.go index 52f4593e4..8e3bcefd0 100644 --- a/xeth/whisper.go +++ b/xeth/whisper.go @@ -64,7 +64,7 @@ func (self *Whisper) HasIdentity(key string) bool { func (self *Whisper) Watch(opts *Options) int { filter := whisper.Filter{ - To: crypto.ToECDSA(fromHex(opts.To)), + To: crypto.ToECDSAPub(fromHex(opts.To)), From: crypto.ToECDSAPub(fromHex(opts.From)), Topics: whisper.TopicsFromString(opts.Topics...), } -- cgit v1.2.3 From ddccea75e8f4931689fdf6b57ec7159b65c0d3c5 Mon Sep 17 00:00:00 2001 From: obscuren Date: Tue, 10 Feb 2015 13:20:06 +0100 Subject: Fixed "to" field --- xeth/whisper.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'xeth/whisper.go') diff --git a/xeth/whisper.go b/xeth/whisper.go index 8e3bcefd0..d9c7e1614 100644 --- a/xeth/whisper.go +++ b/xeth/whisper.go @@ -99,8 +99,9 @@ type Options struct { type WhisperMessage struct { ref *whisper.Message Payload string `json:"payload"` + To string `json:"to"` From string `json:"from"` - Sent int64 `json:"time"` + Sent int64 `json:"sent"` } func NewWhisperMessage(msg *whisper.Message) WhisperMessage { @@ -108,6 +109,7 @@ func NewWhisperMessage(msg *whisper.Message) WhisperMessage { ref: msg, Payload: toHex(msg.Payload), From: toHex(crypto.FromECDSAPub(msg.Recover())), + To: toHex(crypto.FromECDSAPub(msg.To)), Sent: msg.Sent, } } -- cgit v1.2.3