WIP: lightning analytics & improvements

This commit is contained in:
Overtorment 2018-09-03 23:44:45 +01:00
parent cc8711d40c
commit 3ca08b4c0f
4 changed files with 92 additions and 4 deletions

View file

@ -13,7 +13,7 @@ describe('LightningCustodianWallet', () => {
assert.ok(l1._access_token_created_ts === 0);
l1.balance = 'FAKE';
await l1.createAccount();
await l1.createAccount(true);
await l1.authorize();
await l1.fetchBtcAddress();
await l1.fetchBalance();
@ -139,5 +139,45 @@ describe('LightningCustodianWallet', () => {
if ((end - start) / 1000 > 9) {
console.warn('payInvoice took', (end - start) / 1000, 'sec');
}
// now, trying to pay duplicate invoice
start = +new Date();
let caughtError = false;
try {
await l2.payInvoice(invoice);
} catch (Err) {
caughtError = true;
}
assert.ok(caughtError);
end = +new Date();
if ((end - start) / 1000 > 9) {
console.warn('duplicate payInvoice took', (end - start) / 1000, 'sec');
}
});
it.skip('can create invoice', async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 100 * 1000;
if (!process.env.BLITZHUB) {
console.error('process.env.BLITZHUB not set, skipped');
return;
}
let l2 = new LightningCustodianWallet();
l2.setSecret(process.env.BLITZHUB);
await l2.authorize();
let invoices = await l2.getUserInvoices();
let invoice = await l2.addInvoice(1, 'memo');
let invoices2 = await l2.getUserInvoices();
assert.equal(invoices2.length, invoices.length + 1);
await l2.checkRouteInvoice(invoice);
let start = +new Date();
await l2.payInvoice(invoice);
let end = +new Date();
if ((end - start) / 1000 > 9) {
console.warn('payInvoice took', (end - start) / 1000, 'sec');
}
});
});

View file

@ -15,6 +15,7 @@ A.ENUM = {
INIT: 'INIT',
GOT_NONZERO_BALANCE: 'GOT_NONZERO_BALANCE',
CREATED_WALLET: 'CREATED_WALLET',
CREATED_LIGHTNING_WALLET: 'CREATED_LIGHTNING_WALLET',
};
module.exports = A;

View file

@ -55,12 +55,12 @@ export class LightningCustodianWallet extends LegacyWallet {
}
getTypeReadable() {
return 'Lightning (custodian)';
return 'Lightning';
}
async createAccount() {
async createAccount(isTest) {
let response = await this._api.post('/create', {
body: { partnerid: 'bluewallet', test: true },
body: { partnerid: 'bluewallet', accounttype: (isTest && 'test') || 'common' },
headers: { 'Access-Control-Allow-Origin': '*', 'Content-Type': 'application/json' },
});
let json = response.body;
@ -110,6 +110,52 @@ export class LightningCustodianWallet extends LegacyWallet {
}
}
/**
* Returns list of LND invoices created by user
*
* @return {Promise.<Array>}
*/
async getUserInvoices() {
let response = await this._api.get('/getuserinvoices', {
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
Authorization: 'Bearer' + ' ' + this.access_token,
},
});
let json = response.body;
if (typeof json === 'undefined') {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.originalResponse));
}
if (json && json.error) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}
return json;
}
async addInvoice(amt, memo) {
let response = await this._api.post('/addinvoice', {
body: { amt: 1, memo: 'test' },
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
Authorization: 'Bearer' + ' ' + this.access_token,
},
});
let json = response.body;
if (typeof json === 'undefined') {
throw new Error('API failure: ' + response.err + ' ' + JSON.stringify(response.originalResponse));
}
if (json && json.error) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}
console.log(response.body);
}
async checkRouteInvoice(invoice) {
let response = await this._api.get('/checkrouteinvoice?invoice=' + invoice, {
headers: {

View file

@ -181,6 +181,7 @@ export default class WalletsAdd extends Component {
w.setLabel(this.state.label || w.getTypeReadable());
await w.createAccount();
await w.authorize();
A(A.ENUM.CREATED_LIGHTNING_WALLET);
} else if (this.state.selectedIndex === 1) {
// btc was selected
// index 1 radio - segwit single address