Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #129 from blockchain/remove-apicode
Browse files Browse the repository at this point in the history
Make api_code parameter optional
  • Loading branch information
mpfluger authored Aug 9, 2016
2 parents 27a32ce + aca16fe commit a8ef21f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 41 deletions.
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Start by completing the following steps:

Note that `blockchain-wallet-service` is designed to be run locally on the same machine as your application and therefore will only accept connections from `localhost`. If you modify this service to accept external connections, be sure to add the appropriate firewall rules to prevent unauthorized use.

An API code is required for wallet creation and higher request limits. For basic usage, no API code is required. Request an API code [here](https://blockchain.info/api/api_create_code).

## Upgrading

If you already have an application that uses [Blockchain.info's Wallet API](https://blockchain.info/api/blockchain_wallet_api), you will need to complete the steps in the Getting Started section above and then, in your application code, replace calls to `blockchain.info/merchant/...` with `localhost:<port>/merchant/...`.
Expand All @@ -53,8 +55,6 @@ Query Parameters:
* `label` - label to give to the first address generated in the wallet (optional)
* `email` - email to associate with the newly created wallet (optional)

Get an API code [here](https://blockchain.info/api/api_create_code). **Note**: You must check the "Create Wallets" checkbox under "Permissions" when requesting an API code in order for it to be compatible with this app.

Sample Response:

```json
Expand All @@ -75,7 +75,7 @@ Query Parameters:
* `amount` - amount **in satoshi** to send (required)
* `password` - main wallet password (required)
* `second_password` - second wallet password (required, only if second password is enabled)
* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)
* `from` - bitcoin address or account index to send from (optional)
* `fee` - specify transaction fee **in satoshi** (optional, otherwise fee is computed)

Expand All @@ -101,7 +101,7 @@ Query Parameters:
* `recipients` - a *URI encoded* [JSON object](http://json.org/example.html), with bitcoin addresses as keys and the **satoshi** amounts as values (required, see example below)
* `password` - main wallet password (required)
* `second_password` - second wallet password (required, only if second password is enabled)
* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)
* `from` - bitcoin address or account index to send from (optional)
* `fee` - specify transaction fee **in satoshi** (optional, otherwise fee is computed)

Expand Down Expand Up @@ -150,7 +150,7 @@ Endpoint: `/merchant/:guid/list`
Query Parameters:

* `password` - main wallet password (required)
* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)

Sample Response:

Expand Down Expand Up @@ -180,7 +180,7 @@ Query Parameters:

* `address` - address to fetch balance for (required)
* `password` - main wallet password (required)
* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)

Note: unlike the hosted API, there is no option of a `confirmations` parameter for specifying minimum confirmations.

Expand All @@ -198,7 +198,7 @@ Query Parameters:

* `password` - main wallet password (required)
* `label` - label to give to the address (optional)
* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)

Sample Response:

Expand All @@ -214,7 +214,7 @@ Query Parameters:

* `address` - address to archive (required)
* `password` - main wallet password (required)
* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)

Sample Response:

Expand All @@ -230,7 +230,7 @@ Query Parameters:

* `address` - address to unarchive (required)
* `password` - main wallet password (required)
* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)

Sample Response:

Expand All @@ -244,7 +244,7 @@ Endpoint: `/merchant/:guid/enableHD`

Query Parameters:

* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)

This will upgrade a wallet to an HD (Hierarchical Deterministic) Wallet, which allows the use of accounts. See [BIP32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki) for more information on HD wallets and accounts.

Expand All @@ -254,23 +254,23 @@ Endpoint: `/merchant/:guid/accounts`

Query Parameters:

* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)

### List HD xPubs

Endpoint: `/merchant/:guid/accounts/xpubs`

Query Parameters:

* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)

### Create New HD Account

Endpoint: `/merchant/:guid/accounts/create`

Query Parameters:

* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)
* `label` - label to assign to the newly created account

### Get Single HD Account
Expand All @@ -279,39 +279,39 @@ Endpoint: `/merchant/:guid/accounts/:xpub_or_index`

Query Parameters:

* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)

### Get HD Account Receiving Address

Endpoint: `/merchant/:guid/accounts/:xpub_or_index/receiveAddress`

Query Parameters:

* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)

### Check HD Account Balance

Endpoint: `/merchant/:guid/accounts/:xpub_or_index/balance`

Query Parameters:

* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)

### Archive HD Account

Endpoint: `/merchant/:guid/accounts/:xpub_or_index/archive`

Query Parameters:

* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)

### Unarchive HD Account

Endpoint: `/merchant/:guid/accounts/:xpub_or_index/unarchive`

Query Parameters:

* `api_code` - blockchain.info wallet api code (required)
* `api_code` - blockchain.info wallet api code (optional)

## RPC

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"dependencies": {
"basic-auth": "~1.0.3",
"bitcoinjs-lib": "~2.2.0",
"blockchain-wallet-client-prebuilt": "3.14.*",
"blockchain-wallet-client-prebuilt": "3.22.*",
"blockchain.info": "~2.2.2",
"body-parser": "~1.14.1",
"commander": "~2.9.0",
Expand Down
34 changes: 17 additions & 17 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,105 +51,105 @@ legacyAPI.use(parseOptions({

merchantAPI.all(
'/login',
required(['password', 'api_code']),
required(['password']),
callApi('login')
);

merchantAPI.all(
'/enableHD',
required(['password', 'api_code']),
required(['password']),
callApi('upgradeWallet')
);

// Routing: Legacy Wallet API
legacyAPI.all(
'/balance',
required(['password', 'api_code']),
required(['password']),
callApi('getBalance')
);

legacyAPI.all(
'/list',
required(['password', 'api_code']),
required(['password']),
callApi('listAddresses')
);

legacyAPI.all(
'/address_balance',
required(['address', 'password', 'api_code']),
required(['address', 'password']),
callApi('getAddressBalance')
);

legacyAPI.all(
'/sendmany',
required(['recipients', 'password', 'api_code']),
required(['recipients', 'password']),
callApi('sendMany')
);

legacyAPI.all(
'/payment',
required(['to', 'amount', 'password', 'api_code']),
required(['to', 'amount', 'password']),
callApi('makePayment')
);

legacyAPI.all(
'/new_address',
required(['password', 'api_code']),
required(['password']),
callApi('generateAddress')
);

legacyAPI.all(
'/archive_address',
required(['address', 'password', 'api_code']),
required(['address', 'password']),
callApi('archiveAddress')
);

legacyAPI.all(
'/unarchive_address',
required(['address', 'password', 'api_code']),
required(['address', 'password']),
callApi('unarchiveAddress')
);

// Routing: HD Accounts API
accountsAPI.all(
'/xpubs',
required(['password', 'api_code']),
required(['password']),
callApi('listxPubs')
);

accountsAPI.all(
'/create',
required(['password', 'api_code']),
required(['password']),
callApi('createAccount')
);

accountsAPI.all(
'/:account?',
required(['password', 'api_code']),
required(['password']),
callApi('listAccounts')
);

accountsAPI.all(
'/:account/receiveAddress',
required(['password', 'api_code']),
required(['password']),
callApi('getReceiveAddress')
);

accountsAPI.all(
'/:account/balance',
required(['password', 'api_code']),
required(['password']),
callApi('getAccountBalance')
);

accountsAPI.all(
'/:account/archive',
required(['password', 'api_code']),
required(['password']),
callApi('archiveAccount')
);

accountsAPI.all(
'/:account/unarchive',
required(['password', 'api_code']),
required(['password']),
callApi('unarchiveAccount')
);

Expand Down
13 changes: 8 additions & 5 deletions src/wallet-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,26 @@ WalletCache.prototype.login = function (guid, options) {
var instance = generateInstance()
, pwHash = generatePwHash(options.password)
, deferred = q.defer()
, success = deferred.resolve.bind(null, instance)
, error = deferred.reject
, needs2FA = deferred.reject.bind(null, 'ERR_2FA')
, needsAuth = deferred.reject.bind(null, 'ERR_AUTH')
, timeout = setTimeout(deferred.reject.bind(null, 'ERR_TIMEOUT'), TIMEOUT_MS)
, done = clearTimeout.bind(null, timeout)
, remove = function () { this.instanceStore[guid] = undefined; }.bind(this);

this.instanceStore[guid] = deferred.promise;
instance.API.API_CODE = options.api_code;
instance.WalletStore.isLogoutDisabled = function () { return true; };
overrides.handleSocketErrors(instance.MyWallet.ws);
overrides.substituteWithCryptoRNG(instance.RNG);
instance.MyWallet.login(guid, null, options.password, null, success, needs2FA, null, needsAuth, error);

deferred.promise.then(function () { this.pwHashStore[guid] = pwHash; }.bind(this));
return deferred.promise.catch(remove).fin(done);
var callbacks = { authorizationRequired: needsAuth, needsTwoFactorCode: needs2FA };
var loginP = instance.MyWallet.login(guid, options.password, { twoFactor: null }, callbacks);
var startupPromise = q.race([ deferred.promise, loginP.then(function () { return instance; }) ]);

this.instanceStore[guid] = startupPromise;
startupPromise.then(function () { this.pwHashStore[guid] = pwHash; }.bind(this));

return startupPromise.catch(remove).fin(done);
};

WalletCache.prototype.createWallet = function (options) {
Expand Down

0 comments on commit a8ef21f

Please sign in to comment.