forked from base/withdrawer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out signer types to separate source files
- Loading branch information
Showing
3 changed files
with
102 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package signer | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"math/big" | ||
|
||
opcrypto "github.com/ethereum-optimism/optimism/op-service/crypto" | ||
"github.com/ethereum/go-ethereum/accounts/abi/bind" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
type ecdsaSigner struct { | ||
*ecdsa.PrivateKey | ||
} | ||
|
||
func (s *ecdsaSigner) Address() common.Address { | ||
return crypto.PubkeyToAddress(s.PublicKey) | ||
} | ||
|
||
func (s *ecdsaSigner) SignerFn(chainID *big.Int) bind.SignerFn { | ||
return opcrypto.PrivateKeySignerFn(s.PrivateKey, chainID) | ||
} | ||
|
||
func (s *ecdsaSigner) SignData(data []byte) ([]byte, error) { | ||
sig, err := crypto.Sign(crypto.Keccak256(data), s.PrivateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
sig[crypto.RecoveryIDOffset] += 27 | ||
return sig, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package signer | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"math/big" | ||
|
||
"github.com/decred/dcrd/hdkeychain/v3" | ||
"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" | ||
"github.com/tyler-smith/go-bip39" | ||
) | ||
|
||
type walletSigner struct { | ||
wallet accounts.Wallet | ||
account accounts.Account | ||
} | ||
|
||
func (s *walletSigner) Address() common.Address { | ||
return s.account.Address | ||
} | ||
|
||
func (s *walletSigner) SignerFn(chainID *big.Int) bind.SignerFn { | ||
return func(address common.Address, tx *types.Transaction) (*types.Transaction, error) { | ||
return s.wallet.SignTx(s.account, tx, chainID) | ||
} | ||
} | ||
|
||
func (s *walletSigner) SignData(data []byte) ([]byte, error) { | ||
return s.wallet.SignData(s.account, accounts.MimetypeTypedData, data) | ||
} | ||
|
||
func derivePrivateKey(mnemonic string, path accounts.DerivationPath) (*ecdsa.PrivateKey, error) { | ||
// Parse the seed string into the master BIP32 key. | ||
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
privKey, err := hdkeychain.NewMaster(seed, fakeNetworkParams{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, child := range path { | ||
privKey, err = privKey.Child(child) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
rawPrivKey, err := privKey.SerializedPrivKey() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return crypto.ToECDSA(rawPrivKey) | ||
} | ||
|
||
type fakeNetworkParams struct{} | ||
|
||
func (f fakeNetworkParams) HDPrivKeyVersion() [4]byte { | ||
return [4]byte{} | ||
} | ||
|
||
func (f fakeNetworkParams) HDPubKeyVersion() [4]byte { | ||
return [4]byte{} | ||
} |