Skip to content

Commit

Permalink
Integrate Nimbus status-protocol-go
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Pombeiro committed Nov 18, 2019
1 parent c199c8f commit 2dd74da
Show file tree
Hide file tree
Showing 49 changed files with 320 additions and 300 deletions.
6 changes: 3 additions & 3 deletions account/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions account/address.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions account/generator/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions account/onboarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
9 changes: 5 additions & 4 deletions api/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
12 changes: 5 additions & 7 deletions lib/library_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions mailserver/cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
20 changes: 10 additions & 10 deletions mailserver/db_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 {
Expand All @@ -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
}
11 changes: 6 additions & 5 deletions mailserver/db_key_test.go
Original file line number Diff line number Diff line change
@@ -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())
Expand Down
24 changes: 13 additions & 11 deletions mailserver/mailserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
)

Expand All @@ -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()

Expand All @@ -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",
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion mailserver/mailserver_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 2dd74da

Please sign in to comment.