Ejemplo n.º 1
0
        /// <summary>
        ///   Register a new IPFS algorithm.
        /// </summary>
        /// <param name="name">
        ///   The name of the algorithm.
        /// </param>
        /// <param name="code">
        ///   The IPFS code assigned to thealgorithm.
        /// </param>
        /// <param name="encode">
        ///   A <c>Func</c> to encode a byte array.  If not specified, then a <c>Func</c> is created to
        ///   throw a <see cref="NotImplementedException"/>.
        /// </param>
        /// <param name="decode">
        ///   A <c>Func</c> to decode a string.  If not specified, then a <c>Func</c> is created to
        ///   throw a <see cref="NotImplementedException"/>.
        /// </param>
        /// <returns>
        ///   A new <see cref="MultiBaseAlgorithm"/>.
        /// </returns>
        public static MultiBaseAlgorithm Register(string name,
                                                  char code,
                                                  Func <byte[], string> encode = null,
                                                  Func <string, byte[]> decode = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException(nameof(name));
            }
            if (Names.ContainsKey(name))
            {
                throw new ArgumentException(
                          string.Format("The IPFS multi-base algorithm name '{0}' is already defined.", name));
            }
            if (Codes.ContainsKey(code))
            {
                throw new ArgumentException(
                          string.Format("The IPFS multi-base algorithm code '{0}' is already defined.", code));
            }
            if (encode == null)
            {
                encode = (bytes) =>
                {
                    throw new NotImplementedException(
                              string.Format("The IPFS encode multi-base algorithm '{0}' is not implemented.", name));
                }
            }
            ;

            if (decode == null)
            {
                decode = (s) =>
                {
                    throw new NotImplementedException(
                              string.Format("The IPFS decode multi-base algorithm '{0}' is not implemented.", name));
                }
            }
            ;

            var a = new MultiBaseAlgorithm
            {
                Name   = name,
                Code   = code,
                Encode = encode,
                Decode = decode
            };

            Names[name] = a;
            Codes[code] = a;

            return(a);
        }
Ejemplo n.º 2
0
 /// <summary>
 ///   Remove an IPFS algorithm from the registry.
 /// </summary>
 /// <param name="algorithm">
 ///   The <see cref="MultiBaseAlgorithm"/> to remove.
 /// </param>
 public static void Deregister(MultiBaseAlgorithm algorithm)
 {
     Names.Remove(algorithm.Name);
     Codes.Remove(algorithm.Code);
 }