18
18
#include "base58.h"
19
19
#include "create_tx.h"
20
20
#include "hash_tx.h"
21
- #include "json .h"
21
+ #include "hex .h"
22
22
#include "log.h"
23
23
#include "marshal.h"
24
24
#include <assert.h>
@@ -117,32 +117,19 @@ static void usage(void)
117
117
);
118
118
}
119
119
120
- static bool from_hex (const char * hexstr , u8 * dest )
121
- {
122
- unsigned int i ;
123
-
124
- for (i = 0 ; i < strlen (hexstr ) / 2 ; i ++ ) {
125
- unsigned int v ;
126
-
127
- if (sscanf (hexstr + i * 2 , "%02x" , & v ) != 1 )
128
- return false;
129
- dest [i ] = v ;
130
- }
131
- return true;
132
- }
133
-
134
120
static struct protocol_double_sha parse_txhash (const char * txraw )
135
121
{
136
- union protocol_tx * tx = malloc (strlen (txraw ) / 2 );
122
+ size_t len = strlen (txraw ) / 2 ;
123
+ union protocol_tx * tx = malloc (len );
137
124
struct protocol_double_sha sha ;
138
125
139
- if (strlen ( txraw ) / 2 < sizeof (struct protocol_tx_hdr ))
126
+ if (len < sizeof (struct protocol_tx_hdr ))
140
127
errx (1 , "Short raw tx '%s'" , txraw );
141
128
142
- if (strlen (txraw ) % 2 || ! from_hex (txraw , (u8 * )tx ))
129
+ if (! from_hex (txraw , strlen (txraw ) , (u8 * )tx , len ))
143
130
errx (1 , "Bad raw tx '%s'" , txraw );
144
131
145
- if (marshal_tx_len (tx ) != strlen ( txraw ) / 2 )
132
+ if (marshal_tx_len (tx ) != len )
146
133
errx (1 , "Bad length raw tx '%s'" , txraw );
147
134
148
135
/* You can make this crash, of course */
@@ -157,7 +144,7 @@ static struct protocol_double_sha parse_tx(const char *txstr)
157
144
if (strstarts (txstr , "raw:" ))
158
145
return parse_txhash (txstr + 4 );
159
146
160
- if (strlen (txstr ) != 32 * 2 || ! from_hex (txstr , sha .sha ))
147
+ if (! from_hex (txstr , strlen (txstr ), & sha .sha , sizeof ( sha . sha ) ))
161
148
errx (1 , "Bad sha '%s'" , txstr );
162
149
163
150
return sha ;
@@ -173,7 +160,7 @@ int main(int argc, char *argv[])
173
160
bool normal = false;
174
161
bool to_gateway = false;
175
162
bool pay_fee = true;
176
- char * request ;
163
+ char * txhash ;
177
164
178
165
if (argc < 3 )
179
166
usage ();
@@ -245,12 +232,8 @@ int main(int argc, char *argv[])
245
232
pay_fee , input , key );
246
233
}
247
234
248
- request = tal_arr (NULL , char , 0 );
249
- /* Reuse this as an easy hexstring maker */
250
- json_add_hex (& request , NULL , tx , marshal_tx_len (tx ));
251
- /* Strip "" */
252
- request [strlen (request ) - 1 ] = '\0' ;
253
- printf ("%s" , request + 1 );
254
- tal_free (request );
235
+ txhash = to_hex (NULL , tx , marshal_tx_len (tx ));
236
+ printf ("%s" , txhash );
237
+ tal_free (txhash );
255
238
return 0 ;
256
239
}
0 commit comments