2018-04-11 15:41:51 +00:00
|
|
|
package shhext
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-04-24 15:50:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2018-04-11 15:41:51 +00:00
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewPublicAPI returns instance of the public API.
|
|
|
|
func NewPublicAPI(w *whisper.Whisper, tracker *tracker) *PublicAPI {
|
|
|
|
return &PublicAPI{
|
2018-04-24 15:50:26 +00:00
|
|
|
w: w,
|
|
|
|
publicAPI: whisper.NewPublicWhisperAPI(w),
|
|
|
|
tracker: tracker,
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PublicAPI extends whisper public API.
|
|
|
|
type PublicAPI struct {
|
2018-04-24 15:50:26 +00:00
|
|
|
w *whisper.Whisper
|
|
|
|
publicAPI *whisper.PublicWhisperAPI
|
|
|
|
tracker *tracker
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post shamelessly copied from whisper codebase with slight modifications.
|
2018-04-24 15:50:26 +00:00
|
|
|
func (api *PublicAPI) Post(ctx context.Context, req whisper.NewMessage) (hash hexutil.Bytes, err error) {
|
|
|
|
hash, err = api.publicAPI.Post(ctx, req)
|
2018-04-11 15:41:51 +00:00
|
|
|
if err == nil {
|
2018-04-24 15:50:26 +00:00
|
|
|
var envHash common.Hash
|
|
|
|
copy(envHash[:], hash[:]) // slice can't be used as key
|
|
|
|
api.tracker.Add(envHash)
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
return hash, err
|
|
|
|
}
|