-
Notifications
You must be signed in to change notification settings - Fork 2
/
admin_transaction.go
393 lines (337 loc) · 14.4 KB
/
admin_transaction.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package routes
import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/btcsuite/btcd/btcec"
"github.com/deso-smart/deso-core/v2/lib"
"github.com/pkg/errors"
)
type GetGlobalParamsRequest struct {
}
type GetGlobalParamsResponse struct {
// The current exchange rate.
USDCentsPerBitcoin uint64 `safeForLogging:"true"`
// The current create profile fee
CreateProfileFeeNanos uint64 `safeForLogging:"true"`
// The current minimum fee the network will accept
MinimumNetworkFeeNanosPerKB uint64 `safeForLogging:"true"`
// The fee per copy of an NFT minted.
CreateNFTFeeNanos uint64 `safeForLogging:"true"`
// The maximum number of copies a single NFT can have.
MaxCopiesPerNFT uint64 `safeForLogging:"true"`
}
func (fes *APIServer) GetGlobalParams(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := GetGlobalParamsRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("GetGlobalParams: Problem parsing request body: %v", err))
return
}
// Get a view
utxoView, err := fes.backendServer.GetMempool().GetAugmentedUniversalView()
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("GetGlobalParams: Error getting utxoView: %v", err))
return
}
globalParamsEntry := utxoView.GlobalParamsEntry
// Return all the data associated with the transaction in the response
res := GetGlobalParamsResponse{
USDCentsPerBitcoin: globalParamsEntry.USDCentsPerBitcoin,
CreateProfileFeeNanos: globalParamsEntry.CreateProfileFeeNanos,
MinimumNetworkFeeNanosPerKB: globalParamsEntry.MinimumNetworkFeeNanosPerKB,
CreateNFTFeeNanos: globalParamsEntry.CreateNFTFeeNanos,
MaxCopiesPerNFT: globalParamsEntry.MaxCopiesPerNFT,
}
if err := json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("GetGlobalParams: Problem encoding response as JSON: %v", err))
return
}
}
// UpdateGlobalParamsRequest ...
type UpdateGlobalParamsRequest struct {
UpdaterPublicKeyBase58Check string `safeForLogging:"true"`
// The new exchange rate to set.
USDCentsPerBitcoin int64 `safeForLogging:"true"`
// The fee to create a profile.
CreateProfileFeeNanos int64 `safeForLogging:"true"`
// The fee per copy of an NFT minted.
CreateNFTFeeNanos int64 `safeForLogging:"true"`
// The maximum number of copies a single NFT can have.
MaxCopiesPerNFT int64 `safeForLogging:"true"`
// The new minimum fee the network will accept
MinimumNetworkFeeNanosPerKB int64 `safeForLogging:"true"`
MinFeeRateNanosPerKB uint64 `safeForLogging:"true"`
// No need to specify ProfileEntryResponse in each TransactionFee
TransactionFees []TransactionFee `safeForLogging:"true"`
// Can be left unset when Signature is false or if the user legitimately
// doesn't have a password. Can also be left unset if the user has logged
// in recently as the password will be stored in memory.
Password string
// Whether or not we should sign the transaction after constructing it.
// Setting this flag to false is useful in
// cases where the caller just wants to construct the transaction
// to see what the fees will be, for example.
Sign bool `safeForLogging:"true"`
// Whether or not we should fully validate the transaction.
Validate bool `safeForLogging:"true"`
// Whether or not we should broadcast the transaction after constructing
// it. This will also validate the transaction if it's set.
Broadcast bool `safeForLogging:"true"`
}
// UpdateGlobalParamsResponse ...
type UpdateGlobalParamsResponse struct {
TotalInputNanos uint64
ChangeAmountNanos uint64
FeeNanos uint64
Transaction *lib.MsgDeSoTxn
TransactionHex string
}
func (fes *APIServer) UpdateGlobalParams(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := UpdateGlobalParamsRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("UpdateGlobalParams: Problem parsing request body: %v", err))
return
}
// Decode the updater public key.
updaterPkBytes, _, err := lib.Base58CheckDecode(requestData.UpdaterPublicKeyBase58Check)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("UpdateGlobalParams: Problem decoding updater "+
"base58 public key %s: %v", requestData.UpdaterPublicKeyBase58Check, err))
return
}
// Compute the additional transaction fees as specified by the request body and the node-level fees.
additionalOutputs, err := fes.getTransactionFee(lib.TxnTypeUpdateGlobalParams, updaterPkBytes, requestData.TransactionFees)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("UpdateGlobalParams: TransactionFees specified in Request body are invalid: %v", err))
return
}
// Get a utxoView.
utxoView, err := fes.backendServer.GetMempool().GetAugmentedUniversalView()
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("UpdateGlobalParams: Error constucting utxoView: %v", err))
return
}
// Only update values if they have changed. Values less than 0 are excluded from the transaction
usdCentsPerBitcoin := int64(-1)
if requestData.USDCentsPerBitcoin >= 0 && uint64(requestData.USDCentsPerBitcoin) != utxoView.GlobalParamsEntry.USDCentsPerBitcoin {
usdCentsPerBitcoin = requestData.USDCentsPerBitcoin
}
createProfileFeeNanos := int64(-1)
if requestData.CreateProfileFeeNanos >= 0 && uint64(requestData.CreateProfileFeeNanos) != utxoView.GlobalParamsEntry.CreateProfileFeeNanos {
createProfileFeeNanos = requestData.CreateProfileFeeNanos
}
createNFTFeeNanos := int64(-1)
if requestData.CreateNFTFeeNanos >= 0 && uint64(requestData.CreateNFTFeeNanos) != utxoView.GlobalParamsEntry.CreateNFTFeeNanos {
createNFTFeeNanos = requestData.CreateNFTFeeNanos
}
minimumNetworkFeeNanosPerKb := int64(-1)
if requestData.MinimumNetworkFeeNanosPerKB >= 0 && uint64(requestData.MinimumNetworkFeeNanosPerKB) != utxoView.GlobalParamsEntry.MinimumNetworkFeeNanosPerKB {
minimumNetworkFeeNanosPerKb = requestData.MinimumNetworkFeeNanosPerKB
}
maxCopiesPerNFT := int64(-1)
if requestData.MaxCopiesPerNFT >= 0 && uint64(requestData.MaxCopiesPerNFT) != utxoView.GlobalParamsEntry.MaxCopiesPerNFT {
maxCopiesPerNFT = requestData.MaxCopiesPerNFT
}
// Try and create the update txn for the user.
txn, totalInput, changeAmount, fees, err := fes.blockchain.CreateUpdateGlobalParamsTxn(
updaterPkBytes,
usdCentsPerBitcoin,
createProfileFeeNanos,
createNFTFeeNanos,
maxCopiesPerNFT,
minimumNetworkFeeNanosPerKb,
[]byte{},
requestData.MinFeeRateNanosPerKB,
fes.backendServer.GetMempool(), additionalOutputs)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("UpdateGlobalParams: Problem creating transaction: %v", err))
return
}
txnBytes, err := txn.ToBytes(true)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("UpdateGlobalParams: Problem serializing transaction: %v", err))
return
}
// Return all the data associated with the transaction in the response
res := UpdateGlobalParamsResponse{
TotalInputNanos: totalInput,
ChangeAmountNanos: changeAmount,
FeeNanos: fees,
Transaction: txn,
TransactionHex: hex.EncodeToString(txnBytes),
}
if err := json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("UpdateGlobalParams: Problem encoding response as JSON: %v", err))
return
}
}
// SwapIdentityRequest ...
type SwapIdentityRequest struct {
// This is currently paramUpdater only
UpdaterPublicKeyBase58Check string `safeForLogging:"true"`
// Either a username or a public key works. If it starts with BC and
// is over the username limit it will be interpreted as a username.
FromUsernameOrPublicKeyBase58Check string `safeForLogging:"true"`
// Either a username or a public key works. If it starts with BC and
// // is over the username limit it will be interpreted as a username.
ToUsernameOrPublicKeyBase58Check string `safeForLogging:"true"`
MinFeeRateNanosPerKB uint64 `safeForLogging:"true"`
// No need to specify ProfileEntryResponse in each TransactionFee
TransactionFees []TransactionFee `safeForLogging:"true"`
}
// SwapIdentityResponse ...
type SwapIdentityResponse struct {
TotalInputNanos uint64
ChangeAmountNanos uint64
FeeNanos uint64
Transaction *lib.MsgDeSoTxn
TransactionHex string
}
func (fes *APIServer) getPublicKeyFromUsernameOrPublicKeyString(usernameOrPublicKey string) ([]byte, error) {
if (strings.HasPrefix(usernameOrPublicKey, fes.PublicKeyBase58Prefix)) &&
len(usernameOrPublicKey) >= btcec.PubKeyBytesLenCompressed {
// In this case parse the string as a public key.
var err error
fromPublicKey, _, err := lib.Base58CheckDecode(usernameOrPublicKey)
if err != nil {
return nil, errors.Wrap(err, "getPublicKeyFromUsernameOrPublicKeyString: ")
}
return fromPublicKey, nil
}
// Otherwise, parse the string as a username
utxoView, err := fes.backendServer.GetMempool().GetAugmentedUniversalView()
if err != nil {
return nil, errors.Wrap(fmt.Errorf("getPublicKeyFromUsernameOrPublicKeyString: Error generating "+
"view to verify username: %v", err), "")
}
profileEntry := utxoView.GetProfileEntryForUsername([]byte(usernameOrPublicKey))
if profileEntry == nil {
return nil, errors.Wrap(
fmt.Errorf("getPublicKeyFromUsernameOrPublicKeyString: Profile with username %v does not exist",
usernameOrPublicKey), "")
}
return profileEntry.PublicKey, nil
}
// SwapIdentity ...
func (fes *APIServer) SwapIdentity(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := SwapIdentityRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SwapIdentity: Problem parsing request body: %v", err))
return
}
// Decode the updater public key.
updaterPkBytes, _, err := lib.Base58CheckDecode(requestData.UpdaterPublicKeyBase58Check)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SwapIdentity: Problem decoding updater "+
"base58 public key %s: %v", requestData.UpdaterPublicKeyBase58Check, err))
return
}
// Compute the additional transaction fees as specified by the request body and the node-level fees.
additionalOutputs, err := fes.getTransactionFee(lib.TxnTypeSwapIdentity, updaterPkBytes, requestData.TransactionFees)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SwapIdentity: TransactionFees specified in Request body are invalid: %v", err))
return
}
fromPublicKey, err := fes.getPublicKeyFromUsernameOrPublicKeyString(
requestData.FromUsernameOrPublicKeyBase58Check)
if err != nil {
_AddBadRequestError(ww, err.Error())
return
}
toPublicKey, err := fes.getPublicKeyFromUsernameOrPublicKeyString(
requestData.ToUsernameOrPublicKeyBase58Check)
if err != nil {
_AddBadRequestError(ww, err.Error())
return
}
// Try and create the update txn for the user.
txn, totalInput, changeAmount, fees, err := fes.blockchain.CreateSwapIdentityTxn(
updaterPkBytes,
fromPublicKey,
toPublicKey,
requestData.MinFeeRateNanosPerKB,
fes.backendServer.GetMempool(), additionalOutputs)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SwapIdentity: Problem creating transaction: %v", err))
return
}
txnBytes, err := txn.ToBytes(true)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SwapIdentity: Problem serializing transaction: %v", err))
return
}
// Return all the data associated with the transaction in the response
res := SwapIdentityResponse{
TotalInputNanos: totalInput,
ChangeAmountNanos: changeAmount,
FeeNanos: fees,
Transaction: txn,
TransactionHex: hex.EncodeToString(txnBytes),
}
if err = json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SwapIdentity: Problem encoding response as JSON: %v", err))
return
}
}
// TestSignTransactionWithDerivedKeyRequest ...
type TestSignTransactionWithDerivedKeyRequest struct {
// Transaction hex.
TransactionHex string `safeForLogging:"true"`
// Derived private key in base58Check.
DerivedKeySeedHex string `safeForLogging:"false"`
}
// TestSignTransactionWithDerivedKeyResponse ...
type TestSignTransactionWithDerivedKeyResponse struct {
// Signed Transaction hex.
TransactionHex string `safeForLogging:"true"`
}
// TestSignTransactionWithDerivedKey ...
// This endpoint must not be used by a frontend in a production environment,
// instead it is meant to serve as a debugging tool as well as an example of
// how to properly sign transactions with a derived key.
func (fes *APIServer) TestSignTransactionWithDerivedKey(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := TestSignTransactionWithDerivedKeyRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("TestSignTransactionWithDerivedKey: Problem parsing request body: %v", err))
return
}
// Get the transaction bytes from the request data.
txnBytes, err := hex.DecodeString(requestData.TransactionHex)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("TestSignTransactionWithDerivedKey: Problem decoding transaction hex %v", err))
return
}
// Get the derived private key from the request data.
privBytes, err := hex.DecodeString(requestData.DerivedKeySeedHex)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("TestSignTransactionWithDerivedKey: Problem decoding seed hex %v", err))
return
}
privKeyBytes, _ := btcec.PrivKeyFromBytes(btcec.S256(), privBytes)
// Sign the transaction with a derived key. Since the txn extraData must be modified,
// we also get new transaction bytes, along with the signature.
newTxnBytes, txnSignatureBytes, err := lib.SignTransactionWithDerivedKey(txnBytes, privKeyBytes)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("TestSignTransactionWithDerivedKey: Problem signing transaction: %v", err))
return
}
// The response will contain the new transaction bytes and a signature.
var signedTransactionHex []byte
signedTransactionHex = newTxnBytes[0 : len(newTxnBytes)-1]
signedTransactionHex = append(signedTransactionHex, lib.UintToBuf(uint64(len(txnSignatureBytes)))...)
signedTransactionHex = append(signedTransactionHex, txnSignatureBytes...)
res := TestSignTransactionWithDerivedKeyResponse{
TransactionHex: hex.EncodeToString(signedTransactionHex),
}
if err := json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("TestSignTransactionWithDerivedKey: Problem encoding response as JSON: %v", err))
return
}
}