Skip to content

Commit

Permalink
fix: prevent stripping public key for subsidy transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f committed Sep 6, 2023
1 parent 85f3063 commit edeea93
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
7 changes: 5 additions & 2 deletions types/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ func (tx *Tx) SetPublicKey(pub crypto.PublicKey) {
tx.basicChecked = false
tx.data.PublicKey = pub
if pub == nil {
tx.data.Flags = util.SetFlag(tx.data.Flags, flagStripedPublicKey)
if !tx.IsSubsidyTx() {
tx.data.Flags = util.SetFlag(tx.data.Flags, flagStripedPublicKey)
}
} else {
tx.data.Flags = util.UnsetFlag(tx.data.Flags, flagStripedPublicKey)
}
Expand Down Expand Up @@ -452,7 +454,8 @@ func (tx *Tx) IsFreeTx() bool {
return tx.IsSubsidyTx() || tx.IsSortitionTx() || tx.IsUnbondTx()
}

// StripPublicKey removes public key from the transaction.
// StripPublicKey removes the public key from the transaction.
// It is an alias function for `SetPublicKey(nil)`.
func (tx *Tx) StripPublicKey() {
tx.SetPublicKey(nil)
}
Expand Down
18 changes: 14 additions & 4 deletions types/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ func TestBasicCheck(t *testing.T) {
t.Run("Big memo, Should returns error", func(t *testing.T) {
bigMemo := strings.Repeat("a", 65)

trx := tx.NewSubsidyTx(ts.RandStamp(), ts.RandInt32NonZero(100),
ts.RandAddress(), ts.RandInt64(1e9), bigMemo)
trx := tx.NewTransferTx(ts.RandStamp(), ts.RandInt32NonZero(100),
ts.RandAddress(), ts.RandAddress(), ts.RandInt64(1e9), ts.RandInt64(1e6), bigMemo)

err := trx.BasicCheck()
assert.ErrorIs(t, err, tx.BasicCheckError{
Expand All @@ -111,8 +111,8 @@ func TestBasicCheck(t *testing.T) {
t.Run("Invalid payload, Should returns error", func(t *testing.T) {
invAddr := ts.RandAddress()
invAddr[0] = 2
trx := tx.NewSubsidyTx(ts.RandStamp(), ts.RandInt32NonZero(100),
invAddr, 1e9, "invalid address")
trx := tx.NewTransferTx(ts.RandStamp(), ts.RandInt32NonZero(100),
ts.RandAddress(), invAddr, 1e9, ts.RandInt64(1e6), "invalid address")

err := trx.BasicCheck()
assert.ErrorIs(t, err, tx.BasicCheckError{
Expand Down Expand Up @@ -242,6 +242,16 @@ func TestSubsidyTx(t *testing.T) {
Reason: "public key set for subsidy transaction",
})
})

t.Run("Strip public key", func(t *testing.T) {
stamp := ts.RandStamp()
trx := tx.NewSubsidyTx(stamp, 88, pub.Address(), 2500, "subsidy")
trx.StripPublicKey()

err := trx.BasicCheck()
assert.NoError(t, err)
assert.False(t, trx.IsPublicKeyStriped())
})
}

func TestInvalidSignature(t *testing.T) {
Expand Down

0 comments on commit edeea93

Please sign in to comment.