Skip to content

Commit f1f12fb

Browse files
authored
Allow restore to not create the cold staking accounts (#359)
- Closes #358
1 parent 7d13219 commit f1f12fb

6 files changed

Lines changed: 19 additions & 11 deletions

File tree

src/Features/Blockcore.Features.ColdStaking/ColdStakingManager.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,15 @@ public HdAccount GetOrCreateColdStakingAccount(string walletName, bool isColdWal
258258
/// track addresses under the <see cref="ColdWalletAccountIndex"/> HD account.
259259
/// </summary>
260260
/// <inheritdoc />
261-
public override Wallet.Types.Wallet RecoverWallet(string password, string name, string mnemonic, DateTime creationTime, string passphrase, int? coinType = null)
261+
public override Wallet.Types.Wallet RecoverWallet(string password, string name, string mnemonic, DateTime creationTime, string passphrase, int? coinType = null, bool? isColdStakingWallet = false)
262262
{
263263
Wallet.Types.Wallet wallet = base.RecoverWallet(password, name, mnemonic, creationTime, passphrase, coinType);
264264

265-
this.GetOrCreateColdStakingAccount(wallet.Name, false, password);
266-
this.GetOrCreateColdStakingAccount(wallet.Name, true, password);
265+
if (isColdStakingWallet.HasValue && isColdStakingWallet == true)
266+
{
267+
this.GetOrCreateColdStakingAccount(wallet.Name, false, password);
268+
this.GetOrCreateColdStakingAccount(wallet.Name, true, password);
269+
}
267270

268271
return wallet;
269272
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public IActionResult Recover([FromBody] WalletRecoveryRequest request)
264264

265265
try
266266
{
267-
Types.Wallet wallet = this.walletManager.RecoverWallet(request.Password, request.Name, request.Mnemonic, request.CreationDate, passphrase: request.Passphrase, request.CoinType);
267+
Types.Wallet wallet = this.walletManager.RecoverWallet(request.Password, request.Name, request.Mnemonic, request.CreationDate, passphrase: request.Passphrase, request.CoinType, request.IsColdStakingWallet);
268268

269269
this.SyncFromBestHeightForRecoveredWallets(request.CreationDate);
270270

src/Features/Blockcore.Features.Wallet/Api/Models/RequestModels.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public class WalletRecoveryRequest : RequestModel
124124
/// Optional CoinType to overwrite the default <see cref="Blockcore.Consensus.IConsensus.CoinType"/>.
125125
/// </summary>
126126
public int? CoinType { get; set; }
127+
128+
/// <summary>
129+
/// Optional flag that indicates if the "coldStakingColdAddresses" and "coldStakingHotAddresses" accounts should be restored.
130+
/// </summary>
131+
public bool? IsColdStakingWallet { get; set; }
127132
}
128133

129134
/// <summary>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public interface IWalletManager
147147
/// <param name="creationTime">The date and time this wallet was created.</param>
148148
/// <param name="coinType">Allow to override the default BIP44 cointype.</param>
149149
/// <returns>The recovered wallet.</returns>
150-
Types.Wallet RecoverWallet(string password, string name, string mnemonic, DateTime creationTime, string passphrase = null, int? coinType = null);
150+
Types.Wallet RecoverWallet(string password, string name, string mnemonic, DateTime creationTime, string passphrase = null, int? coinType = null, bool? isColdStakingWallet = false);
151151

152152
/// <summary>
153153
/// Recovers a wallet using extended public key and account index.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ private SecureString CacheSecret(string name, string walletPassword, TimeSpan du
461461
}
462462

463463
/// <inheritdoc />
464-
public virtual Types.Wallet RecoverWallet(string password, string name, string mnemonic, DateTime creationTime, string passphrase, int? coinType = null)
464+
public virtual Types.Wallet RecoverWallet(string password, string name, string mnemonic, DateTime creationTime, string passphrase, int? coinType = null, bool? isColdStakingWallet = false)
465465
{
466466
Guard.NotEmpty(password, nameof(password));
467467
Guard.NotEmpty(name, nameof(name));

src/Tests/Blockcore.Features.Wallet.Tests/WalletControllerTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public void RecoverWalletSuccessfullyReturnsWalletModel()
330330
};
331331

332332
var mockWalletManager = new Mock<IWalletManager>();
333-
mockWalletManager.Setup(w => w.RecoverWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DateTime>(), null, null)).Returns(wallet);
333+
mockWalletManager.Setup(w => w.RecoverWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DateTime>(), null, null, true)).Returns(wallet);
334334

