-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Wallet.cs
497 lines (456 loc) · 22.4 KB
/
Wallet.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
using Neo.Cryptography;
using Neo.IO;
using Neo.Ledger;
using Neo.Network.P2P.Payloads;
using Neo.Persistence;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.SmartContract.Native;
using Neo.VM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using static Neo.Wallets.Helper;
using ECPoint = Neo.Cryptography.ECC.ECPoint;
namespace Neo.Wallets
{
public abstract class Wallet
{
public abstract string Name { get; }
public string Path { get; }
public abstract Version Version { get; }
public abstract bool ChangePassword(string oldPassword, string newPassword);
public abstract bool Contains(UInt160 scriptHash);
public abstract WalletAccount CreateAccount(byte[] privateKey);
public abstract WalletAccount CreateAccount(Contract contract, KeyPair key = null);
public abstract WalletAccount CreateAccount(UInt160 scriptHash);
public abstract bool DeleteAccount(UInt160 scriptHash);
public abstract WalletAccount GetAccount(UInt160 scriptHash);
public abstract IEnumerable<WalletAccount> GetAccounts();
internal Wallet()
{
}
protected Wallet(string path)
{
this.Path = path;
}
public WalletAccount CreateAccount()
{
byte[] privateKey = new byte[32];
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
{
rng.GetBytes(privateKey);
}
WalletAccount account = CreateAccount(privateKey);
Array.Clear(privateKey, 0, privateKey.Length);
return account;
}
public WalletAccount CreateAccount(Contract contract, byte[] privateKey)
{
if (privateKey == null) return CreateAccount(contract);
return CreateAccount(contract, new KeyPair(privateKey));
}
private List<(UInt160 Account, BigInteger Value)> FindPayingAccounts(List<(UInt160 Account, BigInteger Value)> orderedAccounts, BigInteger amount)
{
var result = new List<(UInt160 Account, BigInteger Value)>();
BigInteger sum_balance = orderedAccounts.Select(p => p.Value).Sum();
if (sum_balance == amount)
{
result.AddRange(orderedAccounts);
orderedAccounts.Clear();
}
else
{
for (int i = 0; i < orderedAccounts.Count; i++)
{
if (orderedAccounts[i].Value < amount)
continue;
if (orderedAccounts[i].Value == amount)
{
result.Add(orderedAccounts[i]);
orderedAccounts.RemoveAt(i);
}
else
{
result.Add((orderedAccounts[i].Account, amount));
orderedAccounts[i] = (orderedAccounts[i].Account, orderedAccounts[i].Value - amount);
}
break;
}
if (result.Count == 0)
{
int i = orderedAccounts.Count - 1;
while (orderedAccounts[i].Value <= amount)
{
result.Add(orderedAccounts[i]);
amount -= orderedAccounts[i].Value;
orderedAccounts.RemoveAt(i);
i--;
}
for (i = 0; i < orderedAccounts.Count; i++)
{
if (orderedAccounts[i].Value < amount)
continue;
if (orderedAccounts[i].Value == amount)
{
result.Add(orderedAccounts[i]);
orderedAccounts.RemoveAt(i);
}
else
{
result.Add((orderedAccounts[i].Account, amount));
orderedAccounts[i] = (orderedAccounts[i].Account, orderedAccounts[i].Value - amount);
}
break;
}
}
}
return result;
}
public WalletAccount GetAccount(ECPoint pubkey)
{
return GetAccount(Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash());
}
public BigDecimal GetAvailable(UInt160 asset_id)
{
UInt160[] accounts = GetAccounts().Where(p => !p.WatchOnly).Select(p => p.ScriptHash).ToArray();
return GetBalance(asset_id, accounts);
}
public BigDecimal GetBalance(UInt160 asset_id, params UInt160[] accounts)
{
byte[] script;
using (ScriptBuilder sb = new ScriptBuilder())
{
sb.EmitPush(0);
foreach (UInt160 account in accounts)
{
sb.EmitAppCall(asset_id, "balanceOf", account);
sb.Emit(OpCode.ADD);
}
sb.EmitAppCall(asset_id, "decimals");
script = sb.ToArray();
}
using ApplicationEngine engine = ApplicationEngine.Run(script, gas: 20000000L * accounts.Length);
if (engine.State.HasFlag(VMState.FAULT))
return new BigDecimal(0, 0);
byte decimals = (byte)engine.ResultStack.Pop().GetInteger();
BigInteger amount = engine.ResultStack.Pop().GetInteger();
return new BigDecimal(amount, decimals);
}
public static byte[] GetPrivateKeyFromNEP2(string nep2, string passphrase, int N = 16384, int r = 8, int p = 8)
{
if (nep2 == null) throw new ArgumentNullException(nameof(nep2));
if (passphrase == null) throw new ArgumentNullException(nameof(passphrase));
byte[] data = nep2.Base58CheckDecode();
if (data.Length != 39 || data[0] != 0x01 || data[1] != 0x42 || data[2] != 0xe0)
throw new FormatException();
byte[] addresshash = new byte[4];
Buffer.BlockCopy(data, 3, addresshash, 0, 4);
byte[] datapassphrase = Encoding.UTF8.GetBytes(passphrase);
byte[] derivedkey = SCrypt.DeriveKey(datapassphrase, addresshash, N, r, p, 64);
Array.Clear(datapassphrase, 0, datapassphrase.Length);
byte[] derivedhalf1 = derivedkey[..32];
byte[] derivedhalf2 = derivedkey[32..];
Array.Clear(derivedkey, 0, derivedkey.Length);
byte[] encryptedkey = new byte[32];
Buffer.BlockCopy(data, 7, encryptedkey, 0, 32);
Array.Clear(data, 0, data.Length);
byte[] prikey = XOR(encryptedkey.AES256Decrypt(derivedhalf2), derivedhalf1);
Array.Clear(derivedhalf1, 0, derivedhalf1.Length);
Array.Clear(derivedhalf2, 0, derivedhalf2.Length);
ECPoint pubkey = Cryptography.ECC.ECCurve.Secp256r1.G * prikey;
UInt160 script_hash = Contract.CreateSignatureRedeemScript(pubkey).ToScriptHash();
string address = script_hash.ToAddress();
if (!Encoding.ASCII.GetBytes(address).Sha256().Sha256().AsSpan(0, 4).SequenceEqual(addresshash))
throw new FormatException();
return prikey;
}
public static byte[] GetPrivateKeyFromWIF(string wif)
{
if (wif == null) throw new ArgumentNullException();
byte[] data = wif.Base58CheckDecode();
if (data.Length != 34 || data[0] != 0x80 || data[33] != 0x01)
throw new FormatException();
byte[] privateKey = new byte[32];
Buffer.BlockCopy(data, 1, privateKey, 0, privateKey.Length);
Array.Clear(data, 0, data.Length);
return privateKey;
}
private static Signer[] GetSigners(UInt160 sender, Signer[] cosigners)
{
for (int i = 0; i < cosigners.Length; i++)
{
if (cosigners[i].Account.Equals(sender))
{
if (i == 0) return cosigners;
List<Signer> list = new List<Signer>(cosigners);
list.RemoveAt(i);
list.Insert(0, cosigners[i]);
return list.ToArray();
}
}
return cosigners.Prepend(new Signer
{
Account = sender,
Scopes = WitnessScope.None
}).ToArray();
}
public virtual WalletAccount Import(X509Certificate2 cert)
{
byte[] privateKey;
using (ECDsa ecdsa = cert.GetECDsaPrivateKey())
{
privateKey = ecdsa.ExportParameters(true).D;
}
WalletAccount account = CreateAccount(privateKey);
Array.Clear(privateKey, 0, privateKey.Length);
return account;
}
public virtual WalletAccount Import(string wif)
{
byte[] privateKey = GetPrivateKeyFromWIF(wif);
WalletAccount account = CreateAccount(privateKey);
Array.Clear(privateKey, 0, privateKey.Length);
return account;
}
public virtual WalletAccount Import(string nep2, string passphrase, int N = 16384, int r = 8, int p = 8)
{
byte[] privateKey = GetPrivateKeyFromNEP2(nep2, passphrase, N, r, p);
WalletAccount account = CreateAccount(privateKey);
Array.Clear(privateKey, 0, privateKey.Length);
return account;
}
public Transaction MakeTransaction(TransferOutput[] outputs, UInt160 from = null, Signer[] cosigners = null)
{
UInt160[] accounts;
if (from is null)
{
accounts = GetAccounts().Where(p => !p.Lock && !p.WatchOnly).Select(p => p.ScriptHash).ToArray();
}
else
{
accounts = new[] { from };
}
using (SnapshotView snapshot = Blockchain.Singleton.GetSnapshot())
{
Dictionary<UInt160, Signer> cosignerList = cosigners?.ToDictionary(p => p.Account) ?? new Dictionary<UInt160, Signer>();
byte[] script;
List<(UInt160 Account, BigInteger Value)> balances_gas = null;
using (ScriptBuilder sb = new ScriptBuilder())
{
foreach (var (assetId, group, sum) in outputs.GroupBy(p => p.AssetId, (k, g) => (k, g, g.Select(p => p.Value.Value).Sum())))
{
var balances = new List<(UInt160 Account, BigInteger Value)>();
foreach (UInt160 account in accounts)
using (ScriptBuilder sb2 = new ScriptBuilder())
{
sb2.EmitAppCall(assetId, "balanceOf", account);
using (ApplicationEngine engine = ApplicationEngine.Run(sb2.ToArray(), snapshot))
{
if (engine.State.HasFlag(VMState.FAULT))
throw new InvalidOperationException($"Execution for {assetId.ToString()}.balanceOf('{account.ToString()}' fault");
BigInteger value = engine.ResultStack.Pop().GetInteger();
if (value.Sign > 0) balances.Add((account, value));
}
}
BigInteger sum_balance = balances.Select(p => p.Value).Sum();
if (sum_balance < sum)
throw new InvalidOperationException($"It does not have enough balance, expected: {sum.ToString()} found: {sum_balance.ToString()}");
foreach (TransferOutput output in group)
{
balances = balances.OrderBy(p => p.Value).ToList();
var balances_used = FindPayingAccounts(balances, output.Value.Value);
foreach (var (account, value) in balances_used)
{
if (cosignerList.TryGetValue(account, out Signer signer))
{
if (signer.Scopes != WitnessScope.Global)
signer.Scopes |= WitnessScope.CalledByEntry;
}
else
{
cosignerList.Add(account, new Signer
{
Account = account,
Scopes = WitnessScope.CalledByEntry
});
}
sb.EmitAppCall(output.AssetId, "transfer", account, output.ScriptHash, value);
sb.Emit(OpCode.ASSERT);
}
}
if (assetId.Equals(NativeContract.GAS.Hash))
balances_gas = balances;
}
script = sb.ToArray();
}
if (balances_gas is null)
balances_gas = accounts.Select(p => (Account: p, Value: NativeContract.GAS.BalanceOf(snapshot, p))).Where(p => p.Value.Sign > 0).ToList();
return MakeTransaction(snapshot, script, cosignerList.Values.ToArray(), Array.Empty<TransactionAttribute>(), balances_gas);
}
}
public Transaction MakeTransaction(byte[] script, UInt160 sender = null, Signer[] cosigners = null, TransactionAttribute[] attributes = null)
{
UInt160[] accounts;
if (sender is null)
{
accounts = GetAccounts().Where(p => !p.Lock && !p.WatchOnly).Select(p => p.ScriptHash).ToArray();
}
else
{
accounts = new[] { sender };
}
using (SnapshotView snapshot = Blockchain.Singleton.GetSnapshot())
{
var balances_gas = accounts.Select(p => (Account: p, Value: NativeContract.GAS.BalanceOf(snapshot, p))).Where(p => p.Value.Sign > 0).ToList();
return MakeTransaction(snapshot, script, cosigners ?? Array.Empty<Signer>(), attributes ?? Array.Empty<TransactionAttribute>(), balances_gas);
}
}
private Transaction MakeTransaction(StoreView snapshot, byte[] script, Signer[] cosigners, TransactionAttribute[] attributes, List<(UInt160 Account, BigInteger Value)> balances_gas)
{
Random rand = new Random();
foreach (var (account, value) in balances_gas)
{
Transaction tx = new Transaction
{
Version = 0,
Nonce = (uint)rand.Next(),
Script = script,
ValidUntilBlock = snapshot.Height + Transaction.MaxValidUntilBlockIncrement,
Signers = GetSigners(account, cosigners),
Attributes = attributes,
};
// will try to execute 'transfer' script to check if it works
using (ApplicationEngine engine = ApplicationEngine.Run(script, snapshot.Clone(), tx))
{
if (engine.State == VMState.FAULT)
{
throw new InvalidOperationException($"Failed execution for '{script.ToHexString()}'", engine.FaultException);
}
tx.SystemFee = engine.GasConsumed;
}
tx.NetworkFee = CalculateNetworkFee(snapshot, tx);
if (value >= tx.SystemFee + tx.NetworkFee) return tx;
}
throw new InvalidOperationException("Insufficient GAS");
}
public long CalculateNetworkFee(StoreView snapshot, Transaction tx)
{
UInt160[] hashes = tx.GetScriptHashesForVerifying(snapshot);
// base size for transaction: includes const_header + signers + attributes + script + hashes
int size = Transaction.HeaderSize + tx.Signers.GetVarSize() + tx.Attributes.GetVarSize() + tx.Script.GetVarSize() + IO.Helper.GetVarSize(hashes.Length);
long networkFee = 0;
foreach (UInt160 hash in hashes)
{
byte[] witness_script = GetAccount(hash)?.Contract?.Script;
if (witness_script is null && tx.Witnesses != null)
{
// Try to find the script in the witnesses
foreach (var witness in tx.Witnesses)
{
if (witness.ScriptHash == hash)
{
witness_script = witness.VerificationScript;
break;
}
}
}
if (witness_script is null)
{
var contract = snapshot.Contracts.TryGet(hash);
if (contract is null) continue;
// Empty invocation and verification scripts
size += Array.Empty<byte>().GetVarSize() * 2;
// Check verify cost
ContractMethodDescriptor verify = contract.Manifest.Abi.GetMethod("verify");
if (verify is null) throw new ArgumentException($"The smart contract {contract.ScriptHash} haven't got verify method");
ContractMethodDescriptor init = contract.Manifest.Abi.GetMethod("_initialize");
using ApplicationEngine engine = ApplicationEngine.Create(TriggerType.Verification, tx, snapshot.Clone());
ExecutionContext context = engine.LoadScript(contract.Script, CallFlags.None, verify.Offset);
if (init != null) engine.LoadContext(context.Clone(init.Offset), false);
engine.LoadScript(Array.Empty<byte>(), CallFlags.None);
if (engine.Execute() == VMState.FAULT) throw new ArgumentException($"Smart contract {contract.ScriptHash} verification fault.");
if (engine.ResultStack.Count != 1 || !engine.ResultStack.Pop().GetBoolean()) throw new ArgumentException($"Smart contract {contract.ScriptHash} returns false.");
networkFee += engine.GasConsumed;
}
else if (witness_script.IsSignatureContract())
{
size += 67 + witness_script.GetVarSize();
networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] + ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] + ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.ECDsaVerifyPrice;
}
else if (witness_script.IsMultiSigContract(out int m, out int n))
{
int size_inv = 66 * m;
size += IO.Helper.GetVarSize(size_inv) + size_inv + witness_script.GetVarSize();
networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * m;
using (ScriptBuilder sb = new ScriptBuilder())
networkFee += ApplicationEngine.OpCodePrices[(OpCode)sb.EmitPush(m).ToArray()[0]];
networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHDATA1] * n;
using (ScriptBuilder sb = new ScriptBuilder())
networkFee += ApplicationEngine.OpCodePrices[(OpCode)sb.EmitPush(n).ToArray()[0]];
networkFee += ApplicationEngine.OpCodePrices[OpCode.PUSHNULL] + ApplicationEngine.ECDsaVerifyPrice * n;
}
else
{
//We can support more contract types in the future.
}
}
networkFee += size * NativeContract.Policy.GetFeePerByte(snapshot);
return networkFee;
}
public bool Sign(ContractParametersContext context)
{
bool fSuccess = false;
foreach (UInt160 scriptHash in context.ScriptHashes)
{
WalletAccount account = GetAccount(scriptHash);
if (account != null)
{
// Try to sign self-contained multiSig
Contract multiSigContract = account.Contract;
if (multiSigContract != null &&
multiSigContract.Script.IsMultiSigContract(out int m, out ECPoint[] points))
{
foreach (var point in points)
{
account = GetAccount(point);
if (account?.HasKey != true) continue;
KeyPair key = account.GetKey();
byte[] signature = context.Verifiable.Sign(key);
fSuccess |= context.AddSignature(multiSigContract, key.PublicKey, signature);
if (fSuccess) m--;
if (context.Completed || m <= 0) break;
}
continue;
}
else if (account.HasKey)
{
// Try to sign with regular accounts
KeyPair key = account.GetKey();
byte[] signature = context.Verifiable.Sign(key);
fSuccess |= context.AddSignature(account.Contract, key.PublicKey, signature);
continue;
}
}
// Try Smart contract verification
using var snapshot = Blockchain.Singleton.GetSnapshot();
var contract = snapshot.Contracts.TryGet(scriptHash);
if (contract != null)
{
var deployed = new DeployedContract(contract);
// Only works with verify without parameters
if (deployed.ParameterList.Length == 0)
{
fSuccess |= context.Add(deployed);
}
}
}
return fSuccess;
}
public abstract bool VerifyPassword(string password);
}
}