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