Skip to content

Commit 599ab7d

Browse files
committed
Implement getting claim
1 parent 0883fee commit 599ab7d

2 files changed

Lines changed: 175 additions & 1 deletion

File tree

rpc/claim.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,98 @@ func convertProtobufToClaim(protobuf map[int]any, transactions map[string]any) m
9898

9999
claimValue := map[string]any{}
100100

101+
streamValue, streamOk := decodedProtobufClaimData[1]
102+
if streamOk {
103+
stream := streamValue.(map[int]any)
104+
105+
sourceValue, sourceOk := stream[1]
106+
if sourceOk {
107+
source := map[string]any{}
108+
109+
hashValue, hashOk := (sourceValue.(map[int]any))[1]
110+
if hashOk {
111+
hash := hex.EncodeToString(hashValue.([]uint8))
112+
source["hash"] = hash
113+
}
114+
115+
nameValue, nameOk := (sourceValue.(map[int]any))[2]
116+
if nameOk {
117+
name := string(nameValue.([]uint8))
118+
source["name"] = name
119+
}
120+
121+
mediaTypeValue, mediaTypeOk := (sourceValue.(map[int]any))[4]
122+
if mediaTypeOk {
123+
mediaType := string(mediaTypeValue.([]uint8))
124+
source["media_type"] = mediaType
125+
}
126+
127+
urlValue, urlOk := (sourceValue.(map[int]any))[5]
128+
if urlOk {
129+
url := string(urlValue.([]uint8))
130+
source["url"] = url
131+
}
132+
133+
sdHashValue, sdHashOk := (sourceValue.(map[int]any))[6]
134+
if sdHashOk {
135+
sdHash := hex.EncodeToString(sdHashValue.([]uint8))
136+
source["sd_hash"] = sdHash
137+
}
138+
139+
claimValue["source"] = source
140+
}
141+
142+
authorValue, authorOk := stream[2]
143+
if authorOk {
144+
author := string(authorValue.([]uint8))
145+
claimValue["author"] = author
146+
}
147+
148+
licenseValue, licenseOk := stream[3]
149+
if licenseOk {
150+
license := string(licenseValue.([]uint8))
151+
claimValue["license"] = license
152+
}
153+
154+
licenseURLValue, licenseURLOk := stream[4]
155+
if licenseURLOk {
156+
licenseURL := string(licenseURLValue.([]uint8))
157+
claimValue["license_url"] = licenseURL
158+
}
159+
160+
// Temporary
161+
claimValue["stream_type"] = "document"
162+
163+
_, hasStreamTypeImage := stream[10]
164+
if hasStreamTypeImage {
165+
claimValue["stream_type"] = "image"
166+
}
167+
_, hasStreamTypeVideo := stream[11]
168+
if hasStreamTypeVideo {
169+
claimValue["stream_type"] = "video"
170+
}
171+
_, hasStreamTypeAudio := stream[11]
172+
if hasStreamTypeAudio {
173+
claimValue["stream_type"] = "audio"
174+
}
175+
_, hasStreamTypeSoftware := stream[13]
176+
if hasStreamTypeSoftware {
177+
claimValue["stream_type"] = "software"
178+
}
179+
}
180+
101181
titleValue, titleOk := decodedProtobufClaimData[8]
102182
if titleOk {
103183
title := string(titleValue.([]uint8))
104184
claimValue["title"] = title
105185
}
106186

