nostrdb/block: add bolt11 invoice encoding/decoding
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
committed by
Daniel D’Aquino
parent
acaf327a07
commit
373cd71f69
69
nostrdb/src/invoice.c
Normal file
69
nostrdb/src/invoice.c
Normal file
@@ -0,0 +1,69 @@
|
||||
|
||||
#include "cursor.h"
|
||||
#include "invoice.h"
|
||||
#include "bolt11/bolt11.h"
|
||||
#include "bolt11/amount.h"
|
||||
|
||||
int ndb_encode_invoice(struct cursor *cur, struct bolt11 *invoice) {
|
||||
if (!invoice->description && !invoice->description_hash)
|
||||
return 0;
|
||||
|
||||
if (!cursor_push_byte(cur, 1))
|
||||
return 0;
|
||||
|
||||
// TODO: make cursor_cursor_push_varint uint64_t
|
||||
if (!cursor_push_varint(cur, invoice->msat->millisatoshis))
|
||||
return 0;
|
||||
|
||||
if (!cursor_push_varint(cur, invoice->timestamp))
|
||||
return 0;
|
||||
|
||||
if (!cursor_push_varint(cur, invoice->expiry))
|
||||
return 0;
|
||||
|
||||
if (invoice->description) {
|
||||
if (!cursor_push_byte(cur, 1))
|
||||
return 0;
|
||||
if (!cursor_push_c_str(cur, invoice->description))
|
||||
return 0;
|
||||
} else {
|
||||
if (!cursor_push_byte(cur, 2))
|
||||
return 0;
|
||||
if (!cursor_push(cur, invoice->description_hash->u.u8, 32))
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ndb_decode_invoice(struct cursor *cur, struct ndb_invoice *invoice)
|
||||
{
|
||||
unsigned char desc_type;
|
||||
if (!cursor_pull_byte(cur, &invoice->version))
|
||||
return 0;
|
||||
|
||||
if (!cursor_pull_varint(cur, &invoice->amount))
|
||||
return 0;
|
||||
|
||||
if (!cursor_pull_varint(cur, &invoice->timestamp))
|
||||
return 0;
|
||||
|
||||
if (!cursor_pull_varint(cur, &invoice->expiry))
|
||||
return 0;
|
||||
|
||||
if (!cursor_pull_byte(cur, &desc_type))
|
||||
return 0;
|
||||
|
||||
if (desc_type == 1) {
|
||||
if (!cursor_pull_c_str(cur, (const char**)&invoice->description))
|
||||
return 0;
|
||||
} else if (desc_type == 2) {
|
||||
invoice->description_hash = cur->p;
|
||||
if (!cursor_skip(cur, 32))
|
||||
return 0;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user