-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkv_tx.go
143 lines (121 loc) · 3.02 KB
/
kv_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package sdk
import (
"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"
)
var KVTxType = []byte("kvTx-")
type KVTx struct {
AccountBase
Key []byte `json:"key"`
Value []byte `json:"value"`
}
type KVResult struct {
Key []byte `json:"key"`
Value []byte `json:"value"`
}
func (gs *GoSDK) kvPut(kvTx *KVTx, funcType string) (string, error) {
if kvTx.PrivKey == "" {
return "", fmt.Errorf("account privkey is empty")
}
if strings.Index(kvTx.PrivKey, "0x") == 0 {
kvTx.PrivKey = kvTx.PrivKey[2:]
}
privBytes := common.Hex2Bytes(kvTx.PrivKey)
nonce := kvTx.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
}
}
addrBytes, err := gs.getAddrBytes(privBytes)
if err != nil {
return "", err
}
if nonce == 0 {
nonce, err = gs.getNonce(common.Bytes2Hex(addrBytes))
if err != nil {
return "", err
}
}
kvBytes, err := rlp.EncodeToBytes(&KVResult{Key: kvTx.Key, Value: kvTx.Value})
if err != nil {
return "", err
}
txdata := append(KVTxType, kvBytes...)
tx := types.NewTransaction(nonce, common.Address{}, big.NewInt(0), gs.GasLimit(), big.NewInt(0), txdata)
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) kvGet(key []byte) ([]byte, error) {
query := append([]byte{types.QueryType_Key}, key...)
res := new(types.ResultQuery)
err := gs.sendTxCall("query", query, res)
if err != nil {
return nil, err
}
if 0 != res.Result.Code {
return nil, fmt.Errorf(string(res.Result.Log))
}
return res.Result.Data, nil
}
func (gs *GoSDK) kvGetWithPrefix(prefix, lastKey []byte, limit uint32) ([]*KVResult, error) {
params := struct {
Prefix []byte
LastKey []byte
Limit uint32
}{prefix, lastKey, limit}
bytParams, err := rlp.EncodeToBytes(params)
if err != nil {
return nil, err
}
query := append([]byte{types.QueryType_Key_Prefix}, bytParams...)
res := new(types.ResultQuery)
if err = gs.sendTxCall("query", query, res); err != nil {
return nil, err
}
if 0 != res.Result.Code {
return nil, fmt.Errorf(string(res.Result.Log))
}
kvs := make([]*KVResult, 0)
if err := rlp.DecodeBytes(res.Result.Data, &kvs); err != nil {
return nil, err
}
return kvs, nil
}
func (gs *GoSDK) kvTxSigned(tx, funcType string) (string, error) {
if strings.HasPrefix(tx, "0x") {
tx = tx[2:]
}
txBytes := common.Hex2Bytes(tx)
rpcResult := new(types.ResultBroadcastTxCommit)
err := gs.sendTxCall(funcType, txBytes, rpcResult)
if err != nil {
return "", err
}
return rpcResult.TxHash, nil
}