Browse Source

Merge pull request #128 from stakwork/cash

Cash
jsc2
Evan Feenstra 4 years ago
committed by GitHub
parent
commit
34333f5aaf
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      dist/src/constants.js
  2. 2
      dist/src/constants.js.map
  3. 4
      dist/src/controllers/index.js
  4. 2
      dist/src/controllers/index.js.map
  5. 139
      dist/src/controllers/queries.js
  6. 1
      dist/src/controllers/queries.js.map
  7. 3
      dist/src/models/index.js
  8. 2
      dist/src/models/index.js.map
  9. 60
      dist/src/models/ts/accounting.js
  10. 1
      dist/src/models/ts/accounting.js.map
  11. 25
      dist/src/utils/lightning.js
  12. 2
      dist/src/utils/lightning.js.map
  13. 2
      src/constants.ts
  14. 5
      src/controllers/index.ts
  15. 137
      src/controllers/queries.ts
  16. 3
      src/models/index.ts
  17. 38
      src/models/ts/accounting.ts
  18. 25
      src/utils/lightning.ts

2
dist/src/constants.js

@ -59,6 +59,8 @@ const constants = {
"heartbeat_confirmation": 27,
"keysend": 28,
"boost": 29,
"query": 30,
"query_response": 31
},
network_types: {
"lightning": 0,

2
dist/src/constants.js.map

@ -1 +1 @@
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":";;AAAA,MAAM,SAAS,GAAG;IAChB,cAAc,EAAE,CAAC;IACjB,eAAe,EAAE;QACf,SAAS,EAAE,CAAC;QACZ,OAAO,EAAE,CAAC;QACV,WAAW,EAAE,CAAC;QACd,aAAa,EAAE,CAAC;QAChB,UAAU,EAAE,CAAC;QACb,SAAS,EAAE,CAAC;QACZ,iBAAiB,EAAE,CAAC;KACrB;IACD,gBAAgB,EAAE;QAChB,SAAS,EAAE,CAAC;QACZ,WAAW,EAAE,CAAC;KACf;IACD,QAAQ,EAAE;QACR,SAAS,EAAE,CAAC;QACZ,WAAW,EAAE,CAAC;QACd,WAAW,EAAE,CAAC;QACd,UAAU,EAAE,CAAC;QACb,QAAQ,EAAE,CAAC;QACX,SAAS,EAAE,CAA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ChB,aAAa,EAAE,EAAE;QACjB,YAAY,EAAE,EAAE;QAChB,QAAQ,EAAE,EAAE;QACZ,WAAW,EAAE,EAAE;QACf,gBAAgB,EAAE,EAAE;QACpB,gBAAgB,EAAE,EAAE;QACpB,eAAe,EAAE,EAAE;QACnB,cAAc,EAAE,EAAE;QAClB,aAAa,EAAE,EAAE;QACjB,SAAS,EAAE,EAAE;QACb,SAAS,EAAE,EAAE;QACb,WAAW,EAAE,EAAE;QACf,wBAAwB,EAAE,EAAE;QAC5B,SAAS,EAAE,EAAE;QACb,OAAO,EAAE,EAAE;QACX,OAAO,EAAE,EAAE;QACX,gBAAgB,EAAE,EAAE;KACrB;IACD,aAAa,EAAE;QACb,WAAW,EAAE,CAAC;QACd,MAAM,EAAE,CAAC;KACV;IACD,cAAc,EAAE;QACd,SAAS,EAAE,WAAW;QACtB,UAAU,EAAE,sBAAsB;QAClC,OAAO,EAAE,OAAO;QAChB,2BAA2B,EAAE,2BAA2B;QACxD,SAAS,EAAE,SAAS;KACrB;IACD,UAAU,EAAE;QACV,cAAc,EAAE,CAAC;QACjB,OAAO,EAAE,CAAC;QACV,OAAO,EAAE,CAAC;KACX;IACD,SAAS,EAAE;QACT,SAAS,EAAE,CAAC;QACZ,OAAO,EAAE,CAAC;QACV,QAAQ,EAAE,CAAC;KACZ;IACD,UAAU,EAAE;QACV,GAAG,EAAE,CAAC;QACN,OAAO,EAAE,CAAC;QACV,OAAO,EAAE,CAAC;QACV,KAAK,EAAE,CAAC;QACR,QAAQ,EAAE,CAAC;QACX,QAAQ,EAAE,CAAC;KACZ;CACF,CAAA;AAED,kBAAe,SAAS,CAAA"}

4
dist/src/controllers/index.js

@ -24,6 +24,7 @@ const subcriptions = require("./subscriptions");
const uploads = require("./uploads");
const confirmations = require("./confirmations");
const actions = require("./api");
const queries = require("./queries");
const gitinfo_1 = require("../utils/gitinfo");
const timers = require("../utils/timers");
const builtInBots = require("../builtin");
@ -93,6 +94,7 @@ function set(app) {
app.get('/logs', details.getLogsSince);
app.get('/info', details.getNodeInfo);
app.get('/route', details.checkRoute);
app.get('/query/onchain_address/:app', queries.queryOnchainAddress);
app.post('/action', actions.processAction);
app.get('/bots', bots.getBots);
app.post('/bot', bots.createBot);
@ -151,5 +153,7 @@ exports.ACTIONS = {
[msgtypes.heartbeat]: confirmations.receiveHeartbeat,
[msgtypes.heartbeat_confirmation]: confirmations.receiveHeartbeatConfirmation,
[msgtypes.boost]: messages.receiveBoost,
[msgtypes.query]: queries.receiveQuery,
[msgtypes.query_response]: queries.receiveQueryResponse,
};
//# sourceMappingURL=index.js.map

2
dist/src/controllers/index.js.map

File diff suppressed because one or more lines are too long

139
dist/src/controllers/queries.js

@ -0,0 +1,139 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const res_1 = require("../utils/res");
const models_1 = require("../models");
const network = require("../network");
const constants_1 = require("../constants");
const short = require("short-uuid");
const lightning = require("../utils/lightning");
let queries = {};
const hub_pubkey = '023d70f2f76d283c6c4e58109ee3a2816eb9d8feb40b23d62469060a2b2867b77f';
function queryOnchainAddress(req, res) {
return __awaiter(this, void 0, void 0, function* () {
console.log('=> queryOnchainAddress');
const uuid = short.generate();
const owner = yield models_1.models.Contact.findOne({ where: { isOwner: true } });
const app = req.params.app;
const query = {
type: 'onchain_address',
uuid,
app
};
const opts = {
amt: constants_1.default.min_sat_amount,
dest: hub_pubkey,
data: {
type: constants_1.default.message_types.query,
message: {
content: JSON.stringify(query)
},
sender: { pub_key: owner.publicKey }
}
};
try {
yield network.signAndSend(opts);
}
catch (e) {
res_1.failure(res, e);
return;
}
let i = 0;
let interval = setInterval(() => {
if (i >= 15) {
clearInterval(interval);
delete queries[uuid];
res_1.failure(res, 'no response received');
return;
}
if (queries[uuid]) {
res_1.success(res, queries[uuid].result);
clearInterval(interval);
delete queries[uuid];
return;
}
i++;
}, 1000);
});
}
exports.queryOnchainAddress = queryOnchainAddress;
exports.receiveQuery = (payload) => __awaiter(void 0, void 0, void 0, function* () {
const dat = payload.content || payload;
const sender_pub_key = dat.sender.pub_key;
const content = dat.message.content;
const owner = yield models_1.models.Contact.findOne({ where: { isOwner: true } });
if (!sender_pub_key || !content || !owner) {
return console.log('=> wrong query format');
}
let q;
try {
q = JSON.parse(content);
}
catch (e) {
console.log("=> ERROR receiveQuery,", e);
return;
}
console.log('=> query received', q);
let result = '';
switch (q.type) {
case 'onchain_address':
const addy = yield lightning.newAddress(lightning.NESTED_PUBKEY_HASH);
const acc = {
date: new Date(),
pubkey: sender_pub_key,
onchainAddress: addy,
amount: 0,
sourceApp: q.app,
status: constants_1.default.statuses.pending,
error: '',
};
yield models_1.models.Accounting.create(acc);
result = addy;
}
const ret = {
type: q.type,
uuid: q.uuid,
app: q.app,
result,
};
const opts = {
amt: constants_1.default.min_sat_amount,
dest: sender_pub_key,
data: {
type: constants_1.default.message_types.query_response,
message: {
content: JSON.stringify(ret)
},
sender: { pub_key: owner.publicKey }
}
};
try {
yield network.signAndSend(opts);
}
catch (e) {
console.log("FAILED TO SEND QUERY_RESPONSE");
return;
}
});
exports.receiveQueryResponse = (payload) => __awaiter(void 0, void 0, void 0, function* () {
console.log('=> receiveQueryResponse');
const dat = payload.content || payload;
// const sender_pub_key = dat.sender.pub_key
const content = dat.message.content;
try {
const q = JSON.parse(content);
queries[q.uuid] = q;
}
catch (e) {
console.log("=> ERROR receiveQueryResponse,", e);
}
});
//# sourceMappingURL=queries.js.map

1
dist/src/controllers/queries.js.map

@ -0,0 +1 @@
{"version":3,"file":"queries.js","s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E,EAAE;IACpD,OAAO,CAAC,GAAG,CAAC,yBAAyB,CAAC,CAAA;IACtC,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO,IAAI,OAAO,CAAA;IACtC,4CAA4C;IAC5C,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,CAAC,OAAO,CAAA;IACnC,IAAI;QACF,MAAM,CAAC,GAAS,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QACnC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;KACpB;IAAC,OAAM,CAAC,EAAE;QACT,OAAO,CAAC,GAAG,CAAC,gCAAgC,EAAC,CAAC,CAAC,CAAA;KAChD;AACH,CAAC,CAAA,CAAA"}

3
dist/src/models/index.js

@ -13,9 +13,10 @@ const timer_1 = require("./ts/timer");
const bot_1 = require("./ts/bot");
const chatBot_1 = require("./ts/chatBot");
const botMember_1 = require("./ts/botMember");
const accounting_1 = require("./ts/accounting");
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '../../config/config.json'))[env];
const sequelize = new sequelize_typescript_1.Sequelize(Object.assign(Object.assign({}, config), { logging: process.env.SQL_LOG === 'true' ? console.log : false, models: [chat_1.default, contact_1.default, invite_1.default, message_1.default, subscription_1.default, mediaKey_1.default, chatMember_1.default, timer_1.default, bot_1.default, chatBot_1.default, botMember_1.default] }));
const sequelize = new sequelize_typescript_1.Sequelize(Object.assign(Object.assign({}, config), { logging: process.env.SQL_LOG === 'true' ? console.log : false, models: [chat_1.default, contact_1.default, invite_1.default, message_1.default, subscription_1.default, mediaKey_1.default, chatMember_1.default, timer_1.default, bot_1.default, chatBot_1.default, botMember_1.default, accounting_1.default] }));
exports.sequelize = sequelize;
const models = sequelize.models;
exports.models = models;

