WIP trezor support, wallet creation working

This commit is contained in:
Matias Alejo Garcia 2015-09-26 08:26:31 -03:00
commit 9ce342bba7
8 changed files with 542 additions and 24 deletions

View file

@ -37,7 +37,8 @@ module.exports = function(grunt) {
'src/js/routes.js',
'src/js/services/*.js',
'src/js/models/*.js',
'src/js/controllers/*.js'
'src/js/controllers/*.js',
'src/js/trezor.js'
],
tasks: ['concat:js']
}
@ -77,7 +78,8 @@ module.exports = function(grunt) {
'src/js/controllers/*.js',
'src/js/translations.js',
'src/js/version.js',
'src/js/init.js'
'src/js/init.js',
'src/js/trezor.js'
],
dest: 'public/js/copay.js'
},

View file

@ -8,7 +8,7 @@
<div class="content p20v" ng-controller="createController as create" ng-init="create.setTotalCopayers(1)">
<div class="onGoingProcess" ng-show="create.loading && !create.ledger">
<div class="onGoingProcess" ng-show="create.loading && !create.hwWallet">
<div class="onGoingProcess-content" ng-style="{'background-color':'#222'}">
<div class="spinner">
<div class="rect1"></div>
@ -21,7 +21,7 @@
</div>
</div>
<div class="onGoingProcess" ng-show="create.ledger">
<div class="onGoingProcess" ng-show="create.hwWallet">
<div class="onGoingProcess-content" ng-style="{'background-color':'#222'}">
<div class="spinner">
<div class="rect1"></div>
@ -30,7 +30,7 @@
<div class="rect4"></div>
<div class="rect5"></div>
</div>
<span translate>Connecting to Ledger Wallet...</span>
<span translate>Connecting to {{create.hwWallet}} Wallet...</span>
</div>
</div>
@ -102,10 +102,16 @@
</div>
<div ng-hide="hideAdv" class="row">
<div class="large-12 columns">
<label ng-show="create.isChromeApp() && totalCopayers > 1 " for="hw-ledger" class="oh">
<span translate>Use Ledger hardware wallet</span>
<switch id="hw-ledger" name="hwLedger" ng-model="hwLedger" ng-change="isTestnet=false" class="green right m5t m10b"></switch>
</label>
<label ng-show="1" for="hw-trezor" class="oh">
<span translate>Use TREZOR hardware wallet</span>
<switch id="hw-trezor" name="hwTrezor" ng-model="hwTrezor" ng-change="isTestnet=false" class="green right m5t m10b"></switch>
</label>
<!-- TODO account
<div ng-show="hwLedger">
<label class="oh"><span translate>Ledger Slot</span>
@ -115,17 +121,17 @@
<div class="oh text-gray line-b size-12 p10b m20b"><span translate>Ledger supports up to 20 Copay wallets simultaneously. Select which slot should be used to host this wallet</div>
</div>
-->
<label for="network-name" class="oh" ng-show="!hwLedger">
<label for="network-name" class="oh" ng-show="!hwLedger && !hwTrezor">
<span translate>Testnet</span>
<switch id="network-name" name="isTestnet" ng-model="isTestnet" class="green right m5t m10b"></switch>
</label>
<label ng-show="!hwLedger" for="seed" class="oh">
<label ng-show="!hwLedger && !hwTrezor" for="seed" class="oh">
<span translate>Specify your wallet seed</span>
<switch id="seed" name="setSeed" ng-model="setSeed" class="green right m5t m10b"></switch>
</label>
<label for="createPassphrase" class="line-b oh" ng-show="!setSeed && !hwLedger" ><span translate>Seed Passphrase</span> <small translate>Add an optional passphrase to secure the seed</small>
<label for="createPassphrase" class="line-b oh" ng-show="!setSeed && !hwLedger && !hwTrezor" ><span translate>Seed Passphrase</span> <small translate>Add an optional passphrase to secure the seed</small>
<div class="input">
<input type="text" class="form-control"
name="createPassphrase" ng-model="createPassphrase">
@ -139,7 +145,7 @@
type="text"
name="privateKey" ng-model="privateKey">
</label>
<label for="passphrase" class="line-b oh" ng-show="setSeed && !hwLedger"><span translate>Seed Passphrase</span> <small translate>The seed could require a passphrase to be imported</small>
<label for="passphrase" class="line-b oh" ng-show="setSeed && !hwLedger && !hwTrezor"><span translate>Seed Passphrase</span> <small translate>The seed could require a passphrase to be imported</small>
<div class="input">
<input type="text" class="form-control" name="passphrase" ng-model="passphrase">
</div>
@ -158,11 +164,11 @@
<button type="submit" class="button round black expand m0" ng-show="totalCopayers != 1" ng-disabled="setupForm.$invalid || create.loading || create.ledger">
<button type="submit" class="button round black expand m0" ng-show="totalCopayers != 1" ng-disabled="setupForm.$invalid || create.loading || create.hwWallet">
<span translate>Create {{requiredCopayers}}-of-{{totalCopayers}} wallet</span>
</button>
<button type="submit" class="button round black expand m0" ng-show="totalCopayers == 1" ng-disabled="setupForm.$invalid || create.loading || create.ledger">
<button type="submit" class="button round black expand m0" ng-show="totalCopayers == 1" ng-disabled="setupForm.$invalid || create.loading || create.hwWallet">
<span translate>Create new wallet</span>
</button>
</div>

View file

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('createController',
function($scope, $rootScope, $location, $timeout, $log, lodash, go, profileService, configService, isMobile, isCordova, gettext, isChromeApp, ledger) {
function($scope, $rootScope, $location, $timeout, $log, lodash, go, profileService, configService, isMobile, isCordova, gettext, isChromeApp, ledger, trezor) {
var self = this;
var defaults = configService.getDefaults();
@ -76,11 +76,15 @@ angular.module('copayApp.controllers').controller('createController',
return;
}
if (form.hwLedger.$modelValue) {
self.ledger = true;
if (form.hwLedger.$modelValue || form.hwTrezor.$modelValue) {
self.hwWallet = form.hwLedger.$modelValue ? 'Leger' : 'TREZOR';
var src= form.hwLedger.$modelValue ? leger : trezor;
// TODO : account
ledger.getInfoForNewWallet(0, function(err, lopts) {
self.ledger = false;
var account = 0;
src.getInfoForNewWallet(account, function(err, lopts) {
self.hwWallet = false;
if (err) {
self.error = err;
$scope.$apply();

View file

@ -47,8 +47,8 @@ angular.module('copayApp.controllers').controller('joinController',
if (form.hwLedger.$modelValue) {
self.ledger = true;
// TODO account
ledger.getInfoForNewWallet(0, function(err, lopts) {
// TODO account / network
ledger.getInfoForNewWallet(0, opts.networkName, function(err, lopts) {
self.ledger = false;
if (err) {
self.error = err;

View file

@ -1176,4 +1176,5 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
this.setAddress();
this.setSendFormInputs();
}
});

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.services')
.factory('profileService', function profileServiceFactory($rootScope, $location, $timeout, $filter, $log, lodash, storageService, bwcService, configService, notificationService, isChromeApp, isCordova, gettext, gettextCatalog, nodeWebkit, bwsError, uxLanguage, ledger, bitcore) {
.factory('profileService', function profileServiceFactory($rootScope, $location, $timeout, $filter, $log, lodash, storageService, bwcService, configService, notificationService, isChromeApp, isCordova, gettext, gettextCatalog, nodeWebkit, bwsError, uxLanguage, ledger, bitcore, trezor) {
var root = {};
@ -236,6 +236,7 @@ angular.module('copayApp.services')
root._seedWallet(opts, function(err, walletClient) {
if (err) return cb(err);
console.log('[profileService.js.239:walletClient:]',walletClient); //TODO
walletClient.createWallet(opts.name, opts.myName || 'me', opts.m, opts.n, {
network: opts.networkName
}, function(err, secret) {
@ -561,16 +562,38 @@ angular.module('copayApp.services')
});
};
root._signWithTrezor = function(txp, cb) {
var fc = root.focusedClient;
$log.info('Requesting Trezor to sign the transaction');
trezor.signTx(txp, 0, function(result) {
console.log('[profileService.js.570:result:]',result); //TODO
if (!result.success)
return cb(result);
txp.signatures = lodash.map(result.signatures, function(s) {
return s.substring(0, s.length - 2);
});
return fc.signTxProposal(txp, cb);
});
};
root.signTxProposal = function(txp, cb) {
var fc = root.focusedClient;
if (fc.isPrivKeyExternal()) {
if (fc.getPrivKeyExternalSourceName() != 'ledger') {
var msg = 'Unsupported External Key:' + fc.getPrivKeyExternalSourceName();
$log.error(msg);
return cb(msg);
switch (fc.getPrivKeyExternalSourceName()) {
case 'ledger':
return root._signWithLedger(txp, cb);
case 'trezor':
return root._signWithTrezor(txp, cb);
default:
var msg = 'Unsupported External Key:' + fc.getPrivKeyExternalSourceName();
$log.error(msg);
return cb(msg);
}
return root._signWithLedger(txp, cb);
} else {
return fc.signTxProposal(txp, function(err, signedTxp) {
root.lockFC();

114
src/js/services/trezor.js Normal file
View file

@ -0,0 +1,114 @@
'use strict';
angular.module('copayApp.services')
.factory('trezor', function($log, $timeout, bwcService, gettext, lodash) {
var root = {};
root.ENTROPY_INDEX_PATH = "0xb11e/";
root.callbacks = {};
root.getEntropySource = function(account, callback) {
var path = root.ENTROPY_INDEX_PATH + account + "'";
var xpub = root.getXPubKey(path, function(data) {
if (!data.success) {
$log.warn(data.message);
return callback(data);
}
var b = bwcService.getBitcore();
var x = b.HDPublicKey(data.xpubkey);
data.entropySource = x.publicKey.toString();
return callback(data);
});
};
root.getXPubKeyForAddresses = function(account, callback) {
return root.getXPubKey(root._getPath(account), callback);
};
root.getXPubKey = function(path, callback) {
$log.debug('TREZOR deriving xPub path:', path);
TrezorConnect.getXPubKey(path, callback);
};
root.getInfoForNewWallet = function(account, callback) {
var opts = {};
root.getEntropySource(account, function(data) {
if (!data.success) {
$log.warn(data.message);
return callback(data.message);
}
opts.entropySource = data.entropySource;
$log.debug('Waiting TREZOR to settle...');
$timeout(function() {
root.getXPubKeyForAddresses(account, function(data) {
if (!data.success) {
$log.warn(data.message);
return callback(data);
}
opts.extendedPublicKey = data.xpubkey;
opts.externalSource = 'trezor';
opts.externalIndex = account;
console.log('[trezor.js.51:opts:]', opts); //TODO
return callback(null, opts);
});
}, 5000);
});
};
root.signTx = function(txp, account, callback) {
if (txp.type != 'simple')
return callback('Only simple TXs are supported in TREZOR');
console.log('[trezor.js.90:txp:]', txp); //TODO
if (txp.addressType == 'P2PKH') {
var tx = bwcService.getUtils().buildTx(txp);
// spend one change output
var inputs = lodash.map(txp.inputs, function(i){
var pathArr = i.path.split('/');
console.log('[trezor.js.72:pathArr:]',pathArr); //TODO
var n = [44 | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]) , parseInt(pathArr[2])];
return {
address_n: n,
prev_index: i.vout,
prev_hash: i.txid,
};
});
console.log('[trezor.js.68:inputs:]',inputs); //TODO
var pathArr = txp.changeAddress.path.split('/');
console.log('[trezor.js.82:pathArr:]',pathArr); //TODO
var n = [44 | 0x80000000, 0 | 0x80000000, account | 0x80000000, parseInt(pathArr[1]) , parseInt(pathArr[2])];
// send to 1 address output and one change output
var outputs = [{
address_n: n,
amount: txp.amount - txp.fee,
script_type: 'PAYTOADDRESS'
}, {
address: txp.toAddress,
amount: txp.amount,
script_type: 'PAYTOADDRESS'
}];
console.log('[trezor.js.84:outputs:]',outputs); //TODO
TrezorConnect.signTx(inputs, outputs, function(result) {
console.log('[trezor.js.78:result:]', result); //TODO
callback(result);
});
} else {
var msg = 'P2SH wallets are not supported with TREZOR';
$log.error(msg);
return callback(msg);
}
}
root._getPath = function(account) {
return "44'/0'/" + account + "'";
}
return root;
});

368
src/js/trezor.js Normal file
View file

@ -0,0 +1,368 @@
window.TrezorConnect = (function () {
'use strict';
var CONNECT_ORIGIN = 'https://trezor.github.io';
var CONNECT_PATH = CONNECT_ORIGIN + '/connect';
var CONNECT_POPUP = CONNECT_PATH + '/popup/popup.html';
var ERR_TIMED_OUT = 'Loading timed out';
var ERR_WINDOW_CLOSED = 'Window closed';
var ERR_ALREADY_WAITING = 'Already waiting for a response';
var manager = new PopupManager(
CONNECT_POPUP,
CONNECT_ORIGIN,
'trezor-connect',
function () {
var w = 600;
var h = 500;
var x = (screen.width - w) / 2;
var y = (screen.height - h) / 3;
var params =
'height=' + h +
',width=' + w +
',left=' + x +
',top=' + y +
',menubar=no' +
',toolbar=no' +
',location=no' +
',personalbar=no' +
',status=no';
return params;
}
);
/**
* Public API.
*/
function TrezorConnect() {
/**
* Popup errors.
*/
this.ERR_TIMED_OUT = ERR_TIMED_OUT;
this.ERR_WINDOW_CLOSED = ERR_WINDOW_CLOSED;
this.ERR_ALREADY_WAITING = ERR_ALREADY_WAITING;
/**
* @typedef XPubKeyResult
* @param {boolean} success
* @param {?string} error
* @param {?string} xpubkey serialized extended public key
*/
/**
* Load BIP32 extended public key by path.
*
* Path can be specified either in the string form ("m/44'/1/0") or as
* raw integer array.
*
* @param {string|array<number>} path
* @param {function(XPubKeyResult)} callback
*/
this.getXPubKey = function (path, callback) {
if (typeof path === 'string') {
path = parseHDPath(path);
}
manager.sendWithChannel({
'type': 'xpubkey',
'path': path
}, function (result) {
manager.close();
callback(result);
});
};
/**
* @typedef SignTxResult
* @param {boolean} success
* @param {?string} error
* @param {?string} serialized_tx serialized tx, in hex, including signatures
* @param {?array<string>} signatures array of input signatures, in hex
*/
/**
* Sign a transaction in the device and return both serialized
* transaction and the signatures.
*
* @param {array<TxInputType>} inputs
* @param {array<TxOutputType>} outputs
* @param {function(SignTxResult)} callback
*
* @see https://github.com/trezor/trezor-common/blob/master/protob/types.proto
*/
this.signTx = function (inputs, outputs, callback) {
manager.sendWithChannel({
'type': 'signtx',
'inputs': inputs,
'outputs': outputs
}, function (result) {
manager.close();
callback(result);
});
};
/**
* @typedef RequestLoginResult
* @param {boolean} success
* @param {?string} error
* @param {?string} public_key public key used for signing, in hex
* @param {?string} signature signature, in hex
*/
/**
* Sign a login challenge for active origin.
*
* @param {?string} hosticon
* @param {string} challenge_hidden
* @param {string} challenge_visual
* @param {string|function(RequestLoginResult)} callback
*
* @see https://github.com/trezor/trezor-common/blob/master/protob/messages.proto
*/
this.requestLogin = function (
hosticon,
challenge_hidden,
challenge_visual,
callback
) {
if (typeof callback === 'string') {
// special case for a login through <trezor:login> button.
// `callback` is name of global var
callback = window[callback];
}
if (!callback) {
throw new TypeError('TrezorConnect: login callback not found');
}
manager.sendWithChannel({
'type': 'login',
'icon': hosticon,
'challenge_hidden': challenge_hidden,
'challenge_visual': challenge_visual
}, function (result) {
manager.close();
callback(result);
});
};
var LOGIN_CSS =
'<style>@import url("' + CONNECT_PATH + '/login_buttons.css")</style>';
var LOGIN_ONCLICK =
'TrezorConnect.requestLogin('
+ "'@hosticon@','@challenge_hidden@','@challenge_visual@','@callback@'"
+ ')';
var LOGIN_HTML =
'<div id="trezorconnect-wrapper">'
+ ' <a id="trezorconnect-button" onclick="' + LOGIN_ONCLICK + '">'
+ ' <span id="trezorconnect-icon"></span>'
+ ' <span id="trezorconnect-text">@text@</span>'
+ ' </a>'
+ ' <span id="trezorconnect-info">'
+ ' <a id="trezorconnect-infolink" href="https://www.buytrezor.com/"'
+ ' target="_blank">What is TREZOR?</a>'
+ ' </span>'
+ '</div>';
/**
* Find <trezor:login> elements and replace them with login buttons.
* It's not required to use these special elements, feel free to call
* `TrezorConnect.requestLogin` directly.
*/
this.renderLoginButtons = function () {
var elements = document.getElementsByTagName('trezor:login');
for (var i = 0; i < elements.length; i++) {
var e = elements[i];
var text = e.getAttribute('text') || 'Sign in with TREZOR';
var callback = e.getAttribute('callback') || '';
var hosticon = e.getAttribute('icon') || '';
var challenge_hidden = e.getAttribute('challenge_hidden') || '';
var challenge_visual = e.getAttribute('challenge_visual') || '';
// it's not valid to put markup into attributes, so let users
// supply a raw text and make TREZOR bold
text = text.replace('TREZOR', '<strong>TREZOR</strong>');
e.parentNode.innerHTML =
LOGIN_CSS + LOGIN_HTML
.replace('@text@', text)
.replace('@callback@', callback)
.replace('@hosticon@', hosticon)
.replace('@challenge_hidden@', challenge_hidden)
.replace('@challenge_visual@', challenge_visual);
}
};
}
var exports = new TrezorConnect();
exports.renderLoginButtons();
return exports;
/*
* `getXPubKey()`
*/
function parseHDPath(string) {
return string
.toLowerCase()
.split('/')
.filter(function (p) { return p !== 'm'; })
.map(function (p) {
var n = parseInt(p);
if (p[p.length - 1] === "'") { // hardened index
n = n | 0x80000000;
}
return n;
});
}
/*
* Popup management
*/
function Popup(url, name, params) {
var w = window.open(url, name, params);
var interval;
var iterate = function () {
if (w.closed) {
clearInterval(interval);
if (this.onclose) {
this.onclose();
}
}
}.bind(this);
interval = setInterval(iterate, 100);
this.window = w;
this.onclose = null;
}
function Channel(target, origin, waiting) {
var respond = function (data) {
if (waiting) {
var callback = waiting;
waiting = null;
callback(data);
}
};
var receive = function (event) {
if (event.source === target && event.origin === origin) {
respond(event.data);
}
};
window.addEventListener('message', receive);
this.respond = respond;
this.close = function () {
window.removeEventListener('message', receive);
};
this.send = function (value, callback) {
if (waiting === null) {
waiting = callback;
target.postMessage(value, origin);
} else {
throw new Error(ERR_ALREADY_WAITING);
}
};
}
function ConnectedChannel(url, origin, name, params) {
var ready = function () {
clearTimeout(this.timeout);
this.popup.onclose = null;
this.ready = true;
this.onready();
}.bind(this);
var closed = function () {
clearTimeout(this.timeout);
this.channel.close();
this.onerror(new Error(ERR_WINDOW_CLOSED));
}.bind(this);
var timedout = function () {
this.popup.onclose = null;
this.popup.window.close();
this.channel.close();
this.onerror(new Error(ERR_TIMED_OUT));
}.bind(this);
this.popup = new Popup(url, name, params);
this.channel = new Channel(this.popup.window, origin, ready);
this.timeout = setTimeout(timedout, 5000);
this.popup.onclose = closed;
this.ready = false;
this.onready = null;
this.onerror = null;
}
function PopupManager(url, origin, name, onparams) {
var cc = null;
var closed = function () {
cc.channel.respond(new Error(ERR_WINDOW_CLOSED));
cc.channel.close();
cc = null;
};
var open = function (callback) {
cc = new ConnectedChannel(url, origin, name, onparams());
cc.onready = function () {
cc.popup.onclose = closed;
callback(cc.channel);
};
cc.onerror = function (error) {
cc = null;
callback(error);
};
};
this.close = function () {
if (cc) {
cc.popup.window.close();
}
};
this.waitForChannel = function (callback) {
if (cc) {
if (cc.ready) {
callback(cc.channel);
} else {
callback(new Error(ERR_ALREADY_WAITING));
}
} else {
open(callback);
}
};
this.sendWithChannel = function (message, callback) {
var onresponse = function (response) {
if (response instanceof Error) {
callback({success: false, error: response.message});
} else {
callback(response);
}
};
var onchannel = function (channel) {
if (channel instanceof Error) {
callback({success: false, error: channel.message});
} else {
channel.send(message, onresponse);
}
}
this.waitForChannel(onchannel);
};
}
}());