-
Notifications
You must be signed in to change notification settings - Fork 1
/
btckeyfunctions.pas
267 lines (201 loc) · 6.92 KB
/
btckeyfunctions.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
unit btckeyfunctions;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, ClpEncoders,
TypInfo,
ClpISigner,
ClpBigInteger,
ClpISecureRandom,
ClpSecureRandom,
ClpSignerUtilities,
ClpIX9ECParameters,
ClpIECPublicKeyParameters,
ClpIECPrivateKeyParameters,
ClpIAsymmetricCipherKeyPair,
ClpGeneratorUtilities,
ClpCustomNamedCurves,
ClpECPrivateKeyParameters,
ClpECPublicKeyParameters,
ClpIECDomainParameters,
ClpECDomainParameters,
ClpECKeyGenerationParameters,
ClpIECKeyGenerationParameters,
ClpIAsymmetricCipherKeyPairGenerator,
UBitcoinKey;
type
TKeyPair = record
PublicKey: TBytes;
PrivateKey: TBytes;
end;
type
{$SCOPEDENUMS ON}
TKeyType = (SECP256K1, SECP384R1, SECP521R1, SECT283K1);
{$SCOPEDENUMS OFF}
type
{ TbtcKeyFunctions }
TbtcKeyFunctions = class sealed(TObject)
strict private
const
SigningAlgorithm = 'SHA-1withECDSA';
class var FSecureRandom: ISecureRandom;
class function GetCurveFromKeyType(AKeyType: TKeyType): IX9ECParameters;
static; inline;
class function GetSecureRandom: ISecureRandom; static; inline;
class property SecureRandom: ISecureRandom read GetSecureRandom;
private
class function GetSigner(): ISigner; static;
class function GetCurve(keyType: TKeyType): IX9ECParameters; static;
class function GetDomain(curve: IX9ECParameters)
: IECDomainParameters; static;
public
class function GenerateECKeyPair(AKeyType: TKeyType): TKeyPair; static;
class function SignMessage(const &message: TBytes; const PrivateKey: TBytes;
AKeyType: TKeyType): TBytes; static;
class function VerifySignature(const signature: TBytes;
const &message: TBytes; const PublicKey: TBytes; AKeyType: TKeyType)
: Boolean; static;
class function GenPubKeyFromPvtInput(AKeyType: TKeyType): TKeyPair;
class function GenerateValidRandomBytesForPrivateKey(): String; static;
class procedure GetPublicKeyDetails(const APrivateKey: String;
isCompressed: Boolean; var Address, PubKey: String); static;
class function GetPrivateKeyWIF(const APrivateKey: String;
isCompressed: Boolean): String; static;
end;
implementation
class function TbtcKeyFunctions.GetCurveFromKeyType(AKeyType: TKeyType
): IX9ECParameters;
var
CurveName: string;
begin
CurveName := GetEnumName(TypeInfo(TKeyType), Ord(AKeyType));
Result := TCustomNamedCurves.GetByName(CurveName);
end;
class function TbtcKeyFunctions.GetCurve(keyType: TKeyType): IX9ECParameters;
begin
Result := GetCurveFromKeyType(keyType);
end;
class function TbtcKeyFunctions.GetDomain(curve: IX9ECParameters
): IECDomainParameters;
begin
Result := TECDomainParameters.Create(curve.curve, curve.G, curve.N, curve.H,
curve.GetSeed);
end;
class function TbtcKeyFunctions.GetSecureRandom: ISecureRandom;
begin
if FSecureRandom <> Nil then
begin
Result := FSecureRandom
end
else
begin
FSecureRandom := TSecureRandom.Create();
Result := FSecureRandom;
end;
end;
class function TbtcKeyFunctions.GetSigner(): ISigner;
begin
Result := TSignerUtilities.GetSigner(SigningAlgorithm);
end;
class function TbtcKeyFunctions.SignMessage(const &message: TBytes;
const PrivateKey: TBytes; AKeyType: TKeyType): TBytes;
var
LSigner: ISigner;
LRecreatedPrivKey: IECPrivateKeyParameters;
LCurve: IX9ECParameters;
domain: IECDomainParameters;
begin
LCurve := GetCurve(AKeyType);
domain := GetDomain(LCurve);
LRecreatedPrivKey := TECPrivateKeyParameters.Create('ECDSA',
TBigInteger.Create(1, PrivateKey), domain);
LSigner := GetSigner();
LSigner.Init(True, LRecreatedPrivKey);
LSigner.BlockUpdate(&message, 0, System.Length(&message));
Result := LSigner.GenerateSignature();
end;
class function TbtcKeyFunctions.VerifySignature(const signature: TBytes;
const &message: TBytes; const PublicKey: TBytes; AKeyType: TKeyType): Boolean;
var
LSigner: ISigner;
LRecreatedPubKey: IECPublicKeyParameters;
LCurve: IX9ECParameters;
domain: IECDomainParameters;
begin
LCurve := GetCurve(AKeyType);
domain := GetDomain(LCurve);
LRecreatedPubKey := TECPublicKeyParameters.Create('ECDSA',
LCurve.curve.DecodePoint(PublicKey), domain);
LSigner := GetSigner();
LSigner.Init(False, LRecreatedPubKey);
LSigner.BlockUpdate(&message, 0, System.Length(&message));
Result := LSigner.VerifySignature(signature);
end;
class function TbtcKeyFunctions.GenerateECKeyPair(AKeyType: TKeyType): TKeyPair;
var
LCurve: IX9ECParameters;
domain: IECDomainParameters;
KeyPairGeneratorInstance: IAsymmetricCipherKeyPairGenerator;
askp: IAsymmetricCipherKeyPair;
begin
LCurve := GetCurve(AKeyType);
domain := GetDomain(LCurve);
KeyPairGeneratorInstance := TGeneratorUtilities.GetKeyPairGenerator('ECDSA');
KeyPairGeneratorInstance.Init(TECKeyGenerationParameters.Create(domain,
SecureRandom) as IECKeyGenerationParameters);
askp := KeyPairGeneratorInstance.GenerateKeyPair();
Result.PrivateKey := (askp.Private as IECPrivateKeyParameters)
.D.ToByteArrayUnsigned;
Result.PublicKey := (askp.Public as IECPublicKeyParameters).Q.GetEncoded();
end;
//
//
//
// Starting my own functions from here down. All of the above
// was copied from an example I found on the Lazarus forums
// from xor-el. Below I am trying to implement my own code to
// share with others that want to implement bitcoin functions in
// their applications.
//
//
//
class function TbtcKeyFunctions.GenPubKeyFromPvtInput(AKeyType: TKeyType
): TKeyPair;
var
LCurve: IX9ECParameters;
domain: IECDomainParameters;
KeyPairGeneratorInstance: IAsymmetricCipherKeyPairGenerator;
askp: IAsymmetricCipherKeyPair;
begin
LCurve := GetCurve(AKeyType);
domain := GetDomain(LCurve);
KeyPairGeneratorInstance := TGeneratorUtilities.GetKeyPairGenerator('ECDSA');
KeyPairGeneratorInstance.Init(TECKeyGenerationParameters.Create(domain,
SecureRandom) as IECKeyGenerationParameters);
askp := KeyPairGeneratorInstance.GenerateKeyPair();
Result.PrivateKey := (askp.Private as IECPrivateKeyParameters)
.D.ToByteArrayUnsigned;
Result.PublicKey := (askp.Public as IECPublicKeyParameters).Q.GetEncoded();
end;
class function TbtcKeyFunctions.GenerateValidRandomBytesForPrivateKey(): String;
begin
Result := THex.Encode(TBitcoinKey.GenerateValidRandomBytesForPrivateKey());
end;
class procedure TbtcKeyFunctions.GetPublicKeyDetails(const APrivateKey: String;
isCompressed: Boolean; var Address, PubKey: String);
var
Key: IBitcoinKey;
begin
Key := TBitcoinKey.Create(THex.Decode(APrivateKey), -1, isCompressed);
Address := Key.GeneratePublicAddress(Key.IsCompressed);
PubKey := THex.Encode(Key.PublicKey.Q.Normalize.GetEncoded(Key.IsCompressed));
end;
class function TbtcKeyFunctions.GetPrivateKeyWIF(const APrivateKey: String;
isCompressed: Boolean): String;
var
Key: IBitcoinKey;
begin
Key := TBitcoinKey.Create(THex.Decode(APrivateKey), -1, isCompressed);
Result := Key.GenerateWIFPrivateKey(Key.IsCompressed);
end;
end.