Skip to content

Commit

Permalink
Changed Angor to keep nostr id not npub (#220)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DavidGershony authored Nov 29, 2024
1 parent d478edd commit 4a43a85
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
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 @@ -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
Expand Down
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());

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);
}
}

0 comments on commit 4a43a85

Please sign in to comment.