libbtc
bitcoinclibrary
Functions
tx.c File Reference
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "btc/base58.h"
#include "btc/tx.h"
#include "serialize.h"
#include "sha2.h"
#include "utils.h"

Go to the source code of this file.

Functions

void btc_tx_in_free (btc_tx_in *tx_in)
 
void btc_tx_in_free_cb (void *data)
 
btc_tx_inbtc_tx_in_new ()
 create a new tx input More...
 
void btc_tx_out_free (btc_tx_out *tx_out)
 
void btc_tx_out_free_cb (void *data)
 
btc_tx_outbtc_tx_out_new ()
 create a new tx output More...
 
void btc_tx_free (btc_tx *tx)
 
btc_txbtc_tx_new ()
 create a new tx input More...
 
btc_bool btc_tx_in_deserialize (btc_tx_in *tx_in, struct const_buffer *buf)
 
btc_bool btc_tx_out_deserialize (btc_tx_out *tx_out, struct const_buffer *buf)
 
int btc_tx_deserialize (const unsigned char *tx_serialized, size_t inlen, btc_tx *tx)
 deserialize/parse a p2p serialized bitcoin transaction More...
 
void btc_tx_in_serialize (cstring *s, const btc_tx_in *tx_in)
 
void btc_tx_out_serialize (cstring *s, const btc_tx_out *tx_out)
 
void btc_tx_serialize (cstring *s, const btc_tx *tx)
 serialize a lbc bitcoin data structure into a p2p serialized buffer More...
 
void btc_tx_hash (const btc_tx *tx, uint8_t *hashout)
 
void btc_tx_in_copy (btc_tx_in *dest, const btc_tx_in *src)
 
void btc_tx_out_copy (btc_tx_out *dest, const btc_tx_out *src)
 
void btc_tx_copy (btc_tx *dest, const btc_tx *src)
 
btc_bool btc_tx_sighash (const btc_tx *tx_to, const cstring *fromPubKey, unsigned int in_num, int hashtype, uint8_t *hash)
 
btc_bool btc_tx_add_address_out (btc_tx *tx, const btc_chain *chain, int64_t amount, const char *address)
 
btc_bool btc_tx_add_p2pkh_hash160_out (btc_tx *tx, int64_t amount, uint8_t *hash160)
 
btc_bool btc_tx_add_p2sh_hash160_out (btc_tx *tx, int64_t amount, uint8_t *hash160)
 
btc_bool btc_tx_add_p2pkh_out (btc_tx *tx, int64_t amount, const btc_pubkey *pubkey)
 

Function Documentation

btc_bool btc_tx_add_address_out ( btc_tx tx,
const btc_chain chain,
int64_t  amount,
const char *  address 
)

Definition at line 426 of file tx.c.

References btc_chain::b58prefix_pubkey_address, btc_chain::b58prefix_script_address, btc_base58_decode_check(), btc_tx_add_p2pkh_hash160_out(), and btc_tx_add_p2sh_hash160_out().

427 {
428 
429  uint8_t buf[strlen(address)*2];
430  int r = btc_base58_decode_check(address, buf, sizeof(buf));
431  if (r <= 0)
432  return false;
433 
434  if (buf[0] == chain->b58prefix_pubkey_address)
435  {
436  btc_tx_add_p2pkh_hash160_out(tx, amount, &buf[1]);
437  }
438  else if (buf[0] == chain->b58prefix_script_address)
439  {
440  btc_tx_add_p2sh_hash160_out(tx, amount, &buf[1]);
441  }
442 
443  return true;
444 }
btc_bool btc_tx_add_p2sh_hash160_out(btc_tx *tx, int64_t amount, uint8_t *hash160)
Definition: tx.c:461
int btc_base58_decode_check(const char *str, uint8_t *data, size_t datalen)
Definition: base58.c:219
btc_bool btc_tx_add_p2pkh_hash160_out(btc_tx *tx, int64_t amount, uint8_t *hash160)
Definition: tx.c:447
uint8_t b58prefix_pubkey_address
Definition: chain.h:42
uint8_t b58prefix_script_address
Definition: chain.h:43
btc_bool btc_tx_add_p2pkh_hash160_out ( btc_tx tx,
int64_t  amount,
uint8_t *  hash160 
)

Definition at line 447 of file tx.c.

