implement better xor_encrypt, update uBitcoin #16
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
In this PR I suggest a few changes in the encoding format of the lnurl data:
nonce
together with encrypted data and HMACOther changes:
random(9)
for every byte, now it'srandom(256)
)Encoding format
Suggested data encoding has the following format:
0x01
for XOR-encryption (that is ok for data smaller than the key size), later we can extend it with other encryption formats like AES-CBC-HMAC and what not.<len><nonce>
, in this implementation we use 8-byte nonce but it can be extended if required.<len><payload>
Keys
Keys are derived from a shared secret. There are two keys - for encryption and for authentication.
Round secret for encryption is calculated as
hmac(key, "Round secret:" | nonce)
, HMAC at the end is calculated ashmac(key, "Data:" | payload)
.Payload
Payload is a simple XOR of the round key with actual data contains the following items:
$
for USD cents, can be extended to other currencies as well)Nice thing about varints is that it can encode any number between 0 and 2^64 with 1-byte overhead. For values up to 252 it takes only 1 byte. See https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
Python decoding implementation
Resulting LNURL can be decoded with the following python script (using
embit
library here, but can be easily adopted to any other bitcoin library):