@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package shhapi
package whisperv5
import (
"encoding/json"
@ -27,35 +27,20 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/whisper/whisperv5"
)
var whisperOffLineErr = errors . New ( "whisper is offline" )
// PublicWhisperAPI provides the whisper RPC service.
type PublicWhisperAPI struct {
whisper * whisperv5 . Whisper
whisper * Whisper
}
// NewPublicWhisperAPI create a new RPC whisper service.
func NewPublicWhisperAPI ( ) * PublicWhisperAPI {
w := whisperv5 . NewWhisper ( nil )
func NewPublicWhisperAPI ( w * Whisper ) * PublicWhisperAPI {
return & PublicWhisperAPI { whisper : w }
}
// APIs returns the RPC descriptors the Whisper implementation offers
func APIs ( ) [ ] rpc . API {
return [ ] rpc . API {
{
Namespace : whisperv5 . ProtocolName ,
Version : whisperv5 . ProtocolVersionStr ,
Service : NewPublicWhisperAPI ( ) ,
Public : true ,
} ,
}
}
// Start starts the Whisper worker threads.
func ( api * PublicWhisperAPI ) Start ( ) error {
if api . whisper == nil {
@ -171,11 +156,11 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (uint32, error) {
return 0 , whisperOffLineErr
}
filter := whisperv5 . Filter {
filter := Filter {
Src : crypto . ToECDSAPub ( common . FromHex ( args . From ) ) ,
KeySym : api . whisper . GetSymKey ( args . KeyName ) ,
PoW : args . PoW ,
Messages : make ( map [ common . Hash ] * whisperv5 . ReceivedMessage ) ,
Messages : make ( map [ common . Hash ] * ReceivedMessage ) ,
AcceptP2P : args . AcceptP2P ,
}
if len ( filter . KeySym ) > 0 {
@ -209,7 +194,7 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (uint32, error) {
if len ( args . To ) > 0 {
dst := crypto . ToECDSAPub ( common . FromHex ( args . To ) )
if ! whisperv5 . ValidatePublicKey ( dst ) {
if ! ValidatePublicKey ( dst ) {
info := "NewFilter: Invalid 'To' address"
glog . V ( logger . Error ) . Infof ( info )
return 0 , errors . New ( info )
@ -223,7 +208,7 @@ func (api *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (uint32, error) {
}
if len ( args . From ) > 0 {
if ! whisperv5 . ValidatePublicKey ( filter . Src ) {
if ! ValidatePublicKey ( filter . Src ) {
info := "NewFilter: Invalid 'From' address"
glog . V ( logger . Error ) . Infof ( info )
return 0 , errors . New ( info )
@ -256,7 +241,7 @@ func (api *PublicWhisperAPI) GetMessages(filterId uint32) []WhisperMessage {
}
// toWhisperMessages converts a Whisper message to a RPC whisper message.
func toWhisperMessages ( messages [ ] * whisperv5 . ReceivedMessage ) [ ] WhisperMessage {
func toWhisperMessages ( messages [ ] * ReceivedMessage ) [ ] WhisperMessage {
msgs := make ( [ ] WhisperMessage , len ( messages ) )
for i , msg := range messages {
msgs [ i ] = NewWhisperMessage ( msg )
@ -270,7 +255,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
return whisperOffLineErr
}
params := whisperv5 . MessageParams {
params := MessageParams {
TTL : args . TTL ,
Dst : crypto . ToECDSAPub ( common . FromHex ( args . To ) ) ,
KeySym : api . whisper . GetSymKey ( args . KeyName ) ,
@ -283,7 +268,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
if len ( args . From ) > 0 {
pub := crypto . ToECDSAPub ( common . FromHex ( args . From ) )
if ! whisperv5 . ValidatePublicKey ( pub ) {
if ! ValidatePublicKey ( pub ) {
info := "Post: Invalid 'From' address"
glog . V ( logger . Error ) . Infof ( info )
return errors . New ( info )
@ -311,7 +296,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
if params . Src == nil && filter . Src != nil {
params . Src = filter . KeyAsym
}
if ( params . Topic == whisperv5 . TopicType { } ) {
if ( params . Topic == TopicType { } ) {
sz := len ( filter . Topics )
if sz < 1 {
info := fmt . Sprintf ( "Post: no topics in filter # %d" , args . FilterID )
@ -347,7 +332,7 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
}
if len ( args . To ) > 0 {
if ! whisperv5 . ValidatePublicKey ( params . Dst ) {
if ! ValidatePublicKey ( params . Dst ) {
info := "Post: Invalid 'To' address"
glog . V ( logger . Error ) . Infof ( info )
return errors . New ( info )
@ -355,18 +340,18 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
}
// encrypt and send
message := whisperv5 . NewSentMessage ( & params )
message := NewSentMessage ( & params )
envelope , err := message . Wrap ( & params )
if err != nil {
glog . V ( logger . Error ) . Infof ( err . Error ( ) )
return err
}
if len ( envelope . Data ) > whisperv5 . MaxMessageLength {
if len ( envelope . Data ) > MaxMessageLength {
info := "Post: message is too big"
glog . V ( logger . Error ) . Infof ( info )
return errors . New ( info )
}
if ( envelope . Topic == whisperv5 . TopicType { } && envelope . IsSymmetric ( ) ) {
if ( envelope . Topic == TopicType { } && envelope . IsSymmetric ( ) ) {
info := "Post: topic is missing for symmetric encryption"
glog . V ( logger . Error ) . Infof ( info )
return errors . New ( info )
@ -380,17 +365,17 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
}
type PostArgs struct {
TTL uint32 ` json:"ttl" `
From string ` json:"from" `
To string ` json:"to" `
KeyName string ` json:"keyname" `
Topic whisperv5 . TopicType ` json:"topic" `
Padding hexutil . Bytes ` json:"padding" `
Payload hexutil . Bytes ` json:"payload" `
WorkTime uint32 ` json:"worktime" `
PoW float64 ` json:"pow" `
FilterID uint32 ` json:"filterID" `
PeerID hexutil . Bytes ` json:"peerID" `
TTL uint32 ` json:"ttl" `
From string ` json:"from" `
To string ` json:"to" `
KeyName string ` json:"keyname" `
Topic TopicType ` json:"topic" `
Padding hexutil . Bytes ` json:"padding" `
Payload hexutil . Bytes ` json:"payload" `
WorkTime uint32 ` json:"worktime" `
PoW float64 ` json:"pow" `
FilterID uint32 ` json:"filterID" `
PeerID hexutil . Bytes ` json:"peerID" `
}
type WhisperFilterArgs struct {
@ -398,7 +383,7 @@ type WhisperFilterArgs struct {
From string
KeyName string
PoW float64
Topics [ ] whisperv5 . TopicType
Topics [ ] TopicType
AcceptP2P bool
}
@ -437,13 +422,13 @@ func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
return fmt . Errorf ( "topic[%d] is not a string" , i )
}
}
topicsDecoded := make ( [ ] whisperv5 . TopicType , len ( topics ) )
topicsDecoded := make ( [ ] TopicType , len ( topics ) )
for j , s := range topics {
x := common . FromHex ( s )
if x == nil || len ( x ) != whisperv5 . TopicLength {
if x == nil || len ( x ) != TopicLength {
return fmt . Errorf ( "topic[%d] is invalid" , j )
}
topicsDecoded [ j ] = whisperv5 . BytesToTopic ( x )
topicsDecoded [ j ] = BytesToTopic ( x )
}
args . Topics = topicsDecoded
}
@ -464,7 +449,7 @@ type WhisperMessage struct {
}
// NewWhisperMessage converts an internal message into an API version.
func NewWhisperMessage ( message * whisperv5 . ReceivedMessage ) WhisperMessage {
func NewWhisperMessage ( message * ReceivedMessage ) WhisperMessage {
return WhisperMessage {
Payload : common . ToHex ( message . Payload ) ,
Padding : common . ToHex ( message . Padding ) ,