NewID generates a identifier that can be used as an identifier in the RPC interface. e.g. filter and subscription identifier.
()
| 203 | // NewID generates a identifier that can be used as an identifier in the RPC interface. |
| 204 | // e.g. filter and subscription identifier. |
| 205 | func NewID() ID { |
| 206 | subscriptionIDGenMu.Lock() |
| 207 | defer subscriptionIDGenMu.Unlock() |
| 208 | |
| 209 | id := make([]byte, 16) |
| 210 | for i := 0; i < len(id); i += 7 { |
| 211 | val := subscriptionIDGen.Int63() |
| 212 | for j := 0; i+j < len(id) && j < 7; j++ { |
| 213 | id[i+j] = byte(val) |
| 214 | val >>= 8 |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | rpcId := hex.EncodeToString(id) |
| 219 | // rpc ID's are RPC quantities, no leading zero's and 0 is 0x0 |
| 220 | rpcId = strings.TrimLeft(rpcId, "0") |
| 221 | if rpcId == "" { |
| 222 | rpcId = "0" |
| 223 | } |
| 224 | |
| 225 | return ID("0x" + rpcId) |
| 226 | } |