Re: [PATCH damus 1/7] Implement Codable for NdbNote

1 view
Skip to first unread message

William Casarin

unread,
May 6, 2025, 11:30:54 AM5/6/25
to Daniel D’Aquino, pat...@damus.io
On Wed, Apr 30, 2025 at 04:26:47PM -0700, Daniel D’Aquino wrote:
>Makes it easier to work with other Swift types
>
>Signed-off-by: Daniel D’Aquino <dan...@daquino.me>

modifying this file only makes it harder to backport this to nostrdb
until we have the update branch merged.

I'm not entirely sure we need all this code duplication just to verify a
note during finalization. Can't we just verify after finalization? We
just need to provide the

note = ndb_builder_finalize()
id = ndb_calculate_id(note)
ndb_note_verify(note->pubkey, id, note->sig)
}

>---
>+/// Finalizes the ndb note builder, assuming the signature and pubkey have been manually set, and verifies if the signature is valid for that computed ID.
>+///
>+/// This is useful when manually decoding an ndb note from another user, and verification of both id and signature is needed
>+int ndb_builder_finalize_verify(struct ndb_builder *builder, struct ndb_note **note)
>+{
>+ secp256k1_context *ctx;
>+
>+ int strings_len = builder->strings.p - builder->strings.start;
>+ unsigned char *note_end = builder->note_cur.p + strings_len;
>+ int total_size = note_end - builder->note_cur.start;
>+
>+ // move the strings buffer next to the end of our ndb_note
>+ memmove(builder->note_cur.p, builder->strings.start, strings_len);
>+
>+ // set the strings location
>+ builder->note->strings = builder->note_cur.p - builder->note_cur.start;
>+
>+ // record the total size
>+ //builder->note->size = total_size;
>+
>+ *note = builder->note;
>+
>+ // use the remaining memory for building our id buffer
>+ unsigned char *end = builder->mem.end;
>+ unsigned char *start = (unsigned char*)(*note) + total_size;
>+
>+ if (!ndb_calculate_id(builder->note, start, end - start))
>+ return 0;
>+
>+ // verify! If it's an invalid note we don't need to
>+ // bother finalizing it
>+ ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
>+ if (!ndb_note_verify(ctx, builder->note->pubkey, builder->note->id, builder->note->sig)) {
>+ ndb_debug("note verification failed\n");
>+ return 0;
>+ }
>+
>+ // make sure we're aligned as a whole
>+ total_size = (total_size + 7) & ~7;
>+ assert((total_size % 8) == 0);
>+ return total_size;
>+}
>+
> struct ndb_note * ndb_builder_note(struct ndb_builder *builder)
> {
> return builder->note;
>diff --git a/nostrdb/nostrdb.h b/nostrdb/nostrdb.h
>index 483d3edc1..65d34c8f2 100644
>--- a/nostrdb/nostrdb.h
>+++ b/nostrdb/nostrdb.h
>@@ -366,6 +366,7 @@ int ndb_ws_event_from_json(const char *json, int len, struct ndb_tce *tce, unsig
> int ndb_note_from_json(const char *json, int len, struct ndb_note **, unsigned char *buf, int buflen);
> int ndb_builder_init(struct ndb_builder *builder, unsigned char *buf, int bufsize);
> int ndb_builder_finalize(struct ndb_builder *builder, struct ndb_note **note, struct ndb_keypair *privkey);
>+int ndb_builder_finalize_verify(struct ndb_builder *builder, struct ndb_note **note);
> int ndb_builder_set_content(struct ndb_builder *builder, const char *content, int len);
> void ndb_builder_set_created_at(struct ndb_builder *builder, uint64_t created_at);
> void ndb_builder_set_sig(struct ndb_builder *builder, unsigned char *sig);
Reply all
Reply to author
Forward
0 new messages