@ -13,8 +13,12 @@ var keymgr;
* @ returns { undefined }
* /
function loadKeyFromLocalStorage ( callback ) {
if ( typeof keymgr != "undefined" ) {
callback ( "Key already loaded." , true ) ;
return ;
}
if ( ! inStorage ( "signingkey" ) || getStorage ( "signingkey" ) == "undefined" ) {
var pass = prompt ( "Generating a new signing key. Enter a password to protect it. You'll need to save this password somewhere safe; it cannot be recovered." ) ;
var pass = prompt ( "Generating a new signing key (might take a while, be patient) . Enter a password to protect it. You'll need to save this password somewhere safe; it cannot be recovered.") ;
generatePrivateKey ( getStorage ( "notary_name" ) + " <null@null.com>" , pass , function ( key ) {
if ( typeof key == "undefined" ) {
callback ( "Could not generate key." , false ) ;
@ -25,7 +29,7 @@ function loadKeyFromLocalStorage(callback) {
callback ( "Signing key generated." , true ) ;
} ) ;
} else {
var pass = prompt ( "Enter password to unlock signing key . ") ;
var pass = prompt ( "Enter password to unlock signing key : ") ;
loadPrivateKey ( getStorage ( "signingkey" ) , pass , function ( key ) {
if ( typeof key == "undefined" ) {
callback ( "Could not unlock key. Password is probably incorrect." , false ) ;
@ -37,6 +41,16 @@ function loadKeyFromLocalStorage(callback) {
}
}
function loadKeyFromLocalStorageWithUserFeedback ( ) {
loadKeyFromLocalStorage ( function ( msg , ok ) {
if ( ok ) {
alert ( msg ) ;
} else {
alert ( "Error: " + msg ) ;
}
} ) ;
}
/ * *
* Load a private key .
* @ param { string } armoredkey PGP private key
@ -121,4 +135,64 @@ function generatePrivateKey(userid, passphrase, callback) {
} ) ;
}
} ) ;
}
function exportPublicKey ( ) {
loadKeyFromLocalStorage ( function ( message , ok ) {
if ( ok ) {
openSaveFileDialog ( function ( path ) {
keymgr . export _pgp _public ( { } , function ( err , pgp _public ) {
if ( err ) {
alert ( "Something went wrong." ) ;
} else {
writeToFile ( path , pgp _public ) ;
}
} ) ;
} , "public-key.asc" , ".asc" ) ;
} else {
alert ( "Error: " + message ) ;
}
} ) ;
}
function exportPrivateKey ( ) {
loadKeyFromLocalStorage ( function ( message , ok ) {
if ( ok ) {
var pass = prompt ( "Enter a password to protect the backup key:" ) ;
openSaveFileDialog ( function ( path ) {
keymgr . export _pgp _private ( {
passphrase : pass
} , function ( err , pgp _private ) {
if ( err ) {
alert ( "Something went wrong." ) ;
} else {
writeToFile ( path , pgp _private ) ;
}
} ) ;
} , "private-key.asc" , ".asc" ) ;
} else {
alert ( "Error: " + message ) ;
}
} ) ;
}
function importPrivateKey ( ) {
if ( inStorage ( "signingkey" ) && getStorage ( "signingkey" ) != "undefined" ) {
if ( ! confirm ( "The restored key will replace the current key, which will be unrecoverable unless you made a backup. Continue?" ) ) {
return ;
}
}
openFileDialog ( function ( path ) {
var keyfile = getFileAsString ( path ) ;
var pass = prompt ( "Enter password for imported key (password was set when exported):" ) ;
loadPrivateKey ( keyfile , pass , function ( key ) {
if ( typeof key == "undefined" ) {
alert ( "Could not import key. Password is probably incorrect." ) ;
return ;
}
keymgr = key ;
setStorage ( "signingkey" , keymgr . armored _pgp _private ) ;
alert ( "Private key imported." ) ;
} ) ;
} , ".asc" ) ;
}