From 4a43a857ec1bfaa10cb6e1ae0d3fd88f0d7eb7b5 Mon Sep 17 00:00:00 2001 From: TheDude Date: Fri, 29 Nov 2024 13:25:43 +0000 Subject: [PATCH] Changed Angor to keep nostr id not npub (#220) * Changed the project to hold event id and the project id derivation to be int.max not uin max decided by 2 * Bug fix and added class for parsing --- .../Operations/Types/ProjectIndexerData.cs | 2 +- .../Storage/Mongo/AngorMongoData.cs | 4 +- .../Storage/Mongo/Types/Project.cs | 2 +- .../Sync/SyncTasks/ProjectsSyncRunner.cs | 56 ++++++++++--------- 4 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/Blockcore.Indexer.Angor/Operations/Types/ProjectIndexerData.cs b/src/Blockcore.Indexer.Angor/Operations/Types/ProjectIndexerData.cs index eb1827a..66a42fb 100644 --- a/src/Blockcore.Indexer.Angor/Operations/Types/ProjectIndexerData.cs +++ b/src/Blockcore.Indexer.Angor/Operations/Types/ProjectIndexerData.cs @@ -3,7 +3,7 @@ namespace Blockcore.Indexer.Angor.Operations.Types; public class ProjectIndexerData { public string FounderKey { get; set; } - public string NostrPubKey { get; set; } + public string NostrEventId { get; set; } public string ProjectIdentifier { get; set; } public long CreatedOnBlock { get; set; } public string TrxId { get; set; } diff --git a/src/Blockcore.Indexer.Angor/Storage/Mongo/AngorMongoData.cs b/src/Blockcore.Indexer.Angor/Storage/Mongo/AngorMongoData.cs index 2aa71dd..36a24b6 100644 --- a/src/Blockcore.Indexer.Angor/Storage/Mongo/AngorMongoData.cs +++ b/src/Blockcore.Indexer.Angor/Storage/Mongo/AngorMongoData.cs @@ -42,7 +42,7 @@ public AngorMongoData(ILogger dbLogger, SyncConnection connection, return new ProjectIndexerData { FounderKey = project.FounderKey, - NostrPubKey = project.NPubKey, + NostrEventId = project.NosrtEventId, ProjectIdentifier = project.AngorKey, TrxId = project.TransactionId, TotalInvestmentsCount = total, @@ -236,7 +236,7 @@ public async Task> GetProjectsAsync(int? offset, Items = projects.Select(_ => new ProjectIndexerData { FounderKey = _.FounderKey, - NostrPubKey =_.NPubKey, + NostrEventId =_.NosrtEventId, ProjectIdentifier = _.AngorKey, TrxId = _.TransactionId, CreatedOnBlock = _.BlockIndex diff --git a/src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs b/src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs index 92b6819..8cb908c 100644 --- a/src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs +++ b/src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs @@ -5,7 +5,7 @@ public class Project public string AngorKey { get; set; } public string FounderKey { get; set; } - public string NPubKey { get; set; } + public string NosrtEventId { get; set; } public string AngorKeyScriptHex { get; set; } public long BlockIndex { get; set; } public string TransactionId { get; set; } //TODO check if this should be a lookup diff --git a/src/Blockcore.Indexer.Angor/Sync/SyncTasks/ProjectsSyncRunner.cs b/src/Blockcore.Indexer.Angor/Sync/SyncTasks/ProjectsSyncRunner.cs index 6becda8..1f1604f 100644 --- a/src/Blockcore.Indexer.Angor/Sync/SyncTasks/ProjectsSyncRunner.cs +++ b/src/Blockcore.Indexer.Angor/Sync/SyncTasks/ProjectsSyncRunner.cs @@ -101,22 +101,17 @@ public override async Task OnExecute() { var script = Script.FromHex(output.ScriptHex); - var ops = script.ToOps(); + var parsedData = new DataFromOps(); - if (ops.Count != 3 || - ops.First().Name != Op.GetOpName(OpcodeType.OP_RETURN) || - ops[1].PushData.Length != 33 || - ops[2].PushData.Length != 32) + if (!parsedData.TryParse(script.ToOps())) return null; - var founderKey = new PubKey(script.ToOps()[1].PushData); - var checkForExistingProject = await AngorMongoDb.ProjectTable.AsQueryable() - .AnyAsync(_ => _.FounderKey == founderKey.ToHex()); + .AnyAsync(_ => _.FounderKey == parsedData.FounderPubKey.ToHex()); if (checkForExistingProject) return null; - var projectId = GetProjectIdDerivation(founderKey.ToHex()); + var projectId = GetProjectIdDerivation(parsedData.FounderPubKey); if (projectId == 0) return null; @@ -127,8 +122,6 @@ public override async Task OnExecute() var projectIdentifier = encoder.Encode(0, angorKey.WitHash.ToBytes()); - var nPubKey = Encoders.Hex.EncodeData(script.ToOps()[2].PushData); //Schnorr signature not supported - var angorFeeOutput = await AngorMongoDb.OutputTable .AsQueryable() .Where(_ => @@ -146,28 +139,41 @@ public override async Task OnExecute() TransactionId = output.Outpoint.TransactionId, AngorKeyScriptHex = angorKey.WitHash.ScriptPubKey.ToHex(), BlockIndex = output.BlockIndex, - FounderKey = founderKey.ToHex(), - NPubKey = nPubKey, + FounderKey = parsedData.FounderPubKey.ToHex(), + NosrtEventId = parsedData.keyType == 1 ? parsedData.NostrEventId : string.Empty, AddressOnFeeOutput = angorFeeOutput.Address }; } - private uint GetProjectIdDerivation(string founderKey) + private class DataFromOps { - ExtKey.UseBCForHMACSHA512 = true; - Hashes.UseBCForHMACSHA512 = true; - - var key = new PubKey(founderKey); - - var hashOfid = Hashes.Hash256(key.ToBytes()); + public PubKey FounderPubKey { get; set; } + public short keyType { get; set; } + public string NostrEventId { get; set; } - var projectid = hashOfid.GetLow32(); + public bool TryParse(IList ops) + { + if (ops.Count != 4 || + ops.First().Name != Op.GetOpName(OpcodeType.OP_RETURN) || + ops[1].PushData.Length != 33 || + ops[2].PushData.Length != 2 || + ops[3].PushData.Length != 32) + return false; + + FounderPubKey = new PubKey(ops[1].PushData); + keyType = BitConverter.ToInt16(ops[2].PushData); + NostrEventId = Encoders.Hex.EncodeData(ops[3].PushData); + return true; + } + } - var ret = projectid / 2; // the max size of bip32 derivation range is 2,147,483,648 (2^31) the max number of uint is 4,294,967,295 so we must divide by zero + private uint GetProjectIdDerivation(PubKey founderKey) + { + ExtKey.UseBCForHMACSHA512 = true; + Hashes.UseBCForHMACSHA512 = true; - if (ret > int.MaxValue) - throw new Exception(); + var hashOfid = Hashes.Hash256(founderKey.ToBytes()); - return ret; + return (uint)(hashOfid.GetLow64() & int.MaxValue); } }