Skip to content

Commit 91f0790

Browse files
committed
Fix getting claim id
1 parent 599ab7d commit 91f0790

3 files changed

Lines changed: 52 additions & 15 deletions

File tree

rpc/claim.go

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,30 @@ func convertProtobufToClaim(protobuf map[int]any, transactions map[string]any) m
3232
}
3333
}
3434

35+
if len(claimScript.ClaimData) < 85 {
36+
// TODO: Older schemes
37+
return claim
38+
}
39+
3540
decodedProtobufClaimData, _ := DecodeRawProto(claimScript.ClaimData[85:])
3641

37-
claimID := claimScript.ClaimID
38-
slices.Reverse(claimID)
39-
claim["claim_id"] = hex.EncodeToString(claimID)
42+
noutValue, noutOk := protobuf[2]
43+
if noutOk {
44+
nout := noutValue.(uint64)
45+
claim["nout"] = nout
46+
}
47+
48+
claimID := claimScript.ClaimID
49+
if claimID == nil{
50+
var nout uint32
51+
noutValue, ok := noutValue.(uint32)
52+
if ok{
53+
nout = noutValue
54+
}
55+
claimID = ComputeClaimID(txidValue.([]byte),nout)
56+
}
57+
//slices.Reverse(claimID)
58+
claim["claim_id"] = hex.EncodeToString(claimID)
4059

4160
heightValue, heightOk := protobuf[3]
4261
if heightOk {
@@ -51,14 +70,18 @@ func convertProtobufToClaim(protobuf map[int]any, transactions map[string]any) m
5170

5271
shortURLValue, shortURLOk := meta[3]
5372
if shortURLOk {
54-
shortURL := string(shortURLValue.([]uint8))
55-
claim["short_url"] = "lbry://" + shortURL
73+
shortURL, ok := shortURLValue.([]uint8)
74+
if ok {
75+
claim["short_url"] = "lbry://" + string(shortURL)
76+
}
5677
}
5778

5879
canonicalURLValue, canonicalURLOk := meta[4]
5980
if canonicalURLOk {
60-
canonicalURL := string(canonicalURLValue.([]uint8))
61-
claim["canonical_url"] = "lbry://" + canonicalURL
81+
canonicalURL, ok := canonicalURLValue.([]uint8)
82+
if ok {
83+
claim["canonical_url"] = "lbry://" + string(canonicalURL)
84+
}
6285
}
6386

6487
isControllingValue, isControllingOk := meta[5]
@@ -206,10 +229,16 @@ func convertProtobufToClaim(protobuf map[int]any, transactions map[string]any) m
206229
tagsValue, tagsOk := decodedProtobufClaimData[11]
207230
if tagsOk {
208231
var tags []string
209-
for _, tagValue := range tagsValue.([]any) {
210-
tags = append(tags, string(tagValue.([]uint8)))
232+
_, ok := tagsValue.([]any)
233+
if ok {
234+
for _, tagValue := range tagsValue.([]any) {
235+
tag, ok := tagValue.([]uint8)
236+
if ok {
237+
tags = append(tags, string(tag))
238+
}
239+
}
240+
claimValue["tags"] = tags
211241
}
212-
claimValue["tags"] = tags
213242
}
214243

215244
claim["value"] = claimValue

rpc/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,9 @@ func handleJSONRPCMessageResolve(w http.ResponseWriter, params any) {
651651
txIDs := []string{}
652652

653653
for _, claim := range resolutionData {
654+
if claim == nil {
655+
continue
656+
}
654657
claimMap := claim.(map[int]any)
655658
txidValue, txidOk := claimMap[1]
656659
if txidOk {

rpc/transaction.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ func parseClaimScript(script []byte) (*ClaimScript, error) {
197197
if n2 == 0 {
198198
return nil, fmt.Errorf("failed to read claim data")
199199
}
200-
return &ClaimScript{Name: string(nameBytes), ClaimData: claimData}, nil
200+
return &ClaimScript{
201+
Name: string(nameBytes),
202+
ClaimData: claimData,
203+
}, nil
201204

202205
case opUpdateClaim:
203206
// OP_UPDATE_CLAIM <name> <claim_id_20b> <claim_data> OP_2DROP OP_2DROP
@@ -214,8 +217,10 @@ func parseClaimScript(script []byte) (*ClaimScript, error) {
214217
return nil, fmt.Errorf("failed to read claim data")
215218
}
216219
return &ClaimScript{
217-
Name: string(nameBytes), ClaimID: claimIDBytes,
218-
ClaimData: claimData, IsUpdate: true,
220+
Name: string(nameBytes),
221+
ClaimID: claimIDBytes,
222+
ClaimData: claimData,
223+
IsUpdate: true,
219224
}, nil
220225

221226
default:
@@ -241,11 +246,11 @@ func reverseBytes(b []byte) []byte {
241246
}
242247

243248
// computeClaimID computes claim_id from tx_hash (internal byte order) and output index.
244-
func computeClaimID(txHash []byte, nout uint32) string {
249+
func ComputeClaimID(txHash []byte, nout uint32) []byte {
245250
buf := make([]byte, 36)
246251
copy(buf, txHash)
247252
binary.BigEndian.PutUint32(buf[32:], nout)
248-
return hex.EncodeToString(reverseBytes(hash160(buf)))
253+
return reverseBytes(hash160(buf))
249254
}
250255

251256
// claimIDFromBytes converts raw claim_id bytes (from script) to display hex.

0 commit comments

Comments
 (0)