Feat/coinbase integration (#4012)

* Oauth2 and first view

* Connect with Coinbase using mobile

* Buy and Sell through Coinbase

* Fix buy

* Receive and send bitcoin to Coinbase account

* Receive bitcoin from Coinbase to Copay

* Complete user and account information. Connection errors

* Improves error handler

* Removes console.log

* Coinbase background color. Send to Coinbase form validation

* Fix send from different wallet

* Send and receive using Coinbase

* Pagination activity

* Fix Buy and Sell

* One option in the sidebar to Buy and Sell

* Native balance on Coinbase homepage

* Rename receive and send

* Auto-close window after authenticate

* Reorder

* Get payment methods

* Fix when token expired

* Fix token expired

* Integration: sell and send to Coinbase

* Store pending transaction before sell

* Sell flow completed

* Removing files

* Fix sell

* Fix sell

* Fix sell

* Sell completed

* Buy bitcoin through coinbase

* Buy auto

* Currency set to USD

* Select payment methods. Limits

* Removes payment methods from preferences

* Fix signs. Tx ordered by updated. Minor fixes

* Removes console.log

* Improving ux-language things

* Fix selectedpaymentmethod if not verified

* Set error if tx not found

* Price sensitivity. Minor fixes

* Adds coinbase api key to gitignore

* Coinbase production ready

* Fix sell in usd

* Bug fixes

* New Sensitivity step

* Refresh token with a simple click

* Refresh token

* Refactor

* Fix auto reconnect if token expired

Signed-off-by: Gustavo Maximiliano Cortez <cmgustavo83@gmail.com>

* Fix calls if token expired
This commit is contained in:
Gustavo Maximiliano Cortez 2016-04-13 14:08:03 -03:00 committed by Matias Alejo Garcia
parent b011df787c
commit d0dbd85711
39 changed files with 2365 additions and 55 deletions

4
.gitignore vendored
View File

@ -3,6 +3,10 @@ i18n/po/*.mo
i18n/crowdin_api_key.txt
src/js/translations.js
# Coinbase API ClientID/Secret
coinbase.json
src/js/coinbase.js
# version
src/js/version.js

View File

@ -7,6 +7,9 @@ module.exports = function(grunt) {
version: {
command: 'node ./util/version.js'
},
coinbase: {
command: 'node ./util/coinbase.js'
},
clear: {
command: 'rm -Rf bower_components node_modules'
},
@ -75,6 +78,7 @@ module.exports = function(grunt) {
'src/js/controllers/*.js',
'src/js/translations.js',
'src/js/version.js',
'src/js/coinbase.js',
'src/js/init.js',
'src/js/trezor-url.js',
'bower_components/trezor-connect/login.js'
@ -209,7 +213,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-node-webkit-builder');
grunt.loadNpmTasks('grunt-contrib-compress');
grunt.registerTask('default', ['nggettext_compile', 'exec:version', 'browserify', 'concat', 'copy:icons']);
grunt.registerTask('default', ['nggettext_compile', 'exec:version', 'exec:coinbase', 'browserify', 'concat', 'copy:icons']);
grunt.registerTask('prod', ['default', 'uglify']);
grunt.registerTask('translate', ['nggettext_extract']);
grunt.registerTask('test', ['karma:unit']);

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

View File

@ -0,0 +1,20 @@
<div
class="topbar-container"
ng-include="'views/includes/topbar.html'"
ng-init="titleSection='Buy & Sell Bitcoin'; closeToHome = true; noColor = true">
</div>
<div class="content">
<ul class="no-bullet manage text-center">
<li class="white m20t" ng-show="index.glideraEnabled">
<a ng-click="$root.go('glidera')">
<img src="img/glidera-logo.png" width="150">
</a>
</li>
<li class="white m20t" ng-show="index.coinbaseEnabled">
<a ng-click="$root.go('coinbase')">
<img src="img/coinbase-logo.png" width="150">
</a>
</li>
</ul>
</div>

View File

@ -0,0 +1,183 @@
<div
class="topbar-container"
ng-include="'views/includes/topbar.html'"
ng-init="titleSection='Buy'; goBackToState = 'coinbase'; noColor = true">
</div>
<div class="content coinbase" ng-controller="buyCoinbaseController as buy">
<div class="onGoingProcess" ng-show="buy.loading">
<div class="onGoingProcess-content" ng-style="{'background-color': '#2b71b1'}">
<div class="spinner">
<div class="rect1"></div>
<div class="rect2"></div>
<div class="rect3"></div>
<div class="rect4"></div>
<div class="rect5"></div>
</div>
<span>{{buy.loading}}</span>
</div>
</div>
<div class="row m20t" ng-show="buy.error || index.coinbaseError" ng-click="buy.error = null">
<div class="columns">
<div class="box-notification">
<ul class="no-bullet m0 size-12 text-warning">
<li ng-repeat="err in (buy.error.errors || index.coinbaseError.errors)" ng-bind-html="err.message"></li>
</ul>
</div>
</div>
</div>
<div class="row m20t"
ng-show="index.coinbaseAccount && !buy.buyInfo && !buy.receiveInfo">
<div class="columns">
<form name="buyCoinbaseForm"
ng-submit="buy.buyRequest(index.coinbaseToken, index.coinbaseAccount)" novalidate>
<div ng-if="index.coinbaseToken" ng-init="buy.getPaymentMethods(index.coinbaseToken)">
<label>Payment method</label>
<select
ng-model="selectedPaymentMethod.id"
ng-options="item.id as item.name for item in buy.paymentMethods">
</select>
</div>
<label>Amount
<span
ng-if="index.coinbaseToken"
ng-init="buy.getPrice(index.coinbaseToken)"
ng-show="buy.buyPrice"
class="size-11 text-light right">
1 BTC <i class="icon-arrow-right"></i> {{buy.buyPrice.amount}} {{buy.buyPrice.currency}}
</span>
</label>
<div class="input">
<input ng-show="!showAlternative" type="number" id="amount"
name="amount" ng-attr-placeholder="{{'Amount in ' + (showAlternative ? 'USD' : 'BTC')}}"
ng-minlength="0.00000001" ng-maxlength="10000000000"
ng-model="amount" autocomplete="off" ng-disabled="buy.loading">
<input ng-show="showAlternative" type="number" id="fiat"
name="fiat" ng-attr-placeholder="{{'Amount in ' + (showAlternative ? 'USD' : 'BTC')}}"
ng-model="fiat" autocomplete="off" ng-disabled="buy.loading">
<a ng-show="!showAlternative" class="postfix button"
ng-click="showAlternative = true; amount = null">BTC</a>
<a ng-show="showAlternative" class="postfix button black"
ng-click="showAlternative = false; fiat = null">USD</a>
</div>
<div class="text-center text-gray size-12 m10b">
<span ng-show="!(amount || fiat)">
Enter the amount to get the exchange rate
</span>
<span ng-show="!buy.buyPrice && (amount || fiat)">
Not available
</span>
<span ng-show="buy.buyPrice && amount && !fiat">
~ {{buy.buyPrice.amount * amount | currency : 'USD ' : 2}}
</span>
</div>
<div class="text-center">
<i class="db fi-arrow-down size-24 m10v"></i>
</div>
<div
ng-if="index.coinbaseToken"
ng-init="buy.init(index.coinbaseTestnet)"
ng-click="openWalletsModal(buy.otherWallets)">
<label>Copay Wallet</label>
<div class="input">
<input type="text" id="address" name="address" ng-disabled="buy.selectedWalletId"
ng-attr-placeholder="{{'Choose a wallet to receive bitcoin'}}" ng-model="buy.selectedWalletName" required>
<a class="postfix size-12 m0 text-gray">
<i class="icon-wallet size-18"></i>
</a>
</div>
</div>
<div class="input m20t">
<input class="button black expand round"
ng-disabled="buy.loading || (!amount && !fiat) || !selectedPaymentMethod"
ng-style="{'background-color': '#2b71b1'}"
type="submit" value="{{'Continue'}}">
</div>
</form>
</div>
</div>
<div class="m20t row" ng-show="buy.receiveInfo && !buy.sellInfo && !buy.success">
<div class="columns">
<h1>Funds sent to Copay Wallet</h1>
<p class="size-12 text-gray">
Buy confirmed. Funds will be send soon to your selected Copay Wallet
</p>
<button class="m20t outline black round expand"
ng-style="{'background-color': '#2b71b1'}"
ng-click="$root.go('coinbase')">OK</button>
</div>
</div>
<div ng-show="buy.buyInfo && !buy.receiveInfo && !buy.success">
<h4 class="title">Confirm transaction</h4>
<ul class="no-bullet m10t size-12 white">
<li class="line-b line-t p15">
<span class="m10 text-normal text-bold">Amount</span>
<span class="right text-gray">{{buy.buyInfo.amount.amount}} {{buy.buyInfo.amount.currency}}</span>
</li>
<li class="line-b oh p15">
<span class="m10 text-normal text-bold">Fees</span>
<span class="right text-gray">
<div ng-repeat="fee in buy.buyInfo.fees">
<b>{{fee.type}}</b> {{fee.amount.amount}} {{fee.amount.currency}}
</div>
</span>
</li>
<li class="line-b p15">
<span class="m10 text-normal text-bold">Subtotal</span>
<span class="right text-gray">{{buy.buyInfo.subtotal.amount}} {{buy.buyInfo.subtotal.currency}}</span>
</li>
<li class="line-b p15">
<span class="m10 text-normal text-bold">Total</span>
<span class="right text-gray">{{buy.buyInfo.total.amount}} {{buy.buyInfo.total.currency}}</span>
</li>
<li class="line-b p15">
<span class="m10 text-normal text-bold">Payout at</span>
<span class="right text-gray">{{buy.buyInfo.payout_at | amCalendar}}</span>
</li>
<li class="line-b p15">
<span class="m10 text-normal text-bold">Deposit into Copay Wallet</span>
<span class="right text-gray">{{buy.selectedWalletName}}</span>
</li>
</ul>
<div class="row">
<div class="columns">
<button class="button black round expand"
ng-style="{'background-color': '#2b71b1'}"
ng-click="buy.confirmBuy(index.coinbaseToken, index.coinbaseAccount, buy.buyInfo)"
ng-disabled="buy.loading">
Buy
</button>
</div>
</div>
</div>
<div class="m20t row text-center" ng-show="buy.success">
<div class="columns">
<h1>Purchase initiated</h1>
<p class="text-gray">
Bitcoin purchase completed. Coinbase has queued the transfer to your selected Copay wallet.
</p>
<button class="outline dark-gray round expand"
ng-click="$root.go('coinbase')">OK</button>
</div>
</div>
</div>
<div class="extra-margin-bottom"></div>

186
public/views/coinbase.html Normal file
View File

@ -0,0 +1,186 @@
<div class="topbar-container">
<nav ng-controller="topbarController as topbar"
class="tab-bar"
ng-style="{'background-color': '#2b71b1'}">
<section class="left-small">
<a class="p10"
fast-click callback-fn="topbar.goHome()">
<span class="text-close">{{'Close'|translate}}</span>
</a>
</section>
<section class="right-small">
<a class="p10" fast-click callback-fn="$root.go('preferencesCoinbase')">
<i class="fi-widget size-24"></i>
</a>
</section>
<section class="middle tab-bar-section">
<h1 class="title ellipsis">
Buy & Sell Bitcoin
</h1>
</section>
</nav>
</div>
<div class="content coinbase p20b" ng-controller="coinbaseController as coinbase">
<div class="onGoingProcess" ng-show="coinbase.loading || index.coinbaseLoading">
<div class="onGoingProcess-content" ng-style="{'background-color': '#2b71b1'}">
<div class="spinner">
<div class="rect1"></div>
<div class="rect2"></div>
<div class="rect3"></div>
<div class="rect4"></div>
<div class="rect5"></div>
</div>
<span ng-show="coinbase.loading">Connecting to Coinbase...</span>
<span ng-show="index.coinbaseLoading">{{index.coinbaseLoading}}</span>
</div>
</div>
<div class="row" ng-show="index.coinbaseError || (index.coinbaseToken && !index.coinbaseAccount)">
<div class="m20b box-notification" ng-show="index.coinbaseError">
<ul class="no-bullet m0 text-warning size-12">
<li ng-repeat="err in index.coinbaseError.errors" ng-bind-html="err.message"></li>
</ul>
</div>
<div class="m20b box-notification" ng-show="index.coinbaseToken && !index.coinbaseAccount">
<div class="text-warning">
<span>Your primary account should be a WALLET. Set your wallet account as primary and try again.</span>
</div>
</div>
<div class="m10t text-center">
<button
class="dark-gray outline round tiny"
ng-click="index.initCoinbase(index.coinbaseToken)">
Reconnect
</button>
<div class="m20t size-12">
Or go to <a class="text-gray" ng-click="$root.go('preferencesCoinbase')">Preferences</a> and log out manually.
</div>
</div>
</div>
<div ng-if="!index.coinbaseToken && !index.coinbaseLoading && !index.coinbaseError" class="row">
<div class="box-notification text-center size-12 text-warning" ng-show="index.coinbaseTestnet">
<i class="fi-info"></i>
Testnet wallets only work with Coinbase Sandbox Accounts
</div>
<div class="columns" ng-init="showOauthForm = false">
<div class="text-center m20v">
<img src="img/coinbase-logo.png" width="200">
</div>
<div class="text-center small-10 small-centered columns" ng-show="!showOauthForm">
<p class="m20t text-gray size-12">Connect your Coinbase account to get started</p>
<a class="button light-gray outline round small"
ng-click="coinbase.openAuthenticateWindow(); showOauthForm = true">
Connect to Coinbase
</a>
<div>
<a href ng-click="showOauthForm = true" class="text-gray size-12">
Do you already have the Oauth Code?
</a>
</div>
</div>
<div class="text-center" ng-show="showOauthForm">
<div class="text-left box-notification" ng-show="coinbase.error">
<ul class="no-bullet m0 text-warning size-12">
<li ng-repeat="err in coinbase.error.errors" ng-bind-html="err.message"></li>
</ul>
</div>
<form name="oauthCodeForm" ng-submit="coinbase.submitOauthCode(code)" novalidate>
<label>OAuth Code</label>
<input type="text" ng-model="code" ng-disabled="coinbase.loading"
ng-attr-placeholder="{{'Paste the authorization code here'}}" required>
<input
class="button expand round"
ng-style="{'background-color': '#2b71b1'}"
type="submit" value="Get started" ng-disabled="oauthCodeForm.$invalid || coinbase.loading">
</form>
<button class="button light-gray expand outline round"
ng-click="showOauthForm = false; index.coinbaseError = null; coinbase.error = null">
<i class="fi-arrow-left"></i> <span class="tu">Back</span>
</button>
</div>
</div>
</div>
<div ng-if="index.coinbaseToken && index.coinbaseAccount && !index.coinbaseError">
<div class="p20v text-center" ng-show="index.coinbaseAccount" ng-click="index.updateCoinbase({updateAccount: true})">
<img src="img/coinbase-logo.png" width="100">
</div>
<ul ng-show="index.coinbaseAccount" class="no-bullet m0 size-12">
<li class="line-b line-t p15 pointer"
ng-click="$root.go('buyCoinbase')">
<img src="img/buy-bitcoin.svg" alt="buy bitcoin" width="30">
<span class="m10 text-normal text-bold">Buy Bitcoin</span>
<span class="right text-gray">
<i class="icon-arrow-right3 size-24 right"></i>
</span>
</li>
<li class="line-b p15 pointer"
ng-click="$root.go('sellCoinbase')">
<img src="img/sell-bitcoin.svg" alt="sell bitcoin" width="30">
<span class="m10 text-normal text-bold">Sell Bitcoin</span>
<span class="right text-gray">
<i class="icon-arrow-right3 size-24 right"></i>
</span>
</li>
</ul>
<div ng-show="index.coinbasePendingTransactions && !index.coinbaseError">
<h4 class="title">Activity</h4>
<div class="m20b box-notification" ng-show="index.coinbasePendingError">
<ul class="no-bullet m0 text-warning size-12">
<li ng-repeat="err in index.coinbasePendingError.errors" ng-bind-html="err.message"></li>
</ul>
</div>
<div ng-repeat="(id, tx) in index.coinbasePendingTransactions | orderObjectBy:'updated_at':true track by $index"
ng-click="coinbase.openTxModal(tx)"
class="row collapse last-transactions-content">
<div class="large-2 medium-2 small-2 columns">
<img src="img/bought-pending.svg" alt="bought" width="24" ng-show="(tx.type == 'buy' || (tx.to && tx.type == 'send')) && tx.status != 'completed'">
<img src="img/bought.svg" alt="bought" width="30" ng-show="(tx.type == 'buy' || (tx.to && tx.type == 'send')) && tx.status == 'completed'">
<img src="img/sold-pending.svg" alt="sold" width="24" ng-show="tx.from && tx.type == 'send'">
<img src="img/sold.svg" alt="sold" width="30" ng-show="!tx.from && tx.type == 'sell' && tx.status == 'completed'">
</div>
<div class="large-5 medium-5 small-5 columns">
<div class="size-12 m5t">
<span ng-show="tx.type == 'sell' && tx.status == 'completed'">Sold</span>
<span ng-show="tx.type == 'buy' && tx.status == 'completed'">Bought</span>
<span class="text-bold">
<span ng-if="tx.type == 'sell' || (tx.type == 'send' && tx.from)">-</span>{{tx.amount.amount.replace('-','')}}
{{tx.amount.currency}}
</span>
</div>
</div>
<div class="large-4 medium-4 small-4 columns text-right">
<div ng-show="tx.error" class="m5t size-12 text-warning">
Error
</div>
<div ng-show="!tx.error" class="m5t size-12 text-gray">
<div ng-show="tx.status == 'completed'">
<time ng-if="tx.created_at">{{tx.created_at | amTimeAgo}}</time>
</div>
<div ng-show="tx.status == 'pending'">
<span class="label outline gray radius text-gray text-info" ng-if="tx.status == 'pending'">Pending</span>
</div>
</div>
</div>
<div class="large-1 medium-1 small-1 columns text-right">
<i class="icon-arrow-right3 size-18"></i>
</div>
</div>
</div>
</div>
<div class="extra-margin-bottom"></div>
</div>

View File

@ -0,0 +1,35 @@
<div
class="topbar-container"
ng-include="'views/includes/topbar.html'"
ng-init="titleSection='Coinbase'; closeToHome = true">
</div>
<div class="content coinbase" ng-controller="coinbaseUriController as coinbase" ng-init="coinbase.checkCode()">
<div class="onGoingProcess" ng-show="coinbase.loading">
<div class="onGoingProcess-content" ng-style="{'background-color':index.backgroundColor}">
<div class="spinner">
<div class="rect1"></div>
<div class="rect2"></div>
<div class="rect3"></div>
<div class="rect4"></div>
<div class="rect5"></div>
</div>
<span ng-show="coinbase.loading">Connecting to Coinbase...</span>
</div>
</div>
<div class="row m20t">
<div class="large-12 columns">
<div class="text-center">
<img src="img/coinbase-logo.png"
ng-click="index.updateCoinbase()" width="100">
</div>
<div class="m10t text-center" ng-show="coinbase.error">
<div class="notification m10b size-12 text-warning">{{coinbase.error}}</div>
<button class="outline dark-gray tiny round" ng-click="coinbase.submitOauthCode(coinbase.code)">Try again</button>
</div>
</div>
</div>
</div>

View File

@ -3,7 +3,7 @@
<div class="confirmTxModal" ng-controller="confirmTxController as confirm" ng-init="tx = index.confirmTx.txp">
<div class="confirmHead" ng-style="{'background-color':index.backgroundColor}">
<h1 class="m0 text-center text-white size-18" translate>Confirm transaction</h1>
<h1 class="m0 text-center text-white size-18" translate>Send bitcoin</h1>
</div>
<div class="p10">
<div class="size-36">{{tx.amountStr}}</div>

View File

@ -28,14 +28,13 @@
<div translate>Create, join or import</div>
</a>
</li>
<li ng-show="index.glideraEnabled && index.isComplete">
<a ng-click="sidebar.closeMenu(); $root.go('glidera')" class="oh">
<li ng-show="index.isComplete && (index.glideraEnabled || index.coinbaseEnabled)">
<a ng-click="sidebar.closeMenu(); $root.go('buyandsell')" class="oh">
<i class="icon-arrow-right3 size-18 right m10t vm"></i>
<i class="icon-bank size-24 icon vm"></i>
<div class="tu text-bold">
<div class="tu text-bold m5t">
<span class="size-12" translate>Buy &amp; Sell Bitcoin</span>
</div>
<div>Glidera</div>
</a>
</li>
<li>

View File

@ -0,0 +1,16 @@
<div class="m20tp text-center">
<div class="row">
<h1 class="text-center m20b p20h">Are you sure you would like to log out of your Coinbase account?</h1>
<p class="text-gray p20h">You will need to log back in to buy or sell bitcoin in Copay.</p>
<div class="large-6 medium-6 small-6 columns">
<button class="button light-gray expand outline round" ng-click="cancel()">
<i class="fi-arrow-left"></i> <span class="tu">Back</span>
</button>
</div>
<div class="large-6 medium-6 small-6 columns">
<button class="button warning expand round" ng-click="ok()">
<span>Log out</span>
</button>
</div>
</div>
</div>

View File

@ -0,0 +1,107 @@
<nav class="tab-bar">
<section class="left-small">
<a ng-click="cancel()">
<i class="icon-arrow-left3 icon-back"></i>
<span class="text-back">Back</span>
</a>
</section>
<section class="middle tab-bar-section">
<h1 class="title ellipsis">
Details
</h1>
</section>
</nav>
<div class="modal-content fix-modals-touch"
ng-swipe-disable-mouse
ng-swipe-right="cancel()">
<div class="header-modal bg-gray text-center">
<div class="p20">
<img src="img/bought.svg" alt="bought" width="65" ng-show="(tx.type == 'buy' || (tx.type == 'send' && tx.to)) && tx.status == 'completed'">
<img src="img/bought-pending.svg" alt="bought" width="65"
ng-show="(tx.type == 'buy' || (tx.type == 'send' && tx.to)) && tx.status != 'completed'">
<img src="img/sold.svg" alt="bought" width="65" ng-show="tx.type == 'sell' && tx.status == 'completed'">
<img src="img/sold-pending.svg" alt="bought" width="65"
ng-show="(tx.type == 'sell' || (tx.type == 'send' && tx.from)) && tx.status != 'completed'">
</div>
<div ng-show="tx.status == 'completed'">
<span ng-show="tx.type == 'buy' || tx.type == 'send'">Bought</span>
<span ng-show="tx.type == 'sell'">Sold</span>
</div>
<div ng-show="tx.type == 'send' && (tx.to || tx.from) && tx.status != 'completed'">
<span ng-show="tx.to">Receiving purchased bitcoin</span>
<span ng-show="tx.from">Sending bitcoin to sell</span>
</div>
<div ng-show="(tx.type == 'sell' || tx.type == 'buy') && tx.status != 'completed'">
<span ng-show="tx.type == 'buy'">Buying bitcoin</span>
<span ng-show="tx.type == 'sell'">Selling bitcoin</span>
</div>
<div class="size-24 text-bold">
<span ng-if="tx.type == 'sell' || (tx.type == 'send' && tx.from)">-</span>{{tx.amount.amount.replace('-','')}}
{{tx.amount.currency}}
</div>
<div class="label gray radius m10b">
<span ng-if="tx.type == 'sell' || (tx.type == 'send' && tx.from)">-</span>{{tx.native_amount.amount.replace('-','')}}
{{tx.native_amount.currency}}
</div>
</div>
<div class="m20b box-notification" ng-show="tx.error">
<ul class="no-bullet m0 text-warning size-12">
<li ng-repeat="err in tx.error.errors" ng-bind-html="err.message"></li>
</ul>
</div>
<ul class="no-bullet size-14">
<li ng-show="tx.details && tx.status != 'pending'" class="line-b p10 oh">
<span class="text-gray">{{tx.details.title}}</span>
<span class="right">{{tx.details.subtitle}}</span>
</li>
<li class="line-b p10 oh">
<span class="text-gray">Status</span>
<span class="text-success right" ng-if="tx.status == 'completed'">Completed</span>
<span class="text-info right" ng-if="tx.status == 'pending'">Pending</span>
<span class="text-warning right" ng-if="tx.status == 'error'">Error</span>
</li>
<li ng-show="tx.created_at" class="line-b p10 oh">
<span class="text-gray">Date</span>
<span class="right">{{tx.created_at | amCalendar}}</span>
</li>
<li ng-show="tx.price_sensitivity" class="line-b p10 oh">
<span class="text-gray">Price Sensitivity</span>
<span class="right">{{tx.price_sensitivity.name}}</span>
</li>
<li ng-show="tx.sell_price_amount" class="line-b p10 oh">
<span class="text-gray">Sell Price</span>
<span class="right">{{tx.sell_price_amount}} {{tx.sell_price_currency}}</span>
</li>
<li ng-show="tx.description" class="line-b p10 oh">
<span class="text-gray" ng-show="tx.from && tx.type == 'send'">Sent bitcoin from</span>
<span class="text-gray" ng-show="tx.to && tx.type == 'send'">Receive bitcoin in</span>
<span class="right text-bold">{{tx.description}}</span>
</li>
</ul>
<div class="row m20t p20t line-t" ng-show="tx.status == 'error'">
<div class="columns">
<p class="text-center size-12 text-gray">
This action will remove the transaction.
</p>
<button class="button outline round dark-gray expand tiny"
ng-click="remove(tx);"
ng-disabled="loading">
<i class="fi-x"></i>
Remove
</button>
</div>
</div>
<div class="extra-margin-bottom"></div>
</div>

View File

@ -7,8 +7,8 @@
<section class="middle tab-bar-section">
<h1 class="title ellipsis">
<span ng-show="type == 'BUY'">Choose your destination wallet</span>
<span ng-show="type == 'SELL'">Choose your source wallet</span>
<span ng-show="type == 'BUY' || type == 'RECEIVE'">Choose your destination wallet</span>
<span ng-show="type == 'SELL' || type == 'SEND'">Choose your source wallet</span>
</h1>
</section>
</nav>

View File

@ -0,0 +1,60 @@
<div
class="topbar-container"
ng-include="'views/includes/topbar.html'"
ng-init="titleSection='Preferences'; goBackToState = 'coinbase'; noColor = true">
</div>
<div class="content preferences" ng-controller="preferencesCoinbaseController as coinbase">
<ul ng-if="index.coinbaseAccount && !index.coinbaseError" class="no-bullet m0">
<h4 class="title m0">Account</h4>
<li>
<span>ID</span>
<span class="right text-gray enable_text_select">
{{index.coinbaseAccount.id}}
</span>
</li>
<li>
<span>Name</span>
<span class="right text-gray">
{{index.coinbaseAccount.name}}
</span>
</li>
<li>
<span>Balance</span>
<span class="right text-gray">
{{index.coinbaseAccount.balance.amount}} {{index.coinbaseAccount.balance.currency}}
</span>
</li>
<li>
<span>Native Balance</span>
<span class="right text-gray">
{{index.coinbaseAccount.native_balance.amount}} {{index.coinbaseAccount.native_balance.currency}}
</span>
</li>
<h4 class="title m0">User Information</h4>
<li>
<span>ID</span>
<span class="right text-gray enable_text_select">
{{index.coinbaseUser.id}}
</span>
</li>
<li>
<span>Email</span>
<span class="right text-gray">
{{index.coinbaseUser.email}}
</span>
</li>
</ul>
<ul class="no-bullet m0">
<h4></h4>
<li ng-click="coinbase.revokeToken(index.coinbaseTestnet)">
<i class="icon-arrow-right3 size-24 right text-gray"></i>
<span class="text-warning">Log out</span>
</li>
</ul>
<h4></h4>
</div>
<div class="extra-margin-bottom"></div>

View File

@ -63,13 +63,13 @@
<switch id="glidera-enabled" name="glideraEnabled" ng-model="glideraEnabled" class="green right"></switch>
<div>Enable Glidera Service</div>
</li>
<!-- Disabled for testnet
<li ng-show="glideraEnabled">
<span>Glidera Sandbox</span>
<switch id="glidera-testnet" name="glideraTestnet" ng-model="glideraTestnet" class="green right"></switch>
</ul>
<h4></h4>
<ul class="no-bullet m0">
<li>
<switch id="coinbase-enabled" name="coinbaseEnabled" ng-model="coinbaseEnabled" class="green right"></switch>
<div>Enable Coinbase Service</div>
</li>
-->
</ul>
<h4></h4>
<ul class="no-bullet m0">

View File

@ -0,0 +1,215 @@
<div
class="topbar-container"
ng-include="'views/includes/topbar.html'"
ng-init="titleSection='Sell'; goBackToState = 'coinbase'; noColor = true">
</div>
<div class="content coinbase" ng-controller="sellCoinbaseController as sell">
<div class="onGoingProcess" ng-show="sell.loading">
<div class="onGoingProcess-content" ng-style="{'background-color': '#2b71b1'}">
<div class="spinner">
<div class="rect1"></div>
<div class="rect2"></div>
<div class="rect3"></div>
<div class="rect4"></div>
<div class="rect5"></div>
</div>
<span>{{sell.loading}}</span>
</div>
</div>
<div class="row m20t" ng-show="sell.error || index.coinbaseError" ng-click="sell.error = null">
<div class="columns">
<div class="box-notification">
<ul class="no-bullet m0 size-12 text-warning">
<li ng-repeat="err in (sell.error.errors || index.coinbaseError.errors)" ng-bind-html="err.message"></li>
</ul>
</div>
</div>
</div>
<div class="row m20t"
ng-show="index.coinbaseAccount && !sell.sellInfo && !sell.sendInfo">
<div class="columns">
<form
name="sellCoinbaseForm"
ng-submit="sell.depositFunds(index.coinbaseToken, index.coinbaseAccount)"
novalidate>
<div ng-show="!showPriceSensitivity">
<div
ng-if="index.coinbaseToken"
ng-init="sell.init(index.coinbaseTestnet)"
ng-click="openWalletsModal(sell.otherWallets)">
<label>Copay Wallet</label>
<div class="input">
<input type="text" id="address" name="address" ng-disabled="sell.selectedWalletId"
ng-attr-placeholder="{{'Choose your source wallet'}}"
ng-model="sell.selectedWalletName" required>
<a class="postfix size-12 m0 text-gray">
<i class="icon-wallet size-18"></i>
</a>
</div>
</div>
<label>
Amount
<span
ng-if="index.coinbaseToken"
ng-init="sell.getPrice(index.coinbaseToken)"
ng-show="sell.sellPrice"
class="size-11 text-light right">
1 BTC <i class="icon-arrow-right"></i> {{sell.sellPrice.amount}} {{sell.sellPrice.currency}}
</span>
</label>
<div class="input">
<input ng-show="!showAlternative" type="number" id="amount"
name="amount" ng-attr-placeholder="{{'Amount in ' + (showAlternative ? 'USD' : 'BTC')}}"
ng-minlength="0.00000001" ng-maxlength="10000000000"
ng-model="amount" autocomplete="off" ng-disabled="sell.loading">
<input ng-show="showAlternative" type="number" id="fiat"
name="fiat" ng-attr-placeholder="{{'Amount in ' + (showAlternative ? 'USD' : 'BTC')}}"
ng-model="fiat" autocomplete="off" ng-disabled="sell.loading">
<a ng-show="!showAlternative" class="postfix button"
ng-click="showAlternative = true; amount = null">BTC</a>
<a ng-show="showAlternative" class="postfix button black"
ng-click="showAlternative = false; fiat = null">USD</a>
</div>
<div class="text-center text-gray size-12 m10b">
<span ng-show="!(amount || fiat)">
Enter the amount to get the exchange rate
</span>
<span ng-show="!sell.sellPrice && (amount || fiat)">
Not available
</span>
<span ng-show="sell.sellPrice && amount && !fiat">
~ {{sell.sellPrice.amount * amount | currency : 'USD ' : 2}}
</span>
</div>
<div class="text-center">
<i class="db fi-arrow-down size-24 m10v"></i>
</div>
<div ng-if="index.coinbaseToken" ng-init="sell.getPaymentMethods(index.coinbaseToken)">
<label>Deposit into</label>
<select
ng-model="selectedPaymentMethod.id"
ng-options="item.id as item.name for item in sell.paymentMethods">
</select>
</div>
<div class="input m20t">
<a href class="button black expand round"
ng-disabled="sell.loading || (!amount && !fiat)"
ng-style="{'background-color': '#2b71b1'}"
ng-click="showPriceSensitivity = true">Continue</a>
</div>
</div>
<div ng-show="showPriceSensitivity">
<h1>Price Sensitivity</h1>
<p class="size-14 text-gray">
Coinbase has not yet implemented an immediate method to sell bitcoin from a wallet. To make this sale, funds
will be sent to your Coinbase account, and sold when Coinbase accepts the transaction (usually one
hour).
</p>
<label>At what percentage lower price would you accept to sell?</label>
<select
ng-model="selectedPriceSensitivity"
ng-options="item as item.name for item in priceSensitivity track by item.value">
</select>
<p class="size-12 text-gray">
Estimated sale value: {{sell.sellPrice.amount * amount | currency : 'USD ' : 2}} <br>
Still sell if price fall until:
{{(sell.sellPrice.amount - (selectedPriceSensitivity.value / 100) * sell.sellPrice.amount) * amount | currency : 'USD ' : 2}}
</p>
<div class="input m20t row">
<div class="columns large-6 medium-6 small-6">
<a href class="button outline dark-gray expand round" ng-click="showPriceSensitivity = false">Back</a>
</div>
<div class="columns large-6 medium-6 small-6">
<input class="button black expand round"
ng-disabled="sell.loading || (!amount && !fiat)"
ng-style="{'background-color': '#2b71b1'}"
type="submit" value="Confirm">
</div>
</div>
</div>
</form>
</div>
</div>
<div class="m20t row" ng-show="sell.sendInfo && !sell.sellInfo && !sell.success">
<div class="columns">
<h1>Funds sent to Coinbase Account</h1>
<p class="size-12 text-gray">
The transaction is not yet confirmed, and will show as "Processing" in your Activity. The bitcoin sale will be completed automatically once it is confirmed by Coinbase.
</p>
<button class="m20t outline black round expand"
ng-style="{'background-color': '#2b71b1'}"
ng-click="$root.go('coinbase')">OK</button>
</div>
</div>
<div ng-show="sell.sellInfo && !sell.sendInfo && !sell.success">
<h4 class="title">Confirm transaction</h4>
<ul class="no-bullet m10t size-12 white">
<li class="line-b line-t p15">
<span class="m10 text-normal text-bold">Amount</span>
<span class="right text-gray">{{sell.sellInfo.amount.amount}} {{sell.sellInfo.amount.currency}}</span>
</li>
<li class="line-b oh p15">
<span class="m10 text-normal text-bold">Fees</span>
<span class="right text-gray">
<div ng-repeat="fee in sell.sellInfo.fees">
<b>{{fee.type}}</b> {{fee.amount.amount}} {{fee.amount.currency}}
</div>
</span>
</li>
<li class="line-b p15">
<span class="m10 text-normal text-bold">Subtotal</span>
<span class="right text-gray">{{sell.sellInfo.subtotal.amount}} {{sell.sellInfo.subtotal.currency}}</span>
</li>
<li class="line-b p15">
<span class="m10 text-normal text-bold">Total</span>
<span class="right text-gray">{{sell.sellInfo.total.amount}} {{sell.sellInfo.total.currency}}</span>
</li>
<li class="line-b p15">
<span class="m10 text-normal text-bold">Payout at</span>
<span class="right text-gray">{{sell.sellInfo.payout_at | amCalendar}}</span>
</li>
</ul>
<div class="row">
<div class="columns">
<button class="button black round expand"
ng-style="{'background-color': '#2b71b1'}"
ng-click="sell.confirmSell(index.coinbaseToken, index.coinbaseAccount, sell.sellInfo)"
ng-disabled="sell.loading">
Sell
</button>
</div>
</div>
</div>
<div class="m20t row text-center" ng-show="sell.success">
<div class="columns">
<h1>Sale initiated</h1>
<p class="text-gray">
A transfer has been initiated to your bank account and should arrive at {{sell.success.payout_at | amCalendar}}.
</p>
<button class="outline dark-gray round expand"
ng-click="$root.go('coinbase')">OK</button>
</div>
</div>
</div>
<div class="extra-margin-bottom"></div>

View File

@ -73,7 +73,7 @@ h4.title a {
}
.preferences h4, .modal-content h4, .glidera h4, .txModal h4 {
.preferences h4, .modal-content h4, .glidera h4, .coinbase h4, .txModal h4 {
background: #F6F7F9;
padding: 25px 0px 5px 10px;
text-transform: uppercase;
@ -111,6 +111,17 @@ h4.title a {
padding-top: 7px;
}
.disabled-input {
display: block;
margin-bottom: 1.5rem;
background-color: #E4E8EC;
padding-left: 0.5rem;
color: #2C3E50;
font-size: 13px;
height: 35px;
padding-top: 7px;
}
ul.button-group {
margin-top: 8px;
}

View File

@ -74,7 +74,7 @@ angular.module('copayApp.controllers').controller('backupController',
$scope.$apply();
}, 1);
profileService.unlockFC(function(err) {
profileService.unlockFC({}, function(err) {
if (err) {
self.error = bwsError.msg(err, gettext('Could not decrypt'));
$log.warn('Error decrypting credentials:', self.error); //TODO

View File

@ -0,0 +1,206 @@
'use strict';
angular.module('copayApp.controllers').controller('buyCoinbaseController',
function($scope, $modal, $log, $timeout, lodash, profileService, coinbaseService, animationService, txService, bwsError, addressService) {
window.ignoreMobilePause = true;
var self = this;
var fc;
var otherWallets = function(testnet) {
var network = testnet ? 'testnet' : 'livenet';
return lodash.filter(profileService.getWallets(network), function(w) {
return w.network == network && w.m == 1;
});
};
this.init = function(testnet) {
self.otherWallets = otherWallets(testnet);
// Choose focused wallet
try {
var currentWalletId = profileService.focusedClient.credentials.walletId;
lodash.find(self.otherWallets, function(w) {
if (w.id == currentWalletId) {
$timeout(function() {
self.selectedWalletId = w.id;
self.selectedWalletName = w.name;
fc = profileService.getClient(w.id);
$scope.$apply();
}, 100);
}
});
} catch (e) {
$log.debug(e);
};
};
this.getPaymentMethods = function(token) {
coinbaseService.getPaymentMethods(token, function(err, p) {
if (err) {
self.error = err;
return;
}
self.paymentMethods = [];
lodash.each(p.data, function(pm) {
if (pm.allow_buy) {
self.paymentMethods.push(pm);
}
if (pm.allow_buy && pm.primary_buy) {
$scope.selectedPaymentMethod = pm;
}
});
});
};
this.getPrice = function(token) {
var currency = 'USD';
coinbaseService.buyPrice(token, currency, function(err, b) {
if (err) return;
self.buyPrice = b.data || null;
});
};
$scope.openWalletsModal = function(wallets) {
self.error = null;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.type = 'BUY';
$scope.wallets = wallets;
$scope.noColor = true;
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
$scope.selectWallet = function(walletId, walletName) {
if (!profileService.getClient(walletId).isComplete()) {
self.error = bwsError.msg({
'code': 'WALLET_NOT_COMPLETE'
}, 'Could not choose the wallet');
$modalInstance.dismiss('cancel');
return;
}
$modalInstance.close({
'walletId': walletId,
'walletName': walletName,
});
};
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/wallets.html',
windowClass: animationService.modalAnimated.slideUp,
controller: ModalInstanceCtrl,
});
modalInstance.result.finally(function() {
var m = angular.element(document.getElementsByClassName('reveal-modal'));
m.addClass(animationService.modalAnimated.slideOutDown);
});
modalInstance.result.then(function(obj) {
$timeout(function() {
self.selectedWalletId = obj.walletId;
self.selectedWalletName = obj.walletName;
fc = profileService.getClient(obj.walletId);
$scope.$apply();
}, 100);
});
};
this.buyRequest = function(token, account) {
self.error = null;
var accountId = account.id;
var amount = $scope.amount ? $scope.amount : $scope.fiat;
var currency = $scope.amount ? 'BTC' : 'USD';
if (!amount) return;
var dataSrc = {
amount: amount,
currency: currency,
payment_method: $scope.selectedPaymentMethod.id || null
};
this.loading = 'Sending request...';
coinbaseService.buyRequest(token, accountId, dataSrc, function(err, data) {
self.loading = null;
if (err) {
self.error = err;
return;
}
self.buyInfo = data.data;
});
};
this.confirmBuy = function(token, account, buy) {
self.error = null;
var accountId = account.id;
var buyId = buy.id;
this.loading = 'Buying bitcoin...';
coinbaseService.buyCommit(token, accountId, buyId, function(err, b) {
self.loading = null;
if (err) {
self.error = err;
return;
} else {
var tx = b.data.transaction;
if (!tx) return;
self.loading = 'Getting transaction...';
coinbaseService.getTransaction(token, accountId, tx.id, function(err, updatedTx) {
if (err) $log.debug(err);
addressService.getAddress(self.selectedWalletId, false, function(err, addr) {
if (err) {
self.loading = null;
self.error = {errors: [{ message: 'Could not create address' }]};
return;
}
coinbaseService.savePendingTransaction(updatedTx.data, {toAddr: addr}, function(err) {
self.loading = null;
if (err) $log.debug(err);
updatedTx.data['toAddr'] = addr;
if (updatedTx.data.status == 'completed') {
self.sendToCopay(token, account, updatedTx.data);
} else {
self.success = updatedTx.data;
$timeout(function() {
$scope.$emit('Local/CoinbaseTx');
}, 1000);
}
});
});
});
}
});
};
this.sendToCopay = function(token, account, tx) {
self.error = null;
var accountId = account.id;
self.loading = 'Sending funds to Copay...';
var data = {
to: tx.toAddr,
amount: tx.amount.amount,
currency: tx.amount.currency,
description: 'Copay Wallet: ' + self.selectedWalletName
};
coinbaseService.sendTo(token, accountId, data, function(err, res) {
self.loading = null;
if (err) {
self.error = err;
} else {
self.receiveInfo = res.data;
if (!res.data.id) return;
coinbaseService.getTransaction(token, accountId, res.data.id, function(err, sendTx) {
coinbaseService.savePendingTransaction(tx, {remove: true}, function(err) {
coinbaseService.savePendingTransaction(sendTx.data, {}, function(err) {
$timeout(function() {
$scope.$emit('Local/CoinbaseTx');
}, 1000);
});
});
});
}
});
};
});

View File

@ -63,7 +63,7 @@ angular.module('copayApp.controllers').controller('buyGlideraController',
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/glidera-wallets.html',
templateUrl: 'views/modals/wallets.html',
windowClass: animationService.modalAnimated.slideUp,
controller: ModalInstanceCtrl,
});

View File

@ -0,0 +1,103 @@
'use strict';
angular.module('copayApp.controllers').controller('coinbaseController',
function($rootScope, $scope, $timeout, $modal, profileService, configService, storageService, coinbaseService, isChromeApp, animationService, lodash, nodeWebkit) {
this.openAuthenticateWindow = function() {
var oauthUrl = this.getAuthenticateUrl();
if (!nodeWebkit.isDefined()) {
$rootScope.openExternalLink(oauthUrl, '_system');
} else {
var self = this;
var gui = require('nw.gui');
var win = gui.Window.open(oauthUrl, {
focus: true,
position: 'center'
});
win.on ('loaded', function(){
var title = win.title;
if (title.indexOf('Coinbase') == -1) {
$scope.code = title;
self.submitOauthCode(title);
win.close();
}
});
}
}
this.getAuthenticateUrl = function() {
return coinbaseService.getOauthCodeUrl();
};
this.submitOauthCode = function(code) {
var self = this;
var coinbaseTestnet = configService.getSync().coinbase.testnet;
var network = coinbaseTestnet ? 'testnet' : 'livenet';
this.loading = true;
this.error = null;
$timeout(function() {
coinbaseService.getToken(code, function(err, data) {
self.loading = null;
if (err) {
self.error = err;
$timeout(function() {
$scope.$apply();
}, 100);
}
else if (data && data.access_token && data.refresh_token) {
storageService.setCoinbaseToken(network, data.access_token, function() {
storageService.setCoinbaseRefreshToken(network, data.refresh_token, function() {
$scope.$emit('Local/CoinbaseUpdated', data.access_token);
$timeout(function() {
$scope.$apply();
}, 100);
});
});
}
});
}, 100);
};
this.openTxModal = function(tx) {
$rootScope.modalOpened = true;
var self = this;
var config = configService.getSync().wallet.settings;
var fc = profileService.focusedClient;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.tx = tx;
$scope.settings = config;
$scope.color = fc.backgroundColor;
$scope.noColor = true;
$scope.remove = function() {
coinbaseService.savePendingTransaction($scope.tx, {remove: true}, function(err) {
$rootScope.$emit('Local/CoinbaseTx');
$scope.cancel();
});
};
$scope.cancel = lodash.debounce(function() {
$modalInstance.dismiss('cancel');
}, 0, 1000);
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/coinbase-tx-details.html',
windowClass: animationService.modalAnimated.slideRight,
controller: ModalInstanceCtrl,
});
var disableCloseModal = $rootScope.$on('closeModal', function() {
modalInstance.dismiss('cancel');
});
modalInstance.result.finally(function() {
$rootScope.modalOpened = false;
disableCloseModal();
var m = angular.element(document.getElementsByClassName('reveal-modal'));
m.addClass(animationService.modalAnimated.slideOutRight);
});
};
});

View File

@ -0,0 +1,38 @@
'use strict';
angular.module('copayApp.controllers').controller('coinbaseUriController',
function($scope, $stateParams, $timeout, profileService, configService, coinbaseService, storageService, go) {
this.submitOauthCode = function(code) {
var self = this;
var coinbaseTestnet = configService.getSync().coinbase.testnet;
var network = coinbaseTestnet ? 'testnet' : 'livenet';
this.loading = true;
this.error = null;
$timeout(function() {
coinbaseService.getToken(code, function(err, data) {
self.loading = null;
if (err) {
self.error = err;
$timeout(function() {
$scope.$apply();
}, 100);
}
else if (data && data.access_token) {
storageService.setCoinbaseToken(network, data.access_token, function() {
$scope.$emit('Local/CoinbaseUpdated', data.access_token);
$timeout(function() {
go.path('coinbase');
$scope.$apply();
}, 100);
});
}
});
}, 100);
};
this.checkCode = function() {
this.code = $stateParams.code;
this.submitOauthCode(this.code);
};
});

View File

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, latestReleaseService, bwcService, pushNotificationsService, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, nodeWebkit, addonManager, isChromeApp, bwsError, txFormatService, uxLanguage, $state, glideraService, isMobile, addressbookService) {
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, latestReleaseService, bwcService, pushNotificationsService, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, nodeWebkit, addonManager, isChromeApp, bwsError, txFormatService, uxLanguage, $state, glideraService, coinbaseService, isMobile, addressbookService) {
var self = this;
var SOFT_CONFIRMATION_LIMIT = 12;
var errors = bwcService.getErrors();
@ -147,6 +147,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.setAddressbook();
self.initGlidera();
self.initCoinbase();
self.setCustomBWSFlag();
@ -1265,6 +1266,246 @@ angular.module('copayApp.controllers').controller('indexController', function($r
};
self.initCoinbase = function(accessToken) {
self.coinbaseEnabled = configService.getSync().coinbase.enabled;
self.coinbaseTestnet = configService.getSync().coinbase.testnet;
var network = self.coinbaseTestnet ? 'testnet' : 'livenet';
self.coinbaseToken = null;
self.coinbaseError = null;
self.coinbasePermissions = null;
self.coinbaseEmail = null;
self.coinbasePersonalInfo = null;
self.coinbaseTxs = null;
self.coinbaseStatus = null;
if (!self.coinbaseEnabled) return;
coinbaseService.setCredentials(network);
var getToken = function(cb) {
if (accessToken) {
cb(null, accessToken);
} else {
storageService.getCoinbaseToken(network, cb);
}
};
getToken(function(err, accessToken) {
if (err || !accessToken) return;
else {
self.coinbaseLoading = 'Getting primary account...';
coinbaseService.getAccounts(accessToken, function(err, a) {
self.coinbaseLoading = null;
if (err) {
self.coinbaseError = err;
if (err.errors[0] && err.errors[0].id == 'expired_token') {
self.refreshCoinbaseToken();
}
} else {
self.coinbaseToken = accessToken;
lodash.each(a.data, function(account) {
if (account.primary && account.type == 'wallet') {
self.coinbaseAccount = account;
self.updateCoinbase();
}
});
}
});
}
});
};
self.updateCoinbase = lodash.debounce(function(opts) {
if (!self.coinbaseToken || !self.coinbaseAccount) return;
var accessToken = self.coinbaseToken;
var accountId = self.coinbaseAccount.id;
opts = opts || {};
if (opts.updateAccount) {
coinbaseService.getAccount(accessToken, accountId, function(err, a) {
if (err) {
self.coinbaseError = err;
if (err.errors[0] && err.errors[0].id == 'expired_token') {
self.refreshCoinbaseToken();
}
return;
}
self.coinbaseAccount = a.data;
});
}
coinbaseService.getCurrentUser(accessToken, function(err, u) {
if (err) {
self.coinbaseError = err;
if (err.errors[0] && err.errors[0].id == 'expired_token') {
self.refreshCoinbaseToken();
}
return;
}
self.coinbaseUser = u.data;
});
coinbaseService.getPendingTransactions(function(err, txs) {
self.coinbasePendingTransactions = lodash.isEmpty(txs) ? null : txs;
lodash.forEach(txs, function(dataFromStorage, txId) {
if ((dataFromStorage.type == 'sell' && dataFromStorage.status == 'completed') ||
(dataFromStorage.type == 'buy' && dataFromStorage.status == 'completed') ||
dataFromStorage.status == 'error' ||
(dataFromStorage.type == 'send' && dataFromStorage.status == 'completed')) return;
coinbaseService.getTransaction(accessToken, accountId, txId, function(err, tx) {
if (err) {
if (err.errors[0] && err.errors[0].id == 'expired_token') {
self.refreshCoinbaseToken();
return;
}
coinbaseService.savePendingTransaction(dataFromStorage, {status: 'error', error: err}, function(err) {
if (err) $log.debug(err);
});
return;
}
_updateCoinbasePendingTransactions(dataFromStorage, tx.data);
self.coinbasePendingTransactions[txId] = dataFromStorage;
if (tx.data.type == 'send' && tx.data.status == 'completed' && tx.data.from) {
coinbaseService.sellPrice(accessToken, dataFromStorage.sell_price_currency, function(err, s) {
if (err) {
if (err.errors[0] && err.errors[0].id == 'expired_token') {
self.refreshCoinbaseToken();
return;
}
coinbaseService.savePendingTransaction(dataFromStorage, {status: 'error', error: err}, function(err) {
if (err) $log.debug(err);
});
return;
}
var newSellPrice = s.data.amount;
var variance = Math.abs((newSellPrice - dataFromStorage.sell_price_amount) / dataFromStorage.sell_price_amount * 100);
if (variance < dataFromStorage.price_sensitivity.value) {
self.sellPending(tx.data);
} else {
var error = {errors: [{ message: 'Price falls over the selected percentage' }]};
coinbaseService.savePendingTransaction(dataFromStorage, {status: 'error', error: error}, function(err) {
if (err) $log.debug(err);
});
}
});
} else if (tx.data.type == 'buy' && tx.data.status == 'pending' && tx.data.buy) {
self.buyPending(tx.data);
} else {
coinbaseService.savePendingTransaction(dataFromStorage, {}, function(err) {
if (err) $log.debug(err);
});
}
});
});
});
}, 1000);
var _updateCoinbasePendingTransactions = function (obj/*, …*/) {
for (var i=1; i<arguments.length; i++) {
for (var prop in arguments[i]) {
var val = arguments[i][prop];
if (typeof val == "object")
_updateCoinbasePendingTransactions(obj[prop], val);
else
obj[prop] = val ? val : obj[prop];
}
}
return obj;
};
self.refreshCoinbaseToken = function() {
var network = self.coinbaseTestnet ? 'testnet' : 'livenet';
storageService.getCoinbaseRefreshToken(network, function(err, refreshToken) {
coinbaseService.refreshToken(refreshToken, function(err, data) {
if (err) {
self.coinbaseError = err;
} else if (data && data.access_token && data.refresh_token) {
storageService.setCoinbaseToken(network, data.access_token, function() {
storageService.setCoinbaseRefreshToken(network, data.refresh_token, function() {
$timeout(function() {
self.initCoinbase(data.access_token);
}, 100);
});
});
}
});
});
};
self.buyPending = function(tx) {
if (!tx) return;
var data = {
to: tx.toAddr,
amount: tx.amount.amount,
currency: tx.amount.currency,
description: 'To Copay Wallet'
};
coinbaseService.sendTo(self.coinbaseToken, self.coinbaseAccount.id, data, function(err, res) {
if (err) {
if (err.errors[0] && err.errors[0].id == 'expired_token') {
self.refreshCoinbaseToken();
return;
}
coinbaseService.savePendingTransaction(tx, {status: 'error', error: err}, function(err) {
if (err) $log.debug(err);
});
} else {
if (!res.data.id) {
coinbaseService.savePendingTransaction(tx, {status: 'error', error: err}, function(err) {
if (err) $log.debug(err);
});
return;
}
coinbaseService.getTransaction(self.coinbaseToken, self.coinbaseAccount.id, res.data.id, function(err, sendTx) {
coinbaseService.savePendingTransaction(tx, {remove: true}, function(err) {
coinbaseService.savePendingTransaction(sendTx.data, {}, function(err) {
$timeout(function() {
self.updateCoinbase({updateAccount: true});
}, 1000);
});
});
});
}
});
};
self.sellPending = function(tx) {
if (!tx) return;
var data = tx.amount;
data['commit'] = true;
coinbaseService.sellRequest(self.coinbaseToken, self.coinbaseAccount.id, data, function(err, res) {
if (err) {
if (err.errors[0] && err.errors[0].id == 'expired_token') {
self.refreshCoinbaseToken();
return;
}
coinbaseService.savePendingTransaction(tx, {status: 'error', error: err}, function(err) {
if (err) $log.debug(err);
});
} else {
if (!res.data.transaction) {
coinbaseService.savePendingTransaction(tx, {status: 'error', error: err}, function(err) {
if (err) $log.debug(err);
});
return;
}
coinbaseService.savePendingTransaction(tx, {remove: true}, function(err) {
coinbaseService.getTransaction(self.coinbaseToken, self.coinbaseAccount.id, res.data.transaction.id, function(err, updatedTx) {
coinbaseService.savePendingTransaction(updatedTx.data, {}, function(err) {
if (err) $log.debug(err);
$timeout(function() {
self.updateCoinbase({updateAccount: true});
}, 1000);
});
});
});
}
});
};
self.setAddressbook = function(ab) {
if (ab) {
self.addressbook = ab;
@ -1341,10 +1582,20 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.initGlidera(accessToken);
});
$rootScope.$on('Local/CoinbaseUpdated', function(event, accessToken) {
self.initCoinbase(accessToken);
});
$rootScope.$on('Local/GlideraTx', function(event, accessToken, permissions) {
self.updateGlidera();
});
$rootScope.$on('Local/CoinbaseTx', function(event) {
self.updateCoinbase({
updateAccount: true
});
});
$rootScope.$on('Local/GlideraError', function(event) {
self.debouncedUpdate();
});
@ -1460,6 +1711,11 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.updateGlidera();
});
}
if (self.coinbaseEnabled) {
$timeout(function() {
self.updateCoinbase();
});
}
if (self.pendingAmount) {
self.updateAll({
walletStatus: null,

View File

@ -44,7 +44,7 @@ angular.module('copayApp.controllers').controller('preferencesController',
});
} else {
if (!val && fc.hasPrivKeyEncrypted()) {
profileService.unlockFC(function(err) {
profileService.unlockFC({}, function(err) {
if (err) {
$scope.encrypt = true;
return;

View File

@ -0,0 +1,39 @@
'use strict';
angular.module('copayApp.controllers').controller('preferencesCoinbaseController',
function($scope, $modal, $timeout, applicationService, coinbaseService, storageService, animationService) {
this.revokeToken = function(testnet) {
var network = testnet ? 'testnet' : 'livenet';
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.ok = function() {
$modalInstance.close(true);
};
$scope.cancel = function() {
$modalInstance.dismiss();
};
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/coinbase-confirmation.html',
windowClass: animationService.modalAnimated.slideRight,
controller: ModalInstanceCtrl
});
modalInstance.result.then(function(ok) {
if (ok) {
storageService.removeCoinbaseToken(network, function() {
$timeout(function() {
applicationService.restart();
}, 100);
});
}
});
modalInstance.result.finally(function() {
var m = angular.element(document.getElementsByClassName('reveal-modal'));
m.addClass(animationService.modalAnimated.slideOutRight);
});
};
});

View File

@ -23,7 +23,7 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController',
}
$scope.spendUnconfirmed = config.wallet.spendUnconfirmed;
$scope.glideraEnabled = config.glidera.enabled;
$scope.glideraTestnet = config.glidera.testnet;
$scope.coinbaseEnabled = config.coinbase.enabled;
$scope.pushNotifications = config.pushNotifications.enabled;
};
@ -77,15 +77,15 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController',
});
});
var unwatchGlideraTestnet = $scope.$watch('glideraTestnet', function(newVal, oldVal) {
var unwatchCoinbaseEnabled = $scope.$watch('coinbaseEnabled', function(newVal, oldVal) {
if (newVal == oldVal) return;
var opts = {
glidera: {
testnet: newVal
coinbase: {
enabled: newVal
}
};
configService.set(opts, function(err) {
$rootScope.$emit('Local/GlideraUpdated');
$rootScope.$emit('Local/CoinbaseUpdated');
if (err) $log.debug(err);
});
});
@ -93,7 +93,7 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController',
$scope.$on('$destroy', function() {
unwatchSpendUnconfirmed();
unwatchGlideraEnabled();
unwatchGlideraTestnet();
unwatchCoinbaseEnabled();
unwatchPushNotifications();
});
});

View File

@ -0,0 +1,301 @@
'use strict';
angular.module('copayApp.controllers').controller('sellCoinbaseController',
function($scope, $modal, $log, $timeout, lodash, profileService, coinbaseService, animationService, txService, bwsError) {
window.ignoreMobilePause = true;
var self = this;
var fc;
$scope.priceSensitivity = [
{
value : 0.5,
name: '0.5%'
},
{
value : 1,
name: '1%'
},
{
value : 2,
name: '2%'
},
{
value : 5,
name: '5%'
},
{
value : 10,
name: '10%'
}
];
$scope.selectedPriceSensitivity = $scope.priceSensitivity[1];
var otherWallets = function(testnet) {
var network = testnet ? 'testnet' : 'livenet';
return lodash.filter(profileService.getWallets(network), function(w) {
return w.network == network && w.m == 1;
});
};
this.init = function(testnet) {
self.otherWallets = otherWallets(testnet);
// Choose focused wallet
try {
var currentWalletId = profileService.focusedClient.credentials.walletId;
lodash.find(self.otherWallets, function(w) {
if (w.id == currentWalletId) {
$timeout(function() {
self.selectedWalletId = w.id;
self.selectedWalletName = w.name;
fc = profileService.getClient(w.id);
$scope.$apply();
}, 100);
}
});
} catch (e) {
$log.debug(e);
};
};
this.getPaymentMethods = function(token) {
coinbaseService.getPaymentMethods(token, function(err, p) {
if (err) {
self.error = err;
return;
}
self.paymentMethods = [];
lodash.each(p.data, function(pm) {
if (pm.allow_sell) {
self.paymentMethods.push(pm);
}
if (pm.allow_sell && pm.primary_sell) {
$scope.selectedPaymentMethod = pm;
}
});
});
};
this.getPrice = function(token) {
var currency = 'USD';
coinbaseService.sellPrice(token, currency, function(err, s) {
if (err) return;
self.sellPrice = s.data || null;
});
};
$scope.openWalletsModal = function(wallets) {
self.error = null;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.type = 'SELL';
$scope.wallets = wallets;
$scope.noColor = true;
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
$scope.selectWallet = function(walletId, walletName) {
if (!profileService.getClient(walletId).isComplete()) {
self.error = bwsError.msg({
'code': 'WALLET_NOT_COMPLETE'
}, 'Could not choose the wallet');
$modalInstance.dismiss('cancel');
return;
}
$modalInstance.close({
'walletId': walletId,
'walletName': walletName,
});
};
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/wallets.html',
windowClass: animationService.modalAnimated.slideUp,
controller: ModalInstanceCtrl,
});
modalInstance.result.finally(function() {
var m = angular.element(document.getElementsByClassName('reveal-modal'));
m.addClass(animationService.modalAnimated.slideOutDown);
});
modalInstance.result.then(function(obj) {
$timeout(function() {
self.selectedWalletId = obj.walletId;
self.selectedWalletName = obj.walletName;
fc = profileService.getClient(obj.walletId);
$scope.$apply();
}, 100);
});
};
this.depositFunds = function(token, account) {
self.error = null;
if ($scope.amount) {
this.createTx(token, account, $scope.amount)
} else if ($scope.fiat) {
var btcValue = ($scope.fiat / self.sellPrice.amount).toFixed(8);
this.createTx(token, account, btcValue);
}
};
this.sellRequest = function(token, account, ctx) {
self.error = null;
if (!ctx.amount) return;
var accountId = account.id;
var data = ctx.amount;
data['payment_method'] = $scope.selectedPaymentMethod.id || null;
this.loading = 'Sending request...';
coinbaseService.sellRequest(token, accountId, data, function(err, sell) {
self.loading = null;
if (err) {
self.error = err;
return;
}
self.sellInfo = sell.data;
});
};
this.confirmSell = function(token, account, sell) {
self.error = null;
var accountId = account.id;
var sellId = sell.id;
this.loading = 'Selling bitcoin...';
coinbaseService.sellCommit(token, accountId, sellId, function(err, data) {
self.loading = null;
if (err) {
self.error = err;
return;
}
self.success = data.data;
$scope.$emit('Local/CoinbaseTx');
});
};
this.createTx = function(token, account, amount) {
self.error = null;
var accountId = account.id;
var dataSrc = { name : 'Received from Copay: ' + self.selectedWalletName };
var outputs = [];
self.loading = 'Creating transaction...';
$timeout(function() {
coinbaseService.createAddress(token, accountId, dataSrc, function(err, data) {
if (err) {
self.loading = null;
self.error = err;
return;
}
var address, comment;
address = data.data.address;
amount = parseInt((amount * 100000000).toFixed(0));
comment = 'Send funds to Coinbase Account: ' + account.name;
outputs.push({
'toAddress': address,
'amount': amount,
'message': comment
});
var opts = {
selectedClient: fc,
toAddress: address,
amount: amount,
outputs: outputs,
message: comment,
payProUrl: null
};
txService.createTx(opts, function(err, txp) {
if (err) {
$log.debug(err);
self.loading = null;
self.error = {errors: [{ message: 'Could not create transaction: ' + err.message }]};
$scope.$apply();
return;
}
$scope.$emit('Local/NeedsConfirmation', txp, function(accept) {
self.loading = null;
if (accept) {
self.confirmTx(txp, function(err, tx) {
if (err) {
self.error = {errors: [{ message: 'Could not create transaction: ' + err.message }]};
return;
}
self.loading = 'Checking transaction...';
coinbaseService.getTransactions(token, accountId, function(err, ctxs) {
if (err) {
$log.debug(err);
return;
}
lodash.each(ctxs.data, function(ctx) {
if (ctx.type == 'send' && ctx.from) {
if (ctx.status == 'completed') {
self.sellRequest(token, account, ctx);
} else {
// Save to localstorage
self.loading = null;
ctx['price_sensitivity'] = $scope.selectedPriceSensitivity;
ctx['sell_price_amount'] = self.sellPrice.amount;
ctx['sell_price_currency'] = self.sellPrice.currency;
ctx['description'] = 'Copay Wallet: ' + fc.credentials.walletName;
coinbaseService.savePendingTransaction(ctx, null, function(err) {
if (err) $log.debug(err);
self.sendInfo = ctx;
$timeout(function() {
$scope.$emit('Local/CoinbaseTx');
}, 1000);
});
}
return false;
}
});
});
});
}
});
});
});
}, 100);
};
this.confirmTx = function(txp, cb) {
txService.prepare({selectedClient: fc}, function(err) {
if (err) {
$log.debug(err);
return cb(err);
}
self.loading = 'Sending bitcoin to Coinbase...';
txService.publishTx(txp, {selectedClient: fc}, function(err, txpPublished) {
if (err) {
self.loading = null;
$log.debug(err);
return cb({errors: [{ message: 'Transaction could not be published: ' + err.message }]});
} else {
txService.signAndBroadcast(txpPublished, {selectedClient: fc}, function(err, txp) {
if (err) {
self.loading = null;
$log.debug(err);
txService.removeTx(txp, function(err) {
if (err) $log.debug(err);
});
return cb({errors: [{ message: 'The payment was created but could not be completed: ' + err.message }]});
} else {
$timeout(function() {
self.loading = null;
return cb(null, txp);
}, 5000);
}
});
}
});
});
};
});