References btc_script_build_p2pkh(), btc_tx_out_new(), cstr_new_sz(), btc_tx_out_::script_pubkey, btc_tx_out_::value, vector_add(), and btc_tx_::vout.

Referenced by btc_tx_add_address_out(), and btc_tx_add_p2pkh_out().

448 {
449  btc_tx_out* tx_out = btc_tx_out_new();
450 
451  tx_out->script_pubkey = cstr_new_sz(1024);
452  btc_script_build_p2pkh(tx_out->script_pubkey, hash160);
453 
454  tx_out->value = amount;
455 
456  vector_add(tx->vout, tx_out);
457 
458  return true;
459 }
Definition: tx.h:68
btc_tx_out * btc_tx_out_new()
create a new tx output
Definition: tx.c:103
cstring * cstr_new_sz(size_t sz)
Definition: cstr.c:33
btc_bool vector_add(vector *vec, void *data)
Definition: vector.c:113
cstring * script_pubkey
Definition: tx.h:71
int64_t value
Definition: tx.h:70
btc_bool btc_script_build_p2pkh(cstring *script_in, const uint8_t *hash160)
Definition: script.c:323
vector * vout
Definition: tx.h:78
btc_bool btc_tx_add_p2pkh_out ( btc_tx tx,
int64_t  amount,
const btc_pubkey pubkey 
)

Definition at line 475 of file tx.c.

References btc_pubkey_get_hash160(), and btc_tx_add_p2pkh_hash160_out().

476 {
477 
478  uint8_t hash160[20];
479  btc_pubkey_get_hash160(pubkey, hash160);
480  return btc_tx_add_p2pkh_hash160_out(tx, amount, hash160);
481 }
btc_bool btc_tx_add_p2pkh_hash160_out(btc_tx *tx, int64_t amount, uint8_t *hash160)
Definition: tx.c:447
void btc_pubkey_get_hash160(const btc_pubkey *pubkey, uint8_t *hash160)
Definition: ecc_key.c:112
btc_bool btc_tx_add_p2sh_hash160_out ( btc_tx tx,
int64_t  amount,
uint8_t *  hash160 
)

Definition at line 461 of file tx.c.

References btc_script_build_p2sh(), btc_tx_out_new(), cstr_new_sz(), btc_tx_out_::script_pubkey, btc_tx_out_::value, vector_add(), and btc_tx_::vout.

Referenced by btc_tx_add_address_out().

462 {
463  btc_tx_out* tx_out = btc_tx_out_new();
464 
465  tx_out->script_pubkey = cstr_new_sz(1024);
466  btc_script_build_p2sh(tx_out->script_pubkey, hash160);
467 
468  tx_out->value = amount;
469 
470  vector_add(tx->vout, tx_out);
471 
472  return true;
473 }
Definition: tx.h:68
btc_tx_out * btc_tx_out_new()
create a new tx output
Definition: tx.c:103
btc_bool btc_script_build_p2sh(cstring *script_in, const uint8_t *hash160)
Definition: script.c:338
cstring * cstr_new_sz(size_t sz)
Definition: cstr.c:33
btc_bool vector_add(vector *vec, void *data)
Definition: vector.c:113
cstring * script_pubkey
Definition: tx.h:71
int64_t value
Definition: tx.h:70
vector * vout
Definition: tx.h:78
void btc_tx_copy ( btc_tx dest,
const btc_tx src 
)

Definition at line 283 of file tx.c.

References btc_tx_in_copy(), btc_tx_in_free_cb(), btc_tx_out_copy(), btc_tx_out_free_cb(), vector::len, btc_tx_::locktime, vector_add(), vector_free(), vector_idx, vector_new(), btc_tx_::version, btc_tx_::vin, and btc_tx_::vout.

Referenced by btc_tx_sighash().

