kee

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

commit 442616fe98b68a984fd50d818f7fbc317359123e
parent 342ef1a01e0412f794b69202abdc008380b31801
Author: lash <dev@holbrook.no>
Date:   Sun, 16 Jun 2024 03:59:01 +0100

Add mode param to ledger open serialize, start sprint ledger cli

Diffstat:
Msrc/cmd/ledger.c | 16++++++++++++++--
Msrc/gtk/kee-entry.c | 4++--
Msrc/ledger.c | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Msrc/ledger.h | 3++-
4 files changed, 89 insertions(+), 7 deletions(-)

diff --git a/src/cmd/ledger.c b/src/cmd/ledger.c @@ -1,6 +1,7 @@ #include <stdio.h> #include <fcntl.h> #include <unistd.h> +#include <stddef.h> #include "ledger.h" #include "debug.h" @@ -29,7 +30,7 @@ int act_sign(struct kee_cli_t *cli, struct kee_ledger_t *ledger, char *buf) { } c = KEE_CLI_BUFMAX; - r = kee_ledger_serialize_open(ledger, buf, &c); + r = kee_ledger_serialize_open(ledger, buf, &c, KEE_LEDGER_STATE_FINAL); if (r) { debug_logerr(LLOG_CRITICAL, ERR_FAIL, "cannot serialize ledger"); return ERR_FAIL; @@ -46,7 +47,17 @@ int act_sign(struct kee_cli_t *cli, struct kee_ledger_t *ledger, char *buf) { return ERR_OK; } +/// \todo buf (and length) should be in internal cli struct int act_print(struct kee_cli_t *cli, struct kee_ledger_t *ledger, char *buf) { + int r; + size_t c; + + c = KEE_CLI_BUFMAX; + r = kee_ledger_sprint(ledger, buf, &c, 1); + + cli->result = buf; + cli->result_len = r; + return ERR_OK; } @@ -100,7 +111,7 @@ int main(int argc, char **argv) { close(f); c = KEE_CLI_BUFMAX - l; - sprintf(dbg, "Read %lu bytes from %s", c, *(argv+1)); + sprintf(dbg, "Read %lu bytes from %s", c, *(cli.posarg)); debug_log(DEBUG_INFO, dbg); r = cli_decode(&cli, b, &c); @@ -129,6 +140,7 @@ int main(int argc, char **argv) { if (r) { debug_logerr(LLOG_CRITICAL, ERR_FAIL, "print command fail"); } + break; default: debug_logerr(LLOG_CRITICAL, ERR_FAIL, "invalid command"); r = ERR_FAIL; diff --git a/src/gtk/kee-entry.c b/src/gtk/kee-entry.c @@ -113,7 +113,7 @@ static void kee_entry_handle_confirm(GtkButton *butt, KeeEntry *o) { return; } - r = kee_ledger_serialize_open(&o->ledger, out, &out_len); + r = kee_ledger_serialize_open(&o->ledger, out, &out_len, KEE_LEDGER_STATE_RESPONSE); if (r) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "qr transport renderer failed"); return; @@ -216,7 +216,7 @@ static void kee_entry_handle_add(GtkButton *butt, KeeEntry *o) { return; } - r = kee_ledger_serialize_open(&o->ledger, out, &out_len); + r = kee_ledger_serialize_open(&o->ledger, out, &out_len, KEE_LEDGER_STATE_RESPONSE); if (r) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, "qr transport renderer failed"); return; diff --git a/src/ledger.c b/src/ledger.c @@ -4,6 +4,7 @@ #include <gcrypt.h> #include <rerr.h> +#include <hex.h> #include "ledger.h" #include "cadiz.h" @@ -861,7 +862,7 @@ int kee_ledger_sign(struct kee_ledger_t *ledger, struct kee_ledger_item_t *item, return ERR_OK; } -int kee_ledger_serialize_open(struct kee_ledger_t *ledger, char *out, size_t *out_len) { +int kee_ledger_serialize_open(struct kee_ledger_t *ledger, char *out, size_t *out_len, enum kee_ledger_state_e mode) { int r; char err[1024]; char b[1024]; @@ -890,7 +891,7 @@ int kee_ledger_serialize_open(struct kee_ledger_t *ledger, char *out, size_t *ou } c = 1024; - r = kee_ledger_item_serialize(ledger->last_item, b, &c, KEE_LEDGER_STATE_RESPONSE); + r = kee_ledger_item_serialize(ledger->last_item, b, &c, mode); if (r) { return ERR_FAIL; } @@ -1295,6 +1296,73 @@ int kee_ledger_verify(struct kee_ledger_t *ledger, int *idx) { return ERR_OK; } +void kee_ledger_item_sprint(struct kee_ledger_item_t *item, char *out, size_t *char_len) { + char *p; + + p = out; + + p = stpcpy(p, "sig us\t"); + if (memcmp(item->alice_signature, zero_content, SIGNATURE_LENGTH)) { + b2h((unsigned char*)item->alice_signature, SIGNATURE_LENGTH, (unsigned char*)p); + p += SIGNATURE_LENGTH * 2; + } else { + p = stpcpy(p, "(none)"); + } + *p = 0x0a; + p++; + + p = stpcpy(p, "sig them\t"); + if (memcmp(item->bob_signature, zero_content, SIGNATURE_LENGTH)) { + b2h((unsigned char*)item->bob_signature, SIGNATURE_LENGTH, (unsigned char*)p); + p += SIGNATURE_LENGTH * 2; + } else { + p = stpcpy(p, "(none)"); + } + *p = 0x0a; + p++; + + *char_len = (int)(p - out); +} + +int kee_ledger_sprint(struct kee_ledger_t *ledger, char *out, size_t *char_len, int items) { + struct kee_ledger_item_t *item; + int i; + size_t c; + char *p; + + p = out; + + p = stpcpy(p, "taker\t"); + b2h((unsigned char*)ledger->pubkey_alice, PUBKEY_LENGTH, (unsigned char*)p); + p += PUBKEY_LENGTH * 2; + *p = 0x0a; + p++; + + p = stpcpy(p, "giver\t"); + b2h((unsigned char*)ledger->pubkey_bob, PUBKEY_LENGTH, (unsigned char*)p); + p += PUBKEY_LENGTH * 2; + *p = 0x0a; + p++; + + i = 0; + *char_len -= (int)(p - out); + item = ledger->last_item; + while (item != NULL) { + if (i == items) { + break; + } + c = *char_len; + kee_ledger_item_sprint(item, p, &c); + item = item->prev_item; + *char_len -= c; + p += c; + i++; + } + + *p = 0x0; + + return (int)((p + 1) - out); +} /// \todo zero initiator need detect @@ -1340,3 +1408,4 @@ enum kee_initiator_e kee_ledger_item_initiator(struct kee_ledger_t *ledger, stru } return initiator; } + diff --git a/src/ledger.h b/src/ledger.h @@ -65,7 +65,7 @@ void kee_ledger_free(struct kee_ledger_t *ledger); void kee_ledger_resolve(struct kee_ledger_t *ledger, Cadiz *cadiz); void kee_ledger_reset_cache(struct kee_ledger_t *ledger); int kee_ledger_sign(struct kee_ledger_t *ledger, struct kee_ledger_item_t *item, struct gpg_store *gpg, const char *passphrase); -int kee_ledger_serialize_open(struct kee_ledger_t *ledger, char *out, size_t *out_len); +int kee_ledger_serialize_open(struct kee_ledger_t *ledger, char *out, size_t *out_len, enum kee_ledger_state_e mode); int kee_ledger_parse_open(struct kee_ledger_t *ledger, struct gpg_store *gpg, const char *in, size_t in_len); int kee_ledger_put(struct kee_ledger_t *ledger, struct db_ctx *db); int kee_ledger_item_put(struct kee_ledger_t *ledger, struct db_ctx *db, int idx); @@ -77,6 +77,7 @@ struct kee_ledger_item_t *kee_ledger_add_item(struct kee_ledger_t *ledger); void kee_ledger_item_init(struct kee_ledger_item_t *item); int kee_ledger_item_serialize(struct kee_ledger_item_t *item, char *out, size_t *out_len, enum kee_ledger_state_e mode); void kee_ledger_item_free(struct kee_ledger_item_t *item); +int kee_ledger_sprint(struct kee_ledger_t *ledger, char *out, size_t *out_len, int items); enum kee_ledger_state_e kee_ledger_item_state(struct kee_ledger_item_t *item); enum kee_initiator_e kee_ledger_item_initiator(struct kee_ledger_t *ledger, struct gpg_store *gpg, struct kee_ledger_item_t *item);