Tyler the Enginigger on Nostr: # encode a unsigned variable length integer def encode_uvint(value): output = ...
# encode a unsigned variable length integer
def encode_uvint(value):
output = bytearray()
while value >= 0x80:
output.append((value & 0x7F) + 0x80)
value >>= 8
output.append(value)
return output
# decode a unsigned variable length integer
# return the number of bytes consumed and the value
# On failure, the returned number of bytes consumed is -1
def decode_uvint(data):
result = 0
shift = 0
offset = 0
while True:
if offset >= len(data):
return (-1, None)
result |= (data[offset] & 0x7f) << shift
if data[offset] & 0x80 == 0:
return (offset + 1, result)
shift += 7
offset += 1
Published at
2024-11-07 01:07:21Event JSON
{
"id": "a247484c76543795978a3af07856b17204b635b21fe1c82161b1c08136ff7f9a",
"pubkey": "e9c3448952ee9b09ac0c9a13e32b32f20358814e3e4f4fa666064d00361f0eda",
"created_at": 1730941641,
"kind": 1,
"tags": [
[
"p",
"febfa0060ba0f457deec201d190ca2e83b53b6d66d943b026a0a0730ae7c6cee",
"wss://relay.mostr.pub"
],
[
"e",
"df02fa2f7a7ad37fc67c22dc0d676b4990ddfe4b11c57da360baf3d09b208dbe",
"wss://relay.mostr.pub",
"reply"
],
[
"proxy",
"https://nicecrew.digital/objects/3205c999-a86b-48ea-b5ee-f02d70eef483",
"activitypub"
]
],
"content": "\n# encode a unsigned variable length integer\ndef encode_uvint(value):\n output = bytearray()\n while value \u003e= 0x80:\n output.append((value \u0026 0x7F) + 0x80)\n value \u003e\u003e= 8\n output.append(value)\n return output\n\n# decode a unsigned variable length integer\n# return the number of bytes consumed and the value\n# On failure, the returned number of bytes consumed is -1\ndef decode_uvint(data):\n result = 0\n shift = 0\n offset = 0\n while True:\n if offset \u003e= len(data):\n return (-1, None)\n result |= (data[offset] \u0026 0x7f) \u003c\u003c shift\n if data[offset] \u0026 0x80 == 0:\n return (offset + 1, result)\n shift += 7\n offset += 1\n",
"sig": "7b9f29010428331ade99d234ea08c5bb995343a9f8d38eb6442bc55973b98ce3ad2247777d42c9e1b627c621c1e76023e863f48b7eb6d8d4c1704f5601bbf0f2"
}