Skip to content

Commit 191c3e2

Browse files
authored
Fix the derivation path on all keys (#276)
* Fix the derivation (not compiling) * Add a special storage key that will be used for storing data for all the nostr accounts * Fix the failing test * Sorting correctly on fetch projects * When offset param doesn't exist it works for both indexers
1 parent eb3b482 commit 191c3e2

17 files changed

+167
-150
lines changed

src/Angor.Test/DerivationOperationsTest.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void BuildKeys()
6161

6262

6363
var founderKey = derivationOperations.DeriveFounderKey(new WalletWords { Words = mnemonic.ToString() }, 1);
64-
var projectId = derivationOperations.DeriveProjectId(founderKey);
64+
var projectId = derivationOperations.DeriveUniqueProjectIdentifier(founderKey);
6565
var angorKey = derivationOperations.DeriveAngorKey(founderKey, rootKey);
6666
var script = derivationOperations.AngorKeyToScript(angorKey);
6767
}
@@ -75,8 +75,8 @@ public void BuildKeysFromExisitData()
7575

7676
var words = "gospel awkward uphold orchard spike elite inform danger sheriff lens power monitor";
7777
var founderKey = derivationOperations.DeriveFounderKey(new WalletWords { Words = words }, 1);
78-
var founderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(new WalletWords { Words = words }, 1);
79-
var projectId = derivationOperations.DeriveProjectId(founderKey);
78+
var founderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(new WalletWords { Words = words }, founderKey);
79+
var projectId = derivationOperations.DeriveUniqueProjectIdentifier(founderKey);
8080
var angorKey = derivationOperations.DeriveAngorKey(founderKey, rootKey);
8181
var script = derivationOperations.AngorKeyToScript(angorKey);
8282

@@ -210,11 +210,10 @@ public async Task DeriveProjectNostrPrivateKeyAsync_ConcurrencyTest_ReturnsPriva
210210
var derivationOperations = new DerivationOperations(new HdOperations(), _logger, _networkConfiguration.Object);
211211
var words = "gospel awkward uphold orchard spike elite inform danger sheriff lens power monitor";
212212
var walletWords = new WalletWords { Words = words };
213-
int index = 1;
214213

215214
// Act
216215
var tasks = Enumerable.Range(1, 10)
217-
.Select(i => derivationOperations.DeriveProjectNostrPrivateKeyAsync(walletWords, i));
216+
.Select(i => derivationOperations.DeriveProjectNostrPrivateKeyAsync(walletWords, new Key().PubKey.ToHex()));
218217

219218
var results = await Task.WhenAll(tasks);
220219

src/Angor.Test/InvestmentOperationsTest.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void SpendFounderStage_Test()
8585
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(3) }
8686
};
8787
projectInvestmentInfo.FounderKey = derivationOperations.DeriveFounderKey(words, 1);
88-
projectInvestmentInfo.FounderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, 1);
88+
projectInvestmentInfo.FounderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, projectInvestmentInfo.FounderKey);
8989
projectInvestmentInfo.ProjectIdentifier =
9090
derivationOperations.DeriveAngorKey(projectInvestmentInfo.FounderKey, angorRootKey);
9191

@@ -241,7 +241,7 @@ public void SeederTransaction_EndOfProject_Test()
241241
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(3) }
242242
};
243243
projectInvestmentInfo.FounderKey = derivationOperations.DeriveFounderKey(words, 1);
244-
projectInvestmentInfo.FounderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, 1);
244+
projectInvestmentInfo.FounderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, projectInvestmentInfo.FounderKey);
245245
projectInvestmentInfo.ProjectIdentifier =
246246
derivationOperations.DeriveAngorKey(projectInvestmentInfo.FounderKey, angorRootKey);
247247

