github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/whisper/shhclient/client.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:45</date> 10 //</624450123815981056> 11 12 13 package shhclient 14 15 import ( 16 "context" 17 18 "github.com/ethereum/go-ethereum" 19 "github.com/ethereum/go-ethereum/common/hexutil" 20 "github.com/ethereum/go-ethereum/rpc" 21 whisper "github.com/ethereum/go-ethereum/whisper/whisperv6" 22 ) 23 24 //客户端为WhisperV6 RPC API定义类型化包装器。 25 type Client struct { 26 c *rpc.Client 27 } 28 29 //拨号将客户机连接到给定的URL。 30 func Dial(rawurl string) (*Client, error) { 31 c, err := rpc.Dial(rawurl) 32 if err != nil { 33 return nil, err 34 } 35 return NewClient(c), nil 36 } 37 38 //newclient创建使用给定RPC客户端的客户端。 39 func NewClient(c *rpc.Client) *Client { 40 return &Client{c} 41 } 42 43 //version返回耳语子协议版本。 44 func (sc *Client) Version(ctx context.Context) (string, error) { 45 var result string 46 err := sc.c.CallContext(ctx, &result, "shh_version") 47 return result, err 48 } 49 50 //信息返回关于耳语节点的诊断信息。 51 func (sc *Client) Info(ctx context.Context) (whisper.Info, error) { 52 var info whisper.Info 53 err := sc.c.CallContext(ctx, &info, "shh_info") 54 return info, err 55 } 56 57 //setmaxmessagesize设置此节点允许的最大消息大小。进来的 58 //较大的外发邮件将被拒绝。低语消息大小 59 //不能超过基础P2P协议(10 MB)所施加的限制。 60 func (sc *Client) SetMaxMessageSize(ctx context.Context, size uint32) error { 61 var ignored bool 62 return sc.c.CallContext(ctx, &ignored, "shh_setMaxMessageSize", size) 63 } 64 65 //setminimumPow(实验)设置此节点所需的最小功率。 66 //该实验函数被引入到未来的动态调节中。 67 //功率要求。如果节点被消息淹没,它应该引发 68 //POW要求并通知同行。新值应设置为相对于 69 //旧值(例如double)。旧值可以通过shh_info调用获得。 70 func (sc *Client) SetMinimumPoW(ctx context.Context, pow float64) error { 71 var ignored bool 72 return sc.c.CallContext(ctx, &ignored, "shh_setMinPoW", pow) 73 } 74 75 //marktrustedpeer标记特定的受信任的对等机,这将允许它发送历史(过期)消息。 76 //注意:此功能不添加新节点,节点需要作为对等节点存在。 77 func (sc *Client) MarkTrustedPeer(ctx context.Context, enode string) error { 78 var ignored bool 79 return sc.c.CallContext(ctx, &ignored, "shh_markTrustedPeer", enode) 80 } 81 82 //new key pair为消息解密和加密生成一个新的公钥和私钥对。 83 //它返回一个可用于引用键的标识符。 84 func (sc *Client) NewKeyPair(ctx context.Context) (string, error) { 85 var id string 86 return id, sc.c.CallContext(ctx, &id, "shh_newKeyPair") 87 } 88 89 //addprivatekey存储了密钥对,并返回其ID。 90 func (sc *Client) AddPrivateKey(ctx context.Context, key []byte) (string, error) { 91 var id string 92 return id, sc.c.CallContext(ctx, &id, "shh_addPrivateKey", hexutil.Bytes(key)) 93 } 94 95 //删除密钥对删除指定密钥。 96 func (sc *Client) DeleteKeyPair(ctx context.Context, id string) (string, error) { 97 var ignored bool 98 return id, sc.c.CallContext(ctx, &ignored, "shh_deleteKeyPair", id) 99 } 100 101 //hasKeyPair返回节点是否具有私钥或 102 //与给定ID匹配的密钥对。 103 func (sc *Client) HasKeyPair(ctx context.Context, id string) (bool, error) { 104 var has bool 105 return has, sc.c.CallContext(ctx, &has, "shh_hasKeyPair", id) 106 } 107 108 //public key返回密钥ID的公钥。 109 func (sc *Client) PublicKey(ctx context.Context, id string) ([]byte, error) { 110 var key hexutil.Bytes 111 return []byte(key), sc.c.CallContext(ctx, &key, "shh_getPublicKey", id) 112 } 113 114 //private key返回密钥ID的私钥。 115 func (sc *Client) PrivateKey(ctx context.Context, id string) ([]byte, error) { 116 var key hexutil.Bytes 117 return []byte(key), sc.c.CallContext(ctx, &key, "shh_getPrivateKey", id) 118 } 119 120 //NewSymmetricKey生成随机对称密钥并返回其标识符。 121 //可用于加密和解密双方都知道密钥的消息。 122 func (sc *Client) NewSymmetricKey(ctx context.Context) (string, error) { 123 var id string 124 return id, sc.c.CallContext(ctx, &id, "shh_newSymKey") 125 } 126 127 //addSymmetricKey存储密钥,并返回其标识符。 128 func (sc *Client) AddSymmetricKey(ctx context.Context, key []byte) (string, error) { 129 var id string 130 return id, sc.c.CallContext(ctx, &id, "shh_addSymKey", hexutil.Bytes(key)) 131 } 132 133 //generatesymmetrickeyfrompassword根据密码生成密钥,存储并返回其标识符。 134 func (sc *Client) GenerateSymmetricKeyFromPassword(ctx context.Context, passwd string) (string, error) { 135 var id string 136 return id, sc.c.CallContext(ctx, &id, "shh_generateSymKeyFromPassword", passwd) 137 } 138 139 //hassymmetrickey返回与给定ID关联的密钥是否存储在节点中的指示。 140 func (sc *Client) HasSymmetricKey(ctx context.Context, id string) (bool, error) { 141 var found bool 142 return found, sc.c.CallContext(ctx, &found, "shh_hasSymKey", id) 143 } 144 145 //GetSymmetricKey返回与给定标识符关联的对称密钥。 146 func (sc *Client) GetSymmetricKey(ctx context.Context, id string) ([]byte, error) { 147 var key hexutil.Bytes 148 return []byte(key), sc.c.CallContext(ctx, &key, "shh_getSymKey", id) 149 } 150 151 //DeleteSymmetricKey删除与给定标识符关联的对称密钥。 152 func (sc *Client) DeleteSymmetricKey(ctx context.Context, id string) error { 153 var ignored bool 154 return sc.c.CallContext(ctx, &ignored, "shh_deleteSymKey", id) 155 } 156 157 //在网络上发布消息。 158 func (sc *Client) Post(ctx context.Context, message whisper.NewMessage) (string, error) { 159 var hash string 160 return hash, sc.c.CallContext(ctx, &hash, "shh_post", message) 161 } 162 163 //订阅消息订阅与给定条件匹配的消息。这种方法 164 //仅在双向连接(如WebSockets和IPC)上受支持。 165 //NewMessageFilter使用轮询,并且通过HTTP支持。 166 func (sc *Client) SubscribeMessages(ctx context.Context, criteria whisper.Criteria, ch chan<- *whisper.Message) (ethereum.Subscription, error) { 167 return sc.c.ShhSubscribe(ctx, ch, "messages", criteria) 168 } 169 170 //newMessageFilter在节点内创建一个筛选器。此筛选器可用于轮询 171 //对于满足给定条件的新消息(请参阅filtermessages)。过滤器罐 172 //在whisper.filterTimeout中对其进行轮询时超时。 173 func (sc *Client) NewMessageFilter(ctx context.Context, criteria whisper.Criteria) (string, error) { 174 var id string 175 return id, sc.c.CallContext(ctx, &id, "shh_newMessageFilter", criteria) 176 } 177 178 //DeleteMessageFilter删除与给定ID关联的筛选器。 179 func (sc *Client) DeleteMessageFilter(ctx context.Context, id string) error { 180 var ignored bool 181 return sc.c.CallContext(ctx, &ignored, "shh_deleteMessageFilter", id) 182 } 183 184 //filtermessages检索上次调用之间接收的所有消息 185 //此函数与创建筛选器时给定的条件匹配。 186 func (sc *Client) FilterMessages(ctx context.Context, id string) ([]*whisper.Message, error) { 187 var messages []*whisper.Message 188 return messages, sc.c.CallContext(ctx, &messages, "shh_getFilterMessages", id) 189 } 190