Skip to content

Commit 2da28a6

Browse files
authored
Support bech32 addresses in rpc methods (#211)
1 parent 7a30a04 commit 2da28a6

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

src/Features/Blockcore.Features.Wallet/Api/Controllers/WalletRPCController.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public async Task<uint256> SendTransactionAsync(string hex)
167167
/// Uses the first wallet and account.
168168
/// </summary>
169169
/// <param name="account">Parameter is deprecated.</param>
170-
/// <param name="addressType">Address type, currently only 'legacy' is supported.</param>
170+
/// <param name="addressType">Address type, currently only 'legacy' and 'bech32' is supported.</param>
171171
/// <returns>The new address.</returns>
172172
[ActionName("getnewaddress")]
173173
[ActionDescription("Returns a new wallet address for receiving payments.")]
@@ -178,26 +178,28 @@ public NewAddressModel GetNewAddress(string account = "", string addressType = "
178178

179179
if (!string.IsNullOrEmpty(addressType))
180180
{
181-
// Currently segwit and bech32 addresses are not supported.
182-
if (!addressType.Equals("legacy", StringComparison.InvariantCultureIgnoreCase))
183-
throw new RPCServerException(RPCErrorCode.RPC_METHOD_NOT_FOUND, "Only address type 'legacy' is currently supported.");
181+
// Currently segwit addresses are not supported.
182+
if (!addressType.Equals("legacy", StringComparison.InvariantCultureIgnoreCase) && !addressType.Equals("bech32", StringComparison.InvariantCultureIgnoreCase))
183+
throw new RPCServerException(RPCErrorCode.RPC_METHOD_NOT_FOUND, "Only address type 'legacy' and 'bech32' are currently supported.");
184184
}
185185

186186
WalletAccountReference accountReference = this.GetWalletAccountReference();
187187

188188
HdAddress hdAddress = this.walletManager.GetUnusedAddresses(accountReference, 1, alwaysnew: true).Single();
189189

190-
string base58Address = hdAddress.Address;
190+
string address = hdAddress.Address; // legacy address
191+
if (addressType != null && addressType.Equals("bech32", StringComparison.InvariantCultureIgnoreCase))
192+
address = hdAddress.Bech32Address;
191193

192-
return new NewAddressModel(base58Address);
194+
return new NewAddressModel(address);
193195
}
194196

195197
/// <summary>
196198
/// RPC method that gets the last unused address for receiving payments.
197199
/// Uses the first wallet and account.
198200
/// </summary>
199201
/// <param name="account">Parameter is deprecated.</param>
200-
/// <param name="addressType">Address type, currently only 'legacy' is supported.</param>
202+
/// <param name="addressType">Address type, currently only 'legacy' and 'bech32' are supported.</param>
201203
/// <returns>The new address.</returns>
202204
[ActionName("getunusedaddress")]
203205
[ActionDescription("Returns the last unused address for receiving payments.")]
@@ -207,15 +209,18 @@ public NewAddressModel GetUnusedAddress(string account, string addressType)
207209
throw new RPCServerException(RPCErrorCode.RPC_METHOD_DEPRECATED, "Use of 'account' parameter has been deprecated");
208210

209211
if (!string.IsNullOrEmpty(addressType))
210-
{
211-
// Currently segwit and bech32 addresses are not supported.
212-
if (!addressType.Equals("legacy", StringComparison.InvariantCultureIgnoreCase))
213-
throw new RPCServerException(RPCErrorCode.RPC_METHOD_NOT_FOUND, "Only address type 'legacy' is currently supported.");
212+
{
213+
// Currently segwit addresses are not supported.
214+
if (!addressType.Equals("legacy", StringComparison.InvariantCultureIgnoreCase) && !addressType.Equals("bech32", StringComparison.InvariantCultureIgnoreCase))
215+
throw new RPCServerException(RPCErrorCode.RPC_METHOD_NOT_FOUND, "Only address type 'legacy' and 'bech32' are currently supported.");
214216
}
215217
HdAddress hdAddress = this.walletManager.GetUnusedAddress(this.GetWalletAccountReference());
216-
string base58Address = hdAddress.Address;
217218

218-
return new NewAddressModel(base58Address);
219+
string address = hdAddress.Address; // legacy address
220+
if (addressType != null && addressType.Equals("bech32", StringComparison.InvariantCultureIgnoreCase))
221+
address = hdAddress.Bech32Address;
222+
223+
return new NewAddressModel(address);
219224
}
220225

221226
/// <summary>

src/Tests/Blockcore.Features.RPC.Tests/Controller/WalletRPCControllerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void GetNewAddress_WithIncompatibleAddressType_ThrowsException()
8989
});
9090

9191
Assert.NotNull(exception);
92-
Assert.Equal("Only address type 'legacy' is currently supported.", exception.Message);
92+
Assert.Equal("Only address type 'legacy' and 'bech32' are currently supported.", exception.Message);
9393
}
9494

9595
[Fact]
@@ -113,7 +113,7 @@ public void GetUnusedAddress_WithIncompatibleAddressType_ThrowsException()
113113
});
114114

115115
Assert.NotNull(exception);
116-
Assert.Equal("Only address type 'legacy' is currently supported.", exception.Message);
116+
Assert.Equal("Only address type 'legacy' and 'bech32' are currently supported.", exception.Message);
117117
}
118118
}
119119
}

0 commit comments

Comments
 (0)