@@ -294,7 +294,7 @@ public void InvestorTransaction_EndOfProject_Test()
294294
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(3) }
295295
};
296296
projectInvestmentInfo.FounderKey = derivationOperations.DeriveFounderKey(words, 1);
297-
projectInvestmentInfo.FounderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, 1);
297+
projectInvestmentInfo.FounderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, projectInvestmentInfo.FounderKey);
298298
projectInvestmentInfo.ProjectIdentifier =
299299
derivationOperations.DeriveAngorKey(projectInvestmentInfo.FounderKey, angorRootKey);
300300
projectInvestmentInfo.ProjectSeeders = new ProjectSeeders();
@@ -347,7 +347,7 @@ public void InvestorTransaction_WithSeederHashes_EndOfProject_Test()
347347
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(3) }
348348
};
349349
projectInvestmentInfo.FounderKey = derivationOperations.DeriveFounderKey(words, 1);
350-
projectInvestmentInfo.FounderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, 1);
350+
projectInvestmentInfo.FounderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, projectInvestmentInfo.FounderKey);
351351
projectInvestmentInfo.ProjectIdentifier =
352352
derivationOperations.DeriveAngorKey(projectInvestmentInfo.FounderKey, angorRootKey);
353353
projectInvestmentInfo.ProjectSeeders = new ProjectSeeders();
@@ -399,10 +399,10 @@ public void SpendInvestorRecoveryTest()
399399
var investorChangeKey = new Key();
400400

401401
var funderKey = derivationOperations.DeriveFounderKey(words, 1);
402-
var funderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, 1);
402+
var funderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, funderKey);
403403
var angorKey = derivationOperations.DeriveAngorKey(funderKey, angorRootKey);
404404
var funderPrivateKey = derivationOperations.DeriveFounderPrivateKey(words, 1);
405-
var funderRecoveryPrivateKey = derivationOperations.DeriveFounderRecoveryPrivateKey(words, 1);
405+
var funderRecoveryPrivateKey = derivationOperations.DeriveFounderRecoveryPrivateKey(words, funderKey);
406406

407407
var investorContext = new InvestorContext
408408
{
@@ -491,10 +491,10 @@ public void SpendInvestorConsolidatedRecoveryTest()
491491
var words = new WalletWords { Words = new Mnemonic(Wordlist.English, WordCount.Twelve).ToString() };
492492

493493
var funderKey = derivationOperations.DeriveFounderKey(words, 1);
494-
var funderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, 1);
494+
var funderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, funderKey);
495495
var angorKey = derivationOperations.DeriveAngorKey(funderKey, angorRootKey);
496496
var funderPrivateKey = derivationOperations.DeriveFounderPrivateKey(words, 1);
497-
var funderRecoveryPrivateKey = derivationOperations.DeriveFounderRecoveryPrivateKey(words, 1);
497+
var funderRecoveryPrivateKey = derivationOperations.DeriveFounderRecoveryPrivateKey(words, funderKey);
498498

499499
// Create the investor 1 params
500500
var investorKey = new Key();
@@ -607,10 +607,10 @@ public void SpendSeederConsolidatedRecoveryTest()
607607
var words = new WalletWords { Words = new Mnemonic(Wordlist.English, WordCount.Twelve).ToString() };
608608

609609
var funderKey = derivationOperations.DeriveFounderKey(words, 1);
610-
var funderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, 1);
610+
var funderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, funderKey);
611611
var angorKey = derivationOperations.DeriveAngorKey(funderKey, angorRootKey);
612612
var funderPrivateKey = derivationOperations.DeriveFounderPrivateKey(words, 1);
613-
var funderRecoveryPrivateKey = derivationOperations.DeriveFounderRecoveryPrivateKey(words, 1);
613+
var funderRecoveryPrivateKey = derivationOperations.DeriveFounderRecoveryPrivateKey(words, funderKey);
614614

