Skip to content

Commit 0c246e4

Browse files
When segwit address is used to sign, we need to return the address signed with. (#142)
1 parent b3169a7 commit 0c246e4

5 files changed

Lines changed: 34 additions & 9 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Blockcore.Features.BlockStore.Models
4+
{
5+
/// <summary>
6+
/// Class containing details of a signature message.
7+
/// </summary>
8+
public class SignMessageResult
9+
{
10+
[JsonProperty(PropertyName = "signedAddress")]
11+
public string SignedAddress { get; set; }
12+
13+
[JsonProperty(PropertyName = "signature")]
14+
public string Signature { get; set; }
15+
}
16+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text;
88
using Blockcore.Connection;
99
using Blockcore.Connection.Broadcasting;
10+
using Blockcore.Features.BlockStore.Models;
1011
using Blockcore.Features.Wallet.Api.Models;
1112
using Blockcore.Features.Wallet.Exceptions;
1213
using Blockcore.Features.Wallet.Interfaces;
@@ -145,7 +146,7 @@ public IActionResult Create([FromBody]WalletCreationRequest request)
145146
/// Signs a message and returns the signature.
146147
/// </summary>
147148
/// <param name="request">The object containing the parameters used to sign a message.</param>
148-
/// <returns>A JSON object containing the generated signature.</returns>
149+
/// <returns>A JSON object containing the generated signature and the address used to sign.</returns>
149150
[Route("signmessage")]
150151
[HttpPost]
151152
public IActionResult SignMessage([FromBody]SignMessageRequest request)
@@ -160,7 +161,7 @@ public IActionResult SignMessage([FromBody]SignMessageRequest request)
160161

161162
try
162163
{
163-
string signature = this.walletManager.SignMessage(request.Password, request.WalletName, request.AccountName, request.ExternalAddress, request.Message);
164+
SignMessageResult signature = this.walletManager.SignMessage(request.Password, request.WalletName, request.AccountName, request.ExternalAddress, request.Message);
164165
return this.Json(signature);
165166
}
166167
catch (Exception e)

src/Features/Blockcore.Features.Wallet/Interfaces/IWalletManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using Blockcore.Features.BlockStore.Models;
34
using Blockcore.Features.Wallet.Types;
45
using NBitcoin;
56
using NBitcoin.BuilderExtensions;
@@ -89,7 +90,7 @@ public interface IWalletManager
8990
/// <param name="externalAddress">Address to use to sign.</param>
9091
/// <param name="message">Message to sign.</param>
9192
/// <returns>The generated signature.</returns>
92-
string SignMessage(string password, string walletName, string accountName, string externalAddress, string message);
93+
SignMessageResult SignMessage(string password, string walletName, string accountName, string externalAddress, string message);
9394

9495
/// <summary>
9596
/// Verifies the signed message.

src/Features/Blockcore.Features.Wallet/WalletManager.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Blockcore.Configuration;
1010
using Blockcore.Connection.Broadcasting;
1111
using Blockcore.EventBus;
12+
using Blockcore.Features.BlockStore.Models;
1213
using Blockcore.Features.Wallet.Exceptions;
1314
using Blockcore.Features.Wallet.Helpers;
1415
using Blockcore.Features.Wallet.Interfaces;
@@ -314,7 +315,7 @@ public Mnemonic CreateWallet(string password, string name, string passphrase, Mn
314315
}
315316

316317
/// <inheritdoc />
317-
public string SignMessage(string password, string walletName, string accountName, string externalAddress, string message)
318+
public SignMessageResult SignMessage(string password, string walletName, string accountName, string externalAddress, string message)
318319
{
319320
Guard.NotEmpty(password, nameof(password));
320321
Guard.NotEmpty(walletName, nameof(walletName));
@@ -328,7 +329,11 @@ public string SignMessage(string password, string walletName, string accountName
328329
// Sign the message.
329330
HdAddress hdAddress = wallet.GetAddress(externalAddress, account => account.Name.Equals(accountName));
330331
Key privateKey = wallet.GetExtendedPrivateKeyForAddress(password, hdAddress).PrivateKey;
331-
return privateKey.SignMessage(message);
332+
return new SignMessageResult()
333+
{
334+
Signature = privateKey.SignMessage(message),
335+
SignedAddress = hdAddress.Address
336+
};
332337
}
333338

334339
/// <inheritdoc />

src/Tests/Blockcore.IntegrationTests/Wallet/WalletOperationsTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Net;
66
using System.Runtime.CompilerServices;
77
using System.Threading.Tasks;
8+
using Blockcore.Features.BlockStore.Models;
89
using Blockcore.Features.Wallet;
910
using Blockcore.Features.Wallet.Api.Models;
1011
using Blockcore.Features.Wallet.Types;
@@ -1493,7 +1494,7 @@ public async Task GetWalletFiles()
14931494
public async Task SignMessage()
14941495
{
14951496
// Act.
1496-
string signatureResult = await $"http://localhost:{this.fixture.Node.ApiPort}/api"
1497+
SignMessageResult signatureResult = await $"http://localhost:{this.fixture.Node.ApiPort}/api"
14971498
.AppendPathSegment("wallet/signmessage")
14981499
.PostJsonAsync(new SignMessageRequest
14991500
{
@@ -1503,11 +1504,12 @@ public async Task SignMessage()
15031504
Password = this.fixture.walletWithFundsPassword,
15041505
Message = this.fixture.signatureMessage
15051506
})
1506-
.ReceiveJson<string>();
1507+
.ReceiveJson<SignMessageResult>();
15071508

15081509
// Assert.
1509-
signatureResult.Should().Be(this.fixture.validSignature, $"Signature is invalid.");
1510-
Encoders.Base64.DecodeData(signatureResult).Should().BeOfType<byte[]>($"Signature was not a {typeof(byte[])} type.");
1510+
signatureResult.SignedAddress.Should().Be(this.fixture.addressWithFunds, $"Returned address is invalid.");
1511+
signatureResult.Signature.Should().Be(this.fixture.validSignature, $"Signature is invalid.");
1512+
Encoders.Base64.DecodeData(signatureResult.Signature).Should().BeOfType<byte[]>($"Signature was not a {typeof(byte[])} type.");
15111513
}
15121514

15131515
[Fact]

0 commit comments

Comments
 (0)