private void SetString(string psz) { if (_Network == null) { _Network = Network.GetNetworkFromBase58Data(psz, Type); if (_Network == null) { throw new FormatException("Invalid " + this.GetType().Name); } } byte[] vchTemp = Encoders.Base58Check.DecodeData(psz); var expectedVersion = _Network.GetVersionBytes(Type, true); vchVersion = vchTemp.SafeSubarray(0, expectedVersion.Length); if (!Utils.ArrayEqual(vchVersion, expectedVersion)) { throw new FormatException("The version prefix does not match the expected one " + String.Join(",", expectedVersion)); } vchData = vchTemp.SafeSubarray(expectedVersion.Length); wifData = psz; if (!IsValid) { throw new FormatException("Invalid " + this.GetType().Name); } }
private void SetString <T>(string psz) where T : Base58Data { if (_Network == null) { _Network = Network.GetNetworkFromBase58Data(psz, Type); if (_Network == null) { throw new FormatException("Invalid " + this.GetType().Name); } } byte[] vchTemp = _Network.NetworkStringParser.GetBase58CheckEncoder().DecodeData(psz); var expectedVersion = _Network.GetVersionBytes(Type, true); vchVersion = vchTemp.SafeSubarray(0, expectedVersion.Length); if (!Utils.ArrayEqual(vchVersion, expectedVersion)) { if (_Network.NetworkStringParser.TryParse(psz, Network, out T other)) { this.vchVersion = other.vchVersion; this.vchData = other.vchData; this.wifData = other.wifData; } else { throw new FormatException("The version prefix does not match the expected one " + String.Join(",", expectedVersion)); } } else { vchData = vchTemp.SafeSubarray(expectedVersion.Length); wifData = psz; } if (!IsValid) { throw new FormatException("Invalid " + this.GetType().Name); } }
private void SetString <T>(string psz) where T : Base58Data { if (_Network == null) { _Network = Network.GetNetworkFromBase58Data(psz, Type); if (_Network == null) { throw new FormatException("Invalid " + this.GetType().Name); } } byte[] vchTemp = _Network.NetworkStringParser.GetBase58CheckEncoder().DecodeData(psz); #if HAS_SPAN if (!(_Network.GetVersionMemory(Type, false) is ReadOnlyMemory <byte> expectedVersion)) { throw new FormatException("Invalid " + this.GetType().Name); } #else var expectedVersion = _Network.GetVersionBytes(Type, false); if (expectedVersion is null) { throw new FormatException("Invalid " + this.GetType().Name); } #endif #if HAS_SPAN var vchTempMemory = vchTemp.AsMemory(); vchVersion = vchTempMemory.Slice(0, expectedVersion.Length); #else vchVersion = vchTemp.SafeSubarray(0, expectedVersion.Length); #endif #if HAS_SPAN if (!vchVersion.Span.SequenceEqual(expectedVersion.Span)) #else if (!Utils.ArrayEqual(vchVersion, expectedVersion)) #endif { if (_Network.NetworkStringParser.TryParse(psz, Network, out T other)) { this.vchVersion = other.vchVersion; this.vchData = other.vchData; this.wifData = other.wifData; } else { #if HAS_SPAN var expectedVersionHexString = Encoders.Hex.EncodeData(expectedVersion.Span); #else var expectedVersionHexString = Encoders.Hex.EncodeData(expectedVersion); #endif throw new FormatException($"The version prefix does not match the expected one 0x{expectedVersionHexString}"); } } else { #if HAS_SPAN vchData = vchTempMemory.Slice(expectedVersion.Length).ToArray(); #else vchData = vchTemp.SafeSubarray(expectedVersion.Length); #endif wifData = psz; } if (!IsValid) { throw new FormatException("Invalid " + this.GetType().Name); } }