-
Notifications
You must be signed in to change notification settings - Fork 72
Add external sign #844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add external sign #844
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9c763ee
add external sign
081d71a
fix
b866039
update go mod
325aca4
fix
39fd0de
fix config
e362de2
Merge branch 'main' into tpo-externalSign
curryxbo 36856e4
fix
e5e3122
update tpo readme
179056b
fix tpo readme
6f333b9
Update build-bk-prod target to include secret manager
curryxbo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,158 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/rsa" | ||
| "fmt" | ||
| "math/big" | ||
|
|
||
| "github.com/morph-l2/externalsign" | ||
| "github.com/morph-l2/go-ethereum" | ||
| "github.com/morph-l2/go-ethereum/common" | ||
| "github.com/morph-l2/go-ethereum/core/types" | ||
| "github.com/morph-l2/go-ethereum/log" | ||
| ) | ||
|
|
||
| // Signer handles transaction signing with support for both local and external signing | ||
| type Signer struct { | ||
| externalSign bool | ||
| externalSigner *externalsign.ExternalSign | ||
| externalSignUrl string | ||
| externalSignAddress common.Address | ||
| chainID *big.Int | ||
| signer types.Signer | ||
| } | ||
|
|
||
| // NewSigner creates a new Signer instance | ||
| func NewSigner( | ||
| externalSign bool, | ||
| externalSignAppid string, | ||
| externalRsaPriv *rsa.PrivateKey, | ||
| externalSignAddress string, | ||
| externalSignChain string, | ||
| externalSignUrl string, | ||
| chainID *big.Int, | ||
| ) *Signer { | ||
| signer := types.NewLondonSigner(chainID) | ||
|
|
||
| s := &Signer{ | ||
| externalSign: externalSign, | ||
| externalSignUrl: externalSignUrl, | ||
| externalSignAddress: common.HexToAddress(externalSignAddress), | ||
| chainID: chainID, | ||
| signer: signer, | ||
| } | ||
|
|
||
| if externalSign { | ||
| s.externalSigner = externalsign.NewExternalSign( | ||
| externalSignAppid, | ||
| externalRsaPriv, | ||
| externalSignAddress, | ||
| externalSignChain, | ||
| signer, | ||
| ) | ||
| log.Info("External signer initialized", | ||
| "address", externalSignAddress, | ||
| "chain", externalSignChain) | ||
| } | ||
|
|
||
| return s | ||
| } | ||
|
|
||
| // Sign signs a transaction using either external or local signing | ||
| func (s *Signer) Sign(tx *types.Transaction) (*types.Transaction, error) { | ||
| if !s.externalSign { | ||
| return nil, fmt.Errorf("local signing not supported in Signer, use bind.TransactOpts") | ||
| } | ||
|
|
||
| signedTx, err := s.externalSigner.RequestSign(s.externalSignUrl, tx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("external sign request failed: %w", err) | ||
| } | ||
| return signedTx, nil | ||
| } | ||
|
|
||
| // IsExternalSign returns whether external signing is enabled | ||
| func (s *Signer) IsExternalSign() bool { | ||
| return s.externalSign | ||
| } | ||
|
|
||
| // GetFromAddress returns the signer's address | ||
| func (s *Signer) GetFromAddress() common.Address { | ||
| return s.externalSignAddress | ||
| } | ||
|
|
||
| // CreateAndSignTx creates a new transaction and signs it | ||
| func (s *Signer) CreateAndSignTx( | ||
| ctx context.Context, | ||
| client *L2Client, | ||
| to common.Address, | ||
| callData []byte, | ||
| ) (*types.Transaction, error) { | ||
| from := s.externalSignAddress | ||
|
|
||
| nonce, err := client.GetClient().NonceAt(ctx, from, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get nonce: %w", err) | ||
| } | ||
|
|
||
| // Get gas tip cap | ||
| tip, err := client.GetClient().SuggestGasTipCap(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get gas tip cap: %w", err) | ||
| } | ||
|
|
||
| // Get base fee from latest block | ||
| head, err := client.GetClient().HeaderByNumber(ctx, nil) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get block header: %w", err) | ||
| } | ||
|
|
||
| var gasFeeCap *big.Int | ||
| if head.BaseFee != nil { | ||
| gasFeeCap = new(big.Int).Add( | ||
| tip, | ||
| new(big.Int).Mul(head.BaseFee, big.NewInt(2)), | ||
| ) | ||
| } else { | ||
| gasFeeCap = new(big.Int).Set(tip) | ||
| } | ||
|
|
||
| // Estimate gas | ||
| gas, err := client.GetClient().EstimateGas(ctx, ethereum.CallMsg{ | ||
| From: from, | ||
| To: &to, | ||
| GasFeeCap: gasFeeCap, | ||
| GasTipCap: tip, | ||
| Data: callData, | ||
| }) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to estimate gas: %w", err) | ||
| } | ||
|
|
||
| // Add 50% buffer to gas estimate | ||
| gas = gas * 3 / 2 | ||
|
|
||
| // Create transaction | ||
| tx := types.NewTx(&types.DynamicFeeTx{ | ||
| ChainID: s.chainID, | ||
| Nonce: nonce, | ||
| GasTipCap: tip, | ||
| GasFeeCap: gasFeeCap, | ||
| Gas: gas, | ||
| To: &to, | ||
| Data: callData, | ||
| }) | ||
|
|
||
| log.Info("Created transaction for signing", | ||
| "from", from.Hex(), | ||
| "to", to.Hex(), | ||
| "nonce", nonce, | ||
| "gas", gas, | ||
| "gasFeeCap", gasFeeCap, | ||
| "gasTipCap", tip) | ||
|
|
||
| // Sign transaction | ||
| return s.Sign(tx) | ||
| } | ||
|
|
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.