-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathInvestmentIntegrationsTests.cs
More file actions
717 lines (561 loc) · 37.2 KB
/
InvestmentIntegrationsTests.cs
File metadata and controls
717 lines (561 loc) · 37.2 KB
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
using Angor.Shared;
using Angor.Shared.Models;
using Angor.Shared.Networks;
using Angor.Shared.ProtocolNew;
using Angor.Shared.ProtocolNew.Scripts;
using Angor.Shared.ProtocolNew.TransactionBuilders;
using Angor.Test.DataBuilders;
using Blockcore.Consensus.ScriptInfo;
using Blockcore.Consensus.TransactionInfo;
using Blockcore.NBitcoin;
using Blockcore.NBitcoin.Crypto;
using Blockcore.NBitcoin.DataEncoders;
using Blockcore.Networks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using NBitcoin;
using NBitcoin.Policy;
using Coin = Blockcore.NBitcoin.Coin;
using Key = Blockcore.NBitcoin.Key;
using Money = Blockcore.NBitcoin.Money;
using MoneyUnit = NBitcoin.MoneyUnit;
using OutPoint = NBitcoin.OutPoint;
using Script = Blockcore.Consensus.ScriptInfo.Script;
using Transaction = NBitcoin.Transaction;
using uint256 = Blockcore.NBitcoin.uint256;
namespace Angor.Test
{
public class InvestmentIntegrationsTests
{
private Mock<IWalletOperations> _walletOperations;
private Mock<INetworkConfiguration> _networkConfiguration;
private DerivationOperations _derivationOperations;
private SeederTransactionActions _seederTransactionActions;
private InvestorTransactionActions _investorTransactionActions;
private FounderTransactionActions _founderTransactionActions;
private string angorRootKey =
"tpubD8JfN1evVWPoJmLgVg6Usq2HEW9tLqm6CyECAADnH5tyQosrL6NuhpL9X1cQCbSmndVrgLSGGdbRqLfUbE6cRqUbrHtDJgSyQEY2Uu7WwTL";
private FeeEstimation _expectedFeeEstimation = new FeeEstimation()
{ Confirmations = 1, FeeRate = 10000 };
public InvestmentIntegrationsTests()
{
_walletOperations = new Mock<IWalletOperations>();
_walletOperations.Setup(_ => _.GetFeeEstimationAsync())
.ReturnsAsync(new List<FeeEstimation> { _expectedFeeEstimation });
_walletOperations.Setup(_ => _.GetUnspentOutputsForTransaction(It.IsAny<WalletWords>(),
It.IsAny<List<UtxoDataWithPath>>()))
.Returns<WalletWords, List<UtxoDataWithPath>>((_, _) =>
{
var network = Networks.Bitcoin.Testnet();
// create a fake inputTrx
var fakeInputTrx = network.Consensus.ConsensusFactory.CreateTransaction();
var fakeInputKey = new Key();
var fakeTxout = fakeInputTrx.AddOutput(Money.Parse("20.2"), fakeInputKey.ScriptPubKey);
var keys = new List<Key> { fakeInputKey };
var coins = keys.Select(key => new Coin(fakeInputTrx, fakeTxout)).ToList();
return (coins, keys);
});
_networkConfiguration = new Mock<INetworkConfiguration>();
_networkConfiguration.Setup(_ => _.GetNetwork())
.Returns(Networks.Bitcoin.Testnet());
_derivationOperations = new DerivationOperations(new HdOperations(), new NullLogger<DerivationOperations>(), _networkConfiguration.Object);
_seederTransactionActions = new SeederTransactionActions(new NullLogger<SeederTransactionActions>(),
new InvestmentScriptBuilder(new SeederScriptTreeBuilder()),
new ProjectScriptsBuilder(_derivationOperations),
new SpendingTransactionBuilder(_networkConfiguration.Object,
new ProjectScriptsBuilder(_derivationOperations),
new InvestmentScriptBuilder(new SeederScriptTreeBuilder())),
new InvestmentTransactionBuilder(_networkConfiguration.Object,
new ProjectScriptsBuilder(_derivationOperations), new InvestmentScriptBuilder(new SeederScriptTreeBuilder()), new TaprootScriptBuilder()),
new TaprootScriptBuilder(), _networkConfiguration.Object);
_investorTransactionActions = new InvestorTransactionActions(new NullLogger<InvestorTransactionActions>(),
new InvestmentScriptBuilder(new SeederScriptTreeBuilder()),
new ProjectScriptsBuilder(_derivationOperations),
new SpendingTransactionBuilder(_networkConfiguration.Object,
new ProjectScriptsBuilder(_derivationOperations),
new InvestmentScriptBuilder(new SeederScriptTreeBuilder())),
new InvestmentTransactionBuilder(_networkConfiguration.Object,
new ProjectScriptsBuilder(_derivationOperations),
new InvestmentScriptBuilder(new SeederScriptTreeBuilder()), new TaprootScriptBuilder()),
new TaprootScriptBuilder(), _networkConfiguration.Object);
_founderTransactionActions = new FounderTransactionActions(new NullLogger<FounderTransactionActions>(), _networkConfiguration.Object,
new ProjectScriptsBuilder(_derivationOperations),
new InvestmentScriptBuilder(new SeederScriptTreeBuilder()), new TaprootScriptBuilder());
}
[Fact]
public void SpendFounderStage_Test()
{
var network = Networks.Bitcoin.Testnet();
var words = new WalletWords { Words = new Mnemonic(Wordlist.English, WordCount.Twelve).ToString() };
var funderKey = _derivationOperations.DeriveFounderPrivateKey(words, 1);
var funderReceiveCoinsKey = new Key();
var projectInvestmentInfo = new ProjectInfo();
projectInvestmentInfo.TargetAmount = 3;
projectInvestmentInfo.StartDate = DateTime.UtcNow;
projectInvestmentInfo.ExpiryDate = DateTime.UtcNow.AddDays(5);
projectInvestmentInfo.Stages = new List<Stage>
{
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(1) },
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(2) },
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(3) }
};
projectInvestmentInfo.FounderKey = _derivationOperations.DeriveFounderKey(words, 1);
projectInvestmentInfo.FounderRecoveryKey = _derivationOperations.DeriveFounderRecoveryKey(words, 1);
projectInvestmentInfo.ProjectIdentifier =
_derivationOperations.DeriveAngorKey(projectInvestmentInfo.FounderKey, angorRootKey);
// Create the seeder 1 params
var seeder1Key = new Key();
var seeder1secret = new Key();
var seeder1ChangeKey = new Key();
InvestorContext seeder1Context = new InvestorContext() { ProjectInfo = projectInvestmentInfo };
seeder1Context.InvestorKey = Encoders.Hex.EncodeData(seeder1Key.PubKey.ToBytes());
seeder1Context.ChangeAddress = seeder1ChangeKey.PubKey.GetSegwitAddress(network).ToString();
seeder1Context.InvestorSecretHash = Hashes.Hash256(seeder1secret.ToBytes()).ToString();
// Create the seeder 2 params
var seeder2Key = new Key();
var seeder2secret = new Key();
var seeder2ChangeKey = new Key();
InvestorContext seeder2Context = new InvestorContext() { ProjectInfo = projectInvestmentInfo };
seeder2Context.InvestorKey = Encoders.Hex.EncodeData(seeder2Key.PubKey.ToBytes());
seeder2Context.ChangeAddress = seeder2ChangeKey.PubKey.GetSegwitAddress(network).ToString();
seeder2Context.InvestorSecretHash = Hashes.Hash256(seeder2secret.ToBytes()).ToString();
// Create the seeder 3 params
var seeder3Key = new Key();
var seeder3secret = new Key();
var seeder3ChangeKey = new Key();
InvestorContext seeder3Context = new InvestorContext() { ProjectInfo = projectInvestmentInfo };
seeder3Context.InvestorKey = Encoders.Hex.EncodeData(seeder3Key.PubKey.ToBytes());
seeder3Context.ChangeAddress = seeder3ChangeKey.PubKey.GetSegwitAddress(network).ToString();
seeder3Context.InvestorSecretHash = Hashes.Hash256(seeder3secret.ToBytes()).ToString();
// Build seeders hashes
ProjectSeeders projectSeeders = new ProjectSeeders();
projectSeeders.Threshold = 2;
projectSeeders.SecretHashes.Add(seeder1Context.InvestorSecretHash);
projectSeeders.SecretHashes.Add(seeder2Context.InvestorSecretHash);
projectSeeders.SecretHashes.Add(seeder3Context.InvestorSecretHash);
projectInvestmentInfo.ProjectSeeders = projectSeeders;
// Create the investor 1 params
var investor1Key = new Key();
var investor1ChangeKey = new Key();
InvestorContext investor1Context = new InvestorContext() { ProjectInfo = projectInvestmentInfo };
investor1Context.InvestorKey = Encoders.Hex.EncodeData(investor1Key.PubKey.ToBytes());
investor1Context.ChangeAddress = investor1ChangeKey.PubKey.GetSegwitAddress(network).ToString();
// Create the investor 2 params
var investor2Key = new Key();
var investor2ChangeKey = new Key();
InvestorContext investor2Context = new InvestorContext() { ProjectInfo = projectInvestmentInfo };
investor2Context.InvestorKey = Encoders.Hex.EncodeData(investor2Key.PubKey.ToBytes());
investor2Context.ChangeAddress = investor2ChangeKey.PubKey.GetSegwitAddress(network).ToString();
// create founder context
FounderContext founderContext = new FounderContext
{ ProjectInfo = projectInvestmentInfo, ProjectSeeders = projectSeeders };
// create seeder1 investment transaction
var seeder1InvTrx = _seederTransactionActions.CreateInvestmentTransaction(projectInvestmentInfo,
seeder1Context.InvestorKey, new uint256(seeder1Context.InvestorSecretHash), Money.Coins(projectInvestmentInfo.TargetAmount).Satoshi);
founderContext.InvestmentTrasnactionsHex.Add(seeder1InvTrx.ToHex());
// create seeder2 investment transaction
var seeder2InvTrx = _seederTransactionActions.CreateInvestmentTransaction(projectInvestmentInfo,
seeder2Context.InvestorKey, new uint256(seeder2Context.InvestorSecretHash), Money.Coins(projectInvestmentInfo.TargetAmount).Satoshi);
founderContext.InvestmentTrasnactionsHex.Add(seeder2InvTrx.ToHex());
// create seeder3 investment transaction
var seeder3InvTrx = _seederTransactionActions.CreateInvestmentTransaction(projectInvestmentInfo,
seeder3Context.InvestorKey, new uint256(seeder3Context.InvestorSecretHash), Money.Coins(projectInvestmentInfo.TargetAmount).Satoshi);
founderContext.InvestmentTrasnactionsHex.Add(seeder3InvTrx.ToHex());
// create investor 1 investment transaction
var investor1InvTrx = _investorTransactionActions.CreateInvestmentTransaction(projectInvestmentInfo,
investor1Context.InvestorKey, Money.Coins(projectInvestmentInfo.TargetAmount).Satoshi);
founderContext.InvestmentTrasnactionsHex.Add(investor1InvTrx.ToHex());
// create investor 2 investment transaction
var investor2InvTrx = _investorTransactionActions.CreateInvestmentTransaction(projectInvestmentInfo,
investor2Context.InvestorKey, Money.Coins(projectInvestmentInfo.TargetAmount).Satoshi);
founderContext.InvestmentTrasnactionsHex.Add(investor2InvTrx.ToHex());
// spend all investment transactions for stage 1
var founderTrxForSeeder1Stage1 = _founderTransactionActions.SpendFounderStage(projectInvestmentInfo,
founderContext.InvestmentTrasnactionsHex, 1,
funderReceiveCoinsKey.PubKey.ScriptPubKey, Encoders.Hex.EncodeData(funderKey.ToBytes())
, _expectedFeeEstimation);
TransactionValidation.ThanTheTransactionHasNoErrors(founderTrxForSeeder1Stage1.Transaction,
founderContext.InvestmentTrasnactionsHex.Select(_ => Networks.Bitcoin.Testnet().CreateTransaction(_))
.SelectMany(_ => _.Outputs.AsCoins().Where(c => c.Amount > 0)));
}
[Fact]
public void SeederTransaction_EndOfProject_Test()
{
var network = Networks.Bitcoin.Testnet();
var words = new WalletWords { Words = new Mnemonic(Wordlist.English, WordCount.Twelve).ToString() };
var projectInvestmentInfo = new ProjectInfo();
projectInvestmentInfo.TargetAmount = 3;
projectInvestmentInfo.StartDate = DateTime.UtcNow;
projectInvestmentInfo.ExpiryDate = DateTime.UtcNow.AddDays(5);
projectInvestmentInfo.Stages = new List<Stage>
{
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(1) },
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(2) },
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(3) }
};
projectInvestmentInfo.FounderKey = _derivationOperations.DeriveFounderKey(words, 1);
projectInvestmentInfo.FounderRecoveryKey = _derivationOperations.DeriveFounderRecoveryKey(words, 1);
projectInvestmentInfo.ProjectIdentifier =
_derivationOperations.DeriveAngorKey(projectInvestmentInfo.FounderKey, angorRootKey);
projectInvestmentInfo.PenaltyDays = 180;
// Create the seeder 1 params
var seeder11Key = new Key();
var seeder1secret = new Key();
var seeder1ChangeKey = new Key();
var seeder1ReceiveCoinsKey = new Key();
InvestorContext seeder1Context = new InvestorContext() { ProjectInfo = projectInvestmentInfo };
seeder1Context.InvestorKey = Encoders.Hex.EncodeData(seeder11Key.PubKey.ToBytes());
seeder1Context.ChangeAddress = seeder1ChangeKey.PubKey.GetSegwitAddress(network).ToString();
seeder1Context.InvestorSecretHash = Hashes.Hash256(seeder1secret.ToBytes()).ToString();
// create the investment transaction
var seeder1InvTrx = _seederTransactionActions.CreateInvestmentTransaction(projectInvestmentInfo,seeder1Context.InvestorKey,
new uint256(seeder1Context.InvestorSecretHash), Money.Coins(projectInvestmentInfo.TargetAmount).Satoshi);
var seeder1Expierytrx = _seederTransactionActions.RecoverEndOfProjectFunds(seeder1InvTrx.ToHex(), projectInvestmentInfo,
1, seeder1ReceiveCoinsKey.PubKey.ScriptPubKey.WitHash.GetAddress(network).ToString(),
Encoders.Hex.EncodeData(seeder11Key.ToBytes()), _expectedFeeEstimation);
Assert.NotNull(seeder1Expierytrx);
TransactionValidation.ThanTheTransactionHasNoErrors(seeder1Expierytrx.Transaction,
seeder1InvTrx.Outputs.AsCoins().Where(c => c.Amount > 0));
}
[Fact]
public void InvestorTransaction_EndOfProject_Test()
{
DerivationOperations derivationOperations = new DerivationOperations(new HdOperations(),
new NullLogger<DerivationOperations>(), _networkConfiguration.Object);
InvestmentOperations operations = new InvestmentOperations(_walletOperations.Object, derivationOperations);
var network = Networks.Bitcoin.Testnet();
var words = new WalletWords { Words = new Mnemonic(Wordlist.English, WordCount.Twelve).ToString() };
var projectInvestmentInfo = new ProjectInfo();
projectInvestmentInfo.TargetAmount = 3;
projectInvestmentInfo.StartDate = DateTime.UtcNow;
projectInvestmentInfo.ExpiryDate = DateTime.UtcNow.AddDays(5);
projectInvestmentInfo.Stages = new List<Stage>
{
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(1) },
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(2) },
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(3) }
};
projectInvestmentInfo.FounderKey = derivationOperations.DeriveFounderKey(words, 1);
projectInvestmentInfo.FounderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, 1);
projectInvestmentInfo.ProjectIdentifier =
derivationOperations.DeriveAngorKey(projectInvestmentInfo.FounderKey, angorRootKey);
projectInvestmentInfo.ProjectSeeders = new ProjectSeeders();
// Create the seeder 1 params
var seeder11Key = new Key();
var seeder1ChangeKey = new Key();
var seeder1ReceiveCoinsKey = new Key();
InvestorContext seeder1Context = new InvestorContext() { ProjectInfo = projectInvestmentInfo };
seeder1Context.InvestorKey = Encoders.Hex.EncodeData(seeder11Key.PubKey.ToBytes());
seeder1Context.ChangeAddress = seeder1ChangeKey.PubKey.GetSegwitAddress(network).ToString();
// create the investment transaction
var investorInvTrx = _investorTransactionActions.CreateInvestmentTransaction(projectInvestmentInfo,seeder1Context.InvestorKey,
Money.Coins(projectInvestmentInfo.TargetAmount).Satoshi);
var investor1Expierytrx = _investorTransactionActions.RecoverEndOfProjectFunds(investorInvTrx.ToHex(),
projectInvestmentInfo,
1, seeder1ReceiveCoinsKey.PubKey.ScriptPubKey.WitHash.GetAddress(network).ToString(),
Encoders.Hex.EncodeData(seeder11Key.ToBytes()), _expectedFeeEstimation);
Assert.NotNull(investor1Expierytrx);
TransactionValidation.ThanTheTransactionHasNoErrors(investor1Expierytrx.Transaction,
investorInvTrx.Outputs.AsCoins().Where(c => c.Amount > 0));
}
[Fact]
public void InvestorTransaction_WithSeederHashes_EndOfProject_Test()
{
DerivationOperations derivationOperations = new DerivationOperations(new HdOperations(),
new NullLogger<DerivationOperations>(), _networkConfiguration.Object);
InvestmentOperations operations = new InvestmentOperations(_walletOperations.Object, derivationOperations);
var network = Networks.Bitcoin.Testnet();
var words = new WalletWords { Words = new Mnemonic(Wordlist.English, WordCount.Twelve).ToString() };
var projectInvestmentInfo = new ProjectInfo();
projectInvestmentInfo.TargetAmount = 3;
projectInvestmentInfo.StartDate = DateTime.UtcNow;
projectInvestmentInfo.ExpiryDate = DateTime.UtcNow.AddDays(5);
projectInvestmentInfo.Stages = new List<Stage>
{
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(1) },
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(2) },
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(3) }
};
projectInvestmentInfo.FounderKey = derivationOperations.DeriveFounderKey(words, 1);
projectInvestmentInfo.FounderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, 1);
projectInvestmentInfo.ProjectIdentifier =
derivationOperations.DeriveAngorKey(projectInvestmentInfo.FounderKey, angorRootKey);
projectInvestmentInfo.ProjectSeeders = new ProjectSeeders();
// Create the seeder 1 params
var investorKey = new Key();
var investorChangeKey = new Key();
var investorReceiveCoinsKey = new Key();
ProjectSeeders projectSeeders = new ProjectSeeders();
projectSeeders.Threshold = 2;
projectSeeders.SecretHashes.Add(Hashes.Hash256(new Key().ToBytes()).ToString());
projectSeeders.SecretHashes.Add(Hashes.Hash256(new Key().ToBytes()).ToString());
projectSeeders.SecretHashes.Add(Hashes.Hash256(new Key().ToBytes()).ToString());
projectInvestmentInfo.ProjectSeeders = projectSeeders;
var investorPubKey = Encoders.Hex.EncodeData(investorKey.PubKey.ToBytes());
// create the investment transaction
var investorInvTrx = _investorTransactionActions.CreateInvestmentTransaction(projectInvestmentInfo,investorPubKey,
Money.Coins(projectInvestmentInfo.TargetAmount).Satoshi);
var investorExpierytrx = _investorTransactionActions.RecoverEndOfProjectFunds(investorInvTrx.ToHex(),
projectInvestmentInfo, 1, investorReceiveCoinsKey.PubKey.ScriptPubKey.WitHash.GetAddress(network).ToString(),
Encoders.Hex.EncodeData(investorKey.ToBytes()),_expectedFeeEstimation);
Assert.NotNull(investorExpierytrx);
TransactionValidation.ThanTheTransactionHasNoErrors(investorExpierytrx.Transaction,
investorInvTrx.Outputs.AsCoins().Where(c => c.Amount > 0));
}
[Fact]
public void SpendSeederRecoveryTest()
{
var network = Networks.Bitcoin.Testnet();
var words = new WalletWords { Words = new Mnemonic(Wordlist.English, WordCount.Twelve).ToString() };
// Create the seeder 1 params
var seederKey = new Key();
var seederChangeKey = new Key();
var seederFundsRecoveryKey = new Key();
var seederSecret = new Key();
var funderKey = _derivationOperations.DeriveFounderKey(words, 1);
var founderRecoveryKey = _derivationOperations.DeriveFounderRecoveryKey(words, 1);
var angorKey = _derivationOperations.DeriveAngorKey(funderKey, angorRootKey);
var funderPrivateKey = _derivationOperations.DeriveFounderPrivateKey(words, 1);
var founderRecoveryPrivateKey = _derivationOperations.DeriveFounderRecoveryPrivateKey(words, 1);
var investorContext = new InvestorContext
{
ProjectInfo = new ProjectInfo
{
TargetAmount = 3,
StartDate = DateTime.UtcNow,
ExpiryDate = DateTime.UtcNow.AddDays(5),
Stages = new List<Stage>
{
new() { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(1) },
new() { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(2) },
new() { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(3) }
},
FounderKey = funderKey,
FounderRecoveryKey = founderRecoveryKey,
ProjectIdentifier = angorKey,
PenaltyDays = 5,
ProjectSeeders = new ProjectSeeders()
},
InvestorKey = Encoders.Hex.EncodeData(seederKey.PubKey.ToBytes()),
ChangeAddress = seederChangeKey.PubKey.GetSegwitAddress(network).ToString()
};
// create the investment transaction
var investmentTransaction = _seederTransactionActions.CreateInvestmentTransaction(investorContext.ProjectInfo,investorContext.InvestorKey,
Hashes.Hash256(seederSecret.ToBytes()),Money.Coins(investorContext.ProjectInfo.TargetAmount).Satoshi);
investorContext.TransactionHex = investmentTransaction.ToHex();
var recoveryTransaction = _seederTransactionActions.BuildRecoverSeederFundsTransaction(investorContext.ProjectInfo,
investmentTransaction,
investorContext.ProjectInfo.PenaltyDays, Encoders.Hex.EncodeData(seederFundsRecoveryKey.PubKey.ToBytes()));
var founderSignatures = _founderTransactionActions.SignInvestorRecoveryTransactions(investorContext.ProjectInfo,
investmentTransaction.ToHex(),recoveryTransaction,
Encoders.Hex.EncodeData(founderRecoveryPrivateKey.ToBytes()));
var signedRecoveryTransaction = _seederTransactionActions.AddSignaturesToRecoverSeederFundsTransaction(investorContext.ProjectInfo,
investmentTransaction, seederFundsRecoveryKey.PubKey.ToHex(),
founderSignatures, Encoders.Hex.EncodeData(seederKey.ToBytes()),Encoders.Hex.EncodeData(seederSecret.ToBytes()));
// Adding the input that will be spent as fee
signedRecoveryTransaction.Inputs.Add(new Blockcore.Consensus.TransactionInfo.TxIn(new Blockcore.Consensus.TransactionInfo.OutPoint(Blockcore.NBitcoin.uint256.Zero,0))); //Add fee to the transaction
TransactionValidation.ThanTheTransactionHasNoErrors(signedRecoveryTransaction,investmentTransaction.Outputs.AsCoins()
.Where(_ => _.Amount > 0)
// Adding the coin to spend as fee - so the transaction validation doesn't fail
.Append(new Coin(uint256.Zero, 0,new Money(1000),
new Blockcore.Consensus.ScriptInfo.Script("4a8a3d6bb78a5ec5bf2c599eeb1ea522677c4b10132e554d78abecd7561e4b42"))));
}
[Fact]
public void SpendInvestorRecoveryTest()
{
var network = Networks.Bitcoin.Testnet();
var words = new WalletWords { Words = new Mnemonic(Wordlist.English, WordCount.Twelve).ToString() };
// Create the seeder 1 params
var investorKey = new Key();
var investorChangeKey = new Key();
var funderKey = _derivationOperations.DeriveFounderKey(words, 1);
var angorKey = _derivationOperations.DeriveAngorKey(funderKey, angorRootKey);
var founderRecoveryKey = _derivationOperations.DeriveFounderRecoveryKey(words, 1);
var funderPrivateKey = _derivationOperations.DeriveFounderPrivateKey(words, 1);
var founderRecoveryPrivateKey = _derivationOperations.DeriveFounderRecoveryPrivateKey(words, 1);
var investorContext = new InvestorContext
{
ProjectInfo = new ProjectInfo
{
TargetAmount = 3,
StartDate = DateTime.UtcNow,
ExpiryDate = DateTime.UtcNow.AddDays(5),
PenaltyDays = 5,
Stages = new List<Stage>
{
new() { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(1) },
new() { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(2) },
new() { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(3) }
},
FounderKey = funderKey,
FounderRecoveryKey = founderRecoveryKey,
ProjectIdentifier = angorKey,
ProjectSeeders = new ProjectSeeders()
},
InvestorKey = Encoders.Hex.EncodeData(investorKey.PubKey.ToBytes()),
ChangeAddress = investorChangeKey.PubKey.GetSegwitAddress(network).ToString()
};
// create the investment transaction
var investmentTransaction = _investorTransactionActions.CreateInvestmentTransaction(investorContext.ProjectInfo,investorContext.InvestorKey,
Money.Coins(investorContext.ProjectInfo.TargetAmount).Satoshi);
investorContext.TransactionHex = investmentTransaction.ToHex();
var recoveryTransaction = _investorTransactionActions.BuildRecoverInvestorFundsTransaction(investorContext.ProjectInfo,
investmentTransaction);
var founderSignatures = _founderTransactionActions.SignInvestorRecoveryTransactions(investorContext.ProjectInfo,
investmentTransaction.ToHex(),recoveryTransaction,
Encoders.Hex.EncodeData(founderRecoveryPrivateKey.ToBytes()));
var sigCheckResult = _investorTransactionActions.CheckInvestorRecoverySignatures(investorContext.ProjectInfo, investmentTransaction, founderSignatures);
Assert.True(sigCheckResult, "failed to validate the founders signatures");
var signedRecoveryTransaction = _investorTransactionActions.AddSignaturesToRecoverSeederFundsTransaction(investorContext.ProjectInfo,
investmentTransaction,
founderSignatures, Encoders.Hex.EncodeData(investorKey.ToBytes()));
List<Coin> coins = new();
foreach (var indexedTxOut in investmentTransaction.Outputs.AsIndexedOutputs().Where(w => !w.TxOut.ScriptPubKey.IsUnspendable))
{
coins.Add(new Blockcore.NBitcoin.Coin(indexedTxOut));
coins.Add(new Blockcore.NBitcoin.Coin(Blockcore.NBitcoin.uint256.Zero, 0, new Blockcore.NBitcoin.Money(1000),
new Script("4a8a3d6bb78a5ec5bf2c599eeb1ea522677c4b10132e554d78abecd7561e4b42"))); //Adding fee inputs
}
signedRecoveryTransaction.Inputs.Add(new Blockcore.Consensus.TransactionInfo.TxIn(
new Blockcore.Consensus.TransactionInfo.OutPoint(Blockcore.NBitcoin.uint256.Zero, 0), null)); //Add fee to the transaction
TransactionValidation.ThanTheTransactionHasNoErrors(signedRecoveryTransaction, coins);
// recover the coins after the penalty
var releaseTransaction = _investorTransactionActions.BuildAndSignRecoverReleaseFundsTransaction(investorContext.ProjectInfo, investmentTransaction, signedRecoveryTransaction,
investorContext.ChangeAddress, _expectedFeeEstimation, Encoders.Hex.EncodeData(investorKey.ToBytes()));
coins = new();
foreach (var indexedTxOut in signedRecoveryTransaction.Outputs.AsIndexedOutputs())
{
coins.Add(new Blockcore.NBitcoin.Coin(indexedTxOut));
}
TransactionValidation.ThanTheTransactionHasNoErrors(releaseTransaction.Transaction, coins);
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
public void InvestorTransaction_NoPenalty_Test(int stageIndex)
{
var words = new WalletWords { Words = new Mnemonic(Wordlist.English, WordCount.Twelve).ToString() };
// Create the seeder 1 params
var investorKey = new Key();
var investorChangeKey = new Key();
var investorReceiveCoinsKey = new Key();
var seeder1Key = new Key();
var seeder2Key = new Key();
var seeder3Key = new Key();
var projectInvestmentInfo = new ProjectInfo
{
TargetAmount = 3,
StartDate = DateTime.UtcNow,
ExpiryDate = DateTime.UtcNow.AddDays(5),
Stages = new List<Stage>
{
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(1) },
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(2) },
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(3) }
},
FounderKey = _derivationOperations.DeriveFounderKey(words, 1),
FounderRecoveryKey = _derivationOperations.DeriveFounderRecoveryKey(words, 1),
ProjectSeeders = new()
{
Threshold = 2,
SecretHashes = new List<string>()
{
Hashes.Hash256(seeder1Key.ToBytes()).ToString(),
Hashes.Hash256(seeder2Key.ToBytes()).ToString(),
Hashes.Hash256(seeder3Key.ToBytes()).ToString()
}
}
};
projectInvestmentInfo.ProjectIdentifier =
_derivationOperations.DeriveAngorKey(projectInvestmentInfo.FounderKey, angorRootKey);
// create the investment transaction
var investorInvTrx = _investorTransactionActions.CreateInvestmentTransaction(projectInvestmentInfo,
Encoders.Hex.EncodeData(investorKey.PubKey.ToBytes()),
Money.Coins(projectInvestmentInfo.TargetAmount).Satoshi);
var secrets = new List<byte[]>
{
seeder1Key.ToBytes(),
seeder2Key.ToBytes(),
seeder3Key.ToBytes()
};
for (var i = 0; i < secrets.Count; i++)
{
var partSecrets = secrets.Where((_, index) => index != i)
.ToList();
var investorRecoverFundsNoPenalty = _investorTransactionActions.RecoverRemainingFundsWithOutPenalty(
investorInvTrx.ToHex(), projectInvestmentInfo, stageIndex,
investorReceiveCoinsKey.PubKey.ScriptPubKey.WitHash.GetAddress(Networks.Bitcoin.Testnet()).ToString(),
Encoders.Hex.EncodeData(investorKey.ToBytes()), _expectedFeeEstimation,partSecrets
);
Assert.NotNull(investorRecoverFundsNoPenalty);
TransactionValidation.ThanTheTransactionHasNoErrors(investorRecoverFundsNoPenalty.Transaction,
investorInvTrx.Outputs.AsCoins().Where(c => c.Amount > 0));
}
}
[Fact]
public void SpendInvestorReleaseTest()
{
var network = Networks.Bitcoin.Testnet();
var words = new WalletWords { Words = new Mnemonic(Wordlist.English, WordCount.Twelve).ToString() };
// Create the investor params
var investorKey = new Key();
var investorChangeKey = new Key();
var funderKey = _derivationOperations.DeriveFounderKey(words, 1);
var angorKey = _derivationOperations.DeriveAngorKey(funderKey, angorRootKey);
var founderRecoveryKey = _derivationOperations.DeriveFounderRecoveryKey(words, 1);
var funderPrivateKey = _derivationOperations.DeriveFounderPrivateKey(words, 1);
var founderRecoveryPrivateKey = _derivationOperations.DeriveFounderRecoveryPrivateKey(words, 1);
var investorContext = new InvestorContext
{
ProjectInfo = new ProjectInfo
{
TargetAmount = 3,
StartDate = DateTime.UtcNow,
ExpiryDate = DateTime.UtcNow.AddDays(5),
PenaltyDays = 5,
Stages = new List<Stage>
{
new() { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(1) },
new() { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(2) },
new() { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(3) }
},
FounderKey = funderKey,
FounderRecoveryKey = founderRecoveryKey,
ProjectIdentifier = angorKey,
ProjectSeeders = new ProjectSeeders()
},
InvestorKey = Encoders.Hex.EncodeData(investorKey.PubKey.ToBytes()),
ChangeAddress = investorChangeKey.PubKey.GetSegwitAddress(network).ToString()
};
var investorReleaseKey = new Key();
var investorReleasePubKey = Encoders.Hex.EncodeData(investorReleaseKey.PubKey.ToBytes());
// Create the investment transaction
var investmentTransaction = _investorTransactionActions.CreateInvestmentTransaction(investorContext.ProjectInfo, investorContext.InvestorKey,
Money.Coins(investorContext.ProjectInfo.TargetAmount).Satoshi);
investorContext.TransactionHex = investmentTransaction.ToHex();
// Build the release transaction
var releaseTransaction = _investorTransactionActions.BuildReleaseInvestorFundsTransaction(investorContext.ProjectInfo, investmentTransaction, investorReleasePubKey);
// Sign the release transaction
var founderSignatures = _founderTransactionActions.SignInvestorRecoveryTransactions(investorContext.ProjectInfo,
investmentTransaction.ToHex(), releaseTransaction,
Encoders.Hex.EncodeData(founderRecoveryPrivateKey.ToBytes()));
var signedReleaseTransaction = _investorTransactionActions.AddSignaturesToReleaseFundsTransaction(investorContext.ProjectInfo,
investmentTransaction, founderSignatures, Encoders.Hex.EncodeData(investorKey.ToBytes()), investorReleasePubKey);
// Validate the signatures
var sigCheckResult = _investorTransactionActions.CheckInvestorReleaseSignatures(investorContext.ProjectInfo, investmentTransaction, founderSignatures, investorReleasePubKey);
Assert.True(sigCheckResult, "Failed to validate the founder's signatures");
List<Coin> coins = new();
foreach (var indexedTxOut in investmentTransaction.Outputs.AsIndexedOutputs().Where(w => !w.TxOut.ScriptPubKey.IsUnspendable))
{
coins.Add(new Blockcore.NBitcoin.Coin(indexedTxOut));
coins.Add(new Blockcore.NBitcoin.Coin(Blockcore.NBitcoin.uint256.Zero, 0, new Blockcore.NBitcoin.Money(1000),
new Script("4a8a3d6bb78a5ec5bf2c599eeb1ea522677c4b10132e554d78abecd7561e4b42"))); // Adding fee inputs
}
signedReleaseTransaction.Inputs.Add(new Blockcore.Consensus.TransactionInfo.TxIn(
new Blockcore.Consensus.TransactionInfo.OutPoint(Blockcore.NBitcoin.uint256.Zero, 0), null)); // Add fee to the transaction
TransactionValidation.ThanTheTransactionHasNoErrors(signedReleaseTransaction, coins);
}
}
}