Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ontevm #78

Open
wants to merge 1 commit into
base: storage
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions contracts/native/cross_chain_manager/common/param.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/contracts/native"
"github.com/ethereum/go-ethereum/rlp"
Expand Down Expand Up @@ -108,6 +109,48 @@ func (m *MakeTxParam) DecodeRLP(s *rlp.Stream) error {
return nil
}

type MakeTxParamWithSender struct {
Sender common.Address
MakeTxParam
}

//used for param from evm contract
type MakeTxParamWithSenderShim struct {
Sender common.Address
MakeTxParam []byte
}

func (this *MakeTxParamWithSender) Deserialization(data []byte) (err error) {

BytesTy, _ := abi.NewType("bytes", "", nil)
AddrTy, _ := abi.NewType("address", "", nil)
// StringTy, _ := abi.NewType("string", "", nil)

TxParam := abi.Arguments{
{Type: AddrTy, Name: "sender"},
{Type: BytesTy, Name: "makeTxParam"},
}

args, err := TxParam.Unpack(data)
if err != nil {
return
}

shim := new(MakeTxParamWithSenderShim)
err = TxParam.Copy(shim, args)
if err != nil {
return
}

this.Sender = shim.Sender
makeTxParam, err := DecodeTxParam(shim.MakeTxParam)
if err != nil {
return
}
this.MakeTxParam = *makeTxParam
return
}

