whisper: documenting the API functions

This commit is contained in:
Vlad 2017-03-28 17:01:02 +02:00
parent 2eec953e47
commit 87e00e508b
7 changed files with 40 additions and 40 deletions

View file

@ -358,7 +358,7 @@ func configureNode() {
Topics: [][]byte{topic}, Topics: [][]byte{topic},
AllowP2P: p2pAccept, AllowP2P: p2pAccept,
} }
filterID, err = shh.Watch(&filter) filterID, err = shh.Subscribe(&filter)
if err != nil { if err != nil {
utils.Fatalf("Failed to install filter: %s", err) utils.Fatalf("Failed to install filter: %s", err)
} }

View file

@ -261,7 +261,7 @@ func (api *PublicWhisperAPI) Subscribe(args WhisperFilterArgs) (string, error) {
} }
} }
return api.whisper.Watch(&filter) return api.whisper.Subscribe(&filter)
} }
// Unsubscribe disables and removes an existing filter. // Unsubscribe disables and removes an existing filter.
@ -380,25 +380,25 @@ func (api *PublicWhisperAPI) Post(args PostArgs) error {
} }
type PostArgs struct { type PostArgs struct {
Type string `json:"type"` Type string `json:"type"` // "sym"/"asym" (symmetric or asymmetric)
TTL uint32 `json:"ttl"` TTL uint32 `json:"ttl"` // time-to-live in seconds
SignWith string `json:"signWith"` SignWith string `json:"signWith"` // id of the signing key
Key string `json:"key"` Key string `json:"key"` // id of encryption key
Topic hexutil.Bytes `json:"topic"` Topic hexutil.Bytes `json:"topic"` // topic (4 bytes)
Padding hexutil.Bytes `json:"padding"` Padding hexutil.Bytes `json:"padding"` // optional padding bytes
Payload hexutil.Bytes `json:"payload"` Payload hexutil.Bytes `json:"payload"` // payload to be encrypted
PowTime uint32 `json:"powTime"` PowTime uint32 `json:"powTime"` // maximal time in seconds to be spent on PoW
PowTarget float64 `json:"powTarget"` PowTarget float64 `json:"powTarget"` // minimal PoW required for this message
TargetPeer string `json:"targetPeer"` TargetPeer string `json:"targetPeer"` // peer id (for p2p message only)
} }
type WhisperFilterArgs struct { type WhisperFilterArgs struct {
Symmetric bool Symmetric bool // encryption type
Key string Key string // id of the key to be used for decryption
SignedWith string SignedWith string // public key of the sender to be verified
MinPoW float64 MinPoW float64 // minimal PoW requirement
Topics [][]byte Topics [][]byte // list of topics (up to 4 bytes each) to match
AllowP2P bool AllowP2P bool // indicates wheather direct p2p messages are allowed for this filter
} }
// UnmarshalJSON implements the json.Unmarshaler interface, invoked to convert a // UnmarshalJSON implements the json.Unmarshaler interface, invoked to convert a

View file

@ -82,7 +82,7 @@ func (e *Envelope) isAsymmetric() bool {
} }
func (e *Envelope) Ver() uint64 { func (e *Envelope) Ver() uint64 {
return bytesToIntLittleEndian(e.Version) return bytesToUintLittleEndian(e.Version)
} }
// Seal closes the envelope by spending the requested amount of time as a proof // Seal closes the envelope by spending the requested amount of time as a proof

View file

