status-go/services/utils/utils.go

69 lines
2.1 KiB
Go
Raw Permalink Normal View History

package utils
import (
"crypto/ecdsa"
"math/big"
2023-10-25 16:49:18 +00:00
"strings"
"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"
"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"
)
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 {
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
}
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
}