Files
2024-01-03 09:52:28 +01:00

54 lines
1.7 KiB
Go

<<<<<<< HEAD
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
=======
// (c) 2019-2020, Lux Partners Limited. All rights reserved.
>>>>>>> d5328b4 (Sync upstream)
// See the file LICENSE for licensing terms.
package contract
import (
"fmt"
"github.com/ethereum/go-ethereum/common"
)
// PackOrderedHashesWithSelector packs the function selector and ordered list of hashes into [dst]
// byte slice.
// assumes that [dst] has sufficient room for [functionSelector] and [hashes].
// Kept for testing backwards compatibility.
func PackOrderedHashesWithSelector(dst []byte, functionSelector []byte, hashes []common.Hash) error {
copy(dst[:len(functionSelector)], functionSelector)
return PackOrderedHashes(dst[len(functionSelector):], hashes)
}
// PackOrderedHashes packs the ordered list of [hashes] into the [dst] byte buffer.
// assumes that [dst] has sufficient space to pack [hashes] or else this function will panic.
// Kept for testing backwards compatibility.
func PackOrderedHashes(dst []byte, hashes []common.Hash) error {
if len(dst) != len(hashes)*common.HashLength {
return fmt.Errorf("destination byte buffer has insufficient length (%d) for %d hashes", len(dst), len(hashes))
}
var (
start = 0
end = common.HashLength
)
for _, hash := range hashes {
copy(dst[start:end], hash.Bytes())
start += common.HashLength
end += common.HashLength
}
return nil
}
// PackedHash returns packed the byte slice with common.HashLength from [packed]
// at the given [index].
// Assumes that [packed] is composed entirely of packed 32 byte segments.
// Kept for testing backwards compatibility.
func PackedHash(packed []byte, index int) []byte {
start := common.HashLength * index
end := start + common.HashLength
return packed[start:end]
}