2018-10-31 16:19:25 +01:00
$ ( function ( ) {
var destination = $ ( "#Destination" ) . val ( ) ;
var amount = $ ( "#Amount" ) . val ( ) ;
var fee = $ ( "#FeeSatoshiPerByte" ) . val ( ) ;
var substractFee = $ ( "#SubstractFees" ) . val ( ) ;
2019-01-15 15:50:45 +01:00
var noChange = $ ( "#NoChange" ) . val ( ) ;
2018-10-31 16:19:25 +01:00
var loc = window . location , ws _uri ;
if ( loc . protocol === "https:" ) {
ws _uri = "wss:" ;
} else {
ws _uri = "ws:" ;
}
ws _uri += "//" + loc . host ;
ws _uri += loc . pathname + "/ws" ;
var successCallback = loc . protocol + "//" + loc . host + loc . pathname + "/success" ;
var ledgerDetected = false ;
var bridge = new ledgerwebsocket . LedgerWebSocketBridge ( ws _uri ) ;
var cryptoCode = $ ( "#cryptoCode" ) . val ( ) ;
function WriteAlert ( type , message ) {
$ ( "#walletAlert" ) . removeClass ( "alert-danger" ) ;
$ ( "#walletAlert" ) . removeClass ( "alert-warning" ) ;
$ ( "#walletAlert" ) . removeClass ( "alert-success" ) ;
$ ( "#walletAlert" ) . addClass ( "alert-" + type ) ;
$ ( "#walletAlert" ) . css ( "display" , "block" ) ;
$ ( "#alertMessage" ) . text ( message ) ;
}
function Write ( prefix , type , message ) {
$ ( "#" + prefix + "-loading" ) . css ( "display" , "none" ) ;
$ ( "#" + prefix + "-error" ) . css ( "display" , "none" ) ;
$ ( "#" + prefix + "-success" ) . css ( "display" , "none" ) ;
$ ( "#" + prefix + "-" + type ) . css ( "display" , "block" ) ;
$ ( "." + prefix + "-label" ) . text ( message ) ;
}
var updateInfo = function ( ) {
if ( ! ledgerDetected )
return false ;
2018-12-25 11:32:14 +01:00
$ ( ".crypto-info" ) . css ( "display" , "block" ) ;
var args = "" ;
args += "cryptoCode=" + cryptoCode ;
args += "&destination=" + destination ;
args += "&amount=" + amount ;
args += "&feeRate=" + fee ;
args += "&substractFees=" + substractFee ;
2019-01-15 15:50:45 +01:00
args += "&noChange=" + noChange ;
if ( noChange === "True" ) {
WriteAlert ( "warning" , 'WARNING: Because you want to make sure no change UTXO is created, you will end up sending more than the chosen amount to your destination. Please validate the transaction on your ledger' ) ;
}
else {
WriteAlert ( "warning" , 'Please validate the transaction on your ledger' ) ;
}
2018-12-25 11:32:14 +01:00
var confirmButton = $ ( "#confirm-button" ) ;
confirmButton . prop ( "disabled" , true ) ;
confirmButton . addClass ( "disabled" ) ;
bridge . sendCommand ( 'sendtoaddress' , args , 60 * 10 /* timeout */ )
. catch ( function ( reason ) {
WriteAlert ( "danger" , reason ) ;
confirmButton . prop ( "disabled" , false ) ;
confirmButton . removeClass ( "disabled" ) ;
} )
2018-10-31 16:19:25 +01:00
. then ( function ( result ) {
if ( ! result )
return ;
2018-12-25 11:32:14 +01:00
confirmButton . prop ( "disabled" , false ) ;
confirmButton . removeClass ( "disabled" ) ;
2018-10-31 16:19:25 +01:00
if ( result . error ) {
2018-12-25 11:32:14 +01:00
WriteAlert ( "danger" , result . error ) ;
} else {
WriteAlert ( "success" , 'Transaction broadcasted (' + result . transactionId + ')' ) ;
window . location . replace ( successCallback + "?txid=" + result . transactionId ) ;
2018-10-31 16:19:25 +01:00
}
} ) ;
} ;
bridge . isSupported ( )
. then ( function ( supported ) {
if ( ! supported ) {
Write ( 'hw' , 'error' , 'U2F or Websocket are not supported by this browser' ) ;
}
else {
bridge . sendCommand ( 'test' , null , 5 )
. catch ( function ( reason ) {
if ( reason . name === "TransportError" )
reason = "Are you running the ledger app with version equals or above 1.2.4?" ;
Write ( 'hw' , 'error' , reason ) ;
} )
. then ( function ( result ) {
if ( ! result )
return ;
if ( result . error ) {
Write ( 'hw' , 'error' , result . error ) ;
} else {
Write ( 'hw' , 'success' , 'Ledger detected' ) ;
$ ( "#sendform" ) . css ( "display" , "block" ) ;
ledgerDetected = true ;
updateInfo ( ) ;
}
} ) ;
}
} ) ;
} ) ;