Example #1
0
        /// <summary>
        /// Parses a <see cref="ByteString"/> into an instance of a <see cref="RecordKey"/> object.
        /// </summary>
        /// <param name="keyData">The <see cref="ByteString"/> to parse.</param>
        /// <returns>The parsed <see cref="RecordKey"/>.</returns>
        public static RecordKey Parse(ByteString keyData)
        {
            if (keyData == null)
            {
                throw new ArgumentNullException(nameof(keyData));
            }

            byte[] key = keyData.ToByteArray();

            string[] parts = Encoding.UTF8.GetString(key, 0, key.Length).Split(new[] { ':' }, 3);
            if (parts.Length != 3)
            {
                throw new ArgumentOutOfRangeException(nameof(keyData));
            }

            RecordKey result = ParseRecord(
                parts[1],
                LedgerPath.Parse(parts[0]),
                parts[2]);

            // The byte representation of reencoded value must match the input
            if (!result.ToBinary().Equals(keyData))
            {
                throw new ArgumentOutOfRangeException(nameof(keyData));
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Parses a path represented as a string.
        /// </summary>
        /// <param name="path">The string to parse.</param>
        /// <param name="result">The parsed <see cref="LedgerPath"/>.</param>
        /// <returns>A boolean indicating whether the value could be parsed successfully.</returns>
        public static bool TryParse(string path, out LedgerPath result)
        {
            result = null;
            string[] segments = path.Split('/');
            if (segments.Length < 2 || segments[0] != string.Empty || segments[segments.Length - 1] != string.Empty)
            {
                return(false);
            }

            if (segments.Any(segment => !IsValidPathSegment(segment)))
            {
                return(false);
            }

            for (int i = 1; i < segments.Length - 1; i++)
            {
                if (segments[i] == string.Empty)
                {
                    return(false);
                }
            }

            result = new LedgerPath(path, segments.Skip(1).Take(segments.Length - 2));

            return(true);
        }
Example #3
0
        public static RecordKey ParseRecord(string recordType, LedgerPath ledgerPath, string name)
        {
            switch (recordType)
            {
            case "ACC":
                LedgerPath path = LedgerPath.Parse(name);
                return(new RecordKey(RecordType.Account, ledgerPath, path.FullPath));

            case "DATA":
                return(new RecordKey(RecordType.Data, ledgerPath, name));

            default:
                throw new ArgumentOutOfRangeException(nameof(recordType));
            }
        }
Example #4
0
        public AccountKey(LedgerPath account, LedgerPath asset)
        {
            if (account == null)
            {
                throw new ArgumentNullException(nameof(account));
            }

            if (asset == null)
            {
                throw new ArgumentNullException(nameof(asset));
            }

            this.Account = account;
            this.Asset   = asset;
            this.Key     = new RecordKey(RecordType.Account, account, asset.FullPath);
        }
Example #5
0
        public bool IsParentOf(LedgerPath child)
        {
            if (child.Segments.Count < this.Segments.Count)
            {
                return(false);
            }

            for (int i = 0; i < this.Segments.Count; i++)
            {
                if (!StringComparer.Ordinal.Equals(this.Segments[i], child.Segments[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #6
0
        /// <summary>
        /// Parses a path represented as a string.
        /// </summary>
        /// <param name="path">The string to parse.</param>
        /// <param name="result">The parsed <see cref="LedgerPath"/>.</param>
        /// <returns>A boolean indicating whether the value could be parsed successfully.</returns>
        public static bool TryParse(string path, out LedgerPath result)
        {
            result = null;
            string[] segments = path.Split('/');
            if (segments.Length < 2 || segments[0] != string.Empty || segments[segments.Length - 1] != string.Empty)
                return false;

            if (segments.Any(segment => !IsValidPathSegment(segment)))
                return false;

            for (int i = 1; i < segments.Length - 1; i++)
                if (segments[i] == string.Empty)
                    return false;

            result = new LedgerPath(path, segments.Skip(1).Take(segments.Length - 2));

            return true;
        }
Example #7
0
        /// <summary>
        /// Creates an instance of the <see cref="AccountStatus"/> class from an unparsed record.
        /// </summary>
        /// <param name="key">The key of the record.</param>
        /// <param name="record">The record to create the object from.</param>
        /// <returns>A new instance of the <see cref="AccountStatus"/> class.</returns>
        public static AccountStatus FromRecord(RecordKey key, Record record)
        {
            if (key.RecordType != RecordType.Account)
            {
                throw new ArgumentOutOfRangeException(nameof(key));
            }

            long amount;

            if (record.Value.Value.Count == 0)
            {
                amount = 0;
            }
            else if (record.Value.Value.Count == 8)
            {
                amount = BitConverter.ToInt64(record.Value.Value.Reverse().ToArray(), 0);
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(record));
            }

            return(new AccountStatus(new AccountKey(key.Path, LedgerPath.Parse(key.Name)), amount, record.Version));
        }
Example #8
0
 /// <summary>
 /// Creates a new instance of the <see cref="AccountKey"/> class from an account and asset.
 /// </summary>
 /// <param name="account">The account path.</param>
 /// <param name="asset">The asset path.</param>
 /// <returns>An instance of the <see cref="AccountKey"/> class representing the account and asset provided.</returns>
 public static AccountKey Parse(string account, string asset)
 {
     return(new AccountKey(
                LedgerPath.Parse(account),
                LedgerPath.Parse(asset)));
 }
Example #9
0
        public bool IsStrictParentOf(LedgerPath child)
        {
            if (child.Segments.Count <= this.Segments.Count)
                return false;

            for (int i = 0; i < this.Segments.Count; i++)
            {
                if (!StringComparer.Ordinal.Equals(this.Segments[i], child.Segments[i]))
                    return false;
            }

            return true;
        }
Example #10
0
 public RecordKey(RecordType recordType, LedgerPath path, string name)
 {
     this.RecordType = recordType;
     this.Path       = path;
     this.Name       = name;
 }