2023-01-12 15:17:21 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2024-11-20 07:28:18 +00:00
|
|
|
"crypto/ecdsa"
|
2023-01-12 15:17:21 +00:00
|
|
|
"math/big"
|
2023-10-25 16:49:18 +00:00
|
|
|
"strings"
|
2023-01-12 15:17:21 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
ethTypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/status-im/status-go/account"
|
2023-10-25 16:49:18 +00:00
|
|
|
"github.com/status-im/status-go/api/multiformat"
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2023-01-12 15:17:21 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2023-10-25 16:49:18 +00:00
|
|
|
prot_common "github.com/status-im/status-go/protocol/common"
|
2023-01-12 15:17:21 +00:00
|
|
|
)
|
|
|
|
|
2024-11-20 07:28:18 +00:00
|
|
|
func GetSigner(chainID uint64, from types.Address, privateKey *ecdsa.PrivateKey) bind.SignerFn {
|
|
|
|
return func(addr common.Address, tx *ethTypes.Transaction) (*ethTypes.Transaction, error) {
|
|
|
|
s := ethTypes.NewLondonSigner(new(big.Int).SetUint64(chainID))
|
|
|
|
return ethTypes.SignTx(tx, s, privateKey)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func VerifyPasswordAndGetSigner(chainID uint64, accountsManager *account.GethManager, keyStoreDir string, from types.Address, password string) bind.SignerFn {
|
2023-01-12 15:17:21 +00:00
|
|
|
return func(addr common.Address, tx *ethTypes.Transaction) (*ethTypes.Transaction, error) {
|
|
|
|
selectedAccount, err := accountsManager.VerifyAccountPassword(keyStoreDir, from.Hex(), password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s := ethTypes.NewLondonSigner(new(big.Int).SetUint64(chainID))
|
|
|
|
return ethTypes.SignTx(tx, s, selectedAccount.PrivateKey)
|
|
|
|
}
|
|
|
|
}
|
2023-10-25 16:49:18 +00:00
|
|
|
|
|
|
|
func DeserializePublicKey(compressedKey string) (types.HexBytes, error) {
|
|
|
|
rawKey, err := multiformat.DeserializePublicKey(compressedKey, "f")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
secp256k1Code := "fe701"
|
|
|
|
pubKeyBytes := "0x" + strings.TrimPrefix(rawKey, secp256k1Code)
|
|
|
|
|
|
|
|
pubKey, err := prot_common.HexToPubkey(pubKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return crypto.CompressPubkey(pubKey), nil
|
|
|
|
}
|
2023-11-02 05:56:06 +00:00
|
|
|
|
|
|
|
func SerializePublicKey(compressedKey types.HexBytes) (string, error) {
|
|
|
|
rawKey, err := crypto.DecompressPubkey(compressedKey)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
pubKey := types.EncodeHex(crypto.FromECDSAPub(rawKey))
|
|
|
|
|
|
|
|
secp256k1Code := "0xe701"
|
|
|
|
base58btc := "z"
|
|
|
|
multiCodecKey := secp256k1Code + strings.TrimPrefix(pubKey, "0x")
|
|
|
|
cpk, err := multiformat.SerializePublicKey(multiCodecKey, base58btc)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return cpk, nil
|
|
|
|
}
|