diff options
author | obscuren <geffobscura@gmail.com> | 2015-01-30 20:25:12 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-01-30 20:25:12 +0800 |
commit | c03d403437c20584bcbf3cf3fa9d79ac7a0a8ca7 (patch) | |
tree | 137343143b03e4b76bbbfa269c2fc7d35f9f6a35 /xeth | |
parent | c48644490f039fb9756b4cd1fedf11fbb1c4a16f (diff) | |
download | go-tangerine-c03d403437c20584bcbf3cf3fa9d79ac7a0a8ca7.tar go-tangerine-c03d403437c20584bcbf3cf3fa9d79ac7a0a8ca7.tar.gz go-tangerine-c03d403437c20584bcbf3cf3fa9d79ac7a0a8ca7.tar.bz2 go-tangerine-c03d403437c20584bcbf3cf3fa9d79ac7a0a8ca7.tar.lz go-tangerine-c03d403437c20584bcbf3cf3fa9d79ac7a0a8ca7.tar.xz go-tangerine-c03d403437c20584bcbf3cf3fa9d79ac7a0a8ca7.tar.zst go-tangerine-c03d403437c20584bcbf3cf3fa9d79ac7a0a8ca7.zip |
Added whisper interface for xeth, added examples, updated RPC
* Added RPC methods for whisper
* Added whisper example
Diffstat (limited to 'xeth')
-rw-r--r-- | xeth/whisper.go | 116 | ||||
-rw-r--r-- | xeth/xeth.go | 12 |
2 files changed, 123 insertions, 5 deletions
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())), + } +} diff --git a/xeth/xeth.go b/xeth/xeth.go index 4cfa104d0..a65fa1331 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -16,6 +16,7 @@ import ( "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/state" + "github.com/ethereum/go-ethereum/whisper" ) var pipelogger = logger.NewLogger("XETH") @@ -33,6 +34,7 @@ type Backend interface { ClientIdentity() p2p.ClientIdentity Db() ethutil.Database EventMux() *event.TypeMux + Whisper() *whisper.Whisper } type XEth struct { @@ -40,6 +42,7 @@ type XEth struct { blockProcessor *core.BlockProcessor chainManager *core.ChainManager state *State + whisper *Whisper } func New(eth Backend) *XEth { @@ -47,17 +50,16 @@ func New(eth Backend) *XEth { eth: eth, blockProcessor: eth.BlockProcessor(), chainManager: eth.ChainManager(), + whisper: NewWhisper(eth.Whisper()), } xeth.state = NewState(xeth) return xeth } -func (self *XEth) Backend() Backend { - return self.eth -} - -func (self *XEth) State() *State { return self.state } +func (self *XEth) Backend() Backend { return self.eth } +func (self *XEth) State() *State { return self.state } +func (self *XEth) Whisper() *Whisper { return self.whisper } func (self *XEth) BlockByHash(strHash string) *Block { hash := fromHex(strHash) |