-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.go
98 lines (85 loc) · 2.57 KB
/
query.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
package sdk
import (
"context"
"encoding/base64"
"encoding/hex"
"connectrpc.com/connect"
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/query"
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/query/queryconnect"
)
type QueryServiceClient queryconnect.QueryServiceClient
func NewQueryServiceClient(u *UtxorpcClient) QueryServiceClient {
return u.NewQueryServiceClient()
}
func (u *UtxorpcClient) NewQueryServiceClient() QueryServiceClient {
return queryconnect.NewQueryServiceClient(
u.httpClient,
u.baseUrl,
connect.WithGRPC(),
)
}
func (u *UtxorpcClient) QueryService() QueryServiceClient {
return u.Query
}
func (u *UtxorpcClient) ReadParams() (*connect.Response[query.ReadParamsResponse], error) {
ctx := context.Background()
return u.ReadParamsWithContext(ctx)
}
func (u *UtxorpcClient) ReadParamsWithContext(
ctx context.Context,
) (*connect.Response[query.ReadParamsResponse], error) {
req := connect.NewRequest(&query.ReadParamsRequest{})
u.AddHeadersToRequest(req)
return u.Query.ReadParams(ctx, req)
}
func (u *UtxorpcClient) ReadUtxo(
txHashStr string,
txIndex uint32,
) (*connect.Response[query.ReadUtxosResponse], error) {
var txHashBytes []byte
var err error
// Attempt to decode the input as hex
txHashBytes, hexErr := hex.DecodeString(txHashStr)
if hexErr != nil {
// If not hex, attempt to decode as Base64
txHashBytes, err = base64.StdEncoding.DecodeString(txHashStr)
if err != nil {
return nil, err
}
}
// Create TxoRef with the decoded hash bytes
txoRef := &query.TxoRef{
Hash: txHashBytes, // Use the decoded []byte
Index: txIndex,
}
req := &query.ReadUtxosRequest{Keys: []*query.TxoRef{txoRef}}
return u.ReadUtxos(req)
}
func (u *UtxorpcClient) ReadUtxos(
req *query.ReadUtxosRequest,
) (*connect.Response[query.ReadUtxosResponse], error) {
ctx := context.Background()
return u.ReadUtxosWithContext(ctx, req)
}
func (u *UtxorpcClient) ReadUtxosWithContext(
ctx context.Context,
queryReq *query.ReadUtxosRequest,
) (*connect.Response[query.ReadUtxosResponse], error) {
req := connect.NewRequest(queryReq)
u.AddHeadersToRequest(req)
return u.Query.ReadUtxos(ctx, req)
}
func (u *UtxorpcClient) SearchUtxos(
req *query.SearchUtxosRequest,
) (*connect.Response[query.SearchUtxosResponse], error) {
ctx := context.Background()
return u.SearchUtxosWithContext(ctx, req)
}
func (u *UtxorpcClient) SearchUtxosWithContext(
ctx context.Context,
queryReq *query.SearchUtxosRequest,
) (*connect.Response[query.SearchUtxosResponse], error) {
req := connect.NewRequest(queryReq)
u.AddHeadersToRequest(req)
return u.Query.SearchUtxos(ctx, req)
}