status-go/vendor/github.com/wealdtech/go-ens/v3/util/signer.go

52 lines
1.8 KiB
Go

// Copyright 2018 Weald Technology Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util
import (
"crypto/ecdsa"
"errors"
"math/big"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
// KeySigner generates a signer using a private key
func KeySigner(chainID *big.Int, key *ecdsa.PrivateKey) (signerfn bind.SignerFn) {
signerfn = func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
keyAddr := crypto.PubkeyToAddress(key.PublicKey)
if address != keyAddr {
return nil, errors.New("not authorized to sign this account")
}
return types.SignTx(tx, types.NewEIP155Signer(chainID), key)
}
return
}
// AccountSigner generates a signer using an account
func AccountSigner(chainID *big.Int, wallet *accounts.Wallet, account *accounts.Account, passphrase string) (signerfn bind.SignerFn) {
signerfn = func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != account.Address {
return nil, errors.New("not authorized to sign this account")
}
return (*wallet).SignTxWithPassphrase(*account, passphrase, tx, chainID)
}
return
}