Skip to content
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

[action] use correct signer for web3 action #3937

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ type (
)

// Sign signs the action using sender's private key
func Sign(act Envelope, sk crypto.PrivateKey) (SealedEnvelope, error) {
func Sign(act Envelope, sk crypto.PrivateKey, useLondonSigner bool) (SealedEnvelope, error) {
sealed := SealedEnvelope{
Envelope: act,
srcPubkey: sk.PublicKey(),
}

sealed.UseLondonSigner(useLondonSigner)
h, err := sealed.envelopeHash()
if err != nil {
return sealed, errors.Wrap(err, "failed to generate envelope hash")
Expand Down
12 changes: 6 additions & 6 deletions action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestActionProtoAndVerify(t *testing.T) {
SetGasLimit(uint64(100000)).
SetAction(v).Build()

selp, err := Sign(elp, identityset.PrivateKey(28))
selp, err := Sign(elp, identityset.PrivateKey(28), false)
require.NoError(err)
require.Equal(65, len(selp.SrcPubkey().Bytes()))
require.NoError(selp.VerifySignature())
Expand All @@ -49,7 +49,7 @@ func TestActionProtoAndVerify(t *testing.T) {
SetGasLimit(uint64(100000)).
SetAction(v).Build()

selp, err := Sign(elp, identityset.PrivateKey(28))
selp, err := Sign(elp, identityset.PrivateKey(28), false)
require.NoError(err)

selp.srcPubkey = nil
Expand All @@ -62,7 +62,7 @@ func TestActionProtoAndVerify(t *testing.T) {
SetGasLimit(uint64(100000)).
SetAction(v).Build()

selp, err := Sign(elp, identityset.PrivateKey(28))
selp, err := Sign(elp, identityset.PrivateKey(28), false)
require.NoError(err)
selp.signature = []byte("invalid signature")
require.Equal(ErrInvalidSender, errors.Cause(selp.VerifySignature()))
Expand Down Expand Up @@ -112,20 +112,20 @@ func TestIsSystemAction(t *testing.T) {
cf := ClaimFromRewardingFundBuilder{}
actClaimFromRewarding := cf.Build()
act := builder.SetAction(&actClaimFromRewarding).Build()
sel, err := Sign(act, identityset.PrivateKey(1))
sel, err := Sign(act, identityset.PrivateKey(1), false)
require.NoError(err)
require.False(IsSystemAction(sel))

gb := GrantRewardBuilder{}
actGrantReward := gb.Build()
act = builder.SetAction(&actGrantReward).Build()
sel, err = Sign(act, identityset.PrivateKey(1))
sel, err = Sign(act, identityset.PrivateKey(1), false)
require.NoError(err)
require.True(IsSystemAction(sel))

actPollResult := NewPutPollResult(1, 1, nil)
act = builder.SetAction(actPollResult).Build()
sel, err = Sign(act, identityset.PrivateKey(1))
sel, err = Sign(act, identityset.PrivateKey(1), false)
require.NoError(err)
require.True(IsSystemAction(sel))
}
2 changes: 1 addition & 1 deletion action/candidateregister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestCandidateRegister(t *testing.T) {
SetGasPrice(test.GasPrice).
SetAction(cr).Build()
// sign
selp, err := Sign(elp, test.SenderKey)
selp, err := Sign(elp, test.SenderKey, false)
require.NoError(err)
require.NotNil(selp)
ser, err := proto.Marshal(selp.Proto())
Expand Down
2 changes: 1 addition & 1 deletion action/candidateupdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestCandidateUpdateSignVerify(t *testing.T) {
SetGasPrice(_gasprice).
SetAction(cu).Build()
// sign
selp, err := Sign(elp, _senderKey)
selp, err := Sign(elp, _senderKey, false)
require.NoError(err)
require.NotNil(selp)
ser, err := proto.Marshal(selp.Proto())
Expand Down
2 changes: 1 addition & 1 deletion action/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestExecutionSignVerify(t *testing.T) {
require.Equal(ex, ex2)

// sign the Execution
selp, err := Sign(elp, executorKey)
selp, err := Sign(elp, executorKey, false)
require.NoError(err)
require.NotNil(selp)
require.EqualValues(21, ex.BasicActionSize())
Expand Down
2 changes: 2 additions & 0 deletions action/protocol/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type (
ValidateSystemAction bool
AllowCorrectChainIDOnly bool
AddContractStakingVotes bool
UseCorrectSigner bool
SharedGasWithDapp bool
}

Expand Down Expand Up @@ -252,6 +253,7 @@ func WithFeatureCtx(ctx context.Context) context.Context {
ValidateSystemAction: g.IsQuebec(height),
AllowCorrectChainIDOnly: g.IsQuebec(height),
AddContractStakingVotes: g.IsQuebec(height),
UseCorrectSigner: g.IsRedsea(height),
SharedGasWithDapp: g.IsToBeEnabled(height),
},
)
Expand Down
10 changes: 5 additions & 5 deletions action/protocol/execution/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func runExecutions(
SetGasLimit(ecfg.GasLimit()).
SetGasPrice(ecfg.GasPrice()).
Build()
selp, err := action.Sign(elp, ecfg.PrivateKey())
selp, err := action.Sign(elp, ecfg.PrivateKey(), false)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -691,7 +691,7 @@ func TestProtocol_Handle(t *testing.T) {
elp := bd.SetAction(execution).
SetNonce(1).
SetGasLimit(100000).Build()
selp, err := action.Sign(elp, identityset.PrivateKey(27))
selp, err := action.Sign(elp, identityset.PrivateKey(27), false)
require.NoError(err)

require.NoError(ap.Add(context.Background(), selp))
Expand Down Expand Up @@ -746,7 +746,7 @@ func TestProtocol_Handle(t *testing.T) {
elp = bd.SetAction(execution).
SetNonce(2).
SetGasLimit(120000).Build()
selp, err = action.Sign(elp, identityset.PrivateKey(27))
selp, err = action.Sign(elp, identityset.PrivateKey(27), false)
require.NoError(err)

log.S().Infof("execution %+v", execution)
Expand Down Expand Up @@ -782,7 +782,7 @@ func TestProtocol_Handle(t *testing.T) {
elp = bd.SetAction(execution).
SetNonce(3).
SetGasLimit(120000).Build()
selp, err = action.Sign(elp, identityset.PrivateKey(27))
selp, err = action.Sign(elp, identityset.PrivateKey(27), false)
require.NoError(err)

log.S().Infof("execution %+v", execution)
Expand All @@ -806,7 +806,7 @@ func TestProtocol_Handle(t *testing.T) {
elp = bd.SetAction(execution1).
SetNonce(4).
SetGasLimit(100000).SetGasPrice(big.NewInt(10)).Build()
selp, err = action.Sign(elp, identityset.PrivateKey(27))
selp, err = action.Sign(elp, identityset.PrivateKey(27), false)
require.NoError(err)

require.NoError(ap.Add(context.Background(), selp))
Expand Down
1 change: 1 addition & 0 deletions action/protocol/generic_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (v *GenericValidator) Validate(ctx context.Context, selp action.SealedEnvel
}

// Verify action using action sender's public key
selp.UseLondonSigner(MustGetFeatureCtx(ctx).UseCorrectSigner)
if err := selp.VerifySignature(); err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions action/protocol/generic_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestActionProtoAndGenericValidator(t *testing.T) {
SetGasLimit(uint64(100000)).
SetNonce(3).
SetAction(v).Build()
selp, err := action.Sign(elp, identityset.PrivateKey(28))
selp, err := action.Sign(elp, identityset.PrivateKey(28), false)
require.NoError(err)
nselp, err := (&action.Deserializer{}).SetEvmNetworkID(_evmNetworkID).ActionToSealedEnvelope(selp.Proto())
require.NoError(err)
Expand All @@ -104,7 +104,7 @@ func TestActionProtoAndGenericValidator(t *testing.T) {
elp := bd.SetGasPrice(big.NewInt(10)).
SetGasLimit(uint64(10)).
SetAction(v).Build()
selp, err := action.Sign(elp, identityset.PrivateKey(28))
selp, err := action.Sign(elp, identityset.PrivateKey(28), false)
require.NoError(err)
nselp, err := (&action.Deserializer{}).SetEvmNetworkID(_evmNetworkID).ActionToSealedEnvelope(selp.Proto())
require.NoError(err)
Expand All @@ -120,7 +120,7 @@ func TestActionProtoAndGenericValidator(t *testing.T) {
SetGasLimit(uint64(100000)).
SetNonce(1).
SetAction(v).Build()
selp, err := action.Sign(elp, identityset.PrivateKey(27))
selp, err := action.Sign(elp, identityset.PrivateKey(27), false)
require.NoError(err)
nselp, err := (&action.Deserializer{}).SetEvmNetworkID(_evmNetworkID).ActionToSealedEnvelope(selp.Proto())
require.NoError(err)
Expand All @@ -136,7 +136,7 @@ func TestActionProtoAndGenericValidator(t *testing.T) {
SetNonce(1).
SetGasLimit(uint64(100000)).
SetAction(&gr).Build()
selp, err := action.Sign(elp, identityset.PrivateKey(28))
selp, err := action.Sign(elp, identityset.PrivateKey(28), false)
require.NoError(err)
nselp, err := (&action.Deserializer{}).SetEvmNetworkID(_evmNetworkID).ActionToSealedEnvelope(selp.Proto())
require.NoError(err)
Expand All @@ -151,7 +151,7 @@ func TestActionProtoAndGenericValidator(t *testing.T) {
SetNonce(1).
SetGasLimit(uint64(100000)).
SetAction(v).Build()
selp, err := action.Sign(elp, identityset.PrivateKey(28))
selp, err := action.Sign(elp, identityset.PrivateKey(28), false)
require.NoError(err)
nselp, err := (&action.Deserializer{}).SetEvmNetworkID(_evmNetworkID).ActionToSealedEnvelope(selp.Proto())
require.NoError(err)
Expand All @@ -166,7 +166,7 @@ func TestActionProtoAndGenericValidator(t *testing.T) {
elp := bd.SetAction(v).SetGasLimit(100000).
SetGasPrice(big.NewInt(10)).
SetNonce(1).Build()
selp, err := action.Sign(elp, identityset.PrivateKey(27))
selp, err := action.Sign(elp, identityset.PrivateKey(27), false)
require.NoError(err)
require.Error(valid.Validate(ctx, selp))
})
Expand Down
12 changes: 6 additions & 6 deletions action/protocol/poll/governance_protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func TestHandle(t *testing.T) {
elp := bd.SetGasLimit(uint64(100000)).
SetGasPrice(big.NewInt(10)).
SetAction(tsf).Build()
selp, err := action.Sign(elp, senderKey)
selp, err := action.Sign(elp, senderKey, false)
require.NoError(err)
require.NotNil(selp)
receipt, err := p.Handle(ctx, selp.Action(), nil)
Expand All @@ -398,7 +398,7 @@ func TestHandle(t *testing.T) {
elp := bd.SetGasLimit(uint64(100000)).
SetGasPrice(big.NewInt(10)).
SetAction(act2).Build()
selp2, err := action.Sign(elp, senderKey)
selp2, err := action.Sign(elp, senderKey, false)
require.NoError(err)
require.NotNil(selp2)
caller := selp2.SenderAddress()
Expand Down Expand Up @@ -445,7 +445,7 @@ func TestHandle(t *testing.T) {
elp := bd.SetGasLimit(uint64(100000)).
SetGasPrice(big.NewInt(10)).
SetAction(act2).Build()
selp2, err := action.Sign(elp, senderKey)
selp2, err := action.Sign(elp, senderKey, false)
require.NoError(err)
require.NotNil(selp2)
caller := selp2.SenderAddress()
Expand Down Expand Up @@ -480,7 +480,7 @@ func TestHandle(t *testing.T) {
elp := bd.SetGasLimit(uint64(100000)).
SetGasPrice(big.NewInt(10)).
SetAction(act3).Build()
selp3, err := action.Sign(elp, senderKey)
selp3, err := action.Sign(elp, senderKey, false)
require.NoError(err)
require.NotNil(selp3)
caller := selp3.SenderAddress()
Expand Down Expand Up @@ -514,7 +514,7 @@ func TestHandle(t *testing.T) {
elp4 := bd4.SetGasLimit(uint64(100000)).
SetGasPrice(big.NewInt(10)).
SetAction(act4).Build()
selp4, err := action.Sign(elp4, senderKey)
selp4, err := action.Sign(elp4, senderKey, false)
require.NoError(err)
require.NotNil(selp4)
caller := selp4.SenderAddress()
Expand Down Expand Up @@ -548,7 +548,7 @@ func TestHandle(t *testing.T) {
elp5 := bd5.SetGasLimit(uint64(100000)).
SetGasPrice(big.NewInt(10)).
SetAction(act5).Build()
selp5, err := action.Sign(elp5, senderKey)
selp5, err := action.Sign(elp5, senderKey, false)
require.NoError(err)
require.NotNil(selp5)
caller := selp5.SenderAddress()
Expand Down
12 changes: 6 additions & 6 deletions action/protocol/poll/staking_committee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func TestHandle_StakingCommittee(t *testing.T) {
elp := bd.SetGasLimit(uint64(100000)).
SetGasPrice(big.NewInt(10)).
SetAction(tsf).Build()
selp, err := action.Sign(elp, senderKey)
selp, err := action.Sign(elp, senderKey, false)
require.NoError(err)
require.NotNil(selp)
receipt, err := p.Handle(ctx, selp.Action(), nil)
Expand All @@ -234,7 +234,7 @@ func TestHandle_StakingCommittee(t *testing.T) {
elp := bd.SetGasLimit(uint64(100000)).
SetGasPrice(big.NewInt(10)).
SetAction(act2).Build()
selp2, err := action.Sign(elp, senderKey)
selp2, err := action.Sign(elp, senderKey, false)
require.NoError(err)
require.NotNil(selp2)
receipt, err := p.Handle(ctx2, selp2.Action(), sm2)
Expand Down Expand Up @@ -264,7 +264,7 @@ func TestHandle_StakingCommittee(t *testing.T) {
elp := bd.SetGasLimit(uint64(100000)).
SetGasPrice(big.NewInt(10)).
SetAction(act2).Build()
selp2, err := action.Sign(elp, senderKey)
selp2, err := action.Sign(elp, senderKey, false)
require.NoError(err)
require.NotNil(selp2)
caller := selp2.SenderAddress()
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestHandle_StakingCommittee(t *testing.T) {
elp := bd.SetGasLimit(uint64(100000)).
SetGasPrice(big.NewInt(10)).
SetAction(act3).Build()
selp3, err := action.Sign(elp, senderKey)
selp3, err := action.Sign(elp, senderKey, false)
require.NoError(err)
require.NotNil(selp3)
caller := selp3.SenderAddress()
Expand Down Expand Up @@ -337,7 +337,7 @@ func TestHandle_StakingCommittee(t *testing.T) {
elp4 := bd4.SetGasLimit(uint64(100000)).
SetGasPrice(big.NewInt(10)).
SetAction(act4).Build()
selp4, err := action.Sign(elp4, senderKey)
selp4, err := action.Sign(elp4, senderKey, false)
require.NoError(err)
require.NotNil(selp4)
caller := selp4.SenderAddress()
Expand Down Expand Up @@ -373,7 +373,7 @@ func TestHandle_StakingCommittee(t *testing.T) {
elp5 := bd5.SetGasLimit(uint64(100000)).
SetGasPrice(big.NewInt(10)).
SetAction(act5).Build()
selp5, err := action.Sign(elp5, senderKey)
selp5, err := action.Sign(elp5, senderKey, false)
require.NoError(err)
require.NotNil(selp5)
caller := selp5.SenderAddress()
Expand Down
6 changes: 3 additions & 3 deletions action/protocol/rewarding/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func TestProtocol_Handle(t *testing.T) {
SetGasLimit(deposit.GasLimit()).
SetAction(&deposit).
Build()
se1, err := action.Sign(e1, identityset.PrivateKey(0))
se1, err := action.Sign(e1, identityset.PrivateKey(0), false)
require.NoError(t, err)

_, err = p.Handle(ctx, se1.Action(), sm)
Expand All @@ -390,7 +390,7 @@ func TestProtocol_Handle(t *testing.T) {
// Grant
// Test for createGrantRewardAction
e2 := createGrantRewardAction(0, uint64(0))
se2, err := action.Sign(e2, identityset.PrivateKey(0))
se2, err := action.Sign(e2, identityset.PrivateKey(0), false)
require.NoError(t, err)
ctx = protocol.WithActionCtx(
ctx,
Expand Down Expand Up @@ -426,7 +426,7 @@ func TestProtocol_Handle(t *testing.T) {
SetGasLimit(claim.GasLimit()).
SetAction(&claim).
Build()
se3, err := action.Sign(e3, identityset.PrivateKey(0))
se3, err := action.Sign(e3, identityset.PrivateKey(0), false)
require.NoError(t, err)
ctx = protocol.WithActionCtx(
ctx,
Expand Down
24 changes: 18 additions & 6 deletions action/rlp_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ import (
"golang.org/x/crypto/sha3"
)

func rlpRawHash(rawTx *types.Transaction, chainID uint32) (hash.Hash256, error) {
h := types.NewEIP155Signer(big.NewInt(int64(chainID))).Hash(rawTx)
func rlpRawHash(rawTx *types.Transaction, chainID uint32, useLondontSigner bool) (hash.Hash256, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

signer types.Signer

var signer types.Signer
if useLondontSigner {
signer = types.NewLondonSigner(big.NewInt(int64(chainID)))
} else {
signer = types.NewEIP155Signer(big.NewInt(int64(chainID)))
}
Comment on lines +18 to +22
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create a util function to generate signer

h := signer.Hash(rawTx)
return hash.BytesToHash256(h[:]), nil
}

func rlpSignedHash(tx *types.Transaction, chainID uint32, sig []byte) (hash.Hash256, error) {
signedTx, err := reconstructSignedRlpTxFromSig(tx, chainID, sig)
func rlpSignedHash(tx *types.Transaction, chainID uint32, sig []byte, useLondonSigner bool) (hash.Hash256, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

signer types.Signer

signedTx, err := reconstructSignedRlpTxFromSig(tx, chainID, sig, useLondonSigner)
if err != nil {
return hash.ZeroHash256, err
}
Expand All @@ -30,7 +36,7 @@ func rlpSignedHash(tx *types.Transaction, chainID uint32, sig []byte) (hash.Hash
return hash.BytesToHash256(h.Sum(nil)), nil
}

func reconstructSignedRlpTxFromSig(rawTx *types.Transaction, chainID uint32, sig []byte) (*types.Transaction, error) {
func reconstructSignedRlpTxFromSig(rawTx *types.Transaction, chainID uint32, sig []byte, useLondonSigner bool) (*types.Transaction, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

signer types.Signer

if len(sig) != 65 {
return nil, errors.Errorf("invalid signature length = %d, expecting 65", len(sig))
}
Expand All @@ -40,7 +46,13 @@ func reconstructSignedRlpTxFromSig(rawTx *types.Transaction, chainID uint32, sig
sc[64] -= 27
}

signedTx, err := rawTx.WithSignature(types.NewEIP155Signer(big.NewInt(int64(chainID))), sc)
var signer types.Signer
if useLondonSigner {
signer = types.NewLondonSigner(big.NewInt(int64(chainID)))
} else {
signer = types.NewEIP155Signer(big.NewInt(int64(chainID)))
}
signedTx, err := rawTx.WithSignature(signer, sc)
if err != nil {
return nil, err
}
Expand Down
Loading