//used for param from evm contract
type MakeTxParamShim struct {
TxHash []byte
Expand Down
3 changes: 3 additions & 0 deletions contracts/native/cross_chain_manager/entrance.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/ethereum/go-ethereum/contracts/native/cross_chain_manager/heco"
"github.com/ethereum/go-ethereum/contracts/native/cross_chain_manager/msc"
"github.com/ethereum/go-ethereum/contracts/native/cross_chain_manager/okex"
"github.com/ethereum/go-ethereum/contracts/native/cross_chain_manager/ont"
"github.com/ethereum/go-ethereum/contracts/native/cross_chain_manager/polygon"
"github.com/ethereum/go-ethereum/contracts/native/cross_chain_manager/quorum"
"github.com/ethereum/go-ethereum/contracts/native/cross_chain_manager/zilliqa"
Expand Down Expand Up @@ -88,6 +89,8 @@ func GetChainHandler(router uint64) (scom.ChainHandler, error) {
return polygon.NewHandler(), nil
case utils.COSMOS_ROUTER:
return cosmos.NewCosmosHandler(), nil
case utils.ONT_ROUTER:
return ont.NewONTHandler(), nil
case utils.ZILLIQA_ROUTER:
return zilliqa.NewHandler(), nil
default:
Expand Down
152 changes: 152 additions & 0 deletions contracts/native/cross_chain_manager/ont/merkle/common/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright (C) 2021 The Zion Authors
* This file is part of The Zion library.
*
* The Zion is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Zion is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The Zion. If not, see <http://www.gnu.org/licenses/>.
*/

package common

import (
"crypto/sha256"
"errors"
"fmt"
"io"
"math/big"

"github.com/itchyny/base58-go"
"golang.org/x/crypto/ripemd160"
)

const ADDR_LEN = 20

type Address [ADDR_LEN]byte

var ADDRESS_EMPTY = Address{}

// ToHexString returns hex string representation of Address
func (self *Address) ToHexString() string {
return fmt.Sprintf("%x", ToArrayReverse(self[:]))
}

// Serialize serialize Address into io.Writer
func (self *Address) Serialization(sink *ZeroCopySink) {
sink.WriteAddress(*self)
}

// Deserialize deserialize Address from io.Reader
func (self *Address) Deserialization(source *ZeroCopySource) error {
var eof bool
*self, eof = source.NextAddress()
if eof {
return io.ErrUnexpectedEOF
}
return nil
}

// Serialize serialize Address into io.Writer
func (self *Address) Serialize(w io.Writer) error {
_, err := w.Write(self[:])
return err
}

// Deserialize deserialize Address from io.Reader
func (self *Address) Deserialize(r io.Reader) error {
_, err := io.ReadFull(r, self[:])
if err != nil {
return errors.New("deserialize Address error")
}
return nil
}

// ToBase58 returns base58 encoded address string
func (f *Address) ToBase58() string {
data := append([]byte{23}, f[:]...)
temp := sha256.Sum256(data)
temps := sha256.Sum256(temp[:])
data = append(data, temps[0:4]...)

bi := new(big.Int).SetBytes(data).String()
encoded, _ := base58.BitcoinEncoding.Encode([]byte(bi))
return string(encoded)
}

// AddressParseFromBytes returns parsed Address
func AddressParseFromBytes(f []byte) (Address, error) {
if len(f) != ADDR_LEN {
return ADDRESS_EMPTY, errors.New("[Common]: AddressParseFromBytes err, len != 20")
}

var addr Address
copy(addr[:], f)
return addr, nil
}

// AddressParseFromHexString returns parsed Address
func AddressFromHexString(s string) (Address, error) {
hx, err := HexToBytes(s)
if err != nil {
return ADDRESS_EMPTY, err
}
return AddressParseFromBytes(ToArrayReverse(hx))
}

const maxSize = 2048

// AddressFromBase58 returns Address from encoded base58 string
func AddressFromBase58(encoded string) (Address, error) {
if encoded == "" {
return ADDRESS_EMPTY, errors.New("invalid address")
}
if len(encoded) > maxSize {
return ADDRESS_EMPTY, errors.New("invalid address")
}
decoded, err := base58.BitcoinEncoding.Decode([]byte(encoded))
if err != nil {
return ADDRESS_EMPTY, err
}

x, ok := new(big.Int).SetString(string(decoded), 10)
if !ok {
return ADDRESS_EMPTY, errors.New("invalid address")
}

buf := x.Bytes()
if len(buf) != 1+ADDR_LEN+4 || buf[0] != byte(23) {
return ADDRESS_EMPTY, errors.New("wrong encoded address")
}

ph, err := AddressParseFromBytes(buf[1:21])
if err != nil {
return ADDRESS_EMPTY, err
}

addr := ph.ToBase58()

if addr != encoded {
return ADDRESS_EMPTY, errors.New("[AddressFromBase58]: decode encoded verify failed.")
}

return ph, nil
}

func AddressFromVmCode(code []byte) Address {
var addr Address
temp := sha256.Sum256(code)
md := ripemd160.New()
md.Write(temp[:])
md.Sum(addr[:0])

return addr
}
57 changes: 57 additions & 0 deletions contracts/native/cross_chain_manager/ont/merkle/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2021 The Zion Authors
* This file is part of The Zion library.
*
* The Zion is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Zion is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The Zion. If not, see <http://www.gnu.org/licenses/>.
*/

package common

import (
"encoding/hex"
"math/rand"
"os"
)

// GetNonce returns random nonce
func GetNonce() uint64 {
// Fixme replace with the real random number generator
nonce := uint64(rand.Uint32())<<32 + uint64(rand.Uint32())
return nonce
}

// ToHexString convert []byte to hex string
func ToHexString(data []byte) string {
return hex.EncodeToString(data)
}

// HexToBytes convert hex string to []byte
func HexToBytes(value string) ([]byte, error) {
return hex.DecodeString(value)
}

func ToArrayReverse(arr []byte) []byte {
l := len(arr)
x := make([]byte, 0)
for i := l - 1; i >= 0; i-- {
x = append(x, arr[i])
}
return x
}

// FileExisted checks whether filename exists in filesystem
func FileExisted(filename string) bool {
_, err := os.Stat(filename)
return err == nil || os.IsExist(err)
}
41 changes: 41 additions & 0 deletions contracts/native/cross_chain_manager/ont/merkle/common/safeMath.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2021 The Zion Authors
* This file is part of The Zion library.
*
* The Zion is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Zion is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The Zion. If not, see <http://www.gnu.org/licenses/>.
*/

package common

import "math"

const (
MAX_UINT64 = math.MaxUint64
MAX_INT64 = math.MaxInt64
)

func SafeSub(x, y uint64) (uint64, bool) {
return x - y, x < y
}

func SafeAdd(x, y uint64) (uint64, bool) {
return x + y, y > MAX_UINT64-x
}

func SafeMul(x, y uint64) (uint64, bool) {
if x == 0 || y == 0 {
return 0, false
}
return x * y, y > MAX_UINT64/x
}
Loading