browser: fix promise handling.
This commit is contained in:
parent
10ee1dce6e
commit
bb89d203c1
113
browser/index.js
113
browser/index.js
@ -11,7 +11,6 @@ var floating = document.getElementById('floating');
|
|||||||
var send = document.getElementById('send');
|
var send = document.getElementById('send');
|
||||||
var newaddr = document.getElementById('newaddr');
|
var newaddr = document.getElementById('newaddr');
|
||||||
var chainState = document.getElementById('state');
|
var chainState = document.getElementById('state');
|
||||||
var cb = bcoin.spawn.cb;
|
|
||||||
var items = [];
|
var items = [];
|
||||||
var scrollback = 0;
|
var scrollback = 0;
|
||||||
var logger, node, options;
|
var logger, node, options;
|
||||||
@ -49,40 +48,32 @@ logger.writeConsole = function(level, args) {
|
|||||||
send.onsubmit = function(ev) {
|
send.onsubmit = function(ev) {
|
||||||
var value = document.getElementById('amount').value;
|
var value = document.getElementById('amount').value;
|
||||||
var address = document.getElementById('address').value;
|
var address = document.getElementById('address').value;
|
||||||
|
var tx, options;
|
||||||
|
|
||||||
var options = {
|
options = {
|
||||||
outputs: [{
|
outputs: [{
|
||||||
address: address,
|
address: address,
|
||||||
value: utils.satoshi(value)
|
value: utils.satoshi(value)
|
||||||
}]
|
}]
|
||||||
};
|
};
|
||||||
|
|
||||||
cb(node.wallet.createTX(options), function(err, tx) {
|
node.wallet.createTX(options).then(function(mtx) {
|
||||||
if (err)
|
tx = mtx;
|
||||||
return node.logger.error(err);
|
return node.wallet.sign(tx);
|
||||||
|
}).then(function() {
|
||||||
cb(node.wallet.sign(tx), function(err) {
|
return node.sendTX(tx);
|
||||||
if (err)
|
}).then(function() {
|
||||||
return node.logger.error(err);
|
show(tx);
|
||||||
|
|
||||||
cb(node.sendTX(tx), function(err) {
|
|
||||||
if (err)
|
|
||||||
return node.logger.error(err);
|
|
||||||
|
|
||||||
show(tx);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
ev.stopPropagation();
|
ev.stopPropagation();
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
newaddr.onmouseup = function() {
|
newaddr.onmouseup = function() {
|
||||||
cb(node.wallet.createReceive(), function(err) {
|
node.wallet.createReceive().then(function() {
|
||||||
if (err)
|
|
||||||
throw err;
|
|
||||||
formatWallet(node.wallet);
|
formatWallet(node.wallet);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -121,11 +112,7 @@ function addItem(tx) {
|
|||||||
+ ' - ' + kb(tx.getSize()) + ')</a>');
|
+ ' - ' + kb(tx.getSize()) + ')</a>');
|
||||||
tdiv.appendChild(el);
|
tdiv.appendChild(el);
|
||||||
|
|
||||||
el.onmouseup = function(ev) {
|
setMouseup(el, tx);
|
||||||
show(tx);
|
|
||||||
ev.stopPropagation();
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
items.push(el);
|
items.push(el);
|
||||||
|
|
||||||
@ -135,48 +122,63 @@ function addItem(tx) {
|
|||||||
+ ' value=' + utils.btc(node.chain.db.state.value);
|
+ ' value=' + utils.btc(node.chain.db.state.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setMouseup(el, obj) {
|
||||||
|
el.onmouseup = function(ev) {
|
||||||
|
show(obj);
|
||||||
|
ev.stopPropagation();
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function formatWallet(wallet) {
|
function formatWallet(wallet) {
|
||||||
var html = '';
|
var html = '';
|
||||||
var key = wallet.master.toJSON().key;
|
var key = wallet.master.toJSON().key;
|
||||||
|
var i, tx, el;
|
||||||
|
|
||||||
html += '<b>Wallet</b><br>';
|
html += '<b>Wallet</b><br>';
|
||||||
if (bcoin.network.get().type === 'segnet4') {
|
|
||||||
html += 'Current Address (p2wpkh): <b>' + wallet.getAddress() + '</b><br>';
|
if (bcoin.network.primary.witness) {
|
||||||
html += 'Current Address (p2wpkh behind p2sh): <b>' + wallet.getProgramAddress() + '</b><br>';
|
html += 'Current Address (p2wpkh): <b>'
|
||||||
|
+ wallet.getAddress()
|
||||||
|
+ '</b><br>';
|
||||||
|
html += 'Current Address (p2wpkh behind p2sh): <b>'
|
||||||
|
+ wallet.getProgramAddress()
|
||||||
|
+ '</b><br>';
|
||||||
} else {
|
} else {
|
||||||
html += 'Current Address: <b>' + wallet.getAddress() + '</b><br>';
|
html += 'Current Address: <b>' + wallet.getAddress() + '</b><br>';
|
||||||
}
|
}
|
||||||
|
|
||||||
html += 'Extended Private Key: <b>' + key.xprivkey + '</b><br>';
|
html += 'Extended Private Key: <b>' + key.xprivkey + '</b><br>';
|
||||||
html += 'Mnemonic: <b>' + key.mnemonic.phrase + '</b><br>';
|
html += 'Mnemonic: <b>' + key.mnemonic.phrase + '</b><br>';
|
||||||
cb(wallet.getBalance(), function(err, balance) {
|
|
||||||
if (err)
|
|
||||||
throw err;
|
|
||||||
|
|
||||||
html += 'Confirmed Balance: <b>' + utils.btc(balance.confirmed) + '</b><br>';
|
wallet.getBalance().then(function(balance) {
|
||||||
html += 'Unconfirmed Balance: <b>' + utils.btc(balance.unconfirmed) + '</b><br>';
|
html += 'Confirmed Balance: <b>'
|
||||||
|
+ utils.btc(balance.confirmed)
|
||||||
|
+ '</b><br>';
|
||||||
|
|
||||||
|
html += 'Unconfirmed Balance: <b>'
|
||||||
|
+ utils.btc(balance.unconfirmed)
|
||||||
|
+ '</b><br>';
|
||||||
|
|
||||||
html += 'Balance: <b>' + utils.btc(balance.total) + '</b><br>';
|
html += 'Balance: <b>' + utils.btc(balance.total) + '</b><br>';
|
||||||
|
|
||||||
cb(wallet.getHistory(), function(err, txs) {
|
return wallet.getHistory();
|
||||||
if (err)
|
}).then(function(txs) {
|
||||||
throw err;
|
return wallet.toDetails(txs);
|
||||||
|
}).then(function(txs) {
|
||||||
|
html += 'TXs:\n';
|
||||||
|
wdiv.innerHTML = html;
|
||||||
|
|
||||||
cb(wallet.toDetails(txs), function(err, txs) {
|
for (i = 0; i < txs.length; i++) {
|
||||||
if (err)
|
tx = txs[i];
|
||||||
throw err;
|
|
||||||
|
|
||||||
html += 'TXs:\n';
|
el = create(
|
||||||
wdiv.innerHTML = html;
|
'<a style="display:block;" href="#' + tx.hash + '">'
|
||||||
|
+ tx.hash + '</a>');
|
||||||
|
|
||||||
txs.forEach(function(tx) {
|
wdiv.appendChild(el);
|
||||||
var el = create('<a style="display:block;" href="#' + tx.hash + '">' + tx.hash + '</a>');
|
setMouseup(el, tx.toJSON());
|
||||||
wdiv.appendChild(el);
|
}
|
||||||
el.onmouseup = function(ev) {
|
|
||||||
show(tx.toJSON());
|
|
||||||
ev.stopPropagation();
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -200,15 +202,12 @@ node.on('error', function(err) {
|
|||||||
node.chain.on('block', addItem);
|
node.chain.on('block', addItem);
|
||||||
node.mempool.on('tx', addItem);
|
node.mempool.on('tx', addItem);
|
||||||
|
|
||||||
cb(node.open(), function(err) {
|
node.open().then(function() {
|
||||||
if (err)
|
|
||||||
throw err;
|
|
||||||
|
|
||||||
node.startSync();
|
node.startSync();
|
||||||
|
|
||||||
formatWallet(node.wallet);
|
formatWallet(node.wallet);
|
||||||
|
|
||||||
node.wallet.on('update', function() {
|
node.wallet.on('balance', function() {
|
||||||
formatWallet(node.wallet);
|
formatWallet(node.wallet);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user