2024-07-18 15:30:10 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2024-11-05 14:00:28 +00:00
|
|
|
"database/sql"
|
2024-07-18 15:30:10 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2024-11-05 14:00:28 +00:00
|
|
|
persistence "github.com/status-im/status-go/services/connector/database"
|
2024-11-05 21:28:24 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/wallettypes"
|
2024-07-18 15:30:10 +00:00
|
|
|
"github.com/status-im/status-go/signal"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
WalletResponseMaxInterval = 20 * time.Minute
|
|
|
|
|
|
|
|
ErrWalletResponseTimeout = fmt.Errorf("timeout waiting for wallet response")
|
|
|
|
ErrEmptyAccountsShared = fmt.Errorf("empty accounts were shared by wallet")
|
|
|
|
ErrRequestAccountsRejectedByUser = fmt.Errorf("request accounts was rejected by user")
|
|
|
|
ErrSendTransactionRejectedByUser = fmt.Errorf("send transaction was rejected by user")
|
2024-11-19 12:12:08 +00:00
|
|
|
ErrSignRejectedByUser = fmt.Errorf("sign was rejected by user")
|
2024-07-18 15:30:10 +00:00
|
|
|
ErrEmptyRequestID = fmt.Errorf("empty requestID")
|
|
|
|
ErrAnotherConnectorOperationIsAwaitingFor = fmt.Errorf("another connector operation is awaiting for user input")
|
2024-11-05 14:00:28 +00:00
|
|
|
ErrEmptyUrl = fmt.Errorf("empty URL")
|
|
|
|
ErrDAppDoesNotHavePermissions = fmt.Errorf("dApp does not have permissions")
|
2024-07-18 15:30:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MessageType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
RequestAccountsAccepted MessageType = iota
|
|
|
|
SendTransactionAccepted
|
2024-11-19 12:12:08 +00:00
|
|
|
SignAccepted
|
2024-07-18 15:30:10 +00:00
|
|
|
Rejected
|
|
|
|
)
|
|
|
|
|
|
|
|
type Message struct {
|
|
|
|
Type MessageType
|
|
|
|
Data interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ClientSideHandler struct {
|
2024-11-05 14:00:28 +00:00
|
|
|
Db *sql.DB
|
2024-07-18 15:30:10 +00:00
|
|
|
responseChannel chan Message
|
|
|
|
isRequestRunning int32
|
|
|
|
}
|
|
|
|
|
2024-11-05 14:00:28 +00:00
|
|
|
func NewClientSideHandler(db *sql.DB) *ClientSideHandler {
|
2024-07-18 15:30:10 +00:00
|
|
|
return &ClientSideHandler{
|
2024-11-05 14:00:28 +00:00
|
|
|
Db: db,
|
2024-07-18 15:30:10 +00:00
|
|
|
responseChannel: make(chan Message, 1), // Buffer of 1 to avoid blocking
|
|
|
|
isRequestRunning: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientSideHandler) generateRequestID(dApp signal.ConnectorDApp) string {
|
|
|
|
rawID := fmt.Sprintf("%d%s", time.Now().UnixMilli(), dApp.URL)
|
|
|
|
hash := sha256.Sum256([]byte(rawID))
|
|
|
|
return hex.EncodeToString(hash[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientSideHandler) setRequestRunning() bool {
|
|
|
|
return atomic.CompareAndSwapInt32(&c.isRequestRunning, 0, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientSideHandler) clearRequestRunning() {
|
|
|
|
atomic.StoreInt32(&c.isRequestRunning, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientSideHandler) RequestShareAccountForDApp(dApp signal.ConnectorDApp) (types.Address, uint64, error) {
|
|
|
|
if !c.setRequestRunning() {
|
|
|
|
return types.Address{}, 0, ErrAnotherConnectorOperationIsAwaitingFor
|
|
|
|
}
|
|
|
|
defer c.clearRequestRunning()
|
|
|
|
|
|
|
|
requestID := c.generateRequestID(dApp)
|
|
|
|
signal.SendConnectorSendRequestAccounts(dApp, requestID)
|
|
|
|
|
|
|
|
timeout := time.After(WalletResponseMaxInterval)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-c.responseChannel:
|
|
|
|
switch msg.Type {
|
|
|
|
case RequestAccountsAccepted:
|
|
|
|
response := msg.Data.(RequestAccountsAcceptedArgs)
|
|
|
|
if response.RequestID == requestID {
|
|
|
|
return response.Account, response.ChainID, nil
|
|
|
|
}
|
|
|
|
case Rejected:
|
|
|
|
response := msg.Data.(RejectedArgs)
|
|
|
|
if response.RequestID == requestID {
|
|
|
|
return types.Address{}, 0, ErrRequestAccountsRejectedByUser
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-timeout:
|
|
|
|
return types.Address{}, 0, ErrWalletResponseTimeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-13 09:11:24 +00:00
|
|
|
func (c *ClientSideHandler) RequestAccountsAccepted(args RequestAccountsAcceptedArgs) error {
|
2024-08-15 11:49:29 +00:00
|
|
|
if args.RequestID == "" {
|
|
|
|
return ErrEmptyRequestID
|
|
|
|
}
|
|
|
|
|
2024-08-13 09:11:24 +00:00
|
|
|
c.responseChannel <- Message{Type: RequestAccountsAccepted, Data: args}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ClientSideHandler) RequestAccountsRejected(args RejectedArgs) error {
|
|
|
|
if args.RequestID == "" {
|
|
|
|
return ErrEmptyRequestID
|
|
|
|
}
|
|
|
|
|
|
|
|
c.responseChannel <- Message{Type: Rejected, Data: args}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 14:00:28 +00:00
|
|
|
func (c *ClientSideHandler) RecallDAppPermissions(args RecallDAppPermissionsArgs) error {
|
|
|
|
if args.URL == "" {
|
|
|
|
return ErrEmptyUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
dApp, err := persistence.SelectDAppByUrl(c.Db, args.URL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if dApp == nil {
|
|
|
|
return ErrDAppDoesNotHavePermissions
|
|
|
|
}
|
|
|
|
|
|
|
|
err = persistence.DeleteDApp(c.Db, dApp.URL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
signal.SendConnectorDAppPermissionRevoked(signal.ConnectorDApp{
|
|
|
|
URL: dApp.URL,
|
|
|
|
Name: dApp.Name,
|
|
|
|
IconURL: dApp.IconURL,
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-05 21:28:24 +00:00
|
|
|
func (c *ClientSideHandler) RequestSendTransaction(dApp signal.ConnectorDApp, chainID uint64, txArgs *wallettypes.SendTxArgs) (types.Hash, error) {
|
2024-07-18 15:30:10 +00:00
|
|
|
if !c.setRequestRunning() {
|
|
|
|
return types.Hash{}, ErrAnotherConnectorOperationIsAwaitingFor
|
|
|
|
}
|
|
|
|
defer c.clearRequestRunning()
|
|
|
|
|
|
|
|
txArgsJson, err := json.Marshal(txArgs)
|
|
|
|
if err != nil {
|
|
|
|
return types.Hash{}, fmt.Errorf("failed to marshal txArgs: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
requestID := c.generateRequestID(dApp)
|
|
|
|
signal.SendConnectorSendTransaction(dApp, chainID, string(txArgsJson), requestID)
|
|
|
|
|
|
|
|
timeout := time.After(WalletResponseMaxInterval)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-c.responseChannel:
|
|
|
|
switch msg.Type {
|
|
|
|
case SendTransactionAccepted:
|
|
|
|
response := msg.Data.(SendTransactionAcceptedArgs)
|
|
|
|
if response.RequestID == requestID {
|
|
|
|
return response.Hash, nil
|
|
|
|
}
|
|
|
|
case Rejected:
|
|
|
|
response := msg.Data.(RejectedArgs)
|
|
|
|
if response.RequestID == requestID {
|
|
|
|
return types.Hash{}, ErrSendTransactionRejectedByUser
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-timeout:
|
|
|
|
return types.Hash{}, ErrWalletResponseTimeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-13 09:11:24 +00:00
|
|
|
func (c *ClientSideHandler) SendTransactionAccepted(args SendTransactionAcceptedArgs) error {
|
|
|
|
if args.RequestID == "" {
|
|
|
|
return ErrEmptyRequestID
|
|
|
|
}
|
|
|
|
|
|
|
|
c.responseChannel <- Message{Type: SendTransactionAccepted, Data: args}
|
2024-07-18 15:30:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-13 09:11:24 +00:00
|
|
|
func (c *ClientSideHandler) SendTransactionRejected(args RejectedArgs) error {
|
2024-07-18 15:30:10 +00:00
|
|
|
if args.RequestID == "" {
|
|
|
|
return ErrEmptyRequestID
|
|
|
|
}
|
|
|
|
|
|
|
|
c.responseChannel <- Message{Type: Rejected, Data: args}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
func (c *ClientSideHandler) RequestSign(dApp signal.ConnectorDApp, challenge, address string, method string) (string, error) {
|
2024-08-13 09:11:24 +00:00
|
|
|
if !c.setRequestRunning() {
|
|
|
|
return "", ErrAnotherConnectorOperationIsAwaitingFor
|
|
|
|
}
|
|
|
|
defer c.clearRequestRunning()
|
|
|
|
|
|
|
|
requestID := c.generateRequestID(dApp)
|
2024-11-19 12:12:08 +00:00
|
|
|
signal.SendConnectorSign(dApp, requestID, challenge, address, method)
|
2024-08-13 09:11:24 +00:00
|
|
|
|
|
|
|
timeout := time.After(WalletResponseMaxInterval)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg := <-c.responseChannel:
|
|
|
|
switch msg.Type {
|
2024-11-19 12:12:08 +00:00
|
|
|
case SignAccepted:
|
|
|
|
response := msg.Data.(SignAcceptedArgs)
|
2024-08-13 09:11:24 +00:00
|
|
|
if response.RequestID == requestID {
|
|
|
|
return response.Signature, nil
|
|
|
|
}
|
|
|
|
case Rejected:
|
|
|
|
response := msg.Data.(RejectedArgs)
|
|
|
|
if response.RequestID == requestID {
|
2024-11-19 12:12:08 +00:00
|
|
|
return "", ErrSignRejectedByUser
|
2024-08-13 09:11:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-timeout:
|
|
|
|
return "", ErrWalletResponseTimeout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
func (c *ClientSideHandler) SignAccepted(args SignAcceptedArgs) error {
|
2024-07-18 15:30:10 +00:00
|
|
|
if args.RequestID == "" {
|
|
|
|
return ErrEmptyRequestID
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
c.responseChannel <- Message{Type: SignAccepted, Data: args}
|
2024-07-18 15:30:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-19 12:12:08 +00:00
|
|
|
func (c *ClientSideHandler) SignRejected(args RejectedArgs) error {
|
2024-07-18 15:30:10 +00:00
|
|
|
if args.RequestID == "" {
|
|
|
|
return ErrEmptyRequestID
|
|
|
|
}
|
|
|
|
|
|
|
|
c.responseChannel <- Message{Type: Rejected, Data: args}
|
|
|
|
return nil
|
|
|
|
}
|