diff --git a/Navigation.ts b/Navigation.ts index 153dcf3d61..1efb98d2ca 100644 --- a/Navigation.ts +++ b/Navigation.ts @@ -128,6 +128,7 @@ import RestoreChannelBackups from './views/Settings/EmbeddedNode/RestoreChannelB import RawTxHex from './views/RawTxHex'; import CustodialWalletWarning from './views/Settings/CustodialWalletWarning'; +import Lsps1Test from './views/Settings/Lsps1Test'; const AppScenes = { Wallet: { @@ -450,6 +451,9 @@ const AppScenes = { }, CustodialWalletWarning: { screen: CustodialWalletWarning + }, + Lsps1Test: { + screen: Lsps1Test } }; diff --git a/ios/LndMobile/Lnd.swift b/ios/LndMobile/Lnd.swift index 09d9beb676..b420ff4a72 100644 --- a/ios/LndMobile/Lnd.swift +++ b/ios/LndMobile/Lnd.swift @@ -76,7 +76,6 @@ open class Lnd { "InvoicesCancelInvoice": { bytes, cb in LndmobileInvoicesCancelInvoice(bytes, cb) }, "ConnectPeer": { bytes, cb in LndmobileConnectPeer(bytes, cb) }, "SendCustomMessage": { bytes, cb in LndmobileSendCustomMessage(bytes, cb) }, - "SubscribeCustomMessages": { bytes, cb in LndmobileSubscribeCustomMessages(bytes, cb) }, "DecodePayReq": { bytes, cb in LndmobileDecodePayReq(bytes, cb) }, "DescribeGraph": { bytes, cb in LndmobileDescribeGraph(bytes, cb) }, "GetInfo": { bytes, cb in LndmobileGetInfo(bytes, cb) }, @@ -140,6 +139,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)}, diff --git a/lndmobile/index.ts b/lndmobile/index.ts index 5686d4b39c..21be6b91f0 100644 --- a/lndmobile/index.ts +++ b/lndmobile/index.ts @@ -161,9 +161,9 @@ export const connectPeer = async ( * @throws */ export const sendCustomMessage = async ( - peer: Uint8Array | null, - type: number | null, - data: Uint8Array | null + peer: string, + type: number, + data: string ): Promise => { return await sendCommand< lnrpc.ISendCustomMessageRequest, @@ -174,9 +174,9 @@ export const sendCustomMessage = async ( response: lnrpc.SendCustomMessageResponse, method: 'SendCustomMessage', options: { - peer, + peer: Base64Utils.stringToUint8Array(peer), type, - data + data: Base64Utils.stringToUint8Array(data) } }); }; @@ -184,20 +184,17 @@ export const sendCustomMessage = async ( /** * @throws */ -export const subscribeCustomMessages = - async (): Promise => { - const response = await sendCommand< - lnrpc.ISubscribeCustomMessagesRequest, - lnrpc.SubscribeCustomMessagesRequest, - lnrpc.CustomMessage - >({ - request: lnrpc.SubscribeCustomMessagesRequest, - response: lnrpc.CustomMessage, - method: 'SubscribeCustomMessages', - options: {} - }); - return response; - }; +export const subscribeCustomMessages = async (): Promise => { + const response = await sendStreamCommand< + lnrpc.ISubscribeCustomMessagesRequest, + lnrpc.SubscribeCustomMessagesRequest + >({ + request: lnrpc.SubscribeCustomMessagesRequest, + method: 'SubscribeCustomMessages', + options: {} + }); + return response; +}; /** * @throws diff --git a/stores/LSPStore.ts b/stores/LSPStore.ts index 1db3869888..2e0d4475bf 100644 --- a/stores/LSPStore.ts +++ b/stores/LSPStore.ts @@ -269,4 +269,48 @@ 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) => { + resolve(response); + }) + .catch((error: any) => { + this.error = true; + this.error_msg = 'send message error'; + reject(error); + }); + }); + }; + + @action + public subscribeCustomMessages = () => { + return new Promise((resolve, reject) => { + BackendUtils.subscribeCustomMessages() + .then((response: any) => { + console.log('test'); + resolve(response); + }) + .catch((error: any) => { + this.error = true; + this.error_msg = 'subsribw message error'; + reject(error); + }); + }); + }; } diff --git a/views/Settings/Lsps1Test.tsx b/views/Settings/Lsps1Test.tsx new file mode 100644 index 0000000000..65dcc7a47d --- /dev/null +++ b/views/Settings/Lsps1Test.tsx @@ -0,0 +1,123 @@ +import * as React from 'react'; + +import Header from '../../components/Header'; +import Screen from '../../components/Screen'; +import { inject, observer } from 'mobx-react'; + +import LSPStore from '../../stores/LSPStore'; +import ChannelsStore from '../../stores/ChannelsStore'; +import { View } from 'react-native'; +import Base64Utils from '../../utils/Base64Utils'; + +import Button from '../../components/Button'; + +interface Lsps1TestProps { + LSPStore: LSPStore; + ChannelsStore: ChannelsStore; + navigation: any; +} + +interface Lsps1TestState { + peer: string; +} + +@inject('LSPStore', 'ChannelsStore') +@observer +export default class Lsps1Test extends React.Component< + Lsps1TestProps, + Lsps1TestState +> { + state = { + peer: '0203e5b16ebe87b089f22e18752f1f7a66a1bdf77879df8d1c9e8d912dbfb9beb4' + }; + + encodeMesage = (n: any) => Buffer.from(JSON.stringify(n)).toString(); + + subscribeToCustomMessages() { + this.props.LSPStore.subscribeCustomMessages() + .then((response) => { + console.log('Subscribed to custom messages:', response); + }) + .catch((error) => { + console.error('Error subscribing to custom messages:', error); + }); + } + + sendCustomMessage_lsps1() { + const peer = this.state.peer; + const type = 37913; + const data = this.encodeMesage({ + jsonrpc: '2.0', + method: 'lsps1.get_info', + params: {}, + id: '42' + }); + + this.props.LSPStore.sendCustomMessage({ peer, type, data }) + .then((response) => { + console.log('Custom message sent:', response); + }) + .catch((error) => { + // Handle errors + console.error('Error sending custom message:', error); + }); + } + + sendCustomMessage_lsps0() { + const peer = this.state.peer; + const type = 37913; + const data = this.encodeMesage({ + method: 'lsps0.list_protocols', + jsonrpc: '2.0', + id: 'example#3cad6a54d302edba4c9ade2f7ffac098', + params: {} + }); + + this.props.LSPStore.sendCustomMessage({ peer, type, data }) + .then((response) => { + console.log('Custom message sent:', response); + }) + .catch((error) => { + // Handle errors + console.error('Error sending custom message:', error); + }); + } + + connectPeer() { + const { ChannelsStore } = this.props; + const peer = + '0203e5b16ebe87b089f22e18752f1f7a66a1bdf77879df8d1c9e8d912dbfb9beb4@150.136.37.44:9735'; + const [node_pubkey_string, host] = peer.split('@'); + ChannelsStore.connectPeer({ node_pubkey_string, host }, true, true); + } + + render() { + const { navigation } = this.props; + return ( + +
+ +