-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathBitcoinTransactions.cpp
95 lines (82 loc) · 2.5 KB
/
BitcoinTransactions.cpp
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
#include "BitcoinTransactions.h"
#include "BitcoinScript.h"
#include <stdio.h>
#ifdef _MSC_VER // Disable the stupid ass absurd warning messages from Visual Studio telling you that using stdlib and stdio is 'not valid ANSI C'
#pragma warning(disable:4996)
#endif
class BitcoinTransactionsImpl : public BitcoinTransactions
{
public:
BitcoinTransactionsImpl(void)
{
mBitcoinScript = createBitcoinScript();
}
~BitcoinTransactionsImpl(void)
{
if ( mBitcoinScript )
{
mBitcoinScript->release();
}
}
const char *hashString(const uint8_t *h)
{
static char temp[65];
temp[64] = 0;
for (uint32_t i=0; i<32; i++)
{
sprintf(&temp[i*2],"%02x", h[i] );
}
return temp;
}
virtual void processBlock(const BlockChain::Block &block)
{
uint64_t btc=0;
uint64_t oneBTC = 100000000;
printf("%d transactions.\r\n", block.transactionCount );
for (uint32_t i=0; i<block.transactionCount; i++)
{
const BlockChain::BlockTransaction &t = block.transactions[i];
#if 0
for (uint32_t i=0; i<t.inputCount; i++)
{
const BlockChain::BlockInput &input = t.inputs[i];
mBitcoinScript->executeScript(input.responseScript,input.responseScriptLength);
}
for (uint32_t i=0; i<t.outputCount; i++)
{
const BlockChain::BlockOutput &ou tput = t.outputs[i];
mBitcoinScript->resetStack();
mBitcoinScript->executeScript(output.challengeScript,output.challengeScriptLength);
}
#else
printf("Transaction #%d has %d outputs and %d inputs.\r\n", i+1, t.outputCount, t.inputCount );
for (uint32_t i=0; i<t.inputCount; i++)
{
const BlockChain::BlockInput &input = t.inputs[i];
printf("Input %d : hash(%s) Index: %d\r\n", i+1, hashString(input.transactionHash), input.transactionIndex );
mBitcoinScript->resetStack();
mBitcoinScript->executeScript(input.responseScript,input.responseScriptLength);
}
for (uint32_t i=0; i<t.outputCount; i++)
{
const BlockChain::BlockOutput &output = t.outputs[i];
printf("output %d is %d BTC.\r\n", i+1, (int)(output.value / oneBTC) );
btc+=output.value;
mBitcoinScript->resetStack();
mBitcoinScript->executeScript(output.challengeScript,output.challengeScriptLength);
}
#endif
}
printf("OutputBTC: %0.4f\r\n", (float)btc / (float)oneBTC );
}
virtual void release(void)
{
delete this;
}
BitcoinScript *mBitcoinScript; // the bitcoin script parser
};
BitcoinTransactions *createTransactions(void)
{
BitcoinTransactionsImpl *t = new BitcoinTransactionsImpl;
return static_cast< BitcoinTransactions *>(t);
}