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...
This commit is contained in:
Christian Decker
2017-07-20 14:58:20 +02:00
committed by Rusty Russell
parent de822b7ea0
commit 5912c68185
3 changed files with 18 additions and 3 deletions

View File

@@ -3,6 +3,7 @@
#include "daemon/log.h"
#include "lightningd/lightningd.h"
#include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h>
#include <ccan/tal/tal.h>
#include <inttypes.h>
@@ -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);
}

View File

@@ -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 */

View File

@@ -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);