Exemplo n.º 1
0
        /// <summary>
        /// Returns base58-encoded string representation of this instance.
        /// </summary>
        /// <exception cref="ObjectDisposedException"/>
        /// <exception cref="ArgumentNullException"/>
        /// <param name="getPublic">If true returns extended public key, otherwise extended private key if possible.</param>
        /// <param name="netType">[Default value = <see cref="NetworkType.MainNet"/>] Network type</param>
        /// <returns>Base58-encoded extended key</returns>
        public string ToBase58(bool getPublic, NetworkType netType = NetworkType.MainNet)
        {
            if (isDisposed)
            {
                throw new ObjectDisposedException(nameof(BIP0032), "Instance was disposed.");
            }
            if (!getPublic && PrvKey is null)
            {
                throw new ArgumentNullException(nameof(PrvKey), "Can not get extended private key from public key.");
            }

            byte[] ver = netType switch
            {
                NetworkType.MainNet => getPublic ? pubMainVer : prvMainVer,
                NetworkType.TestNet => getPublic ? pubTestVer : prvTestVer,
                NetworkType.RegTest => getPublic ? pubTestVer : prvTestVer,
                _ => throw new ArgumentException("Network type is not defined."),
            };

            FastStream stream = new FastStream(ExtendedKeyLength);

            stream.Write(ver);
            stream.Write(ExtendedKeyDepth);
            stream.Write(ParentFingerPrint);
            stream.Write(ChildNumber);
            stream.Write(ChainCode);
            if (getPublic)
            {
                stream.Write(PubKey.ToByteArray(true));
            }
            else
            {
                stream.Write((byte)0);
                stream.Write(PrvKey.ToBytes());
            }

            return(b58Encoder.EncodeWithCheckSum(stream.ToByteArray()));
        }
Exemplo n.º 2
0
        public void EncodeWithCheckSumTest(string expected, byte[] data)
        {
            string actual = encoder.EncodeWithCheckSum(data);

            Assert.Equal(expected, actual);
        }
Exemplo n.º 3
0
 public string BuildP2PKH(byte[] data)
 {
     return(b58Encoder.EncodeWithCheckSum(data.AppendToBeginning(versionByte_P2pkh_MainNet)));
 }