615615
// Create the investor 1 params
616616
var seederKey = new Key();
@@ -721,7 +721,7 @@ public void InvestorTransaction_NoPenalty_Test()
721721
new Stage { AmountToRelease = 1, ReleaseDate = DateTime.UtcNow.AddDays(3) }
722722
};
723723
projectInvestmentInfo.FounderKey = derivationOperations.DeriveFounderKey(words, 1);
724-
projectInvestmentInfo.FounderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, 1);
724+
projectInvestmentInfo.FounderRecoveryKey = derivationOperations.DeriveFounderRecoveryKey(words, projectInvestmentInfo.FounderKey);
725725
projectInvestmentInfo.ProjectIdentifier =
726726
derivationOperations.DeriveAngorKey(projectInvestmentInfo.FounderKey, angorRootKey);
727727

src/Angor.Test/ProtocolNew/AngorTestData.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ protected ProjectInfo GivenValidProjectInvestmentInfo( WalletWords? words = null
4747
new() { AmountToRelease = (decimal)0.4, ReleaseDate = startDate.Value.AddDays(3) }
4848
};
4949
projectInvestmentInfo.FounderKey = _derivationOperations.DeriveFounderKey(words, 1);
50-
projectInvestmentInfo.FounderRecoveryKey = _derivationOperations.DeriveFounderRecoveryKey(words, 1);
51-
projectInvestmentInfo.ProjectIdentifier =
52-
_derivationOperations.DeriveAngorKey(projectInvestmentInfo.FounderKey, angorRootKey);
50+
projectInvestmentInfo.FounderRecoveryKey = _derivationOperations.DeriveFounderRecoveryKey(words, projectInvestmentInfo.FounderKey);
51+
projectInvestmentInfo.ProjectIdentifier = _derivationOperations.DeriveAngorKey(projectInvestmentInfo.FounderKey, angorRootKey);
5352

5453
projectInvestmentInfo.ProjectSeeders = new ProjectSeeders { Threshold = 2 };
5554
return projectInvestmentInfo;

src/Angor.Test/ProtocolNew/FounderTransactionActionTest.cs

