diff --git a/src/Features/Blockcore.Features.Wallet/Api/Controllers/WalletRPCController.cs b/src/Features/Blockcore.Features.Wallet/Api/Controllers/WalletRPCController.cs index 4057970ad..85be70da2 100644 --- a/src/Features/Blockcore.Features.Wallet/Api/Controllers/WalletRPCController.cs +++ b/src/Features/Blockcore.Features.Wallet/Api/Controllers/WalletRPCController.cs @@ -167,7 +167,7 @@ public async Task SendTransactionAsync(string hex) /// Uses the first wallet and account. /// /// Parameter is deprecated. - /// Address type, currently only 'legacy' is supported. + /// Address type, currently only 'legacy' and 'bech32' is supported. /// The new address. [ActionName("getnewaddress")] [ActionDescription("Returns a new wallet address for receiving payments.")] @@ -178,18 +178,20 @@ public NewAddressModel GetNewAddress(string account = "", string addressType = " if (!string.IsNullOrEmpty(addressType)) { - // Currently segwit and bech32 addresses are not supported. - if (!addressType.Equals("legacy", StringComparison.InvariantCultureIgnoreCase)) - throw new RPCServerException(RPCErrorCode.RPC_METHOD_NOT_FOUND, "Only address type 'legacy' is currently supported."); + // Currently segwit addresses are not supported. + if (!addressType.Equals("legacy", StringComparison.InvariantCultureIgnoreCase) && !addressType.Equals("bech32", StringComparison.InvariantCultureIgnoreCase)) + throw new RPCServerException(RPCErrorCode.RPC_METHOD_NOT_FOUND, "Only address type 'legacy' and 'bech32' are currently supported."); } WalletAccountReference accountReference = this.GetWalletAccountReference(); HdAddress hdAddress = this.walletManager.GetUnusedAddresses(accountReference, 1, alwaysnew: true).Single(); - string base58Address = hdAddress.Address; + string address = hdAddress.Address; // legacy address + if (addressType != null && addressType.Equals("bech32", StringComparison.InvariantCultureIgnoreCase)) + address = hdAddress.Bech32Address; - return new NewAddressModel(base58Address); + return new NewAddressModel(address); } /// @@ -197,7 +199,7 @@ public NewAddressModel GetNewAddress(string account = "", string addressType = " /// Uses the first wallet and account. /// /// Parameter is deprecated. - /// Address type, currently only 'legacy' is supported. + /// Address type, currently only 'legacy' and 'bech32' are supported. /// The new address. [ActionName("getunusedaddress")] [ActionDescription("Returns the last unused address for receiving payments.")] @@ -207,15 +209,18 @@ public NewAddressModel GetUnusedAddress(string account, string addressType) throw new RPCServerException(RPCErrorCode.RPC_METHOD_DEPRECATED, "Use of 'account' parameter has been deprecated"); if (!string.IsNullOrEmpty(addressType)) - { - // Currently segwit and bech32 addresses are not supported. - if (!addressType.Equals("legacy", StringComparison.InvariantCultureIgnoreCase)) - throw new RPCServerException(RPCErrorCode.RPC_METHOD_NOT_FOUND, "Only address type 'legacy' is currently supported."); + { + // Currently segwit addresses are not supported. + if (!addressType.Equals("legacy", StringComparison.InvariantCultureIgnoreCase) && !addressType.Equals("bech32", StringComparison.InvariantCultureIgnoreCase)) + throw new RPCServerException(RPCErrorCode.RPC_METHOD_NOT_FOUND, "Only address type 'legacy' and 'bech32' are currently supported."); } HdAddress hdAddress = this.walletManager.GetUnusedAddress(this.GetWalletAccountReference()); - string base58Address = hdAddress.Address; - return new NewAddressModel(base58Address); + string address = hdAddress.Address; // legacy address + if (addressType != null && addressType.Equals("bech32", StringComparison.InvariantCultureIgnoreCase)) + address = hdAddress.Bech32Address; + + return new NewAddressModel(address); } /// diff --git a/src/Tests/Blockcore.Features.RPC.Tests/Controller/WalletRPCControllerTests.cs b/src/Tests/Blockcore.Features.RPC.Tests/Controller/WalletRPCControllerTests.cs index 25c043bab..f836944d2 100644 --- a/src/Tests/Blockcore.Features.RPC.Tests/Controller/WalletRPCControllerTests.cs +++ b/src/Tests/Blockcore.Features.RPC.Tests/Controller/WalletRPCControllerTests.cs @@ -89,7 +89,7 @@ public void GetNewAddress_WithIncompatibleAddressType_ThrowsException() }); Assert.NotNull(exception); - Assert.Equal("Only address type 'legacy' is currently supported.", exception.Message); + Assert.Equal("Only address type 'legacy' and 'bech32' are currently supported.", exception.Message); } [Fact] @@ -113,7 +113,7 @@ public void GetUnusedAddress_WithIncompatibleAddressType_ThrowsException() }); Assert.NotNull(exception); - Assert.Equal("Only address type 'legacy' is currently supported.", exception.Message); + Assert.Equal("Only address type 'legacy' and 'bech32' are currently supported.", exception.Message); } } } \ No newline at end of file