Esempio n. 1
0
File: tx.c Progetto: afk11/libbtc
void btc_tx_in_serialize(cstring* s, const btc_tx_in* tx_in)
{
    ser_u256(s, tx_in->prevout.hash);
    ser_u32(s, tx_in->prevout.n);
    ser_varstr(s, tx_in->script_sig);
    ser_u32(s, tx_in->sequence);
}
Esempio n. 2
0
void ser_peer(cstring *s, unsigned int protover, const struct peer *peer)
{
	ser_bp_addr(s, protover, &peer->addr);

	ser_s64(s, peer->last_ok);
	ser_u32(s, peer->n_ok);

	ser_s64(s, peer->last_fail);
	ser_u32(s, peer->n_fail);
}
Esempio n. 3
0
 void put_constants() {
   ser_u32(constants.size());
   for (const auto &cnst : constants) {
     ser_type(cnst->type);
     switch (cnst->type) {
       case TC::NIL:
         break;
       case TC::BLN:
         ser_u8(cnst->u.as_bool);
         break;
       case TC::INT:
         ser_u64(cnst->u.as_int);
         break;
       case TC::FLT:
         ser_f64(cnst->u.as_float);
         break;
       case TC::STR:
         ser_str(*(cnst->u.as_str));
         break;
       case TC::SYM:
         ser_str(*(cnst->u.as_str));
         break;
     }
   }
 }
Esempio n. 4
0
File: tx.c Progetto: afk11/libbtc
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);
}
Esempio n. 5
0
cstring *ser_msg_version(const struct msg_version *mv)
{
	cstring *s = cstr_new_sz(256);

	ser_u32(s, mv->nVersion);
	ser_u64(s, mv->nServices);
	ser_s64(s, mv->nTime);

	ser_bp_addr(s, MIN_PROTO_VERSION, &mv->addrTo);
	ser_bp_addr(s, MIN_PROTO_VERSION, &mv->addrFrom);

	ser_u64(s, mv->nonce);
	ser_str(s, mv->strSubVer, sizeof(mv->strSubVer));
	ser_u32(s, mv->nStartingHeight);

	return s;
}
Esempio n. 6
0
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;
}
Esempio n. 7
0
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);
}
Esempio n. 8
0
static void ser_account(cstring *s, const struct wallet_account *acct)
{
	ser_varstr(s, acct->name);
	ser_u32(s, acct->acct_idx);
	ser_u32(s, acct->next_key_idx);
}
Esempio n. 9
0
 void ser_inst5(Instruction &n, u32 i) {
   ser_opcode(n.code);
   ser_u32(i);
 }
Esempio n. 10
0
 void ser_str(const str &v) {
   ser_u32(v.size());
   crc32(v);
   for (const auto &ch : v)
     put8(ch);
 }
Esempio n. 11
0
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);
}