2019-01-23 07:26:17 -08:00
|
|
|
// @flow
|
2019-02-04 20:41:45 -08:00
|
|
|
|
2019-01-23 07:26:17 -08:00
|
|
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
|
|
import 'babel-polyfill';
|
|
|
|
|
|
|
|
import createTestServer from 'create-test-server';
|
|
|
|
|
2019-01-23 11:37:05 -08:00
|
|
|
const transactions = [];
|
|
|
|
|
2019-01-24 11:23:29 -08:00
|
|
|
const sleep = (time: number) => new Promise(resolve => setTimeout(resolve, time));
|
|
|
|
|
2019-01-23 07:26:17 -08:00
|
|
|
createTestServer({
|
|
|
|
httpPort: '18232',
|
|
|
|
}).then(async (server) => {
|
2019-02-04 20:41:45 -08:00
|
|
|
console.log('[MOCK RPC API]', server.url); // eslint-disable-line
|
2019-01-23 07:26:17 -08:00
|
|
|
|
|
|
|
server.get('/', (req, res) => {
|
|
|
|
res.send('Zcash RPC');
|
|
|
|
});
|
|
|
|
|
2019-01-24 11:23:29 -08:00
|
|
|
server.post('/', async (req, res) => {
|
2019-01-23 07:26:17 -08:00
|
|
|
const { method } = req.body;
|
|
|
|
|
|
|
|
switch (method) {
|
|
|
|
case 'getinfo':
|
2019-01-24 11:23:29 -08:00
|
|
|
sleep(1500).then(() => res.send({ result: { version: 1.0 } }));
|
|
|
|
break;
|
2019-01-23 07:26:17 -08:00
|
|
|
case 'getblockchaininfo':
|
|
|
|
return res.send({ result: { verificationprogress: 0.5 } });
|
|
|
|
case 'z_gettotalbalance':
|
|
|
|
return res.send({
|
|
|
|
result: { transparent: 2.5, private: 3.5, total: 6 },
|
|
|
|
});
|
2019-01-23 09:03:15 -08:00
|
|
|
case 'z_listaddresses':
|
|
|
|
return res.send({
|
2019-01-24 11:23:29 -08:00
|
|
|
result: ['zs1z7rejlpsa98s2rrrfkwmaxu53e4ue0ulcrw0h4x5g8jl04tak0d3mm47vdtahatqrlkngh9sly'],
|
2019-01-23 09:03:15 -08:00
|
|
|
});
|
|
|
|
case 'getaddressesbyaccount':
|
|
|
|
return res.send({
|
|
|
|
result: ['t3Pnbg7XjP7FGPBUuz75H65aczphHgkpoJW'],
|
|
|
|
});
|
|
|
|
case 'listtransactions':
|
|
|
|
return res.send({
|
2019-01-23 11:37:05 -08:00
|
|
|
result: transactions,
|
2019-01-23 09:03:15 -08:00
|
|
|
});
|
2019-01-23 11:37:05 -08:00
|
|
|
case 'z_sendmany':
|
|
|
|
// eslint-disable-next-line
|
2019-01-24 11:23:29 -08:00
|
|
|
sleep(2000).then(() => {
|
|
|
|
const [, [obj], amount, fee] = req.body.params;
|
|
|
|
|
|
|
|
if ((obj.address[0] === 'z' || obj.address[0] === 't') && amount > 0) {
|
|
|
|
transactions.push({
|
|
|
|
account: '',
|
|
|
|
address: obj.address,
|
|
|
|
category: 'send',
|
|
|
|
amount: obj.amount,
|
|
|
|
vout: 0,
|
|
|
|
fee,
|
|
|
|
confirmations: 10,
|
|
|
|
blockhash: 20,
|
|
|
|
blockindex: 10,
|
|
|
|
txid: `operation-id-${transactions.length + 1}`,
|
|
|
|
time: Date.now(),
|
|
|
|
timereceived: Date.now(),
|
|
|
|
comment: '',
|
|
|
|
otheraccount: '',
|
|
|
|
size: 10,
|
|
|
|
});
|
|
|
|
|
|
|
|
return res.send({ result: 'operation-id-1' });
|
|
|
|
}
|
2019-01-23 11:37:05 -08:00
|
|
|
|
2019-01-24 11:23:29 -08:00
|
|
|
return res.status(500).send({ error: { message: 'Invalid address!' } });
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'z_validateaddress':
|
|
|
|
// eslint-disable-next-line
|
|
|
|
const [zAd] = req.body.params;
|
2019-01-23 11:37:05 -08:00
|
|
|
|
2019-01-24 11:23:29 -08:00
|
|
|
if (zAd[0] === 'z' || zAd[0] === 't') {
|
|
|
|
return res.send({ result: { isvalid: true } });
|
2019-01-23 11:37:05 -08:00
|
|
|
}
|
|
|
|
|
2019-01-24 11:23:29 -08:00
|
|
|
return res.send({ result: { isvalid: false } });
|
|
|
|
case 'validateaddress':
|
|
|
|
// eslint-disable-next-line
|
|
|
|
const [tAd] = req.body.params;
|
|
|
|
|
|
|
|
if (tAd[0] === 'z' || tAd[0] === 't') {
|
|
|
|
return res.send({ result: { isvalid: true } });
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.send({ result: { isvalid: false } });
|
|
|
|
case 'z_getoperationstatus':
|
|
|
|
return res.send({
|
|
|
|
result: [{ id: 'operation-id-1', status: 'success', result: { txid: 'txid-1' } }],
|
|
|
|
});
|
2019-02-11 06:47:40 -08:00
|
|
|
case 'z_getbalance':
|
|
|
|
return res.send({
|
|
|
|
result: 5,
|
|
|
|
});
|
2019-01-23 07:26:17 -08:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|