bool bp_txout_match(const struct bp_txout *txout, const struct bp_keyset *ks) { if (!txout || !txout->scriptPubKey || !ks) return false; bool rc = false; struct bscript_addr addrs; if (!bsp_addr_parse(&addrs, txout->scriptPubKey->str, txout->scriptPubKey->len)) return false; struct const_buffer *buf; clist *tmp = addrs.pub; while (tmp) { buf = tmp->data; tmp = tmp->next; if (bpks_lookup(ks, buf->p, buf->len, false)) { rc = true; goto out; } } tmp = addrs.pubhash; while (tmp) { buf = tmp->data; tmp = tmp->next; if (bpks_lookup(ks, buf->p, buf->len, true)) { rc = true; goto out; } } out: clist_free_ext(addrs.pub, buffer_free); clist_free_ext(addrs.pubhash, buffer_free); return rc; }
static void runtest(void) { unsigned int i; struct bp_key keys[4]; /* generate keys */ for (i = 0; i < ARRAY_SIZE(keys); i++) { struct bp_key *key = &keys[i]; assert(bp_key_init(key) == true); assert(bp_key_generate(key) == true); } struct bp_keyset ks; bpks_init(&ks); /* add all but one to keyset */ for (i = 0; i < (ARRAY_SIZE(keys) - 1); i++) assert(bpks_add(&ks, &keys[i]) == true); /* verify all-but-one are in keyset */ for (i = 0; i < (ARRAY_SIZE(keys) - 1); i++) { unsigned char md160[RIPEMD160_DIGEST_LENGTH]; void *pubkey; size_t pklen; assert(bp_pubkey_get(&keys[i], &pubkey, &pklen) == true); bu_Hash160(md160, pubkey, pklen); assert(bpks_lookup(&ks, pubkey, pklen, true) == false); assert(bpks_lookup(&ks, pubkey, pklen, false) == true); assert(bpks_lookup(&ks, md160, sizeof(md160), true) == true); assert(bpks_lookup(&ks, md160, sizeof(md160), false) == false); free(pubkey); } /* verify last key not in keyset */ { unsigned char md160[RIPEMD160_DIGEST_LENGTH]; void *pubkey; size_t pklen; struct bp_key *key = &keys[ARRAY_SIZE(keys) - 1]; assert(bp_pubkey_get(key, &pubkey, &pklen) == true); bu_Hash160(md160, pubkey, pklen); assert(bpks_lookup(&ks, pubkey, pklen, true) == false); assert(bpks_lookup(&ks, pubkey, pklen, false) == false); assert(bpks_lookup(&ks, md160, sizeof(md160), true) == false); assert(bpks_lookup(&ks, md160, sizeof(md160), false) == false); free(pubkey); } bpks_free(&ks); for (i = 0; i < ARRAY_SIZE(keys); i++) { struct bp_key *key = &keys[i]; bp_key_free(key); } }