private static string Validate(string bech32, ref Network expectedNetwork) { if (bech32 == null) { throw new ArgumentNullException("bech32"); } var networks = new[] { expectedNetwork }; foreach (var network in networks) { var encoder = expectedNetwork.GetBech32Encoder(EncoderType, false); if (encoder == null) { continue; } try { byte witVersion; var data = encoder.Decode(bech32, out witVersion); if (data.Length == 20 && witVersion == 0) { return(bech32); } } catch (Bech32FormatException) { throw; } catch (FormatException) { continue; } } throw new FormatException("Invalid CoinWitPubKeyAddress"); }
public CoinWitPubKeyAddress(string bech32, Network expectedNetwork) : base(Validate(bech32, ref expectedNetwork), expectedNetwork) { this.Type = EncoderType; var encoder = expectedNetwork.GetBech32Encoder(EncoderType, true); byte witVersion; var decoded = encoder.Decode(bech32, out witVersion); _Hash = new WitKeyId(decoded); }
public static byte[] AddressToBin(string aAddress, Network aNetwork) { byte[] lData = null; const int SCRIPT_ADDRESS = 1; const int PUBKEY_ADDRESS = 0; if (!string.IsNullOrEmpty(aAddress)) { lData = (aNetwork.NetworkStringParser.GetBase58CheckEncoder()).IsValidData(aAddress) ? (aNetwork.NetworkStringParser.GetBase58CheckEncoder()).DecodeData(aAddress) : null; if (lData != null) { var lScriptVer = aNetwork.GetVersionBytes(SCRIPT_ADDRESS, true); var lPubVer = aNetwork.GetVersionBytes(PUBKEY_ADDRESS, true); if (!(((lScriptVer != null && lData.StartWith(lScriptVer)) && (lData.Length == lScriptVer.Length + 20)) || ((lPubVer != null && lData.StartWith(lPubVer)) && (lData.Length == lPubVer.Length + 20)))) { lData = null; } } else if (aNetwork.ChainParams.Capabilities.HasFlag(CapablityFlags.SegWitSupport)) { int i = Network.BEACH32_WITNESS_PUBKEY_ADDRESS; while (lData == null && i < Network.BEACH32_WITNESS_SCRIPT_ADDRESS) { var encoder = aNetwork.GetBech32Encoder(i, true); if (encoder == null) { continue; } Int32 type = i; i++; try { byte witVersion; var bytes = encoder.Decode(aAddress, out witVersion); if (witVersion == 0 && bytes.Length == 20 && type == Network.BEACH32_WITNESS_PUBKEY_ADDRESS) { lData = bytes; } else if (witVersion == 0 && bytes.Length == 32 && type == Network.BEACH32_WITNESS_SCRIPT_ADDRESS) { lData = bytes; } } catch (Bech32FormatException) { throw; } catch (FormatException) { continue; } } } } return(lData); }
public static string CreateBech32(Bech32Type type, byte[] bytes, byte witnessVersion, Network network) { if (network == null) { throw new ArgumentNullException("network"); } if (bytes == null) { throw new ArgumentNullException("bytes"); } var encoder = network.GetBech32Encoder(type, true); return(encoder.Encode(witnessVersion, bytes)); }