From 5912c68185446c57a33ffdf16922ccaf5bd5e21d Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 20 Jul 2017 14:58:20 +0200 Subject: [PATCH] db: Add a hexval helper to decode hex values from db into fields This is going to be handy pretty soon. Channels are almost fully blobs... --- wallet/db.c | 10 ++++++++++ wallet/db.h | 6 ++++++ wallet/wallet.c | 5 ++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/wallet/db.c b/wallet/db.c index 754d973a1..3c7014872 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -3,6 +3,7 @@ #include "daemon/log.h" #include "lightningd/lightningd.h" +#include #include #include #include @@ -329,3 +330,12 @@ bool db_set_intvar(struct db *db, char *varname, s64 val) "');", varname, val); } + +bool sqlite3_column_hexval(sqlite3_stmt *s, int col, void *dest, size_t destlen) +{ + const char *source = sqlite3_column_blob(s, col); + size_t sourcelen = sqlite3_column_bytes(s, col); + if (sourcelen / 2 != destlen) + return false; + return hex_decode(source, sourcelen, dest, destlen); +} diff --git a/wallet/db.h b/wallet/db.h index 3e39efe97..455f2a698 100644 --- a/wallet/db.h +++ b/wallet/db.h @@ -73,4 +73,10 @@ bool db_set_intvar(struct db *db, char *varname, s64 val); */ s64 db_get_intvar(struct db *db, char *varname, s64 defval); +/** + * sqlite3_column_hexval - Helper to populate a binary field from a hex value + */ +bool sqlite3_column_hexval(sqlite3_stmt *s, int col, void *dest, + size_t destlen); + #endif /* WALLET_DB_H */ diff --git a/wallet/wallet.c b/wallet/wallet.c index d4f64e56a..918e2000f 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -335,9 +335,8 @@ bool wallet_shachain_load(struct wallet *wallet, u64 id, while (sqlite3_step(stmt) == SQLITE_ROW) { int pos = sqlite3_column_int(stmt, 2); chain->chain.known[pos].index = sqlite3_column_int64(stmt, 0); - hex_decode( - sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1), - &chain->chain.known[pos].hash, sizeof(struct sha256)); + sqlite3_column_hexval(stmt, 1, &chain->chain.known[pos].hash, + sizeof(struct sha256)); } sqlite3_finalize(stmt);