mirror of
https://gitlab.com/pulsechaincom/go-pulse.git
synced 2025-01-03 01:07:39 +00:00
mobile: use bind.NewKeyedTransactor instead of duplicating (#20888)
It's better to reuse the existing code to create a keyed transactor than to rewrite the logic again.
This commit is contained in:
parent
be9172a7ac
commit
98eab2dbe7
@ -19,7 +19,6 @@
|
|||||||
package geth
|
package geth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -28,7 +27,6 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/accounts/keystore"
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Signer is an interface defining the callback when a contract requires a
|
// Signer is an interface defining the callback when a contract requires a
|
||||||
@ -82,28 +80,14 @@ func NewTransactOpts() *TransactOpts {
|
|||||||
return new(TransactOpts)
|
return new(TransactOpts)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewKeyedTransactor is a utility method to easily create a transaction signer
|
// NewKeyedTransactOpts is a utility method to easily create a transaction signer
|
||||||
// from a single private key.
|
// from a single private key.
|
||||||
func NewKeyedTransactOpts(keyJson []byte, passphrase string) (*TransactOpts, error) {
|
func NewKeyedTransactOpts(keyJson []byte, passphrase string) (*TransactOpts, error) {
|
||||||
key, err := keystore.DecryptKey(keyJson, passphrase)
|
key, err := keystore.DecryptKey(keyJson, passphrase)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
keyAddr := crypto.PubkeyToAddress(key.PrivateKey.PublicKey)
|
return &TransactOpts{*bind.NewKeyedTransactor(key.PrivateKey)}, nil
|
||||||
opts := bind.TransactOpts{
|
|
||||||
From: keyAddr,
|
|
||||||
Signer: func(signer types.Signer, address common.Address, tx *types.Transaction) (*types.Transaction, error) {
|
|
||||||
if address != keyAddr {
|
|
||||||
return nil, errors.New("not authorized to sign this account")
|
|
||||||
}
|
|
||||||
signature, err := crypto.Sign(signer.Hash(tx).Bytes(), key.PrivateKey)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return tx.WithSignature(signer, signature)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
return &TransactOpts{opts}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (opts *TransactOpts) GetFrom() *Address { return &Address{opts.opts.From} }
|
func (opts *TransactOpts) GetFrom() *Address { return &Address{opts.opts.From} }
|
||||||
|
Loading…
Reference in New Issue
Block a user