github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/ui/qt/qwhisper/whisper.go (about)

     1  // QWhisper package. This package is temporarily on hold until QML DApp dev will reemerge.
     2  package qwhisper
     3  
     4  import (
     5  	"time"
     6  
     7  	"github.com/jonasnick/go-ethereum/crypto"
     8  	"github.com/jonasnick/go-ethereum/ethutil"
     9  	"github.com/jonasnick/go-ethereum/logger"
    10  	"github.com/jonasnick/go-ethereum/whisper"
    11  	"github.com/obscuren/qml"
    12  )
    13  
    14  var qlogger = logger.NewLogger("QSHH")
    15  
    16  func fromHex(s string) []byte {
    17  	if len(s) > 1 {
    18  		return ethutil.Hex2Bytes(s[2:])
    19  	}
    20  	return nil
    21  }
    22  func toHex(b []byte) string { return "0x" + ethutil.Bytes2Hex(b) }
    23  
    24  type Whisper struct {
    25  	*whisper.Whisper
    26  	view qml.Object
    27  
    28  	watches map[int]*Watch
    29  }
    30  
    31  func New(w *whisper.Whisper) *Whisper {
    32  	return &Whisper{w, nil, make(map[int]*Watch)}
    33  }
    34  
    35  func (self *Whisper) SetView(view qml.Object) {
    36  	self.view = view
    37  }
    38  
    39  func (self *Whisper) Post(payload []string, to, from string, topics []string, priority, ttl uint32) {
    40  	var data []byte
    41  	for _, d := range payload {
    42  		data = append(data, fromHex(d)...)
    43  	}
    44  
    45  	pk := crypto.ToECDSAPub(fromHex(from))
    46  	if key := self.Whisper.GetIdentity(pk); key != nil {
    47  		msg := whisper.NewMessage(data)
    48  		envelope, err := msg.Seal(time.Duration(priority*100000), whisper.Opts{
    49  			Ttl:    time.Duration(ttl) * time.Second,
    50  			To:     crypto.ToECDSAPub(fromHex(to)),
    51  			From:   key,
    52  			Topics: whisper.TopicsFromString(topics...),
    53  		})
    54  
    55  		if err != nil {
    56  			qlogger.Infoln(err)
    57  			// handle error
    58  			return
    59  		}
    60  
    61  		if err := self.Whisper.Send(envelope); err != nil {
    62  			qlogger.Infoln(err)
    63  			// handle error
    64  			return
    65  		}
    66  	} else {
    67  		qlogger.Infoln("unmatched pub / priv for seal")
    68  	}
    69  
    70  }
    71  
    72  func (self *Whisper) NewIdentity() string {
    73  	key := self.Whisper.NewIdentity()
    74  
    75  	return toHex(crypto.FromECDSAPub(&key.PublicKey))
    76  }
    77  
    78  func (self *Whisper) HasIdentity(key string) bool {
    79  	return self.Whisper.HasIdentity(crypto.ToECDSAPub(fromHex(key)))
    80  }
    81  
    82  func (self *Whisper) Watch(opts map[string]interface{}, view *qml.Common) int {
    83  	filter := filterFromMap(opts)
    84  	var i int
    85  	filter.Fn = func(msg *whisper.Message) {
    86  		if view != nil {
    87  			view.Call("onShhMessage", ToQMessage(msg), i)
    88  		}
    89  	}
    90  
    91  	i = self.Whisper.Watch(filter)
    92  	self.watches[i] = &Watch{}
    93  
    94  	return i
    95  }
    96  
    97  func (self *Whisper) Messages(id int) (messages *ethutil.List) {
    98  	msgs := self.Whisper.Messages(id)
    99  	messages = ethutil.EmptyList()
   100  	for _, message := range msgs {
   101  		messages.Append(ToQMessage(message))
   102  	}
   103  
   104  	return
   105  }
   106  
   107  func filterFromMap(opts map[string]interface{}) (f whisper.Filter) {
   108  	if to, ok := opts["to"].(string); ok {
   109  		f.To = crypto.ToECDSAPub(fromHex(to))
   110  	}
   111  	if from, ok := opts["from"].(string); ok {
   112  		f.From = crypto.ToECDSAPub(fromHex(from))
   113  	}
   114  	if topicList, ok := opts["topics"].(*qml.List); ok {
   115  		var topics []string
   116  		topicList.Convert(&topics)
   117  		f.Topics = whisper.TopicsFromString(topics...)
   118  	}
   119  
   120  	return
   121  }