Integrate Nimbus status-protocol-go

This commit is contained in:
Pedro Pombeiro 2019-10-28 14:50:33 +01:00 committed by Pedro Pombeiro
parent c199c8f342
commit 2dd74da23d
49 changed files with 322 additions and 302 deletions

View File

@ -14,12 +14,12 @@ import (
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/pborman/uuid"
"github.com/status-im/status-go/account/generator"
"github.com/status-im/status-go/extkeys"
statusproto "github.com/status-im/status-protocol-go/types"
)
// errors
@ -325,7 +325,7 @@ func (m *Manager) ImportSingleExtendedKey(extKey *extkeys.ExtendedKey, password
return address, "", err
}
pubKey = hexutil.Encode(crypto.FromECDSAPub(&key.PrivateKey.PublicKey))
pubKey = statusproto.EncodeHex(crypto.FromECDSAPub(&key.PrivateKey.PublicKey))
return
}
@ -349,7 +349,7 @@ func (m *Manager) importExtendedKey(keyPurpose extkeys.KeyPurpose, extKey *extke
if err != nil {
return address, "", err
}
pubKey = hexutil.Encode(crypto.FromECDSAPub(&key.PrivateKey.PublicKey))
pubKey = statusproto.EncodeHex(crypto.FromECDSAPub(&key.PrivateKey.PublicKey))
return
}

View File

