kee

Offline IOU signer with QR as transport
git clone https://holbrook.no/src/kee
Info | Log | Files | Refs | README | LICENSE

commit 0d8f2be167e567d3238f48a9407c4344f5de1c70
parent 74793de06b885955b59f0b6a3f0e35ac47eefd35
Author: lash <dev@holbrook.no>
Date:   Thu, 28 Mar 2024 19:20:21 +0000

Replace reference with data copy from db to entry store

Diffstat:
Msrc/db.c | 4++--
Msrc/gtk/kee-entry-list.c | 2--
Msrc/gtk/kee-entry-store.c | 2--
Msrc/gtk/kee-entry.c | 12+++++++++++-
Msrc/gtk/kee-entry.h | 2+-
Mtestdata.py | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
6 files changed, 76 insertions(+), 32 deletions(-)

diff --git a/src/db.c b/src/db.c @@ -178,10 +178,10 @@ int db_next(struct db_ctx *ctx, enum DbKey pfx, char **key, size_t *key_len, cha return ERR_DB_NOMATCH; } - *key = (char*)ctx->k.mv_data; *key_len = ctx->k.mv_size; - *value = (char*)ctx->v.mv_data; + memcpy(*key, (char*)ctx->k.mv_data, *key_len); *value_len = ctx->v.mv_size; + memcpy(*value, (char*)ctx->v.mv_data, *value_len); return ERR_OK; diff --git a/src/gtk/kee-entry-list.c b/src/gtk/kee-entry-list.c @@ -21,8 +21,6 @@ static void kee_entry_handle_setup(KeeEntryList* o, GtkListItem *item) { } static void kee_entry_handle_bind(KeeEntryList *o, GtkListItem *item) { - GtkWidget *widget; - char *s; KeeEntry *go; g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "entry list bind"); diff --git a/src/gtk/kee-entry-store.c b/src/gtk/kee-entry-store.c @@ -52,7 +52,6 @@ static guint kee_entry_store_get_n_items(GListModel *list) { static gpointer kee_entry_store_get_item(GListModel *list, guint index) { KeeEntry *o; KeeEntryStore *store; - char s; o = g_object_new(KEE_TYPE_ENTRY, NULL); @@ -76,7 +75,6 @@ static void kee_entry_store_iface_init(GListModelInterface *ifc) { /// \todo always scans from 0, inefficient /// \todo enum lookup states static int kee_entry_store_seek(KeeEntryStore *o, int idx) { - int c; int r; int i; size_t key_len; diff --git a/src/gtk/kee-entry.c b/src/gtk/kee-entry.c @@ -20,10 +20,16 @@ struct _KeeEntry { long long timestamp; char mem[4096]; char *unit_of_account; + char decimals; }; G_DEFINE_TYPE(KeeEntry, kee_entry, GTK_TYPE_BOX); +/// \todo free reference to self from parent box necessary..? +static void kee_entry_dispose(GObject *o) { + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "disposing entry"); +} + static void kee_entry_finalize(GObject *o) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "tearing down entry"); //G_OBJECT_CLASS(kee_entry_parent_class)->finalize(o); @@ -32,6 +38,7 @@ static void kee_entry_finalize(GObject *o) { static void kee_entry_class_init(KeeEntryClass *kls) { GObjectClass *object_class = G_OBJECT_CLASS(kls); object_class->finalize = kee_entry_finalize; + object_class->dispose = kee_entry_dispose; } static void kee_entry_init(KeeEntry *o) { @@ -49,7 +56,6 @@ int kee_entry_load(KeeEntry *o, struct db_ctx *db, const char *id) { int kee_entry_deserialize(KeeEntry *o, const char *key, size_t key_len, const char *data, size_t data_len) { int r; struct kee_import im; - char out[4096]; size_t out_len; o->state = 1; @@ -59,6 +65,9 @@ int kee_entry_deserialize(KeeEntry *o, const char *key, size_t key_len, const ch r = import_read(&im, o->unit_of_account, out_len); *(o->unit_of_account + r) = 0; + out_len = 1; + r = import_read(&im, &o->decimals, out_len); + o->state = 0; return ERR_OK; @@ -72,6 +81,7 @@ void kee_entry_apply_list_item_widget(KeeEntry *o) { return; } + //widget = gtk_label_new(o->unit_of_account); widget = gtk_label_new(o->unit_of_account); gtk_box_append(GTK_BOX(o), widget); return; diff --git a/src/gtk/kee-entry.h b/src/gtk/kee-entry.h @@ -18,7 +18,7 @@ G_DECLARE_FINAL_TYPE(KeeEntry, kee_entry, KEE, ENTRY, GtkBox); int kee_entry_load(KeeEntry *o, struct db_ctx *db, const char *id); int kee_entry_deserialize(KeeEntry *o, const char *key, size_t key_len, const char *data, size_t data_len); -void kee_entry_apply_unit_of_account(KeeEntry *o); +void kee_entry_apply_list_item_widget(KeeEntry *o); G_END_DECLS diff --git a/testdata.py b/testdata.py @@ -11,12 +11,13 @@ import time import lmdb import varint -FLAGS_RESULT = 1 << 0 -FLAGS_FIRST = 1 << 1 -FLAGS_CREDIT_DELTA_NEGATIVE = 1 << 2 -FLAGS_COLLATERAL_DELTA_NEGATIVE = 1 << 2 -FLAGS_SIGNER_IS_GIVER = 1 << 6 -FLAGS_BODY_STRING = 1 << 7 +SIGNS_ALICE_CREDIT_DELTA_NEGATIVE = 1 << 0 +SIGNS_BOB_CREDIT_DELTA_NEGATIVE = 1 << 1 +SIGNS_ALICE_COLLATERAL_DELTA_NEGATIVE = 1 << 2 +SIGNS_BOB_COLLATERAL_DELTA_NEGATIVE = 1 << 3 + +FLAGS_SIGNER_IS_BOB = 1 << 0 + NOBODY = b'\x00' * 64 NOSIG = b'\x00' * 65 PFX_LEDGER_HEAD = b'\x01' @@ -47,11 +48,15 @@ def to_absflag(v): class LedgerHead: - def __init__(self, body=NOBODY): + def __init__(self, alice_key=None, bob_key=None, body=NOBODY): self.uoa = "USD" self.uoa_decimals = 2 - self.taker_pubkey_ref = b'\x00' * 64 - self.giver_pubkey_ref = b'\x00' * 64 + if alice_key == None: + alice_key = os.urandom(65) + self.alice_pubkey_ref = alice_key + if bob_key == None: + bob_key = os.urandom(65) + self.bob_pubkey_ref = bob_key self.body = body @@ -68,10 +73,10 @@ class LedgerHead: b = varint.encode(self.uoa_decimals) self.__serialize_add(b, w) - b = self.taker_pubkey_ref + b = self.alice_pubkey_ref self.__serialize_add(b, w) - b = self.giver_pubkey_ref + b = self.bob_pubkey_ref self.__serialize_add(b, w) b = self.body @@ -99,28 +104,38 @@ class LedgerEntry: def __init__(self, head, parent=None, body=NOBODY, signer=None): self.head = head self.flags = 0 + self.signs = 0 self.parent = parent - if self.parent != None: - self.flags |= FLAGS_FIRST - else: - self.parent = head + if self.parent == None: + self.parent = b'\x00' * 64 self.timestamp = time.time_ns() self.body = body v = random.randint(self.credit_delta_min, self.credit_delta_max) + self.flags = v % 2 (v, neg) = to_absflag(v) self.credit_delta = v if neg: - self.flags |= FLAGS_CREDIT_DELTA_NEGATIVE + if self.flags: + self.signs |= SIGNS_BOB_CREDIT_DELTA_NEGATIVE + else: + self.signs |= SIGNS_ALICE_CREDIT_DELTA_NEGATIVE v = random.randint(self.collateral_delta_min, self.collateral_delta_max) + self.response_value = v % 2 (v, neg) = to_absflag(v) self.collateral_delta = v if neg: - self.flags |= FLAGS_COLLATERAL_DELTA_NEGATIVE - - self.signature = NOSIG + if self.flags: + self.signs |= SIGNS_BOB_COLLATERAL_DELTA_NEGATIVE + else: + self.signs |= SIGNS_ALICE_COLLATERAL_DELTA_NEGATIVE + + #self.request_signature = NOSIG + #self.response_signature = NOSIG + self.request_signature = os.urandom(65) + self.response_signature = os.urandom(65) self.signer = signer @@ -140,21 +155,44 @@ class LedgerEntry: b = self.timestamp.to_bytes(8, byteorder='big') self.__serialize_add(b, w) + b = self.signs.to_bytes(1) + self.__serialize_add(b, w) + realvalue = self.credit_delta - if (self.flags & FLAGS_CREDIT_DELTA_NEGATIVE): - realvalue *= -1 + if self.flags & FLAGS_SIGNER_IS_BOB: + if (self.signs & SIGNS_BOB_CREDIT_DELTA_NEGATIVE): + realvalue *= -1 + else: + if (self.signs & SIGNS_ALICE_CREDIT_DELTA_NEGATIVE): + realvalue *= -1 + logg.debug('encoding credit delta {}'.format(realvalue)) b = varint.encode(self.credit_delta) + if self.flags: + self.__serialize_add(b'\x00', w) self.__serialize_add(b, w) + if not self.flags: + self.__serialize_add(b'\x00', w) logg.debug('encoding collateral delta {}'.format(self.collateral_delta)) b = varint.encode(self.collateral_delta) self.__serialize_add(b, w) + if self.flags: + self.__serialize_add(b'\x00', w) self.__serialize_add(self.body, w) + if not self.flags: + self.__serialize_add(b'\x00', w) + + #if self.signer != None: + # self.signature = self.signer(b) - if self.signer != None: - self.signature = self.signer(b) + self.__serialize_add(self.request_signature, w) + + b = self.response_value.to_bytes(1) + self.__serialize_add(b, w) + + self.__serialize_add(self.response_signature, w) return b @@ -165,7 +203,7 @@ class LedgerEntry: r += PFX_LEDGER_ENTRY r += k ts = v[65:65+8] - logg.debug('ts {}: of {}'.format(ts.hex(), v.hex())) + #logg.debug('ts {}: of {}'.format(ts.hex(), v.hex())) r += ts return r