187+
descriptionValue, descriptionOk := decodedProtobufClaimData[9]
188+
if descriptionOk {
189+
description := string(descriptionValue.([]uint8))
190+
claimValue["description"] = description
191+
}
192+
107193
thumbnailValue, thumbnailOk := decodedProtobufClaimData[10]
108194
if thumbnailOk {
109195
thumbnail := map[string]any{}
@@ -128,5 +214,25 @@ func convertProtobufToClaim(protobuf map[int]any, transactions map[string]any) m
128214

129215
claim["value"] = claimValue
130216

217+
_, hasClaimTypeStream := decodedProtobufClaimData[1]
218+
if hasClaimTypeStream {
219+
claim["value_type"] = "stream"
220+
}
221+
_, hasClaimTypeChannel := decodedProtobufClaimData[2]
222+
if hasClaimTypeChannel {
223+
claim["value_type"] = "channel"
224+
}
225+
_, hasClaimTypeCollection := decodedProtobufClaimData[3]
226+
if hasClaimTypeCollection {
227+
claim["value_type"] = "collection"
228+
}
229+
_, hasClaimTypeRepost := decodedProtobufClaimData[4]
230+
if hasClaimTypeRepost {
231+
claim["value_type"] = "repost"
232+
}
233+
234+
// claim["_1"] = protobuf
235+
// claim["_2"] = decodedProtobufClaimData
236+
131237
return claim
132238
}

rpc/server.go

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,75 @@ func handleJSONRPCMessageFileSetStatus(w http.ResponseWriter, params any) {
513513

514514
func handleJSONRPCMessageGet(w http.ResponseWriter, params any) {
515515
// Relaxed
516-
sendErrorResponse(w, 501, "NOT IMPLEMENTED")
516+
var paramsMap map[string]any = params.(map[string]any)
517+
518+
uri, _ := paramsMap["uri"].(string)
519+
520+
resolveResp, _ := SendJSON("s1.lbry.network", 50001, map[string]any{
521+
"jsonrpc": "2.0",
522+
"id": rand.Int() + 1,
523+
"method": "blockchain.claimtrie.resolve",
524+
"params": []string{uri},
525+
})
526+
527+
var resolutions map[string]any = map[string]any{}
528+
529+
_, resultIsString := resolveResp["result"].(string)
530+
if resultIsString {
531+
decodedBase64, _ := base64.StdEncoding.DecodeString(resolveResp["result"].(string))
532+
533+
decodedProtobuf, _ := DecodeRawProto(decodedBase64)
534+
535+
var resolutionData []any
536+
537+
_, okResolution := decodedProtobuf[1].([]any)
538+
if okResolution {
539+
resolutionData = decodedProtobuf[1].([]any)
540+
} else {
541+
resolutionData = []any{decodedProtobuf[1]}
542+
}
543+
544+
txIDs := []string{}
545+
546+
for _, claim := range resolutionData {
547+
claimMap := claim.(map[int]any)
548+
txidValue, txidOk := claimMap[1]
549+
if txidOk {
550+
txidBytes := txidValue.([]byte)
551+
slices.Reverse(txidBytes)
552+
txID := hex.EncodeToString(txidBytes)
553+
txIDs = append(txIDs, txID)
554+
}
555+
}
556+
557+
transactionResp, _ := SendJSON("s1.lbry.network", 50001, map[string]any{
558+
"jsonrpc": "2.0",
559+
"id": rand.Int() + 1,
560+
"method": "blockchain.transaction.get_batch",
561+
"params": txIDs,
562+
})
563+
564+
transactionData := transactionResp["result"].(map[string]any)
565+
566+
for _, claim := range resolutionData {
567+
claimMap, ok := claim.(map[int]any)
568+
if ok {
569+
item := convertProtobufToClaim(claimMap, transactionData)
570+
571+
resolutionKey := uri
572+
resolutions[resolutionKey] = item
573+
}
574+
575+
}
576+
}
577+
578+
sdHash := resolutions[uri].(map[string]any)["value"].(map[string]any)["source"].(map[string]any)["sd_hash"].(string)
579+
580+
streamingURL := "http://localhost:5280/stream/" + sdHash
581+
582+
sendResultResponse(w, map[string]any{
583+
"streaming_url": streamingURL,
584+
})
517585
}
518586

519587
func handleJSONRPCMessagePeerList(w http.ResponseWriter, params any) {

0 commit comments

Comments
 (0)