2023-11-28 17:59:23 +00:00
|
|
|
package transfer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2024-05-02 15:36:42 +00:00
|
|
|
"time"
|
2023-11-28 17:59:23 +00:00
|
|
|
|
|
|
|
ethTypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/status-im/status-go/account"
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
wallet_common "github.com/status-im/status-go/services/wallet/common"
|
2024-06-06 20:08:25 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/router/pathprocessor"
|
2023-11-28 17:59:23 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SignatureDetails struct {
|
|
|
|
R string `json:"r"`
|
|
|
|
S string `json:"s"`
|
|
|
|
V string `json:"v"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransactionDescription struct {
|
|
|
|
chainID uint64
|
2024-03-12 09:15:30 +00:00
|
|
|
from common.Address
|
2023-11-28 17:59:23 +00:00
|
|
|
builtTx *ethTypes.Transaction
|
|
|
|
signature []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransactionManager struct {
|
2024-05-23 16:22:57 +00:00
|
|
|
storage MultiTransactionStorage
|
2023-11-28 17:59:23 +00:00
|
|
|
gethManager *account.GethManager
|
2024-05-26 08:31:13 +00:00
|
|
|
transactor transactions.TransactorIface
|
2023-11-28 17:59:23 +00:00
|
|
|
config *params.NodeConfig
|
2024-06-02 23:37:51 +00:00
|
|
|
accountsDB accounts.AccountsStorage
|
2023-11-28 17:59:23 +00:00
|
|
|
pendingTracker *transactions.PendingTxTracker
|
|
|
|
eventFeed *event.Feed
|
|
|
|
|
|
|
|
multiTransactionForKeycardSigning *MultiTransaction
|
2024-06-06 20:08:25 +00:00
|
|
|
multipathTransactionsData []*pathprocessor.MultipathProcessorTxArgs
|
2024-05-23 16:22:57 +00:00
|
|
|
transactionsForKeycardSigning map[common.Hash]*TransactionDescription
|
|
|
|
}
|
|
|
|
|
|
|
|
type MultiTransactionStorage interface {
|
|
|
|
CreateMultiTransaction(tx *MultiTransaction) error
|
2024-05-26 08:31:13 +00:00
|
|
|
ReadMultiTransactions(details *MultiTxDetails) ([]*MultiTransaction, error)
|
2024-05-23 16:22:57 +00:00
|
|
|
UpdateMultiTransaction(tx *MultiTransaction) error
|
|
|
|
DeleteMultiTransaction(id wallet_common.MultiTransactionIDType) error
|
2023-11-28 17:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTransactionManager(
|
2024-05-23 16:22:57 +00:00
|
|
|
storage MultiTransactionStorage,
|
2023-11-28 17:59:23 +00:00
|
|
|
gethManager *account.GethManager,
|
2024-05-26 08:31:13 +00:00
|
|
|
transactor transactions.TransactorIface,
|
2023-11-28 17:59:23 +00:00
|
|
|
config *params.NodeConfig,
|
2024-06-02 23:37:51 +00:00
|
|
|
accountsDB accounts.AccountsStorage,
|
2023-11-28 17:59:23 +00:00
|
|
|
pendingTxManager *transactions.PendingTxTracker,
|
|
|
|
eventFeed *event.Feed,
|
|
|
|
) *TransactionManager {
|
|
|
|
return &TransactionManager{
|
2024-05-23 16:22:57 +00:00
|
|
|
storage: storage,
|
2023-11-28 17:59:23 +00:00
|
|
|
gethManager: gethManager,
|
|
|
|
transactor: transactor,
|
|
|
|
config: config,
|
|
|
|
accountsDB: accountsDB,
|
|
|
|
pendingTracker: pendingTxManager,
|
|
|
|
eventFeed: eventFeed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
emptyHash = common.Hash{}
|
|
|
|
)
|
|
|
|
|
|
|
|
type MultiTransactionType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
MultiTransactionSend = iota
|
|
|
|
MultiTransactionSwap
|
|
|
|
MultiTransactionBridge
|
2024-06-05 03:35:43 +00:00
|
|
|
MultiTransactionApprove
|
2024-05-23 16:22:57 +00:00
|
|
|
MultiTransactionTypeInvalid = 255
|
2023-11-28 17:59:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MultiTransaction struct {
|
2024-03-12 09:15:30 +00:00
|
|
|
ID wallet_common.MultiTransactionIDType `json:"id"`
|
|
|
|
Timestamp uint64 `json:"timestamp"`
|
|
|
|
FromNetworkID uint64 `json:"fromNetworkID"`
|
|
|
|
ToNetworkID uint64 `json:"toNetworkID"`
|
|
|
|
FromTxHash common.Hash `json:"fromTxHash"`
|
|
|
|
ToTxHash common.Hash `json:"toTxHash"`
|
|
|
|
FromAddress common.Address `json:"fromAddress"`
|
|
|
|
ToAddress common.Address `json:"toAddress"`
|
|
|
|
FromAsset string `json:"fromAsset"`
|
|
|
|
ToAsset string `json:"toAsset"`
|
|
|
|
FromAmount *hexutil.Big `json:"fromAmount"`
|
|
|
|
ToAmount *hexutil.Big `json:"toAmount"`
|
|
|
|
Type MultiTransactionType `json:"type"`
|
2023-11-28 17:59:23 +00:00
|
|
|
CrossTxID string
|
|
|
|
}
|
|
|
|
|
|
|
|
type MultiTransactionCommand struct {
|
|
|
|
FromAddress common.Address `json:"fromAddress"`
|
|
|
|
ToAddress common.Address `json:"toAddress"`
|
|
|
|
FromAsset string `json:"fromAsset"`
|
|
|
|
ToAsset string `json:"toAsset"`
|
|
|
|
FromAmount *hexutil.Big `json:"fromAmount"`
|
|
|
|
Type MultiTransactionType `json:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type MultiTransactionCommandResult struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Hashes map[uint64][]types.Hash `json:"hashes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TransactionIdentity struct {
|
|
|
|
ChainID wallet_common.ChainID `json:"chainId"`
|
|
|
|
Hash common.Hash `json:"hash"`
|
|
|
|
Address common.Address `json:"address"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TxResponse struct {
|
|
|
|
KeyUID string `json:"keyUid,omitempty"`
|
|
|
|
Address types.Address `json:"address,omitempty"`
|
|
|
|
AddressPath string `json:"addressPath,omitempty"`
|
|
|
|
SignOnKeycard bool `json:"signOnKeycard,omitempty"`
|
|
|
|
ChainID uint64 `json:"chainId,omitempty"`
|
2023-12-01 16:42:08 +00:00
|
|
|
MessageToSign interface{} `json:"messageToSign,omitempty"`
|
2023-11-28 17:59:23 +00:00
|
|
|
TxArgs transactions.SendTxArgs `json:"txArgs,omitempty"`
|
2023-12-01 16:42:08 +00:00
|
|
|
RawTx string `json:"rawTx,omitempty"`
|
|
|
|
TxHash common.Hash `json:"txHash,omitempty"`
|
2023-11-28 17:59:23 +00:00
|
|
|
}
|
|
|
|
|
2024-05-02 15:36:42 +00:00
|
|
|
func NewMultiTransaction(timestamp uint64, fromNetworkID, toNetworkID uint64, fromTxHash, toTxHash common.Hash, fromAddress, toAddress common.Address, fromAsset, toAsset string, fromAmount, toAmount *hexutil.Big, txType MultiTransactionType, crossTxID string) *MultiTransaction {
|
|
|
|
if timestamp == 0 {
|
|
|
|
timestamp = uint64(time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MultiTransaction{
|
|
|
|
ID: multiTransactionIDGenerator(),
|
|
|
|
Timestamp: timestamp,
|
|
|
|
FromNetworkID: fromNetworkID,
|
|
|
|
ToNetworkID: toNetworkID,
|
|
|
|
FromTxHash: fromTxHash,
|
|
|
|
ToTxHash: toTxHash,
|
|
|
|
FromAddress: fromAddress,
|
|
|
|
ToAddress: toAddress,
|
|
|
|
FromAsset: fromAsset,
|
|
|
|
ToAsset: toAsset,
|
|
|
|
FromAmount: fromAmount,
|
|
|
|
ToAmount: toAmount,
|
|
|
|
Type: txType,
|
|
|
|
CrossTxID: crossTxID,
|
2023-11-28 17:59:23 +00:00
|
|
|
}
|
2024-05-02 15:36:42 +00:00
|
|
|
}
|
2023-11-28 17:59:23 +00:00
|
|
|
|
2024-05-02 15:36:42 +00:00
|
|
|
func (tm *TransactionManager) SignMessage(message types.HexBytes, account *types.Key) (string, error) {
|
2024-06-02 23:37:51 +00:00
|
|
|
if account == nil || account.PrivateKey == nil {
|
|
|
|
return "", fmt.Errorf("account or private key is nil")
|
|
|
|
}
|
|
|
|
|
2024-05-02 15:36:42 +00:00
|
|
|
signature, err := crypto.Sign(message[:], account.PrivateKey)
|
2023-11-28 17:59:23 +00:00
|
|
|
|
|
|
|
return types.EncodeHex(signature), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tm *TransactionManager) BuildTransaction(chainID uint64, sendArgs transactions.SendTxArgs) (response *TxResponse, err error) {
|
|
|
|
account, err := tm.accountsDB.GetAccountByAddress(sendArgs.From)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to resolve account: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
kp, err := tm.accountsDB.GetKeypairByKeyUID(account.KeyUID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-19 13:38:01 +00:00
|
|
|
txBeingSigned, err := tm.transactor.ValidateAndBuildTransaction(chainID, sendArgs)
|
2023-12-01 16:42:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-28 17:59:23 +00:00
|
|
|
|
|
|
|
// Set potential missing fields that were added while building the transaction
|
|
|
|
if sendArgs.Value == nil {
|
|
|
|
value := hexutil.Big(*txBeingSigned.Value())
|
|
|
|
sendArgs.Value = &value
|
|
|
|
}
|
|
|
|
if sendArgs.Nonce == nil {
|
|
|
|
nonce := hexutil.Uint64(txBeingSigned.Nonce())
|
|
|
|
sendArgs.Nonce = &nonce
|
|
|
|
}
|
2023-12-01 16:42:08 +00:00
|
|
|
if sendArgs.Gas == nil {
|
|
|
|
gas := hexutil.Uint64(txBeingSigned.Gas())
|
|
|
|
sendArgs.Gas = &gas
|
|
|
|
}
|
2023-11-28 17:59:23 +00:00
|
|
|
if sendArgs.GasPrice == nil {
|
|
|
|
gasPrice := hexutil.Big(*txBeingSigned.GasPrice())
|
|
|
|
sendArgs.GasPrice = &gasPrice
|
|
|
|
}
|
2023-12-01 16:42:08 +00:00
|
|
|
|
|
|
|
if sendArgs.IsDynamicFeeTx() {
|
|
|
|
if sendArgs.MaxPriorityFeePerGas == nil {
|
|
|
|
maxPriorityFeePerGas := hexutil.Big(*txBeingSigned.GasTipCap())
|
|
|
|
sendArgs.MaxPriorityFeePerGas = &maxPriorityFeePerGas
|
|
|
|
}
|
|
|
|
if sendArgs.MaxFeePerGas == nil {
|
|
|
|
maxFeePerGas := hexutil.Big(*txBeingSigned.GasFeeCap())
|
|
|
|
sendArgs.MaxFeePerGas = &maxFeePerGas
|
|
|
|
}
|
2023-11-28 17:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
signer := ethTypes.NewLondonSigner(new(big.Int).SetUint64(chainID))
|
|
|
|
|
|
|
|
return &TxResponse{
|
|
|
|
KeyUID: account.KeyUID,
|
|
|
|
Address: account.Address,
|
|
|
|
AddressPath: account.Path,
|
|
|
|
SignOnKeycard: kp.MigratedToKeycard(),
|
|
|
|
ChainID: chainID,
|
2023-12-01 16:42:08 +00:00
|
|
|
MessageToSign: signer.Hash(txBeingSigned),
|
2023-11-28 17:59:23 +00:00
|
|
|
TxArgs: sendArgs,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-12-01 16:42:08 +00:00
|
|
|
func (tm *TransactionManager) BuildRawTransaction(chainID uint64, sendArgs transactions.SendTxArgs, signature []byte) (response *TxResponse, err error) {
|
2023-12-19 13:38:01 +00:00
|
|
|
tx, err := tm.transactor.BuildTransactionWithSignature(chainID, sendArgs, signature)
|
2023-12-01 16:42:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := tx.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &TxResponse{
|
|
|
|
ChainID: chainID,
|
|
|
|
TxArgs: sendArgs,
|
|
|
|
RawTx: types.EncodeHex(data),
|
|
|
|
TxHash: tx.Hash(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-05-23 16:22:57 +00:00
|
|
|
func (tm *TransactionManager) SendTransactionWithSignature(chainID uint64, sendArgs transactions.SendTxArgs, signature []byte) (hash types.Hash, err error) {
|
|
|
|
txWithSignature, err := tm.transactor.BuildTransactionWithSignature(chainID, sendArgs, signature)
|
|
|
|
if err != nil {
|
|
|
|
return hash, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, err = tm.transactor.SendTransactionWithSignature(common.Address(sendArgs.From), sendArgs.Symbol, sendArgs.MultiTransactionID, txWithSignature)
|
|
|
|
return hash, err
|
2023-11-28 17:59:23 +00:00
|
|
|
}
|