mirror of
https://github.com/status-im/status-go.git
synced 2025-01-10 06:36:32 +00:00
4c6ca00520
* feat(connector)_: impl `eth_requestAccounts` for browser plugin * feat(connector)_: add impl for `wallet_switchEthereumChain` and `eth_chainId` * feat(connector)_: add impl for `eth_sendTransaction` * feat(connector)_: add a signal and an endpoint for wallet ui side * chore_: refactor connector tests * feat(connector)_: impl `eth_requestAccounts` with signal * chore(connector)_: Add test, covering full transaction flow And polish impl & test for connector endpoints * fix(connector)_: temporary allow all origins for ws connection * chore_: review fixes * fix(connector)_: make user select chain id for dApp * fix(connector)_: add requestID and fine tune endpoints * chore(connector)_: naming fixes and tests improvments
176 lines
4.4 KiB
Go
176 lines
4.4 KiB
Go
package commands
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
hexutil "github.com/ethereum/go-ethereum/common/hexutil"
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
"github.com/status-im/status-go/signal"
|
|
"github.com/status-im/status-go/transactions"
|
|
)
|
|
|
|
func prepareSendTransactionRequest(dApp signal.ConnectorDApp, from types.Address) (RPCRequest, error) {
|
|
sendArgs := transactions.SendTxArgs{
|
|
From: from,
|
|
To: &types.Address{0x02},
|
|
Value: &hexutil.Big{},
|
|
Data: types.HexBytes("0x0"),
|
|
}
|
|
|
|
sendArgsJSON, err := json.Marshal(sendArgs)
|
|
if err != nil {
|
|
return RPCRequest{}, err
|
|
}
|
|
|
|
var sendArgsMap map[string]interface{}
|
|
err = json.Unmarshal(sendArgsJSON, &sendArgsMap)
|
|
if err != nil {
|
|
return RPCRequest{}, err
|
|
}
|
|
|
|
params := []interface{}{sendArgsMap}
|
|
|
|
return ConstructRPCRequest("eth_sendTransaction", params, &dApp)
|
|
}
|
|
|
|
func TestFailToSendTransactionWithoutPermittedDApp(t *testing.T) {
|
|
db, close := SetupTestDB(t)
|
|
defer close()
|
|
|
|
cmd := &SendTransactionCommand{Db: db}
|
|
|
|
// Don't save dApp in the database
|
|
request, err := prepareSendTransactionRequest(testDAppData, types.Address{0x1})
|
|
assert.NoError(t, err)
|
|
|
|
_, err = cmd.Execute(request)
|
|
assert.Equal(t, ErrDAppIsNotPermittedByUser, err)
|
|
}
|
|
|
|
func TestFailToSendTransactionWithWrongAddress(t *testing.T) {
|
|
db, close := SetupTestDB(t)
|
|
defer close()
|
|
|
|
cmd := &SendTransactionCommand{Db: db}
|
|
|
|
err := PersistDAppData(db, testDAppData, types.Address{0x01}, uint64(0x1))
|
|
assert.NoError(t, err)
|
|
|
|
request, err := prepareSendTransactionRequest(testDAppData, types.Address{0x02})
|
|
assert.NoError(t, err)
|
|
|
|
_, err = cmd.Execute(request)
|
|
assert.Equal(t, ErrParamsFromAddressIsNotShared, err)
|
|
}
|
|
|
|
func TestSendTransactionWithSignalTimout(t *testing.T) {
|
|
db, close := SetupTestDB(t)
|
|
defer close()
|
|
|
|
clientHandler := NewClientSideHandler()
|
|
|
|
cmd := &SendTransactionCommand{
|
|
Db: db,
|
|
ClientHandler: clientHandler,
|
|
}
|
|
|
|
err := PersistDAppData(db, testDAppData, types.Address{0x01}, uint64(0x1))
|
|
assert.NoError(t, err)
|
|
|
|
request, err := prepareSendTransactionRequest(testDAppData, types.Address{0x01})
|
|
assert.NoError(t, err)
|
|
|
|
backupWalletResponseMaxInterval := WalletResponseMaxInterval
|
|
WalletResponseMaxInterval = 1 * time.Millisecond
|
|
|
|
_, err = cmd.Execute(request)
|
|
assert.Equal(t, ErrWalletResponseTimeout, err)
|
|
WalletResponseMaxInterval = backupWalletResponseMaxInterval
|
|
}
|
|
|
|
func TestSendTransactionWithSignalAccepted(t *testing.T) {
|
|
db, close := SetupTestDB(t)
|
|
defer close()
|
|
|
|
fakedTransactionHash := types.Hash{0x051}
|
|
|
|
clientHandler := NewClientSideHandler()
|
|
|
|
cmd := &SendTransactionCommand{
|
|
Db: db,
|
|
ClientHandler: clientHandler,
|
|
}
|
|
|
|
err := PersistDAppData(db, testDAppData, types.Address{0x01}, uint64(0x1))
|
|
assert.NoError(t, err)
|
|
|
|
request, err := prepareSendTransactionRequest(testDAppData, types.Address{0x01})
|
|
assert.NoError(t, err)
|
|
|
|
signal.SetMobileSignalHandler(signal.MobileSignalHandler(func(s []byte) {
|
|
var evt EventType
|
|
err := json.Unmarshal(s, &evt)
|
|
assert.NoError(t, err)
|
|
|
|
switch evt.Type {
|
|
case signal.EventConnectorSendTransaction:
|
|
var ev signal.ConnectorSendTransactionSignal
|
|
err := json.Unmarshal(evt.Event, &ev)
|
|
assert.NoError(t, err)
|
|
|
|
err = clientHandler.SendTransactionAccepted(SendTransactionAcceptedArgs{
|
|
Hash: fakedTransactionHash,
|
|
RequestID: ev.RequestID,
|
|
})
|
|
assert.NoError(t, err)
|
|
}
|
|
}))
|
|
|
|
response, err := cmd.Execute(request)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, response, fakedTransactionHash.String())
|
|
}
|
|
|
|
func TestSendTransactionWithSignalRejected(t *testing.T) {
|
|
db, close := SetupTestDB(t)
|
|
defer close()
|
|
|
|
clientHandler := NewClientSideHandler()
|
|
|
|
cmd := &SendTransactionCommand{
|
|
Db: db,
|
|
ClientHandler: clientHandler,
|
|
}
|
|
|
|
err := PersistDAppData(db, testDAppData, types.Address{0x01}, uint64(0x1))
|
|
assert.NoError(t, err)
|
|
|
|
request, err := prepareSendTransactionRequest(testDAppData, types.Address{0x01})
|
|
assert.NoError(t, err)
|
|
|
|
signal.SetMobileSignalHandler(signal.MobileSignalHandler(func(s []byte) {
|
|
var evt EventType
|
|
err := json.Unmarshal(s, &evt)
|
|
assert.NoError(t, err)
|
|
|
|
switch evt.Type {
|
|
case signal.EventConnectorSendTransaction:
|
|
var ev signal.ConnectorSendTransactionSignal
|
|
err := json.Unmarshal(evt.Event, &ev)
|
|
assert.NoError(t, err)
|
|
|
|
err = clientHandler.SendTransactionRejected(RejectedArgs{
|
|
RequestID: ev.RequestID,
|
|
})
|
|
assert.NoError(t, err)
|
|
}
|
|
}))
|
|
|
|
_, err = cmd.Execute(request)
|
|
assert.Equal(t, ErrSendTransactionRejectedByUser, err)
|
|
}
|