| 
									
										
										
										
											2015-01-30 13:25:12 +01:00
										 |  |  | package xeth | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-16 11:27:38 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/common" | 
					
						
							| 
									
										
										
										
											2015-03-16 14:46:46 -04:00
										 |  |  | 	"github.com/ethereum/go-ethereum/crypto" | 
					
						
							| 
									
										
										
										
											2015-01-30 13:25:12 +01:00
										 |  |  | 	"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 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-16 11:27:38 +01:00
										 |  |  | 	pk := crypto.ToECDSAPub(common.FromHex(from)) | 
					
						
							| 
									
										
										
										
											2015-01-30 13:25:12 +01:00
										 |  |  | 	if key := self.Whisper.GetIdentity(pk); key != nil || len(from) == 0 { | 
					
						
							| 
									
										
										
										
											2015-03-16 11:27:38 +01:00
										 |  |  | 		msg := whisper.NewMessage(common.FromHex(payload)) | 
					
						
							| 
									
										
										
										
											2015-04-10 17:03:08 +03:00
										 |  |  | 		envelope, err := msg.Wrap(time.Duration(priority*100000), whisper.Options{ | 
					
						
							|  |  |  | 			TTL:    time.Duration(ttl) * time.Second, | 
					
						
							| 
									
										
										
										
											2015-03-16 11:27:38 +01:00
										 |  |  | 			To:     crypto.ToECDSAPub(common.FromHex(to)), | 
					
						
							| 
									
										
										
										
											2015-01-30 13:25:12 +01:00
										 |  |  | 			From:   key, | 
					
						
							| 
									
										
										
										
											2015-04-14 15:02:31 +03:00
										 |  |  | 			Topics: whisper.NewTopicsFromStrings(topics...), | 
					
						
							| 
									
										
										
										
											2015-01-30 13:25:12 +01:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		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() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-16 15:17:19 +01:00
										 |  |  | 	return common.ToHex(crypto.FromECDSAPub(&key.PublicKey)) | 
					
						
							| 
									
										
										
										
											2015-01-30 13:25:12 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (self *Whisper) HasIdentity(key string) bool { | 
					
						
							| 
									
										
										
										
											2015-03-16 11:27:38 +01:00
										 |  |  | 	return self.Whisper.HasIdentity(crypto.ToECDSAPub(common.FromHex(key))) | 
					
						
							| 
									
										
										
										
											2015-01-30 13:25:12 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-17 09:11:01 -04:00
										 |  |  | // func (self *Whisper) RemoveIdentity(key string) bool { | 
					
						
							|  |  |  | // 	return self.Whisper.RemoveIdentity(crypto.ToECDSAPub(common.FromHex(key))) | 
					
						
							|  |  |  | // } | 
					
						
							| 
									
										
										
										
											2015-03-16 14:46:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-30 13:25:12 +01:00
										 |  |  | func (self *Whisper) Watch(opts *Options) int { | 
					
						
							|  |  |  | 	filter := whisper.Filter{ | 
					
						
							| 
									
										
										
										
											2015-03-16 11:27:38 +01:00
										 |  |  | 		To:     crypto.ToECDSAPub(common.FromHex(opts.To)), | 
					
						
							|  |  |  | 		From:   crypto.ToECDSAPub(common.FromHex(opts.From)), | 
					
						
							| 
									
										
										
										
											2015-04-14 15:02:31 +03:00
										 |  |  | 		Topics: whisper.NewTopicsFromStrings(opts.Topics...), | 
					
						
							| 
									
										
										
										
											2015-01-30 13:25:12 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var i int | 
					
						
							|  |  |  | 	filter.Fn = func(msg *whisper.Message) { | 
					
						
							|  |  |  | 		opts.Fn(NewWhisperMessage(msg)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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 | 
					
						
							|  |  |  | 	Payload string `json:"payload"` | 
					
						
							| 
									
										
										
										
											2015-02-10 13:20:06 +01:00
										 |  |  | 	To      string `json:"to"` | 
					
						
							| 
									
										
										
										
											2015-01-30 13:25:12 +01:00
										 |  |  | 	From    string `json:"from"` | 
					
						
							| 
									
										
										
										
											2015-02-10 13:20:06 +01:00
										 |  |  | 	Sent    int64  `json:"sent"` | 
					
						
							| 
									
										
										
										
											2015-01-30 13:25:12 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewWhisperMessage(msg *whisper.Message) WhisperMessage { | 
					
						
							|  |  |  | 	return WhisperMessage{ | 
					
						
							|  |  |  | 		ref:     msg, | 
					
						
							| 
									
										
										
										
											2015-03-16 15:17:19 +01:00
										 |  |  | 		Payload: common.ToHex(msg.Payload), | 
					
						
							|  |  |  | 		From:    common.ToHex(crypto.FromECDSAPub(msg.Recover())), | 
					
						
							|  |  |  | 		To:      common.ToHex(crypto.FromECDSAPub(msg.To)), | 
					
						
							| 
									
										
										
										
											2015-02-03 06:56:19 -08:00
										 |  |  | 		Sent:    msg.Sent, | 
					
						
							| 
									
										
										
										
											2015-01-30 13:25:12 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |