Skip to content
This repository has been archived by the owner on Nov 21, 2019. It is now read-only.

Commit

Permalink
Merge pull request #2068 from SteveMieskoski/mew-connect-integration
Browse files Browse the repository at this point in the history
integrate mew connect
  • Loading branch information
gamalielhere authored Sep 17, 2018
2 parents aa06b96 + dd12791 commit 8e74b8a
Show file tree
Hide file tree
Showing 32 changed files with 237,456 additions and 184,540 deletions.
20 changes: 20 additions & 0 deletions app/images/MEWconnectLogo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/images/appstore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions app/includes/header.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,25 @@
</span>
</a>
</li>
<li class="nav-item" ng-show="MEWconnectActive" ng-cloak>
<div style="margin-left: 20px;">
<div ng-show="MEWconnectState == 0" style="border-bottom: solid 2px #929292">
<h4 ><img src="images/MEWconnectLogo.svg" width="120" height="26"> Not Connected</h4>
</div>
<div ng-show="MEWconnectState == 1" style="border-bottom: solid 2px #1995be">
<h4><img src="images/MEWconnectLogo.svg" width="120" height="26" style="margin-right: 10px">Preparing Connection</h4>
</div>
<div ng-show="MEWconnectState == 2" style="border-bottom: solid 2px #23aeb2">
<h4 ><img src="images/MEWconnectLogo.svg" width="120" height="26"> Connected</h4>
</div>
<div ng-show="MEWconnectState == 3" style="border-bottom: solid 2px #febe1a">
<h4><img src="images/MEWconnectLogo.svg" width="120" height="26"> Timed Out</h4>
</div>
<div ng-show="MEWconnectState == 4" style="border-bottom: solid 2px #e74d41">
<h4><img src="images/MEWconnectLogo.svg" width="120" height="26"> Disconnected</h4>
</div>
</div>
</li>
</ul>
</div>
<a aria-hidden="true"
Expand Down
7 changes: 7 additions & 0 deletions app/scripts/controllers/contractsCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,12 @@ var contractsCtrl = function($scope, $sce, walletService) {
$scope.tx.to = $scope.contract.address;
$scope.sendContractModal.open();
}

$scope.reOpenDecryptWalletMEWconnect = function () {
if ($scope.globalService.currentTab === 6) {
$scope.wd = false
}
}
globalFuncs.MEWconnectStatus.registerDecryptOpeners($scope.reOpenDecryptWalletMEWconnect.bind(this))
}
module.exports = contractsCtrl;
87 changes: 85 additions & 2 deletions app/scripts/controllers/decryptWalletCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ var decryptWalletCtrl = function($scope, $sce, walletService) {
hwEther1Path: "m/44'/1313114'/0'/0", // first address: m/44'/1313114'/0'/0/0
hwAtheiosPath: "m/44'/1620'/0'/0", // first address: m/44'/1620'/0'/0/0
};
$scope.canUseMewConnect = MewConnectEth.checkWebRTCAvailable();
$scope.mewConnectMayFail = MewConnectEth.checkBrowser();
$scope.HDWallet.dPath = $scope.HDWallet.defaultDPath;
$scope.mnemonicModel = new Modal(document.getElementById('mnemonicModel'));
$scope.$watch('ajaxReq.type', function() {
Expand Down Expand Up @@ -229,6 +231,8 @@ var decryptWalletCtrl = function($scope, $sce, walletService) {
$scope.scanDigitalBitbox();
} else if ($scope.walletType == 'secalot') {
$scope.scanSecalot();
} else if ($scope.walletType == 'mewConnect') {
$scope.scanMewConnect()
}
}
$scope.onCustomHDDPathChange = function() {
Expand Down Expand Up @@ -295,6 +299,8 @@ var decryptWalletCtrl = function($scope, $sce, walletService) {
$scope.HDWallet.wallets.push(new Wallet(undefined, derivedKey.publicKey, $scope.HDWallet.dPath + "/" + i, $scope.walletType, $scope.digitalBitbox));
} else if ($scope.walletType == "secalot") {
$scope.HDWallet.wallets.push(new Wallet(undefined, derivedKey.publicKey, $scope.HDWallet.dPath + "/" + i, $scope.walletType, $scope.secalot));
} else if ($scope.walletType == 'mewConnect') {
$scope.HDWallet.wallets.push(new Wallet(undefined, derivedKey.publicKey, $scope.HDWallet.dPath + '/' + i, $scope.walletType, $scope.mewConnect))
} else {
$scope.HDWallet.wallets.push(new Wallet(undefined, derivedKey.publicKey, $scope.HDWallet.dPath + "/" + i, $scope.walletType));
}
Expand All @@ -305,7 +311,7 @@ var decryptWalletCtrl = function($scope, $sce, walletService) {
$scope.HDWallet.numWallets = start + limit;
}
$scope.AddRemoveHDAddresses = function(isAdd) {
if ($scope.walletType == "ledger" || $scope.walletType == "trezor" || $scope.walletType == "digitalBitbox" || $scope.walletType == "secalot") {
if ($scope.walletType == "ledger" || $scope.walletType == "trezor" || $scope.walletType == "digitalBitbox" || $scope.walletType == "secalot" || $scope.walletType == 'mewConnect') {
if (isAdd) $scope.setHDAddressesHWWallet($scope.HDWallet.numWallets, $scope.HDWallet.walletsPerDialog);
else $scope.setHDAddressesHWWallet($scope.HDWallet.numWallets - 2 * $scope.HDWallet.walletsPerDialog, $scope.HDWallet.walletsPerDialog);
} else {
Expand Down Expand Up @@ -450,6 +456,83 @@ var decryptWalletCtrl = function($scope, $sce, walletService) {
console.warn("SCANTR", path, $scope.HDWallet)
TrezorConnect.getXPubKey(path, $scope.trezorCallback, '1.5.2');
};
// ================= Mew Connect (start)==============================
$scope.scanMewConnect = function () {
globalFuncs.MEWconnectStatus.newTabOpenedTrigger(true);
globalFuncs.MEWconnectStatus.update(0)
var app = new MewConnectEth()

$scope.mewConnect = MewConnect.init(null, null, {})

Reflect.defineProperty(MewConnect, 'instance', {
value: $scope.mewConnect
})

$scope.$on('$destroy', function () {
globalFuncs.MEWconnectStatus.newTabOpenedTrigger(false);
globalFuncs.MEWconnectStatus.update(0)
$scope.mewConnect.disconnectRTC()
if (MewConnect.instance) {
Reflect.deleteProperty(MewConnect, 'instance')
}
})

$scope.mewConnect.on('codeDisplay', codeDisplay)
$scope.mewConnect.on('RtcConnectedEvent', rtcConnected)
$scope.mewConnect.on('RtcClosedEvent', rtcClosed)
// $scope.mewConnect.on('RtcDisconnectEvent', rtcDisconnected)
$scope.mewConnect.on('address', makeWallet)

app.setMewConnect($scope.mewConnect)
app.signalerConnect()

$scope.connectionCodeTimeout = null

function rtcConnected (data) {
if ($scope.connectionCodeTimeout) {
clearTimeout($scope.connectionCodeTimeout)
}
globalFuncs.MEWconnectStatus.update(2)
$scope.connectionCodeTimeout = null
uiFuncs.notifier.info('Connected Via Mew Connect')
$scope.mewConnect.sendRtcMessage('address', '')
$scope.mewConnectionStatus = 2
}

function rtcClosed (data) {
globalFuncs.MEWconnectStatus.update(4)
$scope.mewConnectionStatus = 4
$scope.wallet = null
walletService.wallet = null
uiFuncs.notifier.danger('Disconnected', 10000)
if (!$scope.$$phase) $scope.$apply()
}


function codeDisplay (data) {
$scope.mewConnectionStatus = 1
globalFuncs.MEWconnectStatus.update(1)
$scope.mewConnectCode = data
$scope.connectionCodeTimeout = setTimeout(() => {
$scope.mewConnectionStatus = 3
globalFuncs.MEWconnectStatus.update(3)
if (!$scope.$$phase) $scope.$apply()
}, 119800) // 200ms before the actual server timeout happens. (to account for transit time, ui lag, etc.)
if (!$scope.$$phase) $scope.$apply()
}

function makeWallet (data) {
var wallet = app.createWallet(data)
$scope.wallet = wallet
walletService.wallet = wallet
if (!$scope.$$phase) $scope.$apply()
}
}

$scope.mewConnectDisconnect = function () {
$scope.mewConnect.disconnectRTC()
}
//= ================ Mew Connect (end)==============================
$scope.getLedgerPath = function() {
return $scope.HDWallet.dPath;
}
Expand All @@ -460,7 +543,7 @@ var decryptWalletCtrl = function($scope, $sce, walletService) {
if (window.web3 === undefined) {
window.addEventListener('message', ({data}) => {
if (data && data.type && data.type === 'ETHEREUM_PROVIDER_SUCCESS') {
window.web3 = new Web3(ethereum);
window.web3 = new Web3(ethereum);
}
});
window.postMessage({ type: 'ETHEREUM_PROVIDER_REQUEST', web3: true }, '*');
Expand Down
7 changes: 6 additions & 1 deletion app/scripts/controllers/sendTxCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,14 @@ var sendTxCtrl = function($scope, $sce, walletService, $rootScope) {
$scope.parsedSignedTx.txFee.eth = etherUnits.toEther( parseInt($scope.parsedSignedTx.txFee.wei), 'wei' ).toString()
$scope.parsedSignedTx.nonce = (txData.nonce=='0x'||txData.nonce==''||txData.nonce==null) ? '0' : new BigNumber(ethFuncs.sanitizeHex(txData.nonce.toString('hex'))).toString()
$scope.parsedSignedTx.data = (txData.data=='0x'||txData.data==''||txData.data==null) ? '(none)' : ethFuncs.sanitizeHex(txData.data.toString('hex'))
}


$scope.reOpenDecryptWalletMEWconnect = function () {
if ($scope.globalService.currentTab === 3) {
$scope.wd = false
}
}
globalFuncs.MEWconnectStatus.registerDecryptOpeners($scope.reOpenDecryptWalletMEWconnect.bind(this))

};
module.exports = sendTxCtrl;
34 changes: 31 additions & 3 deletions app/scripts/controllers/signMsgCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ var signMsgCtrl = function($scope, $sce, walletService) {
$scope.notifier.info("Tap a touch button on your device to confirm signing.");
var app = new SecalotEth($scope.wallet.getHWTransport());
app.signMessage($scope.wallet.getPath(), thisMessage, localCallback);

// Sign via trezor
} else if ((typeof hwType != "undefined") && (hwType == "trezor")) {
TrezorConnect.ethereumSignMessage($scope.wallet.getPath(), thisMessage, function(response) {
Expand All @@ -144,7 +144,28 @@ var signMsgCtrl = function($scope, $sce, walletService) {
}
})

// Sign via PK
//================= Mew Connect (start)==============================
} else if (typeof hwType != "undefined" && hwType == "mewConnect") {
//TODO reset ui when rtc disconnects
// var msg = Buffer.from(thisMessage).toString("hex");
var connectApp = new MewConnectEth();
var mewConnect = MewConnect.instance;
connectApp.setMewConnect(mewConnect);
mewConnect.on('signMessage', (data) =>{
$scope.signMsg.signedMsg = JSON.parse(data);
$scope.notifier.success('Successfully Signed Message with ' + $scope.wallet.getAddressString());
})
mewConnect.on('sign', (data) =>{
$scope.signMsg.signedMsg = JSON.parse(data);
$scope.notifier.success('Successfully Signed Message with ' + $scope.wallet.getAddressString());
})
//TODO hash message before send. Currently sending as plain text
ethUtil.hashPersonalMessage(ethUtil.toBuffer(thisMessage)).toString('hex')
connectApp.signMessage(thisMessage);

//================= Mew Connect (end)==============================

// Sign via PK
} else {
var msg = ethUtil.hashPersonalMessage(ethUtil.toBuffer(thisMessage))
var signed = ethUtil.ecsign(msg, $scope.wallet.getPrivateKey())
Expand Down Expand Up @@ -211,5 +232,12 @@ var signMsgCtrl = function($scope, $sce, walletService) {
$scope.visibility = str;
}

$scope.reOpenDecryptWalletMEWconnect = function () {
if ($scope.globalService.currentTab === 11) {
$scope.wd = false
}
}
globalFuncs.MEWconnectStatus.registerDecryptOpeners($scope.reOpenDecryptWalletMEWconnect.bind(this))

}
module.exports = signMsgCtrl
module.exports = signMsgCtrl
53 changes: 21 additions & 32 deletions app/scripts/controllers/swapCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,6 @@ var swapCtrl = function ($scope, $sce, walletService) {

$scope.verifyMinMaxValues = function () {
if ($scope.checkIfKyber()) {
// // $scope.swapOrder.toAddress = ''
// if($scope.orderRetrievedFromStorage){
// $scope.kyberSwapOrder = {
// fromCoin: $scope.swapOrder.fromCoin,
// toCoin: $scope.swapOrder.toCoin,
// isFrom: true,
// fromVal: $scope.swapOrder.toVal,
// toVal: $scope.swapOrder.toAddress,
// toAddress: '',
// swapRate: '',
// swapPair: ''
// };
// /*{
// $scope.swapOrder
// }*/
// }

return $scope.verifyKyberMinMaxValues();
} else {
return $scope.verifyBityMinMaxValues();
Expand Down Expand Up @@ -424,7 +407,12 @@ var swapCtrl = function ($scope, $sce, walletService) {
};

$scope.kyberInit = function () {
$scope.checkKyberNetwork();
// Recent Additions
$scope.kyberSwapOrder = {};
$scope.parsedKyberTx = {};
$scope.kyberModal = {};
$scope.addressString = null;
// REcent Additions End
$scope.receiveDecimals = 6;
$scope.sendDecimals = 6;
$scope.showedKyberPairAvailableError = false;
Expand Down Expand Up @@ -482,6 +470,7 @@ var swapCtrl = function ($scope, $sce, walletService) {
currency: "" //$scope.swapOrder.toCoin
}
};
$scope.checkKyberNetwork();
};


Expand Down Expand Up @@ -703,11 +692,6 @@ var swapCtrl = function ($scope, $sce, walletService) {
} else {
callback(false);
}
// if (data >= checkValue) {
// $scope.setKyberStatus($scope.kyberStatus.token.send); //TOKENS_APPROVED
// $scope.sendKyberTransaction();
// }
// if (!$scope.$$phase) $scope.$apply();
}
});
}
Expand Down Expand Up @@ -928,12 +912,12 @@ var swapCtrl = function ($scope, $sce, walletService) {
$scope.tx = $scope.buildTransactionObject($scope.kyber.approveKyber($scope.kyberSwapOrder.fromCoin, 0), $scope.kyber.getTokenAddress($scope.kyberSwapOrder.fromCoin));
var txData = uiFuncs.getTxData($scope);
txData.nonce = txData.gasPrice = null;
$scope.generateKyberTransaction(txData, $scope.kyberStatus.token.resetApprove);
return $scope.generateKyberTransaction(txData, $scope.kyberStatus.token.resetApprove);
} else {
if ($scope.kyberTransaction.bypassTokenApprove) {
$scope.openKyberTokenOrder();
return $scope.openKyberTokenOrder();
} else {
$scope.approveTokenKyber();
return $scope.approveTokenKyber();
}

}
Expand All @@ -943,6 +927,10 @@ var swapCtrl = function ($scope, $sce, walletService) {
};

$scope.approveTokenKyber = function (nonce, gasPrice) {
if($scope.kyberTransaction.tokenApproveTx) {
$scope.openKyberTokenOrder(nonce, gasPrice);
return;
}
try {
$scope.tx = $scope.buildTransactionObject($scope.kyber.approveKyber($scope.kyberSwapOrder.fromCoin, $scope.kyberSwapOrder.fromVal), $scope.kyber.getTokenAddress($scope.kyberSwapOrder.fromCoin));
var txData = uiFuncs.getTxData($scope);
Expand All @@ -951,7 +939,7 @@ var swapCtrl = function ($scope, $sce, walletService) {
txData.nonce = "0x" + ethFuncs.decimalToHex(newNonce + 1);
txData.gasPrice = gasPrice;
} else txData.nonce = txData.gasPrice = null;
$scope.generateKyberTransaction(txData, $scope.kyberStatus.token.prepareApprove);
return $scope.generateKyberTransaction(txData, $scope.kyberStatus.token.prepareApprove);

} catch (e) {
console.error(e);
Expand All @@ -970,7 +958,7 @@ var swapCtrl = function ($scope, $sce, walletService) {
} else {
txData.nonce = txData.gasPrice = null;
}
$scope.generateKyberTransaction(txData, $scope.kyberStatus.token.prepare);
return $scope.generateKyberTransaction(txData, $scope.kyberStatus.token.prepare);
} catch (e) {
console.error(e);
}
Expand Down Expand Up @@ -1020,7 +1008,7 @@ var swapCtrl = function ($scope, $sce, walletService) {
switch (stage) {
case "RESET_TOKEN_APPROVAL":
case $scope.kyberStatus.token.resetApprove:
$scope.kyberTransaction.tokenResetTx = rawTx;
$scope.kyberTransaction.tokenResetTx = Object.assign({}, rawTx);
// Trezor needs time between sequential confirmations or it starts before the prior window
// finishes closing and does not initiate a new window, instead throws a window closed error
setTimeout(function () {
Expand All @@ -1029,7 +1017,7 @@ var swapCtrl = function ($scope, $sce, walletService) {
break;
case "GENERATE_APPROVAL_TRANSACTION":
case $scope.kyberStatus.token.prepareApprove:
$scope.kyberTransaction.tokenApproveTx = rawTx;
$scope.kyberTransaction.tokenApproveTx = Object.assign({}, rawTx);
// Build the Swap transaction in the same flow
setTimeout(function () {
// Trezor needs time between sequential confirmations or it starts before the prior window
Expand All @@ -1039,7 +1027,7 @@ var swapCtrl = function ($scope, $sce, walletService) {
break;
case "GENERATE_SWAP_TRANSACTION":
case $scope.kyberStatus.token.prepare:
$scope.kyberTransaction.tokenTx = rawTx;
$scope.kyberTransaction.tokenTx = Object.assign({}, rawTx);
// update status to reflect transaction generation and prompting of user for approval
if ($scope.kyberTransaction.tokenNeedsReset) {
$scope.setKyberStatus($scope.kyberStatus.token.resetApprove); //APPROVE_TOKENS
Expand All @@ -1050,7 +1038,7 @@ var swapCtrl = function ($scope, $sce, walletService) {
break;
case "OPEN_ETH":
case $scope.kyberStatus.eth.open:
$scope.kyberTransaction.ethTx = rawTx;
$scope.kyberTransaction.ethTx = Object.assign({}, rawTx);
$scope.setKyberStatus($scope.kyberStatus.eth.send); //SEND_ETH
$scope.sendKyberModal();
break;
Expand All @@ -1076,6 +1064,7 @@ var swapCtrl = function ($scope, $sce, walletService) {
// Generate the content to populate the modal that prompts of user for approval
$scope.parseKyberSignedTx($scope.kyberTransaction.tokenTx.signedTx);
$scope.kyberModal.open();
break;
case "RESET_TOKEN_APPROVAL":
// Generate the content to populate the modal that prompts of user for approval
$scope.parseKyberSignedTx($scope.kyberTransaction.tokenTx.signedTx);
Expand Down
Loading

0 comments on commit 8e74b8a

Please sign in to comment.