Skip to content

Commit

Permalink
Refactor LSPS1 code + wire up lsps7.get_extendable_channels call
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloudis committed Jan 9, 2025
1 parent d800557 commit 1715e01
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 124 deletions.
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
"general.count": "Count",
"general.experimental": "Experimental",
"general.defaultNodeNickname": "My Lightning Node",
"general.retry": "Retry",
"restart.title": "Restart required",
"restart.msg": "ZEUS has to be restarted before the new configuration is applied.",
"restart.msg1": "Would you like to restart now?",
Expand Down
211 changes: 177 additions & 34 deletions stores/LSPStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { action, observable } from 'mobx';
import { action, observable, reaction } from 'mobx';
import ReactNativeBlobUtil from 'react-native-blob-util';
import { v4 as uuidv4 } from 'uuid';

Expand All @@ -20,25 +20,31 @@ import { LndMobileEventEmitter } from '../utils/LndMobileUtils';
import { localeString } from '../utils/LocaleUtils';
import { errorToUserFriendly } from '../utils/ErrorUtils';

const CUSTOM_MESSAGE_TYPE = 37913;
const JSON_RPC_VERSION = '2.0';

export default class LSPStore {
@observable public info: any = {};
@observable public zeroConfFee: number | undefined;
@observable public feeId: string | undefined;
@observable public pubkey: string;
@observable public getInfoId: string;
@observable public createOrderId: string;
@observable public getOrderId: string;
@observable public loading: boolean = true;
@observable public error: boolean = false;
@observable public error_msg: string = '';
@observable public showLspSettings: boolean = false;
@observable public channelAcceptor: any;
@observable public customMessagesSubscriber: any;
@observable public resolvedCustomMessage: boolean;
// LSPS1
@observable public getInfoId: string;
@observable public createOrderId: string;
@observable public getOrderId: string;
@observable public getInfoData: any = {};
@observable public createOrderResponse: any = {};
@observable public getOrderResponse: any = {};

@observable public resolvedCustomMessage: boolean;
// LSPS7
@observable public getExtendableOrdersId: string;
@observable public getExtendableOrdersData: any = {};

settingsStore: SettingsStore;
channelsStore: ChannelsStore;
Expand All @@ -52,6 +58,13 @@ export default class LSPStore {
this.settingsStore = settingsStore;
this.channelsStore = channelsStore;
this.nodeInfoStore = nodeInfoStore;

reaction(
() => this.channelsStore.channels,
() => {
this.getExtendableChannels();
}
);
}

@action
Expand Down Expand Up @@ -88,7 +101,7 @@ export default class LSPStore {
: DEFAULT_LSPS1_PUBKEY_MAINNET;
if (
BackendUtils.supportsLSPS1customMessage() &&
this.getLSPS1Pubkey() == olympusPubkey
this.getLSPSPubkey() == olympusPubkey
) {
return true;
} else if (
Expand All @@ -101,12 +114,12 @@ export default class LSPStore {
return false;
};

getLSPHost = () =>
getFlowHost = () =>
this.nodeInfoStore!.nodeInfo.isTestNet
? this.settingsStore.settings.lspTestnet
: this.settingsStore.settings.lspMainnet;

getLSPS1Pubkey = () =>
getLSPSPubkey = () =>
this.nodeInfoStore!.nodeInfo.isTestNet
? this.settingsStore.settings.lsps1PubkeyTestnet
: this.settingsStore.settings.lsps1PubkeyMainnet;
Expand All @@ -123,12 +136,14 @@ export default class LSPStore {

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

// Flow 2.0

@action
public getLSPInfo = () => {
return new Promise((resolve, reject) => {
ReactNativeBlobUtil.fetch(
'get',
`${this.getLSPHost()}/api/v1/info`,
`${this.getFlowHost()}/api/v1/info`,
{
'Content-Type': 'application/json'
}
Expand Down Expand Up @@ -188,7 +203,7 @@ export default class LSPStore {
return new Promise((resolve, reject) => {
ReactNativeBlobUtil.fetch(
'post',
`${this.getLSPHost()}/api/v1/fee`,
`${this.getFlowHost()}/api/v1/fee`,
settings.lspAccessKey
? {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -297,7 +312,7 @@ export default class LSPStore {
return new Promise((resolve, reject) => {
ReactNativeBlobUtil.fetch(
'post',
`${this.getLSPHost()}/api/v1/proposal`,
`${this.getFlowHost()}/api/v1/proposal`,
settings.lspAccessKey
? {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -338,6 +353,8 @@ export default class LSPStore {
});
};

// LSPS0

@action
public sendCustomMessage = ({
peer,
Expand Down Expand Up @@ -371,8 +388,7 @@ export default class LSPStore {
const peer = Base64Utils.base64ToHex(decoded.peer);
const data = JSON.parse(Base64Utils.base64ToUtf8(decoded.data));

console.log('peer', peer);
console.log('data', data);
console.log('Received custom message', { peer, data });

if (data.id === this.getInfoId) {
this.getInfoData = data;
Expand All @@ -398,6 +414,16 @@ export default class LSPStore {
} else {
this.getOrderResponse = data;
}
} else if (data.id === this.getExtendableOrdersId) {
if (data.error) {
this.error = true;
this.loading = false;
this.error_msg = data?.error?.message
? errorToUserFriendly(data?.error?.message)
: '';
} else {
this.getExtendableOrdersData = data;
}
}
};

Expand Down Expand Up @@ -449,8 +475,10 @@ export default class LSPStore {
}
};

// LSPS1

@action
public getInfoREST = () => {
public lsps1GetInfoREST = () => {
const endpoint = `${this.getLSPS1Rest()}/api/v1/get_info`;

console.log('Fetching data from:', endpoint);
Expand Down Expand Up @@ -480,7 +508,40 @@ export default class LSPStore {
};

@action
public createOrderREST = (state: any) => {
public lsps1GetInfoCustomMessage = () => {
this.loading = true;
this.error = false;
this.error_msg = '';

this.getInfoId = uuidv4();
const method = 'lsps1.get_info';

this.sendCustomMessage({
peer: this.getLSPSPubkey(),
type: CUSTOM_MESSAGE_TYPE,
data: this.encodeMesage({
jsonrpc: JSON_RPC_VERSION,
method,
params: {},
id: this.getInfoId
})
})
.then((response) => {
console.log(
`Response for custom message (${method}) received:`,
response
);
})
.catch((error) => {
console.error(
`Error sending (${method}) custom message:`,
error
);
});
};

@action
public lsps1CreateOrderREST = (state: any) => {
const data = JSON.stringify({
lsp_balance_sat: state.lspBalanceSat.toString(),
client_balance_sat: state.clientBalanceSat.toString(),
Expand Down Expand Up @@ -534,7 +595,53 @@ export default class LSPStore {
};

@action
public getOrderREST(id: string, RESTHost: string) {
public lsps1CreateOrderCustomMessage = (state: any) => {
this.loading = true;
this.error = false;
this.error_msg = '';

this.createOrderId = uuidv4();
const method = 'lsps1.create_order';

this.sendCustomMessage({
peer: this.getLSPSPubkey(),
type: CUSTOM_MESSAGE_TYPE,
data: this.encodeMesage({
jsonrpc: JSON_RPC_VERSION,
method,
params: {
lsp_balance_sat: state.lspBalanceSat.toString(),
client_balance_sat: state.clientBalanceSat.toString(),
required_channel_confirmations: parseInt(
state.requiredChannelConfirmations
),
funding_confirms_within_blocks: parseInt(
state.confirmsWithinBlocks
),
channel_expiry_blocks: state.channelExpiryBlocks,
token: state.token,
refund_onchain_address: state.refundOnchainAddress,
announce_channel: state.announceChannel
},
id: this.createOrderId
})
})
.then((response) => {
console.log(
`Response for custom message (${method}) received:`,
response
);
})
.catch((error) => {
console.error(
`Error sending (${method}) custom message:`,
error
);
});
};

@action
public lsps1GetOrderREST(id: string, RESTHost: string) {
this.loading = true;
const endpoint = `${RESTHost}/api/v1/get_order?order_id=${id}`;

Expand Down Expand Up @@ -562,31 +669,67 @@ export default class LSPStore {
}

@action
public getOrderCustomMessage(orderId: string, peer: string) {
console.log('Requesting LSPS1...');
public lsps1GetOrderCustomMessage(orderId: string, peer: string) {
this.loading = true;
const type = 37913;
const id = uuidv4();
this.getOrderId = id;
const data = this.encodeMesage({
jsonrpc: '2.0',
method: 'lsps1.get_order',
params: {
order_id: orderId
},
id: this.getOrderId
});

this.getOrderId = uuidv4();
const method = 'lsps1.get_order';

this.sendCustomMessage({
peer,
type,
data
type: CUSTOM_MESSAGE_TYPE,
data: this.encodeMesage({
jsonrpc: JSON_RPC_VERSION,
method,
params: {
order_id: orderId
},
id: this.getOrderId
})
})
.then((response) => {
console.log('Custom message sent:', response);
console.log(
`Response for custom message (${method}) received:`,
response
);
})
.catch((error) => {
console.error('Error sending custom message:', error);
console.error(
`Error sending (${method}) custom message:`,
error
);
});
}

// LSPS7

@action
public getExtendableChannels = () => {
this.loading = true;
this.error = false;
this.error_msg = '';

this.getExtendableOrdersId = uuidv4();
const method = 'lsps7.get_extendable_channels';

this.sendCustomMessage({
peer: this.getLSPSPubkey(),
type: CUSTOM_MESSAGE_TYPE,
data: this.encodeMesage({
jsonrpc: JSON_RPC_VERSION,
method,
params: {},
id: this.getExtendableOrdersId
})
})
.then((response) => {
console.log(`Custom message (${method}) sent:`, response);
})
.catch((error) => {
console.error(
`Error sending (${method}) custom message:`,
error
);
});
};
}
4 changes: 2 additions & 2 deletions views/Settings/LSPS1/Order.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ export default class Orders extends React.Component<OrderProps, OrdersState> {
console.log('Order found in storage->', temporaryOrder);

BackendUtils.supportsLSPS1rest()
? LSPStore.getOrderREST(
? LSPStore.lsps1GetOrderREST(
id,
temporaryOrder?.endpoint
)
: LSPStore.getOrderCustomMessage(
: LSPStore.lsps1GetOrderCustomMessage(
id,
temporaryOrder?.peer
);
Expand Down
Loading

0 comments on commit 1715e01

Please sign in to comment.