invoice.c (1477B)
1 2 #include "cursor.h" 3 #include "invoice.h" 4 #include "nostrdb.h" 5 #include "bolt11/bolt11.h" 6 #include "bolt11/amount.h" 7 8 int ndb_encode_invoice(struct cursor *cur, struct bolt11 *invoice) { 9 if (!invoice->description && !invoice->description_hash) 10 return 0; 11 12 if (!cursor_push_byte(cur, 1)) 13 return 0; 14 15 if (!cursor_push_varint(cur, invoice->msat == NULL ? 0 : invoice->msat->millisatoshis)) 16 return 0; 17 18 if (!cursor_push_varint(cur, invoice->timestamp)) 19 return 0; 20 21 if (!cursor_push_varint(cur, invoice->expiry)) 22 return 0; 23 24 if (invoice->description) { 25 if (!cursor_push_byte(cur, 1)) 26 return 0; 27 if (!cursor_push_c_str(cur, invoice->description)) 28 return 0; 29 } else { 30 if (!cursor_push_byte(cur, 2)) 31 return 0; 32 if (!cursor_push(cur, invoice->description_hash->u.u8, 32)) 33 return 0; 34 } 35 36 return 1; 37 } 38 39 int ndb_decode_invoice(struct cursor *cur, struct ndb_invoice *invoice) 40 { 41 unsigned char desc_type; 42 if (!cursor_pull_byte(cur, &invoice->version)) 43 return 0; 44 45 if (!cursor_pull_varint(cur, &invoice->amount)) 46 return 0; 47 48 if (!cursor_pull_varint(cur, &invoice->timestamp)) 49 return 0; 50 51 if (!cursor_pull_varint(cur, &invoice->expiry)) 52 return 0; 53 54 if (!cursor_pull_byte(cur, &desc_type)) 55 return 0; 56 57 if (desc_type == 1) { 58 if (!cursor_pull_c_str(cur, (const char**)&invoice->description)) 59 return 0; 60 } else if (desc_type == 2) { 61 invoice->description_hash = cur->p; 62 if (!cursor_skip(cur, 32)) 63 return 0; 64 } else { 65 return 0; 66 } 67 68 return 1; 69 }