-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
web3.bip39.pas
217 lines (189 loc) · 6.48 KB
/
web3.bip39.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
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2022 Stefan van As <[email protected]> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under GNU AGPL v3.0 with Commons Clause }
{ }
{ This program is free software: you can redistribute it and/or modify }
{ it under the terms of the GNU Affero General Public License as published }
{ by the Free Software Foundation, either version 3 of the License, or }
{ (at your option) any later version. }
{ }
{ This program is distributed in the hope that it will be useful, }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the }
{ GNU Affero General Public License for more details. }
{ }
{ You should have received a copy of the GNU Affero General Public License }
{ along with this program. If not, see <https://www.gnu.org/licenses/> }
{ }
{******************************************************************************}
unit web3.bip39;
interface
uses
// Delphi
System.Classes,
System.SysUtils;
type
IWordList = interface
function Count: Integer;
function Get(const Index: Integer): string;
procedure LoadFromStream(const Stream: TStream; const Encoding: TEncoding);
end;
TMnemonic = record
strict private
FEntropy: TArray<Integer>;
class function from8bitTo11bit(const input: TBytes): TArray<Integer>; static;
public
constructor Create(entropy: TBytes);
class function English: IWordList; static;
function ToString(const wordlist: IWordList): string;
end;
TWords = (Twelve, Fifteen, Eighteen, TwentyOne, TwentyFour);
TSeed = TBytes;
function create: TMnemonic; overload;
function create(const length: TWords): TMnemonic; overload;
function seed(const sentence, passphrase: string): TSeed;
implementation
uses
// Delphi
System.Types,
// CryptoLib4Pascal
ClpConverters,
ClpDigestUtilities,
ClpIKeyParameter,
ClpPkcs5S2ParametersGenerator,
ClpSecureRandom,
// web3
web3.utils;
{$R 'web3.bip39.res'}
// generate a random 15-word mnemonic
function create: TMnemonic;
begin
Result := create(Fifteen);
end;
function create(const length: TWords): TMnemonic;
const
ENTROPY: array[TWords] of Int32 = (
128, // 12 words
160, // 15 words
192, // 18 words
224, // 21 words
256 // 24 words
);
begin
const rng = TSecureRandom.Create;
try
Result := TMnemonic.Create(rng.GenerateSeed(ENTROPY[length] div 8));
finally
rng.Free;
end;
end;
// from mnemonic sentence to 64-byte seed. please note the password is optional.
function seed(const sentence, passphrase: string): TSeed;
begin
const generator = TPkcs5S2ParametersGenerator.Create(TDigestUtilities.GetDigest('SHA-512'));
try
const password = TConverters.ConvertStringToBytes(sentence, TEncoding.UTF8);
const salt = TConverters.ConvertStringToBytes('mnemonic' + passphrase, TEncoding.UTF8);
generator.Init(password, salt, 2048);
Result := (generator.GenerateDerivedMacParameters(512) as IKeyParameter).GetKey;
finally
generator.Free;
end;
end;
{ TWordList }
type
TWordList = class(TInterfacedObject, IWordList)
strict private
Inner: TStrings;
public
constructor Create;
destructor Destroy; override;
function Count: Integer;
function Get(const Index: Integer): string;
procedure LoadFromStream(const Stream: TStream; const Encoding: TEncoding);
end;
constructor TWordList.Create;
begin
inherited Create;
Inner := TStringList.Create;
end;
destructor TWordList.Destroy;
begin
if Assigned(Inner) then Inner.Free;
inherited Destroy;
end;
function TWordList.Count: Integer;
begin
if Assigned(Inner) then Result := Inner.Count else Result := 0;
end;
function TWordList.Get(const Index: Integer): string;
begin
Result := Inner[Index];
end;
procedure TWordList.LoadFromStream(const Stream: TStream; const Encoding: TEncoding);
begin
Inner.LoadFromStream(Stream, Encoding);
end;
{ TMnemonic }
constructor TMnemonic.Create(entropy: TBytes);
begin
// checksum is the 1st byte of the SHA256 digest
const checksum = web3.utils.sha256(entropy)[0];
// reserve 1 extra byte for the checksum
SetLength(entropy, Length(entropy) + 1);
entropy[High(entropy)] := checksum;
// split entropy in groups of 11 bits, each encoding a number from 0-2047
Self.FEntropy := TMnemonic.from8bitTo11bit(entropy);
end;
// split entropy in groups of 11 bits, each encoding a number from 0-2047, serving as an index into a wordlist
class function TMnemonic.from8bitTo11bit(const input: TBytes): TArray<Integer>;
begin
const len = (Length(input) * 8) div 11;
SetLength(Result, len);
var mnemAnd := 1024;
var idx_output := 0;
for var idx_input := 0 to High(input) do
begin
var byteAnd := 128;
for var I := 0 to 7 do
begin
if (input[idx_input] and byteAnd) = byteAnd then
Result[idx_output] := Result[idx_output] or mnemAnd;
mnemAnd := mnemAnd div 2;
byteAnd := byteAnd div 2;
if mnemAnd < 1 then
begin
mnemAnd := 1024;
Inc(idx_output);
if idx_output >= len then
EXIT;
end;
end;
end;
end;
class function TMnemonic.English: IWordList;
begin
Result := TWordList.Create;
const RS = TResourceStream.Create(hInstance, 'BIP39_ENGLISH_WORDLIST', RT_RCDATA);
try
Result.LoadFromStream(RS, TEncoding.UTF8);
finally
RS.Free;
end;
end;
// convert entropy-in-groups-of-11-bits to mnemonic sentence
function TMnemonic.ToString(const wordlist: IWordList): string;
begin
Result := '';
for var I := 0 to High(FEntropy) do
begin
if Result <> '' then Result := Result + ' ';
Result := Result + wordlist.Get(FEntropy[I]);
end;
end;
end.