View File

@ -70,7 +70,7 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/glidera-wallets.html',
templateUrl: 'views/modals/wallets.html',
windowClass: animationService.modalAnimated.slideUp,
controller: ModalInstanceCtrl,
});
@ -129,7 +129,7 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
self.error = null;
txService.prepare(function(err) {
txService.prepare({selectedClient: fc}, function(err) {
if (err) {
self.error = err;
return;
@ -173,7 +173,7 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
return;
}
txService.sign(txp, function(err, txp) {
txService.sign(txp, {selectedClient: fc}, function(err, txp) {
if (err) {
self.loading = null;
self.error = err;

View File

@ -943,12 +943,12 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
this.confirmTx = function(txp) {
var self = this;
txService.prepare(function(err) {
txService.prepare({}, function(err) {
if (err) {
return self.setSendError(err);
}
self.setOngoingProcess(gettextCatalog.getString('Sending transaction'));
txService.publishTx(txp, function(err, txpPublished) {
txService.publishTx(txp, {}, function(err, txpPublished) {
if (err) {
self.setOngoingProcess();
self.setSendError(err);

View File

@ -68,4 +68,17 @@ angular.module('copayApp.filters', [])
return 0;
};
}
]);
])
.filter('orderObjectBy', function() {
return function(items, field, reverse) {
var filtered = [];
angular.forEach(items, function(item) {
filtered.push(item);
});
filtered.sort(function (a, b) {
return (a[field] > b[field] ? 1 : -1);
});
if(reverse) filtered.reverse();
return filtered;
};
});

View File

@ -11,6 +11,8 @@ angular.element(document).ready(function() {
if (!url) return;
if (url.indexOf('glidera') != -1) {
url = '#/uri-glidera' + url.replace('bitcoin://glidera', '');
} else if (url.indexOf('coinbase') != -1) {
url = '#/uri-coinbase' + url.replace('bitcoin://coinbase', '');
} else {
url = '#/uri-payment/' + url;
}
@ -46,7 +48,7 @@ angular.element(document).ready(function() {
}
setTimeout(function() {
var loc = window.location;
var ignoreMobilePause = loc.toString().match(/(buy|sell)/) ? true : false;
var ignoreMobilePause = loc.toString().match(/(buy|sell|buycoinbase|sellcoinbase)/) ? true : false;
window.ignoreMobilePause = ignoreMobilePause;
}, 100);
}, false);

View File

@ -269,6 +269,64 @@ angular
},
}
})
.state('coinbase', {
url: '/coinbase',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/coinbase.html'
},
}
})
.state('preferencesCoinbase', {
url: '/preferencesCoinbase',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/preferencesCoinbase.html'
},
}
})
.state('uricoinbase', {
url: '/uri-coinbase?code',
needProfile: true,
views: {
'main': {
templateUrl: 'views/coinbaseUri.html'
},
}
})
.state('buyCoinbase', {
url: '/buycoinbase',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/buyCoinbase.html'
},
}
})
.state('sellCoinbase', {
url: '/sellcoinbase',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/sellCoinbase.html'
},
}
})
.state('buyandsell', {
url: '/buyandsell',
needProfile: true,
views: {
'main': {
templateUrl: 'views/buyAndSell.html'
},
}
})
.state('preferencesAdvanced', {
url: '/preferencesAdvanced',
templateUrl: 'views/preferencesAdvanced.html',

View File

@ -23,12 +23,16 @@ angular.module('copayApp.services').factory('animationService', function(isCordo
preferences: 11,
preferencesGlobal: 11,
glidera: 11,
coinbase: 11,
preferencesColor: 12,
backup: 12,
preferencesAdvanced: 12,
buyGlidera: 12,
buyCoinbase: 12,
sellGlidera: 12,
sellCoinbase: 12,
preferencesGlidera: 12,
preferencesCoinbase: 12,
about: 12,
delete: 13,
preferencesLanguage: 12,
@ -46,6 +50,7 @@ angular.module('copayApp.services').factory('animationService', function(isCordo
termOfUse: 13,
translators: 13,
add: 11,
buyandsell: 11,
create: 12,
join: 12,
import: 12,

View File

@ -0,0 +1,365 @@
'use strict';
angular.module('copayApp.services').factory('coinbaseService', function($http, $log, isCordova, lodash, storageService, configService) {
var root = {};
var credentials = {};
root.setCredentials = function(network) {
credentials.SCOPE = ''
+ 'wallet:accounts:read,'
+ 'wallet:addresses:read,'
+ 'wallet:addresses:create,'
+ 'wallet:user:read,'
+ 'wallet:user:email,'
+ 'wallet:buys:read,'
+ 'wallet:buys:create,'
+ 'wallet:sells:read,'
+ 'wallet:sells:create,'
+ 'wallet:transactions:read,'
+ 'wallet:transactions:send,'
+ 'wallet:payment-methods:read';
if (isCordova) {
credentials.REDIRECT_URI = 'bitcoin://coinbase';
} else {
credentials.REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob';
}
if (network == 'testnet') {
credentials.HOST = 'https://sandbox.coinbase.com';
credentials.API = 'https://api.sandbox.coinbase.com';
credentials.CLIENT_ID = '6cdcc82d5d46654c46880e93ab3d2a43c639776347dd88022904bd78cd067841';
credentials.CLIENT_SECRET = '228cb6308951f4b6f41ba010c7d7981b2721a493c40c50fd2425132dcaccce59';
}
else {
credentials.HOST = 'https://coinbase.com';
credentials.API = 'https://api.coinbase.com';
credentials.CLIENT_ID = window.coinbase_client_id;
credentials.CLIENT_SECRET = window.coinbase_client_secret;
};
};
root.getOauthCodeUrl = function() {
return credentials.HOST
+ '/oauth/authorize?response_type=code&client_id='
+ credentials.CLIENT_ID
+ '&redirect_uri='
+ credentials.REDIRECT_URI
+ '&state=SECURE_RANDOM&scope='
+ credentials.SCOPE
+ '&meta[send_limit_amount]=100&meta[send_limit_currency]=USD&meta[send_limit_period]=day';
};
root.getToken = function(code, cb) {
var req = {
method: 'POST',
url: credentials.API + '/oauth/token',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: {
grant_type : 'authorization_code',
code: code,
client_id : credentials.CLIENT_ID,
client_secret: credentials.CLIENT_SECRET,
redirect_uri: credentials.REDIRECT_URI
}
};
$http(req).then(function(data) {
$log.info('Coinbase Authorization Access Token: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Authorization Access Token: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.refreshToken = function(refreshToken, cb) {
var req = {
method: 'POST',
url: credentials.API + '/oauth/token',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: {
grant_type : 'refresh_token',
client_id : credentials.CLIENT_ID,
client_secret: credentials.CLIENT_SECRET,
redirect_uri: credentials.REDIRECT_URI,
refresh_token: refreshToken
}
};
$http(req).then(function(data) {
$log.info('Coinbase Refresh Access Token: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Refresh Access Token: ERROR ' + data.statusText);
return cb(data.data);
});
};
var _get = function(endpoint, token) {
return {
method: 'GET',
url: credentials.API + '/v2' + endpoint,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer ' + token
}
};
};
root.getAccounts = function(token, cb) {
if (!token) return cb('Invalid Token');
$http(_get('/accounts', token)).then(function(data) {
$log.info('Coinbase Get Accounts: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Get Accounts: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.getAccount = function(token, accountId, cb) {
if (!token) return cb('Invalid Token');
$http(_get('/accounts/' + accountId, token)).then(function(data) {
$log.info('Coinbase Get Account: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Get Account: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.getAuthorizationInformation = function(token, cb) {
if (!token) return cb('Invalid Token');
$http(_get('/user/auth', token)).then(function(data) {
$log.info('Coinbase Autorization Information: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Autorization Information: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.getCurrentUser = function(token, cb) {
if (!token) return cb('Invalid Token');
$http(_get('/user', token)).then(function(data) {
$log.info('Coinbase Get Current User: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Get Current User: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.getTransaction = function(token, accountId, transactionId, cb) {
if (!token) return cb('Invalid Token');
$http(_get('/accounts/' + accountId + '/transactions/' + transactionId, token)).then(function(data) {
$log.info('Coinbase Transaction: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Transaction: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.getTransactions = function(token, accountId, cb) {
if (!token) return cb('Invalid Token');
$http(_get('/accounts/' + accountId + '/transactions', token)).then(function(data) {
$log.info('Coinbase Transactions: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Transactions: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.paginationTransactions = function(token, Url, cb) {
if (!token) return cb('Invalid Token');
$http(_get(Url.replace('/v2', ''), token)).then(function(data) {
$log.info('Coinbase Pagination Transactions: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Pagination Transactions: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.sellPrice = function(token, currency, cb) {
$http(_get('/prices/sell?currency=' + currency, token)).then(function(data) {
$log.info('Coinbase Sell Price: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Sell Price: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.buyPrice = function(token, currency, cb) {
$http(_get('/prices/buy?currency=' + currency, token)).then(function(data) {
$log.info('Coinbase Buy Price: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Buy Price: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.getPaymentMethods = function(token, cb) {
$http(_get('/payment-methods', token)).then(function(data) {
$log.info('Coinbase Get Payment Methods: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Get Payment Methods: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.getPaymentMethod = function(token, paymentMethodId, cb) {
$http(_get('/payment-methods/' + paymentMethodId, token)).then(function(data) {
$log.info('Coinbase Get Payment Method: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Get Payment Method: ERROR ' + data.statusText);
return cb(data.data);
});
};
var _post = function(endpoint, token, data) {
return {
method: 'POST',
url: credentials.API + '/v2' + endpoint,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer ' + token
},
data: data
};
};
root.sellRequest = function(token, accountId, data, cb) {
var data = {
amount: data.amount,
currency: data.currency,
payment_method: data.payment_method || null,
commit: data.commit || false
};
$http(_post('/accounts/' + accountId + '/sells', token, data)).then(function(data) {
$log.info('Coinbase Sell Request: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Sell Request: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.sellCommit = function(token, accountId, sellId, cb) {
$http(_post('/accounts/' + accountId + '/sells/' + sellId + '/commit', token)).then(function(data) {
$log.info('Coinbase Sell Commit: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Sell Commit: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.buyRequest = function(token, accountId, data, cb) {
var data = {
amount: data.amount,
currency: data.currency,
payment_method: data.payment_method || null,
commit: false
};
$http(_post('/accounts/' + accountId + '/buys', token, data)).then(function(data) {
$log.info('Coinbase Buy Request: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Buy Request: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.buyCommit = function(token, accountId, buyId, cb) {
$http(_post('/accounts/' + accountId + '/buys/' + buyId + '/commit', token)).then(function(data) {
$log.info('Coinbase Buy Commit: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Buy Commit: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.createAddress = function(token, accountId, data, cb) {
var data = {
name: data.name
};
$http(_post('/accounts/' + accountId + '/addresses', token, data)).then(function(data) {
$log.info('Coinbase Create Address: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Create Address: ERROR ' + data.statusText);
return cb(data.data);
});
};
root.sendTo = function(token, accountId, data, cb) {
var data = {
type: 'send',
to: data.to,
amount: data.amount,
currency: data.currency,
description: data.description
};
$http(_post('/accounts/' + accountId + '/transactions', token, data)).then(function(data) {
$log.info('Coinbase Create Address: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('Coinbase Create Address: ERROR ' + data.statusText);
return cb(data.data);
});
};
// Pending transactions
root.savePendingTransaction = function(ctx, opts, cb) {
var network = configService.getSync().coinbase.testnet ? 'testnet' : 'livenet';
storageService.getCoinbaseTxs(network, function(err, oldTxs) {
if (lodash.isString(oldTxs)) {
oldTxs = JSON.parse(oldTxs);
}
if (lodash.isString(ctx)) {
ctx = JSON.parse(ctx);
}
var tx = oldTxs || {};
tx[ctx.id] = ctx;
if (opts && (opts.error || opts.status)) {
tx[ctx.id] = lodash.assign(tx[ctx.id], opts);
}
if (opts && opts.remove) {
delete(tx[ctx.id]);
}
tx = JSON.stringify(tx);
storageService.setCoinbaseTxs(network, tx, function(err) {
return cb(err);
});
});
};
root.getPendingTransactions = function(cb) {
var network = configService.getSync().coinbase.testnet ? 'testnet' : 'livenet';
storageService.getCoinbaseTxs(network, function(err, txs) {
return cb(err, JSON.parse(txs));
});
};
return root;
});

View File

@ -38,6 +38,11 @@ angular.module('copayApp.services').factory('configService', function(storageSer
testnet: false
},
coinbase: {
enabled: true,
testnet: false
},
rates: {
url: 'https://insight.bitpay.com:443/api/rates',
},
@ -93,6 +98,9 @@ angular.module('copayApp.services').factory('configService', function(storageSer
if (!configCache.glidera) {
configCache.glidera = defaultConfig.glidera;
}
if (!configCache.coinbase) {
configCache.coinbase = defaultConfig.coinbase;
}
if (!configCache.pushNotifications) {
configCache.pushNotifications = defaultConfig.pushNotifications;
}
@ -105,6 +113,10 @@ angular.module('copayApp.services').factory('configService', function(storageSer
// Disabled for testnet
configCache.glidera.testnet = false;
// Coinbase
// Disabled for testnet
configCache.coinbase.testnet = false;
$log.debug('Preferences read:', configCache)
return cb(err, configCache);
});

View File

@ -664,8 +664,9 @@ angular.module('copayApp.services')
} catch (e) {};
};
root.unlockFC = function(cb) {
var fc = root.focusedClient;
root.unlockFC = function(opts, cb) {
opts = opts || {};
var fc = opts.selectedClient || root.focusedClient;
if (!fc.isPrivKeyEncrypted())
return cb();

View File

@ -223,6 +223,26 @@ angular.module('copayApp.services')
storage.remove('glideraToken-' + network, cb);
};
root.setCoinbaseRefreshToken = function(network, token, cb) {
storage.set('coinbaseRefreshToken-' + network, token, cb);
};
root.getCoinbaseRefreshToken = function(network, cb) {
storage.get('coinbaseRefreshToken-' + network, cb);
};
root.setCoinbaseToken = function(network, token, cb) {
storage.set('coinbaseToken-' + network, token, cb);
};
root.getCoinbaseToken = function(network, cb) {
storage.get('coinbaseToken-' + network, cb);
};
root.removeCoinbaseToken = function(network, cb) {
storage.remove('coinbaseToken-' + network, cb);
};
root.setAddressbook = function(network, addressbook, cb) {
storage.set('addressbook-' + network, addressbook, cb);
};
@ -247,5 +267,17 @@ angular.module('copayApp.services')
storage.remove('txsHistory-' + walletId, cb);
}
root.setCoinbaseTxs = function(network, ctx, cb) {
storage.set('coinbaseTxs-' + network, ctx, cb);
};
root.getCoinbaseTxs = function(network, cb) {
storage.get('coinbaseTxs-' + network, cb);
};
root.removeCoinbaseTxs = function(network, cb) {
storage.remove('coinbaseTxs-' + network, cb);
};
return root;
});

View File

@ -4,9 +4,10 @@ angular.module('copayApp.services').factory('txService', function($rootScope, pr
var root = {};
var reportSigningStatus = function(opts) {
opts = opts || {};
if (!opts.reporterFn) return;
var fc = profileService.focusedClient;
var fc = opts.selectedClient || profileService.focusedClient;
if (fc.isPrivKeyExternal()) {
if (fc.getPrivKeyExternalSourceName() == 'ledger') {
@ -58,9 +59,10 @@ angular.module('copayApp.services').factory('txService', function($rootScope, pr
}
};
root.checkTouchId = function(cb) {
root.checkTouchId = function(opts, cb) {
opts = opts || {};
var config = configService.getSync();
var fc = profileService.focusedClient;
var fc = opts.selectedClient || profileService.focusedClient;
config.touchIdFor = config.touchIdFor || {};
if (window.touchidAvailable && config.touchIdFor[fc.credentials.walletId]) {
requestTouchId(cb);
@ -69,17 +71,18 @@ angular.module('copayApp.services').factory('txService', function($rootScope, pr
}
};
root.prepare = function(cb) {
var fc = profileService.focusedClient;
root.prepare = function(opts, cb) {
opts = opts || {};
var fc = opts.selectedClient || profileService.focusedClient;
if (!fc.canSign() && !fc.isPrivKeyExternal())
return cb('Cannot sign'); // should never happen, no need to translate
root.checkTouchId(function(err) {
root.checkTouchId(opts, function(err) {
if (err) {
return cb(err);
};
profileService.unlockFC(function(err) {
profileService.unlockFC(opts, function(err) {
if (err) {
return cb(bwsError.msg(err));
};
@ -90,8 +93,18 @@ angular.module('copayApp.services').factory('txService', function($rootScope, pr
});
};
root.removeTx = function(txp, opts, cb) {
opts = opts || {};
var fc = opts.selectedClient || profileService.focusedClient;
fc.removeTxProposal(txp, function(err) {
return cb(err);
});
};
root.createTx = function(opts, cb) {
var fc = profileService.focusedClient;
opts = opts || {};
var fc = opts.selectedClient || profileService.focusedClient;
var currentSpendUnconfirmed = configService.getSync().wallet.spendUnconfirmed;
var getFee = function(cb) {
@ -114,16 +127,18 @@ angular.module('copayApp.services').factory('txService', function($rootScope, pr
});
};
root.publishTx = function(txp, cb) {
var fc = profileService.focusedClient;
root.publishTx = function(txp, opts, cb) {
opts = opts || {};
var fc = opts.selectedClient || profileService.focusedClient;
fc.publishTxProposal({txp: txp}, function(err, txp) {
if (err) return cb(err);
else return cb(null, txp);
});
};
var _signWithLedger = function(txp, cb) {
var fc = profileService.focusedClient;
var _signWithLedger = function(txp, opts, cb) {
opts = opts || {};
var fc = opts.selectedClient || profileService.focusedClient;
$log.info('Requesting Ledger Chrome app to sign the transaction');
ledger.signTx(txp, fc.credentials.account, function(result) {
@ -138,8 +153,9 @@ angular.module('copayApp.services').factory('txService', function($rootScope, pr
});
};
var _signWithTrezor = function(txp, cb) {
var fc = profileService.focusedClient;
var _signWithTrezor = function(txp, opts, cb) {
opts = opts || {};
var fc = opts.selectedClient || profileService.focusedClient;
$log.info('Requesting Trezor to sign the transaction');
var xPubKeys = lodash.pluck(fc.credentials.publicKeyRing, 'xPubKey');
@ -152,15 +168,16 @@ angular.module('copayApp.services').factory('txService', function($rootScope, pr
});
};
root.sign = function(txp, cb) {
var fc = profileService.focusedClient;
root.sign = function(txp, opts, cb) {
opts = opts || {};
var fc = opts.selectedClient || profileService.focusedClient;
if (fc.isPrivKeyExternal()) {
switch (fc.getPrivKeyExternalSourceName()) {
case 'ledger':
return _signWithLedger(txp, cb);
return _signWithLedger(txp, opts, cb);
case 'trezor':
return _signWithTrezor(txp, cb);
return _signWithTrezor(txp, opts, cb);
default:
var msg = 'Unsupported External Key:' + fc.getPrivKeyExternalSourceName();
$log.error(msg);
@ -175,10 +192,11 @@ angular.module('copayApp.services').factory('txService', function($rootScope, pr
};
root.signAndBroadcast = function(txp, opts, cb) {
opts = opts || {};
reportSigningStatus(opts);
var fc = profileService.focusedClient;
root.sign(txp, function(err, txp) {
var fc = opts.selectedClient || profileService.focusedClient;
root.sign(txp, opts, function(err, txp) {
if (err) {
stopReport(opts);
return cb(bwsError.msg(err), gettextCatalog.getString('Could not accept payment'));
@ -208,7 +226,8 @@ angular.module('copayApp.services').factory('txService', function($rootScope, pr
};
root.prepareAndSignAndBroadcast = function(txp, opts, cb) {
root.prepare(function(err) {
opts = opts || {};
root.prepare(opts, function(err) {
if (err) {
stopReport(opts);
return cb(err);

20
util/coinbase.js Executable file
View File

@ -0,0 +1,20 @@
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var file;
try {
file = fs.readFileSync('./coinbase.json', 'utf8');
} catch(err) {
return;
}
var json = JSON.parse(file);
console.log('Coinbase Client ID: ' + json.client_id);
var content = 'window.coinbase_client_id="' + json.client_id + '";';
content = content + '\nwindow.coinbase_client_secret="' + json.client_secret + '";';
fs.writeFileSync("./src/js/coinbase.js", content);