wallet: always use separate http server.

This commit is contained in:
Christopher Jeffrey 2017-12-12 08:47:39 -08:00
parent fcb3a10318
commit cd7c63d95d
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
8 changed files with 60 additions and 61 deletions

View File

@ -45,7 +45,6 @@ class HTTP extends Server {
this.logger = this.options.logger.context('http');
this.wdb = this.options.node.wdb;
this.rpc = this.options.node.rpc;
this.plugin = this.options.node.plugin;
this.init();
}
@ -94,11 +93,7 @@ class HTTP extends Server {
}));
this.use(this.jsonRPC());
if (!this.plugin)
this.use('/wallet', this.router());
else
this.use(this.router());
this.use(this.router());
this.error((err, req, res) => {
const code = err.statusCode || 500;
@ -117,10 +112,10 @@ class HTTP extends Server {
if (req.path.length === 0)
return;
if (req.path[0] === '_admin')
if (req.path[0] !== 'wallet')
return;
if (req.method === 'PUT' && req.path.length === 1)
if (req.method === 'PUT' && req.path.length === 2)
return;
const id = valid.str('id');
@ -169,7 +164,7 @@ class HTTP extends Server {
});
// Rescan
this.post('/_admin/rescan', async (req, res) => {
this.post('/rescan', async (req, res) => {
const valid = Validator.fromRequest(req);
const height = valid.u32('height');
@ -179,14 +174,14 @@ class HTTP extends Server {
});
// Resend
this.post('/_admin/resend', async (req, res) => {
this.post('/resend', async (req, res) => {
await this.wdb.resend();
res.json(200, { success: true });
});
// Backup WalletDB
this.post('/_admin/backup', async (req, res) => {
this.post('/backup', async (req, res) => {
const valid = Validator.fromRequest(req);
const path = valid.str('path');
@ -198,24 +193,24 @@ class HTTP extends Server {
});
// List wallets
this.get('/_admin/wallets', async (req, res) => {
this.get('/wallets', async (req, res) => {
const wallets = await this.wdb.getWallets();
res.json(200, wallets);
});
// Get wallet
this.get('/:id', async (req, res) => {
this.get('/wallet/:id', async (req, res) => {
const balance = await req.wallet.getBalance();
res.json(200, req.wallet.toJSON(false, balance));
});
// Get wallet master key
this.get('/:id/master', (req, res) => {
this.get('/wallet/:id/master', (req, res) => {
res.json(200, req.wallet.master.toJSON(this.network, true));
});
// Create wallet
this.put('/:id', async (req, res) => {
this.put('/wallet/:id', async (req, res) => {
const valid = Validator.fromRequest(req);
let master = valid.str('master');
@ -250,13 +245,13 @@ class HTTP extends Server {
});
// List accounts
this.get('/:id/account', async (req, res) => {
this.get('/wallet/:id/account', async (req, res) => {
const accounts = await req.wallet.getAccounts();
res.json(200, accounts);
});
// Get account
this.get('/:id/account/:account', async (req, res) => {
this.get('/wallet/:id/account/:account', async (req, res) => {
const valid = Validator.fromRequest(req);
const acct = valid.str('account');
const account = await req.wallet.getAccount(acct);
@ -272,7 +267,7 @@ class HTTP extends Server {
});
// Create account
this.put('/:id/account/:account', async (req, res) => {
this.put('/wallet/:id/account/:account', async (req, res) => {
const valid = Validator.fromRequest(req);
const passphrase = valid.str('passphrase');
@ -299,7 +294,7 @@ class HTTP extends Server {
});
// Change passphrase
this.post('/:id/passphrase', async (req, res) => {
this.post('/wallet/:id/passphrase', async (req, res) => {
const valid = Validator.fromRequest(req);
const passphrase = valid.str('passphrase');
const old = valid.str('old');
@ -312,7 +307,7 @@ class HTTP extends Server {
});
// Unlock wallet
this.post('/:id/unlock', async (req, res) => {
this.post('/wallet/:id/unlock', async (req, res) => {
const valid = Validator.fromRequest(req);
const passphrase = valid.str('passphrase');
const timeout = valid.u32('timeout');
@ -325,13 +320,13 @@ class HTTP extends Server {
});
// Lock wallet
this.post('/:id/lock', async (req, res) => {
this.post('/wallet/:id/lock', async (req, res) => {
await req.wallet.lock();
res.json(200, { success: true });
});
// Import key
this.post('/:id/import', async (req, res) => {
this.post('/wallet/:id/import', async (req, res) => {
const valid = Validator.fromRequest(req);
const acct = valid.str('account');
const passphrase = valid.str('passphrase');
@ -364,7 +359,7 @@ class HTTP extends Server {
});
// Generate new token
this.post('/:id/retoken', async (req, res) => {
this.post('/wallet/:id/retoken', async (req, res) => {
const valid = Validator.fromRequest(req);
const passphrase = valid.str('passphrase');
const token = await req.wallet.retoken(passphrase);
@ -375,7 +370,7 @@ class HTTP extends Server {
});
// Send TX
this.post('/:id/send', async (req, res) => {
this.post('/wallet/:id/send', async (req, res) => {
const valid = Validator.fromRequest(req);
const passphrase = valid.str('passphrase');
const outputs = valid.array('outputs', []);
@ -419,7 +414,7 @@ class HTTP extends Server {
});
// Create TX
this.post('/:id/create', async (req, res) => {
this.post('/wallet/:id/create', async (req, res) => {
const valid = Validator.fromRequest(req);
const passphrase = valid.str('passphrase');
const outputs = valid.array('outputs', []);
@ -462,7 +457,7 @@ class HTTP extends Server {
});
// Sign TX
this.post('/:id/sign', async (req, res) => {
this.post('/wallet/:id/sign', async (req, res) => {
const valid = Validator.fromRequest(req);
const passphrase = valid.str('passphrase');
const raw = valid.buf('tx');
@ -478,7 +473,7 @@ class HTTP extends Server {
});
// Zap Wallet TXs
this.post('/:id/zap', async (req, res) => {
this.post('/wallet/:id/zap', async (req, res) => {
const valid = Validator.fromRequest(req);
const acct = valid.str('account');
const age = valid.u32('age');
@ -491,7 +486,7 @@ class HTTP extends Server {
});
// Abandon Wallet TX
this.del('/:id/tx/:hash', async (req, res) => {
this.del('/wallet/:id/tx/:hash', async (req, res) => {
const valid = Validator.fromRequest(req);
const hash = valid.rhash('hash');
@ -503,13 +498,13 @@ class HTTP extends Server {
});
// List blocks
this.get('/:id/block', async (req, res) => {
this.get('/wallet/:id/block', async (req, res) => {
const heights = await req.wallet.getBlocks();
res.json(200, heights);
});
// Get Block Record
this.get('/:id/block/:height', async (req, res) => {
this.get('/wallet/:id/block/:height', async (req, res) => {
const valid = Validator.fromRequest(req);
const height = valid.u32('height');
@ -526,7 +521,7 @@ class HTTP extends Server {
});
// Add key
this.put('/:id/shared-key', async (req, res) => {
this.put('/wallet/:id/shared-key', async (req, res) => {
const valid = Validator.fromRequest(req);
const acct = valid.str('account');
const b58 = valid.str('accountKey');
@ -541,7 +536,7 @@ class HTTP extends Server {
});
// Remove key
this.del('/:id/shared-key', async (req, res) => {
this.del('/wallet/:id/shared-key', async (req, res) => {
const valid = Validator.fromRequest(req);
const acct = valid.str('account');
const b58 = valid.str('accountKey');
@ -556,7 +551,7 @@ class HTTP extends Server {
});
// Get key by address
this.get('/:id/key/:address', async (req, res) => {
this.get('/wallet/:id/key/:address', async (req, res) => {
const valid = Validator.fromRequest(req);
const b58 = valid.str('address');
@ -574,7 +569,7 @@ class HTTP extends Server {
});
// Get private key
this.get('/:id/wif/:address', async (req, res) => {
this.get('/wallet/:id/wif/:address', async (req, res) => {
const valid = Validator.fromRequest(req);
const b58 = valid.str('address');
const passphrase = valid.str('passphrase');
@ -593,7 +588,7 @@ class HTTP extends Server {
});
// Create address
this.post('/:id/address', async (req, res) => {
this.post('/wallet/:id/address', async (req, res) => {
const valid = Validator.fromRequest(req);
const acct = valid.str('account');
const addr = await req.wallet.createReceive(acct);
@ -602,7 +597,7 @@ class HTTP extends Server {
});
// Create change address
this.post('/:id/change', async (req, res) => {
this.post('/wallet/:id/change', async (req, res) => {
const valid = Validator.fromRequest(req);
const acct = valid.str('account');
const addr = await req.wallet.createChange(acct);
@ -611,7 +606,7 @@ class HTTP extends Server {
});
// Create nested address
this.post('/:id/nested', async (req, res) => {
this.post('/wallet/:id/nested', async (req, res) => {
const valid = Validator.fromRequest(req);
const acct = valid.str('account');
const addr = await req.wallet.createNested(acct);
@ -620,7 +615,7 @@ class HTTP extends Server {
});
// Wallet Balance
this.get('/:id/balance', async (req, res) => {
this.get('/wallet/:id/balance', async (req, res) => {
const valid = Validator.fromRequest(req);
const acct = valid.str('account');
const balance = await req.wallet.getBalance(acct);
@ -634,7 +629,7 @@ class HTTP extends Server {
});
// Wallet UTXOs
this.get('/:id/coin', async (req, res) => {
this.get('/wallet/:id/coin', async (req, res) => {
const valid = Validator.fromRequest(req);
const acct = valid.str('account');
const coins = await req.wallet.getCoins(acct);
@ -649,7 +644,7 @@ class HTTP extends Server {
});
// Locked coins
this.get('/:id/locked', async (req, res) => {
this.get('/wallet/:id/locked', async (req, res) => {
const locked = req.wallet.getLocked();
const result = [];
@ -660,7 +655,7 @@ class HTTP extends Server {
});
// Lock coin
this.put('/:id/locked/:hash/:index', async (req, res) => {
this.put('/wallet/:id/locked/:hash/:index', async (req, res) => {
const valid = Validator.fromRequest(req);
const hash = valid.rhash('hash');
const index = valid.u32('index');
@ -676,7 +671,7 @@ class HTTP extends Server {
});
// Unlock coin
this.del('/:id/locked/:hash/:index', async (req, res) => {
this.del('/wallet/:id/locked/:hash/:index', async (req, res) => {
const valid = Validator.fromRequest(req);
const hash = valid.rhash('hash');
const index = valid.u32('index');
@ -692,7 +687,7 @@ class HTTP extends Server {
});
// Wallet Coin
this.get('/:id/coin/:hash/:index', async (req, res) => {
this.get('/wallet/:id/coin/:hash/:index', async (req, res) => {
const valid = Validator.fromRequest(req);
const hash = valid.rhash('hash');
const index = valid.u32('index');
@ -711,7 +706,7 @@ class HTTP extends Server {
});
// Wallet TXs
this.get('/:id/tx/history', async (req, res) => {
this.get('/wallet/:id/tx/history', async (req, res) => {
const valid = Validator.fromRequest(req);
const acct = valid.str('account');
const txs = await req.wallet.getHistory(acct);
@ -729,7 +724,7 @@ class HTTP extends Server {
});
// Wallet Pending TXs
this.get('/:id/tx/unconfirmed', async (req, res) => {
this.get('/wallet/:id/tx/unconfirmed', async (req, res) => {
const valid = Validator.fromRequest(req);
const acct = valid.str('account');
const txs = await req.wallet.getPending(acct);
@ -746,7 +741,7 @@ class HTTP extends Server {
});
// Wallet TXs within time range
this.get('/:id/tx/range', async (req, res) => {
this.get('/wallet/:id/tx/range', async (req, res) => {
const valid = Validator.fromRequest(req);
const acct = valid.str('account');
@ -768,7 +763,7 @@ class HTTP extends Server {
});
// Last Wallet TXs
this.get('/:id/tx/last', async (req, res) => {
this.get('/wallet/:id/tx/last', async (req, res) => {
const valid = Validator.fromRequest(req);
const acct = valid.str('account');
const limit = valid.u32('limit');
@ -783,7 +778,7 @@ class HTTP extends Server {
});
// Wallet TX
this.get('/:id/tx/:hash', async (req, res) => {
this.get('/wallet/:id/tx/:hash', async (req, res) => {
const valid = Validator.fromRequest(req);
const hash = valid.rhash('hash');
@ -802,7 +797,7 @@ class HTTP extends Server {
});
// Resend
this.post('/:id/resend', async (req, res) => {
this.post('/wallet/:id/resend', async (req, res) => {
await req.wallet.resend();
res.json(200, { success: true });
});

View File

@ -39,7 +39,6 @@ class Plugin extends EventEmitter {
this.logger = node.logger;
this.client = new NodeClient(node);
this.plugin = true;
this.wdb = new WalletDB({
network: node.network,
@ -62,13 +61,16 @@ class Plugin extends EventEmitter {
network: node.network,
logger: node.logger,
node: this,
ssl: config.bool(['wallet-ssl', 'ssl']),
keyFile: config.path(['wallet-ssl-key', 'ssl-key']),
certFile: config.path(['wallet-ssl-cert', 'ssl-cert']),
host: config.str(['wallet-http-host', 'http-host']),
port: config.uint(['wallet-http-port', 'http-port']),
apiKey: config.str(['wallet-api-key', 'api-key']),
walletAuth: config.bool('wallet-auth'),
noAuth: config.bool(['wallet-no-auth', 'no-auth'])
});
this.http.attach('/wallet', node.http);
this.init();
}
@ -80,9 +82,11 @@ class Plugin extends EventEmitter {
async open() {
await this.wdb.open();
this.rpc.wallet = this.wdb.primary;
await this.http.open();
}
async close() {
await this.http.close();
this.rpc.wallet = null;
await this.wdb.close();
}

View File

@ -67,8 +67,8 @@ class WalletNode extends Node {
host: this.config.str('http-host'),
port: this.config.uint('http-port'),
apiKey: this.config.str('api-key'),
noAuth: this.config.bool('no-auth'),
walletAuth: this.config.bool('wallet-auth')
walletAuth: this.config.bool('wallet-auth'),
noAuth: this.config.bool('no-auth')
});
this.init();

View File

@ -30,7 +30,7 @@ const workers = new WorkerPool({
});
const chain = new Chain({
db: 'memory',
memory: true,
network,
workers
});

View File

@ -18,7 +18,7 @@ const node = new FullNode({
network: 'regtest',
apiKey: 'foo',
walletAuth: true,
db: 'memory',
memory: true,
workers: true,
plugins: [require('../lib/wallet/plugin')]
});
@ -31,7 +31,7 @@ const client = new NodeClient({
});
const wallet = new WalletClient({
port: network.rpcPort,
port: network.walletPort,
apiKey: 'foo'
});

View File

@ -27,13 +27,13 @@ const workers = new WorkerPool({
});
const chain = new Chain({
db: 'memory',
memory: true,
workers
});
const mempool = new Mempool({
chain,
db: 'memory',
memory: true,
workers
});

View File

@ -14,7 +14,7 @@ const TX = require('../lib/primitives/tx');
const Address = require('../lib/primitives/address');
const node = new FullNode({
db: 'memory',
memory: true,
apiKey: 'foo',
network: 'regtest',
workers: true,

View File

@ -25,7 +25,7 @@ class NodeContext {
const node = new FullNode({
network: this.network,
db: 'memory',
memory: true,
logger: new Logger({
level: 'debug',
file: false,