-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransactions.go
216 lines (197 loc) · 5.96 KB
/
transactions.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import "C"
import (
"encoding/hex"
"encoding/json"
"math"
"strconv"
dcrwallet "decred.org/dcrwallet/v4/wallet"
"github.com/decred/dcrd/txscript/v4/stdaddr"
"github.com/decred/libwallet/asset/dcr"
)
const defaultAccount = "default"
//export createSignedTransaction
func createSignedTransaction(cName, cCreateSignedTxJSONReq *C.char) *C.char {
w, exists := loadedWallet(cName)
if !exists {
return errCResponse("wallet with name %q does not exist", goString(cName))
}
signSendJSONReq := goString(cCreateSignedTxJSONReq)
var req CreateSignedTxReq
if err := json.Unmarshal([]byte(signSendJSONReq), &req); err != nil {
return errCResponse("malformed sign send request: %v", err)
}
outputs := make([]*dcr.Output, len(req.Outputs))
for i, out := range req.Outputs {
o := &dcr.Output{
Address: out.Address,
Amount: uint64(out.Amount),
}
outputs[i] = o
}
inputs := make([]*dcr.Input, len(req.Inputs))
for i, in := range req.Inputs {
o := &dcr.Input{
TxID: in.TxID,
Vout: uint32(in.Vout),
}
inputs[i] = o
}
ignoreInputs := make([]*dcr.Input, len(req.IgnoreInputs))
for i, in := range req.IgnoreInputs {
o := &dcr.Input{
TxID: in.TxID,
Vout: uint32(in.Vout),
}
ignoreInputs[i] = o
}
if err := w.MainWallet().Unlock(w.ctx, []byte(req.Password), nil); err != nil {
return errCResponse("cannot unlock wallet: %v", err)
}
defer w.MainWallet().Lock()
txBytes, txhash, fee, err := w.CreateSignedTransaction(w.ctx, outputs, inputs, ignoreInputs, uint64(req.FeeRate), req.SendAll)
if err != nil {
return errCResponse("unable to sign send transaction: %v", err)
}
res := &CreateSignedTxRes{
SignedHex: hex.EncodeToString(txBytes),
Txid: txhash.String(),
Fee: int(fee),
}
b, err := json.Marshal(res)
if err != nil {
return errCResponse("unable to marshal sign send transaction result: %v", err)
}
return successCResponse("%s", b)
}
//export sendRawTransaction
func sendRawTransaction(cName, cTxHex *C.char) *C.char {
w, exists := loadedWallet(cName)
if !exists {
return errCResponse("wallet with name %q does not exist", goString(cName))
}
txHash, err := w.SendRawTransaction(w.ctx, goString(cTxHex))
if err != nil {
return errCResponse("unable to sign send transaction: %v", err)
}
return successCResponse("%s", txHash)
}
//export listUnspents
func listUnspents(cName *C.char) *C.char {
w, exists := loadedWallet(cName)
if !exists {
return errCResponse("wallet with name %q does not exist", goString(cName))
}
res, err := w.MainWallet().ListUnspent(w.ctx, 1, math.MaxInt32, nil, defaultAccount)
if err != nil {
return errCResponse("unable to get unspents: %v", err)
}
// Add is change to results.
unspentRes := make([]ListUnspentRes, len(res))
for i, unspent := range res {
addr, err := stdaddr.DecodeAddress(unspent.Address, w.MainWallet().ChainParams())
if err != nil {
return errCResponse("unable to decode address: %v", err)
}
ka, err := w.MainWallet().KnownAddress(w.ctx, addr)
if err != nil {
return errCResponse("unspent address is not known: %v", err)
}
isChange := false
if ka, ok := ka.(dcrwallet.BIP0044Address); ok {
_, branch, _ := ka.Path()
isChange = branch == 1
}
unspentRes[i] = ListUnspentRes{
ListUnspentResult: unspent,
IsChange: isChange,
}
}
b, err := json.Marshal(unspentRes)
if err != nil {
return errCResponse("unable to marshal list unspents result: %v", err)
}
return successCResponse("%s", b)
}
//export estimateFee
func estimateFee(cName, cNBlocks *C.char) *C.char {
w, exists := loadedWallet(cName)
if !exists {
return errCResponse("wallet with name %q does not exist", goString(cName))
}
nBlocks, err := strconv.ParseUint(goString(cNBlocks), 10, 64)
if err != nil {
return errCResponse("number of blocks is not a uint64: %v", err)
}
txFee, err := w.FetchFeeFromOracle(w.ctx, nBlocks)
if err != nil {
return errCResponse("unable to get fee from oracle: %v", err)
}
return successCResponse("%d", uint64(txFee*1e8))
}
//export listTransactions
func listTransactions(cName, cFrom, cCount *C.char) *C.char {
w, exists := loadedWallet(cName)
if !exists {
return errCResponse("wallet with name %q does not exist", goString(cName))
}
from, err := strconv.ParseInt(goString(cFrom), 10, 32)
if err != nil {
return errCResponse("from is not an int: %v", err)
}
count, err := strconv.ParseInt(goString(cCount), 10, 32)
if err != nil {
return errCResponse("count is not an int: %v", err)
}
res, err := w.MainWallet().ListTransactions(w.ctx, int(from), int(count))
if err != nil {
return errCResponse("unable to get transactions: %v", err)
}
_, blockHeight := w.MainWallet().MainChainTip(w.ctx)
ltRes := make([]*ListTransactionRes, len(res))
for i, ltw := range res {
// Use earliest of receive time or block time if the transaction is mined.
receiveTime := ltw.TimeReceived
if ltw.BlockTime != 0 && ltw.BlockTime < ltw.TimeReceived {
receiveTime = ltw.BlockTime
}
var height int64
if ltw.Confirmations > 0 {
height = int64(blockHeight) - ltw.Confirmations + 1
}
lt := &ListTransactionRes{
Address: ltw.Address,
Amount: ltw.Amount,
Category: ltw.Category,
Confirmations: ltw.Confirmations,
Height: height,
Fee: ltw.Fee,
Time: receiveTime,
TxID: ltw.TxID,
Vout: ltw.Vout,
}
ltRes[i] = lt
}
b, err := json.Marshal(ltRes)
if err != nil {
return errCResponse("unable to marshal list transactions result: %v", err)
}
return successCResponse("%s", b)
}
//export bestBlock
func bestBlock(cName *C.char) *C.char {
w, exists := loadedWallet(cName)
if !exists {
return errCResponse("wallet with name %q does not exist", goString(cName))
}
blockHash, blockHeight := w.MainWallet().MainChainTip(w.ctx)
res := &BestBlockRes{
Hash: blockHash.String(),
Height: int(blockHeight),
}
b, err := json.Marshal(res)
if err != nil {
return errCResponse("unable to marshal best block result: %v", err)
}
return successCResponse("%s", b)
}