This repository has been archived by the owner on Sep 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc32b58
commit 5160c3a
Showing
5 changed files
with
137 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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,11 @@ | ||
package common | ||
|
||
import ( | ||
"encoding/hex" | ||
"strings" | ||
) | ||
|
||
// DecodeHex decodes hex encoded string to raw byte slice | ||
func DecodeHex(input string) ([]byte, error) { | ||
return hex.DecodeString(strings.TrimPrefix(input, "0x")) | ||
} |
This file contains 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 |
---|---|---|
|
@@ -21,7 +21,8 @@ import ( | |
) | ||
|
||
const ( | ||
HashLength = 32 | ||
HashLength = 32 | ||
ProofLength = 24 | ||
) | ||
|
||
type Etherman struct { | ||
|
This file contains 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,37 @@ | ||
package etherman | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"encoding/hex" | ||
) | ||
|
||
func generateRandomHexString(length int) (string, error) { | ||
numBytes := length / 2 | ||
randomBytes := make([]byte, numBytes) | ||
|
||
if _, err := rand.Read(randomBytes); err != nil { | ||
return "", err | ||
} | ||
|
||
return hex.EncodeToString(randomBytes), nil | ||
} | ||
|
||
func generateProof() (string, error) { | ||
var buf bytes.Buffer | ||
buf.WriteString("0x") | ||
|
||
for i := 0; i < 2*ProofLength; i++ { | ||
hash, err := generateRandomHexString(HashLength) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
_, err = buf.WriteString(hash) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
return buf.String(), nil | ||
} |
This file contains 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 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,53 @@ | ||
package etherman | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/0xPolygon/beethoven/common" | ||
) | ||
|
||
func TestConvertProof(t *testing.T) { | ||
validInputProof, err := generateProof() | ||
require.NoError(t, err) | ||
|
||
validRawProof, err := common.DecodeHex(validInputProof) | ||
require.NoError(t, err) | ||
|
||
validProof, err := BytesToProof(validRawProof) | ||
require.NoError(t, err) | ||
|
||
tests := []struct { | ||
name string | ||
input string | ||
expected Proof | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "valid proof", | ||
input: validInputProof, | ||
expected: validProof, | ||
expectedErr: "", | ||
}, | ||
{ | ||
name: "invalid proof length", | ||
input: "0x1234", | ||
expectedErr: fmt.Sprintf("invalid proof length. Expected length: %d, Actual length %d", ProofLength*HashLength*2+2, 6), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result, err := ConvertProof(tt.input) | ||
|
||
if tt.expectedErr != "" { | ||
require.ErrorContains(t, err, tt.expectedErr) | ||
} else { | ||
require.NoError(t, err) | ||
require.True(t, result.Equals(tt.expected), "expected: %v, actual: %v", tt.expected, result) | ||
} | ||
}) | ||
} | ||
} |