Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed Angor to keep nostr id not npub #220

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

public class ProjectIndexerData
{
public string FounderKey { get; set; }

Check warning on line 5 in src/Blockcore.Indexer.Angor/Operations/Types/ProjectIndexerData.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (ubuntu-latest)

Non-nullable property 'FounderKey' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 5 in src/Blockcore.Indexer.Angor/Operations/Types/ProjectIndexerData.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (macos-latest)

Non-nullable property 'FounderKey' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string NostrPubKey { get; set; }
public string NostrEventId { get; set; }

Check warning on line 6 in src/Blockcore.Indexer.Angor/Operations/Types/ProjectIndexerData.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (ubuntu-latest)

Non-nullable property 'NostrEventId' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 6 in src/Blockcore.Indexer.Angor/Operations/Types/ProjectIndexerData.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (macos-latest)

Non-nullable property 'NostrEventId' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string ProjectIdentifier { get; set; }

Check warning on line 7 in src/Blockcore.Indexer.Angor/Operations/Types/ProjectIndexerData.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (ubuntu-latest)

Non-nullable property 'ProjectIdentifier' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 7 in src/Blockcore.Indexer.Angor/Operations/Types/ProjectIndexerData.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (macos-latest)

Non-nullable property 'ProjectIdentifier' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public long CreatedOnBlock { get; set; }
public string TrxId { get; set; }

Check warning on line 9 in src/Blockcore.Indexer.Angor/Operations/Types/ProjectIndexerData.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (macos-latest)

Non-nullable property 'TrxId' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public long? TotalInvestmentsCount { get; set; }
}
4 changes: 2 additions & 2 deletions src/Blockcore.Indexer.Angor/Storage/Mongo/AngorMongoData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public AngorMongoData(ILogger<AngorMongoDb> dbLogger, SyncConnection connection,
return new ProjectIndexerData
{
FounderKey = project.FounderKey,
NostrPubKey = project.NPubKey,
NostrEventId = project.NosrtEventId,
ProjectIdentifier = project.AngorKey,
TrxId = project.TransactionId,
TotalInvestmentsCount = total,
Expand Down Expand Up @@ -236,7 +236,7 @@ public async Task<QueryResult<ProjectIndexerData>> GetProjectsAsync(int? offset,
Items = projects.Select(_ => new ProjectIndexerData
{
FounderKey = _.FounderKey,
NostrPubKey =_.NPubKey,
NostrEventId =_.NosrtEventId,
ProjectIdentifier = _.AngorKey,
TrxId = _.TransactionId,
CreatedOnBlock = _.BlockIndex
Expand Down
2 changes: 1 addition & 1 deletion src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

public class Project
{
public string AngorKey { get; set; }

Check warning on line 5 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (windows-latest)

Non-nullable property 'AngorKey' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 5 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (ubuntu-latest)

Non-nullable property 'AngorKey' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string FounderKey { get; set; }

Check warning on line 6 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (windows-latest)

Non-nullable property 'FounderKey' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 6 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (ubuntu-latest)

Non-nullable property 'FounderKey' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

public string NPubKey { get; set; }
public string NosrtEventId { get; set; }

Check warning on line 8 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (windows-latest)

Non-nullable property 'NosrtEventId' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 8 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (ubuntu-latest)

Non-nullable property 'NosrtEventId' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public string AngorKeyScriptHex { get; set; }

Check warning on line 9 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (windows-latest)

Non-nullable property 'AngorKeyScriptHex' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 9 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (ubuntu-latest)

Non-nullable property 'AngorKeyScriptHex' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public long BlockIndex { get; set; }
public string TransactionId { get; set; } //TODO check if this should be a lookup

Check warning on line 11 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (windows-latest)

Non-nullable property 'TransactionId' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 11 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (ubuntu-latest)

Non-nullable property 'TransactionId' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

public string AddressOnFeeOutput { get; set; } // for indexed lookups against the output table

Check warning on line 13 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (windows-latest)

Non-nullable property 'AddressOnFeeOutput' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 13 in src/Blockcore.Indexer.Angor/Storage/Mongo/Types/Project.cs

View workflow job for this annotation

GitHub Actions / buildAndUnitTest (ubuntu-latest)

Non-nullable property 'AddressOnFeeOutput' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
}
56 changes: 31 additions & 25 deletions src/Blockcore.Indexer.Angor/Sync/SyncTasks/ProjectsSyncRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,17 @@ public override async Task<bool> 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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't wanna change the database? FounderPubKey well probably not because we will add it back later


if (checkForExistingProject) return null;

var projectId = GetProjectIdDerivation(founderKey.ToHex());
var projectId = GetProjectIdDerivation(parsedData.FounderPubKey);

if (projectId == 0)
return null;
Expand All @@ -127,8 +122,6 @@ public override async Task<bool> 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(_ =>
Expand All @@ -146,28 +139,41 @@ public override async Task<bool> 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<Op> 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);
}
}
Loading