Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When segwit address is used to sign, we need to return the address signed with. #142

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Newtonsoft.Json;

namespace Blockcore.Features.BlockStore.Models
{
/// <summary>
/// Class containing details of a signature message.
/// </summary>
public class SignMessageResult
{
[JsonProperty(PropertyName = "signedAddress")]
public string SignedAddress { get; set; }

[JsonProperty(PropertyName = "signature")]
public string Signature { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using Blockcore.Connection;
using Blockcore.Connection.Broadcasting;
using Blockcore.Features.BlockStore.Models;
using Blockcore.Features.Wallet.Api.Models;
using Blockcore.Features.Wallet.Exceptions;
using Blockcore.Features.Wallet.Interfaces;
Expand Down Expand Up @@ -145,7 +146,7 @@ public IActionResult Create([FromBody]WalletCreationRequest request)
/// Signs a message and returns the signature.
/// </summary>
/// <param name="request">The object containing the parameters used to sign a message.</param>
/// <returns>A JSON object containing the generated signature.</returns>
/// <returns>A JSON object containing the generated signature and the address used to sign.</returns>
[Route("signmessage")]
[HttpPost]
public IActionResult SignMessage([FromBody]SignMessageRequest request)
Expand All @@ -160,7 +161,7 @@ public IActionResult SignMessage([FromBody]SignMessageRequest request)

try
{
string signature = this.walletManager.SignMessage(request.Password, request.WalletName, request.AccountName, request.ExternalAddress, request.Message);
SignMessageResult signature = this.walletManager.SignMessage(request.Password, request.WalletName, request.AccountName, request.ExternalAddress, request.Message);
return this.Json(signature);
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Blockcore.Features.BlockStore.Models;
using Blockcore.Features.Wallet.Types;
using NBitcoin;
using NBitcoin.BuilderExtensions;
Expand Down Expand Up @@ -89,7 +90,7 @@ public interface IWalletManager
/// <param name="externalAddress">Address to use to sign.</param>
/// <param name="message">Message to sign.</param>
/// <returns>The generated signature.</returns>
string SignMessage(string password, string walletName, string accountName, string externalAddress, string message);
SignMessageResult SignMessage(string password, string walletName, string accountName, string externalAddress, string message);

/// <summary>
/// Verifies the signed message.
Expand Down
9 changes: 7 additions & 2 deletions src/Features/Blockcore.Features.Wallet/WalletManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Blockcore.Configuration;
using Blockcore.Connection.Broadcasting;
using Blockcore.EventBus;
using Blockcore.Features.BlockStore.Models;
using Blockcore.Features.Wallet.Exceptions;
using Blockcore.Features.Wallet.Helpers;
using Blockcore.Features.Wallet.Interfaces;
Expand Down Expand Up @@ -314,7 +315,7 @@ public Mnemonic CreateWallet(string password, string name, string passphrase, Mn
}

/// <inheritdoc />
public string SignMessage(string password, string walletName, string accountName, string externalAddress, string message)
public SignMessageResult SignMessage(string password, string walletName, string accountName, string externalAddress, string message)
{
Guard.NotEmpty(password, nameof(password));
Guard.NotEmpty(walletName, nameof(walletName));
Expand All @@ -328,7 +329,11 @@ public string SignMessage(string password, string walletName, string accountName
// Sign the message.
HdAddress hdAddress = wallet.GetAddress(externalAddress, account => account.Name.Equals(accountName));
Key privateKey = wallet.GetExtendedPrivateKeyForAddress(password, hdAddress).PrivateKey;
return privateKey.SignMessage(message);
return new SignMessageResult()
{
Signature = privateKey.SignMessage(message),
SignedAddress = hdAddress.Address
};
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Blockcore.Features.BlockStore.Models;
using Blockcore.Features.Wallet;
using Blockcore.Features.Wallet.Api.Models;
using Blockcore.Features.Wallet.Types;
Expand Down Expand Up @@ -1493,7 +1494,7 @@ public async Task GetWalletFiles()
public async Task SignMessage()
{
// Act.
string signatureResult = await $"http://localhost:{this.fixture.Node.ApiPort}/api"
SignMessageResult signatureResult = await $"http://localhost:{this.fixture.Node.ApiPort}/api"
.AppendPathSegment("wallet/signmessage")
.PostJsonAsync(new SignMessageRequest
{
Expand All @@ -1503,11 +1504,12 @@ public async Task SignMessage()
Password = this.fixture.walletWithFundsPassword,
Message = this.fixture.signatureMessage
})
.ReceiveJson<string>();
.ReceiveJson<SignMessageResult>();

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

[Fact]
Expand Down