public string CheckBase58Address(string address) { if (!Base58.IsValid(address)) { return("The given address contains invalid base-58 characters."); } if (!Base58.IsValidWithChecksum(address)) { return("The given address has an invalid checksum."); } byte[] addrBa = Base58.DecodeWithChecksum(address); if (addrBa[0] != ConstantsFO.P2pkhAddrFirstByte && addrBa[0] != ConstantsFO.P2shAddrFirstByte) { return("The given address starts with an invalid byte."); } if (addrBa.Length != 21) { return("The given address byte length is invalid."); } return($"The given address is a valid base-58 encoded address used for " + $"{(addrBa[0] == ConstantsFO.P2pkhAddrFirstByte ? "P2PKH" : "P2SH")} scripts."); }
public string CheckPrivateKey(string key) { if (!Base58.IsValid(key)) { return("The given key contains invalid base-58 characters."); } if (!Base58.IsValidWithChecksum(key)) { return("The given key has an invalid checksum."); } byte[] keyBa = Base58.DecodeWithChecksum(key); if (keyBa[0] != ConstantsFO.PrivKeyFirstByte) { return($"Invalid first key byte (actual={keyBa[0]}, expected={ConstantsFO.PrivKeyFirstByte})."); } if (keyBa.Length == 33) { if (!IsPrivateKeyInRange(keyBa.SubArray(1))) { return("Invalid key integer value (outside of the range defined by secp256k1 curve)."); } return("The given key is a valid uncompressed private key."); } else if (keyBa.Length == 34) { if (keyBa[^ 1] != ConstantsFO.PrivKeyCompLastByte)
public bool CheckBase58Bip38(string bip38, out string message) { if (!Base58.IsValid(bip38)) { message = "The given BIP-38 string contains invalid base-58 characters."; return(false); } if (!Base58.IsValidWithChecksum(bip38)) { message = "The given BIP-38 string has an invalid checksum."; return(false); } byte[] data = Base58.DecodeWithChecksum(bip38); if (data.Length != ConstantsFO.Bip38ByteLen) { message = "The given BIP-38 string has an invalid byte length."; return(false); } if (data[0] != 1 || (data[1] != 0x42 && data[1] != 0x43)) { message = "The given BIP-38 string has invalid starting bytes."; return(false); } message = "The given BIP-38 string is valid."; return(true); }
[InlineData("1BvBMSOYstWetqTFn5Au4m4GFg7xJaNVN2", false)] // Invalid char public void IsValidWithChecksumTest(string s, bool expected) { Assert.Equal(expected, Base58.IsValidWithChecksum(s)); }