Пример #1
0
// SendSelf wraps a payload into a Whisper envelope and forwards it to itself.
func selfSend(shh *whisper.Whisper, payload []byte) error {
	ok := make(chan struct{})

	// Start watching for self messages, output any arrivals
	id := shh.NewIdentity()
	shh.Watch(whisper.Filter{
		To: &id.PublicKey,
		Fn: func(msg *whisper.Message) {
			fmt.Printf("Message received: %s, signed with 0x%x.\n", string(msg.Payload), msg.Signature)
			close(ok)
		},
	})
	// Wrap the payload and encrypt it
	msg := whisper.NewMessage(payload)
	envelope, err := msg.Wrap(whisper.DefaultPoW, whisper.Options{
		From: id,
		To:   &id.PublicKey,
		TTL:  whisper.DefaultTTL,
	})
	if err != nil {
		return fmt.Errorf("failed to seal message: %v", err)
	}
	// Dump the message into the system and wait for it to pop back out
	if err := shh.Send(envelope); err != nil {
		return fmt.Errorf("failed to send self-message: %v", err)
	}
	select {
	case <-ok:
	case <-time.After(time.Second):
		return fmt.Errorf("failed to receive message in time")
	}
	return nil
}
Пример #2
0
// Post injects a message into the whisper network for distribution.
func (self *Whisper) Post(payload string, to, from string, topics []string, priority, ttl uint32) error {
	// Decode the topic strings
	topicsDecoded := make([][]byte, len(topics))
	for i, topic := range topics {
		topicsDecoded[i] = common.FromHex(topic)
	}
	// Construct the whisper message and transmission options
	message := whisper.NewMessage(common.FromHex(payload))
	options := whisper.Options{
		To:     crypto.ToECDSAPub(common.FromHex(to)),
		TTL:    time.Duration(ttl) * time.Second,
		Topics: whisper.NewTopics(topicsDecoded...),
	}
	if len(from) != 0 {
		if key := self.Whisper.GetIdentity(crypto.ToECDSAPub(common.FromHex(from))); key != nil {
			options.From = key
		} else {
			return fmt.Errorf("unknown identity to send from: %s", from)
		}
	}
	// Wrap and send the message
	pow := time.Duration(priority) * time.Millisecond
	envelope, err := message.Wrap(pow, options)
	if err != nil {
		return err
	}
	if err := self.Whisper.Send(envelope); err != nil {
		return err
	}
	return nil
}
Пример #3
0
func (self *Whisper) Post(payload []string, to, from string, topics []string, priority, ttl uint32) {
	var data []byte
	for _, d := range payload {
		data = append(data, common.FromHex(d)...)
	}

	pk := crypto.ToECDSAPub(common.FromHex(from))
	if key := self.Whisper.GetIdentity(pk); key != nil {
		msg := whisper.NewMessage(data)
		envelope, err := msg.Wrap(time.Duration(priority*100000), whisper.Options{
			TTL:    time.Duration(ttl) * time.Second,
			To:     crypto.ToECDSAPub(common.FromHex(to)),
			From:   key,
			Topics: whisper.NewTopicsFromStrings(topics...),
		})

		if err != nil {
			qlogger.Infoln(err)
			// handle error
			return
		}

		if err := self.Whisper.Send(envelope); err != nil {
			qlogger.Infoln(err)
			// handle error
			return
		}
	} else {
		qlogger.Infoln("unmatched pub / priv for seal")
	}

}