-
Notifications
You must be signed in to change notification settings - Fork 1
/
UBitcoinKey.pas
199 lines (165 loc) · 5.31 KB
/
UBitcoinKey.pas
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
unit UBitcoinKey;
interface
uses
Classes,
SysUtils,
UECKey,
ClpEncoders,
ClpBigInteger,
ClpArrayUtils,
ClpSecureRandom,
ClpISecureRandom,
ClpDigestUtilities,
ClpIECPublicKeyParameters,
ClpIECPrivateKeyParameters;
resourcestring
SInvalidECKeySize = 'The size of an EC key should be %d but ACount is %d';
SInvalidECKey = 'Invalid EC Key';
type
IBitcoinKey = interface(IInterface)
['{5CEAC001-F1A9-426F-A770-1E2D60BDDCB6}']
function GeneratePublicAddress(Compressed: Boolean = True): String;
function GenerateWIFPrivateKey(Compressed: Boolean = True): String;
function GetPrivateKey: IECPrivateKeyParameters;
function GetPublicKey: IECPublicKeyParameters;
function GetIsCompressed: Boolean;
property IsCompressed: Boolean read GetIsCompressed;
property PrivateKey: IECPrivateKeyParameters read GetPrivateKey;
property PublicKey: IECPublicKeyParameters read GetPublicKey;
end;
type
TBitcoinKey = class sealed(TInterfacedObject, IBitcoinKey)
strict private
const
ValidKeySize = 32;
VersionByte = '00';
WIFPrivateKeyHeader = '80';
CompressedWIFPrivateKeyMarker = '01';
var
FECKey: IECKey;
FIsCompressed: Boolean;
class var
FSecureRandom: ISecureRandom;
function GetIsCompressed: Boolean; inline;
function GetPrivateKey: IECPrivateKeyParameters; inline;
function GetPublicKey: IECPublicKeyParameters; inline;
class function ValidateScalarKey(const AData: TBytes): Boolean;
static; inline;
class function DoubleSHA256Hash(const AMessage: TBytes): TBytes;
static; inline;
class constructor BitcoinKey();
public
constructor Create(); overload;
constructor Create(ACompressed: Boolean); overload;
constructor Create(const AData: TBytes; ACount: Int32 = -1;
ACompressed: Boolean = True); overload;
function GeneratePublicAddress(Compressed: Boolean = True): String;
function GenerateWIFPrivateKey(Compressed: Boolean = True): String;
property IsCompressed: Boolean read GetIsCompressed;
property PrivateKey: IECPrivateKeyParameters read GetPrivateKey;
property PublicKey: IECPublicKeyParameters read GetPublicKey;
class property SecureRandom: ISecureRandom read FSecureRandom;
class function GenerateValidRandomBytesForPrivateKey(): TBytes; static;
end;
implementation
{ TBitcoinKey }
function TBitcoinKey.GetIsCompressed: Boolean;
begin
Result := FIsCompressed;
end;
function TBitcoinKey.GetPrivateKey: IECPrivateKeyParameters;
begin
Result := FECKey.PrivateKey;
end;
function TBitcoinKey.GetPublicKey: IECPublicKeyParameters;
begin
Result := FECKey.PublicKey;
end;
class function TBitcoinKey.ValidateScalarKey(const AData: TBytes): Boolean;
var
D: TBigInteger;
begin
Result := True;
D := TBigInteger.Create(1, AData);
if ((D.CompareTo(TBigInteger.One) < 0) or (D.CompareTo(TECKey.Secp256k1.N)
>= 0)) then
begin
Exit(False);
end;
end;
constructor TBitcoinKey.Create();
begin
Create(True);
end;
constructor TBitcoinKey.Create(ACompressed: Boolean);
var
data: TBytes;
begin
Inherited Create();
data := GenerateValidRandomBytesForPrivateKey();
FIsCompressed := ACompressed;
FECKey := TECKey.Create(data, True);
end;
constructor TBitcoinKey.Create(const AData: TBytes; ACount: Int32;
ACompressed: Boolean);
begin
if (ACount = -1) then
ACount := System.Length(AData);
if (ACount <> ValidKeySize) then
begin
raise EArgumentException.CreateResFmt(@SInvalidECKeySize,
[ValidKeySize, ACount]);
end;
if (ValidateScalarKey(AData)) then
begin
FIsCompressed := ACompressed;
FECKey := TECKey.Create(System.Copy(AData, 0, ACount), True);
end
else
raise EArgumentException.CreateRes(@SInvalidECKey);
end;
class function TBitcoinKey.DoubleSHA256Hash(const AMessage: TBytes): TBytes;
begin
Result := TDigestUtilities.CalculateDigest('SHA256',
TDigestUtilities.CalculateDigest('SHA256', AMessage));
end;
function TBitcoinKey.GeneratePublicAddress(Compressed: Boolean): String;
var
Hash, WithVersionByte, Checksum: TBytes;
begin
Hash := TDigestUtilities.CalculateDigest('RIPEMD160',
TDigestUtilities.CalculateDigest('SHA256',
FECKey.PublicKey.Q.GetEncoded(Compressed)));
WithVersionByte := TArrayUtils.Concatenate(THex.Decode(VersionByte), Hash);
Hash := DoubleSHA256Hash(WithVersionByte);
Checksum := System.Copy(Hash, 0, 4);
Result := TBase58.Encode(TArrayUtils.Concatenate(WithVersionByte, Checksum));
end;
class function TBitcoinKey.GenerateValidRandomBytesForPrivateKey: TBytes;
begin
System.SetLength(Result, ValidKeySize);
repeat
FSecureRandom.NextBytes(Result);
until (ValidateScalarKey(Result));
end;
function TBitcoinKey.GenerateWIFPrivateKey(Compressed: Boolean): String;
var
PrivateKeyBytes, Checksum, Hash, WithVersionByte: TBytes;
begin
PrivateKeyBytes := FECKey.GetPrivateKeyAsBytes;
WithVersionByte := TArrayUtils.Concatenate(THex.Decode(WIFPrivateKeyHeader),
PrivateKeyBytes);
if Compressed then
begin
WithVersionByte := TArrayUtils.Concatenate(WithVersionByte,
THex.Decode(CompressedWIFPrivateKeyMarker));
end;
Hash := DoubleSHA256Hash(WithVersionByte);
Checksum := System.Copy(Hash, 0, 4);
Result := TBase58.Encode(TArrayUtils.Concatenate(WithVersionByte, Checksum));
end;
class constructor TBitcoinKey.BitcoinKey;
begin
FSecureRandom := TSecureRandom.Create();
end;
end.