public void VerifyTypeTest(string address, PubkeyScriptType scrType, byte[] expected) { Address addr = new Address(); bool b = addr.VerifyType(address, scrType, out byte[] actual); Assert.True(b); Assert.Equal(expected, actual); }
internal string GetAddress(PublicKey pubkey, PubkeyScriptType addrType, NetworkType netType, bool compressed) { return(addrType switch { PubkeyScriptType.P2PKH => GetP2pkh(pubkey, netType, compressed), PubkeyScriptType.P2WPKH => GetP2wpkh(pubkey, 0, netType), PubkeyScriptType.P2WSH => GetP2wsh(pubkey, 0, netType), _ => throw new ArgumentException($"Address is not defined for {addrType.ToString()} type of script."), });
public MockDeserializablePubScript(PubkeyScriptType pubT, int streamIndex, int bytesToRead, string errorToReturn = null) : base(streamIndex, bytesToRead, errorToReturn) { typeToReturn = pubT; }
public MockSerializablePubScript(PubkeyScriptType typeRes, byte[] data, byte streamFirstByte) : base(data, streamFirstByte) { typeToReturn = typeRes; }
public void GetPublicScriptTypeTest(byte[] data, PubkeyScriptType expected) { PubkeyScript scr = new PubkeyScript(data); Assert.Equal(expected, scr.GetPublicScriptType()); }
public string ToAddress(PubkeyScriptType addrType, NetworkType netType, bool compressed) { return(addrMaker.GetAddress(this, addrType, netType, compressed)); }
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); }
/// <summary> /// Checks if the given address string is of the given <see cref="PubkeyScriptType"/> type and returns the /// decoded hash from the address. /// </summary> /// <param name="address">Address to check</param> /// <param name="scrType">The public key script type to check against</param> /// <param name="hash">The hash used in creation of this address</param> /// <returns>True if the address is the same script type, otherwise false</returns> public bool VerifyType(string address, PubkeyScriptType scrType, out byte[] hash) { hash = null; if (string.IsNullOrWhiteSpace(address)) { return(false); } switch (scrType) { case PubkeyScriptType.P2PKH: if (!b58Encoder.IsValid(address)) { return(false); } byte[] decoded = b58Encoder.DecodeWithCheckSum(address); if (decoded[0] != P2pkhVerMainNet && decoded[0] != P2pkhVerTestNet && decoded[0] != P2pkhVerRegTest || decoded.Length != 21) { return(false); } hash = decoded.SubArray(1); return(true); case PubkeyScriptType.P2SH: if (!b58Encoder.IsValid(address)) { return(false); } decoded = b58Encoder.DecodeWithCheckSum(address); if (decoded[0] != P2shVerMainNet && decoded[0] != P2shVerTestNet && decoded[0] != P2shVerRegTest || decoded.Length != 21) { return(false); } hash = decoded.SubArray(1); return(true); case PubkeyScriptType.P2WPKH: if (!b32Encoder.IsValid(address)) { return(false); } decoded = b32Encoder.Decode(address, out byte witVer, out string hrp); if (witVer != 0 || decoded.Length != 20 || (hrp != HrpMainNet && hrp != HrpTestNet && hrp != HrpRegTest)) { return(false); } hash = decoded; return(true); case PubkeyScriptType.P2WSH: if (!b32Encoder.IsValid(address)) { return(false); } decoded = b32Encoder.Decode(address, out witVer, out hrp); if (witVer != 0 || decoded.Length != 32 || (hrp != HrpMainNet && hrp != HrpTestNet && hrp != HrpRegTest)) { return(false); } hash = decoded; return(true); default: return(false); } }