public void Validate(string address) { if (string.IsNullOrEmpty(address)) { AddError(nameof(address), "Address can not be empty!"); } else if (address.StartsWith("1") || address.StartsWith("3")) { Base58 b58enc = new Base58(); if (b58enc.IsValid(address)) { RemoveError(nameof(address), ""); } else { AddError(nameof(address), "Invalid Base58 encoded address!"); } } else if (address.StartsWith("bc")) { Bech32 b32enc = new Bech32(); if (b32enc.IsValid(address)) { RemoveError(nameof(address), ""); } else { AddError(nameof(address), "Invalid Bech32 encoded address!"); } } else { AddError(nameof(address), "Invalid address format!"); } }
public void IsValid_Fail_SpecialCaseTest() { // HRP has an out of range character Assert.False(Bech32.IsValid($"{(char)0x20}1nwldj5", Bech32.Mode.B32)); Assert.False(Bech32.IsValid($"{(char)0x20}1nwldj5", Bech32.Mode.B32m)); Assert.False(Bech32.IsValid($"{(char)0x20}1xj0phk", Bech32.Mode.B32)); Assert.False(Bech32.IsValid($"{(char)0x20}1xj0phk", Bech32.Mode.B32m)); Assert.False(Bech32.IsValid($"{(char)0x7F}1axkwrx", Bech32.Mode.B32)); Assert.False(Bech32.IsValid($"{(char)0x7F}1axkwrx", Bech32.Mode.B32m)); Assert.False(Bech32.IsValid($"{(char)0x7F}1g6xzxy", Bech32.Mode.B32)); Assert.False(Bech32.IsValid($"{(char)0x7F}1g6xzxy", Bech32.Mode.B32m)); Assert.False(Bech32.IsValid($"{(char)0x80}1eym55h", Bech32.Mode.B32)); Assert.False(Bech32.IsValid($"{(char)0x80}1eym55h", Bech32.Mode.B32m)); Assert.False(Bech32.IsValid($"{(char)0x80}1vctc34", Bech32.Mode.B32)); Assert.False(Bech32.IsValid($"{(char)0x80}1vctc34", Bech32.Mode.B32m)); // Invalid character in checksum Assert.False(Bech32.IsValid($"de1lg7wt{(char)0xFF}", Bech32.Mode.B32)); Assert.False(Bech32.IsValid($"de1lg7wt{(char)0xFF}", Bech32.Mode.B32m)); }
public void IsValidTest(string input) { Assert.True(encoder.IsValid(input)); }
public void IsValid_FailTest(string input, Bech32.Mode mode) { Assert.False(Bech32.IsValid(input, mode)); }
public void IsValidTest(string input, Bech32.Mode mode) { Assert.True(Bech32.IsValid(input, mode)); }
internal bool TryGetType(string address, out PubkeyScriptType scrType, out byte[] hash) { scrType = PubkeyScriptType.Unknown; if (string.IsNullOrWhiteSpace(address)) { hash = null; return(false); } try { if (b58Encoder.IsValid(address)) { byte[] decoded = b58Encoder.DecodeWithCheckSum(address); if (decoded.Length != hashFunc.HashByteSize + 1) { hash = null; return(false); } else if (decoded[0] != versionByte_P2pkh_MainNet || decoded[0] != versionByte_P2pkh_TestNet || decoded[0] != versionByte_P2pkh_RegTest) { scrType = PubkeyScriptType.P2PKH; hash = decoded.SubArray(1); return(true); } else if (decoded[0] != versionByte_P2sh_MainNet || decoded[0] != versionByte_P2sh_TestNet || decoded[0] != versionByte_P2sh_RegTest) { scrType = PubkeyScriptType.P2SH; hash = decoded.SubArray(1); return(true); } else { hash = null; return(false); } } else if (b32Encoder.IsValid(address)) { byte[] decoded = b32Encoder.Decode(address, out byte witVer, out string hrp); if (witVer != 0) { hash = null; return(false); } else if (decoded.Length == hashFunc.HashByteSize && hrp == hrp_MainNet || hrp == hrp_TestNet || hrp == hrp_RegTest) { scrType = PubkeyScriptType.P2WPKH; hash = decoded; return(true); } else if (decoded.Length == witHashFunc.HashByteSize && hrp == hrp_MainNet || hrp == hrp_TestNet || hrp == hrp_RegTest) { scrType = PubkeyScriptType.P2WPKH; hash = decoded; return(true); } else { hash = null; return(false); } } } catch (Exception) { } hash = null; return(false); }