2
dist/src/models/index.js.map

@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/models/index.ts"],"names":[],"mappings":";;AAAA,+DAAiD;AACjD,6BAA4B;AAC5B,oCAA4B;AAC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E7E,MAAM,SAAS,GAAG,IAAI,gCAAS,iCAC1B,MAAM,KACT,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,OAAO,KAAK,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,EAC7D,MAAM,EAAE,CAAC,cAAI,EAAE,iBAAO,EAAE,gBAAM,EAAE,iBAAO,EAAE,sBAAY,EAAE,kBAAQ,EAAE,oBAAU,EAAE,eAAK,EAAE,aAAG,EAAE,iBAAO,EAAE,mBAAS,EAAE,oBAAU,CAAC,IACxH,CAAA;AAIA,8BAAS;AAHX,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,CAAA;AAI7B,wBAAM"}

60
dist/src/models/ts/accounting.js

@ -0,0 +1,60 @@
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const sequelize_typescript_1 = require("sequelize-typescript");
let Accounting = class Accounting extends sequelize_typescript_1.Model {
};
__decorate([
sequelize_typescript_1.Column({
type: sequelize_typescript_1.DataType.BIGINT,
primaryKey: true,
unique: true,
autoIncrement: true
}),
__metadata("design:type", Number)
], Accounting.prototype, "id", void 0);
__decorate([
sequelize_typescript_1.Column,
__metadata("design:type", Date)
], Accounting.prototype, "date", void 0);
__decorate([
sequelize_typescript_1.Column,
__metadata("design:type", String)
], Accounting.prototype, "pubkey", void 0);
__decorate([
sequelize_typescript_1.Column,
__metadata("design:type", String)
], Accounting.prototype, "onchainAddress", void 0);
__decorate([
sequelize_typescript_1.Column(sequelize_typescript_1.DataType.DECIMAL),
__metadata("design:type", Number)
], Accounting.prototype, "amount", void 0);
__decorate([
sequelize_typescript_1.Column,
__metadata("design:type", String)
], Accounting.prototype, "sourceApp", void 0);
__decorate([
sequelize_typescript_1.Column(sequelize_typescript_1.DataType.BIGINT),
__metadata("design:type", Number)
], Accounting.prototype, "status", void 0);
__decorate([
sequelize_typescript_1.Column,
__metadata("design:type", String)
], Accounting.prototype, "error", void 0);
__decorate([
sequelize_typescript_1.Column(sequelize_typescript_1.DataType.BIGINT),
__metadata("design:type", Number)
], Accounting.prototype, "chanId", void 0);
Accounting = __decorate([
sequelize_typescript_1.Table({ tableName: 'sphinx_accountings', underscored: true })
], Accounting);
exports.default = Accounting;
//# sourceMappingURL=accounting.js.map

