ToArray() public method

Returns the IPFS binary representation as a byte array.
The binary representation is a sequence of MultiHash.
public ToArray ( ) : byte[]
return byte[]
Example #1
0
        /// <summary>
        ///   Returns a string representation of the CID
        ///   according to the provided format specifier.
        /// </summary>
        /// <param name="format">
        ///   A single format specifier that indicates how to format the value of the
        ///   CID.  Can be "G" or "L".
        /// </param>
        /// <returns>
        ///   The CID in the specified <paramref name="format"/>.
        /// </returns>
        /// <exception cref="FormatException">
        ///   <paramref name="format"/> is not valid.
        /// </exception>
        /// <remarks>
        /// <para>
        ///   The "G" format specifier is the same as calling <see cref="Encode"/>.
        /// </para>
        /// <list type="table">
        /// <listheader>
        ///   <term>Specifier</term>
        ///   <description>return value</description>
        /// </listheader>
        ///  <item>
        ///    <term>G</term>
        ///    <description>QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V</description>
        ///  </item>
        ///  <item>
        ///    <term>L</term>
        ///    <description>base58btc cidv0 dag-pb sha2-256 Qm...</description>
        ///  </item>
        /// </list>
        /// </remarks>
        public string ToString(string format)
        {
            switch (format)
            {
            case "G":
                return(Encode());

            case "L":
                var sb = new StringBuilder();
                sb.Append(Encoding);
                sb.Append(' ');
                sb.Append("cidv");
                sb.Append(Version);
                sb.Append(' ');
                sb.Append(ContentType);
                if (Hash != null)
                {
                    sb.Append(' ');
                    sb.Append(Hash.Algorithm.Name);
                    sb.Append(' ');
                    sb.Append(MultiBase.Encode(Hash.ToArray(), Encoding).Substring(1));
                }
                return(sb.ToString());

            default:
                throw new FormatException($"Invalid CID format specifier '{format}'.");
            }
        }
Example #2
0
        public override void WriteValue(CodedOutputStream stream)
        {
            var bytes = MultiHash.ToArray();

            stream.WriteLength(bytes.Length);
            stream.WriteSomeBytes(bytes);
        }
Example #3
0
        /// <summary>
        ///   A CID that is readable by a human.
        /// </summary>
        /// <returns>
        ///  e.g. "base58btc cidv0 dag-pb sha2-256 Qm..."
        /// </returns>
        public override string ToString()
        {
            var sb = new StringBuilder();

            sb.Append(Encoding);
            sb.Append(' ');
            sb.Append("cidv");
            sb.Append(Version);
            sb.Append(' ');
            sb.Append(ContentType);
            if (Hash != null)
            {
                sb.Append(' ');
                sb.Append(Hash.Algorithm.Name);
                sb.Append(' ');
                sb.Append(MultiBase.Encode(Hash.ToArray(), Encoding).Substring(1));
            }
            return(sb.ToString());
        }
Example #4
0
        public void Binary()
        {
            var mh = new MultiHash("QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB");

            Assert.AreEqual("sha2-256", mh.Algorithm.Name);
            Assert.AreEqual(32, mh.Digest.Length);

            var binary = mh.ToArray();
            var mh1    = new MultiHash(binary);

            Assert.AreEqual(mh.Algorithm.Name, mh1.Algorithm.Name);
            CollectionAssert.AreEqual(mh.Digest, mh1.Digest);
        }