diff --git a/src/Neo.FileStorage.API/Neo.FileStorage.API.csproj b/src/Neo.FileStorage.API/Neo.FileStorage.API.csproj index 37fd682..2154ac1 100644 --- a/src/Neo.FileStorage.API/Neo.FileStorage.API.csproj +++ b/src/Neo.FileStorage.API/Neo.FileStorage.API.csproj @@ -19,7 +19,7 @@ snupkg The Neo Project true - 3.7.5 + 3.8.1 true 4 latest @@ -52,17 +52,14 @@ - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - + + + + + + + + - + \ No newline at end of file diff --git a/src/Neo.FileStorage.API/cryptography/Helper.cs b/src/Neo.FileStorage.API/cryptography/Helper.cs index 24b4fa1..d781add 100644 --- a/src/Neo.FileStorage.API/cryptography/Helper.cs +++ b/src/Neo.FileStorage.API/cryptography/Helper.cs @@ -17,100 +17,101 @@ using System.Buffers.Binary; using System.Security.Cryptography; -namespace Neo.FileStorage.API.Cryptography; - -public static class Helper +namespace Neo.FileStorage.API.Cryptography { - public const int Sha256HashLength = 32; - - internal static byte[] RIPEMD160(this byte[] value) - { - var hash = new byte[20]; - var digest = new RipeMD160Digest(); - digest.BlockUpdate(value, 0, value.Length); - digest.DoFinal(hash, 0); - return hash; - } - - public static byte[] Sha256(this byte[] value) - { - using var sha256 = SHA256.Create(); - return sha256.ComputeHash(value); - } - - internal static byte[] Sha256(this byte[] value, int offset, int count) - { - using var sha256 = SHA256.Create(); - return sha256.ComputeHash(value, offset, count); - } - - internal static byte[] Sha256(this ReadOnlySpan value) + public static class Helper { - var buffer = new byte[32]; - using var sha256 = SHA256.Create(); - sha256.TryComputeHash(value, buffer, out _); - return buffer; - } - - public static ByteString Sha256(this IMessage data) - { - return ByteString.CopyFrom(data.ToByteArray().Sha256()); - } + public const int Sha256HashLength = 32; - public static ByteString Sha256(this ByteString data) - { - return ByteString.CopyFrom(data.ToByteArray().Sha256()); - } + internal static byte[] RIPEMD160(this byte[] value) + { + var hash = new byte[20]; + var digest = new RipeMD160Digest(); + digest.BlockUpdate(value, 0, value.Length); + digest.DoFinal(hash, 0); + return hash; + } + + public static byte[] Sha256(this byte[] value) + { + using var sha256 = SHA256.Create(); + return sha256.ComputeHash(value); + } - public static Checksum Sha256Checksum(this IMessage data) - { - return new Checksum + internal static byte[] Sha256(this byte[] value, int offset, int count) { - Type = ChecksumType.Sha256, - Sum = data.Sha256() - }; - } + using var sha256 = SHA256.Create(); + return sha256.ComputeHash(value, offset, count); + } - public static Checksum Sha256Checksum(this ByteString data) - { - return new Checksum + internal static byte[] Sha256(this ReadOnlySpan value) { - Type = ChecksumType.Sha256, - Sum = data.Sha256() - }; - } + var buffer = new byte[32]; + using var sha256 = SHA256.Create(); + sha256.TryComputeHash(value, buffer, out _); + return buffer; + } - public static ByteString TzHash(this IMessage data) - { - return ByteString.CopyFrom(new TzHash().ComputeHash(data.ToByteArray())); - } + public static ByteString Sha256(this IMessage data) + { + return ByteString.CopyFrom(data.ToByteArray().Sha256()); + } - public static ByteString TzHash(this ByteString data) - { - return ByteString.CopyFrom(new TzHash().ComputeHash(data.ToByteArray())); - } + public static ByteString Sha256(this ByteString data) + { + return ByteString.CopyFrom(data.ToByteArray().Sha256()); + } - public static Checksum TzChecksum(this IMessage data) - { - return new Checksum + public static Checksum Sha256Checksum(this IMessage data) { - Type = ChecksumType.Sha256, - Sum = data.TzHash() - }; - } + return new Checksum + { + Type = ChecksumType.Sha256, + Sum = data.Sha256() + }; + } + + public static Checksum Sha256Checksum(this ByteString data) + { + return new Checksum + { + Type = ChecksumType.Sha256, + Sum = data.Sha256() + }; + } + + public static ByteString TzHash(this IMessage data) + { + return ByteString.CopyFrom(new TzHash().ComputeHash(data.ToByteArray())); + } - public static Checksum TzChecksum(this ByteString data) - { - return new Checksum + public static ByteString TzHash(this ByteString data) { - Type = ChecksumType.Sha256, - Sum = data.TzHash() - }; - } + return ByteString.CopyFrom(new TzHash().ComputeHash(data.ToByteArray())); + } - public static ulong Murmur64(this byte[] value, uint seed) - { - using var murmur = new Murmur3_128(seed); - return BinaryPrimitives.ReadUInt64LittleEndian(murmur.ComputeHash(value)); + public static Checksum TzChecksum(this IMessage data) + { + return new Checksum + { + Type = ChecksumType.Sha256, + Sum = data.TzHash() + }; + } + + public static Checksum TzChecksum(this ByteString data) + { + return new Checksum + { + Type = ChecksumType.Sha256, + Sum = data.TzHash() + }; + } + + public static ulong Murmur64(this byte[] value, uint seed) + { + using var murmur = new Murmur3_128(seed); + return BinaryPrimitives.ReadUInt64LittleEndian(murmur.ComputeHash(value)); + } } } diff --git a/src/Neo.FileStorage.API/cryptography/Key.cs b/src/Neo.FileStorage.API/cryptography/Key.cs index 7feac4c..3a27bee 100644 --- a/src/Neo.FileStorage.API/cryptography/Key.cs +++ b/src/Neo.FileStorage.API/cryptography/Key.cs @@ -19,149 +19,150 @@ using System.Text; using static Neo.FileStorage.API.Helper; -namespace Neo.FileStorage.API.Cryptography; - -public static class KeyExtension +namespace Neo.FileStorage.API.Cryptography { - public const byte NeoAddressVersion = 0x35; - private const int CompressedPublicKeyLength = 33; - private const int UncompressedPublicKeyLength = 65; - - private static readonly uint CheckSigDescriptor = - BinaryPrimitives.ReadUInt32LittleEndian(Encoding.ASCII.GetBytes("System.Crypto.CheckSig").Sha256()); - - public static byte[] Compress(this byte[] publicKey) + public static class KeyExtension { - if (publicKey.Length != UncompressedPublicKeyLength) - throw new FormatException( - $"{nameof(Compress)} argument isn't uncompressed public key. expected length={UncompressedPublicKeyLength}, actual={publicKey.Length}"); - var secp256r1 = SecNamedCurves.GetByName("secp256r1"); - var point = secp256r1.Curve.DecodePoint(publicKey); - return point.GetEncoded(true); - } + public const byte NeoAddressVersion = 0x35; + private const int CompressedPublicKeyLength = 33; + private const int UncompressedPublicKeyLength = 65; - public static byte[] Decompress(this byte[] publicKey) - { - if (publicKey.Length != CompressedPublicKeyLength) - throw new FormatException( - $"{nameof(Decompress)} argument isn't compressed public key. expected length={CompressedPublicKeyLength}, actual={publicKey.Length}"); - var secp256r1 = SecNamedCurves.GetByName("secp256r1"); - var point = secp256r1.Curve.DecodePoint(publicKey); - return point.GetEncoded(false); - } - - private static byte[] CreateSignatureRedeemScript(this byte[] publicKey) - { - if (publicKey.Length != CompressedPublicKeyLength) - throw new FormatException( - $"{nameof(CreateSignatureRedeemScript)} argument isn't compressed public key. expected length={CompressedPublicKeyLength}, actual={publicKey.Length}"); - var script = new byte[] { 0x0c, CompressedPublicKeyLength }; //PUSHDATA1 33 - script = Concat(script, publicKey); - script = Concat(script, new byte[] { 0x41 }); //SYSCALL - script = Concat(script, BitConverter.GetBytes(CheckSigDescriptor)); //Neo_Crypto_CheckSig - return script; - } - - public static byte[] GetScriptHash(this byte[] publicKey) - { - var script = publicKey.CreateSignatureRedeemScript(); - return script.Sha256().RIPEMD160(); - } + private static readonly uint CheckSigDescriptor = + BinaryPrimitives.ReadUInt32LittleEndian(Encoding.ASCII.GetBytes("System.Crypto.CheckSig").Sha256()); - private static string ToAddress(this byte[] scriptHash, byte version) - { - Span data = stackalloc byte[21]; - data[0] = version; - scriptHash.CopyTo(data[1..]); - return Base58.Base58CheckEncode(data); - } + public static byte[] Compress(this byte[] publicKey) + { + if (publicKey.Length != UncompressedPublicKeyLength) + throw new FormatException( + $"{nameof(Compress)} argument isn't uncompressed public key. expected length={UncompressedPublicKeyLength}, actual={publicKey.Length}"); + var secp256r1 = SecNamedCurves.GetByName("secp256r1"); + var point = secp256r1.Curve.DecodePoint(publicKey); + return point.GetEncoded(true); + } + + public static byte[] Decompress(this byte[] publicKey) + { + if (publicKey.Length != CompressedPublicKeyLength) + throw new FormatException( + $"{nameof(Decompress)} argument isn't compressed public key. expected length={CompressedPublicKeyLength}, actual={publicKey.Length}"); + var secp256r1 = SecNamedCurves.GetByName("secp256r1"); + var point = secp256r1.Curve.DecodePoint(publicKey); + return point.GetEncoded(false); + } + + private static byte[] CreateSignatureRedeemScript(this byte[] publicKey) + { + if (publicKey.Length != CompressedPublicKeyLength) + throw new FormatException( + $"{nameof(CreateSignatureRedeemScript)} argument isn't compressed public key. expected length={CompressedPublicKeyLength}, actual={publicKey.Length}"); + var script = new byte[] { 0x0c, CompressedPublicKeyLength }; //PUSHDATA1 33 + script = Concat(script, publicKey); + script = Concat(script, new byte[] { 0x41 }); //SYSCALL + script = Concat(script, BitConverter.GetBytes(CheckSigDescriptor)); //Neo_Crypto_CheckSig + return script; + } + + public static byte[] GetScriptHash(this byte[] publicKey) + { + var script = publicKey.CreateSignatureRedeemScript(); + return script.Sha256().RIPEMD160(); + } - private static byte[] GetPrivateKeyFromWIF(string wif) - { - if (wif == null) throw new ArgumentNullException(); - var data = wif.Base58CheckDecode(); - if (data.Length != 34 || data[0] != 0x80 || data[33] != 0x01) - throw new FormatException(); - var privateKey = new byte[32]; - Buffer.BlockCopy(data, 1, privateKey, 0, privateKey.Length); - Array.Clear(data, 0, data.Length); - return privateKey; - } + private static string ToAddress(this byte[] scriptHash, byte version) + { + Span data = stackalloc byte[21]; + data[0] = version; + scriptHash.CopyTo(data[1..]); + return Base58.Base58CheckEncode(data); + } - public static string Address(this ECDsa key) - { - return key.PublicKey().PublicKeyToAddress(); - } + private static byte[] GetPrivateKeyFromWIF(string wif) + { + if (wif == null) throw new ArgumentNullException(); + var data = wif.Base58CheckDecode(); + if (data.Length != 34 || data[0] != 0x80 || data[33] != 0x01) + throw new FormatException(); + var privateKey = new byte[32]; + Buffer.BlockCopy(data, 1, privateKey, 0, privateKey.Length); + Array.Clear(data, 0, data.Length); + return privateKey; + } + + public static string Address(this ECDsa key) + { + return key.PublicKey().PublicKeyToAddress(); + } - public static OwnerID OwnerID(this ECDsa key) - { - return Refs.OwnerID.FromPublicKey(key.PublicKey()); - } + public static OwnerID OwnerID(this ECDsa key) + { + return Refs.OwnerID.FromPublicKey(key.PublicKey()); + } - public static string PublicKeyToAddress(this byte[] publicKey) - { - if (publicKey.Length != CompressedPublicKeyLength) - throw new FormatException(nameof(publicKey) + - $" isn't encoded compressed public key. expected length={CompressedPublicKeyLength}, actual={publicKey.Length}"); - return publicKey.GetScriptHash().ToAddress(NeoAddressVersion); - } + public static string PublicKeyToAddress(this byte[] publicKey) + { + if (publicKey.Length != CompressedPublicKeyLength) + throw new FormatException(nameof(publicKey) + + $" isn't encoded compressed public key. expected length={CompressedPublicKeyLength}, actual={publicKey.Length}"); + return publicKey.GetScriptHash().ToAddress(NeoAddressVersion); + } - public static byte[] PublicKey(this ECDsa key) - { - var param = key.ExportParameters(false); - var pubkey = new byte[33]; - var pos = 33 - param.Q.X.Length; + public static byte[] PublicKey(this ECDsa key) + { + var param = key.ExportParameters(false); + var pubkey = new byte[33]; + var pos = 33 - param.Q.X.Length; - param.Q.X.CopyTo(pubkey, pos); - if (new BigInteger(param.Q.Y.Reverse().Concat(new byte[] { 0x00 }).ToArray()).IsEven) - pubkey[0] = 0x2; - else - pubkey[0] = 0x3; + param.Q.X.CopyTo(pubkey, pos); + if (new BigInteger(param.Q.Y.Reverse().Concat(new byte[] { 0x00 }).ToArray()).IsEven) + pubkey[0] = 0x2; + else + pubkey[0] = 0x3; - return pubkey; - } + return pubkey; + } - public static byte[] PrivateKey(this ECDsa key) - { - return key.ExportParameters(true).D; - } + public static byte[] PrivateKey(this ECDsa key) + { + return key.ExportParameters(true).D; + } - public static ECDsa LoadPrivateKey(this byte[] private_key) - { - var secp256r1 = SecNamedCurves.GetByName("secp256r1"); - var public_key = - secp256r1.G.Multiply(new Org.BouncyCastle.Math.BigInteger(1, private_key)).GetEncoded(false)[1..]; - var key = ECDsa.Create(new ECParameters + public static ECDsa LoadPrivateKey(this byte[] private_key) { - Curve = ECCurve.NamedCurves.nistP256, - D = private_key, - Q = new ECPoint + var secp256r1 = SecNamedCurves.GetByName("secp256r1"); + var public_key = + secp256r1.G.Multiply(new Org.BouncyCastle.Math.BigInteger(1, private_key)).GetEncoded(false)[1..]; + var key = ECDsa.Create(new ECParameters { - X = public_key[..32], - Y = public_key[32..] - } - }); - return key; - } - - public static ECDsa LoadWif(this string wif) - { - var private_key = GetPrivateKeyFromWIF(wif); - return LoadPrivateKey(private_key); - } + Curve = ECCurve.NamedCurves.nistP256, + D = private_key, + Q = new ECPoint + { + X = public_key[..32], + Y = public_key[32..] + } + }); + return key; + } + + public static ECDsa LoadWif(this string wif) + { + var private_key = GetPrivateKeyFromWIF(wif); + return LoadPrivateKey(private_key); + } - public static ECDsa LoadPublicKey(this byte[] public_key) - { - var public_key_full = public_key.Decompress()[1..]; - var key = ECDsa.Create(new ECParameters + public static ECDsa LoadPublicKey(this byte[] public_key) { - Curve = ECCurve.NamedCurves.nistP256, - Q = new ECPoint + var public_key_full = public_key.Decompress()[1..]; + var key = ECDsa.Create(new ECParameters { - X = public_key_full[..32], - Y = public_key_full[32..] - } - }); - return key; + Curve = ECCurve.NamedCurves.nistP256, + Q = new ECPoint + { + X = public_key_full[..32], + Y = public_key_full[32..] + } + }); + return key; + } } -} \ No newline at end of file +} diff --git a/src/Neo.FileStorage.API/object/Extension.Object.cs b/src/Neo.FileStorage.API/object/Extension.Object.cs index 5de1f6c..e5e5e87 100644 --- a/src/Neo.FileStorage.API/object/Extension.Object.cs +++ b/src/Neo.FileStorage.API/object/Extension.Object.cs @@ -19,178 +19,179 @@ using System.Security.Cryptography; using Version = Neo.FileStorage.API.Refs.Version; -namespace Neo.FileStorage.API.Object; - -public partial class Object +namespace Neo.FileStorage.API.Object { - public const int ChunkSize = 3 * (1 << 20); + public partial class Object + { + public const int ChunkSize = 3 * (1 << 20); - private Object parent; + private Object parent; - public Version Version => Header?.Version; - public ulong PayloadSize => Header?.PayloadLength ?? 0; - public ContainerID ContainerId => Header?.ContainerId; - public OwnerID OwnerId => Header?.OwnerId; - public ulong CreationEpoch => Header?.CreationEpoch ?? 0; + public Version Version => Header?.Version; + public ulong PayloadSize => Header?.PayloadLength ?? 0; + public ContainerID ContainerId => Header?.ContainerId; + public OwnerID OwnerId => Header?.OwnerId; + public ulong CreationEpoch => Header?.CreationEpoch ?? 0; - public Checksum PayloadChecksum - { - get => Header?.PayloadHash; - set => Header.PayloadHash = value; - } + public Checksum PayloadChecksum + { + get => Header?.PayloadHash; + set => Header.PayloadHash = value; + } - public Checksum PayloadHomomorphicHash - { - get => Header?.HomomorphicHash; - set => Header.HomomorphicHash = value; - } + public Checksum PayloadHomomorphicHash + { + get => Header?.HomomorphicHash; + set => Header.HomomorphicHash = value; + } - public List Attributes => Header?.Attributes.ToList(); - public ObjectID PreviousId => Header?.Split?.Previous; + public List Attributes => Header?.Attributes.ToList(); + public ObjectID PreviousId => Header?.Split?.Previous; - public IEnumerable Children - { - get => Header?.Split?.Children; - set + public IEnumerable Children { - if (Header is null) Header = new Header(); - if (Header.Split is null) Header.Split = new Header.Types.Split(); - Header.Split.Children.Clear(); - Header.Split.Children.AddRange(value); + get => Header?.Split?.Children; + set + { + if (Header is null) Header = new Header(); + if (Header.Split is null) Header.Split = new Header.Types.Split(); + Header.Split.Children.Clear(); + Header.Split.Children.AddRange(value); + } } - } - public SplitID SplitId - { - get => Header?.Split?.SplitId; - set + public SplitID SplitId { - if (Header is null) Header = new Header(); - if (Header.Split is null) Header.Split = new Header.Types.Split(); - Header.Split.SplitId = value.ToByteString(); + get => Header?.Split?.SplitId; + set + { + if (Header is null) Header = new Header(); + if (Header.Split is null) Header.Split = new Header.Types.Split(); + Header.Split.SplitId = value.ToByteString(); + } } - } - public ObjectID ParentId => Header?.Split?.Parent; - public SessionToken SessionToken => Header?.SessionToken; + public ObjectID ParentId => Header?.Split?.Parent; + public SessionToken SessionToken => Header?.SessionToken; - public ObjectType ObjectType - { - get => Header.ObjectType; - set => Header.ObjectType = value; - } + public ObjectType ObjectType + { + get => Header.ObjectType; + set => Header.ObjectType = value; + } - public bool HasParent => Header?.Split != null; - public Address Address => new(ContainerId, ObjectId); + public bool HasParent => Header?.Split != null; + public Address Address => new(ContainerId, ObjectId); - public Object Parent - { - get + public Object Parent { - if (parent is not null) return parent; - var split = Header?.Split; - if (split is null) return null; - if (split.ParentSignature is null && split.ParentHeader is null) - return null; - Object obj = new() + get { - Header = split.ParentHeader, - Signature = split.ParentSignature, - ObjectId = split.Parent - }; - return parent = obj; + if (parent is not null) return parent; + var split = Header?.Split; + if (split is null) return null; + if (split.ParentSignature is null && split.ParentHeader is null) + return null; + Object obj = new() + { + Header = split.ParentHeader, + Signature = split.ParentSignature, + ObjectId = split.Parent + }; + return parent = obj; + } + set + { + parent = value; + if (Header is null) Header = new Header(); + if (Header.Split is null) Header.Split = new Header.Types.Split(); + Header.Split.Parent = parent.ObjectId; + Header.Split.ParentSignature = parent.Signature; + Header.Split.ParentHeader = parent.Header; + } } - set + + public ObjectID CalculateID() { - parent = value; - if (Header is null) Header = new Header(); - if (Header.Split is null) Header.Split = new Header.Types.Split(); - Header.Split.Parent = parent.ObjectId; - Header.Split.ParentSignature = parent.Signature; - Header.Split.ParentHeader = parent.Header; + return new ObjectID + { + Value = Header.Sha256() + }; } - } - public ObjectID CalculateID() - { - return new ObjectID + public bool VerifyID() { - Value = Header.Sha256() - }; - } - - public bool VerifyID() - { - return CalculateID().Equals(ObjectId); - } + return CalculateID().Equals(ObjectId); + } - public Checksum CalculatePayloadChecksum(ChecksumType type) - { - if (Payload is null) - throw new InvalidOperationException("cant calculate payload checksum: invalid payload"); - return type switch - { - ChecksumType.Sha256 => Payload.Sha256Checksum(), - ChecksumType.Tz => Payload.TzChecksum(), - _ => throw new InvalidOperationException("unsupport checksum type") - }; - } + public Checksum CalculatePayloadChecksum(ChecksumType type) + { + if (Payload is null) + throw new InvalidOperationException("cant calculate payload checksum: invalid payload"); + return type switch + { + ChecksumType.Sha256 => Payload.Sha256Checksum(), + ChecksumType.Tz => Payload.TzChecksum(), + _ => throw new InvalidOperationException("unsupport checksum type") + }; + } - public bool VerifyPayloadChecksum() - { - return CalculatePayloadChecksum(ChecksumType.Sha256).Equals(Header?.PayloadHash); - } + public bool VerifyPayloadChecksum() + { + return CalculatePayloadChecksum(ChecksumType.Sha256).Equals(Header?.PayloadHash); + } - public Signature CalculateIDSignature(ECDsa key) - { - return key.SignMessagePart(ObjectId); - } + public Signature CalculateIDSignature(ECDsa key) + { + return key.SignMessagePart(ObjectId); + } - public bool VerifyIDSignature() - { - return Signature.VerifyMessagePart(ObjectId); - } + public bool VerifyIDSignature() + { + return Signature.VerifyMessagePart(ObjectId); + } - public void SetVerificationFields(ECDsa key) - { - Header.PayloadHash = CalculatePayloadChecksum(ChecksumType.Sha256); - Header.HomomorphicHash = CalculatePayloadChecksum(ChecksumType.Tz); - ObjectId = CalculateID(); - Signature = CalculateIDSignature(key); - } + public void SetVerificationFields(ECDsa key) + { + Header.PayloadHash = CalculatePayloadChecksum(ChecksumType.Sha256); + Header.HomomorphicHash = CalculatePayloadChecksum(ChecksumType.Tz); + ObjectId = CalculateID(); + Signature = CalculateIDSignature(key); + } - public bool CheckVerificationFields() - { - if (!VerifyIDSignature()) return false; - if (!VerifyID()) return false; - return true; - } + public bool CheckVerificationFields() + { + if (!VerifyIDSignature()) return false; + if (!VerifyID()) return false; + return true; + } - public Object CutPayload() - { - var obj = Clone(); - obj.payload_ = ByteString.Empty; - return obj; - } + public Object CutPayload() + { + var obj = Clone(); + obj.payload_ = ByteString.Empty; + return obj; + } - public StorageGroup.StorageGroup GetStorageGroup() - { - if (ObjectType != ObjectType.StorageGroup) - return null; - return StorageGroup.StorageGroup.Parser.ParseFrom(Payload); - } + public StorageGroup.StorageGroup GetStorageGroup() + { + if (ObjectType != ObjectType.StorageGroup) + return null; + return StorageGroup.StorageGroup.Parser.ParseFrom(Payload); + } - public Lock.Lock GetLock() - { - if (ObjectType != ObjectType.Lock) - return null; - return Lock.Lock.Parser.ParseFrom(Payload); - } + public Lock.Lock GetLock() + { + if (ObjectType != ObjectType.Lock) + return null; + return Lock.Lock.Parser.ParseFrom(Payload); + } - public Tombstone.Tombstone GetTombstone() - { - if (ObjectType != ObjectType.Tombstone) - return null; - return Tombstone.Tombstone.Parser.ParseFrom(Payload); + public Tombstone.Tombstone GetTombstone() + { + if (ObjectType != ObjectType.Tombstone) + return null; + return Tombstone.Tombstone.Parser.ParseFrom(Payload); + } } } diff --git a/src/Neo.FileStorage.API/refs/Extension.ContainerID.cs b/src/Neo.FileStorage.API/refs/Extension.ContainerID.cs index bde401e..0441a4e 100644 --- a/src/Neo.FileStorage.API/refs/Extension.ContainerID.cs +++ b/src/Neo.FileStorage.API/refs/Extension.ContainerID.cs @@ -14,28 +14,29 @@ using System; using static Neo.FileStorage.API.Cryptography.Helper; -namespace Neo.FileStorage.API.Refs; - -public partial class ContainerID +namespace Neo.FileStorage.API.Refs { - public const int ValueSize = Sha256HashLength; - - public static ContainerID FromValue(byte[] hash) + public partial class ContainerID { - if (hash.Length != Sha256HashLength) throw new FormatException("ContainerID must be a hash256"); - return new ContainerID + public const int ValueSize = Sha256HashLength; + + public static ContainerID FromValue(byte[] hash) { - Value = ByteString.CopyFrom(hash) - }; - } + if (hash.Length != Sha256HashLength) throw new FormatException("ContainerID must be a hash256"); + return new ContainerID + { + Value = ByteString.CopyFrom(hash) + }; + } - public static ContainerID FromString(string id) - { - return FromValue(Base58.Decode(id)); - } + public static ContainerID FromString(string id) + { + return FromValue(Base58.Decode(id)); + } - public string String() - { - return Base58.Encode(Value.ToByteArray()); + public string String() + { + return Base58.Encode(Value.ToByteArray()); + } } -} \ No newline at end of file +} diff --git a/src/Neo.FileStorage.API/status/Extension.Status.cs b/src/Neo.FileStorage.API/status/Extension.Status.cs index 0c4d4f4..e623616 100644 --- a/src/Neo.FileStorage.API/status/Extension.Status.cs +++ b/src/Neo.FileStorage.API/status/Extension.Status.cs @@ -13,71 +13,72 @@ using System; using System.Collections.Generic; -namespace Neo.FileStorage.API.Status; - -public sealed partial class Status +namespace Neo.FileStorage.API.Status { - private static readonly Dictionary Sections = new() + public sealed partial class Status { - { typeof(Success), Section.Success }, - { typeof(CommonFail), Section.FailureCommon }, - { typeof(Object), Section.Object }, - { typeof(Container), Section.Container }, - { typeof(Session), Section.Session } - }; + private static readonly Dictionary Sections = new() + { + { typeof(Success), Section.Success }, + { typeof(CommonFail), Section.FailureCommon }, + { typeof(Object), Section.Object }, + { typeof(Container), Section.Container }, + { typeof(Session), Section.Session } + }; - private static readonly Dictionary SectionTypes = new() - { - { Section.Success, typeof(Success) }, - { Section.FailureCommon, typeof(CommonFail) }, - { Section.Object, typeof(Object) }, - { Section.Container, typeof(Container) }, - { Section.Session, typeof(Session) } - }; + private static readonly Dictionary SectionTypes = new() + { + { Section.Success, typeof(Success) }, + { Section.FailureCommon, typeof(CommonFail) }, + { Section.Object, typeof(Object) }, + { Section.Container, typeof(Container) }, + { Section.Session, typeof(Session) } + }; - public static uint Globalize(object ev) - { - if (Sections.TryGetValue(ev.GetType(), out var section)) return ((uint)(int)section << 10) + (uint)(int)ev; + public static uint Globalize(object ev) + { + if (Sections.TryGetValue(ev.GetType(), out var section)) return ((uint)(int)section << 10) + (uint)(int)ev; - throw new InvalidOperationException("invalid type"); - } + throw new InvalidOperationException("invalid type"); + } - public static object Localize(uint code) - { - var section = (Section)(code / 1024); - var ev = code % 1024; - if (SectionTypes.TryGetValue(section, out var t)) return Enum.ToObject(t, ev); + public static object Localize(uint code) + { + var section = (Section)(code / 1024); + var ev = code % 1024; + if (SectionTypes.TryGetValue(section, out var t)) return Enum.ToObject(t, ev); - throw new InvalidOperationException("invalid code"); - } + throw new InvalidOperationException("invalid code"); + } - private static bool InSection(uint code, Section i) - { - return code >= (int)i << 10 && code < (int)(i + 1) << 10; - } + private static bool InSection(uint code, Section i) + { + return code >= (int)i << 10 && code < (int)(i + 1) << 10; + } - public bool IsSuccess() - { - return InSection(Code, Section.Success); - } + public bool IsSuccess() + { + return InSection(Code, Section.Success); + } - private static StatusCode ToGrpcStatusCode(uint code) - { - var ev = Localize(code); - return ev switch + private static StatusCode ToGrpcStatusCode(uint code) { - Success.Ok => StatusCode.OK, - CommonFail.Internal => StatusCode.Internal, - Object.AccessDenied => StatusCode.PermissionDenied, - Object.NotFound => StatusCode.NotFound, - Container.NotFound => StatusCode.NotFound, - Session.TokenExpired => StatusCode.Unauthenticated, - _ => StatusCode.Unknown - }; - } + var ev = Localize(code); + return ev switch + { + Success.Ok => StatusCode.OK, + CommonFail.Internal => StatusCode.Internal, + Object.AccessDenied => StatusCode.PermissionDenied, + Object.NotFound => StatusCode.NotFound, + Container.NotFound => StatusCode.NotFound, + Session.TokenExpired => StatusCode.Unauthenticated, + _ => StatusCode.Unknown + }; + } - public Grpc.Core.Status ToGrpcStatus() - { - return new Grpc.Core.Status(ToGrpcStatusCode(Code), Message); + public Grpc.Core.Status ToGrpcStatus() + { + return new Grpc.Core.Status(ToGrpcStatusCode(Code), Message); + } } } diff --git a/tests/Neo.FileStorage.API.UnitTests/Status/UT_Status.cs b/tests/Neo.FileStorage.API.UnitTests/Status/UT_Status.cs index f5ff343..0d1356f 100644 --- a/tests/Neo.FileStorage.API.UnitTests/Status/UT_Status.cs +++ b/tests/Neo.FileStorage.API.UnitTests/Status/UT_Status.cs @@ -12,26 +12,27 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using Neo.FileStorage.API.Status; -namespace Neo.FileStorage.API.UnitTests.Status; - -[TestClass] -public class UT_Status +namespace Neo.FileStorage.API.UnitTests.Status { - [TestMethod] - public void TestGlobalize() + [TestClass] + public class UT_Status { - var code1 = Success.Ok; - Assert.AreEqual(0u, API.Status.Status.Globalize(code1)); - var code2 = CommonFail.Internal; - Assert.AreEqual(1024u, API.Status.Status.Globalize(code2)); - var code3 = API.Status.Object.NotFound; - Assert.AreEqual(2049u, API.Status.Status.Globalize(code3)); - } + [TestMethod] + public void TestGlobalize() + { + var code1 = Success.Ok; + Assert.AreEqual(0u, API.Status.Status.Globalize(code1)); + var code2 = CommonFail.Internal; + Assert.AreEqual(1024u, API.Status.Status.Globalize(code2)); + var code3 = API.Status.Object.NotFound; + Assert.AreEqual(2049u, API.Status.Status.Globalize(code3)); + } - [TestMethod] - public void TestLocalize() - { - Assert.AreEqual(Success.Ok, API.Status.Status.Localize(0)); - Assert.AreEqual(CommonFail.Internal, API.Status.Status.Localize(1024)); + [TestMethod] + public void TestLocalize() + { + Assert.AreEqual(Success.Ok, API.Status.Status.Localize(0)); + Assert.AreEqual(CommonFail.Internal, API.Status.Status.Localize(1024)); + } } -} \ No newline at end of file +}