Skip to content

Commit

Permalink
WIP: lsps...
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamkmr04 committed Apr 3, 2024
1 parent 38e7dfa commit e054f14
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 29 deletions.
4 changes: 4 additions & 0 deletions Navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -450,6 +451,9 @@ const AppScenes = {
},
CustodialWalletWarning: {
screen: CustodialWalletWarning
},
Lsps1Test: {
screen: Lsps1Test
}
};

Expand Down
3 changes: 2 additions & 1 deletion ios/LndMobile/Lnd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand Down Expand Up @@ -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)},
Expand Down
14 changes: 5 additions & 9 deletions lndmobile/LndMobileInjection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,11 @@ export interface ILndMobileInjections {
dest: string;
dest_custom_records?: any;
}) => Promise<lnrpc.Payment>;
sendCustomMessage: ({
peer,
type,
data
}: {
peer: Uint8Array | null;
type: number | null;
data: Uint8Array | null;
}) => Promise<lnrpc.SendCustomMessageResponse>;
sendCustomMessage: (
peer: Uint8Array | null,
type: number | null,
data: Uint8Array | null
) => Promise<lnrpc.SendCustomMessageResponse>;
subscribeCustomMessages: () => Promise<lnrpc.CustomMessage>;
};
channel: {
Expand Down
35 changes: 16 additions & 19 deletions lndmobile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<lnrpc.SendCustomMessageResponse> => {
return await sendCommand<
lnrpc.ISendCustomMessageRequest,
Expand All @@ -174,30 +174,27 @@ export const sendCustomMessage = async (
response: lnrpc.SendCustomMessageResponse,
method: 'SendCustomMessage',
options: {
peer,
peer: Base64Utils.hexToBase64(peer),
type,
data
data: Base64Utils.hexToBase64(data)
}
});
};

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

/**
* @throws
Expand Down
3 changes: 3 additions & 0 deletions stores/ChannelsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ export default class ChannelsStore {
perm
})
.then(() => {
console.log('here in store');
if (!silent) {
this.errorPeerConnect = false;
this.connectingToPeer = false;
Expand All @@ -544,6 +545,8 @@ export default class ChannelsStore {
resolve(true);
})
.catch((error: Error) => {
console.log('here in store-- catch block', error);

if (!silent) {
this.connectingToPeer = false;
this.peerSuccess = false;
Expand Down
45 changes: 45 additions & 0 deletions stores/LSPStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,49 @@ 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 = () => {
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);
});
});
};
}
146 changes: 146 additions & 0 deletions views/Settings/Lsps1Test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
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';

Check failure on line 10 in views/Settings/Lsps1Test.tsx

View workflow job for this annotation

GitHub Actions / lint

'Base64Utils' is defined but never used

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: '028c589131fae8c7e2103326542d568373019b50a9eb376a139a330c8545efb79a@130.44.132.180:9735'
};

encodeMesage = (n: any) => Buffer.from(JSON.stringify(n)).toString('hex');

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 [node_pubkey_string] = peer.split('@');
const type = 37913;
const data = this.encodeMesage({
jsonrpc: '2.0',
method: 'lsps1.get_info',
params: {},
id: '42'
});

this.props.LSPStore.sendCustomMessage({
peer: node_pubkey_string,
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 [node_pubkey_string] = peer.split('@');
const type = 37913;
const data = this.encodeMesage({
method: 'lsps0.list_protocols',
jsonrpc: '2.0',
id: 'example#3cad6a54d302edba4c9ade2f7ffac098',
params: {}
});

this.props.LSPStore.sendCustomMessage({
peer: node_pubkey_string,
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 = this.state.peer;
const [node_pubkey_string, host] = peer.split('@');
try {
console.log('here');
ChannelsStore.connectPeer(
{
node_pubkey_string,
host,
local_funding_amount: ''
},
true,
true
);
} catch (err) {
console.log('here');
console.log(err);
}
}

render() {
const { navigation } = this.props;
return (
<Screen>
<Header leftComponent="Back" navigation={navigation} />
<View>
<Button
title="Subscribe to Custom Messages"
onPress={() => this.subscribeToCustomMessages()}
containerStyle={{ paddingBottom: 20 }}
/>
<Button
title="Send Custom Message lsps1"
onPress={() => this.sendCustomMessage_lsps1()}
containerStyle={{ paddingBottom: 20 }}
/>
<Button
title="Send Custom Message lsps0"
onPress={() => this.sendCustomMessage_lsps0()}
containerStyle={{ paddingBottom: 20 }}
/>
<Button
title="connect peer"
onPress={() => this.connectPeer()}
/>
</View>
</Screen>
);
}
}
33 changes: 33 additions & 0 deletions views/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,39 @@ export default class Settings extends React.Component<
</View>
)}

{selectedNode && (
<View
style={{
backgroundColor: themeColor('secondary'),
width: '90%',
borderRadius: 10,
alignSelf: 'center',
marginVertical: 5
}}
>
<TouchableOpacity
onPress={() => navigation.navigate('Lsps1Test')}
>
<View style={styles.columnField}>
<View style={styles.icon}></View>
<Text
style={{
...styles.columnText,
color: themeColor('text')
}}
>
Test lsps1
</Text>
<View style={styles.ForwardArrow}>
<ForwardIcon
stroke={forwardArrowColor}
/>
</View>
</View>
</TouchableOpacity>
</View>
)}

{selectedNode &&
!BackendUtils.isLNDBased() &&
implementation !== 'lndhub' && (
Expand Down

0 comments on commit e054f14

Please sign in to comment.