public ValidatedAddress ValidateAddress([FromQuery] string address) { Guard.NotEmpty(address, nameof(address)); var result = new ValidatedAddress { IsValid = false, Address = address, }; try { // P2WPKH if (BitcoinWitPubKeyAddress.IsValid(address, this.network, out Exception _)) { result.IsValid = true; } // P2WSH else if (BitcoinWitScriptAddress.IsValid(address, this.network, out Exception _)) { result.IsValid = true; } // P2PKH else if (BitcoinPubKeyAddress.IsValid(address, this.network)) { result.IsValid = true; } // P2SH else if (BitcoinScriptAddress.IsValid(address, this.network)) { result.IsValid = true; result.IsScript = true; } } catch (Exception e) { this.logger.LogDebug("Exception occurred: {0}", e.ToString()); result.IsValid = false; return(result); } if (result.IsValid) { NBitcoin.Script scriptPubKey = BitcoinAddress.Create(address, this.network).ScriptPubKey; result.ScriptPubKey = scriptPubKey.ToHex(); result.IsWitness = scriptPubKey.IsWitness(this.network); } return(result); }
public IActionResult ValidateAddress([FromQuery] string address) { Guard.NotEmpty(address, nameof(address)); var result = new ValidatedAddress { IsValid = false, Address = address, }; try { // P2WPKH if (BitcoinWitPubKeyAddress.IsValid(address, this.network, out Exception _)) { result.IsValid = true; } // P2WSH else if (BitcoinWitScriptAddress.IsValid(address, this.network, out Exception _)) { result.IsValid = true; } // P2PKH else if (BitcoinPubKeyAddress.IsValid(address, this.network)) { result.IsValid = true; } // P2SH else if (BitcoinScriptAddress.IsValid(address, this.network)) { result.IsValid = true; result.IsScript = true; } } catch (Exception e) { this.logger.LogError("Exception occurred: {0}", e.ToString()); return(ErrorHelpers.BuildErrorResponse(HttpStatusCode.BadRequest, e.Message, e.ToString())); } if (result.IsValid) { NBitcoin.Script scriptPubKey = BitcoinAddress.Create(address, this.network).ScriptPubKey; result.ScriptPubKey = scriptPubKey.ToHex(); result.IsWitness = scriptPubKey.IsWitness(this.network); } return(this.Json(result)); }