@@ -14,6 +14,7 @@ const secp256k1 = require('bcrypto/lib/secp256k1');
1414const network = Network . get ( 'regtest' ) ;
1515const assert = require ( 'bsert' ) ;
1616const { BufferSet} = require ( 'buffer-map' ) ;
17+ const util = require ( '../lib/utils/util' ) ;
1718const common = require ( './util/common' ) ;
1819const Outpoint = require ( '../lib/primitives/outpoint' ) ;
1920const consensus = require ( '../lib/protocol/consensus' ) ;
@@ -96,6 +97,92 @@ describe('Wallet HTTP', function() {
9697 } ) ;
9798 } ) ;
9899
100+ describe ( 'Unlock wallet' , function ( ) {
101+ const WNAME = 'lockedWallet' ;
102+ const PASSPHRASE = 'test1234' ;
103+ let walletClient , wallet ;
104+
105+ before ( async ( ) => {
106+ await beforeAll ( ) ;
107+
108+ await wclient . createWallet ( WNAME , {
109+ passphrase : PASSPHRASE
110+ } ) ;
111+
112+ walletClient = wclient . wallet ( WNAME ) ;
113+ wallet = await nodeCtx . wdb . get ( WNAME ) ;
114+
115+ await wallet . lock ( ) ;
116+ } ) ;
117+
118+ after ( afterAll ) ;
119+
120+ afterEach ( async ( ) => {
121+ await walletClient . lock ( ) ;
122+ } ) ;
123+
124+ it ( 'unlock wallet' , async ( ) => {
125+ assert . strictEqual ( wallet . master . encrypted , true ) ;
126+ await walletClient . unlock ( PASSPHRASE , 1 ) ;
127+ const until = util . now ( ) + 1 ;
128+
129+ assert ( wallet . master . until <= until ) ;
130+ assert ( wallet . master . key ) ;
131+
132+ await sleep ( 1000 ) ;
133+ assert ( ! wallet . master . key ) ;
134+ } ) ;
135+
136+ it ( 'should fail to unlock with wrong passphrase' , async ( ) => {
137+ let err ;
138+ try {
139+ await walletClient . unlock ( 'wrong' , 100 ) ;
140+ } catch ( e ) {
141+ err = e ;
142+ }
143+
144+ assert ( err ) ;
145+ assert . strictEqual ( err . message , 'Could not decrypt.' ) ;
146+ } ) ;
147+
148+ it ( 'should unlock for an hour' , async ( ) => {
149+ // Unlock for an hour
150+ await walletClient . unlock ( PASSPHRASE , 3600 ) ;
151+ const until = util . now ( ) + 3600 ;
152+
153+ assert ( wallet . master . until <= until ) ;
154+ assert ( wallet . master . key ) ;
155+ } ) ;
156+
157+ it ( 'should unlock for max duration' , async ( ) => {
158+ const MAX = 2073600 ;
159+ await walletClient . unlock ( PASSPHRASE , MAX ) ;
160+ } ) ;
161+
162+ it ( 'should fail unlock for invalid durations' , async ( ) => {
163+ const MAX = 2073600 ;
164+ const invalid = [
165+ [ 'text' , 'timeout must be a int.' ] ,
166+ [ - 1 , 'timeout must be a uint.' ] ,
167+ [ - 100 , 'timeout must be a uint.' ] ,
168+ [ MAX + 1 , 'Timeout must be less than 24 days.' ]
169+ ] ;
170+
171+ for ( const [ duration , message ] of invalid ) {
172+ let err ;
173+
174+ try {
175+ await walletClient . unlock ( PASSPHRASE , duration ) ;
176+ } catch ( e ) {
177+ err = e ;
178+ }
179+
180+ assert ( err , `Expected error for duration: ${ duration } ` ) ;
181+ assert . strictEqual ( err . message , message ) ;
182+ }
183+ } ) ;
184+ } ) ;
185+
99186 describe ( 'Lookahead' , function ( ) {
100187 before ( beforeAll ) ;
101188 after ( afterAll ) ;
0 commit comments