Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLNRest: add QR format #2294

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions utils/ConnectionFormatUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,48 @@ describe('ConnectionFormatUtils', () => {
});
});
});

describe('processCLNRestConnectUrl', () => {
it('handles plainnet properly - w/o http forced', () => {
expect(
ConnectionFormatUtils.processCLNRestConnectUrl(
'clnrest://8.8.0.0:2056?&rune=OSqc7ixY6F-gjcigBfxtzKUI54uzgFSA6YfBQoWGDV89MA==&protocol=http'
)
).toEqual({
host: 'https://8.8.0.0',
rune: 'OSqc7ixY6F-gjcigBfxtzKUI54uzgFSA6YfBQoWGDV89MA==',
port: '2056',
enableTor: false,
implementation: 'cln-rest'
});
});

it('handles plainnet properly - with http forced', () => {
expect(
ConnectionFormatUtils.processCLNRestConnectUrl(
'clnrest://http://8.8.0.0:2056?&rune=OSqc7ixY6F-gjcigBfxtzKUI54uzgFSA6YfBQoWGDV89MA==&protocol=http'
)
).toEqual({
host: 'http://8.8.0.0',
rune: 'OSqc7ixY6F-gjcigBfxtzKUI54uzgFSA6YfBQoWGDV89MA==',
port: '2056',
enableTor: false,
implementation: 'cln-rest'
});
});

it('handles Tor properly', () => {
expect(
ConnectionFormatUtils.processCLNRestConnectUrl(
'clnrest://http://y7enfk2mdfawf.onion:2056?&rune=OSqc7ixY6F-gjcigBfxtzKUI54uzgFSA6YfBQoWGDV89MA==&protocol=http'
)
).toEqual({
host: 'http://y7enfk2mdfawf.onion',
rune: 'OSqc7ixY6F-gjcigBfxtzKUI54uzgFSA6YfBQoWGDV89MA==',
port: '2056',
enableTor: true,
implementation: 'cln-rest'
});
});
});
});
52 changes: 52 additions & 0 deletions utils/ConnectionFormatUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,58 @@ class ConnectionFormatUtils {
implementation: 'c-lightning-REST'
};
};

processCLNRestConnectUrl = (input: string) => {
let host, port;
const forceHttp = input.includes('clnrest://http://');
const clrConnectionString = forceHttp
? input.replace('clnrest://http://', '')
: input.split('clnrest://')[1];
const params = input.split('?')[1];

const result: any = {};
if (params) {
params.split('&').forEach(function (part) {
// split on only the first = sign
const item = part.split(/=(.*)/s);
result[item[0]] = decodeURIComponent(item[1]);
});
}

// is IPv6
if (input.includes('[')) {
host =
clrConnectionString && clrConnectionString.split(']:')[0] + ']';
port =
clrConnectionString &&
clrConnectionString.split(']:')[1] &&
clrConnectionString.split(']:')[1].split('?')[0];
} else {
host = clrConnectionString && clrConnectionString.split(':')[0];
port =
clrConnectionString &&
clrConnectionString.split(':')[1] &&
clrConnectionString.split(':')[1].split('?')[0];
}
const rune = result.rune;

// prepend https by default
host = host.includes('://')
? host
: forceHttp
? 'http://' + host
: 'https://' + host;

const enableTor: boolean = host.includes('.onion');

return {
host,
port,
rune,
enableTor,
implementation: 'cln-rest'
};
};
}

const connectionFormatUtils = new ConnectionFormatUtils();
Expand Down
27 changes: 27 additions & 0 deletions utils/handleAnything.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,33 @@ const handleAnything = async (
{ cancelable: false }
);
}
} else if (value.includes('clnrest://')) {
if (isClipboardValue) return true;
const { host, port, rune, implementation, enableTor } =
ConnectionFormatUtils.processCLNRestConnectUrl(value);

if (host && port && rune) {
return [
'NodeConfiguration',
{
node: {
host,
port,
rune,
implementation,
enableTor
},
isValid: true
}
];
} else {
Alert.alert(
localeString('general.error'),
localeString('views.LNDConnectConfigQRScanner.error'),
[{ text: localeString('general.ok'), onPress: () => void 0 }],
{ cancelable: false }
);
}
} else if (
value.includes('https://terminal.lightning.engineering#/connect/pair/')
) {
Expand Down
Loading