-
Notifications
You must be signed in to change notification settings - Fork 20
/
Address.cs
383 lines (342 loc) · 16.1 KB
/
Address.cs
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// Autarkysoft.Bitcoin
// Copyright (c) 2020 Autarkysoft
// Distributed under the MIT software license, see the accompanying
// file LICENCE or http://www.opensource.org/licenses/mit-license.php.
using Autarkysoft.Bitcoin.Blockchain.Scripts;
using Autarkysoft.Bitcoin.Cryptography.Asymmetric.KeyPairs;
using Autarkysoft.Bitcoin.Cryptography.Hashing;
using System;
namespace Autarkysoft.Bitcoin.Encoders
{
/// <summary>
/// Provides methods to check and create bitcoin addresses from public keys.
/// </summary>
public class Address
{
private const byte P2pkhVerMainNet = 0;
private const byte P2pkhVerTestNet = 111;
private const byte P2pkhVerRegTest = 0;
private const byte P2shVerMainNet = 5;
private const byte P2shVerTestNet = 196;
private const byte P2shVerRegTest = 5;
private const string HrpMainNet = "bc";
private const string HrpTestNet = "tb";
private const string HrpRegTest = "bcrt";
private readonly Base58 b58Encoder = new Base58();
private readonly Bech32 b32Encoder = new Bech32();
/// <summary>
/// Address type, there are currently 4 defined types
/// </summary>
public enum AddressType
{
/// <summary>
/// Unknown or invalid address
/// </summary>
Unknown,
/// <summary>
/// Pay to public key hash
/// </summary>
P2PKH,
/// <summary>
/// Pay to script hash
/// </summary>
P2SH,
/// <summary>
/// Pay to witness public key hash
/// </summary>
P2WPKH,
/// <summary>
/// Pay to witness script hash
/// </summary>
P2WSH
}
/// <summary>
/// Returns the type of the given address
/// </summary>
/// <param name="address">Address string to check</param>
/// <param name="netType">Network type</param>
/// <returns>Address type</returns>
public AddressType GetAddressType(string address, NetworkType netType)
{
if (string.IsNullOrWhiteSpace(address))
{
return AddressType.Unknown;
}
else if (b58Encoder.IsValid(address))
{
byte[] decoded = b58Encoder.DecodeWithCheckSum(address);
if (decoded.Length == 21)
{
if ((netType == NetworkType.MainNet && decoded[0] == P2pkhVerMainNet) ||
(netType == NetworkType.TestNet && decoded[0] == P2pkhVerTestNet) ||
(netType == NetworkType.RegTest && decoded[0] == P2pkhVerRegTest))
{
return AddressType.P2PKH;
}
else if ((netType == NetworkType.MainNet && decoded[0] == P2shVerMainNet) ||
(netType == NetworkType.TestNet && decoded[0] == P2shVerTestNet) ||
(netType == NetworkType.RegTest && decoded[0] == P2shVerRegTest))
{
return AddressType.P2SH;
}
}
}
else if (b32Encoder.IsValid(address))
{
byte[] decoded = b32Encoder.Decode(address, out byte witVer, out string hrp);
if (witVer == 0)
{
if (decoded.Length == 20 && (
(netType == NetworkType.MainNet && hrp == HrpMainNet) ||
(netType == NetworkType.TestNet && hrp == HrpTestNet) ||
(netType == NetworkType.RegTest && hrp == HrpRegTest)
))
{
return AddressType.P2WPKH;
}
else if (decoded.Length == 32 && (
(netType == NetworkType.MainNet && hrp == HrpMainNet) ||
(netType == NetworkType.TestNet && hrp == HrpTestNet) ||
(netType == NetworkType.RegTest && hrp == HrpRegTest)
))
{
return AddressType.P2WSH;
}
}
}
return AddressType.Unknown;
}
/// <summary>
/// Return the pay to public key hash address from the given <see cref="PublicKey"/>.
/// </summary>
/// <exception cref="ArgumentException"/>
/// <exception cref="ArgumentNullException"/>
/// <param name="pubk">Public key to use</param>
/// <param name="useCompressed">
/// [Default value = true]
/// Indicates wheter to use compressed or compressed public key to generate the address
/// </param>
/// <param name="netType">[Default value = <see cref="NetworkType.MainNet"/>] Network type</param>
/// <returns>The resulting address</returns>
public string GetP2pkh(PublicKey pubk, bool useCompressed = true, NetworkType netType = NetworkType.MainNet)
{
if (pubk is null)
throw new ArgumentNullException(nameof(pubk), "Public key can not be null.");
byte ver = netType switch
{
NetworkType.MainNet => P2pkhVerMainNet,
NetworkType.TestNet => P2pkhVerTestNet,
NetworkType.RegTest => P2pkhVerRegTest,
_ => throw new ArgumentException(Err.InvalidNetwork)
};
using Ripemd160Sha256 hashFunc = new Ripemd160Sha256();
byte[] data = hashFunc.ComputeHash(pubk.ToByteArray(useCompressed)).AppendToBeginning(ver);
return b58Encoder.EncodeWithCheckSum(data);
}
/// <summary>
/// Return the pay to script hash address from the given <see cref="IScript"/>.
/// </summary>
/// <exception cref="ArgumentException"/>
/// <exception cref="ArgumentNullException"/>
/// <param name="redeem">Redeem script to use</param>
/// <param name="netType">[Default value = <see cref="NetworkType.MainNet"/>] Network type</param>
/// <returns>The resulting address</returns>
public string GetP2sh(IScript redeem, NetworkType netType = NetworkType.MainNet)
{
if (redeem is null)
throw new ArgumentNullException(nameof(redeem), "Redeem script can not be null.");
byte ver = netType switch
{
NetworkType.MainNet => P2shVerMainNet,
NetworkType.TestNet => P2shVerTestNet,
NetworkType.RegTest => P2shVerRegTest,
_ => throw new ArgumentException(Err.InvalidNetwork)
};
using Ripemd160Sha256 hashFunc = new Ripemd160Sha256();
byte[] data = hashFunc.ComputeHash(redeem.Data).AppendToBeginning(ver);
return b58Encoder.EncodeWithCheckSum(data);
}
/// <summary>
/// Return the pay to witness public key hash address from the given <see cref="PublicKey"/>.
/// </summary>
/// <exception cref="ArgumentException"/>
/// <exception cref="ArgumentNullException"/>
/// <param name="pubk">Public key to use</param>
/// <param name="witVer">Witness version to use</param>
/// <param name="useCompressed">
/// [Default value = true]
/// Indicates wheter to use compressed or compressed public key to generate the address
/// <para/> Note: using uncompressed public keys makes the output non-standard and can lead to money loss.
/// </param>
/// <param name="netType">[Default value = <see cref="NetworkType.MainNet"/>] Network type</param>
/// <returns>The resulting address</returns>
public string GetP2wpkh(PublicKey pubk, byte witVer, bool useCompressed = true, NetworkType netType = NetworkType.MainNet)
{
if (pubk is null)
throw new ArgumentNullException(nameof(pubk), "Public key can not be null.");
if (witVer != 0)
throw new ArgumentException("Currently only address version 0 is defined for P2WPKH.", nameof(witVer));
string hrp = netType switch
{
NetworkType.MainNet => HrpMainNet,
NetworkType.TestNet => HrpTestNet,
NetworkType.RegTest => HrpRegTest,
_ => throw new ArgumentException(Err.InvalidNetwork),
};
using Ripemd160Sha256 hashFunc = new Ripemd160Sha256();
byte[] hash160 = hashFunc.ComputeHash(pubk.ToByteArray(useCompressed));
return b32Encoder.Encode(hash160, witVer, hrp);
}
/// <summary>
/// Return the pay to witness public key hash address from the given <see cref="PublicKey"/>.
/// </summary>
/// <exception cref="ArgumentException"/>
/// <exception cref="ArgumentNullException"/>
/// <param name="pubk">Public key to use</param>
/// <param name="witVer">Witness version to use</param>
/// <param name="useCompressed">
/// [Default value = true]
/// Indicates wheter to use compressed or compressed public key to generate the address
/// <para/> Note: using uncompressed public keys makes the output non-standard and can lead to money loss.
/// </param>
/// <param name="netType">[Default value = <see cref="NetworkType.MainNet"/>] Network type</param>
/// <returns>The resulting address</returns>
public string GetP2sh_P2wpkh(PublicKey pubk, byte witVer, bool useCompressed = true,
NetworkType netType = NetworkType.MainNet)
{
if (pubk is null)
throw new ArgumentNullException(nameof(pubk), "Public key can not be null.");
if (witVer != 0)
throw new ArgumentException("Currently only address version 0 is defined for P2WPKH.", nameof(witVer));
if (netType != NetworkType.MainNet && netType != NetworkType.TestNet && netType != NetworkType.RegTest)
throw new ArgumentException(Err.InvalidNetwork);
RedeemScript rdm = new RedeemScript();
rdm.SetToP2SH_P2WPKH(pubk, useCompressed);
return GetP2sh(rdm, netType);
}
/// <summary>
/// Return the pay to witness script hash address from the given <see cref="IScript"/>.
/// </summary>
/// <exception cref="ArgumentException"/>
/// <exception cref="ArgumentNullException"/>
/// <param name="script">Script to use</param>
/// <param name="witVer">Witness version to use</param>
/// <param name="netType">[Default value = <see cref="NetworkType.MainNet"/>] Network type</param>
/// <returns>The resulting address</returns>
public string GetP2wsh(IScript script, byte witVer, NetworkType netType = NetworkType.MainNet)
{
if (script is null)
throw new ArgumentNullException(nameof(script), "Script can not be null.");
if (witVer != 0)
throw new ArgumentException("Currently only address version 0 is defined for P2WSH.", nameof(witVer));
string hrp = netType switch
{
NetworkType.MainNet => HrpMainNet,
NetworkType.TestNet => HrpTestNet,
NetworkType.RegTest => HrpRegTest,
_ => throw new ArgumentException(Err.InvalidNetwork),
};
using Sha256 witHashFunc = new Sha256();
byte[] hash = witHashFunc.ComputeHash(script.Data);
return b32Encoder.Encode(hash, witVer, hrp);
}
/// <summary>
/// Return the pay to witness script hash wrapped in a pay to script hash address from the given <see cref="IScript"/>.
/// </summary>
/// <exception cref="ArgumentException"/>
/// <exception cref="ArgumentNullException"/>
/// <param name="script">Public key to use</param>
/// <param name="witVer">Witness version to use</param>
/// <param name="netType">[Default value = <see cref="NetworkType.MainNet"/>] Network type</param>
/// <returns>The resulting address</returns>
public string GetP2sh_P2wsh(IScript script, byte witVer, NetworkType netType = NetworkType.MainNet)
{
if (script is null)
throw new ArgumentNullException(nameof(script), "Script can not be null.");
if (witVer != 0)
throw new ArgumentException("Currently only address version 0 is defined for P2WSH-P2SH.", nameof(witVer));
if (netType != NetworkType.MainNet && netType != NetworkType.TestNet && netType != NetworkType.RegTest)
throw new ArgumentException(Err.InvalidNetwork);
RedeemScript rdm = new RedeemScript();
rdm.SetToP2SH_P2WSH(script);
return GetP2sh(rdm, netType);
}
/// <summary>
/// Checks if the given address string is of the given <see cref="PubkeyScriptType"/> type and returns the
/// decoded hash from the address.
/// </summary>
/// <param name="address">Address to check</param>
/// <param name="scrType">The public key script type to check against</param>
/// <param name="hash">The hash used in creation of this address</param>
/// <returns>True if the address is the same script type, otherwise false</returns>
public bool VerifyType(string address, PubkeyScriptType scrType, out byte[] hash)
{
hash = null;
if (string.IsNullOrWhiteSpace(address))
{
return false;
}
switch (scrType)
{
case PubkeyScriptType.P2PKH:
if (!b58Encoder.IsValid(address))
{
return false;
}
byte[] decoded = b58Encoder.DecodeWithCheckSum(address);
if (decoded[0] != P2pkhVerMainNet &&
decoded[0] != P2pkhVerTestNet &&
decoded[0] != P2pkhVerRegTest ||
decoded.Length != 21)
{
return false;
}
hash = decoded.SubArray(1);
return true;
case PubkeyScriptType.P2SH:
if (!b58Encoder.IsValid(address))
{
return false;
}
decoded = b58Encoder.DecodeWithCheckSum(address);
if (decoded[0] != P2shVerMainNet &&
decoded[0] != P2shVerTestNet &&
decoded[0] != P2shVerRegTest ||
decoded.Length != 21)
{
return false;
}
hash = decoded.SubArray(1);
return true;
case PubkeyScriptType.P2WPKH:
if (!b32Encoder.IsValid(address))
{
return false;
}
decoded = b32Encoder.Decode(address, out byte witVer, out string hrp);
if (witVer != 0 || decoded.Length != 20 ||
(hrp != HrpMainNet && hrp != HrpTestNet && hrp != HrpRegTest))
{
return false;
}
hash = decoded;
return true;
case PubkeyScriptType.P2WSH:
if (!b32Encoder.IsValid(address))
{
return false;
}
decoded = b32Encoder.Decode(address, out witVer, out hrp);
if (witVer != 0 || decoded.Length != 32 ||
(hrp != HrpMainNet && hrp != HrpTestNet && hrp != HrpRegTest))
{
return false;
}
hash = decoded;
return true;
default:
return false;
}
}
}
}