diff --git a/bin/unlock.js b/bin/unlock.js index 66cf79dacbf3d69dff23a2d061f39697661a61bc..d7f2315141f2adc0b8b71f7e36a06e72a6ecfd6f 100755 --- a/bin/unlock.js +++ b/bin/unlock.js @@ -1,27 +1,52 @@ #!/usr/bin/node -const Libbfa = require( process.env.BFAHOME + '/bin/libbfa.js'); -const bfa = new Libbfa(); -const rl = require('readline').createInterface( - { input: process.stdin, output: process.stdout } - ); -var web3 = bfa.newweb3(); +const Libbfa = require( process.env.BFAHOME + '/bin/libbfa.js'); +const bfa = new Libbfa(); +const Writable = require('stream').Writable; +var mutableStdout = new Writable( + { + write: function( chunk, encoding, callback ) + { + if ( ! this.muted ) + process.stdout.write(chunk, encoding); + callback(); + } + } ); +mutableStdout.muted = false; +const rl = require('readline').createInterface( + { + input: process.stdin, + output: mutableStdout, + terminal: true, + historySize: 0 + } ); +var web3 = bfa.newweb3(); + // First time we try to unlock, we will use this empty passphrase. +// Do not edit. var passphrase = ""; function atLeastOneFailedSoGetNewPass(x) { if ( !process.stdin.isTTY ) bye( 0, "Stdin is not a tty. Will not try with other passwords." ); + // first we print the question + mutableStdout.muted = false; rl.question( "Enter another password to try: ", (answer) => { - passphrase = answer; + process.stdout.write( "\n" ); + mutableStdout.muted = false; + passphrase = answer; if ( answer == "" ) bye( 0, "Bye." ); unlockall(); } ); + // Asking the question is an async event, so + // we set the object to mute output while the + // user types his password. + mutableStdout.muted = true; } function bye( exitcode, msg ) @@ -31,13 +56,6 @@ function bye( exitcode, msg ) process.exit( exitcode ); } -function pluralEnglish( num, single, plural ) -{ - if ( num == 1 ) - return single; - return plural; -} - function unlockall() { var wallets = new Array(); @@ -61,7 +79,7 @@ function unlockall() { var addr = wallets[i].accounts[j].address; //console.log( "Trying to unlock " + addr + "." ); - var promise = + var promise = web3.eth.personal.unlockAccount( addr, passphrase, 0 ) .catch( error => @@ -79,7 +97,7 @@ function unlockall() console.log( "Attempting to unlock " + promises.length - + " account" + pluralEnglish(promises.length,"","s") + + " account" + ( promises.length == 1 ? '' : 's' ) + empty + "." ); Promise.all( promises ) @@ -88,7 +106,7 @@ function unlockall() { if ( failures == 0 ) bye( 0, "OK." ); - console.log( failures + " account" + pluralEnglish(failures," is","s are") + " still locked." ); + console.log( failures + " account" + (failures==1 ? " is" : "s are") + " still locked." ); atLeastOneFailedSoGetNewPass(); }, function(x) @@ -105,11 +123,4 @@ function unlockall() ) } -web3.eth .personal.extend({ - methods: [{ - name: 'bfalistWallets', - call: 'personal_listWallets', - params: 0 - }] -}); unlockall();