Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bech32 embeds #1078

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

vitorpamplona
Copy link
Collaborator

@vitorpamplona vitorpamplona commented Feb 23, 2024

Creates a nembed bech32 encoding to carry JSON-stringified events as Nostr URIs and facilitate the loading of those events by Clients without having to ping a relay.

This is particularly helpful for exporting existing/pre-signed health information payloads from corporate/private relays and send to users who might not have direct access to them. Health providers are used to including these payloads inside private communications with patients and adding information around them for better context.

A use case would be the following encrypted DM:

Hi Vitor, 

These are your new prescription for eyeglasses. This one is for distance. 
It's a small power but it can help when you are driving. 

nostr:nembed1...

The next one is for reading. This was adjusted to the distance of books. 
If you find yourself struggling to use a desktop computer, we can adjust 
those numbers for that need as well. 

nostr:nembed1...

Please double-check the sign of each number when ordering your glasses online. 

Here's your complete medical record with all the raw data from your visit today: 

nostr:nembed1...

Thanks for dropping by. Your business is very much appreciated. 

PS: I am not attached to the embed name. Suggestions are welcome.

@staab
Copy link
Member

staab commented Feb 23, 2024

I like this. It could solve the ninvite problem in #1062, and it seems like it could be applied very broadly to limit the spread of events that shouldn't stand on their own or need to be private. The one question I have is how long are these embeds? If it's 50 lines of text, that breaks the human readable (or at least parseable) requirement of kind 1s. Also, encoding json in a tlv is silly, we should at least use the tlv to reduce the payload size.

@vitorpamplona
Copy link
Collaborator Author

So an eyeglasses payload from an electronic medical record looks like this:

{
  "resourceType": "VisionPrescription",
  "status": "active",
  "created": "2014-06-15",
  "patient": {
    "reference": "Patient/example"
  },
  "dateWritten": "2014-06-15",
  "prescriber": {
    "reference": "Practitioner/example"
  },
  "lensSpecification": [
    {
      "eye": "right",
      "sphere": -2,
      "prism": [
        {
          "amount": 0.5,
          "base": "down"
        }
      ],
      "add": 2
    },
    {
      "eye": "left",
      "sphere": -1,
      "cylinder": -0.5,
      "axis": 180,
      "prism": [
        {
          "amount": 0.5,
          "base": "up"
        }
      ],
      "add": 2
    }
  ]
}

It is signed as a kind:82 (Medical Data) like this:

{
  "id": "73a5ace1c95de239ea355a745abfafee39d4dc613081a5c3dc25158c9385620d",
  "pubkey": "bcd4715cc34f98dce7b52fddaf1d826e5ce0263479b7e110a5bd3c3789486ca8",
  "created_at": 1708705561,
  "kind": 82,
  "tags": [],
  "content": "{\"resourceType\":\"VisionPrescription\",\"status\":\"active\",\"created\":\"2014-06-15\",\"patient\":{\"reference\":\"Patient/example\"},\"dateWritten\":\"2014-06-15\",\"prescriber\":{\"reference\":\"Practitioner/example\"},\"lensSpecification\":[{\"eye\":\"right\",\"sphere\":-2,\"prism\":[{\"amount\":0.5,\"base\":\"down\"}],\"add\":2},{\"eye\":\"left\",\"sphere\":-1,\"cylinder\":-0.5,\"axis\":180,\"prism\":[{\"amount\":0.5,\"base\":\"up\"}],\"add\":2}]}",
  "sig": "e87f7c360a1b01fc436b081037eb11b5e0c23714907427c5e124611a3a004d3c13841d965842766dd85a0a3eaa6c14e053764f67848e892f3a4776ce5046b63f"
}

which creates the following bech32:

