void btc_tx_serialize(cstring* s, const btc_tx* tx) { ser_s32(s, tx->version); ser_varlen(s, tx->vin ? tx->vin->len : 0); unsigned int i; if (tx->vin) { for (i = 0; i < tx->vin->len; i++) { btc_tx_in* tx_in; tx_in = vector_idx(tx->vin, i); btc_tx_in_serialize(s, tx_in); } } ser_varlen(s, tx->vout ? tx->vout->len : 0); if (tx->vout) { for (i = 0; i < tx->vout->len; i++) { btc_tx_out* tx_out; tx_out = vector_idx(tx->vout, i); btc_tx_out_serialize(s, tx_out); } } ser_u32(s, tx->locktime); }
void logdb_record_ser(logdb_record* rec, cstring *buf) { ser_bytes(buf, &rec->mode, 1); ser_varlen(buf, rec->key->len); ser_bytes(buf, rec->key->str, rec->key->len); /* write value for a WRITE operation */ if (rec->mode == RECORD_TYPE_WRITE) { ser_varlen(buf, rec->value->len); ser_bytes(buf, rec->value->str, rec->value->len); } }
cstring *ser_msg_vinv(const struct msg_vinv *mv) { cstring *s = cstr_new(NULL); if (!mv || !mv->invs || !mv->invs->len) { ser_varlen(s, 0); return s; } ser_varlen(s, mv->invs->len); unsigned int i; for (i = 0; i < mv->invs->len; i++) { struct bp_inv *inv; inv = parr_idx(mv->invs, i); ser_bp_inv(s, inv); } return s; }
cstring *ser_msg_headers(const struct msg_headers *mh) { cstring *s = cstr_new(NULL); if (!mh || !mh->headers || !mh->headers->len) { ser_varlen(s, 0); return s; } ser_varlen(s, mh->headers->len); unsigned int i; for (i = 0; i < mh->headers->len; i++) { struct bp_block *block; block = parr_idx(mh->headers, i); ser_bp_block(s, block); } return s; }
cstring *ser_msg_addr(unsigned int protover, const struct msg_addr *ma) { cstring *s = cstr_new(NULL); if (!ma || !ma->addrs || !ma->addrs->len) { ser_varlen(s, 0); return s; } ser_varlen(s, ma->addrs->len); unsigned int i; for (i = 0; i < ma->addrs->len; i++) { struct bp_address *addr; addr = parr_idx(ma->addrs, i); ser_bp_addr(s, protover, addr); } return s; }
static cstring *ser_wallet_root(const struct wallet *wlt) { cstring *rs = cstr_new_sz(8); ser_u32(rs, wlt->version); ser_bytes(rs, &wlt->chain->netmagic[0], 4); const uint32_t n_settings = 1; ser_varlen(rs, n_settings); ser_str(rs, "def_acct", 64); ser_varstr(rs, wlt->def_acct); return rs; }
void test_serialize() { char hex0[] = "28969cdfa74a12c82f3bad960b0b000aca2ac329deea5c2328ebc6f2ba9802c1"; char hex1[] = "28969cdfa74a12c82f3bad960b0b000aca2ac329deea5c2328ebc6f2ba9802c2"; char hex2[] = "28969cdfa74a12c82f3bad960b0b000aca2ac329deea5c2328ebc6f2ba9802c3"; uint8_t* hash0 = malloc(32); uint8_t* hash1 = malloc(32); uint8_t* hash2 = malloc(32); memcpy(hash0, utils_hex_to_uint8(hex0), 32); memcpy(hash1, utils_hex_to_uint8(hex1), 32); memcpy(hash2, utils_hex_to_uint8(hex2), 32); vector* vec = vector_new(5, free); vector_add(vec, hash0); vector_add(vec, hash1); vector_add(vec, hash2); cstring* s = cstr_new_sz(200); ser_u256_vector(s, vec); vector_free(vec, true); vector* vec2 = vector_new(0, NULL); struct const_buffer buf = {s->str, s->len}; deser_u256_vector(&vec2, &buf); vector_free(vec2, true); cstr_free(s, true); cstring* s2 = cstr_new_sz(200); ser_u16(s2, 0xAAFF); ser_u32(s2, 0xDDBBAAFF); ser_u64(s2, 0x99FF99FFDDBBAAFF); ser_varlen(s2, 10); ser_varlen(s2, 1000); ser_varlen(s2, 100000000); ser_str(s2, "test", 4); cstring* s3 = cstr_new("foo"); ser_varstr(s2, s3); cstr_free(s3, true); // ser_varlen(s2, (uint64_t)0x9999999999999999); // uint64 varlen is not supported right now struct const_buffer buf2 = {s2->str, s2->len}; uint16_t num0; deser_u16(&num0, &buf2); assert(num0 == 43775); //0xAAFF uint32_t num1; deser_u32(&num1, &buf2); assert(num1 == 3720063743); //0xDDBBAAFF uint64_t num2; deser_u64(&num2, &buf2); assert(num2 == 0x99FF99FFDDBBAAFF); //0x99FF99FFDDBBAAFF uint32_t num3; deser_varlen(&num3, &buf2); assert(num3 == 10); deser_varlen(&num3, &buf2); assert(num3 == 1000); deser_varlen(&num3, &buf2); assert(num3 == 100000000); char strbuf[255]; deser_str(strbuf, &buf2, 255); assert(strncmp(strbuf, "test", 4) == 0); cstring* deser_test = cstr_new_sz(0); deser_varstr(&deser_test, &buf2); assert(strncmp(deser_test->str, "foo", 3) == 0); cstr_free(deser_test, true); cstr_free(s2, true); }
void bp_tx_sigserializer(cstring *s, const cstring *scriptCode, const struct bp_tx *txTo, unsigned int nIn, int nHashType) { const bool fAnyoneCanPay = (!!(nHashType & SIGHASH_ANYONECANPAY)); const bool fHashSingle = ((nHashType & 0x1f) == SIGHASH_SINGLE); const bool fHashNone = ((nHashType & 0x1f) == SIGHASH_NONE); /** Serialize txTo */ // Serialize nVersion ser_u32(s, txTo->nVersion); // Serialize vin unsigned int nInputs = fAnyoneCanPay ? 1 : txTo->vin->len; ser_varlen(s, nInputs); unsigned int nInput; for (nInput = 0; nInput < nInputs; nInput++) { /** Serialize an input of txTo */ // In case of SIGHASH_ANYONECANPAY, only the input being signed is serialized if (fAnyoneCanPay) nInput = nIn; struct bp_txin *txin = parr_idx(txTo->vin, nInput); // Serialize the prevout ser_bp_outpt(s, &txin->prevout); // Serialize the script if (nInput != nIn) // Blank out other inputs' signatures ser_varlen(s, (int)0); else if (scriptCode == NULL) cstr_append_c(s, 0); else { /** Serialize the passed scriptCode, skipping OP_CODESEPARATORs */ struct const_buffer it = { scriptCode->str, scriptCode->len }; struct const_buffer itBegin = it; struct bscript_op op; unsigned int nCodeSeparators = 0; struct bscript_parser bp; bsp_start(&bp, &it); while (bsp_getop(&op, &bp)) { if (op.op == OP_CODESEPARATOR) nCodeSeparators++; } ser_varlen(s, scriptCode->len - nCodeSeparators); it = itBegin; bsp_start(&bp, &it); while (bsp_getop(&op, &bp)) { if (op.op == OP_CODESEPARATOR) { ser_bytes(s, itBegin.p, it.p - itBegin.p - 1); itBegin = it; } } if (itBegin.p != scriptCode->str + scriptCode->len) ser_bytes(s, itBegin.p, it.p - itBegin.p); } // Serialize the nSequence if ((nInput != nIn) && (fHashSingle || fHashNone)) // let the others update at will ser_u32(s, (int)0); else ser_u32(s, txin->nSequence); } // Serialize vout unsigned int nOutputs = fHashNone ? 0 : (fHashSingle ? (nIn + 1) : txTo->vout->len); ser_varlen(s, nOutputs); unsigned int nOutput; for (nOutput = 0; nOutput < nOutputs; nOutput++) { struct bp_txout *txout = parr_idx(txTo->vout, nOutput); if (fHashSingle && (nOutput != nIn)) // Do not lock-in the txout payee at other indices as txin; bp_txout_set_null(txout); ser_bp_txout(s, txout); } // Serialize nLockTime ser_u32(s, txTo->nLockTime); }