335335
Mock<IWalletSyncManager> walletSyncManager = new Mock<IWalletSyncManager>();
336336
walletSyncManager.Setup(w => w.WalletTip).Returns(new ChainedHeader(this.Network.GetGenesis().Header, this.Network.GetGenesis().Header.GetHash(), 3));
@@ -368,7 +368,7 @@ public void RecoverWalletWithDatedAfterCurrentSyncHeightDoesNotMoveSyncHeight()
368368
DateTime lastBlockDateTime = chainIndexer.Tip.Header.BlockTime.DateTime;
369369

370370
var mockWalletManager = new Mock<IWalletManager>();
371-
mockWalletManager.Setup(w => w.RecoverWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DateTime>(), null, null)).Returns(wallet);
371+
mockWalletManager.Setup(w => w.RecoverWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DateTime>(), null, null, true)).Returns(wallet);
372372

373373
Mock<IWalletSyncManager> walletSyncManager = new Mock<IWalletSyncManager>();
374374
walletSyncManager.Setup(w => w.WalletTip).Returns(new ChainedHeader(this.Network.GetGenesis().Header, this.Network.GetGenesis().Header.GetHash(), 3));
@@ -419,7 +419,7 @@ public void RecoverWalletWithInvalidOperationExceptionReturnsConflict()
419419
{
420420
string errorMessage = "An error occurred.";
421421
var mockWalletManager = new Mock<IWalletManager>();
422-
mockWalletManager.Setup(w => w.RecoverWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DateTime>(), null, null))
422+
mockWalletManager.Setup(w => w.RecoverWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DateTime>(), null, null, true))
423423
.Throws(new WalletException(errorMessage));
424424

425425
var controller = new WalletController(this.LoggerFactory.Object, mockWalletManager.Object, new Mock<IWalletTransactionHandler>().Object, new Mock<IWalletSyncManager>().Object, It.IsAny<ConnectionManager>(), this.Network, this.chainIndexer, new Mock<IBroadcasterManager>().Object, DateTimeProvider.Default);
@@ -445,7 +445,7 @@ public void RecoverWalletWithInvalidOperationExceptionReturnsConflict()
445445
public void RecoverWalletWithFileNotFoundExceptionReturnsNotFound()
446446
{
447447
var mockWalletManager = new Mock<IWalletManager>();
448-
mockWalletManager.Setup(w => w.RecoverWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DateTime>(), null, null))
448+
mockWalletManager.Setup(w => w.RecoverWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DateTime>(), null, null, true))
449449
.Throws(new FileNotFoundException("File not found."));
450450

451451
var controller = new WalletController(this.LoggerFactory.Object, mockWalletManager.Object, new Mock<IWalletTransactionHandler>().Object, new Mock<IWalletSyncManager>().Object, It.IsAny<ConnectionManager>(), this.Network, this.chainIndexer, new Mock<IBroadcasterManager>().Object, DateTimeProvider.Default);
@@ -472,7 +472,7 @@ public void RecoverWalletWithFileNotFoundExceptionReturnsNotFound()
472472
public void RecoverWalletWithExceptionReturnsBadRequest()
473473
{
474474
var mockWalletManager = new Mock<IWalletManager>();
475-
mockWalletManager.Setup(w => w.RecoverWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DateTime>(), null, null))
475+
mockWalletManager.Setup(w => w.RecoverWallet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<DateTime>(), null, null, true))
476476
.Throws(new FormatException("Formatting failed."));
477477

478478
var controller = new WalletController(this.LoggerFactory.Object, mockWalletManager.Object, new Mock<IWalletTransactionHandler>().Object, new Mock<IWalletSyncManager>().Object, It.IsAny<ConnectionManager>(), this.Network, this.chainIndexer, new Mock<IBroadcasterManager>().Object, DateTimeProvider.Default);

0 commit comments

Comments
 (0)