Support for Litecoin and Dogecoin added

This commit is contained in:
2021-01-17 18:48:47 +01:00
parent fc71aed660
commit 881b350252
14 changed files with 538 additions and 34 deletions

View File

@@ -1,6 +1,7 @@
import { Subscriber } from 'zeromq';
import { invoiceManager, logger, rpcClient } from '../../app';
import * as rpc from 'jayson';
import { invoiceManager, logger } from '../../app';
import { IInvoice } from '../../models/invoice/invoice.interface';
import { BackendProvider, IRawTransaction, ITransaction, ITransactionList } from '../backendProvider';
import { CryptoUnits, PaymentStatus } from '../types';
@@ -8,27 +9,36 @@ import { CryptoUnits, PaymentStatus } from '../types';
export class Provider implements BackendProvider {
private sock: Subscriber;
private rpcClient: rpc.HttpClient;
NAME = 'Bitcoin Core';
DESCRIPTION = 'This provider communicates with the Bitcoin Core application.';
AUTHOR = 'LibrePay Team';
VERSION = '0.1';
CRYPTO = CryptoUnits.BITCOIN;
CRYPTO = [CryptoUnits.BITCOIN];
onEnable() {
this.sock = new Subscriber();
this.sock.connect('tcp://127.0.0.1:29000');
this.sock.subscribe('rawtx');
this.rpcClient = rpc.Client.http({
port: 18332,
auth: 'admin:admin'
});
this.listener();
this.watchConfirmations();
return true;
//logger.info('The Bitcoin Core backend is now available!');
}
async getNewAddress(): Promise<string> {
return new Promise<string>((resolve, reject) => {
rpcClient.request('getnewaddress', ['', 'bech32'], async (err, message) => {
this.rpcClient.request('getnewaddress', ['', 'bech32'], async (err, message) => {
if (err) {
reject(err);
return;
@@ -41,7 +51,7 @@ export class Provider implements BackendProvider {
async getTransaction(txId: string): Promise<ITransaction> {
return new Promise<ITransaction>((resolve, reject) => {
rpcClient.request('gettransaction', [txId], (err, message) => {
this.rpcClient.request('gettransaction', [txId], (err, message) => {
if (err) {
reject(err);
return;
@@ -52,9 +62,9 @@ export class Provider implements BackendProvider {
});
}
async decodeRawTransaction(rawTx: string): Promise<IRawTransaction> {
private async decodeRawTransaction(rawTx: string): Promise<IRawTransaction> {
return new Promise<IRawTransaction>((resolve, reject) => {
rpcClient.request('decoderawtransaction', [rawTx], (err, decoded) => {
this.rpcClient.request('decoderawtransaction', [rawTx], (err, decoded) => {
if (err) {
reject(err);
return;
@@ -72,7 +82,7 @@ export class Provider implements BackendProvider {
commentTo?: string,
subtractFeeFromAmount?: boolean): Promise<string> {
return new Promise<string>((resolve, reject) => {
rpcClient.request('sendtoaddress', [recipient, amount, comment, commentTo, subtractFeeFromAmount], (err, decoded) => {
this.rpcClient.request('sendtoaddress', [recipient, amount, comment, commentTo, subtractFeeFromAmount], (err, decoded) => {
if (err) {
reject(err);
return;
@@ -84,7 +94,6 @@ export class Provider implements BackendProvider {
}
async listener() {
logger.info('Now listing for incoming transaction to any invoices ...');
for await (const [topic, msg] of this.sock) {
const rawtx = msg.toString('hex');
const tx = await this.decodeRawTransaction(rawtx);
@@ -92,7 +101,7 @@ export class Provider implements BackendProvider {
tx.vout.forEach(output => {
// Loop over each output and check if the address of one matches the one of an invoice.
invoiceManager.getPendingInvoices().forEach(async invoice => {
invoiceManager.getPendingInvoices().filter(item => { return item.paymentMethod === CryptoUnits.BITCOIN }).forEach(async invoice => {
if (output.scriptPubKey.addresses === undefined) return; // Sometimes (weird) transaction don't have any addresses
logger.debug(`${output.scriptPubKey.addresses} <-> ${invoice.receiveAddress}`);
@@ -112,7 +121,7 @@ export class Provider implements BackendProvider {
async watchConfirmations() {
setInterval(() => {
invoiceManager.getUnconfirmedTransactions().forEach(async invoice => {
invoiceManager.getUnconfirmedTransactions().filter(item => { return item.paymentMethod === CryptoUnits.BITCOIN }).forEach(async invoice => {
if (invoice.transcationHash.length === 0) return;
const transcation = invoice.transcationHash;
@@ -126,7 +135,7 @@ export class Provider implements BackendProvider {
if (invoice.status === PaymentStatus.DONE || invoice.status === PaymentStatus.CANCELLED) return;
if (invoice.paymentMethod !== CryptoUnits.BITCOIN) return;
rpcClient.request('listreceivedbyaddress', [0, false, false, invoice.receiveAddress], async (err, message) => {
this.rpcClient.request('listreceivedbyaddress', [0, false, false, invoice.receiveAddress], async (err, message) => {
if (err) {
logger.error(`There was an error while getting transcations of address ${invoice.receiveAddress}: ${err.message}`);
return;