1
dist/src/models/ts/accounting.js.map

@ -0,0 +1 @@
{"version":3,"file":"accounting.js","sourceRoot":"","sources":["../../../../src/models/ts/accounting.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,+DAAsE;AAGtE,IAAqB,UAAU,GAA/B,MAAqB,UAAW,SAAQ,4BAAiB;CAkCxD,CAAA;AA1BC;IANC,6BAAM,CAAC;QACN,IAAI,EAAE,+BAAQ,CAAC,MAAM;QACrB,UAAU,EAAE,IAAI;QAChB,MAAM,EAAE,IAAI;QACZ,aAAa,EAAE,IAAI;KACpB,CAAC;;sCACQ;AAGV;IADC,6BAAM;8BACD,IAAI;wCAAA;AAGV;IADC,6BAAM;;0CACO;AAGd;IADC,6BAAM;;kDACe;AAGtB;IADC,6BAAM,CAAC,+BAAQ,CAAC,OAAO,CAAC;;0CACX;AAGd;IADC,6BAAM;;6CACU;AAGjB;IADC,6BAAM,CAAC,+BAAQ,CAAC,MAAM,CAAC;;0CACV;AAGd;IADC,6BAAM;;yCACM;AAGb;IADC,6BAAM,CAAC,+BAAQ,CAAC,MAAM,CAAC;;0CACV;AAhCK,UAAU;IAD9B,4BAAK,CAAC,EAAE,SAAS,EAAE,oBAAoB,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC;GACzC,UAAU,CAkC9B;kBAlCoB,UAAU"}

25
dist/src/utils/lightning.js

@ -144,6 +144,31 @@ const queryRoute = (pub_key, amt) => __awaiter(void 0, void 0, void 0, function*
});
});
exports.queryRoute = queryRoute;
exports.WITNESS_PUBKEY_HASH = 0;
exports.NESTED_PUBKEY_HASH = 1;
exports.UNUSED_WITNESS_PUBKEY_HASH = 2;
exports.UNUSED_NESTED_PUBKEY_HASH = 3;
function newAddress(type = exports.NESTED_PUBKEY_HASH) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise(function (resolve, reject) {
return __awaiter(this, void 0, void 0, function* () {
let lightning = yield loadLightning();
lightning.newAddress({ type }, (err, response) => {
if (err) {
reject(err);
return;
}
if (!(response && response.address)) {
reject('no address');
return;
}
resolve(response.address);
});
});
});
});
}
exports.newAddress = newAddress;
const keysend = (opts) => {
return new Promise(function (resolve, reject) {
return __awaiter(this, void 0, void 0, function* () {

2
dist/src/utils/lightning.js.map

File diff suppressed because one or more lines are too long

2
src/constants.ts

@ -57,6 +57,8 @@ const constants = {
"heartbeat_confirmation": 27,
"keysend": 28, // no e2e
"boost": 29,
"query": 30,
"query_response": 31
},
network_types: {
"lightning": 0,

5
src/controllers/index.ts

@ -13,6 +13,7 @@ import * as subcriptions from './subscriptions'
import * as uploads from './uploads'
import * as confirmations from './confirmations'
import * as actions from './api'
import * as queries from './queries'
import { checkTag } from '../utils/gitinfo'
import * as timers from '../utils/timers'
import * as builtInBots from '../builtin'
@ -96,6 +97,8 @@ export async function set(app) {
app.get('/info', details.getNodeInfo)
app.get('/route', details.checkRoute)
app.get('/query/onchain_address/:app', queries.queryOnchainAddress)
app.post('/action', actions.processAction)
app.get('/bots', bots.getBots)
app.post('/bot', bots.createBot)
@ -152,4 +155,6 @@ export const ACTIONS = {
[msgtypes.heartbeat]: confirmations.receiveHeartbeat,
[msgtypes.heartbeat_confirmation]: confirmations.receiveHeartbeatConfirmation,
[msgtypes.boost]: messages.receiveBoost,
[msgtypes.query]: queries.receiveQuery,
[msgtypes.query_response]: queries.receiveQueryResponse,
}

137
src/controllers/queries.ts

@ -0,0 +1,137 @@
import { success, failure } from '../utils/res'
import { models } from '../models'
import * as network from '../network'
import constants from '../constants'
import * as short from 'short-uuid'
import * as lightning from '../utils/lightning'
type QueryType = 'onchain_address'
export interface Query {
type: QueryType
uuid: string
result?: string
app: string
}
let queries: { [k: string]: Query } = {}
const hub_pubkey = '023d70f2f76d283c6c4e58109ee3a2816eb9d8feb40b23d62469060a2b2867b77f'
export async function queryOnchainAddress(req, res) {
console.log('=> queryOnchainAddress')
const uuid = short.generate()
const owner = await models.Contact.findOne({ where: { isOwner: true } })
const app = req.params.app;
const query:Query = {
type:'onchain_address',
uuid,
app
}
const opts = {
amt: constants.min_sat_amount,
dest: hub_pubkey,
data: <network.Msg>{
type: constants.message_types.query,
message: {
content: JSON.stringify(query)
},
sender: { pub_key: owner.publicKey }
}
}
try {
await network.signAndSend(opts)
} catch (e) {
failure(res, e)
return
}
let i = 0
let interval = setInterval(() => {
if (i >= 15) {
clearInterval(interval)
delete queries[uuid]
failure(res, 'no response received')
return
}
if (queries[uuid]) {
success(res, queries[uuid].result)
clearInterval(interval)
delete queries[uuid]
return
}
i++
}, 1000)
}
export const receiveQuery = async (payload) => {
const dat = payload.content || payload
const sender_pub_key = dat.sender.pub_key
const content = dat.message.content
const owner = await models.Contact.findOne({ where: { isOwner: true } })
if(!sender_pub_key || !content || !owner) {
return console.log('=> wrong query format')
}
let q:Query
try {
q = JSON.parse(content)
} catch(e) {
console.log("=> ERROR receiveQuery,",e)
return
}
console.log('=> query received', q)
let result = ''
switch (q.type) {
case 'onchain_address':
const addy = await lightning.newAddress(lightning.NESTED_PUBKEY_HASH)
const acc = {
date: new Date(),
pubkey: sender_pub_key,
onchainAddress: addy,
amount: 0,
sourceApp: q.app,
status:constants.statuses.pending,
error:'',
}
await models.Accounting.create(acc)
result = addy
}
const ret:Query = {
type: q.type,
uuid: q.uuid,
app: q.app,
result,
}
const opts = {
amt: constants.min_sat_amount,
dest: sender_pub_key,
data: <network.Msg>{
type: constants.message_types.query_response,
message: {
content: JSON.stringify(ret)
},
sender: { pub_key: owner.publicKey }
}
}
try {
await network.signAndSend(opts)
} catch (e) {
console.log("FAILED TO SEND QUERY_RESPONSE")
return
}
}
export const receiveQueryResponse = async (payload) => {
console.log('=> receiveQueryResponse')
const dat = payload.content || payload
// const sender_pub_key = dat.sender.pub_key
const content = dat.message.content
try {
const q:Query = JSON.parse(content)
queries[q.uuid] = q
} catch(e) {
console.log("=> ERROR receiveQueryResponse,",e)
}
}

3
src/models/index.ts

@ -11,6 +11,7 @@ import Timer from './ts/timer'
import Bot from './ts/bot'
import ChatBot from './ts/chatBot'
import BotMember from './ts/botMember'
import Accounting from './ts/accounting'
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '../../config/config.json'))[env]
@ -18,7 +19,7 @@ const config = require(path.join(__dirname, '../../config/config.json'))[env]
const sequelize = new Sequelize({
...config,
logging: process.env.SQL_LOG === 'true' ? console.log : false,
models: [Chat, Contact, Invite, Message, Subscription, MediaKey, ChatMember, Timer, Bot, ChatBot, BotMember]
models: [Chat, Contact, Invite, Message, Subscription, MediaKey, ChatMember, Timer, Bot, ChatBot, BotMember, Accounting]
})
const models = sequelize.models

38
src/models/ts/accounting.ts

@ -0,0 +1,38 @@
import { Table, Column, Model, DataType } from 'sequelize-typescript';
@Table({ tableName: 'sphinx_accountings', underscored: true })
export default class Accounting extends Model<Accounting> {
@Column({
type: DataType.BIGINT,
primaryKey: true,
unique: true,
autoIncrement: true
})
id: number
@Column
date: Date
@Column
pubkey: string
@Column
onchainAddress: string
@Column(DataType.DECIMAL)
amount: number
@Column
sourceApp: string
@Column(DataType.BIGINT)
status: number
@Column
error: string
@Column(DataType.BIGINT)
chanId: number
}

25
src/utils/lightning.ts

@ -136,6 +136,31 @@ const queryRoute = async (pub_key, amt) => {
})
}
export const WITNESS_PUBKEY_HASH = 0;
export const NESTED_PUBKEY_HASH = 1;
export const UNUSED_WITNESS_PUBKEY_HASH = 2;
export const UNUSED_NESTED_PUBKEY_HASH = 3;
export type NewAddressType = 0|1|2|3
export async function newAddress(type:NewAddressType=NESTED_PUBKEY_HASH): Promise<string> {
return new Promise(async function (resolve, reject) {
let lightning = await loadLightning()
lightning.newAddress(
{ type },
(err, response) => {
if (err) {
reject(err)
return
}
if(!(response && response.address)) {
reject('no address')
return
}
resolve(response.address)
}
)
})
}
const keysend = (opts) => {
return new Promise(async function (resolve, reject) {
let lightning = await loadLightning()

Loading…
Cancel
Save