+22-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using Angor.Shared.Networks;
44
using Angor.Shared.ProtocolNew;
55
using Angor.Shared.ProtocolNew.Scripts;
6-
using Angor.Shared.ProtocolNew.TransactionBuilders;
76
using Angor.Test.DataBuilders;
87
using Blockcore.NBitcoin;
98
using Blockcore.NBitcoin.DataEncoders;
@@ -97,22 +96,20 @@ private Transaction GivenAnInvestorTransaction(ProjectInfo projectInvestmentInfo
9796
[Fact]
9897
public void SignInvestorRecoveryTransactions_CreatesValidSignatures()
9998
{
100-
var words = new WalletWords
101-
{ Words = "sorry poet adapt sister barely loud praise spray option oxygen hero surround" };
99+
var words = new WalletWords { Words = "sorry poet adapt sister barely loud praise spray option oxygen hero surround" };
102100
var projectInvestmentInfo = GivenValidProjectInvestmentInfo(words);
103101

104-
var founderRecoveryPrivateKey = _derivationOperations.DeriveFounderRecoveryPrivateKey(words, 1);
102+
var founderRecoveryPrivateKey = _derivationOperations.DeriveFounderRecoveryPrivateKey(words, projectInvestmentInfo.FounderKey);
105103

106-
var investmentTrxHex =
107-
"010000080005c0c62d0000000000160014e503a24793c82bf7f7eb18cfca6589df1360dcf40000000000000000236a21038a7eedf38d874799c0d7579d5f08d605ca039da7f6dc7c57e6abd82f0f380334e0930400000000002251207aade2c416ca565c1b66041eac56d707a26aa8bab7d217190ecf3aa57899c9a360e316000000000022512048dc0a52f43379c6515962a40eec91d183582c8e2c861d612e4e564617d08a67804f120000000000225120f57ad9ed9e0fb880cda0847cfea668a4c3ccb1b9a08bdbcc3f029e8ad0380c9d00000000";
108-
var recoveryTrxHex = "010000000396174a48b38bc96a0addafd1ccfdc478a53aae572c3cc5240b33231939eb67880200000000ffffffff96174a48b38bc96a0addafd1ccfdc478a53aae572c3cc5240b33231939eb67880300000000ffffffff96174a48b38bc96a0addafd1ccfdc478a53aae572c3cc5240b33231939eb67880400000000ffffffff03e09304000000000022002012f9e11fd7142b631007a4c8cde632a0f851fcc6b61fbd6f09ceebf30ea6d4eb60e316000000000022002012f9e11fd7142b631007a4c8cde632a0f851fcc6b61fbd6f09ceebf30ea6d4eb804f12000000000022002012f9e11fd7142b631007a4c8cde632a0f851fcc6b61fbd6f09ceebf30ea6d4eb00000000";
104+
var investmentTrxHex = "01000008000500c06e31d910010016001416623eb694e4db75d29e5a1cdf7c278b92cc40e10000000000000000236a21038a8157a3a5c6acb0e8b52798e5ed04bc7fc10226b6ab771ec4ffdb43b2ca909400e057eb481b0000225120dc7dea05eee77059b9a3752e364d8347f5021f92f4031888be106848c5b859e40060b7986c8800002251207d3252a2f7778d5fc18b7186ecbf091ce7951878ded82d4136c4a0991ae9f04c00805fad236d00002251207ac912655a09ff2231a6675dd9c796692ceb51733edd78031f965d94e938d10100000000";
105+
var recoveryTrxHex = "0100000003e3bc520467543c1c47d51c70fa06178892306b1431f7e6770ddde2330f3887460200000000ffffffffe3bc520467543c1c47d51c70fa06178892306b1431f7e6770ddde2330f3887460300000000ffffffffe3bc520467543c1c47d51c70fa06178892306b1431f7e6770ddde2330f3887460400000000ffffffff0300e057eb481b0000220020428faca0638007b13911825a7845cd7a8bc682e58688b89396afc45cf69d98180060b7986c880000220020428faca0638007b13911825a7845cd7a8bc682e58688b89396afc45cf69d981800805fad236d0000220020428faca0638007b13911825a7845cd7a8bc682e58688b89396afc45cf69d981800000000";
109106
var recoveryTransaction = Networks.Bitcoin.Testnet().CreateTransaction(recoveryTrxHex);
110107
var key = new NBitcoin.Key(founderRecoveryPrivateKey.ToBytes());
111108
var expectedHashes = new List<NBitcoin.uint256>()
112109
{
113-
new(Encoders.Hex.DecodeData("76e77d8738586b9bafd708421371314872165e9f113946a11bc1112e2fbc8f41")),
114-
new(Encoders.Hex.DecodeData("7f0fa60f9f991f291bdaea122635eaab8f3e1a7b29fcb4e5f2e270f0c11ba435")),
115-
new (Encoders.Hex.DecodeData("4e4f9533500faea374fd71e59ae3ef20cc9bb397f6a95ee6dcf47fc17e10708b"))
110+
new(Encoders.Hex.DecodeData("bc319cc189ac8ef54e2c3b0051bccf77db0d9dec9c39142ac011e98a4f3b4610")),
111+
new(Encoders.Hex.DecodeData("0d0735181252e9fb4b28644c8d9b0131aa7d9d65c374e406309783548623a82a")),
112+
new (Encoders.Hex.DecodeData("a1b81722e2946e2679561f7ec3b475a35f0d3624a20b7366f6fcb94769fb080e"))
116113
};
117114

118115
var result = _sut.SignInvestorRecoveryTransactions(projectInvestmentInfo, investmentTrxHex, recoveryTransaction, Encoders.Hex.EncodeData(founderRecoveryPrivateKey.ToBytes()));
@@ -200,30 +197,32 @@ public void SpendFounderStage_TestUsingInvestmentOperations(int stageNumber)
200197
transactionList.Select(_ => _.Outputs.AsIndexedOutputs().ElementAt(stageNumber + 1).ToCoin()));
201198
}
202199

