This repository has been archived by the owner on May 12, 2021. It is now read-only.
forked from hrobeers/poolserver
/
Bitcoin.h
75 lines (63 loc) · 1.84 KB
/
Bitcoin.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
#ifndef BITCOIN_H_
#define BITCOIN_H_
#include "Block.h"
#include "Transaction.h"
#include "VarInt.h"
#include "Script.h"
#include "BigNum.h"
#include "Util.h"
#include <gmpxx.h>
namespace Bitcoin
{
inline BigInt TargetToDiff(BigInt val)
{
static BigInt c("0x00000000ffff0000000000000000000000000000000000000000000000000000");
return (c / val);
}
inline BigInt DiffToTarget(BigInt val)
{
// Algebra says that it is the same!
return TargetToDiff(val);
}
inline BigInt TargetFromBits(uint32 bits)
{
uint8 nbytes = (bits >> 24) & 0xFF;
mpz_t power;
mpz_init(power);
mpz_ui_pow_ui(power, 2, 8 * (nbytes - 3));
BigInt result = BigInt(bits & 0xFFFFFF) * BigInt(power);
mpz_clear(power);
return result;
}
inline Transaction CreateCoinbaseTX(uint32 blockHeight, BinaryData pubkey, int64 value, BinaryData extras)
{
// Extranonce placeholder
BinaryData extranonce_ph(8, 0);
ByteBuffer scriptsig;
uint8 three=3;
scriptsig << three << blockHeight;
scriptsig << extranonce_ph << extras;
Bitcoin::OutPoint outpoint;
outpoint.hash.resize(32, 0);
outpoint.n = 0xFFFFFFFF;
TxIn txin;
txin.prevout = outpoint;
txin.script = scriptsig.Binary();
txin.n = 0;
ByteBuffer temp;
temp.Append<uint8>(pubkey.size());
temp << pubkey;
temp.Append<uint8>(Bitcoin::OP_CHECKSIG);
TxOut txout;
txout.value = value;
txout.scriptPubKey = Bitcoin::Script(temp.Binary());
Transaction tx;
tx.version = 1;
tx.time = Util::Date();
tx.in.push_back(txin);
tx.out.push_back(txout);
tx.lockTime = 0;
return tx;
}
}
#endif