fiatjaf / go-lnurl

helpers for writing lnurl stuff in wallets and services

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

params.Metadata.Encode() doesn't add a space

cjc21 opened this issue · comments

commented
func (params LNURLPayParams) MetadataEncoded() string {
	if params.EncodedMetadata == "" {
		params.EncodedMetadata = params.Metadata.Encode()
	}
	return params.EncodedMetadata
}

In this code, params.Metadata.Encode() doesn't add a space when encoding the description.

Current---> "[[\"text/plain\",\"hello\"]]"
Expected-> "[[\"text/plain\", \"hello\"]]"

I can get around this by setting the EncodedMetadata directly, but I believe the space should be included automatically?

Why is a space expected?

commented

If I create a lnurlp on lnbits, the space is included in the description hash. Using the method above to pay that lnurlp removed the space thus causes a mismatch in description hash.

I'm still playing around with this library so let me know if I'm misunderstanding something

If you are reading the lnurlp from lnbits, then this library should populate EncodedMetadata with whatever it got from lnbits and you should use that to check the hash, never try to reencode anything, the .Encode() function is for when you are using this library to generate your own lnurlp/metadata.

(Let me know if it is not working like I described above -- writing software without tests has some risks.)

Ok that makes sense. Setting EncodedMetadata did it for me

That's not quite what I said, but fine!

commented

I'm not encoding it manually though. The way I'm reading lnurlp now is constructing LNURLPayParams, and then calling Call. MetadataEncoded() is invoked within Call, which invokes Encode() if EncodedMetadata is not set. So setting EncodedMetadata avoids the Encode() call. (the workaround I mentioned above)

lnurlParams := lnurl.LNURLPayParams{
		Callback:    payload.Params.Callback,
		MaxSendable: payload.Params.MaxSendable,
		MinSendable: payload.Params.MinSendable,
		Metadata:    payload.Params.Metadata,
		Tag:         payload.Params.Tag,
}
lnurlParams.EncodedMetadata = payload.Params.EncodedMetadata     // avoid the Encode() call

Sounds reasonable?

Yes, except for the fact that .EncodedMetadata should be already filled with the metadata string as received from the service when you call HandleLNURL() or if you do json.Unmarshal() with LNURLPayParams and the received JSON response (that's what HandleLNURL() does).