284 {
285  dest->version = src->version;
286  dest->locktime = src->locktime;
287 
288  if (!src->vin)
289  dest->vin = NULL;
290  else {
291  unsigned int i;
292 
293  if (dest->vin)
294  vector_free(dest->vin, true);
295 
296  dest->vin = vector_new(src->vin->len, btc_tx_in_free_cb);
297 
298  for (i = 0; i < src->vin->len; i++) {
299  btc_tx_in* tx_in_old, *tx_in_new;
300 
301  tx_in_old = vector_idx(src->vin, i);
302  tx_in_new = malloc(sizeof(*tx_in_new));
303  btc_tx_in_copy(tx_in_new, tx_in_old);
304  vector_add(dest->vin, tx_in_new);
305  }
306  }
307 
308  if (!src->vout)
309  dest->vout = NULL;
310  else {
311  unsigned int i;
312 
313  if (dest->vout)
314  vector_free(dest->vout, true);
315 
316  dest->vout = vector_new(src->vout->len,
318 
319  for (i = 0; i < src->vout->len; i++) {
320  btc_tx_out* tx_out_old, *tx_out_new;
321 
322  tx_out_old = vector_idx(src->vout, i);
323  tx_out_new = malloc(sizeof(*tx_out_new));
324  btc_tx_out_copy(tx_out_new, tx_out_old);
325  vector_add(dest->vout, tx_out_new);
326  }
327  }
328 }
vector * vector_new(size_t res, void(*free_f)(void *))
Definition: vector.c:31
Definition: tx.h:68
void btc_tx_in_free_cb(void *data)
Definition: tx.c:54
Definition: tx.h:61
uint32_t locktime
Definition: tx.h:79
btc_bool vector_add(vector *vec, void *data)
Definition: vector.c:113
#define vector_idx(vec, idx)
Definition: vector.h:59
vector * vin
Definition: tx.h:77
void btc_tx_out_copy(btc_tx_out *dest, const btc_tx_out *src)
Definition: tx.c:268
size_t len
Definition: vector.h:42
void btc_tx_in_copy(btc_tx_in *dest, const btc_tx_in *src)
Definition: tx.c:252
void vector_free(vector *vec, btc_bool free_array)
Definition: vector.c:71
uint32_t version
Definition: tx.h:76
void btc_tx_out_free_cb(void *data)
Definition: tx.c:90
vector * vout
Definition: tx.h:78
int btc_tx_deserialize ( const unsigned char *  tx_serialized,
size_t  inlen,
btc_tx tx 
)

deserialize/parse a p2p serialized bitcoin transaction

Definition at line 157 of file tx.c.

References btc_tx_in_deserialize(), btc_tx_in_new(), btc_tx_out_deserialize(), btc_tx_out_new(), deser_u32(), deser_varlen(), btc_tx_::locktime, vector_add(), btc_tx_::version, btc_tx_::vin, and btc_tx_::vout.

158 {
159  struct const_buffer buf = {tx_serialized, inlen};
160 
161  //tx needs to be initialized
162  deser_u32(&tx->version, &buf);
163  uint32_t vlen;
164  if (!deser_varlen(&vlen, &buf))
165  return false;
166 
167  unsigned int i;
168  for (i = 0; i < vlen; i++) {
169  btc_tx_in* tx_in = btc_tx_in_new();
170 
171  if (!btc_tx_in_deserialize(tx_in, &buf)) {
172  free(tx_in);
173  }
174 
175  vector_add(tx->vin, tx_in);
176  }
177 
178  if (!deser_varlen(&vlen, &buf))
179  return false;
180  for (i = 0; i < vlen; i++) {
181  btc_tx_out* tx_out = btc_tx_out_new();
182 
183  if (!btc_tx_out_deserialize(tx_out, &buf)) {
184  free(tx_out);
185  }
186 
187  vector_add(tx->vout, tx_out);
188  }
189 
190  if (!deser_u32(&tx->locktime, &buf))
191  return false;
192 
193  return true;
194 }
Definition: tx.h:68
btc_tx_out * btc_tx_out_new()
create a new tx output
Definition: tx.c:103
btc_bool btc_tx_in_deserialize(btc_tx_in *tx_in, struct const_buffer *buf)
Definition: tx.c:136
btc_tx_in * btc_tx_in_new()
create a new tx input
Definition: tx.c:67
Definition: tx.h:61
btc_bool deser_u32(uint32_t *vo, struct const_buffer *buf)
Definition: serialize.c:126
btc_bool deser_varlen(uint32_t *lo, struct const_buffer *buf)
Definition: serialize.c:148
uint32_t locktime
Definition: tx.h:79
btc_bool vector_add(vector *vec, void *data)
Definition: vector.c:113
vector * vin
Definition: tx.h:77
uint32_t version
Definition: tx.h:76
btc_bool btc_tx_out_deserialize(btc_tx_out *tx_out, struct const_buffer *buf)
Definition: tx.c:148
vector * vout
Definition: tx.h:78
void btc_tx_free ( btc_tx tx)

Definition at line 112 of file tx.c.

References vector_free(), btc_tx_::vin, and btc_tx_::vout.

Referenced by btc_tx_sighash().

113 {
114  if (tx->vin)
115  vector_free(tx->vin, true);
116 
117  if (tx->vout)
118  vector_free(tx->vout, true);
119 
120  free(tx);
121 }
vector * vin
Definition: tx.h:77
void vector_free(vector *vec, btc_bool free_array)
Definition: vector.c:71
vector * vout
Definition: tx.h:78
void btc_tx_hash ( const btc_tx tx,
uint8_t *  hashout 
)

Definition at line 240 of file tx.c.

References btc_tx_serialize(), cstr_free(), cstr_new_sz(), cstring::len, sha256_Raw(), and cstring::str.

241 {
242  cstring *txser = cstr_new_sz(1024);
243  btc_tx_serialize(txser, tx);
244 
245 
246  sha256_Raw((const uint8_t*)txser->str, txser->len, hashout);
247  sha256_Raw(hashout, 32, hashout);
248  cstr_free(txser, true);
249 }
char * str
Definition: cstr.h:43
void sha256_Raw(const sha2_byte *data, size_t len, uint8_t digest[SHA256_DIGEST_LENGTH])
Definition: sha2.c:652
cstring * cstr_new_sz(size_t sz)
Definition: cstr.c:33
void btc_tx_serialize(cstring *s, const btc_tx *tx)
serialize a lbc bitcoin data structure into a p2p serialized buffer
Definition: tx.c:210
size_t len
Definition: cstr.h:44
void cstr_free(cstring *s, btc_bool free_buf)
Definition: cstr.c:69
Definition: cstr.h:41
void btc_tx_in_copy ( btc_tx_in dest,
const btc_tx_in src 
)

Definition at line 252 of file tx.c.

References cstr_append_buf(), cstr_new_sz(), cstring::len, btc_tx_in_::prevout, btc_tx_in_::script_sig, btc_tx_in_::sequence, and cstring::str.

Referenced by btc_tx_copy().

253 {
254  memcpy(&dest->prevout, &src->prevout, sizeof(dest->prevout));
255  dest->sequence = src->sequence;
256 
257  if (!src->script_sig)
258  dest->script_sig = NULL;
259  else {
260  dest->script_sig = cstr_new_sz(src->script_sig->len);
262  src->script_sig->str,
263  src->script_sig->len);
264  }
265 }
char * str
Definition: cstr.h:43
uint32_t sequence
Definition: tx.h:65
cstring * cstr_new_sz(size_t sz)
Definition: cstr.c:33
btc_tx_outpoint prevout
Definition: tx.h:63
cstring * script_sig
Definition: tx.h:64
size_t len
Definition: cstr.h:44
btc_bool cstr_append_buf(cstring *s, const void *buf, size_t sz)
Definition: cstr.c:106
btc_bool btc_tx_in_deserialize ( btc_tx_in tx_in,
struct const_buffer buf 
)

