-
Notifications
You must be signed in to change notification settings - Fork 3
/
tx.go
113 lines (95 loc) · 2.66 KB
/
tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright © 2017 ZhongAn Technology
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sdk
import (
"encoding/hex"
"fmt"
"math/big"
"strings"
"github.com/dappledger/ann-go-sdk/common"
"github.com/dappledger/ann-go-sdk/rlp"
"github.com/dappledger/ann-go-sdk/types"
)
const maxPayloadSize = 4096
type Tx struct {
AccountBase
To string `json:"to"` //to address;
Payload string `json:"payload"`
Value *big.Int `json:"value"`
}
func (gs *GoSDK) sendTx(sendTx *Tx, funcType string) (hash string, err error) {
if sendTx.PrivKey == "" {
return "", fmt.Errorf("account privkey is empty")
}
if strings.Index(sendTx.PrivKey, "0x") == 0 {
sendTx.PrivKey = sendTx.PrivKey[2:]
}
privBytes := common.Hex2Bytes(sendTx.PrivKey)
nonce := sendTx.Nonce
if nonce == 0 {
addrBytes, err := gs.getAddrBytes(privBytes)
if err != nil {
return "", err
}
nonce, err = gs.getNonce(common.Bytes2Hex(addrBytes))
if err != nil {
return "", err
}
}
to := common.HexToAddress(sendTx.To)
value := sendTx.Value
payload := sendTx.Payload
if len(payload) > maxPayloadSize {
err = fmt.Errorf("payload length must be less than 4096")
return "", err
}
data := []byte(payload)
tx := types.NewTransaction(nonce, to, value, gs.GasLimit(), big.NewInt(0), data)
signer, sig, err := gs.signTx(privBytes, tx)
if err != nil {
return "", err
}
sigTx, err := tx.WithSignature(signer, sig)
if err != nil {
return "", err
}
txBytes, err := rlp.EncodeToBytes(sigTx)
if err != nil {
return "", err
}
rpcResult := new(types.ResultBroadcastTxCommit)
err = gs.sendTxCall(funcType, txBytes, rpcResult)
if err != nil {
return "", err
}
return rpcResult.TxHash, nil
}
func (gs *GoSDK) txPayLoad(txStr string) (string, error) {
if strings.Index(txStr, "0x") == 0 {
txStr = txStr[2:]
}
txHash, err := hex.DecodeString(txStr)
if err != nil {
return "", err
}
query := make([]byte, len(txHash)+1)
query[0] = QueryTxExecution
copy(query[1:], txHash)
query = append([]byte{types.QueryType_PayLoad}, query...)
res := new(types.ResultQuery)
err = gs.sendTxCall("query", query, res)
if err != nil {
return "", err
}
return string(res.Result.Data), nil
}