Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Commit

Permalink
test: add dcc sign bytes test (#71)
Browse files Browse the repository at this point in the history
* test: add dcc sign bytes test

* chore: rename byt to buf as suggested by @rahulghangas
  • Loading branch information
rach-id authored Jan 27, 2023
1 parent 1d84100 commit 16eb6c5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.1 // indirect
github.com/stretchr/testify v1.8.1
github.com/tendermint/tm-db v0.6.7 // indirect
golang.org/x/crypto v0.2.0 // indirect
golang.org/x/net v0.2.0 // indirect
Expand Down
62 changes: 62 additions & 0 deletions types/data_commitment_confirm_test.go
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
}

0 comments on commit 16eb6c5

Please sign in to comment.