Definition at line 136 of file tx.c.

References deser_u256(), deser_u32(), deser_varstr(), btc_tx_outpoint_::hash, btc_tx_outpoint_::n, btc_tx_in_::prevout, btc_tx_in_::script_sig, and btc_tx_in_::sequence.

Referenced by btc_tx_deserialize().

137 {
138  deser_u256(tx_in->prevout.hash, buf);
139  if (!deser_u32(&tx_in->prevout.n, buf))
140  return false;
141  if (!deser_varstr(&tx_in->script_sig, buf))
142  return false;
143  if (!deser_u32(&tx_in->sequence, buf))
144  return false;
145  return true;
146 }
uint32_t sequence
Definition: tx.h:65
btc_tx_outpoint prevout
Definition: tx.h:63
cstring * script_sig
Definition: tx.h:64
btc_bool deser_u32(uint32_t *vo, struct const_buffer *buf)
Definition: serialize.c:126
uint32_t n
Definition: tx.h:58
btc_bool deser_varstr(cstring **so, struct const_buffer *buf)
Definition: serialize.c:205
uint256 hash
Definition: tx.h:57
static btc_bool deser_u256(uint8_t *vo, struct const_buffer *buf)
Definition: serialize.h:71
void btc_tx_in_free ( btc_tx_in tx_in)

