2018-04-10 10:02:54 +00:00
|
|
|
package personal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
2018-04-10 10:02:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrInvalidPersonalSignAccount is returned when the account passed to
|
|
|
|
// personal_sign isn't equal to the currently selected account.
|
|
|
|
ErrInvalidPersonalSignAccount = errors.New("invalid account as only the selected one can generate a signature")
|
|
|
|
)
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
// SignParams required to sign messages
|
|
|
|
type SignParams struct {
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
Address string `json:"account"`
|
|
|
|
Password string `json:"password"`
|
2018-04-10 10:02:54 +00:00
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
// RecoverParams are for calling `personal_ecRecover`
|
|
|
|
type RecoverParams struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
Signature string `json:"signature"`
|
2018-04-10 10:02:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PublicAPI represents a set of APIs from the `web3.personal` namespace.
|
|
|
|
type PublicAPI struct {
|
2018-08-16 11:37:53 +00:00
|
|
|
rpcClient *rpc.Client
|
|
|
|
rpcTimeout time.Duration
|
2018-04-10 10:02:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAPI creates an instance of the personal API.
|
2018-08-16 11:37:53 +00:00
|
|
|
func NewAPI() *PublicAPI {
|
2018-04-10 10:02:54 +00:00
|
|
|
return &PublicAPI{
|
2018-08-16 11:37:53 +00:00
|
|
|
rpcTimeout: 300 * time.Second,
|
2018-04-10 10:02:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRPC sets RPC params (client and timeout) for the API calls.
|
|
|
|
func (api *PublicAPI) SetRPC(rpcClient *rpc.Client, timeout time.Duration) {
|
|
|
|
api.rpcClient = rpcClient
|
|
|
|
api.rpcTimeout = timeout
|
|
|
|
}
|
|
|
|
|
2018-04-18 11:04:02 +00:00
|
|
|
// Recover is an implementation of `personal_ecRecover` or `web3.personal.ecRecover` API
|
2019-11-23 17:57:05 +00:00
|
|
|
func (api *PublicAPI) Recover(rpcParams RecoverParams) (addr types.Address, err error) {
|
2018-08-16 11:37:53 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), api.rpcTimeout)
|
|
|
|
defer cancel()
|
2019-11-23 17:57:05 +00:00
|
|
|
var gethAddr common.Address
|
2018-08-16 11:37:53 +00:00
|
|
|
err = api.rpcClient.CallContextIgnoringLocalHandlers(
|
|
|
|
ctx,
|
2019-11-23 17:57:05 +00:00
|
|
|
&gethAddr,
|
2021-09-22 17:49:20 +00:00
|
|
|
api.rpcClient.UpstreamChainID,
|
2018-08-16 11:37:53 +00:00
|
|
|
params.PersonalRecoverMethodName,
|
|
|
|
rpcParams.Message, rpcParams.Signature)
|
2019-11-23 17:57:05 +00:00
|
|
|
addr = types.Address(gethAddr)
|
2018-08-16 11:37:53 +00:00
|
|
|
|
|
|
|
return
|
2018-04-18 11:04:02 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 10:02:54 +00:00
|
|
|
// Sign is an implementation of `personal_sign` or `web3.personal.sign` API
|
2019-11-23 17:57:05 +00:00
|
|
|
func (api *PublicAPI) Sign(rpcParams SignParams, verifiedAccount *account.SelectedExtKey) (result types.HexBytes, err error) {
|
2018-08-16 11:37:53 +00:00
|
|
|
if !strings.EqualFold(rpcParams.Address, verifiedAccount.Address.Hex()) {
|
|
|
|
err = ErrInvalidPersonalSignAccount
|
2018-04-10 10:02:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), api.rpcTimeout)
|
|
|
|
defer cancel()
|
2019-11-23 17:57:05 +00:00
|
|
|
var gethResult hexutil.Bytes
|
2018-08-16 11:37:53 +00:00
|
|
|
err = api.rpcClient.CallContextIgnoringLocalHandlers(
|
|
|
|
ctx,
|
2019-11-23 17:57:05 +00:00
|
|
|
&gethResult,
|
2021-09-22 17:49:20 +00:00
|
|
|
api.rpcClient.UpstreamChainID,
|
2018-08-16 11:37:53 +00:00
|
|
|
params.PersonalSignMethodName,
|
|
|
|
rpcParams.Data, rpcParams.Address, rpcParams.Password)
|
2019-11-23 17:57:05 +00:00
|
|
|
result = types.HexBytes(gethResult)
|
2018-04-10 10:02:54 +00:00
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
return
|
2018-04-10 10:02:54 +00:00
|
|
|
}
|