-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_fetch.go
More file actions
187 lines (170 loc) · 4.79 KB
/
client_fetch.go
File metadata and controls
187 lines (170 loc) · 4.79 KB
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
package hcs11
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
hedera "github.com/hashgraph/hedera-sdk-go/v2"
)
// UpdateAccountMemoWithProfile updates the requested resource.
func (c *Client) UpdateAccountMemoWithProfile(
ctx context.Context,
accountID string,
profileTopicID string,
) (TransactionResult, error) {
if strings.TrimSpace(accountID) == "" {
return TransactionResult{
Success: false,
Error: "account ID is required",
}, nil
}
if strings.TrimSpace(c.operatorPrivateKey) == "" {
return TransactionResult{
Success: false,
Error: "operator private key is required",
}, nil
}
parsedAccountID, err := hedera.AccountIDFromString(accountID)
if err != nil {
return TransactionResult{}, fmt.Errorf("invalid account ID: %w", err)
}
transaction := hedera.NewAccountUpdateTransaction().
SetAccountID(parsedAccountID).
SetAccountMemo(c.SetProfileForAccountMemo(profileTopicID, 1))
response, err := transaction.Execute(c.hederaClient)
if err != nil {
return TransactionResult{}, err
}
receipt, err := response.GetReceipt(c.hederaClient)
if err != nil {
return TransactionResult{}, err
}
if receipt.Status.String() != "SUCCESS" {
return TransactionResult{
Success: false,
Error: fmt.Sprintf("transaction failed: %s", receipt.Status.String()),
}, nil
}
return TransactionResult{
Success: true,
}, nil
}
// FetchProfileByAccountID fetches the requested resource data.
func (c *Client) FetchProfileByAccountID(
ctx context.Context,
accountID string,
network string,
) (FetchProfileResponse, error) {
normalizedAccountID := strings.TrimSpace(accountID)
if normalizedAccountID == "" {
return FetchProfileResponse{
Success: false,
Error: "account ID is required",
}, nil
}
if strings.TrimSpace(network) == "" {
network = c.network
}
memo, err := c.mirrorClient.GetAccountMemo(ctx, normalizedAccountID)
if err != nil {
return FetchProfileResponse{}, err
}
if !strings.HasPrefix(memo, "hcs-11:") {
return FetchProfileResponse{
Success: false,
Error: fmt.Sprintf("account %s does not have a valid HCS-11 memo", normalizedAccountID),
}, nil
}
reference := strings.TrimPrefix(memo, "hcs-11:")
switch {
case strings.HasPrefix(reference, "hcs://"):
return c.fetchFromHCSReference(ctx, reference, network)
case strings.HasPrefix(reference, "ipfs://"):
ipfsHash := strings.TrimPrefix(reference, "ipfs://")
return c.fetchFromURL(ctx, "https://ipfs.io/ipfs/"+ipfsHash, "")
case strings.HasPrefix(reference, "ar://"):
arweaveID := strings.TrimPrefix(reference, "ar://")
return c.fetchFromURL(ctx, "https://arweave.net/"+arweaveID, "")
default:
return FetchProfileResponse{
Success: false,
Error: fmt.Sprintf("invalid protocol reference format: %s", reference),
}, nil
}
}
func (c *Client) fetchFromHCSReference(
ctx context.Context,
reference string,
network string,
) (FetchProfileResponse, error) {
parts := strings.Split(reference, "/")
if len(parts) < 4 {
return FetchProfileResponse{
Success: false,
Error: fmt.Sprintf("invalid HCS protocol reference format: %s", reference),
}, nil
}
profileTopicID := strings.TrimSpace(parts[3])
cdnURL := fmt.Sprintf(
"%s/api/inscription-cdn/%s?network=%s",
c.inscriberBaseURL,
profileTopicID,
strings.TrimSpace(network),
)
response, err := c.fetchFromURL(ctx, cdnURL, profileTopicID)
if err != nil {
return FetchProfileResponse{}, err
}
return response, nil
}
func (c *Client) fetchFromURL(
ctx context.Context,
endpoint string,
profileTopicID string,
) (FetchProfileResponse, error) {
request, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return FetchProfileResponse{}, err
}
request.Header.Set("Accept", "application/json")
response, err := http.DefaultClient.Do(request)
if err != nil {
return FetchProfileResponse{}, err
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
return FetchProfileResponse{}, err
}
if response.StatusCode < 200 || response.StatusCode >= 300 {
return FetchProfileResponse{
Success: false,
Error: fmt.Sprintf("failed to fetch profile content: %s", response.Status),
}, nil
}
var profile HCS11Profile
if err := json.Unmarshal(body, &profile); err != nil {
return FetchProfileResponse{
Success: false,
Error: "invalid HCS-11 profile data",
}, nil
}
validation := c.ValidateProfile(profile)
if !validation.Valid {
return FetchProfileResponse{
Success: false,
Error: fmt.Sprintf("invalid HCS-11 profile data: %s", strings.Join(validation.Errors, ", ")),
}, nil
}
return FetchProfileResponse{
Success: true,
Profile: &profile,
TopicInfo: &ResolvedTopicInfo{
InboundTopic: profile.InboundTopicID,
OutboundTopic: profile.OutboundTopicID,
ProfileTopicID: profileTopicID,
},
}, nil
}