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

add reverse record #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
51 changes: 51 additions & 0 deletions src/NameService/NameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public sealed class NameService : Framework.SmartContract
private const byte Prefix_Root = 0x20;
private const byte Prefix_Name = 0x21;
private const byte Prefix_Record = 0x22;
private const byte Prefix_ReverseRecord = 0x23;

private const int NameMaxLength = 255;
private const ulong OneYear = 365ul * 24 * 3600 * 1000;
Expand Down Expand Up @@ -228,6 +229,20 @@ public static bool Register(string name, UInt160 owner)
else
balanceMap.Put(oldOwner, balance);
accountMap.Delete(oldOwner + tokenKey);

//clear record
StorageMap recordMap = new(context, Prefix_Record);
var allrecords = (Iterator<RecordState>)recordMap.Find(tokenKey, FindOptions.ValuesOnly | FindOptions.DeserializeValues);
foreach (var state in allrecords)
{
byte[] recordKey = GetRecordKey(tokenKey, state.Name, state.Type);
StorageMap reverseMap = new(context, Prefix_ReverseRecord);
if (state.Type == RecordType.TXT && reverseMap[state.Data] is not null)
{
reverseMap.Delete(state.Data);
}
recordMap.Delete(recordKey);
}
}
else
{
Expand Down Expand Up @@ -579,5 +594,41 @@ private static bool CheckIPv6(string ipv6)
}
return true;
}

public bool SetReverseRecord(UInt160 owner, string name)
{
if (!Runtime.CheckWitness(owner)) return false;
StorageContext context = Storage.CurrentContext;
StorageMap reverseMap = new(context, Prefix_ReverseRecord);
string record = GetRecord(name, RecordType.TXT);
if (record == owner.ToAddress(53))
Copy link
Member

Choose a reason for hiding this comment

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

I prefer to use the protocol settings value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer to use the protocol settings value

how to get the settings value from smart contract?

Copy link
Member

Choose a reason for hiding this comment

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

System.Runtime.GetAddressVersion

{
reverseMap.Put(record, name);
return true;
}
return false;
}

[Safe]
public string GetReverseRecord(UInt160 owner)
{
StorageContext context = Storage.CurrentContext;
StorageMap reverseMap = new(context, Prefix_ReverseRecord);
string address = owner.ToAddress(53);
if (reverseMap[address] is not null)
{
string name = reverseMap[address];
string record = GetRecord(name, RecordType.TXT);
if (record == owner.ToAddress(53))
{
return name;
}
else
{
return "";
}
}
return "";
}
}
}