Skip to content

Commit

Permalink
Merge branch 'v1.0.2-rc1-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Feb 20, 2020
2 parents 8eb9de5 + e0430e0 commit 4c244a4
Show file tree
Hide file tree
Showing 44 changed files with 1,993 additions and 1,126 deletions.
50 changes: 50 additions & 0 deletions accounts/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"encoding/json"
"fmt"
"io"

"github.com/sero-cash/go-czero-import/c_type"
)

// The ABI holds information about a contract's context and available
Expand Down Expand Up @@ -49,10 +51,39 @@ func JSON(reader io.Reader) (ABI, error) {
// of 4 bytes and arguments are all 32 bytes.
// Method ids are created from the first 4 bytes of the hash of the
// methods string signature. (signature = baz(uint32,string32))

func (abi ABI) PackPrefix(name string, rand c_type.Uint128, args ...interface{}) ([]byte, error) {
var ret []byte
ret = append(ret, rand[:]...)
if name == "" {
// constructor
addressPrefix, err := abi.Constructor.Inputs.PackPrefix(args...)
if err != nil {
return nil, err
}
ret = append(ret, addressPrefix...)
return ret, nil

}
method, exist := abi.Methods[name]
if !exist {
return nil, fmt.Errorf("method '%s' not found", name)
}

addressPrefix, err := method.Inputs.PackPrefix(args...)
if err != nil {
return nil, err
}
ret = append(ret, addressPrefix...)

return ret, nil
}

func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
// Fetch the ABI of the requested method
if name == "" {
// constructor

arguments, err := abi.Constructor.Inputs.Pack(args...)
if err != nil {
return nil, err
Expand Down Expand Up @@ -91,6 +122,25 @@ func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) {
return fmt.Errorf("abi: could not locate named method or event")
}

// UnpackIntoMap unpacks a log into the provided map[string]interface{}
func (abi ABI) UnpackIntoMap(v map[string]interface{}, name string, data []byte) (err error) {
if len(data) == 0 {
return fmt.Errorf("abi: unmarshalling empty output")
}
// since there can't be naming collisions with contracts and events,
// we need to decide whether we're calling a method or an event
if method, ok := abi.Methods[name]; ok {
if len(data)%32 != 0 {
return fmt.Errorf("abi: improperly formatted output")
}
return method.Outputs.UnpackIntoMap(v, data)
}
if event, ok := abi.Events[name]; ok {
return event.Inputs.UnpackIntoMap(v, data)
}
return fmt.Errorf("abi: could not locate named method or event")
}

// UnmarshalJSON implements json.Unmarshaler interface
func (abi *ABI) UnmarshalJSON(data []byte) error {
var fields []struct {
Expand Down
53 changes: 53 additions & 0 deletions accounts/abi/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ package abi
import (
"encoding/json"
"fmt"
"math/big"
"reflect"
"strings"

"github.com/sero-cash/go-sero/common/math"

"github.com/sero-cash/go-czero-import/c_type"
)

// Argument holds the name of the argument and the corresponding type.
Expand Down Expand Up @@ -100,6 +105,29 @@ func (arguments Arguments) Unpack(v interface{}, data []byte) error {
return arguments.unpackAtomic(v, marshalledValues)
}

// UnpackIntoMap performs the operation hexdata -> mapping of argument name to argument value
func (arguments Arguments) UnpackIntoMap(v map[string]interface{}, data []byte) error {
marshalledValues, err := arguments.UnpackValues(data)
if err != nil {
return err
}

return arguments.unpackIntoMap(v, marshalledValues)
}

// unpackIntoMap unpacks marshalledValues into the provided map[string]interface{}
func (arguments Arguments) unpackIntoMap(v map[string]interface{}, marshalledValues []interface{}) error {
// Make sure map is not nil
if v == nil {
return fmt.Errorf("abi: cannot unpack into a nil map")
}

for i, arg := range arguments.NonIndexed() {
v[arg.Name] = marshalledValues[i]
}
return nil
}

func (arguments Arguments) unpackTuple(v interface{}, marshalledValues []interface{}) error {

var (
Expand Down Expand Up @@ -229,6 +257,31 @@ func (arguments Arguments) PackValues(args []interface{}) ([]byte, error) {
return arguments.Pack(args...)
}

func (arguments Arguments) PackPrefix(args ...interface{}) ([]byte, error) {
abiArgs := arguments
if len(args) != len(abiArgs) {
return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(abiArgs))
}
var result []c_type.PKr
for i, a := range args {
input := abiArgs[i]
// pack the input
pkrs, err := input.Type.getAllAddress(reflect.ValueOf(a))
if err != nil {
return nil, err
}
result = append(result, pkrs...)
}
var ret []byte
lenBytes := math.PaddedBigBytes(big.NewInt(int64(len(result))), 2)
ret = append(ret, lenBytes...)
for _, pkr := range result {
ret = append(ret, pkr[:]...)
}
return ret, nil

}

// Pack performs the operation Go format -> Hexdata
func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
// Make sure arguments match up and pack them
Expand Down
89 changes: 89 additions & 0 deletions accounts/abi/bind/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library 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 go-ethereum library 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package bind

import (
"encoding/binary"
"io"
"io/ioutil"
"math/big"

"github.com/sero-cash/go-sero/zero/txtool"
"github.com/sero-cash/go-sero/zero/txtool/flight"

"github.com/sero-cash/go-czero-import/superzk"

"github.com/sero-cash/go-czero-import/c_type"

"github.com/sero-cash/go-sero/accounts/keystore"
"github.com/sero-cash/go-sero/crypto"
)

// NewTransactor is a utility method to easily create a transaction signer from
// an encrypted json key stream and the associated passphrase.
func NewTransactor(keyin io.Reader, passphrase string, value *big.Int) (*TransactOpts, error) {
superzk.ZeroInit_NoCircuit()
json, err := ioutil.ReadAll(keyin)
if err != nil {
return nil, err
}
key, err := keystore.DecryptKey(json, passphrase)
if err != nil {
return nil, err
}
fromPkr := GetMainPkr(key)

return NewKeyedTransactor(key, fromPkr, value), nil
}

func encodeNumber(number uint64) []byte {
enc := make([]byte, 8)
binary.BigEndian.PutUint64(enc, number)
return enc
}

func GetMainPkr(key *keystore.Key) c_type.PKr {

salt := encodeNumber(1)
//log.Info("GenIndexPKr", "salt", hexutil.Encode(salt))
random := append(key.Tk[:], salt...)
r := crypto.Keccak256Hash(random).HashToUint256()
pk := key.Address.ToUint512()
return superzk.Pk2PKr(&pk, r)
}

// NewKeyedTransactor is a utility method to easily create a transaction signer
// from a single private key.
func NewKeyedTransactor(key *keystore.Key, refundTo c_type.PKr, value *big.Int) *TransactOpts {
tk := crypto.PrivkeyToTk(key.PrivateKey, key.Version)
return &TransactOpts{
From: tk.ToPk(),
FromPKr: refundTo,
Value: value,
Encrypter: func(txParam *txtool.GTxParam) (*txtool.GTx, error) {
priKey := crypto.FromECDSA(key.PrivateKey)
var seed c_type.Uint256
copy(seed[:], priKey[:])
sk := superzk.Seed2Sk(&seed, key.Version)
gtx, err := flight.SignTx(&sk, txParam)
if err != nil {
return nil, err
}
return &gtx, nil
},
}
}
8 changes: 6 additions & 2 deletions accounts/abi/bind/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"errors"
"math/big"

"github.com/sero-cash/go-sero/zero/txtool"

sero "github.com/sero-cash/go-sero"
"github.com/sero-cash/go-sero/common"
"github.com/sero-cash/go-sero/core/types"
Expand Down Expand Up @@ -80,8 +82,10 @@ type ContractTransactor interface {
// transactions may be added or removed by miners, but it should provide a basis
// for setting a reasonable default.
EstimateGas(ctx context.Context, call sero.CallMsg) (gas uint64, err error)
// SendTransaction injects the transaction into the pending pool for execution.
SendTransaction(ctx context.Context, tx *types.Transaction) error

GenContractTx(ctx context.Context, msg sero.CallMsg) (*txtool.GTxParam, error)

CommitTx(ctx context.Context, arg *txtool.GTx) error
}

// ContractFilterer defines the methods needed to access log events using one-off
Expand Down
Loading

0 comments on commit 4c244a4

Please sign in to comment.