dendrite/web3/account.go
Tak Wai Wong 65ee181de4
Authorization - config, interface, and default implementation (#33)
* add config yaml for enable_auth

* zion_space_manager_localhost.go

* Placeholders for authorization

* rename func and type

* re-run go mod tidy

Co-authored-by: Tak Wai Wong <tak@hntlabs.com>
2022-09-26 16:46:52 -07:00

66 lines
1.4 KiB
Go

package web3
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)
type CreateTransactionSignerArgs struct {
PrivateKey string
ChainId int64
Client *ethclient.Client
GasValue int64 // in wei
GasLimit int64 // in units
}
func CreateTransactionSigner(args CreateTransactionSignerArgs) (*bind.TransactOpts, error) {
privateKey, err := crypto.HexToECDSA(args.PrivateKey)
if err != nil {
return nil, err
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
return nil, errors.New("cannot create public key ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := args.Client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
return nil, err
}
gasPrice, err := args.Client.SuggestGasPrice((context.Background()))
if err != nil {
return nil, err
}
signer, err := bind.NewKeyedTransactorWithChainID(privateKey, big.NewInt(args.ChainId))
if err != nil {
return nil, err
}
signer.Nonce = big.NewInt(int64(nonce))
signer.Value = big.NewInt(args.GasValue)
signer.GasLimit = uint64(args.GasLimit)
signer.GasPrice = gasPrice
fmt.Printf("{ nonce: %d, value: %d, gasLimit: %d, gasPrice: %d }\n",
signer.Nonce,
signer.Value,
signer.GasLimit,
signer.GasPrice,
)
return signer, nil
}