Skip to content

Commit

Permalink
Add chainID to txArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Feb 28, 2024
1 parent 8f35c73 commit cb80dbf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ func Sign(priv *ecdsa.PrivateKey, hash []byte) ([]byte, error) {
return nil, fmt.Errorf("hash is required to be exactly %d bytes (%d)", types.HashLength, len(hash))
}

if priv.Curve != btcec.S256() {
return nil, errors.New("private key curve is not secp256k1")
}
// if priv.Curve != btcec.S256() {
// return nil, fmt.Errorf("private key curve is not secp256k1. Actual: %s", priv.Curve.Params().Name)
// }

// convert from ecdsa.PrivateKey to btcec.PrivateKey
btcPrivKey, err := convertToBtcPrivKey(priv)
Expand Down
5 changes: 5 additions & 0 deletions jsonrpc/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ func DecodeTxn(arg *txnArgs, store nonceGetter, forceSetNonce bool) (*types.Tran
arg.Gas = argUintPtr(0)
}

if arg.ChainID == nil {
arg.ChainID = argBytesPtr([]byte{})
}

txType := types.LegacyTxType
if arg.Type != nil {
txType = types.TxType(*arg.Type)
Expand All @@ -234,6 +238,7 @@ func DecodeTxn(arg *txnArgs, store nonceGetter, forceSetNonce bool) (*types.Tran
txn.SetValue(new(big.Int).SetBytes(*arg.Value))
txn.SetInput(input)
txn.SetNonce(uint64(*arg.Nonce))
txn.SetChainID(new(big.Int).SetBytes(*arg.ChainID))

if arg.To != nil {
txn.SetTo(arg.To)
Expand Down
14 changes: 14 additions & 0 deletions jsonrpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ type txnArgs struct {
Nonce *argUint64 `json:"nonce"`
Type *argUint64 `json:"type"`
AccessList *types.TxAccessList `json:"accessList,omitempty"`
ChainID *argBytes `json:"chainId,omitempty"`
}

// data retrieves the transaction calldata. Input field is preferred.
Expand Down Expand Up @@ -452,6 +453,19 @@ func (args *txnArgs) setDefaults(priceLimit uint64, eth *Eth) error {
args.Gas = &estimatedGasUint64
}

// If chain id is provided, ensure it matches the local chain id. Otherwise, set the local
// chain id as the default.
want := eth.chainID

if args.ChainID != nil {
have := new(big.Int).SetBytes(*args.ChainID)
if have.Uint64() != want {
return fmt.Errorf("chainId does not match node's (have=%v, want=%v)", have, want)
}
} else {
args.ChainID = argBytesPtr(new(big.Int).SetUint64(want).Bytes())
}

return nil
}

Expand Down

0 comments on commit cb80dbf

Please sign in to comment.