nbd-wtf / nostr-tools

Tools for developing Nostr clients.
The Unlicense
685 stars 188 forks source link

nip19.decode has some issue #393

Closed itawa-panema closed 5 months ago

itawa-panema commented 5 months ago

There is some issues decoding some nip19 encoded values.

Here is an example from a Fiatjaf's note [1]:

nevent1qqstmzcm5zh6gx4t3mn2f0rywmpugpfpxz9zqkx792y5ml0un7hr98gppamhxue69uhkummnw3ezumt0d5q3samnwvaz7tmjv4kxz7fwwdhx7un59eek7cmfv9kqysphvesn2dnxx4jrvwfkxfskyvt9xd3kgdpjx3jnwdfcvvenqvpjvgurvd34vcmkyvry8pjxxet989nx2wt9xgursephxu6nzctrxyung5ueddn:

import { nip19 } from "nostr-tools";
nip19.decode("nevent1qqstmzcm5zh6gx4t3mn2f0rywmpugpfpxz9zqkx792y5ml0un7hr98gppamhxue69uhkummnw3ezumt0d5q3samnwvaz7tmjv4kxz7fwwdhx7un59eek7cmfv9kqysphvesn2dnxx4jrvwfkxfskyvt9xd3kgdpjx3jnwdfcvvenqvpjvgurvd34vcmkyvry8pjxxet989nx2wt9xgursephxu6nzctrxyung5ueddn")

returned:

1045 |       if (!tlv[0]?.[0])
1046 |         throw new Error("missing TLV 0 for nevent");
1047 |       if (tlv[0][0].length !== 32)
1048 |         throw new Error("TLV 0 should be 32 bytes");
1049 |       if (tlv[2] && tlv[2][0].length !== 32)
1050 |         throw new Error("TLV 2 should be 32 bytes");
                     ^
error: TLV 2 should be 32 bytes

FYI, this nevent can be decoded well with https://nak.nostr.com/

-- [1] note1nj5u8xhlsq06q6kkscxdx7l4zkapckqkzth5xup4r77hx7ys4g7qt3kmg6

fiatjaf commented 5 months ago

No, nevent1qqstmzcm5zh6gx4t3mn2f0rywmpugpfpxz9zqkx792y5ml0un7hr98gppamhxue69uhkummnw3ezumt0d5q3samnwvaz7tmjv4kxz7fwwdhx7un59eek7cmfv9kqysphvesn2dnxx4jrvwfkxfskyvt9xd3kgdpjx3jnwdfcvvenqvpjvgurvd34vcmkyvry8pjxxet989nx2wt9xgursephxu6nzctrxyung5ueddn is invalid because the pubkey is 64 bytes, not 32, I don't know why or who made such an invalid code, but it is, and nostr-tools is correct.

itawa-panema commented 5 months ago

It's weird because is is found on your note.

I found another one of your note that doesn't provide a valid nevent:

note hex id: 600502fe324f95aebd749bd700c6f17619fc768700d1b228a6a691f81672cf2b

"If anyone knows more about this, please give your assessment.
nostr:nevent1qqsr3n57p07s736jmp8ct82hz92y080g9rglt63qrj4edg4pd7vpt3gpp4mhxue69uhkummn9ekx7mqzgq6rve3kv93kge3exqmrgv3nxscnyvnxvycrsvrxx4jrjetxxcun2denvsmnjvfsxsux2vrzxg6nvd3nxpnrsctxxgunvvenx93rvefhntdeca"

nostr-tools returns

failed to decode bech32 nevent1qqsr3n57p07s736jmp8ct82hz92y080g9rglt63qrj4edg4pd7vpt3gpp4mhxue69uhkummn9ekx7mqzgq6rve3kv93kge3exqmrgv3nxscnyvnxvycrsvrxx4jrjetxxcun2denvsmnjvfsxsux2vrzxg6nvd3nxpnrsctxxgunvvenx93rvefhntdeca 
Error: TLV 2 should be 32 bytes

And https://nak.nostr.com/ is able to decode it.

I have other example of nevent you have published that are not handled well by nostr-tools but can be decoded by nak or nostrudel

fiatjaf commented 5 months ago

The fact that it is found on my note is irrelevant.

itawa-panema commented 5 months ago

Well, if the nostr-tools software is unable to decode the events (nevents) present in the notes of the nostr-tools creator, it could potentially indicate the presence of a bug in the nostr-tools library. Additionally, the statement does not explain why other nostr clients, such as nak or nostrudel, are able to successfully decode the same events.

fiatjaf commented 5 months ago

It does explain: they are not checking if the encoded values are correct like nostr-tools is. My nip19 code was not created with nostr-tools, therefore it is not nostr-tools' fault that it is broken.

itawa-panema commented 5 months ago

Thank you for clarify. It is a relief that it is not a nostr-tools issue. I was pretty worried when I saw all those errors popping on my client.