Skip to content

Commit

Permalink
Add multiple ICON Networks support
Browse files Browse the repository at this point in the history
  • Loading branch information
Spl3en committed Mar 7, 2019
1 parent 268b1ea commit fb3ea14
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 34 deletions.
8 changes: 8 additions & 0 deletions src/css/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,14 @@ progress.light{
grid-template-columns: repeat(2, 1fr);
grid-column-gap: 1rem;
}

.main-balance-network {
margin: 1.5rem 0;
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-column-gap: 1rem;
}

.div-main-address-qr{
text-align:right;
}
Expand Down
24 changes: 17 additions & 7 deletions src/html/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,39 @@ <h2 class="title">Address:</h2>
</div>

<div class="main-balance-overview">

<div class="div-overview-balance">
<h2 class="title">Balance:</h2>
<h4 class="text-block" id="balance-available" title="Available Balance">
<i class="fas fa-unlock-alt"></i> ICX
<span data-cplabel="Balance" style="float: right;" class="tctcl">0.00</span>
<span data-cplabel="Balance" style="float: right;" class="tctcl">...</span>
</h4>
</div>

<div class="div-overview-balance half-width half-width-end">
<h2 class="title">Select network</h4>
<select required="required" tabindex="0" name="node-address" id="overview-network-id" class="text-block">
<option value="1">Mainnet</option>
<option value="2">Testnet for Exchanges (Euljiro)</option>
<option value="3">Testnet for DApps (Yeouido)</option>
</select>
</div>

<div class="div-main-address-qr" id="div-w-qr">
<span class="form-help form-help-qr hidden" id="scan-qr-help">Scan to share your address</span>
<div class="input-wrap">
<span class="form-ew form-msg text-spaced-error" id="text-overview-error"></span>
<span class="form-ew form-msg text-spaced-warning" id="text-overview-warning"></span>
<span class="form-ew form-msg text-spaced-success" id="text-overview-success"></span>
</div>
</div>

<div class="input-wrap button-wrap">
<button tabindex="0" type="button" class="form-bt button-green"
data-section="section-overview-show"
title="Backup your private keys, YOU REALLY HAVE TO DO THIS! (ctrl+e)"
title="Backup your private keys (ctrl+e)"
id="button-overview-showkeys">Backup Keys</button>
<button tabindex="0" type="button" class="form-bt button-green"
title="Close current wallet, so you can open another wallet (ctrl+x)"
id="button-overview-closewallet">Close</button>
<div id="fusionProgress" class="fusion-progress hidden">
<i class="fas fa-cogs fa-spin"></i> Wallet optimization in progress <i class="fas fa-cogs fa-spin"></i>
</div>
</div>
</div>
</section>
Expand Down
8 changes: 5 additions & 3 deletions src/html/send.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ <h4 class="text-spaced input-required">Recipient Address</h4>
</div>

<div class="input-wrap">

<div class="div-send-amount inline-block-div half-width">
<h4 class="text-spaced input-required">Amount (ICX)</h4>
<input tabindex="0" title="Required, amount of ICX you want to send."
value = "0.00" required="required" type="number" step="1.0"
min="0.00" class="text-block" id="input-send-amount"
placeholder="0.00"
data-noclear="1" value = "0" required="required" type="number" step="0.1"
min="0" class="text-block" id="input-send-amount"
placeholder="0"
/>
<span id="sendFormHelp" class="form-help"></span>
<span tabindex="0" id="sendMaxAmount" class="hidden">send all</span>
</div>

<div class="div-send-fee inline-block-div half-width half-width-end">
<h4 class="text-spaced input-required">Step Limit (step)</h4>
<input tabindex="0" title="Required, Step price is paid in loop and 1 loop is fixed to 0.000000000000000001 (10^18) ICX. ICON transaction fee is imposed according to various factors such as the number of smart contract usage, the amount of blockchain database used and the size of transaction data, etc." required="required"
Expand Down
27 changes: 21 additions & 6 deletions src/js/ws_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ var SvalinnManager = function () {
this.serviceApi = null;
this.syncWorker = null;
this.fusionTxHash = [];

this.iconNetworks = [
{desc: "None", url: "unknown", nid: 0},
{desc: "Mainnet", url: "https://ctz.solidwallet.io", nid: 1},
{desc: "Testnet for Exchanges (Euljiro)", url: "https://test-ctz.solidwallet.io", nid: 2},
{desc: "Testnet for DApps (Yeouido)", url: "https://bicon.net.solidwallet.io", nid: 3}
];
};