@ -327,7 +327,7 @@ func (msg *ReceivedMessage) extractPadding(end int) (int, bool) {
paddingSize := 0 paddingSize := 0
sz := int(msg.Raw[0] & paddingMask) // number of bytes containing the entire size of padding, could be zero sz := int(msg.Raw[0] & paddingMask) // number of bytes containing the entire size of padding, could be zero
if sz != 0 { if sz != 0 {
paddingSize = int(bytesToIntLittleEndian(msg.Raw[1 : 1+sz])) paddingSize = int(bytesToUintLittleEndian(msg.Raw[1 : 1+sz]))
if paddingSize < sz || paddingSize+1 > end { if paddingSize < sz || paddingSize+1 > end {
return 0, false return 0, false
} }

View file

@ -120,7 +120,7 @@ func initialize(t *testing.T) {
topics = append(topics, sharedTopic) topics = append(topics, sharedTopic)
f := Filter{KeySym: sharedKey} f := Filter{KeySym: sharedKey}
f.Topics = [][]byte{topics[0][:]} f.Topics = [][]byte{topics[0][:]}
node.filerId, err = node.shh.Watch(&f) node.filerId, err = node.shh.Subscribe(&f)
if err != nil { if err != nil {
t.Fatalf("failed to install the filter: %s.", err) t.Fatalf("failed to install the filter: %s.", err)
} }

View file

@ -74,7 +74,6 @@ type Whisper struct {
} }
// New creates a Whisper client ready to communicate through the Ethereum P2P network. // New creates a Whisper client ready to communicate through the Ethereum P2P network.
// Param s should be passed if you want to implement mail server, otherwise nil.
func New() *Whisper { func New() *Whisper {
whisper := &Whisper{ whisper := &Whisper{
privateKeys: make(map[string]*ecdsa.PrivateKey), privateKeys: make(map[string]*ecdsa.PrivateKey),
@ -147,6 +146,7 @@ func (w *Whisper) SetMinimumPoW(val float64) error {
return nil return nil
} }
// getPeer retrieves peer by ID
func (w *Whisper) getPeer(peerID []byte) (*Peer, error) { func (w *Whisper) getPeer(peerID []byte) (*Peer, error) {
w.peerMu.Lock() w.peerMu.Lock()
defer w.peerMu.Unlock() defer w.peerMu.Unlock()
@ -159,8 +159,8 @@ func (w *Whisper) getPeer(peerID []byte) (*Peer, error) {
return nil, fmt.Errorf("Could not find peer with ID: %x", peerID) return nil, fmt.Errorf("Could not find peer with ID: %x", peerID)
} }
// MarkPeerTrusted marks specific peer trusted, which will allow it // AllowP2PMessagesFromPeer marks specific peer trusted,
// to send historic (expired) messages. // which will allow it to send historic (expired) messages.
func (w *Whisper) AllowP2PMessagesFromPeer(peerID []byte) error { func (w *Whisper) AllowP2PMessagesFromPeer(peerID []byte) error {
p, err := w.getPeer(peerID) p, err := w.getPeer(peerID)
if err != nil { if err != nil {
@ -198,7 +198,7 @@ func (w *Whisper) SendP2PDirect(peer *Peer, envelope *Envelope) error {
return p2p.Send(peer.ws, p2pCode, envelope) return p2p.Send(peer.ws, p2pCode, envelope)
} }
// NewIdentity generates a new cryptographic identity for the client, and injects // NewKeyPair generates a new cryptographic identity for the client, and injects
// it into the known identities for message decryption. Returns ID of the new key pair. // it into the known identities for message decryption. Returns ID of the new key pair.
func (w *Whisper) NewKeyPair() (string, error) { func (w *Whisper) NewKeyPair() (string, error) {
key, err := crypto.GenerateKey() key, err := crypto.GenerateKey()
@ -227,7 +227,7 @@ func (w *Whisper) NewKeyPair() (string, error) {
return id, nil return id, nil
} }
// DeleteIdentity deletes the specified key if it exists. // DeleteKeyPair deletes the specified key if it exists.
func (w *Whisper) DeleteKeyPair(key string) bool { func (w *Whisper) DeleteKeyPair(key string) bool {
w.keyMu.Lock() w.keyMu.Lock()
defer w.keyMu.Unlock() defer w.keyMu.Unlock()
@ -239,7 +239,7 @@ func (w *Whisper) DeleteKeyPair(key string) bool {
return false return false
} }
// HasIdentity checks if the the whisper node is configured with the private key // HasKeyPair checks if the the whisper node is configured with the private key
// of the specified public pair. // of the specified public pair.
func (w *Whisper) HasKeyPair(id string) bool { func (w *Whisper) HasKeyPair(id string) bool {
w.keyMu.RLock() w.keyMu.RLock()
@ -247,7 +247,7 @@ func (w *Whisper) HasKeyPair(id string) bool {
return w.privateKeys[id] != nil return w.privateKeys[id] != nil
} }
// GetIdentity retrieves the private key of the specified identity. // GetPrivateKey retrieves the private key of the specified identity.
func (w *Whisper) GetPrivateKey(id string) (*ecdsa.PrivateKey, error) { func (w *Whisper) GetPrivateKey(id string) (*ecdsa.PrivateKey, error) {
w.keyMu.RLock() w.keyMu.RLock()
defer w.keyMu.RUnlock() defer w.keyMu.RUnlock()
@ -370,9 +370,9 @@ func (w *Whisper) GetSymKey(id string) ([]byte, error) {
return nil, fmt.Errorf("non-existent key ID") return nil, fmt.Errorf("non-existent key ID")
} }
// Watch installs a new message handler used for filtering, decrypting // Subscribe installs a new message handler used for filtering, decrypting
// and subsequent storing of incoming messages. // and subsequent storing of incoming messages.
func (w *Whisper) Watch(f *Filter) (string, error) { func (w *Whisper) Subscribe(f *Filter) (string, error) {
return w.filters.Install(f) return w.filters.Install(f)
} }
@ -422,7 +422,7 @@ func (w *Whisper) Stop() error {
return nil return nil
} }
// handlePeer is called by the underlying P2P layer when the whisper sub-protocol // HandlePeer is called by the underlying P2P layer when the whisper sub-protocol
// connection is negotiated. // connection is negotiated.
func (wh *Whisper) HandlePeer(peer *p2p.Peer, rw p2p.MsgReadWriter) error { func (wh *Whisper) HandlePeer(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
// Create the new peer and start tracking it // Create the new peer and start tracking it
@ -696,7 +696,7 @@ func (w *Whisper) Stats() string {
return result return result
} }
// envelopes retrieves all the messages currently pooled by the node. // Envelopes retrieves all the messages currently pooled by the node.
func (w *Whisper) Envelopes() []*Envelope { func (w *Whisper) Envelopes() []*Envelope {
w.poolMu.RLock() w.poolMu.RLock()
defer w.poolMu.RUnlock() defer w.poolMu.RUnlock()
@ -781,8 +781,8 @@ func containsOnlyZeros(data []byte) bool {
return true return true
} }
// bytesToIntLittleEndian converts the slice to 64-bit unsigned integer. // bytesToUintLittleEndian converts the slice to 64-bit unsigned integer.
func bytesToIntLittleEndian(b []byte) (res uint64) { func bytesToUintLittleEndian(b []byte) (res uint64) {
mul := uint64(1) mul := uint64(1)
for i := 0; i < len(b); i++ { for i := 0; i < len(b); i++ {
res += uint64(b[i]) * mul res += uint64(b[i]) * mul
@ -791,8 +791,8 @@ func bytesToIntLittleEndian(b []byte) (res uint64) {
return res return res
} }
// BytesToIntBigEndian converts the slice to 64-bit unsigned integer. // BytesToUintBigEndian converts the slice to 64-bit unsigned integer.
func BytesToIntBigEndian(b []byte) (res uint64) { func BytesToUintBigEndian(b []byte) (res uint64) {
for i := 0; i < len(b); i++ { for i := 0; i < len(b); i++ {
res *= 256 res *= 256
res += uint64(b[i]) res += uint64(b[i])
@ -800,7 +800,7 @@ func BytesToIntBigEndian(b []byte) (res uint64) {
return res return res
} }
// DeriveSymmetricKey derives symmetric key material from the key or password. // deriveKeyMaterial derives symmetric key material from the key or password.
// pbkdf2 is used for security, in case people use password instead of randomly generated keys. // pbkdf2 is used for security, in case people use password instead of randomly generated keys.
func deriveKeyMaterial(key []byte, version uint64) (derivedKey []byte, err error) { func deriveKeyMaterial(key []byte, version uint64) (derivedKey []byte, err error) {
if version == 0 { if version == 0 {

View file

@ -92,8 +92,8 @@ func TestWhisperBasic(t *testing.T) {
} }
buf := []byte{0xFF, 0xE5, 0x80, 0x2, 0} buf := []byte{0xFF, 0xE5, 0x80, 0x2, 0}
le := bytesToIntLittleEndian(buf) le := bytesToUintLittleEndian(buf)
be := BytesToIntBigEndian(buf) be := BytesToUintBigEndian(buf)
if le != uint64(0x280e5ff) { if le != uint64(0x280e5ff) {
t.Fatalf("failed bytesToIntLittleEndian: %d.", le) t.Fatalf("failed bytesToIntLittleEndian: %d.", le)
} }
@ -565,7 +565,7 @@ func TestCustomization(t *testing.T) {
} }
// check w.messages() // check w.messages()
id, err := w.Watch(f) id, err := w.Subscribe(f)
time.Sleep(5 * time.Millisecond) time.Sleep(5 * time.Millisecond)
mail := f.Retrieve() mail := f.Retrieve()
if len(mail) > 0 { if len(mail) > 0 {