This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathtx.h
210 lines (161 loc) · 5.02 KB
/
tx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#ifndef __TX_H
#define __TX_H
#include <stdint.h>
#include "common.h"
#include "endian.h"
#include "varint.h"
typedef struct {
uint64_t value;
uint64_t script_len;
uint8_t *script;
} bbp_txout_t;
typedef struct {
uint8_t txid[32];
uint32_t index;
} bbp_outpoint_t;
typedef struct {
bbp_outpoint_t outpoint;
uint64_t script_len;
uint8_t *script;
uint32_t sequence;
} bbp_txin_t;
typedef struct {
uint32_t version;
uint64_t inputs_len;
bbp_txin_t *inputs;
uint64_t outputs_len;
bbp_txout_t *outputs;
uint32_t locktime;
} bbp_tx_t;
typedef enum {
BBP_SIGHASH_ALL = 0x01
} bbp_sighash_t;
typedef uint8_t *bbp_message_t;
void bbp_outpoint_fill(bbp_outpoint_t *outpoint, const char *txid, uint32_t index) {
bbp_parse_hex(outpoint->txid, txid);
bbp_reverse(outpoint->txid, 32);
outpoint->index = bbp_eint32(BBP_LITTLE, index);
}
void bbp_txout_create_p2pkh(bbp_txout_t *txout, const uint64_t value, const char *hash160) {
char script[52] = { 0 };
sprintf(script, "76a914%s88ac", hash160);
txout->value = bbp_eint64(BBP_LITTLE, value);
txout->script = bbp_alloc_hex(script, (size_t *)&txout->script_len);
}
void bbp_txout_destroy(bbp_txout_t *txout) {
free(txout->script);
}
void bbp_txin_create_p2pkh(bbp_txin_t *txin, const bbp_outpoint_t *outpoint,
const char *sig, const char *pub, bbp_sighash_t flag) {
char script[400] = { 0 };
sprintf(script, "%02lx%s%02x%02lx%s", strlen(sig) / 2 + 1, sig, flag, strlen(pub) / 2, pub);
memcpy(&txin->outpoint, outpoint, sizeof(bbp_outpoint_t));
txin->script = bbp_alloc_hex(script, (size_t *)&txin->script_len);
txin->sequence = 0xffffffff;
}
void bbp_txin_destroy(bbp_txin_t *txin) {
free(txin->script);
}
/* signable message */
void bbp_txin_create_signable(bbp_txin_t *txin, const bbp_outpoint_t *outpoint, const bbp_txout_t *utxo) {
memcpy(&txin->outpoint, outpoint, sizeof(bbp_outpoint_t));
txin->script_len = utxo->script_len;
txin->script = malloc(utxo->script_len);
memcpy(txin->script, utxo->script, utxo->script_len);
txin->sequence = 0xffffffff;
}
void bbp_txin_create_truncated(bbp_txin_t *txin, const bbp_outpoint_t *outpoint) {
memcpy(&txin->outpoint, outpoint, sizeof(bbp_outpoint_t));
txin->script_len = 0;
txin->script = NULL;
txin->sequence = 0xffffffff;
}
size_t bbp_tx_size(const bbp_tx_t *tx, bbp_sighash_t flag) {
size_t size = 0;
int i;
/* version */
size += sizeof(uint32_t);
/* inputs count */
size += bbp_varint_size(tx->inputs_len);
/* inputs */
for (i = 0; i < tx->inputs_len; ++i) {
bbp_txin_t *txin = &tx->inputs[i];
/* outpoint */
size += sizeof(bbp_outpoint_t);
/* script */
size += bbp_varint_size(txin->script_len);
size += txin->script_len;
/* sequence */
size += sizeof(uint32_t);
}
/* outputs count */
size += bbp_varint_size(tx->outputs_len);
/* outputs */
for (i = 0; i < tx->outputs_len; ++i) {
bbp_txout_t *txout = &tx->outputs[i];
/* value */
size += sizeof(uint64_t);
/* script */
size += bbp_varint_size(txout->script_len);
size += txout->script_len;
}
/* locktime */
size += sizeof(uint32_t);
if (flag) {
/* sighash */
size += sizeof(uint32_t);
}
return size;
}
void bbp_tx_serialize(const bbp_tx_t *tx, uint8_t *raw, bbp_sighash_t flag) {
uint8_t *ptr;
size_t varlen;
int i;
ptr = raw;
/* version */
*(uint32_t *)ptr = bbp_eint32(BBP_LITTLE, tx->version);
ptr += sizeof(uint32_t);
/* inputs count */
bbp_varint_set(ptr, tx->inputs_len, &varlen);
ptr += varlen;
/* inputs */
for (i = 0; i < tx->inputs_len; ++i) {
bbp_txin_t *txin = &tx->inputs[i];
/* outpoint */
memcpy(ptr, txin->outpoint.txid, 32);
ptr += 32;
*(uint32_t *)ptr = bbp_eint32(BBP_LITTLE, txin->outpoint.index);
ptr += sizeof(uint32_t);
/* script */
bbp_varint_set(ptr, txin->script_len, &varlen);
ptr += varlen;
memcpy(ptr, txin->script, txin->script_len);
ptr += txin->script_len;
/* sequence */
*(uint32_t *)ptr = bbp_eint32(BBP_LITTLE, txin->sequence);
ptr += sizeof(uint32_t);
}
/* outputs count */
bbp_varint_set(ptr, tx->outputs_len, &varlen);
ptr += varlen;
/* outputs */
for (i = 0; i < tx->outputs_len; ++i) {
bbp_txout_t *txout = &tx->outputs[i];
/* value */
*(uint64_t *)ptr = bbp_eint64(BBP_LITTLE, txout->value);
ptr += sizeof(uint64_t);
/* script */
bbp_varint_set(ptr, txout->script_len, &varlen);
ptr += varlen;
memcpy(ptr, txout->script, txout->script_len);
ptr += txout->script_len;
}
/* locktime */
*(uint32_t *)ptr = bbp_eint32(BBP_LITTLE, tx->locktime);
ptr += sizeof(uint32_t);
if (flag) {
/* sighash */
*(uint32_t *)ptr = bbp_eint32(BBP_LITTLE, flag);
}
}
#endif