| 281 | } |
| 282 | |
| 283 | struct accounts *init_accounts(const tal_t *ctx, struct command *init_cmd) |
| 284 | { |
| 285 | struct json_out *params = json_out_new(tmpctx); |
| 286 | const jsmntok_t *result; |
| 287 | const char *buf; |
| 288 | const jsmntok_t *datastore, *t; |
| 289 | size_t i; |
| 290 | struct accounts *accounts = tal(ctx, struct accounts); |
| 291 | |
| 292 | accounts->htable = tal(accounts, struct account_htable); |
| 293 | account_htable_init(accounts->htable); |
| 294 | memleak_add_helper(accounts->htable, memleak_scan_accounts_htable); |
| 295 | |
| 296 | json_out_start(params, NULL, '{'); |
| 297 | json_out_start(params, "key", '['); |
| 298 | json_out_addstr(params, NULL, "bookkeeper"); |
| 299 | json_out_addstr(params, NULL, "account"); |
| 300 | json_out_end(params, ']'); |
| 301 | json_out_end(params, '}'); |
| 302 | result = jsonrpc_request_sync(tmpctx, init_cmd, |
| 303 | "listdatastore", |
| 304 | params, &buf); |
| 305 | |
| 306 | datastore = json_get_member(buf, result, "datastore"); |
| 307 | json_for_each_arr(i, t, datastore) { |
| 308 | size_t datalen; |
| 309 | const jsmntok_t *key, *datatok; |
| 310 | const u8 *data; |
| 311 | |
| 312 | /* Key is an array, first two elements are bookkeeper, account */ |
| 313 | key = json_get_member(buf, t, "key") + 3; |
| 314 | datatok = json_get_member(buf, t, "hex"); |
| 315 | /* In case someone creates a subdir? */ |
| 316 | if (!datatok) |
| 317 | continue; |
| 318 | |
| 319 | data = json_tok_bin_from_hex(tmpctx, buf, datatok); |
| 320 | datalen = tal_bytelen(data); |
| 321 | |
| 322 | if (fromwire_account(accounts, &data, &datalen) == NULL) { |
| 323 | plugin_err(init_cmd->plugin, |
| 324 | "Invalid account %.*s in datastore", |
| 325 | json_tok_full_len(key), |
| 326 | json_tok_full(buf, key)); |
| 327 | } |
| 328 | } |
| 329 | return accounts; |
| 330 | } |
no test coverage detected