Definition at line 39 of file tx.c.

References cstr_free(), btc_tx_outpoint_::hash, btc_tx_outpoint_::n, btc_tx_in_::prevout, and btc_tx_in_::script_sig.

Referenced by btc_tx_in_free_cb().

40 {
41  if (!tx_in)
42  return;
43 
44  memset(&tx_in->prevout.hash, 0, 32);
45  tx_in->prevout.n = 0;
46 
47  if (tx_in->script_sig) {
48  cstr_free(tx_in->script_sig, true);
49  tx_in->script_sig = NULL;
50  }
51 }
btc_tx_outpoint prevout
Definition: tx.h:63
cstring * script_sig
Definition: tx.h:64
uint32_t n
Definition: tx.h:58
uint256 hash
Definition: tx.h:57
void cstr_free(cstring *s, btc_bool free_buf)
Definition: cstr.c:69
void btc_tx_in_free_cb ( void *  data)

Definition at line 54 of file tx.c.

References btc_tx_in_free().

Referenced by btc_tx_copy(), and btc_tx_new().

55 {
56  if (!data)
57  return;
58 
59  btc_tx_in* tx_in = data;
60  btc_tx_in_free(tx_in);
61 
62  memset(tx_in, 0, sizeof(*tx_in));
63  free(tx_in);
64 }
Definition: tx.h:61
void btc_tx_in_free(btc_tx_in *tx_in)
Definition: tx.c:39
btc_tx_in* btc_tx_in_new ( )

create a new tx input

Definition at line 67 of file tx.c.

References btc_tx_in_::prevout, and btc_tx_in_::sequence.

Referenced by btc_tx_deserialize().

68 {
69  btc_tx_in* tx_in;
70  tx_in = calloc(1, sizeof(*tx_in));
71  memset(&tx_in->prevout, 0, sizeof(tx_in->prevout));
72  tx_in->sequence = UINT32_MAX;
73  return tx_in;
74 }
uint32_t sequence
Definition: tx.h:65
Definition: tx.h:61
btc_tx_outpoint prevout
Definition: tx.h:63
void btc_tx_in_serialize ( cstring s,
const btc_tx_in tx_in 
)

Definition at line 196 of file tx.c.

References btc_tx_outpoint_::hash, btc_tx_outpoint_::n, btc_tx_in_::prevout, btc_tx_in_::script_sig, btc_tx_in_::sequence, ser_u256(), ser_u32(), and ser_varstr().

Referenced by btc_tx_serialize().

197 {
198  ser_u256(s, tx_in->prevout.hash);
199  ser_u32(s, tx_in->prevout.n);
200  ser_varstr(s, tx_in->script_sig);
201  ser_u32(s, tx_in->sequence);
202 }
uint32_t sequence
Definition: tx.h:65
void ser_u32(cstring *s, uint32_t v_)
Definition: serialize.c:24
static void ser_u256(cstring *s, const unsigned char *v_)
Definition: serialize.h:44
btc_tx_outpoint prevout
Definition: tx.h:63
void ser_varstr(cstring *s, cstring *s_in)
Definition: serialize.c:68
cstring * script_sig
Definition: tx.h:64
uint32_t n
Definition: tx.h:58
uint256 hash
Definition: tx.h:57
btc_tx* btc_tx_new ( )

create a new tx input

Definition at line 124 of file tx.c.

References btc_tx_in_free_cb(), btc_tx_out_free_cb(), btc_tx_::locktime, vector_new(), btc_tx_::version, btc_tx_::vin, and btc_tx_::vout.

Referenced by btc_tx_sighash().

125 {
126  btc_tx* tx;
127  tx = calloc(1, sizeof(*tx));
128  tx->vin = vector_new(8, btc_tx_in_free_cb);
130  tx->version = 1;
131  tx->locktime = 0;
132  return tx;
133 }
vector * vector_new(size_t res, void(*free_f)(void *))
Definition: vector.c:31
void btc_tx_in_free_cb(void *data)
Definition: tx.c:54
uint32_t locktime
Definition: tx.h:79
vector * vin
Definition: tx.h:77
uint32_t version
Definition: tx.h:76
void btc_tx_out_free_cb(void *data)
Definition: tx.c:90
Definition: tx.h:74
vector * vout
Definition: tx.h:78
void btc_tx_out_copy ( btc_tx_out dest,
const btc_tx_out src 
)

