Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
B
Breeze
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
3
Issues
3
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DeStream-public
Breeze
Commits
1c4eaa2a
Commit
1c4eaa2a
authored
May 06, 2017
by
Jeremy Bokobza
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added method to find a block given a date
parent
1dc3c9a7
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
81 additions
and
20 deletions
+81
-20
ControllersTests.cs
Breeze/src/Breeze.Api.Tests/ControllersTests.cs
+5
-5
ChainExtensions.cs
Breeze/src/Breeze.Wallet/ChainExtensions.cs
+35
-1
WalletController.cs
Breeze/src/Breeze.Wallet/Controllers/WalletController.cs
+13
-9
ITracker.cs
Breeze/src/Breeze.Wallet/ITracker.cs
+9
-1
IWalletManager.cs
Breeze/src/Breeze.Wallet/IWalletManager.cs
+2
-2
RequestModels.cs
Breeze/src/Breeze.Wallet/Models/RequestModels.cs
+7
-1
Tracker.cs
Breeze/src/Breeze.Wallet/Tracker.cs
+9
-0
WalletManager.cs
Breeze/src/Breeze.Wallet/WalletManager.cs
+1
-1
No files found.
Breeze/src/Breeze.Api.Tests/ControllersTests.cs
View file @
1c4eaa2a
...
...
@@ -22,7 +22,7 @@ namespace Breeze.Api.Tests
var
mockWalletCreate
=
new
Mock
<
IWalletManager
>();
mockWalletCreate
.
Setup
(
wallet
=>
wallet
.
CreateWallet
(
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>(),
null
)).
Returns
(
mnemonic
);
var
controller
=
new
WalletController
(
mockWalletCreate
.
Object
);
var
controller
=
new
WalletController
(
mockWalletCreate
.
Object
,
new
Mock
<
ITracker
>().
Object
);
// Act
var
result
=
controller
.
Create
(
new
WalletCreationRequest
...
...
@@ -50,9 +50,9 @@ namespace Breeze.Api.Tests
};
var
mockWalletWrapper
=
new
Mock
<
IWalletManager
>();
mockWalletWrapper
.
Setup
(
w
=>
w
.
RecoverWallet
(
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>(),
null
,
null
)).
Returns
(
wallet
);
mockWalletWrapper
.
Setup
(
w
=>
w
.
RecoverWallet
(
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>(),
null
,
It
.
IsAny
<
DateTime
>()
)).
Returns
(
wallet
);
var
controller
=
new
WalletController
(
mockWalletWrapper
.
Object
);
var
controller
=
new
WalletController
(
mockWalletWrapper
.
Object
,
new
Mock
<
ITracker
>().
Object
);
// Act
var
result
=
controller
.
Recover
(
new
WalletRecoveryRequest
...
...
@@ -85,7 +85,7 @@ namespace Breeze.Api.Tests
var
mockWalletWrapper
=
new
Mock
<
IWalletManager
>();
mockWalletWrapper
.
Setup
(
w
=>
w
.
LoadWallet
(
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>())).
Returns
(
wallet
);
var
controller
=
new
WalletController
(
mockWalletWrapper
.
Object
);
var
controller
=
new
WalletController
(
mockWalletWrapper
.
Object
,
new
Mock
<
ITracker
>().
Object
);
// Act
var
result
=
controller
.
Load
(
new
WalletLoadRequest
...
...
@@ -111,7 +111,7 @@ namespace Breeze.Api.Tests
var
mockWalletWrapper
=
new
Mock
<
IWalletManager
>();
mockWalletWrapper
.
Setup
(
wallet
=>
wallet
.
LoadWallet
(
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>(),
It
.
IsAny
<
string
>())).
Throws
<
FileNotFoundException
>();
var
controller
=
new
WalletController
(
mockWalletWrapper
.
Object
);
var
controller
=
new
WalletController
(
mockWalletWrapper
.
Object
,
new
Mock
<
ITracker
>().
Object
);
// Act
var
result
=
controller
.
Load
(
new
WalletLoadRequest
...
...
Breeze/src/Breeze.Wallet/ChainExtensions.cs
View file @
1c4eaa2a
...
...
@@ -39,5 +39,39 @@ namespace Breeze.Wallet
throw
new
Exception
(
"No support for this coin."
);
}
}
/// <summary>
/// Gets the height of the first block created after this date.
/// </summary>
/// <param name="chain">The chain of blocks.</param>
/// <param name="date">The date.</param>
/// <returns>The height of the first block created after the date.</returns>
public
static
int
GetHeightAtTime
(
this
ConcurrentChain
chain
,
DateTime
date
)
{
int
blockSyncStart
=
0
;
int
upperLimit
=
chain
.
Tip
.
Height
;
int
lowerLimit
=
0
;
bool
found
=
false
;
while
(!
found
)
{
int
check
=
lowerLimit
+
(
upperLimit
-
lowerLimit
)
/
2
;
if
(
chain
.
GetBlock
(
check
).
Header
.
BlockTime
>=
date
)
{
upperLimit
=
check
;
}
else
if
(
chain
.
GetBlock
(
check
).
Header
.
BlockTime
<
date
)
{
lowerLimit
=
check
;
}
if
(
upperLimit
-
lowerLimit
<=
1
)
{
blockSyncStart
=
upperLimit
;
found
=
true
;
}
}
return
blockSyncStart
;
}
}
}
}
\ No newline at end of file
Breeze/src/Breeze.Wallet/Controllers/WalletController.cs
View file @
1c4eaa2a
...
...
@@ -14,14 +14,17 @@ namespace Breeze.Wallet.Controllers
/// <summary>
/// Controller providing operations on a wallet.
/// </summary>
[
Route
(
"api/v{version:apiVersion}/[controller]"
)]
[
Route
(
"api/v{version:apiVersion}/[controller]"
)]
public
class
WalletController
:
Controller
{
private
readonly
IWalletManager
walletManager
;
public
WalletController
(
IWalletManager
walletManager
)
private
readonly
ITracker
tracker
;
public
WalletController
(
IWalletManager
walletManager
,
ITracker
tracker
)
{
this
.
walletManager
=
walletManager
;
this
.
tracker
=
tracker
;
}
/// <summary>
...
...
@@ -60,7 +63,7 @@ namespace Breeze.Wallet.Controllers
/// </summary>
/// <param name="request">The name of the wallet to load.</param>
/// <returns></returns>
[
Route
(
"load"
)]
[
Route
(
"load"
)]
[
HttpPost
]
public
IActionResult
Load
([
FromBody
]
WalletLoadRequest
request
)
{
...
...
@@ -120,9 +123,10 @@ namespace Breeze.Wallet.Controllers
// get the wallet folder
DirectoryInfo
walletFolder
=
GetWalletFolder
(
request
.
FolderPath
);
Wallet
wallet
=
this
.
walletManager
.
RecoverWallet
(
request
.
Password
,
walletFolder
.
FullName
,
request
.
Name
,
request
.
Network
,
request
.
Mnemonic
);
// TODO give the tracker the date at which this wallet was originally created so that it can start syncing blocks for it
Wallet
wallet
=
this
.
walletManager
.
RecoverWallet
(
request
.
Password
,
walletFolder
.
FullName
,
request
.
Name
,
request
.
Network
,
request
.
Mnemonic
,
null
,
request
.
CreationDate
);
// start syncing the wallet from the creation date
this
.
tracker
.
SyncFrom
(
request
.
CreationDate
);
return
this
.
Json
(
new
WalletModel
{
...
...
@@ -179,7 +183,7 @@ namespace Breeze.Wallet.Controllers
/// </summary>
/// <param name="request">The request parameters.</param>
/// <returns></returns>
[
Route
(
"history"
)]
[
Route
(
"history"
)]
[
HttpGet
]
public
IActionResult
GetHistory
([
FromQuery
]
WalletHistoryRequest
request
)
{
...
...
@@ -269,7 +273,7 @@ namespace Breeze.Wallet.Controllers
/// </summary>
/// <param name="request">The transaction parameters.</param>
/// <returns>All the details of the transaction, including the hex used to execute it.</returns>
[
Route
(
"build-transaction"
)]
[
Route
(
"build-transaction"
)]
[
HttpPost
]
public
IActionResult
BuildTransaction
([
FromBody
]
BuildTransactionRequest
request
)
{
...
...
@@ -296,7 +300,7 @@ namespace Breeze.Wallet.Controllers
/// </summary>
/// <param name="request">The hex representing the transaction.</param>
/// <returns></returns>
[
Route
(
"send-transaction"
)]
[
Route
(
"send-transaction"
)]
[
HttpPost
]
public
IActionResult
SendTransaction
([
FromBody
]
SendTransactionRequest
request
)
{
...
...
Breeze/src/Breeze.Wallet/ITracker.cs
View file @
1c4eaa2a
using
System.Threading.Tasks
;
using
System
;
using
System.Threading.Tasks
;
using
NBitcoin
;
namespace
Breeze.Wallet
...
...
@@ -16,5 +17,12 @@ namespace Breeze.Wallet
/// </summary>
/// <returns></returns>
Task
WaitForChainDownloadAsync
();
/// <summary>
/// Synchronize the wallet starting from the date passed as a parameter.
/// </summary>
/// <param name="date">The date from which to start the sync process.</param>
/// <returns></returns>
void
SyncFrom
(
DateTime
date
);
}
}
Breeze/src/Breeze.Wallet/IWalletManager.cs
View file @
1c4eaa2a
...
...
@@ -39,9 +39,9 @@ namespace Breeze.Wallet
/// <param name="network">The network in which to creae this wallet</param>
/// <param name="mnemonic">The user's mnemonic for the wallet.</param>
/// <param name="passphrase">The passphrase used in the seed.</param>
/// <param name="creationTime">The time this wallet was created.</param>
/// <param name="creationTime">The
date and
time this wallet was created.</param>
/// <returns>The recovered wallet.</returns>
Wallet
RecoverWallet
(
string
password
,
string
folderPath
,
string
name
,
string
network
,
string
mnemonic
,
string
passphrase
=
null
,
DateTime
Offset
?
creationTime
=
null
);
Wallet
RecoverWallet
(
string
password
,
string
folderPath
,
string
name
,
string
network
,
string
mnemonic
,
string
passphrase
=
null
,
DateTime
?
creationTime
=
null
);
/// <summary>
/// Deleted a wallet.
...
...
Breeze/src/Breeze.Wallet/Models/RequestModels.cs
View file @
1c4eaa2a
using
System.Collections
;
using
System
;
using
System.Collections
;
using
System.Collections.Generic
;
using
System.ComponentModel.DataAnnotations
;
using
Newtonsoft.Json
;
using
Newtonsoft.Json.Converters
;
namespace
Breeze.Wallet.Models
{
...
...
@@ -45,6 +48,9 @@ namespace Breeze.Wallet.Models
public
string
Name
{
get
;
set
;
}
public
string
Network
{
get
;
set
;
}
[
JsonConverter
(
typeof
(
IsoDateTimeConverter
))]
public
DateTime
CreationDate
{
get
;
set
;
}
}
public
class
WalletHistoryRequest
...
...
Breeze/src/Breeze.Wallet/Tracker.cs
View file @
1c4eaa2a
...
...
@@ -84,6 +84,15 @@ namespace Breeze.Wallet
repeatEvery
:
TimeSpans
.
FiveSeconds
);
}
/// <inheritdoc />
public
void
SyncFrom
(
DateTime
date
)
{
int
blockSyncStart
=
this
.
chain
.
GetHeightAtTime
(
date
);
// start syncing blocks
this
.
blockNotification
.
SyncFrom
(
this
.
chain
.
GetBlock
(
blockSyncStart
).
HashBlock
);
}
private
bool
BlocksSynced
()
{
return
this
.
walletManager
.
Wallets
.
All
(
w
=>
w
.
AccountsRoot
.
Single
(
a
=>
a
.
CoinType
==
this
.
coinType
).
LastBlockSyncedHeight
==
this
.
chain
.
Tip
.
Height
);
...
...
Breeze/src/Breeze.Wallet/WalletManager.cs
View file @
1c4eaa2a
...
...
@@ -73,7 +73,7 @@ namespace Breeze.Wallet
}
/// <inheritdoc />
public
Wallet
RecoverWallet
(
string
password
,
string
folderPath
,
string
name
,
string
network
,
string
mnemonic
,
string
passphrase
=
null
,
DateTime
Offset
?
creationTime
=
null
)
public
Wallet
RecoverWallet
(
string
password
,
string
folderPath
,
string
name
,
string
network
,
string
mnemonic
,
string
passphrase
=
null
,
DateTime
?
creationTime
=
null
)
{
// for now the passphrase is set to be the password by default.
if
(
passphrase
==
null
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment