Skip to content

Commit

Permalink
WIP: LSPS1
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloudis committed Apr 3, 2024
1 parent e7ee7ef commit e80d2e0
Show file tree
Hide file tree
Showing 11 changed files with 341 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ import RestoreChannelBackups from './views/Settings/EmbeddedNode/RestoreChannelB

import RawTxHex from './views/RawTxHex';

import Lsps1Test from './views/Settings/Lsps1Test';

const AppScenes = {
Wallet: {
screen: Wallet
Expand Down Expand Up @@ -445,6 +447,9 @@ const AppScenes = {
},
SetNodePicture: {
screen: SetNodePicture
},
Lsps1Test: {
screen: Lsps1Test
}
};

Expand Down
7 changes: 6 additions & 1 deletion backends/EmbeddedLND.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const {
listPayments,
getNetworkInfo,
queryRoutes,
lookupInvoice
lookupInvoice,
sendCustomMessage,
subscribeCustomMessages
} = lndMobile.index;
const {
channelBalance,
Expand Down Expand Up @@ -49,6 +51,9 @@ export default class EmbeddedLND extends LND {
data.spend_unconfirmed,
data.send_all
);
sendCustomMessage = async (data: any) =>
await sendCustomMessage(data.peer, data.type, data.data);
subscribeCustomMessages = async () => await subscribeCustomMessages();
getMyNodeInfo = async () => await getInfo();
getNetworkInfo = async () => await getNetworkInfo();
getInvoices = async () => await listInvoices();
Expand Down
8 changes: 8 additions & 0 deletions backends/LND.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,14 @@ export default class LND {
spend_unconfirmed: data.spend_unconfirmed,
send_all: data.send_all
});
sendCustomMessage = (data: any) =>
this.postRequest('/v1/custommessage', {
peer: data.peer,
type: data.type,
data: data.data
});
subscribeCustomMessages = () =>
this.getRequest('/v1/custommessage/subscribe');
getMyNodeInfo = () => this.getRequest('/v1/getinfo');
getInvoices = (data: any) =>
this.getRequest(
Expand Down
14 changes: 14 additions & 0 deletions backends/LightningNodeConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ export default class LightningNodeConnect {
send_all: data.send_all
})
.then((data: lnrpc.SendCoinsResponse) => snakeize(data));
sendCustomMessage = async (data: any) =>
await this.lnc.lnd.lightning
.sendCustomMessage({
peer: data.peer,
type: data.type,
data: data.data
})
.then((data: lnrpc.SendCustomMessageResponse) => snakeize(data));
subscribeCustomMessages = async () =>
await this.lnc.lnd.lightning
.subscribeCustomMessages({})
.then((data: lnrpc.custommessage) => {
snakeize(data);
});
getMyNodeInfo = async () =>
await this.lnc.lnd.lightning
.getInfo({})
Expand Down
3 changes: 3 additions & 0 deletions ios/LndMobile/Lnd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ open class Lnd {
"AddInvoice": { bytes, cb in LndmobileAddInvoice(bytes, cb) },
"InvoicesCancelInvoice": { bytes, cb in LndmobileInvoicesCancelInvoice(bytes, cb) },
"ConnectPeer": { bytes, cb in LndmobileConnectPeer(bytes, cb) },
"SendCustomMessage": { bytes, cb in LndmobileSendCustomMessage(bytes, cb) },
"DecodePayReq": { bytes, cb in LndmobileDecodePayReq(bytes, cb) },
"DescribeGraph": { bytes, cb in LndmobileDescribeGraph(bytes, cb) },
"GetInfo": { bytes, cb in LndmobileGetInfo(bytes, cb) },
Expand Down Expand Up @@ -135,6 +136,8 @@ open class Lnd {
"RouterSendPaymentV2": { req, cb in return LndmobileRouterSendPaymentV2(req, cb) },
"SubscribeState": { req, cb in return LndmobileSubscribeState(req, cb) },
"RouterTrackPaymentV2": { req, cb in return LndmobileRouterTrackPaymentV2(req, cb) },
"SubscribeCustomMessages": { bytes, cb in LndmobileSubscribeCustomMessages(bytes, cb) },

// channel
//
"CloseChannel": { req, cb in return LndmobileCloseChannel(req, cb)},
Expand Down
17 changes: 15 additions & 2 deletions lndmobile/LndMobileInjection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ import {
listPayments,
listInvoices,
subscribeChannelGraph,
sendKeysendPaymentV2
sendKeysendPaymentV2,
sendCustomMessage,
subscribeCustomMessages,
decodeCustomMessage
} from './index';
import {
channelBalance,
Expand Down Expand Up @@ -219,6 +222,13 @@ export interface ILndMobileInjections {
dest: string;
dest_custom_records?: any;
}) => Promise<lnrpc.Payment>;
sendCustomMessage: (
peer: Uint8Array | null,
type: number | null,
data: Uint8Array | null
) => Promise<lnrpc.SendCustomMessageResponse>;
subscribeCustomMessages: () => Promise<lnrpc.CustomMessage>;
decodeCustomMessage: (data: string) => lnrpc.CustomMessage;
};
channel: {
channelBalance: () => Promise<lnrpc.ChannelBalanceResponse>;
Expand Down Expand Up @@ -382,7 +392,10 @@ export default {
listPayments,
listInvoices,
subscribeChannelGraph,
sendKeysendPaymentV2
sendKeysendPaymentV2,
sendCustomMessage,
subscribeCustomMessages,
decodeCustomMessage
},
channel: {
channelBalance,
Expand Down
46 changes: 46 additions & 0 deletions lndmobile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,52 @@ export const connectPeer = async (
});
};

/**
* @throws
*/
export const sendCustomMessage = async (
peer: string,
type: number,
data: string
): Promise<lnrpc.SendCustomMessageResponse> => {
return await sendCommand<
lnrpc.ISendCustomMessageRequest,
lnrpc.SendCustomMessageRequest,
lnrpc.SendCustomMessageResponse
>({
request: lnrpc.SendCustomMessageRequest,
response: lnrpc.SendCustomMessageResponse,
method: 'SendCustomMessage',
options: {
peer: Base64Utils.hexToBase64(peer),
type,
data: Base64Utils.hexToBase64(data)
}
});
};

/**
* @throws
*/
export const subscribeCustomMessages = async (): Promise<string> => {
const response = await sendStreamCommand<
lnrpc.ISubscribeCustomMessagesRequest,
lnrpc.SubscribeCustomMessagesRequest
>({
request: lnrpc.SubscribeCustomMessagesRequest,
method: 'SubscribeCustomMessages',
options: {}
});
return response;
};

export const decodeCustomMessage = (data: string): lnrpc.CustomMessage => {
return decodeStreamResult<lnrpc.CustomMessage>({
response: lnrpc.CustomMessage,
base64Result: data
});
};

/**
* @throws
*/
Expand Down
63 changes: 62 additions & 1 deletion stores/LSPStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ChannelsStore from './ChannelsStore';
import NodeInfoStore from './NodeInfoStore';

import lndMobile from '../lndmobile/LndMobileInjection';
const { channel } = lndMobile;
const { index, channel } = lndMobile;

import BackendUtils from '../utils/BackendUtils';
import Base64Utils from '../utils/Base64Utils';
Expand All @@ -21,6 +21,7 @@ export default class LSPStore {
@observable public error_msg: string = '';
@observable public showLspSettings: boolean = false;
@observable public channelAcceptor: any;
@observable public customMessagesSubscriber: any;

settingsStore: SettingsStore;
channelsStore: ChannelsStore;
Expand All @@ -44,6 +45,7 @@ export default class LSPStore {
this.error_msg = '';
this.showLspSettings = false;
this.channelAcceptor = undefined;
this.customMessagesSubscriber = undefined;
};

@action
Expand Down Expand Up @@ -269,4 +271,63 @@ export default class LSPStore {
});
});
};

@action
public sendCustomMessage = ({
peer,
type,
data
}: {
peer: string;
type: number | null;
data: string;
}) => {
return new Promise((resolve, reject) => {
if (!peer || !type || !data) {
reject('Invalid parameters for custom message.');
return;
}

BackendUtils.sendCustomMessage({ peer, type, data })
.then((response: any) => {
console.log(response);
resolve(response);
})
.catch((error: any) => {
this.error = true;
this.error_msg = 'send message error';
reject(error);
});
});
};

@action
public subscribeCustomMessages = async () => {
if (this.customMessagesSubscriber) return;
if (this.settingsStore.implementation === 'embedded-lnd') {
this.customMessagesSubscriber = LndMobileEventEmitter.addListener(
'SubscribeCustomMessages',
async (event: any) => {
try {
const decoded = index.decodeCustomMessage(event.data);
const peer = Base64Utils.base64ToHex(decoded.peer);
const data = JSON.parse(
Base64Utils.base64ToUtf8(decoded.data)
);

console.log('peer', peer);
console.log('data', data);
} catch (error: any) {
console.error(
'sub custom messages error: ' + error.message
);
}
}
);

await index.subscribeCustomMessages();
} else {
BackendUtils.subscribeCustomMessages();
}
};
}
4 changes: 4 additions & 0 deletions utils/BackendUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class BackendUtils {
getLightningBalance = (...args: any[]) =>
this.call('getLightningBalance', args);
sendCoins = (...args: any[]) => this.call('sendCoins', args);
sendCustomMessage = (...args: any[]) =>
this.call('sendCustomMessage', args);
subscribeCustomMessages = (...args: any[]) =>
this.call('subscribeCustomMessages', args);
getMyNodeInfo = (...args: any[]) => this.call('getMyNodeInfo', args);
getNetworkInfo = (...args: any[]) => this.call('getNetworkInfo', args);
getInvoices = (...args: any[]) => this.call('getInvoices', args);
Expand Down
Loading

0 comments on commit e80d2e0

Please sign in to comment.