2024-08-13 09:11:24 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/signal"
|
|
|
|
)
|
|
|
|
|
|
|
|
func preparePersonalSignRequest(dApp signal.ConnectorDApp, challenge, address string) (RPCRequest, error) {
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"challenge": challenge,
|
|
|
|
"address": address,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ConstructRPCRequest("personal_sign", []interface{}{params}, &dApp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailToPersonalSignWithMissingDAppFields(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_PersonalSign)
|
|
|
|
t.Cleanup(close)
|
2024-08-13 09:11:24 +00:00
|
|
|
|
|
|
|
// Missing DApp fields
|
|
|
|
request, err := ConstructRPCRequest("personal_sign", []interface{}{}, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
result, err := state.cmd.Execute(state.ctx, request)
|
2024-08-13 09:11:24 +00:00
|
|
|
assert.Equal(t, ErrRequestMissingDAppData, err)
|
|
|
|
assert.Empty(t, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailToPersonalSignForUnpermittedDApp(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_PersonalSign)
|
|
|
|
t.Cleanup(close)
|
2024-08-13 09:11:24 +00:00
|
|
|
|
|
|
|
request, err := preparePersonalSignRequest(testDAppData,
|
|
|
|
"0x506c65617365207369676e2074686973206d65737361676520746f20636f6e6669726d20796f7572206964656e746974792e",
|
|
|
|
"0x4B0897b0513FdBeEc7C469D9aF4fA6C0752aBea7",
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
result, err := state.cmd.Execute(state.ctx, request)
|
2024-08-13 09:11:24 +00:00
|
|
|
assert.Equal(t, ErrDAppIsNotPermittedByUser, err)
|
|
|
|
assert.Empty(t, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailToPersonalSignWithoutParams(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_PersonalSign)
|
|
|
|
t.Cleanup(close)
|
2024-08-13 09:11:24 +00:00
|
|
|
|
|
|
|
request, err := ConstructRPCRequest("personal_sign", nil, &testDAppData)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
result, err := state.cmd.Execute(state.ctx, request)
|
2024-08-13 09:11:24 +00:00
|
|
|
assert.Equal(t, ErrEmptyRPCParams, err)
|
|
|
|
assert.Empty(t, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailToPersonalSignWithSignalTimout(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_PersonalSign)
|
|
|
|
t.Cleanup(close)
|
2024-08-13 09:11:24 +00:00
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
err := PersistDAppData(state.walletDb, testDAppData, types.Address{0x01}, uint64(0x1))
|
2024-08-13 09:11:24 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
request, err := preparePersonalSignRequest(testDAppData,
|
|
|
|
"0x506c65617365207369676e2074686973206d65737361676520746f20636f6e6669726d20796f7572206964656e746974792e",
|
|
|
|
"0x4B0897b0513FdBeEc7C469D9aF4fA6C0752aBea7",
|
|
|
|
)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
backupWalletResponseMaxInterval := WalletResponseMaxInterval
|
|
|
|
WalletResponseMaxInterval = 1 * time.Millisecond
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
_, err = state.cmd.Execute(state.ctx, request)
|
2024-08-13 09:11:24 +00:00
|
|
|
assert.Equal(t, ErrWalletResponseTimeout, err)
|
|
|
|
WalletResponseMaxInterval = backupWalletResponseMaxInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPersonalSignWithSignalAccepted(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_PersonalSign)
|
|
|
|
t.Cleanup(close)
|
2024-08-13 09:11:24 +00:00
|
|
|
|
|
|
|
fakedSignature := "0x051"
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
err := PersistDAppData(state.walletDb, testDAppData, types.Address{0x01}, uint64(0x1))
|
2024-08-13 09:11:24 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
challenge := "0x506c65617365207369676e2074686973206d65737361676520746f20636f6e6669726d20796f7572206964656e746974792e"
|
|
|
|
address := "0x4B0897b0513FdBeEc7C469D9aF4fA6C0752aBea7"
|
|
|
|
request, err := preparePersonalSignRequest(testDAppData, challenge, address)
|
|
|
|
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.EventConnectorPersonalSign:
|
|
|
|
var ev signal.ConnectorPersonalSignSignal
|
|
|
|
err := json.Unmarshal(evt.Event, &ev)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ev.Challenge, challenge)
|
|
|
|
assert.Equal(t, ev.Address, address)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
err = state.handler.PersonalSignAccepted(PersonalSignAcceptedArgs{
|
2024-08-13 09:11:24 +00:00
|
|
|
Signature: fakedSignature,
|
|
|
|
RequestID: ev.RequestID,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}))
|
2024-09-03 08:41:56 +00:00
|
|
|
t.Cleanup(signal.ResetMobileSignalHandler)
|
2024-08-13 09:11:24 +00:00
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
response, err := state.cmd.Execute(state.ctx, request)
|
2024-08-13 09:11:24 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, response, fakedSignature)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPersonalSignWithSignalRejected(t *testing.T) {
|
2024-09-19 18:24:28 +00:00
|
|
|
state, close := setupCommand(t, Method_PersonalSign)
|
|
|
|
t.Cleanup(close)
|
2024-08-13 09:11:24 +00:00
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
err := PersistDAppData(state.walletDb, testDAppData, types.Address{0x01}, uint64(0x1))
|
2024-08-13 09:11:24 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
challenge := "0x506c65617365207369676e2074686973206d65737361676520746f20636f6e6669726d20796f7572206964656e746974792e"
|
|
|
|
address := "0x4B0897b0513FdBeEc7C469D9aF4fA6C0752aBea7"
|
|
|
|
request, err := preparePersonalSignRequest(testDAppData, challenge, address)
|
|
|
|
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.EventConnectorPersonalSign:
|
|
|
|
var ev signal.ConnectorPersonalSignSignal
|
|
|
|
err := json.Unmarshal(evt.Event, &ev)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
err = state.handler.PersonalSignRejected(RejectedArgs{
|
2024-08-13 09:11:24 +00:00
|
|
|
RequestID: ev.RequestID,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}))
|
2024-09-03 08:41:56 +00:00
|
|
|
t.Cleanup(signal.ResetMobileSignalHandler)
|
2024-08-13 09:11:24 +00:00
|
|
|
|
2024-09-19 18:24:28 +00:00
|
|
|
_, err = state.cmd.Execute(state.ctx, request)
|
2024-08-13 09:11:24 +00:00
|
|
|
assert.Equal(t, ErrPersonalSignRejectedByUser, err)
|
|
|
|
}
|