SvalinnManager.prototype.init = function () {
Expand Down Expand Up @@ -153,7 +160,7 @@ SvalinnManager.prototype._wipeConfig = function () {

SvalinnManager.prototype.startService = function (walletFile, onError, onSuccess, onDelay) {

const keystore = JSON.parse (fs.readFileSync (walletFile, 'utf8'));
const keystore = JSON.parse (fs.readFileSync (walletFile, 'utf8').replace(/^\uFEFF/, ''));

try {
wsession.set ('loadedWalletAddress', keystore.address);
Expand Down Expand Up @@ -225,7 +232,7 @@ SvalinnManager.prototype.createTransaction = function (tx, transactionFile, wall

// Open the wallet
try {
const keystore = JSON.parse (fs.readFileSync (walletFile, 'utf8'));
const keystore = JSON.parse (fs.readFileSync (walletFile, 'utf8').replace(/^\uFEFF/, ''));
const wallet = IconService.IconWallet.loadKeystore (keystore, walletPass);

const icxTransaction = new IconService.IconBuilder.IcxTransactionBuilder()
Expand Down Expand Up @@ -316,7 +323,7 @@ SvalinnManager.prototype.getSecretKeys = function (walletPass) {
return new Promise((resolve, reject) => {
try {
const walletFile = settings.get ('recentWallet');
const keystore = JSON.parse (fs.readFileSync (walletFile, 'utf8'));
const keystore = JSON.parse (fs.readFileSync (walletFile, 'utf8').replace(/^\uFEFF/, ''));
const wallet = IconService.IconWallet.loadKeystore (keystore, walletPass);
return resolve (wallet.getPrivateKey());
} catch (err) {
Expand All @@ -338,9 +345,13 @@ SvalinnManager.prototype.sendTransaction = function (params) {

SvalinnManager.prototype.sendSignedTransaction = function (tx) {

let mgr = this;

return new Promise((resolve, reject) => {
try {
const httpProvider = new IconService.HttpProvider ('http://iconation.team:9000/api/v3');
let nid = parseInt(tx.nid, 16);
let url = mgr.iconNetworks[nid].url;
const httpProvider = new IconService.HttpProvider (url + "/api/v3");
const iconService = new IconService (httpProvider);
txHash = iconService.sendTransaction (tx).execute();
return resolve (txHash);
Expand Down Expand Up @@ -560,15 +571,19 @@ SvalinnManager.prototype.notifyUpdate = function (msg) {
uiupdater.updateUiState(msg);
};

SvalinnManager.prototype.getBalance = function (address) {
SvalinnManager.prototype.getBalance = function (address, nid) {

let mgr = this;

return new Promise ((resolve, reject) => {
try {
const httpProvider = new IconService.HttpProvider ('http://iconation.team:9000/api/v3');
let url = mgr.iconNetworks[nid].url;
const httpProvider = new IconService.HttpProvider (url + "/api/v3");
const iconService = new IconService (httpProvider);
const balance = iconService.getBalance (address).execute();
return resolve (balance);
} catch (err) {
console.log("[getBalance] err : ", err);
return reject (err);
}
});
Expand Down
76 changes: 58 additions & 18 deletions src/js/wsui_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ const WALLET_REFRESH_INTERVAL = 4 * 1000;

let refreshWalletWorker;


let WALLET_OPEN_IN_PROGRESS = false;

/* dom elements vars; */
Expand All @@ -59,6 +58,7 @@ let overviewWalletRescanButton;
let overviewPaymentIdGen;
let overviewIntegratedAddressGen;
let overviewShowKeyButton;
let overviewNetworkId;
// addressbook page
let addressBookInputName;
let addressBookInputWallet;
Expand Down Expand Up @@ -159,6 +159,8 @@ function populateElementVars() {
overviewPaymentIdGen = document.getElementById('payment-id-gen');
overviewIntegratedAddressGen = document.getElementById('integrated-wallet-gen');
overviewWalletRescanButton = document.getElementById('button-overview-rescan');
overviewNetworkId = document.getElementById('overview-network-id');

// addressbook page
addressBookInputName = document.getElementById('input-addressbook-name');
addressBookInputWallet = document.getElementById('input-addressbook-wallet');
Expand Down Expand Up @@ -1248,6 +1250,34 @@ function handleWalletOpen() {
}
}

function balanceUpdate (address)
{
let networkId = overviewNetworkId ? parseInt(overviewNetworkId.value) : 1;

wsmanager.getBalance (address, networkId).then ((balance) => {
balance = Number.parseFloat (balance / Math.pow(10, 18));
wsmanager.notifyUpdate ({
type: 'balanceUpdated',
data: balance
});
formMessageReset();
}).catch((err) => {
wsmanager.notifyUpdate ({
type: 'balanceUpdated',
data: "..."
});
formMessageSet('overview', 'warning', `Cannot get the balance (are you connected to Internet?)`);
});
}

overviewNetworkId.addEventListener ('change', () => {
wsmanager.notifyUpdate ({
type: 'balanceUpdated',
data: "..."
});
balanceUpdate (overviewWalletAddress.value);
});

walletOpenButtonOpen.addEventListener('click', () => {
formMessageReset();

Expand All @@ -1267,19 +1297,6 @@ function handleWalletOpen() {
return false;
}

function balanceUpdate (address) {

wsmanager.getBalance (address).then ((balance) => {
balance = Number.parseFloat (balance / Math.pow(10, 18));
wsmanager.notifyUpdate ({
type: 'balanceUpdated',
data: balance
});
}).catch((err) => {
formMessageSet('load', 'warning', `Cannot get the balance (are you connected to Internet?)`);
});
}

function onSuccess() {
walletOpenInputPath.value = settings.get('recentWallet');
overviewWalletAddress.value = wsession.get('loadedWalletAddress');
Expand Down Expand Up @@ -1650,6 +1667,23 @@ function handleSendTransfer() {
return;
}

// Check network ID
let networkId = walletOpenInputNode.value ? walletOpenInputNode.value.trim() : '';
if (networkId == '') {
formMessageSet('send', 'error', `You need to submit a network.`);
return;
}

networkId = parseInt(networkId);

if (networkId > wsmanager.iconNetworks.length) {
formMessageSet('send', 'error', `Invalid network.`);
return;
}

let network = wsmanager.iconNetworks[networkId];

// Check amount
let amount = sendInputAmount.value ? parseFloat(sendInputAmount.value) : 0;

if (amount < 0) {
Expand All @@ -1662,6 +1696,7 @@ function handleSendTransfer() {
return;
}

// Check fee
let fee = sendInputFee.value ? parseFloat(sendInputFee.value) : 0;
let minFee = config.minimumFee;
if (fee < minFee) {
Expand All @@ -1684,14 +1719,12 @@ function handleSendTransfer() {
from: wsession.get('loadedWalletAddress'),
value: Math.trunc (Math.pow (10, 18) * amount), // 1 ICX = 10**18 Loops
stepLimit: fee,
nid: 2,
nid: network.nid,
nonce: 0,
version: "0x3",
timestamp: timeStampInMs * 1000
};

console.log(tx);

// if (paymentId.length) tx.paymentId = paymentId;

let tpl = `
Expand All @@ -1708,6 +1741,8 @@ function handleSendTransfer() {
<dd class="dd-ib">${tx.value / Math.pow (10, 18)} ${config.assetTicker}</dd>
<dt class="dt-ib">Step Limit</dt>
<dd class="dd-ib">${tx.stepLimit} steps</dd>
<dt class="dt-ib">Network</dt>
<dd class="dd-ib">${wsmanager.iconNetworks[tx.nid].desc} (${wsmanager.iconNetworks[tx.nid].url})</dd>
</dl>
</div>
<div class="div-panel-buttons">
Expand Down Expand Up @@ -1784,6 +1819,9 @@ function handleSendTransfer() {
return this;
}

let nid = parseInt(tx.nid, 16);
let txValue = tx.value ? parseInt(tx.value, 16) / Math.pow (10, 18) : 0;

let tpl = `
<div class="div-transaction-panel">
<h4>Transfer Confirmation</h4>
Expand All @@ -1795,9 +1833,11 @@ function handleSendTransfer() {
<dt class="dt-ib">Destination address:</dt>
<dd class="dd-ib">${tx.to}</dd>
<dt class="dt-ib">Amount:</dt>
<dd class="dd-ib">${tx.value ? parseInt(tx.value, 16) / Math.pow (10, 18) : 0} ${config.assetTicker}</dd>
<dd class="dd-ib">${txValue} ${config.assetTicker}</dd>
<dt class="dt-ib">Step Limit</dt>
<dd class="dd-ib">${parseInt(tx.stepLimit, 16)} steps</dd>
<dt class="dt-ib">Network</dt>
<dd class="dd-ib">${wsmanager.iconNetworks[nid].desc} (${wsmanager.iconNetworks[nid].url})</dd>
</dl>
</div>
<div class="div-panel-buttons">
Expand Down

0 comments on commit fb3ea14

Please sign in to comment.