Skip to content

Commit

Permalink
add transferv2 to sigsvr (#1387)
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonZhouPW authored Feb 18, 2022
1 parent 6e16709 commit dbefccb
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmd/sigsvr/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func init() {
DefCliRpcSvr.RegHandler("sigrawtx", handlers.SigRawTransaction)
DefCliRpcSvr.RegHandler("sigmutilrawtx", handlers.SigMutilRawTransaction)
DefCliRpcSvr.RegHandler("sigtransfertx", handlers.SigTransferTransaction)
DefCliRpcSvr.RegHandler("sigtransfertxv2", handlers.SigTransferTransactionV2)
DefCliRpcSvr.RegHandler("signeovminvoketx", handlers.SigNeoVMInvokeTx)
DefCliRpcSvr.RegHandler("signeovminvokeabitx", handlers.SigNeoVMInvokeAbiTx)
DefCliRpcSvr.RegHandler("signativeinvoketx", handlers.SigNativeInvokeTx)
Expand Down
59 changes: 59 additions & 0 deletions cmd/sigsvr/handlers/sig_transfer_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package handlers
import (
"encoding/hex"
"encoding/json"
"math/big"
"strconv"

clisvrcom "github.com/ontio/ontology/cmd/sigsvr/common"
Expand Down Expand Up @@ -104,3 +105,61 @@ func SigTransferTransaction(req *clisvrcom.CliRpcRequest, resp *clisvrcom.CliRpc
SignedTx: hex.EncodeToString(sink.Bytes()),
}
}

func SigTransferTransactionV2(req *clisvrcom.CliRpcRequest, resp *clisvrcom.CliRpcResponse) {
rawReq := &SigTransferTransactionReq{}
err := json.Unmarshal(req.Params, rawReq)
if err != nil {
resp.ErrorCode = clisvrcom.CLIERR_INVALID_PARAMS
return
}
amount, f := new(big.Int).SetString(rawReq.Amount, 10)

if !f {
resp.ErrorCode = clisvrcom.CLIERR_INVALID_PARAMS
return
}
mutable, err := cliutil.TransferTxV2(rawReq.GasPrice, rawReq.GasLimit, rawReq.Asset, rawReq.From, rawReq.To, amount)
if err != nil {
log.Infof("Cli Qid:%s SigTransferTransaction TransferTx error:%s", req.Qid, err)
resp.ErrorCode = clisvrcom.CLIERR_INVALID_PARAMS
return
}
if rawReq.Payer != "" {
payerAddress, err := common.AddressFromBase58(rawReq.Payer)
if err != nil {
log.Infof("Cli Qid:%s SigTransferTransaction AddressFromBase58 error:%s", req.Qid, err)
resp.ErrorCode = clisvrcom.CLIERR_INVALID_PARAMS
return
}
mutable.Payer = payerAddress
}

signer, err := req.GetAccount()
if err != nil {
log.Infof("Cli Qid:%s SigTransferTransaction GetAccount:%s", req.Qid, err)
resp.ErrorCode = clisvrcom.CLIERR_ACCOUNT_UNLOCK
return
}
if signer == nil {
resp.ErrorCode = clisvrcom.CLIERR_ACCOUNT_UNLOCK
return
}
err = cliutil.SignTransaction(signer, mutable)
if err != nil {
log.Infof("Cli Qid:%s SigTransferTransaction SignTransaction error:%s", req.Qid, err)
resp.ErrorCode = clisvrcom.CLIERR_INTERNAL_ERR
return
}
tx, err := mutable.IntoImmutable()
if err != nil {
log.Infof("Cli Qid:%s SigTransferTransaction tx IntoInmmutable error:%s", req.Qid, err)
resp.ErrorCode = clisvrcom.CLIERR_INTERNAL_ERR
return
}
sink := common.ZeroCopySink{}
tx.Serialization(&sink)
resp.Result = &SinTransferTransactionRsp{
SignedTx: hex.EncodeToString(sink.Bytes()),
}
}
39 changes: 39 additions & 0 deletions cmd/utils/ont.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ import (
"encoding/json"
"fmt"
"io"
"math/big"
"math/rand"
"sort"
"strconv"
"strings"
"time"

"github.com/laizy/bigint"
"github.com/ontio/ontology-crypto/keypair"
sig "github.com/ontio/ontology-crypto/signature"
"github.com/ontio/ontology/account"
Expand All @@ -38,6 +40,7 @@ import (
"github.com/ontio/ontology/common/serialization"
"github.com/ontio/ontology/core/payload"
"github.com/ontio/ontology/core/signature"
"github.com/ontio/ontology/core/states"
"github.com/ontio/ontology/core/types"
cutils "github.com/ontio/ontology/core/utils"
httpcom "github.com/ontio/ontology/http/base/common"
Expand All @@ -50,6 +53,7 @@ const (
VERSION_CONTRACT_ONT = byte(0)
VERSION_CONTRACT_ONG = byte(0)
CONTRACT_TRANSFER = "transfer"
CONTRACT_TRANSFER_V2 = "transferV2"
CONTRACT_TRANSFER_FROM = "transferFrom"
CONTRACT_APPROVE = "approve"

Expand Down Expand Up @@ -243,6 +247,41 @@ func TransferTx(gasPrice, gasLimit uint64, asset, from, to string, amount uint64
return mutableTx, nil
}

func TransferTxV2(gasPrice, gasLimit uint64, asset, from, to string, amount *big.Int) (*types.MutableTransaction, error) {
fromAddr, err := common.AddressFromBase58(from)
if err != nil {
return nil, fmt.Errorf("from address:%s invalid:%s", from, err)
}
toAddr, err := common.AddressFromBase58(to)
if err != nil {
return nil, fmt.Errorf("to address:%s invalid:%s", to, err)
}
var sts []*ont.TransferStateV2
sts = append(sts, &ont.TransferStateV2{
From: fromAddr,
To: toAddr,
Value: states.NativeTokenBalance{Balance: bigint.New(amount)},
})
var version byte
var contractAddr common.Address
switch strings.ToLower(asset) {
case ASSET_ONT:
version = VERSION_CONTRACT_ONT
contractAddr = utils.OntContractAddress
case ASSET_ONG:
version = VERSION_CONTRACT_ONG
contractAddr = utils.OngContractAddress
default:
return nil, fmt.Errorf("unsupport asset:%s", asset)
}
invokeCode, err := cutils.BuildNativeInvokeCode(contractAddr, version, CONTRACT_TRANSFER_V2, []interface{}{sts})
if err != nil {
return nil, fmt.Errorf("build invoke code error:%s", err)
}
mutableTx := NewInvokeTransaction(gasPrice, gasLimit, invokeCode)
return mutableTx, nil
}

func TransferFromTx(gasPrice, gasLimit uint64, asset, sender, from, to string, amount uint64) (*types.MutableTransaction, error) {
senderAddr, err := common.AddressFromBase58(sender)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions core/utils/transaction_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"reflect"
"time"

"github.com/laizy/bigint"

"github.com/ontio/ontology/common"
"github.com/ontio/ontology/core/payload"
"github.com/ontio/ontology/core/types"
Expand Down Expand Up @@ -130,6 +132,8 @@ func BuildNeoVMParam(builder *vm.ParamsBuilder, smartContractParams []interface{
builder.EmitPushByteArray([]byte(v))
case *big.Int:
builder.EmitPushInteger(v)
case bigint.Int:
builder.EmitPushInteger(v.BigInt())
case []byte:
builder.EmitPushByteArray(v)
case common.Address:
Expand Down
62 changes: 57 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ go 1.17

require (
github.com/JohnCGriffin/overflow v0.0.0-20170615021017-4d914c927216
github.com/Workiva/go-datastructures v1.0.50 // indirect
github.com/blang/semver v3.5.1+incompatible
github.com/emirpasic/gods v1.12.0 // indirect
github.com/ethereum/go-ethereum v1.9.25
github.com/gammazero/workerpool v1.1.2
github.com/gorilla/websocket v1.4.1
github.com/gosuri/uilive v0.0.3 // indirect
github.com/gosuri/uiprogress v0.0.1
github.com/graph-gophers/graphql-go v1.2.1-0.20210916100229-446a2dd13dd5
github.com/hashicorp/golang-lru v0.5.4
Expand All @@ -19,11 +16,9 @@ require (
github.com/itchyny/base58-go v0.1.0
github.com/json-iterator/go v1.1.10
github.com/laizy/bigint v0.1.3
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/ontio/ontology-crypto v1.2.1
github.com/ontio/ontology-eventbus v0.9.1
github.com/ontio/wagon v0.4.2
github.com/orcaman/concurrent-map v0.0.0-20210501183033-44dafcb38ecc // indirect
github.com/pborman/uuid v1.2.0
github.com/prometheus/client_golang v0.9.1
github.com/scylladb/go-set v1.0.2
Expand All @@ -34,6 +29,63 @@ require (
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d
)

require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/VictoriaMetrics/fastcache v1.5.7 // indirect
github.com/Workiva/go-datastructures v1.0.50 // indirect
github.com/aristanetworks/goarista v0.0.0-20170210015632-ea17b1a17847 // indirect
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea // indirect
github.com/edsrzf/mmap-go v0.0.0-20160512033002-935e0e8a636c // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/gammazero/deque v0.1.0 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20190607065134-2772fd86a8ff // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/gogo/protobuf v1.1.1 // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/golang/snappy v0.0.3-0.20201103224600-674baa8c7fc3 // indirect
github.com/google/uuid v1.0.0 // indirect
github.com/gosuri/uilive v0.0.3 // indirect
github.com/huin/goupnp v1.0.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2-0.20160603034137-1fa385a6f458 // indirect
github.com/karalabe/usb v0.0.0-20190919080040-51dc0efba356 // indirect
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/olekukonko/tablewriter v0.0.2-0.20190409134802-7e037d187b0c // indirect
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/orcaman/concurrent-map v0.0.0-20210501183033-44dafcb38ecc // indirect
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 // indirect
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce // indirect
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d // indirect
github.com/prometheus/tsdb v0.6.2-0.20190402121629-4f204dcbc150 // indirect
github.com/rjeczalik/notify v0.9.1 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shirou/gopsutil v2.20.5+incompatible // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4 // indirect
github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570 // indirect
github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3 // indirect
github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef // indirect
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208 // indirect
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
golang.org/x/text v0.3.6 // indirect
google.golang.org/protobuf v1.23.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)

replace (
golang.org/x/crypto => github.com/golang/crypto v0.0.0-20210921155107-089bfa567519
golang.org/x/net => github.com/golang/net v0.0.0-20210924151903-3ad01bbaa167
Expand Down

0 comments on commit dbefccb

Please sign in to comment.