Definition at line 268 of file tx.c.

References cstr_append_buf(), cstr_new_sz(), cstring::len, btc_tx_out_::script_pubkey, cstring::str, and btc_tx_out_::value.

Referenced by btc_tx_copy().

269 {
270  dest->value = src->value;
271 
272  if (!src->script_pubkey)
273  dest->script_pubkey = NULL;
274  else {
277  src->script_pubkey->str,
278  src->script_pubkey->len);
279  }
280 }
char * str
Definition: cstr.h:43
cstring * cstr_new_sz(size_t sz)
Definition: cstr.c:33
cstring * script_pubkey
Definition: tx.h:71
size_t len
Definition: cstr.h:44
int64_t value
Definition: tx.h:70
btc_bool cstr_append_buf(cstring *s, const void *buf, size_t sz)
Definition: cstr.c:106
btc_bool btc_tx_out_deserialize ( btc_tx_out tx_out,
struct const_buffer buf 
)

Definition at line 148 of file tx.c.

References deser_s64(), deser_varstr(), btc_tx_out_::script_pubkey, and btc_tx_out_::value.

Referenced by btc_tx_deserialize().

149 {
150  if (!deser_s64(&tx_out->value, buf))
151  return false;
152  if (!deser_varstr(&tx_out->script_pubkey, buf))
153  return false;
154  return true;
155 }
cstring * script_pubkey
Definition: tx.h:71
btc_bool deser_varstr(cstring **so, struct const_buffer *buf)
Definition: serialize.c:205
static btc_bool deser_s64(int64_t *vo, struct const_buffer *buf)
Definition: serialize.h:80
int64_t value
Definition: tx.h:70
void btc_tx_out_free ( btc_tx_out tx_out)

Definition at line 77 of file tx.c.

References cstr_free(), btc_tx_out_::script_pubkey, and btc_tx_out_::value.

Referenced by btc_tx_out_free_cb().

78 {
79  if (!tx_out)
80  return;
81  tx_out->value = 0;
82 
83  if (tx_out->script_pubkey) {
84  cstr_free(tx_out->script_pubkey, true);
85  tx_out->script_pubkey = NULL;
86  }
87 }
cstring * script_pubkey
Definition: tx.h:71
int64_t value
Definition: tx.h:70
void cstr_free(cstring *s, btc_bool free_buf)
Definition: cstr.c:69
void btc_tx_out_free_cb ( void *  data)

Definition at line 90 of file tx.c.

References btc_tx_out_free().

Referenced by btc_tx_copy(), btc_tx_new(), and btc_tx_sighash().

91 {
92  if (!data)
93  return;
94 
95  btc_tx_out* tx_out = data;
96  btc_tx_out_free(tx_out);
97 
98  memset(tx_out, 0, sizeof(*tx_out));
99  free(tx_out);
100 }
Definition: tx.h:68
void btc_tx_out_free(btc_tx_out *tx_out)
Definition: tx.c:77
btc_tx_out* btc_tx_out_new ( )

create a new tx output

Definition at line 103 of file tx.c.

Referenced by btc_tx_add_p2pkh_hash160_out(), btc_tx_add_p2sh_hash160_out(), and btc_tx_deserialize().

104 {
105  btc_tx_out* tx_out;
106  tx_out = calloc(1, sizeof(*tx_out));
107 
108  return tx_out;
109 }
Definition: tx.h:68
void btc_tx_out_serialize ( cstring s,
const btc_tx_out tx_out 
)

Definition at line 204 of file tx.c.

References btc_tx_out_::script_pubkey, ser_s64(), ser_varstr(), and btc_tx_out_::value.

Referenced by btc_tx_serialize().

205 {
206  ser_s64(s, tx_out->value);
207  ser_varstr(s, tx_out->script_pubkey);
208 }
static void ser_s64(cstring *s, int64_t v_)
Definition: serialize.h:58
void ser_varstr(cstring *s, cstring *s_in)
Definition: serialize.c:68
cstring * script_pubkey
Definition: tx.h:71
int64_t value
Definition: tx.h:70
void btc_tx_serialize ( cstring s,
const btc_tx tx 
)

