This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: add dcc sign bytes test * chore: rename byt to buf as suggested by @rahulghangas
- Loading branch information
Showing
2 changed files
with
63 additions
and
1 deletion.
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
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,62 @@ | ||
package types_test | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"fmt" | ||
"math/big" | ||
"strconv" | ||
"testing" | ||
|
||
celestiatypes "github.com/celestiaorg/celestia-app/x/qgb/types" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/celestiaorg/orchestrator-relayer/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDataCommitmentTupleRootSignBytes(t *testing.T) { | ||
nonce := int64(1) | ||
commitment := bytes.Repeat([]byte{2}, 32) | ||
|
||
hexRepresentation := strconv.FormatInt(nonce, 16) | ||
// Make sure hex representation has even length | ||
if len(hexRepresentation)%2 == 1 { | ||
hexRepresentation = "0" + hexRepresentation | ||
} | ||
hexBytes, err := hex.DecodeString(hexRepresentation) | ||
require.NoError(t, err) | ||
paddedNonce, err := padBytes(hexBytes, 32) | ||
require.NoError(t, err) | ||
|
||
expectedHash := crypto.Keccak256Hash(append( | ||
celestiatypes.DcDomainSeparator[:], | ||
append( | ||
paddedNonce, | ||
commitment..., | ||
)..., | ||
)) | ||
|
||
result := types.DataCommitmentTupleRootSignBytes(big.NewInt(nonce), commitment) | ||
|
||
assert.Equal(t, expectedHash, result) | ||
} | ||
|
||
// padBytes Pad bytes to a given length | ||
func padBytes(buf []byte, length int) ([]byte, error) { | ||
l := len(buf) | ||
if l > length { | ||
return nil, fmt.Errorf( | ||
"cannot pad bytes because length of bytes array: %d is greater than given length: %d", | ||
l, | ||
length, | ||
) | ||
} | ||
if l == length { | ||
return buf, nil | ||
} | ||
tmp := make([]byte, length) | ||
copy(tmp[length-l:], buf) | ||
return tmp, nil | ||
} |