github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/api/shh.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/ethereum/go-ethereum/eth"
    23  	"github.com/ethereum/go-ethereum/rpc/codec"
    24  	"github.com/ethereum/go-ethereum/rpc/shared"
    25  	"github.com/ethereum/go-ethereum/xeth"
    26  )
    27  
    28  const (
    29  	ShhApiVersion = "1.0"
    30  )
    31  
    32  var (
    33  	// mapping between methods and handlers
    34  	shhMapping = map[string]shhhandler{
    35  		"shh_version":          (*shhApi).Version,
    36  		"shh_post":             (*shhApi).Post,
    37  		"shh_hasIdentity":      (*shhApi).HasIdentity,
    38  		"shh_newIdentity":      (*shhApi).NewIdentity,
    39  		"shh_newFilter":        (*shhApi).NewFilter,
    40  		"shh_uninstallFilter":  (*shhApi).UninstallFilter,
    41  		"shh_getFilterChanges": (*shhApi).GetFilterChanges,
    42  	}
    43  )
    44  
    45  func newWhisperOfflineError(method string) error {
    46  	return shared.NewNotAvailableError(method, "whisper offline")
    47  }
    48  
    49  // net callback handler
    50  type shhhandler func(*shhApi, *shared.Request) (interface{}, error)
    51  
    52  // shh api provider
    53  type shhApi struct {
    54  	xeth     *xeth.XEth
    55  	ethereum *eth.Ethereum
    56  	methods  map[string]shhhandler
    57  	codec    codec.ApiCoder
    58  }
    59  
    60  // create a new whisper api instance
    61  func NewShhApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *shhApi {
    62  	return &shhApi{
    63  		xeth:     xeth,
    64  		ethereum: eth,
    65  		methods:  shhMapping,
    66  		codec:    coder.New(nil),
    67  	}
    68  }
    69  
    70  // collection with supported methods
    71  func (self *shhApi) Methods() []string {
    72  	methods := make([]string, len(self.methods))
    73  	i := 0
    74  	for k := range self.methods {
    75  		methods[i] = k
    76  		i++
    77  	}
    78  	return methods
    79  }
    80  
    81  // Execute given request
    82  func (self *shhApi) Execute(req *shared.Request) (interface{}, error) {
    83  	if callback, ok := self.methods[req.Method]; ok {
    84  		return callback(self, req)
    85  	}
    86  
    87  	return nil, shared.NewNotImplementedError(req.Method)
    88  }
    89  
    90  func (self *shhApi) Name() string {
    91  	return shared.ShhApiName
    92  }
    93  
    94  func (self *shhApi) ApiVersion() string {
    95  	return ShhApiVersion
    96  }
    97  
    98  func (self *shhApi) Version(req *shared.Request) (interface{}, error) {
    99  	w := self.xeth.Whisper()
   100  	if w == nil {
   101  		return nil, newWhisperOfflineError(req.Method)
   102  	}
   103  
   104  	return w.Version(), nil
   105  }
   106  
   107  func (self *shhApi) Post(req *shared.Request) (interface{}, error) {
   108  	w := self.xeth.Whisper()
   109  	if w == nil {
   110  		return nil, newWhisperOfflineError(req.Method)
   111  	}
   112  
   113  	args := new(WhisperMessageArgs)
   114  	if err := self.codec.Decode(req.Params, &args); err != nil {
   115  		return nil, err
   116  	}
   117  
   118  	err := w.Post(args.Payload, args.To, args.From, args.Topics, args.Priority, args.Ttl)
   119  	if err != nil {
   120  		return false, err
   121  	}
   122  
   123  	return true, nil
   124  }
   125  
   126  func (self *shhApi) HasIdentity(req *shared.Request) (interface{}, error) {
   127  	w := self.xeth.Whisper()
   128  	if w == nil {
   129  		return nil, newWhisperOfflineError(req.Method)
   130  	}
   131  
   132  	args := new(WhisperIdentityArgs)
   133  	if err := self.codec.Decode(req.Params, &args); err != nil {
   134  		return nil, err
   135  	}
   136  
   137  	return w.HasIdentity(args.Identity), nil
   138  }
   139  
   140  func (self *shhApi) NewIdentity(req *shared.Request) (interface{}, error) {
   141  	w := self.xeth.Whisper()
   142  	if w == nil {
   143  		return nil, newWhisperOfflineError(req.Method)
   144  	}
   145  
   146  	return w.NewIdentity(), nil
   147  }
   148  
   149  func (self *shhApi) NewFilter(req *shared.Request) (interface{}, error) {
   150  	args := new(WhisperFilterArgs)
   151  	if err := self.codec.Decode(req.Params, &args); err != nil {
   152  		return nil, err
   153  	}
   154  
   155  	id := self.xeth.NewWhisperFilter(args.To, args.From, args.Topics)
   156  	return newHexNum(big.NewInt(int64(id)).Bytes()), nil
   157  }
   158  
   159  func (self *shhApi) UninstallFilter(req *shared.Request) (interface{}, error) {
   160  	args := new(FilterIdArgs)
   161  	if err := self.codec.Decode(req.Params, &args); err != nil {
   162  		return nil, err
   163  	}
   164  	return self.xeth.UninstallWhisperFilter(args.Id), nil
   165  }
   166  
   167  func (self *shhApi) GetFilterChanges(req *shared.Request) (interface{}, error) {
   168  	w := self.xeth.Whisper()
   169  	if w == nil {
   170  		return nil, newWhisperOfflineError(req.Method)
   171  	}
   172  
   173  	// Retrieve all the new messages arrived since the last request
   174  	args := new(FilterIdArgs)
   175  	if err := self.codec.Decode(req.Params, &args); err != nil {
   176  		return nil, err
   177  	}
   178  
   179  	return self.xeth.WhisperMessagesChanged(args.Id), nil
   180  }
   181  
   182  func (self *shhApi) GetMessages(req *shared.Request) (interface{}, error) {
   183  	w := self.xeth.Whisper()
   184  	if w == nil {
   185  		return nil, newWhisperOfflineError(req.Method)
   186  	}
   187  
   188  	// Retrieve all the cached messages matching a specific, existing filter
   189  	args := new(FilterIdArgs)
   190  	if err := self.codec.Decode(req.Params, &args); err != nil {
   191  		return nil, err
   192  	}
   193  
   194  	return self.xeth.WhisperMessages(args.Id), nil
   195  }