serialize a lbc bitcoin data structure into a p2p serialized buffer

Definition at line 210 of file tx.c.

References btc_tx_in_serialize(), btc_tx_out_serialize(), vector::len, btc_tx_::locktime, ser_u32(), ser_varlen(), vector_idx, btc_tx_::version, btc_tx_::vin, and btc_tx_::vout.

Referenced by btc_tx_hash(), and btc_tx_sighash().

211 {
212  ser_u32(s, tx->version);
213 
214  ser_varlen(s, tx->vin ? tx->vin->len : 0);
215 
216  unsigned int i;
217  if (tx->vin) {
218  for (i = 0; i < tx->vin->len; i++) {
219  btc_tx_in* tx_in;
220 
221  tx_in = vector_idx(tx->vin, i);
222  btc_tx_in_serialize(s, tx_in);
223  }
224  }
225 
226  ser_varlen(s, tx->vout ? tx->vout->len : 0);
227 
228  if (tx->vout) {
229  for (i = 0; i < tx->vout->len; i++) {
230  btc_tx_out* tx_out;
231 
232  tx_out = vector_idx(tx->vout, i);
233  btc_tx_out_serialize(s, tx_out);
234  }
235  }
236 
237  ser_u32(s, tx->locktime);
238 }
Definition: tx.h:68
void btc_tx_out_serialize(cstring *s, const btc_tx_out *tx_out)
Definition: tx.c:204
void ser_u32(cstring *s, uint32_t v_)
Definition: serialize.c:24
Definition: tx.h:61
void ser_varlen(cstring *s, uint32_t vlen)
Definition: serialize.c:36
void btc_tx_in_serialize(cstring *s, const btc_tx_in *tx_in)
Definition: tx.c:196
uint32_t locktime
Definition: tx.h:79
#define vector_idx(vec, idx)
Definition: vector.h:59
vector * vin
Definition: tx.h:77
size_t len
Definition: vector.h:42
uint32_t version
Definition: tx.h:76
vector * vout
Definition: tx.h:78
btc_bool btc_tx_sighash ( const btc_tx tx_to,
const cstring fromPubKey,
unsigned int  in_num,
int  hashtype,
uint8_t *  hash 
)

Definition at line 330 of file tx.c.

References btc_script_copy_without_op_codeseperator(), btc_tx_copy(), btc_tx_free(), btc_tx_new(), btc_tx_out_free_cb(), btc_tx_serialize(), cstr_append_buf(), cstr_free(), cstr_new_sz(), cstr_resize(), vector::len, cstring::len, btc_tx_out_::script_pubkey, btc_tx_in_::script_sig, btc_tx_in_::sequence, ser_s32(), sha256_Raw(), SIGHASH_ANYONECANPAY, SIGHASH_NONE, SIGHASH_SINGLE, cstring::str, btc_tx_out_::value, vector_free(), vector_idx, vector_new(), vector_remove_range(), vector_resize(), btc_tx_::vin, and btc_tx_::vout.

