BCNet.Utils.DecodeBase58StringChecked C# (CSharp) Method

DecodeBase58StringChecked() public static method

public static DecodeBase58StringChecked ( string encoded ) : byte[]
encoded string
return byte[]
        public static byte[] DecodeBase58StringChecked(string encoded)
        {
            byte[] test = DecodeBase58String(encoded);
            if (test == null || test.Length < 4)
                return null;

            byte[] hashData = new byte[test.Length - 4];
            Array.Copy(test, hashData, test.Length - 4);
            byte[] hash = BCNet.Utils.GenerateHash(hashData);

            int testIndex = test.Length - 4;
            for (int i = 0; i < 4; i++)
            {
                if (test[testIndex + i] != hash[i])
                    return null;
            }

            return hashData;
        }

Usage Example

        public void NodeDiscovered(string node)
        {
            byte[] decoded = Utils.DecodeBase58StringChecked(node.Substring(1));
            if (decoded == null)
            {
                return;
            }

            MemoryStream ms = new MemoryStream(decoded);
            BinaryReader br = new BinaryReader(ms);

            byte[] addrBytes = br.ReadBytes(4);
            string address   = "";

            for (int i = 0; i < 4; i++)
            {
                //byte b = addrBytes[3 - i];
                byte b = addrBytes[i];
                address += b.ToString();
                if (i < 3)
                {
                    address += ".";
                }
            }
            ushort port = Utils.Byteswap(br.ReadUInt16());

            AddNode(address, port);
            br.Close();
        }