Skip to content

Commit

Permalink
feat(api): Adds check wallet endpoint
Browse files Browse the repository at this point in the history
Provides last imported address and hash of all wallet addresses
  • Loading branch information
nitsujlangston committed Jan 3, 2019
1 parent 84d7a69 commit a606095
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/bitcore-client/bin/wallet-check
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env node

const program = require('../ts_build/program');
const { Wallet } = require('../ts_build/wallet');

program
.version(require('../package.json').version)
.option('--name <name>', 'REQUIRED - Wallet name')
.option('--path [path]', 'optional - Custom wallet storage path')
.parse(process.argv);

const main = async () => {
const { name, path } = program;
try {
const wallet = await Wallet.loadWallet({ name, path });
const registered = await wallet.checkWallet();
// TODO compare with local addresses
console.log(registered);
} catch (e) {
console.error(e);
}
};

main();
10 changes: 10 additions & 0 deletions packages/bitcore-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,14 @@ export class Client {
const url = `${this.baseUrl}/tx/send`;
return request.post(url, { body: payload, json: true });
}

async checkWallet(params) {
const { pubKey } = params;
const url = `${this.baseUrl}/wallet/${pubKey}/check`;
const signature = this.sign({ method: 'GET', url });
return request.get(url, {
headers: { 'x-signature': signature },
json: true
});
}
}
6 changes: 6 additions & 0 deletions packages/bitcore-client/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,10 @@ export class Wallet {
let keys = await Promise.all(keyPromises);
return TxProvider.sign({ ...payload, keys });
}

async checkWallet() {
return this.client.checkWallet({
pubKey: this.authPubKey
});
}
}
4 changes: 4 additions & 0 deletions packages/bitcore-node/src/providers/chain-state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class ChainStateProxy implements CSP.ChainStateProvider {
return this.get(params).streamWalletAddresses(params);
}

walletCheck(params: CSP.WalletCheckParams) {
return this.get(params).walletCheck(params);
}

async updateWallet(params: CSP.UpdateWalletParams) {
return this.get(params).updateWallet(params);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { TransactionJSON } from '../../../types/Transaction';
import through2 from 'through2';
import crypto from 'crypto';

import { MongoBound } from '../../../models/base';
import { ObjectId } from 'mongodb';
import { CoinStorage, ICoin } from '../../../models/coin';
import { BlockStorage, IBlock } from '../../../models/block';
import { WalletStorage, IWallet } from '../../../models/wallet';
import { WalletAddressStorage } from '../../../models/walletAddress';
import { WalletAddressStorage, IWalletAddress } from '../../../models/walletAddress';
import { CSP } from '../../../types/namespaces/ChainStateProvider';
import { Storage } from '../../../services/storage';
import { RPC } from '../../../rpc';
Expand Down Expand Up @@ -258,6 +259,22 @@ export class InternalStateProvider implements CSP.IChainStateService {
Storage.apiStreamingFind(WalletAddressStorage, query, { limit }, req, res);
}

async walletCheck(params: CSP.WalletCheckParams) {
let { wallet } = params;
return new Promise(resolve => {
const addressStream = WalletAddressStorage.collection.find({ wallet });
const hash = crypto.createHash('sha256');
let lastAddress;
addressStream.on('data', (walletAddress: IWalletAddress) => {
lastAddress = walletAddress.address;
hash.update(walletAddress.address);
});
addressStream.on('end', () => {
resolve({ lastAddress, hash: hash.digest('hex') });
});
});
}

async streamMissingWalletAddresses(params: CSP.StreamWalletMissingAddressesParams) {
const { chain, network, pubKey, res } = params;
const wallet = await WalletStorage.collection.findOne({ pubKey });
Expand Down
15 changes: 15 additions & 0 deletions packages/bitcore-node/src/routes/api/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ router.get('/:pubKey/addresses', authenticate, async (req: AuthenticatedRequest,
}
});

router.get('/:pubKey/check', authenticate, async (req: AuthenticatedRequest, res) => {
const { chain, network } = req.params;
const wallet = req.wallet!._id!;
try {
const result = await ChainStateProvider.walletCheck({
chain,
network,
wallet
});
return res.send(result);
} catch (err) {
return res.status(500).json(err);
}
});

// update wallet
router.post('/:pubKey', authenticate, async (req: AuthenticatedRequest, res) => {
let { chain, network } = req.params;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export declare namespace CSP {
limit: number;
};

export type WalletCheckParams = ChainNetwork & {
wallet: ObjectId;
};

export type StreamWalletMissingAddressesParams = ChainNetwork & {
pubKey: string;
req: Request;
Expand Down Expand Up @@ -127,6 +131,7 @@ export declare namespace CSP {
getDailyTransactions(params: { chain: string; network: string }): Promise<DailyTransactionsJSON>;
getTransaction(params: StreamTransactionParams): Promise<TransactionJSON | string | undefined>;
streamWalletAddresses(params: StreamWalletAddressesParams): any;
walletCheck(params: WalletCheckParams): any;
streamWalletTransactions(params: StreamWalletTransactionsParams): any;
streamWalletUtxos(params: StreamWalletUtxosParams): any;
streamMissingWalletAddresses(params: StreamWalletMissingAddressesParams);
Expand Down

0 comments on commit a606095

Please sign in to comment.