static void test_txout(const struct bitc_txout *txout) { struct const_buffer buf = { txout->scriptPubKey->str, txout->scriptPubKey->len }; struct bscript_parser bsp; struct bscript_op op; clist *ops = NULL; /* * parse script */ bsp_start(&bsp, &buf); while (bsp_getop(&op, &bsp)) { struct bscript_op *op_p; op_p = memdup(&op, sizeof(op)); ops = clist_append(ops, op_p); } assert(!bsp.error); /* * build script */ clist *tmp = ops; cstring *s = cstr_new_sz(256); while (tmp) { struct bscript_op *op_p; op_p = tmp->data; tmp = tmp->next; if (is_bsp_pushdata(op_p->op)) { bsp_push_data(s, op_p->data.p, op_p->data.len); } else { bsp_push_op(s, op_p->op); } } clist_free_ext(ops, free); /* byte-compare original and newly created scripts */ assert(cstr_equal(s, txout->scriptPubKey)); cstr_free(s, true); }
bool bkeys_pubkey_append(struct bp_keystore *ks, const bu160_t *key_id, GString *scriptSig) { struct bp_key key; bp_key_init(&key); if (!bkeys_key_get(ks, key_id, &key)) return false; void *pubkey = NULL; size_t pk_len = 0; if (!bp_pubkey_get(&key, &pubkey, &pk_len)) return false; bsp_push_data(scriptSig, pubkey, pk_len); free(pubkey); /* no bp_key_free(&key), as bkeys_key_get() returns a ref */ return true; }
static void string_find_del(cstring *s, const struct buffer *buf) { /* wrap buffer in a script */ cstring *script = cstr_new_sz(buf->len + 8); bsp_push_data(script, buf->p, buf->len); /* search for script, as a substring of 's' */ void *p; unsigned int sublen = script->len; while ((p = memmem(s->str, s->len, script->str, sublen)) != NULL) { void *begin = s->str; void *end = begin + s->len; void *tail = p + sublen; unsigned int tail_len = end - tail; unsigned int new_len = s->len - sublen; memmove(p, tail, tail_len); cstr_resize(s, new_len); } cstr_free(script, true); }
GString *parse_script_str(const char *enc) { char **tokens = g_strsplit_set(enc, " \t\n", 0); assert (tokens != NULL); GString *script = g_string_sized_new(64); unsigned int idx; for (idx = 0; tokens[idx] != NULL; idx++) { char *token = tokens[idx]; if (is_digitstr(token)) { int64_t v = strtoll(token, NULL, 10); bsp_push_int64(script, v); } else if (is_hexstr(token, true)) { GString *raw = hex2str(token); g_string_append_len(script, raw->str, raw->len); g_string_free(raw, TRUE); } else if ((strlen(token) >= 2) && (token[0] == '\'') && (token[strlen(token) - 1] == '\'')) bsp_push_data(script, &token[1], strlen(token) - 2); else if (GetOpType(token) != OP_INVALIDOPCODE) bsp_push_op(script, GetOpType(token)); else assert(!"parse error"); } g_strfreev(tokens); return script; }
static bool sign1(const bu160_t *key_id, struct bp_keystore *ks, const bu256_t *hash, int nHashType, cstring *scriptSig) { struct bp_key key; bool rc = false; bp_key_init(&key); /* find private key in keystore */ if (!bkeys_key_get(ks, key_id, &key)) goto out; void *sig = NULL; size_t siglen = 0; /* sign hash with private key */ if (!bp_sign(&key, hash, sizeof(*hash), &sig, &siglen)) goto out; /* append nHashType to signature */ unsigned char ch = (unsigned char) nHashType; sig = realloc(sig, siglen + 1); memcpy(sig + siglen, &ch, 1); siglen++; /* append signature to scriptSig */ bsp_push_data(scriptSig, sig, siglen); free(sig); rc = true; out: bp_key_free(&key); return rc; }