331 {
332  if (in_num >= tx_to->vin->len)
333  return false;
334 
335  btc_bool ret = true;
336 
337  btc_tx* tx_tmp = btc_tx_new();
338  btc_tx_copy(tx_tmp, tx_to);
339 
340  cstring* new_script = cstr_new_sz(fromPubKey->len);
341  btc_script_copy_without_op_codeseperator(fromPubKey, new_script);
342 
343  unsigned int i;
344  btc_tx_in* tx_in;
345  for (i = 0; i < tx_tmp->vin->len; i++) {
346  tx_in = vector_idx(tx_tmp->vin, i);
347  cstr_resize(tx_in->script_sig, 0);
348 
349  if (i == in_num)
351  new_script->str,
352  new_script->len);
353  }
354  cstr_free(new_script, true);
355  /* Blank out some of the outputs */
356  if ((hashtype & 0x1f) == SIGHASH_NONE) {
357  /* Wildcard payee */
358  if (tx_tmp->vout)
359  vector_free(tx_tmp->vout, true);
360 
361  tx_tmp->vout = vector_new(1, btc_tx_out_free_cb);
362 
363  /* Let the others update at will */
364  for (i = 0; i < tx_tmp->vin->len; i++) {
365  tx_in = vector_idx(tx_tmp->vin, i);
366  if (i != in_num)
367  tx_in->sequence = 0;
368  }
369  }
370 
371  else if ((hashtype & 0x1f) == SIGHASH_SINGLE) {
372  /* Only lock-in the txout payee at same index as txin */
373  unsigned int n_out = in_num;
374  if (n_out >= tx_tmp->vout->len) {
375  //TODO: set error code
376  ret = false;
377  goto out;
378  }
379 
380  vector_resize(tx_tmp->vout, n_out + 1);
381 
382  for (i = 0; i < n_out; i++) {
383  btc_tx_out* tx_out;
384 
385  tx_out = vector_idx(tx_tmp->vout, i);
386  tx_out->value = -1;
387  if (tx_out->script_pubkey) {
388  cstr_free(tx_out->script_pubkey, true);
389  tx_out->script_pubkey = NULL;
390  }
391  }
392 
393  /* Let the others update at will */
394  for (i = 0; i < tx_tmp->vin->len; i++) {
395  tx_in = vector_idx(tx_tmp->vin, i);
396  if (i != in_num)
397  tx_in->sequence = 0;
398  }
399  }
400 
401  /* Blank out other inputs completely;
402  not recommended for open transactions */
403  if (hashtype & SIGHASH_ANYONECANPAY) {
404  if (in_num > 0)
405  vector_remove_range(tx_tmp->vin, 0, in_num);
406  vector_resize(tx_tmp->vin, 1);
407  }
408 
409  cstring* s = cstr_new_sz(512);
410  btc_tx_serialize(s, tx_tmp);
411  char hextest[4096];
412  ser_s32(s, hashtype);
413 
414  sha256_Raw((const uint8_t*)s->str, s->len, hash);
415  sha256_Raw(hash, 32, hash);
416 
417  cstr_free(s, true);
418 
419 out:
420  btc_tx_free(tx_tmp);
421 
422  return ret;
423 }
vector * vector_new(size_t res, void(*free_f)(void *))
Definition: vector.c:31
uint8_t btc_bool
Definition: btc.h:34
Definition: tx.h:68
void btc_tx_copy(btc_tx *dest, const btc_tx *src)
Definition: tx.c:283
char * str
Definition: cstr.h:43
void vector_remove_range(vector *vec, size_t pos, size_t len)
Definition: vector.c:124
uint32_t sequence
Definition: tx.h:65
static void ser_s32(cstring *s, int32_t v_)
Definition: serialize.h:53
void btc_tx_free(btc_tx *tx)
Definition: tx.c:112
void sha256_Raw(const sha2_byte *data, size_t len, uint8_t digest[SHA256_DIGEST_LENGTH])
Definition: sha2.c:652
Definition: tx.h:61
cstring * cstr_new_sz(size_t sz)
Definition: cstr.c:33
btc_tx * btc_tx_new()
create a new tx input
Definition: tx.c:124
cstring * script_sig
Definition: tx.h:64
void btc_tx_serialize(cstring *s, const btc_tx *tx)
serialize a lbc bitcoin data structure into a p2p serialized buffer
Definition: tx.c:210
btc_bool cstr_resize(cstring *s, size_t new_sz)
Definition: cstr.c:81
cstring * script_pubkey
Definition: tx.h:71
#define vector_idx(vec, idx)
Definition: vector.h:59
vector * vin
Definition: tx.h:77
size_t len
Definition: cstr.h:44
size_t len
Definition: vector.h:42
void vector_free(vector *vec, btc_bool free_array)
Definition: vector.c:71
int64_t value
Definition: tx.h:70
void btc_tx_out_free_cb(void *data)
Definition: tx.c:90
btc_bool vector_resize(vector *vec, size_t newsz)
Definition: vector.c:154
btc_bool btc_script_copy_without_op_codeseperator(const cstring *script_in, cstring *script_out)
Definition: script.c:36
void cstr_free(cstring *s, btc_bool free_buf)
Definition: cstr.c:69
Definition: tx.h:74
btc_bool cstr_append_buf(cstring *s, const void *buf, size_t sz)
Definition: cstr.c:106
Definition: cstr.h:41
vector * vout
Definition: tx.h:78