status-go/api/utils_test.go
Dmitry Shulyak be9c55bc16
Accounts data management (#1530)
* WIP accounts implementation

* Accounts datasore and changes to status mobile API

* Add library changes and method to update config

* Handle error after account selection

* Add two methods to start account to backend

* Use encrypted database for settings and add a service for them

* Resolve linter warning

* Bring back StartNode StopNode for tests

* Add sub accounts and get/save api

* Changes to accounts structure

* Login use root address and fetch necessary info from database

* Cover accounts store with tests

* Refactor in progress

* Initialize status keystore instance before starting ethereum node

* Rework library tests

* Resolve failures in private api test and send transaction test

* Pass pointer to initialized config to unmarshal

* Use multiaccounts/accounts naming consistently

Multiaccount is used as a login identifier
Account references an address and a key, if account is not watch-only.

* Add login timestamp stored in the database to accounts.Account object

* Add photo-path field for multiaccount struct

* Add multiaccoutns rpc with updateAccount method

Update to any other account that wasn't used for login will return an error

* Fix linter in services/accounts

* Select account before starting a node

* Save list of accounts on first login

* Pass account manager to accounts service to avoid selecting account before starting a node

* Add logs to login with save and regualr login
2019-08-20 18:38:40 +03:00

80 lines
2.2 KiB
Go

package api
import (
"fmt"
"testing"
"github.com/ethereum/go-ethereum/crypto"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/services/personal"
"github.com/status-im/status-go/t/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHashMessage(t *testing.T) {
backend := NewStatusBackend()
config, err := utils.MakeTestNodeConfig(params.StatusChainNetworkID)
require.NoError(t, err)
require.NoError(t, backend.AccountManager().InitKeystore(config.KeyStoreDir))
err = backend.StartNode(config)
require.NoError(t, err)
defer func() {
require.NoError(t, backend.StopNode())
}()
key, err := crypto.GenerateKey()
require.NoError(t, err)
addr := crypto.PubkeyToAddress(key.PublicKey)
scenarios := []struct {
message string
expectedHash string
recoverMessage string
}{
{
message: "XYZ",
expectedHash: "634349abf2de883d23e8b46972896c7652a06670c990410d3436d9b44db09e6b",
recoverMessage: fmt.Sprintf("0x%x", "XYZ"),
},
{
message: "0xXYZ",
expectedHash: "f9c57a8998c71a2c8d74d70abe6561838f0d6cb6d82bc85bd70afcc82368055c",
recoverMessage: fmt.Sprintf("0x%x", "0xXYZ"),
},
{
message: "1122",
expectedHash: "3f07e02a153f02bdf97d77161746257626e9c39e4c3cf59896365fd1e6a9c7c3",
recoverMessage: fmt.Sprintf("0x%x", "1122"),
},
{
message: "0x1122",
expectedHash: "86d79d0957efa9b7d91f1116e70d0ee934cb9cdeccefa07756aed2bee119a2f3",
recoverMessage: "0x1122",
},
}
for _, s := range scenarios {
t.Run(s.message, func(t *testing.T) {
hash, err := HashMessage(s.message)
require.Nil(t, err)
require.Equal(t, s.expectedHash, fmt.Sprintf("%x", hash))
// simulate signature from external signer like a keycard
sig, err := crypto.Sign(hash, key)
require.NoError(t, err)
sig[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
// check that the message was wrapped correctly before hashing it
recParams := personal.RecoverParams{
Message: s.recoverMessage,
Signature: fmt.Sprintf("0x%x", sig),
}
recoveredAddr, err := backend.Recover(recParams)
require.NoError(t, err)
assert.Equal(t, addr, recoveredAddr)
})
}
}