forked from bitcoin-sv/spv-wallet-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
1170 lines (1023 loc) · 33.6 KB
/
http.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package walletclient
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"strconv"
bip32 "github.com/bitcoin-sv/go-sdk/compat/bip32"
ec "github.com/bitcoin-sv/go-sdk/primitives/ec"
"github.com/bitcoin-sv/spv-wallet-go-client/utils"
"github.com/bitcoin-sv/spv-wallet/models"
"github.com/bitcoin-sv/spv-wallet/models/filter"
)
// SetSignRequest turn the signing of the http request on or off
func (wc *WalletClient) SetSignRequest(signRequest bool) {
wc.signRequest = signRequest
}
// IsSignRequest return whether to sign all requests
func (wc *WalletClient) IsSignRequest() bool {
return wc.signRequest
}
// SetAdminKey set the admin key
func (wc *WalletClient) SetAdminKey(adminKey *bip32.ExtendedKey) {
wc.adminXPriv = adminKey
}
// GetXPub will get the xpub of the current xpub
func (wc *WalletClient) GetXPub(ctx context.Context) (*models.Xpub, error) {
var xPub models.Xpub
if err := wc.doHTTPRequest(
ctx, http.MethodGet, "/xpub", nil, wc.xPriv, true, &xPub,
); err != nil {
return nil, err
}
return &xPub, nil
}
// UpdateXPubMetadata update the metadata of the logged in xpub
func (wc *WalletClient) UpdateXPubMetadata(ctx context.Context, metadata map[string]any) (*models.Xpub, error) {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldMetadata: metadata,
})
if err != nil {
return nil, WrapError(err)
}
var xPub models.Xpub
if err := wc.doHTTPRequest(
ctx, http.MethodPatch, "/xpub", jsonStr, wc.xPriv, true, &xPub,
); err != nil {
return nil, err
}
return &xPub, nil
}
// GetAccessKey will get an access key by id
func (wc *WalletClient) GetAccessKey(ctx context.Context, id string) (*models.AccessKey, error) {
var accessKey models.AccessKey
if err := wc.doHTTPRequest(
ctx, http.MethodGet, "/access-key?"+FieldID+"="+id, nil, wc.xPriv, true, &accessKey,
); err != nil {
return nil, err
}
return &accessKey, nil
}
// GetAccessKeys will get all access keys matching the metadata filter
func (wc *WalletClient) GetAccessKeys(
ctx context.Context,
conditions *filter.AccessKeyFilter,
metadata map[string]any,
queryParams *filter.QueryParams,
) ([]*models.AccessKey, error) {
return Search[filter.AccessKeyFilter, []*models.AccessKey](
ctx, http.MethodPost,
"/access-key/search",
wc.xPriv,
conditions,
metadata,
queryParams,
wc.doHTTPRequest,
)
}
// GetAccessKeysCount will get the count of access keys
func (wc *WalletClient) GetAccessKeysCount(
ctx context.Context,
conditions *filter.AccessKeyFilter,
metadata map[string]any,
) (int64, error) {
return Count[filter.AccessKeyFilter](
ctx, http.MethodPost,
"/access-key/count",
wc.xPriv,
conditions,
metadata,
wc.doHTTPRequest,
)
}
// RevokeAccessKey will revoke an access key by id
func (wc *WalletClient) RevokeAccessKey(ctx context.Context, id string) (*models.AccessKey, error) {
var accessKey models.AccessKey
if err := wc.doHTTPRequest(
ctx, http.MethodDelete, "/access-key?"+FieldID+"="+id, nil, wc.xPriv, true, &accessKey,
); err != nil {
return nil, err
}
return &accessKey, nil
}
// CreateAccessKey will create new access key
func (wc *WalletClient) CreateAccessKey(ctx context.Context, metadata map[string]any) (*models.AccessKey, error) {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldMetadata: metadata,
})
if err != nil {
return nil, WrapError(err)
}
var accessKey models.AccessKey
if err := wc.doHTTPRequest(
ctx, http.MethodPost, "/access-key", jsonStr, wc.xPriv, true, &accessKey,
); err != nil {
return nil, err
}
return &accessKey, nil
}
// GetDestinationByID will get a destination by id
func (wc *WalletClient) GetDestinationByID(ctx context.Context, id string) (*models.Destination, error) {
var destination models.Destination
if err := wc.doHTTPRequest(
ctx, http.MethodGet, fmt.Sprintf("/destination?%s=%s", FieldID, id), nil, wc.xPriv, true, &destination,
); err != nil {
return nil, err
}
return &destination, nil
}
// GetDestinationByAddress will get a destination by address
func (wc *WalletClient) GetDestinationByAddress(ctx context.Context, address string) (*models.Destination, error) {
var destination models.Destination
if err := wc.doHTTPRequest(
ctx, http.MethodGet, "/destination?"+FieldAddress+"="+address, nil, wc.xPriv, true, &destination,
); err != nil {
return nil, err
}
return &destination, nil
}
// GetDestinationByLockingScript will get a destination by locking script
func (wc *WalletClient) GetDestinationByLockingScript(ctx context.Context, lockingScript string) (*models.Destination, error) {
var destination models.Destination
if err := wc.doHTTPRequest(
ctx, http.MethodGet, "/destination?"+FieldLockingScript+"="+lockingScript, nil, wc.xPriv, true, &destination,
); err != nil {
return nil, err
}
return &destination, nil
}
// GetDestinations will get all destinations matching the metadata filter
func (wc *WalletClient) GetDestinations(ctx context.Context, conditions *filter.DestinationFilter, metadata map[string]any, queryParams *filter.QueryParams) ([]*models.Destination, error) {
return Search[filter.DestinationFilter, []*models.Destination](
ctx, http.MethodPost,
"/destination/search",
wc.xPriv,
conditions,
metadata,
queryParams,
wc.doHTTPRequest,
)
}
// GetDestinationsCount will get the count of destinations matching the metadata filter
func (wc *WalletClient) GetDestinationsCount(ctx context.Context, conditions *filter.DestinationFilter, metadata map[string]any) (int64, error) {
return Count(
ctx,
http.MethodPost,
"/destination/count",
wc.xPriv,
conditions,
metadata,
wc.doHTTPRequest,
)
}
// NewDestination will create a new destination and return it
func (wc *WalletClient) NewDestination(ctx context.Context, metadata map[string]any) (*models.Destination, error) {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldMetadata: metadata,
})
if err != nil {
return nil, WrapError(err)
}
var destination models.Destination
if err := wc.doHTTPRequest(
ctx, http.MethodPost, "/destination", jsonStr, wc.xPriv, true, &destination,
); err != nil {
return nil, err
}
return &destination, nil
}
// UpdateDestinationMetadataByID updates the destination metadata by id
func (wc *WalletClient) UpdateDestinationMetadataByID(ctx context.Context, id string, metadata map[string]any) (*models.Destination, error) {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldID: id,
FieldMetadata: metadata,
})
if err != nil {
return nil, WrapError(err)
}
var destination models.Destination
if err := wc.doHTTPRequest(
ctx, http.MethodPatch, "/destination", jsonStr, wc.xPriv, true, &destination,
); err != nil {
return nil, err
}
return &destination, nil
}
// UpdateDestinationMetadataByAddress updates the destination metadata by address
func (wc *WalletClient) UpdateDestinationMetadataByAddress(ctx context.Context, address string, metadata map[string]any) (*models.Destination, error) {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldAddress: address,
FieldMetadata: metadata,
})
if err != nil {
return nil, WrapError(err)
}
var destination models.Destination
if err := wc.doHTTPRequest(
ctx, http.MethodPatch, "/destination", jsonStr, wc.xPriv, true, &destination,
); err != nil {
return nil, err
}
return &destination, nil
}
// UpdateDestinationMetadataByLockingScript updates the destination metadata by locking script
func (wc *WalletClient) UpdateDestinationMetadataByLockingScript(ctx context.Context, lockingScript string, metadata map[string]any) (*models.Destination, error) {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldLockingScript: lockingScript,
FieldMetadata: metadata,
})
if err != nil {
return nil, WrapError(err)
}
var destination models.Destination
if err := wc.doHTTPRequest(
ctx, http.MethodPatch, "/destination", jsonStr, wc.xPriv, true, &destination,
); err != nil {
return nil, err
}
return &destination, nil
}
// GetTransaction will get a transaction by ID
func (wc *WalletClient) GetTransaction(ctx context.Context, txID string) (*models.Transaction, error) {
var transaction models.Transaction
if err := wc.doHTTPRequest(ctx, http.MethodGet, "/transaction?"+FieldID+"="+txID, nil, wc.xPriv, wc.signRequest, &transaction); err != nil {
return nil, err
}
return &transaction, nil
}
// GetTransactions will get transactions by conditions
func (wc *WalletClient) GetTransactions(
ctx context.Context,
conditions *filter.TransactionFilter,
metadata map[string]any,
queryParams *filter.QueryParams,
) ([]*models.Transaction, error) {
return Search[filter.TransactionFilter, []*models.Transaction](
ctx, http.MethodPost,
"/transaction/search",
wc.xPriv,
conditions,
metadata,
queryParams,
wc.doHTTPRequest,
)
}
// GetTransactionsCount get number of user transactions
func (wc *WalletClient) GetTransactionsCount(
ctx context.Context,
conditions *filter.TransactionFilter,
metadata map[string]any,
) (int64, error) {
return Count[filter.TransactionFilter](
ctx, http.MethodPost,
"/transaction/count",
wc.xPriv,
conditions,
metadata,
wc.doHTTPRequest,
)
}
// DraftToRecipients is a draft transaction to a slice of recipients
func (wc *WalletClient) DraftToRecipients(ctx context.Context, recipients []*Recipients, metadata map[string]any) (*models.DraftTransaction, error) {
outputs := make([]map[string]interface{}, 0)
for _, recipient := range recipients {
outputs = append(outputs, map[string]interface{}{
FieldTo: recipient.To,
FieldSatoshis: recipient.Satoshis,
FieldOpReturn: recipient.OpReturn,
})
}
return wc.createDraftTransaction(ctx, map[string]interface{}{
FieldConfig: map[string]interface{}{
FieldOutputs: outputs,
},
FieldMetadata: metadata,
})
}
// DraftTransaction is a draft transaction
func (wc *WalletClient) DraftTransaction(ctx context.Context, transactionConfig *models.TransactionConfig, metadata map[string]any) (*models.DraftTransaction, error) {
return wc.createDraftTransaction(ctx, map[string]interface{}{
FieldConfig: transactionConfig,
FieldMetadata: metadata,
})
}
// createDraftTransaction will create a draft transaction
func (wc *WalletClient) createDraftTransaction(ctx context.Context,
jsonData map[string]interface{},
) (*models.DraftTransaction, error) {
jsonStr, err := json.Marshal(jsonData)
if err != nil {
return nil, WrapError(err)
}
var draftTransaction *models.DraftTransaction
if err := wc.doHTTPRequest(
ctx, http.MethodPost, "/transaction", jsonStr, wc.xPriv, true, &draftTransaction,
); err != nil {
return nil, err
}
if draftTransaction == nil {
return nil, ErrCouldNotFindDraftTransaction
}
return draftTransaction, nil
}
// RecordTransaction will record a transaction
func (wc *WalletClient) RecordTransaction(ctx context.Context, hex, referenceID string, metadata map[string]any) (*models.Transaction, error) {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldHex: hex,
FieldReferenceID: referenceID,
FieldMetadata: metadata,
})
if err != nil {
return nil, WrapError(err)
}
var transaction models.Transaction
if err := wc.doHTTPRequest(
ctx, http.MethodPost, "/transaction/record", jsonStr, wc.xPriv, wc.signRequest, &transaction,
); err != nil {
return nil, err
}
return &transaction, nil
}
// UpdateTransactionMetadata update the metadata of a transaction
func (wc *WalletClient) UpdateTransactionMetadata(ctx context.Context, txID string, metadata map[string]any) (*models.Transaction, error) {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldID: txID,
FieldMetadata: metadata,
})
if err != nil {
return nil, WrapError(err)
}
var transaction models.Transaction
if err := wc.doHTTPRequest(
ctx, http.MethodPatch, "/transaction", jsonStr, wc.xPriv, wc.signRequest, &transaction,
); err != nil {
return nil, err
}
return &transaction, nil
}
// SetSignatureFromAccessKey will set the signature on the header for the request from an access key
func SetSignatureFromAccessKey(header *http.Header, privateKeyHex, bodyString string) error {
// Create the signature
authData, err := createSignatureAccessKey(privateKeyHex, bodyString)
if err != nil {
return WrapError(err)
}
// Set the auth header
header.Set(models.AuthAccessKey, authData.AccessKey)
setSignatureHeaders(header, authData)
return nil
}
// GetUtxo will get a utxo by transaction ID
func (wc *WalletClient) GetUtxo(ctx context.Context, txID string, outputIndex uint32) (*models.Utxo, error) {
outputIndexStr := strconv.FormatUint(uint64(outputIndex), 10)
url := fmt.Sprintf("/utxo?%s=%s&%s=%s", FieldTransactionID, txID, FieldOutputIndex, outputIndexStr)
var utxo models.Utxo
if err := wc.doHTTPRequest(
ctx, http.MethodGet, url, nil, wc.xPriv, true, &utxo,
); err != nil {
return nil, err
}
return &utxo, nil
}
// GetUtxos will get a list of utxos filtered by conditions and metadata
func (wc *WalletClient) GetUtxos(ctx context.Context, conditions *filter.UtxoFilter, metadata map[string]any, queryParams *filter.QueryParams) ([]*models.Utxo, error) {
return Search[filter.UtxoFilter, []*models.Utxo](
ctx, http.MethodPost,
"/utxo/search",
wc.xPriv,
conditions,
metadata,
queryParams,
wc.doHTTPRequest,
)
}
// GetUtxosCount will get the count of utxos filtered by conditions and metadata
func (wc *WalletClient) GetUtxosCount(ctx context.Context, conditions *filter.UtxoFilter, metadata map[string]any) (int64, error) {
return Count[filter.UtxoFilter](
ctx, http.MethodPost,
"/utxo/count",
wc.xPriv,
conditions,
metadata,
wc.doHTTPRequest,
)
}
// createSignatureAccessKey will create a signature for the given access key & body contents
func createSignatureAccessKey(privateKeyHex, bodyString string) (payload *models.AuthPayload, err error) {
// No key?
if privateKeyHex == "" {
err = CreateErrorResponse("error-unauthorized-missing-access-key", "missing access key")
return
}
var privateKey *ec.PrivateKey
if privateKey, err = ec.PrivateKeyFromHex(
privateKeyHex,
); err != nil {
return
}
publicKey := privateKey.PubKey()
// Get the AccessKey
payload = new(models.AuthPayload)
payload.AccessKey = hex.EncodeToString(publicKey.SerializeCompressed())
// auth_nonce is a random unique string to seed the signing message
// this can be checked server side to make sure the request is not being replayed
payload.AuthNonce, err = utils.RandomHex(32)
if err != nil {
return nil, err
}
return createSignatureCommon(payload, bodyString, privateKey)
}
// doHTTPRequest will create and submit the HTTP request
func (wc *WalletClient) doHTTPRequest(ctx context.Context, method string, path string,
rawJSON []byte, xPriv *bip32.ExtendedKey, sign bool, responseJSON interface{},
) error {
req, err := http.NewRequestWithContext(ctx, method, wc.server+path, bytes.NewBuffer(rawJSON))
if err != nil {
return WrapError(err)
}
req.Header.Set("Content-Type", "application/json")
if xPriv != nil {
err := wc.authenticateWithXpriv(sign, req, xPriv, rawJSON)
if err != nil {
return err
}
} else {
err := wc.authenticateWithAccessKey(req, rawJSON)
if err != nil {
return err
}
}
var resp *http.Response
defer func() {
if resp != nil && resp.Body != nil {
_ = resp.Body.Close()
}
}()
if resp, err = wc.httpClient.Do(req); err != nil {
return WrapError(err)
}
if resp.StatusCode >= http.StatusBadRequest {
return WrapResponseError(resp)
}
if responseJSON == nil {
return nil
}
err = json.NewDecoder(resp.Body).Decode(&responseJSON)
if err != nil {
return WrapError(err)
}
return nil
}
func (wc *WalletClient) authenticateWithXpriv(sign bool, req *http.Request, xPriv *bip32.ExtendedKey, rawJSON []byte) error {
if sign {
if err := addSignature(&req.Header, xPriv, string(rawJSON)); err != nil {
return err
}
} else {
var xPub string
xPub, err := bip32.GetExtendedPublicKey(xPriv)
if err != nil {
return WrapError(err)
}
req.Header.Set(models.AuthHeader, xPub)
req.Header.Set("", xPub)
}
return nil
}
func (wc *WalletClient) authenticateWithAccessKey(req *http.Request, rawJSON []byte) error {
if wc.accessKey == nil {
return ErrMissingAccessKey
}
return SetSignatureFromAccessKey(&req.Header, hex.EncodeToString(wc.accessKey.Serialize()), string(rawJSON))
}
// AcceptContact will accept the contact associated with the paymail
func (wc *WalletClient) AcceptContact(ctx context.Context, paymail string) error {
if err := wc.doHTTPRequest(
ctx, http.MethodPatch, "/contact/accepted/"+paymail, nil, wc.xPriv, wc.signRequest, nil,
); err != nil {
return err
}
return nil
}
// RejectContact will reject the contact associated with the paymail
func (wc *WalletClient) RejectContact(ctx context.Context, paymail string) error {
if err := wc.doHTTPRequest(
ctx, http.MethodPatch, "/contact/rejected/"+paymail, nil, wc.xPriv, wc.signRequest, nil,
); err != nil {
return err
}
return nil
}
// ConfirmContact will confirm the contact associated with the paymail
func (wc *WalletClient) ConfirmContact(ctx context.Context, contact *models.Contact, passcode, requesterPaymail string, period, digits uint) error {
isTotpValid, err := wc.ValidateTotpForContact(contact, passcode, requesterPaymail, period, digits)
if err != nil {
return WrapError(ErrTotpInvalid)
}
if !isTotpValid {
return WrapError(ErrTotpInvalid)
}
if err := wc.doHTTPRequest(
ctx, http.MethodPatch, "/contact/confirmed/"+contact.Paymail, nil, wc.xPriv, wc.signRequest, nil,
); err != nil {
return err
}
return nil
}
// GetContacts will get contacts by conditions
func (wc *WalletClient) GetContacts(ctx context.Context, conditions *filter.ContactFilter, metadata map[string]any, queryParams *filter.QueryParams) (*models.SearchContactsResponse, error) {
return Search[filter.ContactFilter, *models.SearchContactsResponse](
ctx, http.MethodPost,
"/contact/search",
wc.xPriv,
conditions,
metadata,
queryParams,
wc.doHTTPRequest,
)
}
// UpsertContact add or update contact. When adding a new contact, the system utilizes Paymail's PIKE capability to dispatch an invitation request, asking the counterparty to include the current user in their contacts.
func (wc *WalletClient) UpsertContact(ctx context.Context, paymail, fullName, requesterPaymail string, metadata map[string]any) (*models.Contact, error) {
return wc.UpsertContactForPaymail(ctx, paymail, fullName, metadata, requesterPaymail)
}
// UpsertContactForPaymail add or update contact. When adding a new contact, the system utilizes Paymail's PIKE capability to dispatch an invitation request, asking the counterparty to include the current user in their contacts.
func (wc *WalletClient) UpsertContactForPaymail(ctx context.Context, paymail, fullName string, metadata map[string]any, requesterPaymail string) (*models.Contact, error) {
payload := map[string]interface{}{
"fullName": fullName,
FieldMetadata: metadata,
}
if requesterPaymail != "" {
payload["requesterPaymail"] = requesterPaymail
}
jsonStr, err := json.Marshal(payload)
if err != nil {
return nil, WrapError(err)
}
var result models.Contact
if err := wc.doHTTPRequest(
ctx, http.MethodPut, "/contact/"+paymail, jsonStr, wc.xPriv, wc.signRequest, &result,
); err != nil {
return nil, err
}
return &result, nil
}
// GetSharedConfig gets the shared config
func (wc *WalletClient) GetSharedConfig(ctx context.Context) (*models.SharedConfig, error) {
var model *models.SharedConfig
key := wc.xPriv
if wc.adminXPriv != nil {
key = wc.adminXPriv
}
if key == nil {
return nil, WrapError(ErrMissingKey)
}
if err := wc.doHTTPRequest(
ctx, http.MethodGet, "/shared-config", nil, key, true, &model,
); err != nil {
return nil, err
}
return model, nil
}
// AdminNewXpub will register an xPub
func (wc *WalletClient) AdminNewXpub(ctx context.Context, rawXPub string, metadata map[string]any) error {
// Adding a xpub needs to be signed by an admin key
if wc.adminXPriv == nil {
return WrapError(ErrAdminKey)
}
jsonStr, err := json.Marshal(map[string]interface{}{
FieldMetadata: metadata,
FieldXpubKey: rawXPub,
})
if err != nil {
return WrapError(err)
}
var xPubData models.Xpub
return wc.doHTTPRequest(
ctx, http.MethodPost, "/admin/xpub", jsonStr, wc.adminXPriv, true, &xPubData,
)
}
// AdminGetStatus get whether admin key is valid
func (wc *WalletClient) AdminGetStatus(ctx context.Context) (bool, error) {
var status bool
if err := wc.doHTTPRequest(
ctx, http.MethodGet, "/admin/status", nil, wc.adminXPriv, true, &status,
); err != nil {
return false, err
}
return status, nil
}
// AdminGetStats get admin stats
func (wc *WalletClient) AdminGetStats(ctx context.Context) (*models.AdminStats, error) {
var stats *models.AdminStats
if err := wc.doHTTPRequest(
ctx, http.MethodGet, "/admin/stats", nil, wc.adminXPriv, true, &stats,
); err != nil {
return nil, err
}
return stats, nil
}
// AdminGetAccessKeys get all access keys filtered by conditions
func (wc *WalletClient) AdminGetAccessKeys(
ctx context.Context,
conditions *filter.AdminAccessKeyFilter,
metadata map[string]any,
queryParams *filter.QueryParams,
) ([]*models.AccessKey, error) {
return Search[filter.AdminAccessKeyFilter, []*models.AccessKey](
ctx, http.MethodPost,
"/admin/access-keys/search",
wc.adminXPriv,
conditions,
metadata,
queryParams,
wc.doHTTPRequest,
)
}
// AdminGetAccessKeysCount get a count of all the access keys filtered by conditions
func (wc *WalletClient) AdminGetAccessKeysCount(
ctx context.Context,
conditions *filter.AdminAccessKeyFilter,
metadata map[string]any,
) (int64, error) {
return Count[filter.AdminAccessKeyFilter](
ctx, http.MethodPost,
"/admin/access-keys/count",
wc.adminXPriv,
conditions,
metadata,
wc.doHTTPRequest,
)
}
// AdminGetBlockHeaders get all block headers filtered by conditions
func (wc *WalletClient) AdminGetBlockHeaders(
ctx context.Context,
conditions map[string]interface{},
metadata map[string]any,
queryParams *filter.QueryParams,
) ([]*models.BlockHeader, error) {
var models []*models.BlockHeader
if err := wc.adminGetModels(ctx, conditions, metadata, queryParams, "/admin/block-headers/search", &models); err != nil {
return nil, err
}
return models, nil
}
// AdminGetBlockHeadersCount get a count of all the block headers filtered by conditions
func (wc *WalletClient) AdminGetBlockHeadersCount(
ctx context.Context,
conditions map[string]interface{},
metadata map[string]any,
) (int64, error) {
return wc.adminCount(ctx, conditions, metadata, "/admin/block-headers/count")
}
// AdminGetDestinations get all block destinations filtered by conditions
func (wc *WalletClient) AdminGetDestinations(ctx context.Context, conditions *filter.DestinationFilter,
metadata map[string]any, queryParams *filter.QueryParams,
) ([]*models.Destination, error) {
return Search[filter.DestinationFilter, []*models.Destination](
ctx, http.MethodPost,
"/admin/destinations/search",
wc.adminXPriv,
conditions,
metadata,
queryParams,
wc.doHTTPRequest,
)
}
// AdminGetDestinationsCount get a count of all the destinations filtered by conditions
func (wc *WalletClient) AdminGetDestinationsCount(ctx context.Context, conditions *filter.DestinationFilter, metadata map[string]any) (int64, error) {
return Count(
ctx,
http.MethodPost,
"/admin/destinations/count",
wc.adminXPriv,
conditions,
metadata,
wc.doHTTPRequest,
)
}
// AdminGetPaymail get a paymail by address
func (wc *WalletClient) AdminGetPaymail(ctx context.Context, address string) (*models.PaymailAddress, error) {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldAddress: address,
})
if err != nil {
return nil, WrapError(err)
}
var model *models.PaymailAddress
if err := wc.doHTTPRequest(
ctx, http.MethodPost, "/admin/paymail/get", jsonStr, wc.adminXPriv, true, &model,
); err != nil {
return nil, err
}
return model, nil
}
// AdminGetPaymails get all block paymails filtered by conditions
func (wc *WalletClient) AdminGetPaymails(
ctx context.Context,
conditions *filter.AdminPaymailFilter,
metadata map[string]any,
queryParams *filter.QueryParams,
) ([]*models.PaymailAddress, error) {
return Search[filter.AdminPaymailFilter, []*models.PaymailAddress](
ctx, http.MethodPost,
"/admin/paymails/search",
wc.adminXPriv,
conditions,
metadata,
queryParams,
wc.doHTTPRequest,
)
}
// AdminGetPaymailsCount get a count of all the paymails filtered by conditions
func (wc *WalletClient) AdminGetPaymailsCount(ctx context.Context, conditions *filter.AdminPaymailFilter, metadata map[string]any) (int64, error) {
return Count(
ctx, http.MethodPost,
"/admin/paymails/count",
wc.adminXPriv,
conditions,
metadata,
wc.doHTTPRequest,
)
}
// AdminCreatePaymail create a new paymail for a xpub
func (wc *WalletClient) AdminCreatePaymail(ctx context.Context, rawXPub string, address string, publicName string, avatar string) (*models.PaymailAddress, error) {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldXpubKey: rawXPub,
FieldAddress: address,
FieldPublicName: publicName,
FieldAvatar: avatar,
})
if err != nil {
return nil, WrapError(err)
}
var model *models.PaymailAddress
if err := wc.doHTTPRequest(
ctx, http.MethodPost, "/admin/paymail/create", jsonStr, wc.adminXPriv, true, &model,
); err != nil {
return nil, err
}
return model, nil
}
// AdminDeletePaymail delete a paymail address from the database
func (wc *WalletClient) AdminDeletePaymail(ctx context.Context, address string) error {
jsonStr, err := json.Marshal(map[string]interface{}{
FieldAddress: address,
})
if err != nil {
return WrapError(err)
}
if err := wc.doHTTPRequest(
ctx, http.MethodDelete, "/admin/paymail/delete", jsonStr, wc.adminXPriv, true, nil,
); err != nil {
return err
}
return nil
}
// AdminGetTransactions get all block transactions filtered by conditions
func (wc *WalletClient) AdminGetTransactions(
ctx context.Context,
conditions *filter.TransactionFilter,
metadata map[string]any,
queryParams *filter.QueryParams,
) ([]*models.Transaction, error) {
return Search[filter.TransactionFilter, []*models.Transaction](
ctx, http.MethodPost,
"/admin/transactions/search",
wc.adminXPriv,
conditions,
metadata,
queryParams,
wc.doHTTPRequest,
)
}
// AdminGetTransactionsCount get a count of all the transactions filtered by conditions
func (wc *WalletClient) AdminGetTransactionsCount(
ctx context.Context,
conditions *filter.TransactionFilter,
metadata map[string]any,
) (int64, error) {
return Count[filter.TransactionFilter](
ctx, http.MethodPost,
"/admin/transactions/count",
wc.adminXPriv,
conditions,
metadata,
wc.doHTTPRequest,
)
}
// AdminGetUtxos get all block utxos filtered by conditions
func (wc *WalletClient) AdminGetUtxos(
ctx context.Context,
conditions *filter.AdminUtxoFilter,
metadata map[string]any,
queryParams *filter.QueryParams,
) ([]*models.Utxo, error) {
return Search[filter.AdminUtxoFilter, []*models.Utxo](
ctx, http.MethodPost,
"/admin/utxos/search",
wc.adminXPriv,
conditions,
metadata,
queryParams,
wc.doHTTPRequest,
)
}
// AdminGetUtxosCount get a count of all the utxos filtered by conditions
func (wc *WalletClient) AdminGetUtxosCount(
ctx context.Context,
conditions *filter.AdminUtxoFilter,
metadata map[string]any,
) (int64, error) {
return Count[filter.AdminUtxoFilter](
ctx, http.MethodPost,
"/admin/utxos/count",
wc.adminXPriv,
conditions,
metadata,
wc.doHTTPRequest,
)
}
// AdminGetXPubs get all block xpubs filtered by conditions
func (wc *WalletClient) AdminGetXPubs(ctx context.Context, conditions *filter.XpubFilter,
metadata map[string]any, queryParams *filter.QueryParams,
) ([]*models.Xpub, error) {
return Search[filter.XpubFilter, []*models.Xpub](
ctx, http.MethodPost,
"/admin/xpubs/search",
wc.adminXPriv,
conditions,
metadata,
queryParams,
wc.doHTTPRequest,
)
}
// AdminGetXPubsCount get a count of all the xpubs filtered by conditions
func (wc *WalletClient) AdminGetXPubsCount(
ctx context.Context,
conditions *filter.XpubFilter,
metadata map[string]any,
) (int64, error) {
return Count[filter.XpubFilter](
ctx, http.MethodPost,
"/admin/xpubs/count",
wc.adminXPriv,
conditions,
metadata,
wc.doHTTPRequest,
)
}
func (wc *WalletClient) adminGetModels(
ctx context.Context,
conditions map[string]interface{},