From 97b5bbe94e574eb266f9738cb1fe7cff3c5144b7 Mon Sep 17 00:00:00 2001 From: cloud8little <413074538@qq.com> Date: Thu, 16 Jun 2022 12:03:43 +0800 Subject: [PATCH] add reverse record and clear records of expired token --- src/NameService/NameService.cs | 51 ++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/NameService/NameService.cs b/src/NameService/NameService.cs index 4f8d1d4..bda76ad 100644 --- a/src/NameService/NameService.cs +++ b/src/NameService/NameService.cs @@ -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; @@ -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)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 { @@ -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)) + { + 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 ""; + } } }