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
@@ -2,7 +2,7 @@ CFLAGS = -Wall -Wno-misleading-indentation -Wno-unused-function -Werror -O2 -g -
|
|||||||
HEADERS = deps/lmdb/lmdb.h deps/secp256k1/include/secp256k1.h src/sha256.h src/nostrdb.h src/cursor.h src/hex.h src/jsmn.h src/config.h src/sha256.h src/random.h src/memchr.h src/cpu.h $(C_BINDINGS)
|
HEADERS = deps/lmdb/lmdb.h deps/secp256k1/include/secp256k1.h src/sha256.h src/nostrdb.h src/cursor.h src/hex.h src/jsmn.h src/config.h src/sha256.h src/random.h src/memchr.h src/cpu.h $(C_BINDINGS)
|
||||||
FLATCC_SRCS=deps/flatcc/src/runtime/json_parser.c deps/flatcc/src/runtime/verifier.c deps/flatcc/src/runtime/builder.c deps/flatcc/src/runtime/emitter.c deps/flatcc/src/runtime/refmap.c
|
FLATCC_SRCS=deps/flatcc/src/runtime/json_parser.c deps/flatcc/src/runtime/verifier.c deps/flatcc/src/runtime/builder.c deps/flatcc/src/runtime/emitter.c deps/flatcc/src/runtime/refmap.c
|
||||||
BOLT11_SRCS = src/bolt11/bolt11.c src/bolt11/bech32.c src/bolt11/tal.c src/bolt11/talstr.c src/bolt11/take.c src/bolt11/list.c src/bolt11/utf8.c src/bolt11/amount.c src/bolt11/hash_u5.c
|
BOLT11_SRCS = src/bolt11/bolt11.c src/bolt11/bech32.c src/bolt11/tal.c src/bolt11/talstr.c src/bolt11/take.c src/bolt11/list.c src/bolt11/utf8.c src/bolt11/amount.c src/bolt11/hash_u5.c
|
||||||
SRCS = src/nostrdb.c src/sha256.c $(BOLT11_SRCS) $(FLATCC_SRCS)
|
SRCS = src/nostrdb.c src/sha256.c src/invoice.c $(BOLT11_SRCS) $(FLATCC_SRCS)
|
||||||
LDS = $(OBJS) $(ARS)
|
LDS = $(OBJS) $(ARS)
|
||||||
OBJS = $(SRCS:.c=.o)
|
OBJS = $(SRCS:.c=.o)
|
||||||
DEPS = $(OBJS) $(HEADERS) $(ARS)
|
DEPS = $(OBJS) $(HEADERS) $(ARS)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "short_types.h"
|
#include "short_types.h"
|
||||||
#include "hash_u5.h"
|
#include "hash_u5.h"
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
#include "amount.h"
|
||||||
#include "node_id.h"
|
#include "node_id.h"
|
||||||
//#include <secp256k1_recovery.h>
|
//#include <secp256k1_recovery.h>
|
||||||
|
|
||||||
|
|||||||
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;
|
||||||
|
}
|
||||||
20
nostrdb/src/invoice.h
Normal file
20
nostrdb/src/invoice.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
#ifndef NDB_INVOICE_H
|
||||||
|
#define NDB_INVOICE_H
|
||||||
|
|
||||||
|
struct bolt11;
|
||||||
|
|
||||||
|
struct ndb_invoice {
|
||||||
|
unsigned char version;
|
||||||
|
uint64_t amount;
|
||||||
|
uint64_t timestamp;
|
||||||
|
uint64_t expiry;
|
||||||
|
char *description;
|
||||||
|
unsigned char *description_hash;
|
||||||
|
};
|
||||||
|
|
||||||
|
// ENCODING
|
||||||
|
int ndb_encode_invoice(struct cursor *cur, struct bolt11 *invoice);
|
||||||
|
int ndb_decode_invoice(struct cursor *cur, struct ndb_invoice *invoice);
|
||||||
|
|
||||||
|
#endif /* NDB_INVOICE_H */
|
||||||
Reference in New Issue
Block a user