200+
// a method to generate test data
203201
public void GenerateVectorsFor_SignInvestorRecoveryTransactions_CreatesValidSignatures()
204202
{
205-
// this cod generates text vectors for SignInvestorRecoveryTransactions_CreatesValidSignatures
203+
// this code generates text vectors for SignInvestorRecoveryTransactions_CreatesValidSignatures
206204

207205
//InvestorTransactionActions _investorTransactionActions; // needed to recreate the vector parameters
208206

209-
// needed to recreate the vector parameters
210-
//_investorTransactionActions = new InvestorTransactionActions(
211-
// new InvestmentScriptBuilder(new SeederScriptTreeBuilder()),
207+
//// needed to recreate the vector parameters
208+
//_investorTransactionActions = new InvestorTransactionActions(NullLogger<InvestorTransactionActions>.Instance,
209+
// new InvestmentScriptBuilder(new SeederScriptTreeBuilder()),
212210
// new ProjectScriptsBuilder(_derivationOperations),
213211
// new SpendingTransactionBuilder(_networkConfiguration.Object, new ProjectScriptsBuilder(_derivationOperations), new InvestmentScriptBuilder(new SeederScriptTreeBuilder())),
214-
// new InvestmentTransactionBuilder(_networkConfiguration.Object, new ProjectScriptsBuilder(_derivationOperations), new InvestmentScriptBuilder(new SeederScriptTreeBuilder())),
212+
// new InvestmentTransactionBuilder(_networkConfiguration.Object, new ProjectScriptsBuilder(_derivationOperations), new InvestmentScriptBuilder(new SeederScriptTreeBuilder()), new TaprootScriptBuilder()),
215213
// new TaprootScriptBuilder(),
216214
// _networkConfiguration.Object);
217215

218-
// and in the method SignInvestorRecoveryTransactions use the method
219-
// Encoders.Hex.Encode() on the hash of each GetSignatureHashTaproot
216+
//var words = new WalletWords { Words = "sorry poet adapt sister barely loud praise spray option oxygen hero surround" };
217+
//var projectInvestmentInfo = GivenValidProjectInvestmentInfo(words);
218+
//var founderRecoveryPrivateKey = _derivationOperations.DeriveFounderRecoveryPrivateKey(words, projectInvestmentInfo.FounderKey);
219+
220+
//// and in the method SignInvestorRecoveryTransactions use the method
221+
////Encoders.Hex.Encode() on the hash of each GetSignatureHashTaproot
220222
//var investorKey = _derivationOperations.DeriveInvestorKey(words, projectInvestmentInfo.FounderKey);
221-
//var build_investmentTransaction = _investorTransactionActions.CreateInvestmentTransaction(projectInvestmentInfo, investorKey,
222-
// Money.Coins(projectInvestmentInfo.TargetAmount).Satoshi);
223-
//var build_recoveryTransaction = _investorTransactionActions.BuildRecoverInvestorFundsTransaction(projectInvestmentInfo,
224-
// build_investmentTransaction);
225-
//var build_founderSignatures = _sut.SignInvestorRecoveryTransactions(projectInvestmentInfo,
226-
// build_investmentTransaction.ToHex(), build_recoveryTransaction, Encoders.Hex.EncodeData(founderRecoveryPrivateKey.ToBytes()));
223+
//var build_investmentTransaction = _investorTransactionActions.CreateInvestmentTransaction(projectInvestmentInfo, investorKey, Money.Coins(projectInvestmentInfo.TargetAmount).Satoshi);
224+
//var build_recoveryTransaction = _investorTransactionActions.BuildRecoverInvestorFundsTransaction(projectInvestmentInfo, build_investmentTransaction);
225+
//var build_founderSignatures = _sut.SignInvestorRecoveryTransactions(projectInvestmentInfo, build_investmentTransaction.ToHex(), build_recoveryTransaction, Encoders.Hex.EncodeData(founderRecoveryPrivateKey.ToBytes()));
227226
//var build_investmentTransactionHex = build_investmentTransaction.ToHex();
228227
//var build_recoveryTransactionHex = build_recoveryTransaction.ToHex();
229228

0 commit comments

Comments
 (0)