@ -1,8 +1,8 @@
package account
import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
statusproto "github.com/status-im/status-protocol-go/types"
)
func CreateAddress() (address, pubKey, privKey string, err error) {
@ -15,8 +15,8 @@ func CreateAddress() (address, pubKey, privKey string, err error) {
pubKeyBytes := crypto.FromECDSAPub(&key.PublicKey)
addressBytes := crypto.PubkeyToAddress(key.PublicKey)
privKey = hexutil.Encode(privKeyBytes)
pubKey = hexutil.Encode(pubKeyBytes)
privKey = statusproto.EncodeHex(privKeyBytes)
pubKey = statusproto.EncodeHex(pubKeyBytes)
address = addressBytes.Hex()
return

View File

@ -3,9 +3,9 @@ package generator
import (
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/status-im/status-go/extkeys"
statusproto "github.com/status-im/status-protocol-go/types"
)
type account struct {
@ -14,7 +14,7 @@ type account struct {
}
func (a *account) toAccountInfo() AccountInfo {
publicKeyHex := hexutil.Encode(crypto.FromECDSAPub(&a.privateKey.PublicKey))
publicKeyHex := statusproto.EncodeHex(crypto.FromECDSAPub(&a.privateKey.PublicKey))
addressHex := crypto.PubkeyToAddress(a.privateKey.PublicKey).Hex()
return AccountInfo{

View File

@ -3,11 +3,11 @@ package account
import (
"fmt"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/pborman/uuid"
"github.com/status-im/status-go/account/generator"
"github.com/status-im/status-go/extkeys"
statusproto "github.com/status-im/status-protocol-go/types"
)
// OnboardingAccount is returned during onboarding and contains its ID and the mnemonic to re-generate the same account Info keys.
@ -107,7 +107,7 @@ func (o *Onboarding) deriveAccount(masterExtendedKey *extkeys.ExtendedKey, purpo
privateKeyECDSA := extendedKey.ToECDSA()
address := crypto.PubkeyToAddress(privateKeyECDSA.PublicKey)
publicKeyHex := hexutil.Encode(crypto.FromECDSAPub(&privateKeyECDSA.PublicKey))
publicKeyHex := statusproto.EncodeHex(crypto.FromECDSAPub(&privateKeyECDSA.PublicKey))
return address.Hex(), publicKeyHex, nil
}

View File

@ -40,6 +40,7 @@ import (
"github.com/status-im/status-go/services/wallet"
"github.com/status-im/status-go/signal"
"github.com/status-im/status-go/transactions"
statusproto "github.com/status-im/status-protocol-go/types"
)
const (
@ -76,7 +77,7 @@ type StatusBackend struct {
allowAllRPC bool // used only for tests, disables api method restrictions
}
// NewStatusBackend create a new NewStatusBackend instance
// NewStatusBackend create a new StatusBackend instance
func NewStatusBackend() *StatusBackend {
defer log.Info("Status backend initialized", "version", params.Version, "commit", params.GitCommit)
@ -153,7 +154,7 @@ func (b *StatusBackend) GetAccounts() ([]multiaccounts.Account, error) {
b.mu.Lock()
defer b.mu.Unlock()
if b.multiaccountsDB == nil {
return nil, errors.New("accoutns db wasn't initialized")
return nil, errors.New("accounts db wasn't initialized")
}
return b.multiaccountsDB.GetAccounts()
}
@ -162,7 +163,7 @@ func (b *StatusBackend) SaveAccount(account multiaccounts.Account) error {
b.mu.Lock()
defer b.mu.Unlock()
if b.multiaccountsDB == nil {
return errors.New("accoutns db wasn't initialized")
return errors.New("accounts db wasn't initialized")
}
return b.multiaccountsDB.SaveAccount(account)
}
@ -1021,6 +1022,6 @@ func (b *StatusBackend) SignHash(hexEncodedHash string) (string, error) {
return "", fmt.Errorf("SignHash: could not sign the hash: %v", err)
}
hexEncodedSignature := hexutil.Encode(signature)
hexEncodedSignature := statusproto.EncodeHex(signature)
return hexEncodedSignature, nil
}

2
go.mod
View File

@ -31,7 +31,7 @@ require (
github.com/russolsen/transit v0.0.0-20180705123435-0794b4c4505a
github.com/status-im/migrate/v4 v4.6.2-status.2
github.com/status-im/rendezvous v1.3.0
github.com/status-im/status-protocol-go v0.5.1
github.com/status-im/status-protocol-go v0.5.2
github.com/status-im/tcp-shaker v0.0.0-20191114194237-215893130501
github.com/status-im/whisper v1.6.1
github.com/stretchr/testify v1.4.0

7
go.sum
View File

@ -416,6 +416,9 @@ github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-pointer v0.0.0-20190911064623-a0a44394634f h1:QTRRO+ozoYgT3CQRIzNVYJRU3DB8HRnkZv6mr4ISmMA=
github.com/mattn/go-pointer v0.0.0-20190911064623-a0a44394634f/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=
github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
@ -596,8 +599,8 @@ github.com/status-im/migrate/v4 v4.6.2-status.2 h1:SdC+sMDl/aI7vUlwD2qj2p7KsK4T6
github.com/status-im/migrate/v4 v4.6.2-status.2/go.mod h1:c/kc90n47GZu/58nnz1OMLTf7uE4Da4gZP5qmU+A/v8=
github.com/status-im/rendezvous v1.3.0 h1:7RK/MXXW+tlm0asKm1u7Qp7Yni6AO29a7j8+E4Lbjg4=
github.com/status-im/rendezvous v1.3.0/go.mod h1:+hzjuP+j/XzLPeF6E50b88pWOTLdTcwjvNYt+Gh1W1s=
github.com/status-im/status-protocol-go v0.5.1 h1:mCqYJrL/zWMScFjSLdboL5WANLn01Cz8bAxBwPxww7k=
github.com/status-im/status-protocol-go v0.5.1/go.mod h1:KR/eihnrUq2dZegUOVjrA/1poSNhasA/o82VYyRgeB0=
github.com/status-im/status-protocol-go v0.5.2 h1:C6m6N6TLzJbuJmV4u8iNzs0cj+Q1CfBWdS0LZLtGkN8=
github.com/status-im/status-protocol-go v0.5.2/go.mod h1:L5/7fKnycEBOiLm3TuCHDUNcn0kNNhSNsYLkqbUQngg=
github.com/status-im/tcp-shaker v0.0.0-20191114194237-215893130501 h1:oa0KU5jJRNtXaM/P465MhvSFo/HM2O8qi2DDuPcd7ro=
github.com/status-im/tcp-shaker v0.0.0-20191114194237-215893130501/go.mod h1:RYo/itke1oU5k/6sj9DNM3QAwtE5rZSgg5JnkOv83hk=
github.com/status-im/whisper v1.5.2 h1:26NgiKusmPic38eQdtXnaY+iaQ/LuQ3Dh0kCGYT/Uxs=

View File

@ -24,20 +24,18 @@ import (
"time"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/event"
"github.com/status-im/status-go/account"
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/signal"
. "github.com/status-im/status-go/t/utils" //nolint: golint
"github.com/status-im/status-go/transactions"
statusproto "github.com/status-im/status-protocol-go/types"
"github.com/stretchr/testify/require"
"github.com/ethereum/go-ethereum/event"
)
import (
"github.com/ethereum/go-ethereum/common"
"github.com/status-im/status-go/multiaccounts/accounts"
)
const initJS = `
@ -463,7 +461,7 @@ func testLoginWithKeycard(t *testing.T, feed *event.Feed) bool { //nolint: gocyc
t.Errorf("whisper service not running: %v", err)
}
chatPubKeyHex := hexutil.Encode(crypto.FromECDSAPub(&chatPrivKey.PublicKey))
chatPubKeyHex := statusproto.EncodeHex(crypto.FromECDSAPub(&chatPrivKey.PublicKey))
if whisperService.HasKeyPair(chatPubKeyHex) {
t.Error("identity already present in whisper")
return false

View File

@ -5,8 +5,9 @@ import (
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
statusproto "github.com/status-im/status-protocol-go/types"
whisper "github.com/status-im/whisper/whisperv6"
"github.com/stretchr/testify/require"
"github.com/syndtr/goleveldb/leveldb"
@ -117,8 +118,8 @@ func testMessagesCount(t *testing.T, expected int, s *WMailServer) {
func countMessages(t *testing.T, db DB) int {
var (
count int
zero common.Hash
emptyTopic whisper.TopicType
zero statusproto.Hash
emptyTopic whispertypes.TopicType
)
now := time.Now()

View File

@ -4,14 +4,14 @@ import (
"encoding/binary"
"errors"
"github.com/ethereum/go-ethereum/common"
whisper "github.com/status-im/whisper/whisperv6"
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
statusproto "github.com/status-im/status-protocol-go/types"
)
const (
// DBKeyLength is a size of the envelope key.
DBKeyLength = common.HashLength + timestampLength + whisper.TopicLength
CursorLength = common.HashLength + timestampLength
DBKeyLength = statusproto.HashLength + timestampLength + whispertypes.TopicLength
CursorLength = statusproto.HashLength + timestampLength
)
var (
@ -30,12 +30,12 @@ func (k *DBKey) Bytes() []byte {
return k.raw
}
func (k *DBKey) Topic() whisper.TopicType {
return whisper.BytesToTopic(k.raw[timestampLength+common.HashLength:])
func (k *DBKey) Topic() whispertypes.TopicType {
return whispertypes.BytesToTopic(k.raw[timestampLength+statusproto.HashLength:])
}
func (k *DBKey) EnvelopeHash() common.Hash {
return common.BytesToHash(k.raw[timestampLength : common.HashLength+timestampLength])
func (k *DBKey) EnvelopeHash() statusproto.Hash {
return statusproto.BytesToHash(k.raw[timestampLength : statusproto.HashLength+timestampLength])
}
func (k *DBKey) Cursor() []byte {
@ -44,11 +44,11 @@ func (k *DBKey) Cursor() []byte {
}
// NewDBKey creates a new DBKey with the given values.
func NewDBKey(timestamp uint32, topic whisper.TopicType, h common.Hash) *DBKey {
func NewDBKey(timestamp uint32, topic whispertypes.TopicType, h statusproto.Hash) *DBKey {
var k DBKey
k.raw = make([]byte, DBKeyLength)
binary.BigEndian.PutUint32(k.raw, timestamp)
copy(k.raw[timestampLength:], h[:])
copy(k.raw[timestampLength+common.HashLength:], topic[:])
copy(k.raw[timestampLength+statusproto.HashLength:], topic[:])
return &k
}

View File

@ -1,16 +1,17 @@
package mailserver
import (
"github.com/ethereum/go-ethereum/common"
whisper "github.com/status-im/whisper/whisperv6"
"github.com/stretchr/testify/require"
"testing"
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
statusproto "github.com/status-im/status-protocol-go/types"
"github.com/stretchr/testify/require"
)
func TestNewDBKey(t *testing.T) {
topic := whisper.BytesToTopic([]byte{0x01, 0x02, 0x03, 0x04})
topic := whispertypes.BytesToTopic([]byte{0x01, 0x02, 0x03, 0x04})
hash := common.BytesToHash([]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32})
hash := statusproto.BytesToHash([]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32})
dbKey := NewDBKey(0xabcdef12, topic, hash)
expected := []byte{0xab, 0xcd, 0xef, 0x12, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31, 0x32, 0x01, 0x02, 0x03, 0x04}
require.Equal(t, expected, dbKey.Bytes())

View File

@ -30,6 +30,8 @@ import (
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/status-im/status-go/params"
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
statusproto "github.com/status-im/status-protocol-go/types"
whisper "github.com/status-im/whisper/whisperv6"
prom "github.com/prometheus/client_golang/prometheus"
@ -197,7 +199,7 @@ func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope)
return
}
requestID := request.Hash()
requestID := statusproto.Hash(request.Hash())
peerID := peerIDString(peer)
log.Info("[mailserver:DeliverMail] delivering mail",
@ -336,7 +338,7 @@ func (s *WMailServer) DeliverMail(peer *whisper.Peer, request *whisper.Envelope)
"last", lastEnvelopeHash,
"next", nextPageCursor)
if err := s.sendHistoricMessageResponse(peer, request.Hash(), lastEnvelopeHash, nextPageCursor); err != nil {
if err := s.sendHistoricMessageResponse(peer, requestID, lastEnvelopeHash, nextPageCursor); err != nil {
deliveryFailuresCounter.WithLabelValues("historic_msg_resp").Inc()
log.Error("[mailserver:DeliverMail] error sending historic message response",
"err", err,
@ -354,7 +356,7 @@ func (s *WMailServer) Deliver(peer *whisper.Peer, r whisper.MessagesRequest) {
deliveryAttemptsCounter.Inc()
var (
requestIDHash = common.BytesToHash(r.ID)
requestIDHash = statusproto.BytesToHash(r.ID)
requestIDStr = requestIDHash.String()
peerID = peerIDString(peer)
err error
@ -590,8 +592,8 @@ func (s *WMailServer) exceedsPeerRequests(peer []byte) bool {
func (s *WMailServer) createIterator(lower, upper uint32, cursor []byte, bloom []byte, limit uint32) (Iterator, error) {
var (
emptyHash common.Hash
emptyTopic whisper.TopicType
emptyHash statusproto.Hash
emptyTopic whispertypes.TopicType
ku, kl *DBKey
)
@ -618,7 +620,7 @@ func (s *WMailServer) processRequestInBundles(
requestID string,
output chan<- []rlp.RawValue,
cancel <-chan struct{},
) ([]byte, common.Hash) {
) ([]byte, statusproto.Hash) {
timer := prom.NewTimer(requestsInBundlesDuration)
defer timer.ObserveDuration()
@ -629,7 +631,7 @@ func (s *WMailServer) processRequestInBundles(
processedEnvelopes int
processedEnvelopesSize int64
nextCursor []byte
lastEnvelopeHash common.Hash
lastEnvelopeHash statusproto.Hash
)
log.Info("[mailserver:processRequestInBundles] processing request",
@ -758,14 +760,14 @@ func (s *WMailServer) sendRawEnvelopes(peer *whisper.Peer, envelopes []rlp.RawVa
return nil
}
func (s *WMailServer) sendHistoricMessageResponse(peer *whisper.Peer, requestID, lastEnvelopeHash common.Hash, cursor []byte) error {
payload := whisper.CreateMailServerRequestCompletedPayload(requestID, lastEnvelopeHash, cursor)
func (s *WMailServer) sendHistoricMessageResponse(peer *whisper.Peer, requestID, lastEnvelopeHash statusproto.Hash, cursor []byte) error {
payload := whisper.CreateMailServerRequestCompletedPayload(common.Hash(requestID), common.Hash(lastEnvelopeHash), cursor)
return s.w.SendHistoricMessageResponse(peer, payload)
}
// this method doesn't return an error because it is already in the error handling chain
func (s *WMailServer) trySendHistoricMessageErrorResponse(peer *whisper.Peer, requestID common.Hash, errorToReport error) {
payload := whisper.CreateMailServerRequestFailedPayload(requestID, errorToReport)
func (s *WMailServer) trySendHistoricMessageErrorResponse(peer *whisper.Peer, requestID statusproto.Hash, errorToReport error) {
payload := whisper.CreateMailServerRequestFailedPayload(common.Hash(requestID), errorToReport)
err := s.w.SendHistoricMessageResponse(peer, payload)
// if we can't report an error, probably something is wrong with p2p connection,

View File

@ -10,7 +10,7 @@ import (
type DB interface {
Close() error
// SaveEnvelope stores an envelope
SaveEnvelope(*whisper.Envelope) error
SaveEnvelope(*whisper.Envelope) error // TODO: Migrate to whispertypes.Envelope
// GetEnvelope returns an rlp encoded envelope from the datastore
GetEnvelope(*DBKey) ([]byte, error)
// Prune removes envelopes older than time

View File

@ -2,16 +2,18 @@ package mailserver
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/status-im/status-go/params"
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
statusproto "github.com/status-im/status-protocol-go/types"
whisper "github.com/status-im/whisper/whisperv6"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors"
"github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/util"
"time"
)
type LevelDB struct {
@ -46,9 +48,9 @@ func (i *LevelDBIterator) GetEnvelope(bloom []byte) ([]byte, error) {
return nil, err
}
} else {
envelopeBloom = whisper.TopicToBloom(key.Topic())
envelopeBloom = whispertypes.TopicToBloom(key.Topic())
}
if !whisper.BloomFilterMatch(bloom, envelopeBloom) {
if !whispertypes.BloomFilterMatch(bloom, envelopeBloom) {
return nil, nil
}
return rawValue, nil
@ -88,8 +90,8 @@ func (db *LevelDB) GetEnvelope(key *DBKey) ([]byte, error) {
func (db *LevelDB) Prune(t time.Time, batchSize int) (int, error) {
defer recoverLevelDBPanics("Prune")
var zero common.Hash
var emptyTopic whisper.TopicType
var zero statusproto.Hash
var emptyTopic whispertypes.TopicType
kl := NewDBKey(0, emptyTopic, zero)
ku := NewDBKey(uint32(t.Unix()), emptyTopic, zero)
query := CursorQuery{
@ -138,7 +140,7 @@ func (db *LevelDB) Prune(t time.Time, batchSize int) (int, error) {
func (db *LevelDB) SaveEnvelope(env *whisper.Envelope) error {
defer recoverLevelDBPanics("SaveEnvelope")
key := NewDBKey(env.Expiry-env.TTL, env.Topic, env.Hash())
key := NewDBKey(env.Expiry-env.TTL, whispertypes.TopicType(env.Topic), statusproto.Hash(env.Hash()))
rawEnvelope, err := rlp.EncodeToBytes(env)
if err != nil {
log.Error(fmt.Sprintf("rlp.EncodeToBytes failed: %s", err))

View File

@ -9,13 +9,14 @@ import (
_ "github.com/lib/pq"
"github.com/status-im/migrate/v4"
"github.com/status-im/migrate/v4/database/postgres"
"github.com/status-im/migrate/v4/source/go_bindata"
bindata "github.com/status-im/migrate/v4/source/go_bindata"
"github.com/status-im/status-go/mailserver/migrations"
"github.com/status-im/status-go/params"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
statusproto "github.com/status-im/status-protocol-go/types"
whisper "github.com/status-im/whisper/whisperv6"
)
@ -158,8 +159,8 @@ func (i *PostgresDB) GetEnvelope(key *DBKey) ([]byte, error) {
}
func (i *PostgresDB) Prune(t time.Time, batch int) (int, error) {
var zero common.Hash
var emptyTopic whisper.TopicType
var zero statusproto.Hash
var emptyTopic whispertypes.TopicType
kl := NewDBKey(0, emptyTopic, zero)
ku := NewDBKey(uint32(t.Unix()), emptyTopic, zero)
statement := "DELETE FROM envelopes WHERE id BETWEEN $1 AND $2"
@ -178,7 +179,8 @@ func (i *PostgresDB) Prune(t time.Time, batch int) (int, error) {
}
func (i *PostgresDB) SaveEnvelope(env *whisper.Envelope) error {
key := NewDBKey(env.Expiry-env.TTL, env.Topic, env.Hash())
topic := whispertypes.TopicType(env.Topic)
key := NewDBKey(env.Expiry-env.TTL, topic, statusproto.Hash(env.Hash()))
rawEnvelope, err := rlp.EncodeToBytes(env)
if err != nil {
log.Error(fmt.Sprintf("rlp.EncodeToBytes failed: %s", err))
@ -198,7 +200,7 @@ func (i *PostgresDB) SaveEnvelope(env *whisper.Envelope) error {
_, err = stmt.Exec(
key.Bytes(),
rawEnvelope,
topicToByte(env.Topic),
topicToByte(topic),
)
if err != nil {
@ -212,7 +214,7 @@ func (i *PostgresDB) SaveEnvelope(env *whisper.Envelope) error {
return nil
}
func topicToByte(t whisper.TopicType) []byte {
func topicToByte(t whispertypes.TopicType) []byte {
return []byte{t[0], t[1], t[2], t[3]}
}

View File

@ -33,6 +33,8 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/status-im/status-go/params"
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
statusproto "github.com/status-im/status-protocol-go/types"
whisper "github.com/status-im/whisper/whisperv6"
"github.com/stretchr/testify/suite"
)
@ -44,7 +46,7 @@ var seed = time.Now().Unix()
var testPayload = []byte("test payload")
type ServerTestParams struct {
topic whisper.TopicType
topic whispertypes.TopicType
birth uint32
low uint32
upp uint32
@ -257,7 +259,7 @@ func (s *MailserverSuite) TestArchive() {
s.NoError(err)
s.server.Archive(env)
key := NewDBKey(env.Expiry-env.TTL, env.Topic, env.Hash())
key := NewDBKey(env.Expiry-env.TTL, whispertypes.TopicType(env.Topic), statusproto.Hash(env.Hash()))
archivedEnvelope, err := s.server.db.GetEnvelope(key)
s.NoError(err)
@ -277,8 +279,8 @@ func (s *MailserverSuite) TestManageLimits() {
}
func (s *MailserverSuite) TestDBKey() {
var h common.Hash
var emptyTopic whisper.TopicType
var h statusproto.Hash
var emptyTopic whispertypes.TopicType
i := uint32(time.Now().Unix())
k := NewDBKey(i, emptyTopic, h)
s.Equal(len(k.Bytes()), DBKeyLength, "wrong DB key length")
@ -305,7 +307,7 @@ func (s *MailserverSuite) TestRequestPaginationLimit() {
env, err := generateEnvelope(sentTime)
s.NoError(err)
s.server.Archive(env)
key := NewDBKey(env.Expiry-env.TTL, env.Topic, env.Hash())
key := NewDBKey(env.Expiry-env.TTL, whispertypes.TopicType(env.Topic), statusproto.Hash(env.Hash()))
archiveKeys = append(archiveKeys, fmt.Sprintf("%x", key.Cursor()))
sentEnvelopes = append(sentEnvelopes, env)
sentHashes = append(sentHashes, env.Hash())
@ -421,7 +423,7 @@ func (s *MailserverSuite) TestMailServer() {
s.Equal(tc.params.low, lower)
s.Equal(tc.params.upp, upper)
s.Equal(tc.params.limit, limit)
s.Equal(whisper.TopicToBloom(tc.params.topic), bloom)
s.Equal(whispertypes.TopicToBloom(tc.params.topic), bloom)
s.Equal(tc.expect, s.messageExists(env, tc.params.low, tc.params.upp, bloom, tc.params.limit))
src[0]++
@ -452,7 +454,7 @@ func (s *MailserverSuite) TestDecodeRequest() {
srcKey, err := s.shh.GetPrivateKey(id)
s.Require().NoError(err)
env := s.createEnvelope(whisper.TopicType{0x01}, data, srcKey)
env := s.createEnvelope(whispertypes.TopicType{0x01}, data, srcKey)
decodedPayload, err := s.server.decodeRequest(nil, env)
s.Require().NoError(err)
@ -478,7 +480,7 @@ func (s *MailserverSuite) TestDecodeRequestNoUpper() {
srcKey, err := s.shh.GetPrivateKey(id)
s.Require().NoError(err)
env := s.createEnvelope(whisper.TopicType{0x01}, data, srcKey)
env := s.createEnvelope(whispertypes.TopicType{0x01}, data, srcKey)
decodedPayload, err := s.server.decodeRequest(nil, env)
s.Require().NoError(err)
@ -684,7 +686,7 @@ func (s *MailserverSuite) defaultServerParams(env *whisper.Envelope) *ServerTest
birth := env.Expiry - env.TTL
return &ServerTestParams{
topic: env.Topic,
topic: whispertypes.TopicType(env.Topic),
birth: birth,
low: birth - 1,
upp: birth + 1,
@ -694,7 +696,7 @@ func (s *MailserverSuite) defaultServerParams(env *whisper.Envelope) *ServerTest
}
func (s *MailserverSuite) createRequest(p *ServerTestParams) *whisper.Envelope {
bloom := whisper.TopicToBloom(p.topic)
bloom := whispertypes.TopicToBloom(p.topic)
data := make([]byte, 8)
binary.BigEndian.PutUint32(data, p.low)
binary.BigEndian.PutUint32(data[4:], p.upp)
@ -709,7 +711,7 @@ func (s *MailserverSuite) createRequest(p *ServerTestParams) *whisper.Envelope {
return s.createEnvelope(p.topic, data, p.key)
}
func (s *MailserverSuite) createEnvelope(topic whisper.TopicType, data []byte, srcKey *ecdsa.PrivateKey) *whisper.Envelope {
func (s *MailserverSuite) createEnvelope(topic whispertypes.TopicType, data []byte, srcKey *ecdsa.PrivateKey) *whisper.Envelope {
key, err := s.shh.GetSymKey(keyID)
if err != nil {
s.T().Fatalf("failed to retrieve sym key with seed %d: %s.", seed, err)
@ -717,7 +719,7 @@ func (s *MailserverSuite) createEnvelope(topic whisper.TopicType, data []byte, s
params := &whisper.MessageParams{
KeySym: key,
Topic: topic,
Topic: whisper.TopicType(topic),
Payload: data,
PoW: powRequirement * 2,
WorkTime: 2,
@ -769,7 +771,7 @@ func generateEnvelope(sentTime time.Time) (*whisper.Envelope, error) {
func processRequestAndCollectHashes(
server *WMailServer, lower, upper uint32, cursor []byte, bloom []byte, limit int,
) ([]common.Hash, []byte, common.Hash) {
) ([]common.Hash, []byte, statusproto.Hash) {
iter, _ := server.createIterator(lower, upper, cursor, nil, 0)
defer iter.Release()
bundles := make(chan []rlp.RawValue, 10)

View File

@ -30,10 +30,10 @@ import (
"github.com/status-im/status-go/services/personal"
"github.com/status-im/status-go/services/shhext"
"github.com/status-im/status-go/services/status"
"github.com/status-im/status-go/services/whisperbridge"
"github.com/status-im/status-go/static"
"github.com/status-im/status-go/timesource"
gethbridge "github.com/status-im/status-protocol-go/bridge/geth"
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
whisper "github.com/status-im/whisper/whisperv6"
"github.com/syndtr/goleveldb/leveldb"
)
@ -272,11 +272,11 @@ func activateStatusService(stack *node.Node, config *params.NodeConfig) error {
}
return stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
var whisper *whisper.Whisper
if err := ctx.Service(&whisper); err != nil {
var service *whisperbridge.WhisperService
if err := ctx.Service(&service); err != nil {
return nil, err
}
svc := status.New(whisper)
svc := status.New(service.Whisper)
return svc, nil
})
}
@ -310,48 +310,19 @@ func activateShhService(stack *node.Node, config *params.NodeConfig, db *leveldb
}
err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
whisperServiceConfig := &whisper.Config{
MaxMessageSize: whisper.DefaultMaxMessageSize,
MinimumAcceptedPOW: params.WhisperMinimumPoW,
}
return createShhService(ctx, &config.WhisperConfig, &config.ClusterConfig)
})
if err != nil {
return
}
if config.WhisperConfig.MaxMessageSize > 0 {
whisperServiceConfig.MaxMessageSize = config.WhisperConfig.MaxMessageSize
// Register Whisper status-protocol-go bridge
err = stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
var whisper *whisper.Whisper
if err := ctx.Service(&whisper); err != nil {
return nil, err
}
if config.WhisperConfig.MinimumPoW > 0 {
whisperServiceConfig.MinimumAcceptedPOW = config.WhisperConfig.MinimumPoW
}
whisperService := whisper.New(whisperServiceConfig)
if config.WhisperConfig.EnableRateLimiter {
r := whisperRateLimiter(config)
whisperService.SetRateLimiter(r)
}
if config.WhisperConfig.EnableNTPSync {
timesource, err := whisperTimeSource(ctx)
if err != nil {
return nil, err
}
whisperService.SetTimeSource(timesource)
}
// enable mail service
if config.WhisperConfig.EnableMailServer {
if err := registerMailServer(whisperService, &config.WhisperConfig); err != nil {
return nil, fmt.Errorf("failed to register MailServer: %v", err)
}
}
if config.WhisperConfig.LightClient {
emptyBloomFilter := make([]byte, 64)
if err := whisperService.SetBloomFilter(emptyBloomFilter); err != nil {
return nil, err
}
}
return whisperService, nil
return &whisperbridge.WhisperService{Whisper: gethbridge.NewGethWhisperWrapper(whisper)}, nil
})
if err != nil {
return
@ -359,14 +330,59 @@ func activateShhService(stack *node.Node, config *params.NodeConfig, db *leveldb
// TODO(dshulyak) add a config option to enable it by default, but disable if app is started from statusd
return stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
var whisper *whisper.Whisper
if err := ctx.Service(&whisper); err != nil {
var service *whisperbridge.WhisperService
if err := ctx.Service(&service); err != nil {
return nil, err
}
return shhext.New(gethbridge.NewGethWhisperWrapper(whisper), shhext.EnvelopeSignalHandler{}, db, config.ShhextConfig), nil
return shhext.New(service.Whisper, shhext.EnvelopeSignalHandler{}, db, config.ShhextConfig), nil
})
}
func createShhService(ctx *node.ServiceContext, whisperConfig *params.WhisperConfig, clusterConfig *params.ClusterConfig) (*whisper.Whisper, error) {
whisperServiceConfig := &whisper.Config{
MaxMessageSize: whisper.DefaultMaxMessageSize,
MinimumAcceptedPOW: params.WhisperMinimumPoW,
}
if whisperConfig.MaxMessageSize > 0 {
whisperServiceConfig.MaxMessageSize = whisperConfig.MaxMessageSize
}
if whisperConfig.MinimumPoW > 0 {
whisperServiceConfig.MinimumAcceptedPOW = whisperConfig.MinimumPoW
}
whisperService := whisper.New(whisperServiceConfig)
if whisperConfig.EnableRateLimiter {
r := whisperRateLimiter(whisperConfig, clusterConfig)
whisperService.SetRateLimiter(r)
}
if whisperConfig.EnableNTPSync {
timesource, err := whisperTimeSource(ctx)
if err != nil {
return nil, err
}
whisperService.SetTimeSource(timesource)
}
// enable mail service
if whisperConfig.EnableMailServer {
if err := registerMailServer(whisperService, whisperConfig); err != nil {
return nil, fmt.Errorf("failed to register MailServer: %v", err)
}
}
if whisperConfig.LightClient {
emptyBloomFilter := make([]byte, 64)
if err := whisperService.SetBloomFilter(emptyBloomFilter); err != nil {
return nil, err
}
}
return whisperService, nil
}
// activateIncentivisationService configures Whisper and adds it to the given node.
func activateIncentivisationService(stack *node.Node, config *params.NodeConfig) (err error) {
if !config.WhisperConfig.Enabled {
@ -382,7 +398,7 @@ func activateIncentivisationService(stack *node.Node, config *params.NodeConfig)
logger.Info("activating incentivisation")
// TODO(dshulyak) add a config option to enable it by default, but disable if app is started from statusd
return stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
var w whispertypes.Whisper
var w *whisperbridge.WhisperService
if err := ctx.Service(&w); err != nil {
return nil, err
}
@ -406,7 +422,7 @@ func activateIncentivisationService(stack *node.Node, config *params.NodeConfig)
return nil, err
}
return incentivisation.New(privateKey, w.PublicWhisperAPI(), incentivisationConfig, contract), nil
return incentivisation.New(privateKey, w.Whisper.PublicWhisperAPI(), incentivisationConfig, contract), nil
})
}
@ -457,10 +473,10 @@ func whisperTimeSource(ctx *node.ServiceContext) (func() time.Time, error) {
return timeSource.Now, nil
}
func whisperRateLimiter(config *params.NodeConfig) *whisper.PeerRateLimiter {
func whisperRateLimiter(whisperConfig *params.WhisperConfig, clusterConfig *params.ClusterConfig) *whisper.PeerRateLimiter {
enodes := append(
parseNodes(config.ClusterConfig.StaticNodes),
parseNodes(config.ClusterConfig.TrustedMailServers)...,
parseNodes(clusterConfig.StaticNodes),
parseNodes(clusterConfig.TrustedMailServers)...,
)
var (
ips []string
@ -473,8 +489,8 @@ func whisperRateLimiter(config *params.NodeConfig) *whisper.PeerRateLimiter {
return whisper.NewPeerRateLimiter(
&whisper.MetricsRateLimiterHandler{},
&whisper.PeerRateLimiterConfig{
LimitPerSecIP: config.WhisperConfig.RateLimitIP,
LimitPerSecPeerID: config.WhisperConfig.RateLimitPeerID,
LimitPerSecIP: whisperConfig.RateLimitIP,
LimitPerSecPeerID: whisperConfig.RateLimitPeerID,
WhitelistedIPs: ips,
WhitelistedPeerIDs: peerIDs,
},

View File

@ -442,7 +442,7 @@ func (s *Service) postPing() (hexutil.Bytes, error) {
}
msg.Payload = payload
msg.Sig = s.whisperKeyID
msg.SigID = s.whisperKeyID
msg.SymKeyID = s.whisperSymKeyID
return s.w.Post(context.TODO(), msg)

View File

@ -739,7 +739,7 @@ func createBloomFilter(r MessagesRequest) []byte {
return topicsToBloom(r.Topics...)
}
return whisper.TopicToBloom(whisper.TopicType(r.Topic))
return whispertypes.TopicToBloom(r.Topic)
}
func topicsToBloom(topics ...whispertypes.TopicType) []byte {
@ -749,9 +749,9 @@ func topicsToBloom(topics ...whispertypes.TopicType) []byte {
i.Or(i, new(big.Int).SetBytes(bloom[:]))
}
combined := make([]byte, whisper.BloomFilterSize)
combined := make([]byte, whispertypes.BloomFilterSize)
data := i.Bytes()
copy(combined[whisper.BloomFilterSize-len(data):], data[:])
copy(combined[whispertypes.BloomFilterSize-len(data):], data[:])
return combined
}

View File

@ -9,9 +9,7 @@ import (
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
statusproto "github.com/status-im/status-protocol-go/types"
whisper "github.com/status-im/whisper/whisperv6"
"github.com/ethereum/go-ethereum/common"
"github.com/status-im/status-go/mailserver"
"github.com/stretchr/testify/assert"
@ -64,7 +62,7 @@ func TestMessagesRequest_setDefaults(t *testing.T) {
}
func TestMakeMessagesRequestPayload(t *testing.T) {
var emptyTopic whisper.TopicType
var emptyTopic whispertypes.TopicType
testCases := []struct {
Name string
Req MessagesRequest
@ -83,7 +81,7 @@ func TestMakeMessagesRequestPayload(t *testing.T) {
{
Name: "valid cursor",
Req: MessagesRequest{
Cursor: hex.EncodeToString(mailserver.NewDBKey(123, emptyTopic, common.Hash{}).Cursor()),
Cursor: hex.EncodeToString(mailserver.NewDBKey(123, emptyTopic, statusproto.Hash{}).Cursor()),
},
Err: "",
},

View File

@ -1,13 +0,0 @@
package whisperutils
import (
"github.com/ethereum/go-ethereum/crypto"
whisper "github.com/status-im/whisper/whisperv6"
)
var discoveryTopic = "contact-discovery"
var DiscoveryTopicBytes = ToTopic(discoveryTopic)
func ToTopic(s string) whisper.TopicType {
return whisper.BytesToTopic(crypto.Keccak256([]byte(s)))
}

View File

@ -245,4 +245,3 @@ func (_Example *ExampleSession) Verify(v uint8, r [32]byte, s [32]byte) (*types.
func (_Example *ExampleTransactorSession) Verify(v uint8, r [32]byte, s [32]byte) (*types.Transaction, error) {
return _Example.Contract.Verify(&_Example.TransactOpts, v, r, s)
}

View File

@ -118,7 +118,7 @@ func WatchAccountsChanges(ctx context.Context, feed *event.Feed, initial []commo
log.Error("accounts watcher subscription failed", "error", err)
}
case n := <-accounts:
log.Debug("wallet received updated list of accoutns", "accounts", n)
log.Debug("wallet received updated list of accounts", "accounts", n)
restart := false
for _, acc := range n {
_, exist := listen[acc.Address]

View File

@ -0,0 +1,45 @@
package whisperbridge
import (
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
)
// Make sure that WhisperService implements node.Service interface.
var _ node.Service = (*WhisperService)(nil)
type WhisperService struct {
Whisper whispertypes.Whisper
}
// Protocols returns a new protocols list. In this case, there are none.
func (w *WhisperService) Protocols() []p2p.Protocol {
return []p2p.Protocol{}
}
// APIs returns a list of new APIs.
func (w *WhisperService) APIs() []rpc.API {
return []rpc.API{
{
Namespace: "status",
Version: "1.0",
Service: w.Whisper,
Public: false,
},
}
}
// Start is run when a service is started.
// It does nothing in this case but is required by `node.Service` interface.
func (w *WhisperService) Start(server *p2p.Server) error {
return nil
}
// Stop is run when a service is stopped.
// It does nothing in this case but is required by `node.Service` interface.
func (w *WhisperService) Stop() error {
return nil
}

View File

@ -363,4 +363,3 @@ func (_Eventer *EventerFilterer) WatchMessage(opts *bind.WatchOpts, sink chan<-
}
}), nil
}

View File

@ -24,7 +24,7 @@ vendor:
install-linter:
# install linter
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.19.0
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.21.0
.PHONY: install-linter
install-dev:

View File

@ -1,24 +1,24 @@
package gethbridge
import (
"crypto/ecdsa"
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
whisper "github.com/status-im/whisper/whisperv6"
)
type gethFilterWrapper struct {
filter *whisper.Filter
id string
}
// NewGethFilterWrapper returns an object that wraps Geth's Filter in a whispertypes interface
func NewGethFilterWrapper(f *whisper.Filter) whispertypes.Filter {
func NewGethFilterWrapper(f *whisper.Filter, id string) whispertypes.Filter {
if f.Messages == nil {
panic("Messages should not be nil")
}
return &gethFilterWrapper{
filter: f,
id: id,
}
}
@ -27,12 +27,7 @@ func GetGethFilterFrom(f whispertypes.Filter) *whisper.Filter {
return f.(*gethFilterWrapper).filter
}
// KeyAsym returns the private Key of recipient
func (w *gethFilterWrapper) KeyAsym() *ecdsa.PrivateKey {
return w.filter.KeyAsym
}
// KeySym returns the key associated with the Topic
func (w *gethFilterWrapper) KeySym() []byte {
return w.filter.KeySym
// ID returns the filter ID
func (w *gethFilterWrapper) ID() string {
return w.id
}

View File

@ -1,43 +0,0 @@
package gethbridge
import (
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
whisper "github.com/status-im/whisper/whisperv6"
)
type gethMessageStoreWrapper struct {
messageStore whisper.MessageStore
}
// NewGethMessageStoreWrapper returns an object that wraps Geth's MessageStore in a whispertypes interface
func NewGethMessageStoreWrapper(messageStore whisper.MessageStore) whispertypes.MessageStore {
if messageStore == nil {
panic("messageStore cannot be nil")
}
return &gethMessageStoreWrapper{
messageStore: messageStore,
}
}
// GetGethMessageStoreFrom retrieves the underlying whisper MessageStore interface from a wrapped MessageStore interface
func GetGethMessageStoreFrom(m whispertypes.MessageStore) whisper.MessageStore {
return m.(*gethMessageStoreWrapper).messageStore
}
func (w *gethMessageStoreWrapper) Add(m whispertypes.ReceivedMessage) error {
return w.messageStore.Add(GetGethReceivedMessageFrom(m))
}
func (w *gethMessageStoreWrapper) Pop() ([]whispertypes.ReceivedMessage, error) {
msgs, err := w.messageStore.Pop()
if err != nil {
return nil, err
}
wrappedMsgs := make([]whispertypes.ReceivedMessage, len(msgs))
for index, m := range msgs {
wrappedMsgs[index] = NewGethReceivedMessageWrapper(m)
}
return wrappedMsgs, err
}

View File

@ -90,7 +90,7 @@ func (w *gethPublicWhisperAPIWrapper) Post(ctx context.Context, req whispertypes
msg := whisper.NewMessage{
SymKeyID: req.SymKeyID,
PublicKey: req.PublicKey,
Sig: req.Sig,
Sig: req.SigID, // Sig is really a SigID
TTL: req.TTL,
Topic: whisper.TopicType(req.Topic),
Payload: req.Payload,

View File

@ -1,26 +0,0 @@
package gethbridge
import (
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
whisper "github.com/status-im/whisper/whisperv6"
)
type gethReceivedMessageWrapper struct {
receivedMessage *whisper.ReceivedMessage
}
// NewGethReceivedMessageWrapper returns an object that wraps Geth's ReceivedMessage in a whispertypes interface
func NewGethReceivedMessageWrapper(receivedMessage *whisper.ReceivedMessage) whispertypes.ReceivedMessage {
if receivedMessage == nil {
panic("receivedMessage cannot be nil")
}
return &gethReceivedMessageWrapper{
receivedMessage: receivedMessage,
}
}
// GetGethReceivedMessageFrom retrieves the underlying whisper ReceivedMessage struct from a wrapped ReceivedMessage interface
func GetGethReceivedMessageFrom(m whispertypes.ReceivedMessage) *whisper.ReceivedMessage {
return m.(*gethReceivedMessageWrapper).receivedMessage
}

View File

@ -32,8 +32,8 @@ func (w *gethWhisperWrapper) PublicWhisperAPI() whispertypes.PublicWhisperAPI {
return NewGethPublicWhisperAPIWrapper(whisper.NewPublicWhisperAPI(w.whisper))
}
func (w *gethWhisperWrapper) NewMessageStore() whispertypes.MessageStore {
return NewGethMessageStoreWrapper(w.whisper.NewMessageStore())
func (w *gethWhisperWrapper) Poll() {
// noop
}
// MinPow returns the PoW value required by this node.
@ -112,27 +112,57 @@ func (w *gethWhisperWrapper) GetSymKey(id string) ([]byte, error) {
return w.whisper.GetSymKey(id)
}
func (w *gethWhisperWrapper) Subscribe(f whispertypes.Filter) (string, error) {
return w.whisper.Subscribe(GetGethFilterFrom(f))
func (w *gethWhisperWrapper) Subscribe(opts *whispertypes.SubscriptionOptions) (string, error) {
var (
err error
keyAsym *ecdsa.PrivateKey
keySym []byte
)
if opts.SymKeyID != "" {
keySym, err = w.GetSymKey(opts.SymKeyID)
if err != nil {
return "", err
}
}
if opts.PrivateKeyID != "" {
keyAsym, err = w.GetPrivateKey(opts.PrivateKeyID)
if err != nil {
return "", err
}
}
f, err := w.createFilterWrapper("", keyAsym, keySym, opts.PoW, opts.Topics)
if err != nil {
return "", err
}
id, err := w.whisper.Subscribe(GetGethFilterFrom(f))
if err != nil {
return "", err
}
f.(*gethFilterWrapper).id = id
return id, nil
}
func (w *gethWhisperWrapper) GetFilter(id string) whispertypes.Filter {
return NewGethFilterWrapper(w.whisper.GetFilter(id))
return NewGethFilterWrapper(w.whisper.GetFilter(id), id)
}
func (w *gethWhisperWrapper) Unsubscribe(id string) error {
return w.whisper.Unsubscribe(id)
}
func (w *gethWhisperWrapper) CreateFilterWrapper(keyAsym *ecdsa.PrivateKey, keySym []byte, pow float64, topics [][]byte, messages whispertypes.MessageStore) whispertypes.Filter {
func (w *gethWhisperWrapper) createFilterWrapper(id string, keyAsym *ecdsa.PrivateKey, keySym []byte, pow float64, topics [][]byte) (whispertypes.Filter, error) {
return NewGethFilterWrapper(&whisper.Filter{
KeyAsym: keyAsym,
KeySym: keySym,
PoW: pow,
AllowP2P: true,
Topics: topics,
Messages: GetGethMessageStoreFrom(messages),
})
Messages: whisper.NewMemoryMessageStore(),
}, id), nil
}
func (w *gethWhisperWrapper) SendMessagesRequest(peerID []byte, r whispertypes.MessagesRequest) error {

View File

@ -5,7 +5,6 @@ import (
"crypto/sha1"
"encoding/hex"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
statusproto "github.com/status-im/status-protocol-go/types"
protocol "github.com/status-im/status-protocol-go/v1"
@ -195,7 +194,7 @@ func stringSliceToPublicKeys(slice []string, prefixed bool) ([]*ecdsa.PublicKey,
err error
)
if prefixed {
b, err = hexutil.Decode(item)
b, err = statusproto.DecodeHex(item)
} else {
b, err = hex.DecodeString(item)
}

View File

@ -12,6 +12,7 @@ require (
github.com/jinzhu/copier v0.0.0-20190625015134-976e0346caa8
github.com/karalabe/usb v0.0.0-20190919080040-51dc0efba356 // indirect
github.com/lucasb-eyer/go-colorful v1.0.2
github.com/mattn/go-pointer v0.0.0-20190911064623-a0a44394634f
github.com/minio/sha256-simd v0.1.1 // indirect
github.com/multiformats/go-multihash v0.0.8 // indirect
github.com/mutecomm/go-sqlcipher v0.0.0-20190227152316-55dbde17881f

View File

@ -212,6 +212,8 @@ github.com/mattn/go-isatty v0.0.0-20180830101745-3fb116b82035/go.mod h1:M+lRXTBq
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-pointer v0.0.0-20190911064623-a0a44394634f h1:QTRRO+ozoYgT3CQRIzNVYJRU3DB8HRnkZv6mr4ISmMA=
github.com/mattn/go-pointer v0.0.0-20190911064623-a0a44394634f/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=

View File

@ -4,7 +4,6 @@ import (
"context"
"crypto/ecdsa"
"database/sql"
"reflect"
"time"
"github.com/ethereum/go-ethereum/crypto"
@ -330,7 +329,7 @@ func (p *messageProcessor) Process(shhMessage *whispertypes.Message) ([]*protoco
default:
hlogger.Error(
"skipped a public message of unsupported type",
zap.String("type", reflect.TypeOf(m).String()),
zap.Any("value", statusMessage.ParsedMessage),
)
}
}

View File

@ -7,8 +7,6 @@ import (
"strings"
"time"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/pkg/errors"
"go.uber.org/zap"
@ -499,7 +497,7 @@ func (m *Messenger) AddMembersToChat(ctx context.Context, chat *Chat, members []
}
encodedMembers := make([]string, len(members))
for idx, member := range members {
encodedMembers[idx] = hexutil.Encode(crypto.FromECDSAPub(member))
encodedMembers[idx] = statusproto.EncodeHex(crypto.FromECDSAPub(member))
}
event := protocol.NewMembersAddedEvent(encodedMembers, group.NextClockValue())
err = group.ProcessEvent(&m.identity.PublicKey, event)

View File

@ -478,13 +478,11 @@ func (s *filtersManager) addSymmetric(chatID string) (*whisperFilter, error) {
}
}
f := s.whisper.CreateFilterWrapper(
nil, symKey,
minPow,
topics,
s.whisper.NewMessageStore())
id, err := s.whisper.Subscribe(f)
id, err := s.whisper.Subscribe(&whispertypes.SubscriptionOptions{
SymKeyID: symKeyID,
PoW: minPow,
Topics: topics,
})
if err != nil {
return nil, err
}
@ -511,13 +509,16 @@ func (s *filtersManager) addAsymmetric(chatID string, listen bool) (*whisperFilt
topic := toTopic(chatID)
topics := [][]byte{topic}
f := s.whisper.CreateFilterWrapper(
s.privateKey, nil,
pow,
topics,
s.whisper.NewMessageStore())
privateKeyID, err := s.whisper.AddKeyPair(s.privateKey)
if err != nil {
return nil, err
}
id, err := s.whisper.Subscribe(f)
id, err := s.whisper.Subscribe(&whispertypes.SubscriptionOptions{
PrivateKeyID: privateKeyID,
PoW: pow,
Topics: topics,
})
if err != nil {
return nil, err
}

View File

@ -41,7 +41,9 @@ const (
)
const (
// EnvelopeTimeNotSynced represents the code passed to notify of a clock skew situation
EnvelopeTimeNotSynced uint = 1000
// EnvelopeOtherError represents the code passed to notify of a generic error situation
EnvelopeOtherError
)

View File

@ -1,17 +1,6 @@
package whispertypes
import (
"crypto/ecdsa"
)
// Filter represents a Whisper message filter
type Filter interface {
KeyAsym() *ecdsa.PrivateKey // Private Key of recipient
KeySym() []byte // Key associated with the Topic
}
// MessageStore defines the interface for a temporary message store.
type MessageStore interface {
Add(ReceivedMessage) error
Pop() ([]ReceivedMessage, error)
ID() string
}

View File

@ -7,6 +7,8 @@ import (
)
const (
// MaxLimitInMessagesRequest represents the maximum number of messages
// that can be requested from the mailserver
MaxLimitInMessagesRequest = 1000
)
@ -28,6 +30,7 @@ type MessagesRequest struct {
Bloom []byte `json:"bloom"`
}
// SetDefaults sets the From and To defaults
func (r *MessagesRequest) SetDefaults(now time.Time) {
// set From and To defaults
if r.To == 0 {

View File

@ -4,6 +4,7 @@ import (
"crypto/ecdsa"
)
// NegotiatedSecret represents a negotiated secret (both public and private keys)
type NegotiatedSecret struct {
PublicKey *ecdsa.PublicKey
Key []byte

View File

@ -1,6 +0,0 @@
package whispertypes
// ReceivedMessage represents a data packet to be received through the
// Whisper protocol and successfully decrypted.
type ReceivedMessage interface {
}

View File

@ -10,7 +10,7 @@ import (
type NewMessage struct {
SymKeyID string `json:"symKeyID"`
PublicKey []byte `json:"pubKey"`
Sig string `json:"sig"`
SigID string `json:"sig"`
TTL uint32 `json:"ttl"`
Topic TopicType `json:"topic"`
Payload []byte `json:"payload"`

View File

@ -7,8 +7,9 @@ import (
const (
// TopicLength is the expected length of the topic, in bytes
TopicLength = 4
BloomFilterSize = 64 // in bytes
TopicLength = 4
// BloomFilterSize is the expected length of a bloom filter byte array, in bytes
BloomFilterSize = 64
)
// TopicType represents a cryptographically secure, probabilistic partial
@ -63,6 +64,7 @@ func TopicToBloom(topic TopicType) []byte {
return b
}
// BloomFilterMatch returns true if a sample matches a bloom filter
func BloomFilterMatch(filter, sample []byte) bool {
if filter == nil {
return true
@ -79,6 +81,7 @@ func BloomFilterMatch(filter, sample []byte) bool {
return true
}
// MakeFullNodeBloom returns a bloom filter which matches all topics
func MakeFullNodeBloom() []byte {
bloom := make([]byte, BloomFilterSize)
for i := 0; i < BloomFilterSize; i++ {

View File

@ -5,11 +5,29 @@ import (
"time"
)
const (
// PubKeyLength represents the length (in bytes) of an uncompressed public key
PubKeyLength = 512 / 8
// AesKeyLength represents the length (in bytes) of an private key
AesKeyLength = 256 / 8
)
// SubscriptionOptions represents the parameters passed to Whisper.Subscribe
// to customize the subscription behavior
type SubscriptionOptions struct {
PrivateKeyID string
SymKeyID string
PoW float64
Topics [][]byte
}
// Whisper represents a dark communication interface through the Ethereum
// network, using its very own P2P communication layer.
type Whisper interface {
PublicWhisperAPI() PublicWhisperAPI
NewMessageStore() MessageStore
// Poll must be run periodically on the main thread by the host application
Poll()
// MinPow returns the PoW value required by this node.
MinPow() float64
@ -43,12 +61,10 @@ type Whisper interface {
DeleteSymKey(id string) bool
GetSymKey(id string) ([]byte, error)
Subscribe(f Filter) (string, error)
Subscribe(opts *SubscriptionOptions) (string, error)
GetFilter(id string) Filter
Unsubscribe(id string) error
CreateFilterWrapper(keyAsym *ecdsa.PrivateKey, keySym []byte, pow float64, topics [][]byte, messages MessageStore) Filter
// RequestHistoricMessages sends a message with p2pRequestCode to a specific peer,
// which is known to implement MailServer interface, and is supposed to process this
// request and respond with a number of peer-to-peer messages (possibly expired),

View File

@ -366,7 +366,7 @@ func (a *WhisperServiceTransport) addSig(newMessage *whispertypes.NewMessage) er
if err != nil {
return err
}
newMessage.Sig = sigID
newMessage.SigID = sigID
return nil
}

View File

@ -11,11 +11,11 @@ import (
"strings"
"time"
"github.com/ethereum/go-ethereum/common/hexutil"
gethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/status-im/status-protocol-go/crypto"
statusproto "github.com/status-im/status-protocol-go/types"
)
const (
@ -94,7 +94,7 @@ func (u *MembershipUpdate) extractFrom() error {
if err != nil {
return errors.Wrap(err, "failed to extract signature")
}
u.From = hexutil.Encode(gethcrypto.FromECDSAPub(publicKey))
u.From = statusproto.EncodeHex(gethcrypto.FromECDSAPub(publicKey))
return nil
}
@ -282,7 +282,7 @@ type Group struct {
}
func groupChatID(creator *ecdsa.PublicKey) string {
return uuid.New().String() + "-" + hexutil.Encode(gethcrypto.FromECDSAPub(creator))
return uuid.New().String() + "-" + statusproto.EncodeHex(gethcrypto.FromECDSAPub(creator))
}
func NewGroupWithMembershipUpdates(chatID string, updates []MembershipUpdate) (*Group, error) {
@ -396,7 +396,7 @@ func (g *Group) ProcessEvents(from *ecdsa.PublicKey, events []MembershipUpdateEv
}
func (g *Group) ProcessEvent(from *ecdsa.PublicKey, event MembershipUpdateEvent) error {
fromHex := hexutil.Encode(gethcrypto.FromECDSAPub(from))
fromHex := statusproto.EncodeHex(gethcrypto.FromECDSAPub(from))
if !g.validateEvent(fromHex, event) {
return fmt.Errorf("invalid event %#+v from %s", event, from)
}
@ -523,7 +523,7 @@ func stringSliceEquals(slice1, slice2 []string) bool {
}
func publicKeyToString(publicKey *ecdsa.PublicKey) string {
return hexutil.Encode(gethcrypto.FromECDSAPub(publicKey))
return statusproto.EncodeHex(gethcrypto.FromECDSAPub(publicKey))
}
type stringSet struct {

View File

@ -150,7 +150,7 @@ func (m *StatusMessage) HandleApplicationMetadata() error {
func (m *StatusMessage) HandleApplication() error {
value, err := decodeTransitMessage(m.DecryptedPayload)
if err != nil {
log.Printf("[message::DecodeMessage] could not decode message: %#x", m.Hash)
log.Printf("[message::DecodeMessage] could not decode message: %#x, err: %v", m.Hash, err.Error())
return err
}
m.ParsedMessage = value

2
vendor/modules.txt vendored
View File

@ -367,7 +367,7 @@ github.com/status-im/migrate/v4/source/go_bindata
github.com/status-im/rendezvous
github.com/status-im/rendezvous/protocol
github.com/status-im/rendezvous/server
# github.com/status-im/status-protocol-go v0.5.1
# github.com/status-im/status-protocol-go v0.5.2
github.com/status-im/status-protocol-go
github.com/status-im/status-protocol-go/applicationmetadata
github.com/status-im/status-protocol-go/bridge/geth