github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/swarm/pss/types.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:44</date> 10 //</624450117209952257> 11 12 13 package pss 14 15 import ( 16 "encoding/json" 17 "fmt" 18 "sync" 19 20 "github.com/ethereum/go-ethereum/common" 21 "github.com/ethereum/go-ethereum/common/hexutil" 22 "github.com/ethereum/go-ethereum/p2p" 23 "github.com/ethereum/go-ethereum/rlp" 24 "github.com/ethereum/go-ethereum/swarm/storage" 25 whisper "github.com/ethereum/go-ethereum/whisper/whisperv5" 26 ) 27 28 const ( 29 defaultWhisperTTL = 6000 30 ) 31 32 const ( 33 pssControlSym = 1 34 pssControlRaw = 1 << 1 35 ) 36 37 var ( 38 topicHashMutex = sync.Mutex{} 39 topicHashFunc = storage.MakeHashFunc("SHA256")() 40 rawTopic = Topic{} 41 ) 42 43 //主题是耳语主题类型的PSS封装 44 type Topic whisper.TopicType 45 46 func (t *Topic) String() string { 47 return hexutil.Encode(t[:]) 48 } 49 50 //marshaljson实现json.marshaler接口 51 func (t Topic) MarshalJSON() (b []byte, err error) { 52 return json.Marshal(t.String()) 53 } 54 55 //marshaljson实现json.marshaler接口 56 func (t *Topic) UnmarshalJSON(input []byte) error { 57 topicbytes, err := hexutil.Decode(string(input[1 : len(input)-1])) 58 if err != nil { 59 return err 60 } 61 copy(t[:], topicbytes) 62 return nil 63 } 64 65 //pssadress是[]字节的别名。它表示可变长度的地址 66 type PssAddress []byte 67 68 //marshaljson实现json.marshaler接口 69 func (a PssAddress) MarshalJSON() ([]byte, error) { 70 return json.Marshal(hexutil.Encode(a[:])) 71 } 72 73 //unmashaljson实现json.marshaler接口 74 func (a *PssAddress) UnmarshalJSON(input []byte) error { 75 b, err := hexutil.Decode(string(input[1 : len(input)-1])) 76 if err != nil { 77 return err 78 } 79 for _, bb := range b { 80 *a = append(*a, bb) 81 } 82 return nil 83 } 84 85 //保存用于缓存的消息摘要 86 type pssDigest [digestLength]byte 87 88 //隐藏对控件标志字节的按位操作 89 type msgParams struct { 90 raw bool 91 sym bool 92 } 93 94 func newMsgParamsFromBytes(paramBytes []byte) *msgParams { 95 if len(paramBytes) != 1 { 96 return nil 97 } 98 return &msgParams{ 99 raw: paramBytes[0]&pssControlRaw > 0, 100 sym: paramBytes[0]&pssControlSym > 0, 101 } 102 } 103 104 func (m *msgParams) Bytes() (paramBytes []byte) { 105 var b byte 106 if m.raw { 107 b |= pssControlRaw 108 } 109 if m.sym { 110 b |= pssControlSym 111 } 112 paramBytes = append(paramBytes, b) 113 return paramBytes 114 } 115 116 //PSSMSG封装通过PSS传输的消息。 117 type PssMsg struct { 118 To []byte 119 Control []byte 120 Expire uint32 121 Payload *whisper.Envelope 122 } 123 124 func newPssMsg(param *msgParams) *PssMsg { 125 return &PssMsg{ 126 Control: param.Bytes(), 127 } 128 } 129 130 //邮件被标记为原始/外部加密 131 func (msg *PssMsg) isRaw() bool { 132 return msg.Control[0]&pssControlRaw > 0 133 } 134 135 //消息被标记为对称加密 136 func (msg *PssMsg) isSym() bool { 137 return msg.Control[0]&pssControlSym > 0 138 } 139 140 //序列化消息以在缓存中使用 141 func (msg *PssMsg) serialize() []byte { 142 rlpdata, _ := rlp.EncodeToBytes(struct { 143 To []byte 144 Payload *whisper.Envelope 145 }{ 146 To: msg.To, 147 Payload: msg.Payload, 148 }) 149 return rlpdata 150 } 151 152 //pssmsg的字符串表示法 153 func (msg *PssMsg) String() string { 154 return fmt.Sprintf("PssMsg: Recipient: %x", common.ToHex(msg.To)) 155 } 156 157 //pssmsg的消息处理程序函数的签名 158 //此类型的实现将传递给PSS。请与主题一起注册, 159 type HandlerFunc func(msg []byte, p *p2p.Peer, asymmetric bool, keyid string) error 160 161 type handlerCaps struct { 162 raw bool 163 prox bool 164 } 165 166 //处理程序定义接收内容时要执行的代码。 167 type handler struct { 168 f HandlerFunc 169 caps *handlerCaps 170 } 171 172 //NewHandler返回新的消息处理程序 173 func NewHandler(f HandlerFunc) *handler { 174 return &handler{ 175 f: f, 176 caps: &handlerCaps{}, 177 } 178 } 179 180 //withraw是一种可链接的方法,允许处理原始消息。 181 func (h *handler) WithRaw() *handler { 182 h.caps.raw = true 183 return h 184 } 185 186 //WithProxbin是一种可链接的方法,它允许使用Kademlia深度作为参考向邻近地区发送具有完整地址的消息。 187 func (h *handler) WithProxBin() *handler { 188 h.caps.prox = true 189 return h 190 } 191 192 //StateStore处理保存和加载PSS对等机及其相应的密钥。 193 //目前尚未实施 194 type stateStore struct { 195 values map[string][]byte 196 } 197 198 func (store *stateStore) Load(key string) ([]byte, error) { 199 return nil, nil 200 } 201 202 func (store *stateStore) Save(key string, v []byte) error { 203 return nil 204 } 205 206 //bytestoopic散列任意长度的字节片,并将其截断为主题的长度,只使用摘要的第一个字节。 207 func BytesToTopic(b []byte) Topic { 208 topicHashMutex.Lock() 209 defer topicHashMutex.Unlock() 210 topicHashFunc.Reset() 211 topicHashFunc.Write(b) 212 return Topic(whisper.BytesToTopic(topicHashFunc.Sum(nil))) 213 } 214