Skip to content

Commit e17883f

Browse files
authored
Merge pull request #2275 from synonymdev/nodeid-check
fix: validate node id
2 parents 0d67faa + 1b5a020 commit e17883f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/utils/lightning/index.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { EmitterSubscription } from 'react-native';
22
import Keychain from '@synonymdev/react-native-keychain';
33
import * as bitcoin from 'bitcoinjs-lib';
4+
import ecc from '@bitcoinerlab/secp256k1';
45
import RNFS from 'react-native-fs';
56
import { err, ok, Result } from '@synonymdev/result';
67
import { EPaymentType, TGetAddressHistory } from 'beignet';
@@ -1039,6 +1040,23 @@ export const parseUri = (
10391040
return ok({ publicKey, ip, port });
10401041
};
10411042

1043+
const isValidLightningNodePublicKey = (pubkey: string): boolean => {
1044+
const pubkeyBytes = new Uint8Array(Buffer.from(pubkey, 'hex'));
1045+
if (pubkeyBytes.length !== 33) {
1046+
return false;
1047+
}
1048+
1049+
if (!ecc.isPoint(pubkeyBytes)) {
1050+
return false;
1051+
}
1052+
1053+
if (!ecc.isPointCompressed(pubkeyBytes)) {
1054+
return false;
1055+
}
1056+
1057+
return true;
1058+
};
1059+
10421060
/**
10431061
* Prompt LDK to add a specified peer.
10441062
* @param {string} peer
@@ -1056,6 +1074,10 @@ export const addPeer = async ({
10561074
return err(parsedUri.error.message);
10571075
}
10581076

1077+
if (!isValidLightningNodePublicKey(parsedUri.value.publicKey)) {
1078+
return err(i18n.t('lightning:error_add_msg'));
1079+
}
1080+
10591081
const res = await lm.addPeer({
10601082
pubKey: parsedUri.value.publicKey,
10611083
address: parsedUri.value.ip,

0 commit comments

Comments
 (0)