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

fix: update merkle logic to prevent second preimage attack #83

Merged
merged 1 commit into from
Jun 14, 2024
Merged
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
2 changes: 2 additions & 0 deletions x/ophost/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,9 @@ func (ms MsgServer) FinalizeTokenWithdrawal(ctx context.Context, req *types.MsgF
seed = append(seed, types.Splitter)
seed = binary.BigEndian.AppendUint64(seed, amount.Uint64())

// double hash the leaf node
withdrawalHash = sha3.Sum256(seed)
withdrawalHash = sha3.Sum256(withdrawalHash[:])
}

if ok, err := ms.HasProvenWithdrawal(ctx, bridgeId, withdrawalHash); err != nil {
Expand Down
38 changes: 23 additions & 15 deletions x/ophost/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper_test

import (
"encoding/base64"
"encoding/hex"
"testing"
"time"
Expand Down Expand Up @@ -190,18 +191,18 @@ func Test_FinalizeTokenWithdrawal(t *testing.T) {
require.NoError(t, err)

// fund amount
amount := sdk.NewCoin("l1denom", math.NewInt(3_000_000))
amount := sdk.NewCoin("uinit", math.NewInt(1_000_000))
input.Faucet.Fund(ctx, types.BridgeAddress(1), amount)

outputRoot := decodeHex(t, "1d1ff385c7ea31c99289091fd4548072e5cd061b6bbde2b406ff62dd97e3edc3")
version := decodeHex(t, "0000000000000000000000000000000000000000000000000000000000000001")
stateRoot := decodeHex(t, "0000000000000000000000000000000000000000000000000000000000000002")
storageRoot := decodeHex(t, "b88a9b5af9f2a469cefb7e2e388eb146e7154f2eea8aaad1422f232f1b62067e")
blockHash := decodeHex(t, "0000000000000000000000000000000000000000000000000000000000000003")
outputRoot := decodeBase64(t, "0cg24XcpDwTIFXHY4jNyxg2EQS5RUqcMvlMJeuI5rf4=")
version := decodeBase64(t, "Ch4nNnd/gKYr6y33K2SYeEgcDKEBlLgytRNr77rlQBc=")
stateRoot := decodeBase64(t, "C2ZdjJ7uX41NaadA/FjlMiG6btiDfYnxE2ABqJocHxI=")
storageRoot := decodeBase64(t, "VcN+0UZbTtGyyLfQtAHW+bCv5ixadyyT0ZZ26aUT1JY=")
blockHash := decodeBase64(t, "tgmfQJT4uipVToW631xz0RXdrfzu7n5XxGNoPpX6isI=")
proofs := [][]byte{
decodeHex(t, "32e1a72a7c215563f9426bfe267b6fa22ba49b1fba7162d80094dc2f2b6c5a3a"),
decodeHex(t, "627dc2af9ee001b0e119100599dc3923ccdff2c53f06d89f40400edb1e7907e1"),
decodeHex(t, "bafac86e9ebc05a07701c151846c6de7bca68cd315f7a82fffe05fc4301ac47e"),
decodeBase64(t, "gnUeNU3EnW4iBOk8wounvu98aTER0BP5dOD0lkuwBBE="),
decodeBase64(t, "yE4zjliK5P9sfdzR2iNh6nYHmD+mjDK6dONuZ3QlVcA="),
decodeBase64(t, "GQXXUQ5P/egGvbAHkYfWHIAfgyCEmnjz/fUMKrWCEn8="),
}

now := time.Now().UTC()
Expand All @@ -210,19 +211,19 @@ func Test_FinalizeTokenWithdrawal(t *testing.T) {
require.NoError(t, err)

ctx = ctx.WithBlockTime(now.Add(time.Second * 60))
addr04, err := input.AccountKeeper.AddressCodec().BytesToString(decodeHex(t, "0000000000000000000000000000000000000004"))
sender, err := input.AccountKeeper.AddressCodec().BytesToString(decodeHex(t, "70b337786a5a87d896d5f9480016817529d0d61b"))
require.NoError(t, err)
addr01, err := input.AccountKeeper.AddressCodec().BytesToString(decodeHex(t, "0000000000000000000000000000000000000001"))
receiver, err := input.AccountKeeper.AddressCodec().BytesToString(decodeHex(t, "f56d386248d1ced6acd23c364909fe88e2ea6f70"))
require.NoError(t, err)
_, err = ms.FinalizeTokenWithdrawal(ctx, types.NewMsgFinalizeTokenWithdrawal(
1, 1, 4, proofs,
addr04,
addr01,
1, 1, 1, proofs,
sender,
receiver,
amount,
version, stateRoot, storageRoot, blockHash,
))
require.NoError(t, err)
require.Equal(t, amount, input.BankKeeper.GetBalance(ctx, decodeHex(t, "0000000000000000000000000000000000000001"), amount.Denom))
require.Equal(t, amount, input.BankKeeper.GetBalance(ctx, sdk.AccAddress(decodeHex(t, "f56d386248d1ced6acd23c364909fe88e2ea6f70")), amount.Denom))
}

func decodeHex(t *testing.T, str string) []byte {
Expand All @@ -232,6 +233,13 @@ func decodeHex(t *testing.T, str string) []byte {
return bz
}

func decodeBase64(t *testing.T, str string) []byte {
bz, err := base64.StdEncoding.DecodeString(str)
require.NoError(t, err)

return bz
}

func Test_UpdateProposal(t *testing.T) {
ctx, input := createDefaultTestInput(t)
ms := keeper.NewMsgServerImpl(input.OPHostKeeper)
Expand Down
Loading