kee

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

commit 7c3e4a4808d5e7bde91b86dfe033cbe0af16e138
parent 6544c99372def1beb6f523e05fbfaf233d71a9a8
Author: lash <dev@holbrook.no>
Date:   Thu, 16 May 2024 14:38:40 +0100

Change timestamp to octet string, 32 bit compat

Diffstat:
Msrc/asn1/schema_entry.txt | 2+-
Msrc/ledger.c | 45+++++++++++++++++++++++++--------------------
Mtestdata_asn1.py | 14+++++++++++---
3 files changed, 37 insertions(+), 24 deletions(-)

diff --git a/src/asn1/schema_entry.txt b/src/asn1/schema_entry.txt @@ -16,7 +16,7 @@ Kee DEFINITIONS EXPLICIT TAGS ::= BEGIN --- flags KeeEntryFlags, parent OCTET STRING, --- timestamp GeneralizedTime, - timestamp INTEGER, + timestamp OCTET STRING, creditDelta INTEGER, collateralDelta INTEGER, body OCTET STRING, diff --git a/src/ledger.c b/src/ledger.c @@ -427,31 +427,25 @@ struct kee_ledger_item_t *kee_ledger_parse_item(struct kee_ledger_t *ledger, con cur->response = 1; } - /// \todo apply timestamp in structure /// \todo document timestamp size c = 8; r = asn1_read_value(item, "timestamp", tmp, &c); if (r != ASN1_SUCCESS) { return NULL; } + if (is_le()) { + flip_endian(4, (char*)tmp); + flip_endian(4, ((char*)tmp)+4); + } + memcpy(&cur->time.tv_sec, tmp, 4); + memcpy(&cur->time.tv_nsec, ((char*)tmp)+4, 4); c = 64; r = asn1_read_value(item, "body", tmp, &c); if (r != ASN1_SUCCESS) { return NULL; } - if (is_le()) { - r = to_endian(TO_ENDIAN_LITTLE, 4, tmp); - if (r) { - return NULL; - } - r = to_endian(TO_ENDIAN_LITTLE, 4, tmp+4); - if (r) { - return NULL; - } - } - memcpy(&cur->time.tv_sec, tmp, 4); - memcpy(&cur->time.tv_nsec, tmp+4, 4); + r = kee_content_init(&(cur->content), tmp, 0); if (r) { @@ -645,8 +639,8 @@ int kee_ledger_item_serialize(struct kee_ledger_item_t *item, char *out, size_t int r; char err[1024]; asn1_node node; -// char timedata[8]; - long long nanotime; + char timedata[8]; +// long long nanotime; int c; int credit_delta; int collateral_delta; @@ -672,15 +666,26 @@ int kee_ledger_item_serialize(struct kee_ledger_item_t *item, char *out, size_t return r; } - nanotime = item->time.tv_sec * 1000000000; - nanotime += item->time.tv_nsec; - r = to_endian(TO_ENDIAN_BIG, 8, &nanotime); + //nanotime = item->time.tv_sec * 1000000000; + //nanotime += item->time.tv_nsec; + //r = to_endian(TO_ENDIAN_BIG, 8, &nanotime); + //if (r) { + // return 1; + //} + memcpy(timedata, &item->time.tv_sec, 4); + memcpy(((char*)timedata)+4, &item->time.tv_nsec, 4); + r = to_endian(TO_ENDIAN_BIG, 4, timedata); if (r) { - return 1; + return ERR_FAIL; + } + r = to_endian(TO_ENDIAN_BIG, 4, ((char*)timedata)+4); + if (r) { + return ERR_FAIL; } c = 8; - r = asn1_write_value(node, "Kee.KeeEntry.timestamp", &nanotime, c); + //r = asn1_write_value(node, "Kee.KeeEntry.timestamp", &nanotime, c); + r = asn1_write_value(node, "Kee.KeeEntry.timestamp", timedata, c); if (r != ASN1_SUCCESS) { return r; } diff --git a/testdata_asn1.py b/testdata_asn1.py @@ -22,6 +22,7 @@ from faker.providers import lorem import varint from pyasn1.codec.der.encoder import encode as der_encode from pyasn1.codec.der.decoder import decode as der_decode +from pyasn1.codec.native.decoder import decode from pyasn1.type.univ import Any from pygcrypt.gctypes.sexpression import SExpression from pygcrypt.gctypes.key import Key as GKey @@ -424,7 +425,13 @@ class LedgerItem(Ledger): self.parent = parent if self.parent == None: self.parent = b'\x00' * 64 - self.timestamp = time.time_ns() + #self.timestamp = time.time_ns() + self.timestamp = b'' + t = time.time_ns() + v = int(t / 1000000000) + self.timestamp += v.to_bytes(4, byteorder='big') + v = t - (v * 1000000000) + self.timestamp += v.to_bytes(4, byteorder='big') self.body = LedgerItemContent() @@ -502,8 +509,9 @@ class LedgerItem(Ledger): r += k o = der_decode(v, asn1Spec=KeeEntry()) - ts = o[0]['timestamp'] - tsb = int(ts).to_bytes(8, byteorder='big') + #ts = o[0]['timestamp'] + #tsb = int(ts).to_bytes(8, byteorder='big') + tsb = o[0]['timestamp'].asOctets() #logg.debug('ts {} ({}): of {}'.format(ts, tsb, v.hex())) r += tsb return r