github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/whisper/shhclient/client.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 // 10 // 11 // 12 // 13 // 14 // 15 // 16 // 17 // 18 // 19 // 20 // 21 // 22 // 23 // 24 25 package shhclient 26 27 import ( 28 "context" 29 30 "github.com/ethereum/go-ethereum" 31 "github.com/ethereum/go-ethereum/common/hexutil" 32 "github.com/ethereum/go-ethereum/rpc" 33 whisper "github.com/ethereum/go-ethereum/whisper/whisperv6" 34 ) 35 36 // 37 type Client struct { 38 c *rpc.Client 39 } 40 41 // 42 func Dial(rawurl string) (*Client, error) { 43 c, err := rpc.Dial(rawurl) 44 if err != nil { 45 return nil, err 46 } 47 return NewClient(c), nil 48 } 49 50 // 51 func NewClient(c *rpc.Client) *Client { 52 return &Client{c} 53 } 54 55 // 56 func (sc *Client) Version(ctx context.Context) (string, error) { 57 var result string 58 err := sc.c.CallContext(ctx, &result, "shh_version") 59 return result, err 60 } 61 62 // 63 func (sc *Client) Info(ctx context.Context) (whisper.Info, error) { 64 var info whisper.Info 65 err := sc.c.CallContext(ctx, &info, "shh_info") 66 return info, err 67 } 68 69 // 70 // 71 // 72 func (sc *Client) SetMaxMessageSize(ctx context.Context, size uint32) error { 73 var ignored bool 74 return sc.c.CallContext(ctx, &ignored, "shh_setMaxMessageSize", size) 75 } 76 77 // 78 // 79 // 80 // 81 // 82 func (sc *Client) SetMinimumPoW(ctx context.Context, pow float64) error { 83 var ignored bool 84 return sc.c.CallContext(ctx, &ignored, "shh_setMinPoW", pow) 85 } 86 87 // 88 // 89 func (sc *Client) MarkTrustedPeer(ctx context.Context, enode string) error { 90 var ignored bool 91 return sc.c.CallContext(ctx, &ignored, "shh_markTrustedPeer", enode) 92 } 93 94 // 95 // 96 func (sc *Client) NewKeyPair(ctx context.Context) (string, error) { 97 var id string 98 return id, sc.c.CallContext(ctx, &id, "shh_newKeyPair") 99 } 100 101 // 102 func (sc *Client) AddPrivateKey(ctx context.Context, key []byte) (string, error) { 103 var id string 104 return id, sc.c.CallContext(ctx, &id, "shh_addPrivateKey", hexutil.Bytes(key)) 105 } 106 107 // 108 func (sc *Client) DeleteKeyPair(ctx context.Context, id string) (string, error) { 109 var ignored bool 110 return id, sc.c.CallContext(ctx, &ignored, "shh_deleteKeyPair", id) 111 } 112 113 // 114 // 115 func (sc *Client) HasKeyPair(ctx context.Context, id string) (bool, error) { 116 var has bool 117 return has, sc.c.CallContext(ctx, &has, "shh_hasKeyPair", id) 118 } 119 120 // 121 func (sc *Client) PublicKey(ctx context.Context, id string) ([]byte, error) { 122 var key hexutil.Bytes 123 return []byte(key), sc.c.CallContext(ctx, &key, "shh_getPublicKey", id) 124 } 125 126 // 127 func (sc *Client) PrivateKey(ctx context.Context, id string) ([]byte, error) { 128 var key hexutil.Bytes 129 return []byte(key), sc.c.CallContext(ctx, &key, "shh_getPrivateKey", id) 130 } 131 132 // 133 // 134 func (sc *Client) NewSymmetricKey(ctx context.Context) (string, error) { 135 var id string 136 return id, sc.c.CallContext(ctx, &id, "shh_newSymKey") 137 } 138 139 // 140 func (sc *Client) AddSymmetricKey(ctx context.Context, key []byte) (string, error) { 141 var id string 142 return id, sc.c.CallContext(ctx, &id, "shh_addSymKey", hexutil.Bytes(key)) 143 } 144 145 // 146 func (sc *Client) GenerateSymmetricKeyFromPassword(ctx context.Context, passwd string) (string, error) { 147 var id string 148 return id, sc.c.CallContext(ctx, &id, "shh_generateSymKeyFromPassword", passwd) 149 } 150 151 // 152 func (sc *Client) HasSymmetricKey(ctx context.Context, id string) (bool, error) { 153 var found bool 154 return found, sc.c.CallContext(ctx, &found, "shh_hasSymKey", id) 155 } 156 157 // 158 func (sc *Client) GetSymmetricKey(ctx context.Context, id string) ([]byte, error) { 159 var key hexutil.Bytes 160 return []byte(key), sc.c.CallContext(ctx, &key, "shh_getSymKey", id) 161 } 162 163 // 164 func (sc *Client) DeleteSymmetricKey(ctx context.Context, id string) error { 165 var ignored bool 166 return sc.c.CallContext(ctx, &ignored, "shh_deleteSymKey", id) 167 } 168 169 // 170 func (sc *Client) Post(ctx context.Context, message whisper.NewMessage) (string, error) { 171 var hash string 172 return hash, sc.c.CallContext(ctx, &hash, "shh_post", message) 173 } 174 175 // 176 // 177 // 178 func (sc *Client) SubscribeMessages(ctx context.Context, criteria whisper.Criteria, ch chan<- *whisper.Message) (ethereum.Subscription, error) { 179 return sc.c.ShhSubscribe(ctx, ch, "messages", criteria) 180 } 181 182 // 183 // 184 // 185 func (sc *Client) NewMessageFilter(ctx context.Context, criteria whisper.Criteria) (string, error) { 186 var id string 187 return id, sc.c.CallContext(ctx, &id, "shh_newMessageFilter", criteria) 188 } 189 190 // 191 func (sc *Client) DeleteMessageFilter(ctx context.Context, id string) error { 192 var ignored bool 193 return sc.c.CallContext(ctx, &ignored, "shh_deleteMessageFilter", id) 194 } 195 196 // 197 // 198 func (sc *Client) FilterMessages(ctx context.Context, id string) ([]*whisper.Message, error) { 199 var messages []*whisper.Message 200 return messages, sc.c.CallContext(ctx, &messages, "shh_getFilterMessages", id) 201 }