nembed10v3xjepz8g3rwvmpx4skxef3vvun2er9xgenjetpxv6n2cfhxs6kzcnxv9nx2efn89jrgerrx
ccnxvpcx9sn2cenv33nydf3x5uxxwfn8q6nvv3svs3zcgnsw43xketeygazycnrvs6rwvf4vd3nxdrx8
yuxgcm9xa3r2vnxv3jxze33vsurydn9x43k2vpjxcengdeevgmk2vf3xpsn2cnyxd3nxdec8y6rsdnrv
yuzytpzvdex2ct5v4j97ct5ygarzdes8qmnqdf4xccjcgntd9hxgg368qezcgn5v9nhxg36tdwjcgnrd
ah8getwws3r5gnmts38yetnda6hycm923uhqe2uyga9cgjkd9ekjmmw2pex2umrwf5hqarfdah9cg3vt
s38xarpw36hxhpz8fwzyctrw35hve2uygk9cgnrwfjkzar9v3wzywjuygerqvf595crvtf3x4wzytzuy
fcxzarfv4h8ghpz8fa4cgnjv4nx2un9de3k2hpz8fwzy5rpw35k2mn59ajhsctdwpkx2hpz05k9cgnyv
96x24mjd968getwts3r5hpzxgcrzdpdxqmz6vf4ts3zchpzwpex2umrwf5kyetjts3r576uyfex2en9w
fjkucm9ts3r5hpz2pexzcm5d96xjmmwv4ez7etcv9khqmr9ts386tzuyfkx2mnn2dcx2cmfve5kxct5d
9hkuhpz8fdhkhpzv4uk2hpz8fwzyunfva58ghpz93wzyumsdpjhye2uygaz6v3vts38qunfwdk4cg36t
da4cgnpd4hh2mn5ts3r5vpwx5k9cgnzv9ek2hpz8fwzyer0wah9cgnat5k9cgnpv3j9cg36xf7jc76uy
fjhje2uyga9cgnvv4n8ghpz93wzyumsdpjhye2uygaz6vfvts3xx7tvd9hxgetjts3r5tfs9c6jchpzv
9uxju6uygarzwps93wzyurjd9ek6hpz8fdhkhpzv9kk7atww3wzyw3s9c6jchpzvfshxe2uyga9cgn4w
pwzyl2a93wzyctyv3wzyw3j04wh6g3vyfekjeez8g3x2wphvcmkxvekxpsnzc3sx9nxxdpnxe3rqwp3x
qenwetzxyckydt9xp3nyvehxy6rjvphxserwce4v5cnydpkxyckzvmpxqcrgepnvvcnxwp5x9jrjd348
q6rydekxejxgwp4vycxzvm9v9snvce3x3jnqdfnxumrge3kxuurgwr98qunye3nvy6rwdekvdjn2vp5x
e3rvvmxyf7slvlkjd

Also, encoding json in a tlv is silly, we should at least use the tlv to reduce the payload size.

Agree. But I didn't want to make a whole new serialization scheme for events.

@vitorpamplona
Copy link
Collaborator Author

vitorpamplona commented Feb 23, 2024

Funny thing... if we start using compression, we could gzip the JSON-stringified event and the final re-encoded bech32 becomes smaller than the original event.

The full JSON of the signed event is 802 chars
The bech32 encoded of the raw string is 1287 chars

Compression of the signed event JSON string is 474 bytes
The bech32 encoded compression yelds 770 chars

nembed1r79ssqqqqqqqqqqq34fvkmkmxqg0c9u7aktvhp6gly4s9frgp7c4ztrh2u8pz0cyf9hxxp877
7h228x6yzr3upq2kwen8wlyhjvu5enrd3tjgvrc9mkgqztvwj0rxzukjjwu0sj2eqs53qvgxefhzyqcn
0zvpjyg62vet6f0u4z6a2j43ppcyj8yg0tkvfjfykaguxfmurkp4zfy6c6naf9kysqmqxqt00ezndepx
7z9ndtgzzwggllzvsmfqmzwxe4w0j4u6jf5ax7aefxts87due560h8ndxffxykdm0tqcv579upundmt9
adm8kv6aa66ee7ns03rf4r76tl66ktde5uwzaqevurdy4rlexxtak7wkze3p44kt9pecjeafe24mdp5h
d6r9jyfz69c0f052400rkqlzjajn864arr4qfcaa4fmjedqytp60zkr0k50hvpjls4hhyxnlrvqh4dwz
ghdkmy7k306ujl82rmul5ajv8l6ezs24mw7c478uva30rl8ew7gnlgjk428qhr6l879hetw80zepmuhu
ph2h0ayplf864yxlww46rl69re3a669zp07hltxy8dl7d977s0kcaehehq93al656vgkmgsyraxstgkn
w5d20qypm0xd48vs3gq3q68cus4xu4pfxqyp7n4vsp5qscvmpa8d4399lzs43c55zqafe9kpkk5vv46y
emjmduq02gyv4wscm38e4pyxg2h87crymcwxvpzqvqqq9cs8mr

As a comparison, a regular nevent with relays in it is this big:

nevent1qqst8w0xhjf9ehy98tmgcmk7zaj6p4lxjwjf8z08s9trzmjg4yzdywcpzemhxue69uhhv6t5d
aezumn0wd68yvfwvdhk6qgawaehxw309ahx7um5wgkhqatz9emk2mrvdaexgetj9ehx2aqpz3mhxue69
uhhyetvv9ujumn0wd68ytnzvupzp78lz8r60568sd2a8dx3wnj6gume02gxaf92vx4fk67qv5kpagt65
gxsjx

@AsaiToshiya
Copy link
Collaborator

PS: I am not attached to the embed name. Suggestions are welcome.

These are just ideas: nraw, njson, nstuff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants