2020-05-20 12:16:09 +08:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2023-03-06 08:38:39 +08:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2020-05-20 12:16:09 +08:00
|
|
|
|
2022-10-11 11:16:30 +08:00
|
|
|
"github.com/tendermint/iavl"
|
|
|
|
"github.com/tendermint/tendermint/crypto/merkle"
|
2023-11-30 19:36:54 +08:00
|
|
|
"github.com/tendermint/tendermint/crypto/secp256k1"
|
2022-10-11 11:16:30 +08:00
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
|
|
|
|
2023-11-30 19:36:54 +08:00
|
|
|
//nolint:staticcheck
|
2023-04-18 15:51:26 +08:00
|
|
|
v1 "github.com/ethereum/go-ethereum/core/vm/lightclient/v1"
|
|
|
|
v2 "github.com/ethereum/go-ethereum/core/vm/lightclient/v2"
|
2020-05-20 12:16:09 +08:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-08-04 13:56:13 +08:00
|
|
|
uint64TypeLength uint64 = 8
|
2020-05-20 12:16:09 +08:00
|
|
|
precompileContractInputMetaDataLength uint64 = 32
|
|
|
|
consensusStateLengthBytesLength uint64 = 32
|
|
|
|
|
|
|
|
tmHeaderValidateResultMetaDataLength uint64 = 32
|
|
|
|
merkleProofValidateResultLength uint64 = 32
|
|
|
|
)
|
|
|
|
|
|
|
|
// input:
|
|
|
|
// consensus state length | consensus state | tendermint header |
|
|
|
|
// 32 bytes | | |
|
2023-04-18 15:51:26 +08:00
|
|
|
func decodeTendermintHeaderValidationInput(input []byte) (*v1.ConsensusState, *v1.Header, error) {
|
2020-08-04 13:56:13 +08:00
|
|
|
csLen := binary.BigEndian.Uint64(input[consensusStateLengthBytesLength-uint64TypeLength : consensusStateLengthBytesLength])
|
2023-03-24 09:51:11 +08:00
|
|
|
|
|
|
|
if consensusStateLengthBytesLength+csLen < consensusStateLengthBytesLength {
|
|
|
|
return nil, nil, fmt.Errorf("integer overflow, csLen: %d", csLen)
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:16:09 +08:00
|
|
|
if uint64(len(input)) <= consensusStateLengthBytesLength+csLen {
|
|
|
|
return nil, nil, fmt.Errorf("expected payload size %d, actual size: %d", consensusStateLengthBytesLength+csLen, len(input))
|
|
|
|
}
|
|
|
|
|
2023-04-18 15:51:26 +08:00
|
|
|
cs, err := v1.DecodeConsensusState(input[consensusStateLengthBytesLength : consensusStateLengthBytesLength+csLen])
|
2020-05-20 12:16:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2023-04-18 15:51:26 +08:00
|
|
|
header, err := v1.DecodeHeader(input[consensusStateLengthBytesLength+csLen:])
|
2020-05-20 12:16:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &cs, header, nil
|
|
|
|
}
|
|
|
|
|
2023-04-18 15:51:26 +08:00
|
|
|
// tmHeaderValidate implemented as a native contract. Used to validate the light
|
|
|
|
// client's new header for tendermint v0.31.12 and its compatible version.
|
2020-05-20 12:16:09 +08:00
|
|
|
type tmHeaderValidate struct{}
|
|
|
|
|
|
|
|
func (c *tmHeaderValidate) RequiredGas(input []byte) uint64 {
|
|
|
|
return params.TendermintHeaderValidateGas
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tmHeaderValidate) Run(input []byte) (result []byte, err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("internal error: %v\n", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if uint64(len(input)) <= precompileContractInputMetaDataLength {
|
|
|
|
return nil, fmt.Errorf("invalid input")
|
|
|
|
}
|
|
|
|
|
2020-08-04 13:56:13 +08:00
|
|
|
payloadLength := binary.BigEndian.Uint64(input[precompileContractInputMetaDataLength-uint64TypeLength : precompileContractInputMetaDataLength])
|
2020-05-20 12:16:09 +08:00
|
|
|
if uint64(len(input)) != payloadLength+precompileContractInputMetaDataLength {
|
|
|
|
return nil, fmt.Errorf("invalid input: input size should be %d, actual the size is %d", payloadLength+precompileContractInputMetaDataLength, len(input))
|
|
|
|
}
|
|
|
|
|
|
|
|
cs, header, err := decodeTendermintHeaderValidationInput(input[precompileContractInputMetaDataLength:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
validatorSetChanged, err := cs.ApplyHeader(header)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
consensusStateBytes, err := cs.EncodeConsensusState()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// result
|
|
|
|
// | validatorSetChanged | empty | consensusStateBytesLength | new consensusState |
|
|
|
|
// | 1 byte | 23 bytes | 8 bytes | |
|
|
|
|
lengthBytes := make([]byte, tmHeaderValidateResultMetaDataLength)
|
|
|
|
if validatorSetChanged {
|
|
|
|
copy(lengthBytes[:1], []byte{0x01})
|
|
|
|
}
|
|
|
|
consensusStateBytesLength := uint64(len(consensusStateBytes))
|
2020-08-04 13:56:13 +08:00
|
|
|
binary.BigEndian.PutUint64(lengthBytes[tmHeaderValidateResultMetaDataLength-uint64TypeLength:], consensusStateBytesLength)
|
2020-05-20 12:16:09 +08:00
|
|
|
|
|
|
|
result = append(lengthBytes, consensusStateBytes...)
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-11-30 19:36:54 +08:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------------------------------------
|
2020-05-20 12:16:09 +08:00
|
|
|
|
2022-10-11 11:16:30 +08:00
|
|
|
// iavlMerkleProofValidate implemented as a native contract.
|
|
|
|
type iavlMerkleProofValidate struct {
|
|
|
|
basicIavlMerkleProofValidate
|
|
|
|
}
|
2020-05-20 12:16:09 +08:00
|
|
|
|
|
|
|
func (c *iavlMerkleProofValidate) RequiredGas(input []byte) uint64 {
|
|
|
|
return params.IAVLMerkleProofValidateGas
|
|
|
|
}
|
|
|
|
|
|
|
|
// input:
|
|
|
|
// | payload length | payload |
|
|
|
|
// | 32 bytes | |
|
|
|
|
func (c *iavlMerkleProofValidate) Run(input []byte) (result []byte, err error) {
|
2022-10-11 11:16:30 +08:00
|
|
|
return c.basicIavlMerkleProofValidate.Run(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
// tmHeaderValidate implemented as a native contract.
|
|
|
|
type tmHeaderValidateNano struct{}
|
|
|
|
|
|
|
|
func (c *tmHeaderValidateNano) RequiredGas(input []byte) uint64 {
|
|
|
|
return params.TendermintHeaderValidateGas
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *tmHeaderValidateNano) Run(input []byte) (result []byte, err error) {
|
|
|
|
return nil, fmt.Errorf("suspend")
|
|
|
|
}
|
|
|
|
|
|
|
|
type iavlMerkleProofValidateNano struct{}
|
|
|
|
|
|
|
|
func (c *iavlMerkleProofValidateNano) RequiredGas(_ []byte) uint64 {
|
|
|
|
return params.IAVLMerkleProofValidateGas
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *iavlMerkleProofValidateNano) Run(_ []byte) (result []byte, err error) {
|
|
|
|
return nil, fmt.Errorf("suspend")
|
|
|
|
}
|
|
|
|
|
2023-02-02 19:36:37 +08:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------------------------------------
|
2022-10-11 11:16:30 +08:00
|
|
|
type iavlMerkleProofValidateMoran struct {
|
|
|
|
basicIavlMerkleProofValidate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *iavlMerkleProofValidateMoran) RequiredGas(_ []byte) uint64 {
|
|
|
|
return params.IAVLMerkleProofValidateGas
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *iavlMerkleProofValidateMoran) Run(input []byte) (result []byte, err error) {
|
|
|
|
c.basicIavlMerkleProofValidate.verifiers = []merkle.ProofOpVerifier{
|
|
|
|
forbiddenAbsenceOpVerifier,
|
|
|
|
singleValueOpVerifier,
|
|
|
|
multiStoreOpVerifier,
|
|
|
|
forbiddenSimpleValueOpVerifier,
|
|
|
|
}
|
|
|
|
return c.basicIavlMerkleProofValidate.Run(input)
|
|
|
|
}
|
|
|
|
|
2023-03-07 09:59:03 +08:00
|
|
|
type iavlMerkleProofValidatePlanck struct {
|
2023-03-01 15:49:05 +08:00
|
|
|
basicIavlMerkleProofValidate
|
|
|
|
}
|
|
|
|
|
2023-03-07 09:59:03 +08:00
|
|
|
func (c *iavlMerkleProofValidatePlanck) RequiredGas(_ []byte) uint64 {
|
2023-03-01 15:49:05 +08:00
|
|
|
return params.IAVLMerkleProofValidateGas
|
|
|
|
}
|
|
|
|
|
2023-03-07 09:59:03 +08:00
|
|
|
func (c *iavlMerkleProofValidatePlanck) Run(input []byte) (result []byte, err error) {
|
2023-04-18 15:51:26 +08:00
|
|
|
c.basicIavlMerkleProofValidate.proofRuntime = v1.Ics23CompatibleProofRuntime()
|
2023-03-01 15:49:05 +08:00
|
|
|
c.basicIavlMerkleProofValidate.verifiers = []merkle.ProofOpVerifier{
|
|
|
|
forbiddenAbsenceOpVerifier,
|
|
|
|
singleValueOpVerifier,
|
|
|
|
multiStoreOpVerifier,
|
|
|
|
forbiddenSimpleValueOpVerifier,
|
|
|
|
}
|
2023-03-06 08:38:39 +08:00
|
|
|
c.basicIavlMerkleProofValidate.keyVerifier = keyVerifier
|
|
|
|
c.basicIavlMerkleProofValidate.opsVerifier = proofOpsVerifier
|
2023-03-01 15:49:05 +08:00
|
|
|
return c.basicIavlMerkleProofValidate.Run(input)
|
|
|
|
}
|
|
|
|
|
2023-05-05 17:06:10 +08:00
|
|
|
type iavlMerkleProofValidatePlato struct {
|
|
|
|
basicIavlMerkleProofValidate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *iavlMerkleProofValidatePlato) RequiredGas(_ []byte) uint64 {
|
|
|
|
return params.IAVLMerkleProofValidateGas
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *iavlMerkleProofValidatePlato) Run(input []byte) (result []byte, err error) {
|
|
|
|
c.basicIavlMerkleProofValidate.proofRuntime = v1.Ics23ProofRuntime()
|
|
|
|
c.basicIavlMerkleProofValidate.verifiers = []merkle.ProofOpVerifier{
|
|
|
|
forbiddenAbsenceOpVerifier,
|
|
|
|
singleValueOpVerifier,
|
|
|
|
multiStoreOpVerifier,
|
|
|
|
forbiddenSimpleValueOpVerifier,
|
|
|
|
}
|
|
|
|
c.basicIavlMerkleProofValidate.keyVerifier = keyVerifier
|
|
|
|
c.basicIavlMerkleProofValidate.opsVerifier = proofOpsVerifier
|
|
|
|
return c.basicIavlMerkleProofValidate.Run(input)
|
|
|
|
}
|
|
|
|
|
2023-03-01 15:49:05 +08:00
|
|
|
func successfulMerkleResult() []byte {
|
|
|
|
result := make([]byte, merkleProofValidateResultLength)
|
|
|
|
binary.BigEndian.PutUint64(result[merkleProofValidateResultLength-uint64TypeLength:], 0x01)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-10-11 11:16:30 +08:00
|
|
|
type basicIavlMerkleProofValidate struct {
|
2023-04-18 15:51:26 +08:00
|
|
|
keyVerifier v1.KeyVerifier
|
2023-03-06 08:38:39 +08:00
|
|
|
opsVerifier merkle.ProofOpsVerifier
|
2023-03-01 15:49:05 +08:00
|
|
|
verifiers []merkle.ProofOpVerifier
|
|
|
|
proofRuntime *merkle.ProofRuntime
|
2022-10-11 11:16:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *basicIavlMerkleProofValidate) Run(input []byte) (result []byte, err error) {
|
2020-05-20 12:16:09 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("internal error: %v\n", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if uint64(len(input)) <= precompileContractInputMetaDataLength {
|
|
|
|
return nil, fmt.Errorf("invalid input: input should include %d bytes payload length and payload", precompileContractInputMetaDataLength)
|
|
|
|
}
|
|
|
|
|
2020-08-04 13:56:13 +08:00
|
|
|
payloadLength := binary.BigEndian.Uint64(input[precompileContractInputMetaDataLength-uint64TypeLength : precompileContractInputMetaDataLength])
|
2020-05-20 12:16:09 +08:00
|
|
|
if uint64(len(input)) != payloadLength+precompileContractInputMetaDataLength {
|
|
|
|
return nil, fmt.Errorf("invalid input: input size should be %d, actual the size is %d", payloadLength+precompileContractInputMetaDataLength, len(input))
|
|
|
|
}
|
|
|
|
|
2023-04-18 15:51:26 +08:00
|
|
|
kvmp, err := v1.DecodeKeyValueMerkleProof(input[precompileContractInputMetaDataLength:])
|
2020-05-20 12:16:09 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-03-01 15:49:05 +08:00
|
|
|
if c.proofRuntime == nil {
|
2023-04-18 15:51:26 +08:00
|
|
|
kvmp.SetProofRuntime(v1.DefaultProofRuntime())
|
2023-03-01 15:49:05 +08:00
|
|
|
} else {
|
|
|
|
kvmp.SetProofRuntime(c.proofRuntime)
|
|
|
|
}
|
2022-10-11 11:16:30 +08:00
|
|
|
kvmp.SetVerifiers(c.verifiers)
|
2023-03-06 08:38:39 +08:00
|
|
|
kvmp.SetOpsVerifier(c.opsVerifier)
|
|
|
|
kvmp.SetKeyVerifier(c.keyVerifier)
|
|
|
|
|
2020-05-20 12:16:09 +08:00
|
|
|
valid := kvmp.Validate()
|
|
|
|
if !valid {
|
|
|
|
return nil, fmt.Errorf("invalid merkle proof")
|
|
|
|
}
|
|
|
|
|
2023-03-01 15:49:05 +08:00
|
|
|
return successfulMerkleResult(), nil
|
2020-05-20 12:16:09 +08:00
|
|
|
}
|
2022-10-07 11:46:38 +08:00
|
|
|
|
2022-10-11 11:16:30 +08:00
|
|
|
func forbiddenAbsenceOpVerifier(op merkle.ProofOperator) error {
|
|
|
|
if op == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if _, ok := op.(iavl.IAVLAbsenceOp); ok {
|
|
|
|
return cmn.NewError("absence proof suspend")
|
|
|
|
}
|
|
|
|
return nil
|
2022-10-07 11:46:38 +08:00
|
|
|
}
|
|
|
|
|
2022-10-11 11:16:30 +08:00
|
|
|
func forbiddenSimpleValueOpVerifier(op merkle.ProofOperator) error {
|
|
|
|
if op == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if _, ok := op.(merkle.SimpleValueOp); ok {
|
|
|
|
return cmn.NewError("simple value proof suspend")
|
|
|
|
}
|
|
|
|
return nil
|
2022-10-07 11:46:38 +08:00
|
|
|
}
|
|
|
|
|
2022-10-11 11:16:30 +08:00
|
|
|
func multiStoreOpVerifier(op merkle.ProofOperator) error {
|
|
|
|
if op == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-18 15:51:26 +08:00
|
|
|
if mop, ok := op.(v1.MultiStoreProofOp); ok {
|
2022-10-11 11:16:30 +08:00
|
|
|
storeNames := make(map[string]bool, len(mop.Proof.StoreInfos))
|
|
|
|
for _, store := range mop.Proof.StoreInfos {
|
|
|
|
if exist := storeNames[store.Name]; exist {
|
|
|
|
return cmn.NewError("duplicated store")
|
|
|
|
} else {
|
|
|
|
storeNames[store.Name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-10-07 11:46:38 +08:00
|
|
|
}
|
|
|
|
|
2022-10-11 11:16:30 +08:00
|
|
|
func singleValueOpVerifier(op merkle.ProofOperator) error {
|
|
|
|
if op == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if valueOp, ok := op.(iavl.IAVLValueOp); ok {
|
|
|
|
if len(valueOp.Proof.Leaves) != 1 {
|
|
|
|
return cmn.NewError("range proof suspended")
|
|
|
|
}
|
|
|
|
for _, innerNode := range valueOp.Proof.LeftPath {
|
|
|
|
if len(innerNode.Right) > 0 && len(innerNode.Left) > 0 {
|
|
|
|
return cmn.NewError("both right and left hash exit!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-10-07 11:46:38 +08:00
|
|
|
}
|
2023-03-06 08:38:39 +08:00
|
|
|
|
|
|
|
func proofOpsVerifier(poz merkle.ProofOperators) error {
|
|
|
|
if len(poz) != 2 {
|
|
|
|
return cmn.NewError("proof ops should be 2")
|
|
|
|
}
|
|
|
|
|
|
|
|
// for legacy proof type
|
2023-04-18 15:51:26 +08:00
|
|
|
if _, ok := poz[1].(v1.MultiStoreProofOp); ok {
|
2023-03-06 08:38:39 +08:00
|
|
|
if _, ok := poz[0].(iavl.IAVLValueOp); !ok {
|
|
|
|
return cmn.NewError("invalid proof op")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// for ics23 proof type
|
2023-04-18 15:51:26 +08:00
|
|
|
if op2, ok := poz[1].(v1.CommitmentOp); ok {
|
|
|
|
if op2.Type != v1.ProofOpSimpleMerkleCommitment {
|
2023-03-06 08:38:39 +08:00
|
|
|
return cmn.NewError("invalid proof op")
|
|
|
|
}
|
|
|
|
|
2023-04-18 15:51:26 +08:00
|
|
|
op1, ok := poz[0].(v1.CommitmentOp)
|
2023-03-06 08:38:39 +08:00
|
|
|
if !ok {
|
|
|
|
return cmn.NewError("invalid proof op")
|
|
|
|
}
|
|
|
|
|
2023-04-18 15:51:26 +08:00
|
|
|
if op1.Type != v1.ProofOpIAVLCommitment {
|
2023-03-06 08:38:39 +08:00
|
|
|
return cmn.NewError("invalid proof op")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmn.NewError("invalid proof type")
|
|
|
|
}
|
|
|
|
|
|
|
|
func keyVerifier(key string) error {
|
|
|
|
// https://github.com/bnb-chain/tendermint/blob/72375a6f3d4a72831cc65e73363db89a0073db38/crypto/merkle/proof_key_path.go#L88
|
|
|
|
// since the upper function is ambiguous, `x:00` can be decoded to both kind of key type
|
|
|
|
// we check the key here to make sure the key will not start from `x:`
|
|
|
|
if strings.HasPrefix(url.PathEscape(key), "x:") {
|
|
|
|
return cmn.NewError("key should not start with x:")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-18 15:51:26 +08:00
|
|
|
|
|
|
|
// cometBFTLightBlockValidate implemented as a native contract. Used to validate the light blocks for CometBFT v0.37.0
|
|
|
|
// and its compatible version. Besides, in order to support the BLS cross-chain infrastructure, the SetRelayerAddress
|
|
|
|
// and SetBlsKey methods should be implemented for the validator.
|
|
|
|
type cometBFTLightBlockValidate struct{}
|
|
|
|
|
|
|
|
func (c *cometBFTLightBlockValidate) RequiredGas(input []byte) uint64 {
|
|
|
|
return params.CometBFTLightBlockValidateGas
|
|
|
|
}
|
|
|
|
|
2023-06-20 18:04:18 +08:00
|
|
|
func (c *cometBFTLightBlockValidate) run(input []byte, isHertz bool) (result []byte, err error) {
|
2023-04-18 15:51:26 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("internal error: %v\n", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cs, block, err := v2.DecodeLightBlockValidationInput(input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-06-20 18:04:18 +08:00
|
|
|
validatorSetChanged, err := cs.ApplyLightBlock(block, isHertz)
|
2023-04-18 15:51:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
consensusStateBytes, err := cs.EncodeConsensusState()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result = v2.EncodeLightBlockValidationResult(validatorSetChanged, consensusStateBytes)
|
|
|
|
return result, nil
|
|
|
|
}
|
2023-06-20 18:04:18 +08:00
|
|
|
|
|
|
|
func (c *cometBFTLightBlockValidate) Run(input []byte) (result []byte, err error) {
|
|
|
|
return c.run(input, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
type cometBFTLightBlockValidateHertz struct {
|
|
|
|
cometBFTLightBlockValidate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cometBFTLightBlockValidateHertz) Run(input []byte) (result []byte, err error) {
|
|
|
|
return c.run(input, true)
|
|
|
|
}
|
2023-11-30 19:36:54 +08:00
|
|
|
|
|
|
|
// secp256k1SignatureRecover implemented as a native contract.
|
|
|
|
type secp256k1SignatureRecover struct{}
|
|
|
|
|
|
|
|
func (c *secp256k1SignatureRecover) RequiredGas(input []byte) uint64 {
|
|
|
|
return params.EcrecoverGas
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
tmPubKeyLength uint8 = 33
|
|
|
|
tmSignatureLength uint8 = 64
|
|
|
|
tmSignatureMsgHashLength uint8 = 32
|
|
|
|
)
|
|
|
|
|
|
|
|
// input:
|
|
|
|
// | tmPubKey | tmSignature | tmSignatureMsgHash |
|
|
|
|
// | 33 bytes | 64 bytes | 32 bytes |
|
|
|
|
func (c *secp256k1SignatureRecover) Run(input []byte) (result []byte, err error) {
|
|
|
|
if len(input) != int(tmPubKeyLength)+int(tmSignatureLength)+int(tmSignatureMsgHashLength) {
|
|
|
|
return nil, fmt.Errorf("invalid input")
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.runTMSecp256k1Signature(
|
|
|
|
input[:tmPubKeyLength],
|
|
|
|
input[tmPubKeyLength:tmPubKeyLength+tmSignatureLength],
|
|
|
|
input[tmPubKeyLength+tmSignatureLength:],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *secp256k1SignatureRecover) runTMSecp256k1Signature(pubkey, signatureStr, msgHash []byte) (result []byte, err error) {
|
|
|
|
tmPubKey := secp256k1.PubKeySecp256k1(pubkey)
|
|
|
|
ok := tmPubKey.VerifyBytesWithMsgHash(msgHash, signatureStr)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid signature")
|
|
|
|
}
|
|
|
|
return tmPubKey.Address().Bytes(), nil
|
|
|
|
}
|