From 671283f9241fb7d65a8d32edf1f13a204ef71987 Mon Sep 17 00:00:00 2001 From: Aaron Date: Fri, 11 Aug 2023 08:11:49 +1000 Subject: [PATCH] Add ping send functionality (#39) --- package.json | 11 +++++------ src/crypto.ts | 4 ++-- src/index.ts | 18 ++++++++++++++++++ yarn.lock | 39 ++++++++++++++++----------------------- 4 files changed, 41 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 94e2cb9..a58dc93 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lnmessage", - "version": "0.2.3", + "version": "0.2.3-0.0.1", "description": "Talk to Lightning nodes from your browser", "main": "dist/index.js", "type": "module", @@ -16,20 +16,19 @@ }, "devDependencies": { "@types/node": "^18.14.0", - "@types/secp256k1": "^4.0.3", "@types/ws": "^8.5.4", "@typescript-eslint/eslint-plugin": "^5.27.0", "@typescript-eslint/parser": "^5.27.0", "eslint": "^8.16.0", "eslint-config-prettier": "^8.3.0", "prettier": "^2.6.2", - "typescript": "^4.8.2" + "typescript": "^5.1.6" }, "dependencies": { - "@noble/hashes": "^1.2.0", - "@noble/secp256k1": "^1.7.1", + "@noble/hashes": "^1.3.1", + "@noble/secp256k1": "^2.0.0", "buffer": "^6.0.3", "rxjs": "^7.5.7", - "ws": "^8.12.1" + "ws": "^8.13.0" } } diff --git a/src/crypto.ts b/src/crypto.ts index f707927..964a6a8 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -10,7 +10,7 @@ export function sha256(input: Uint8Array): Buffer { } export function ecdh(pubkey: Uint8Array, privkey: Uint8Array) { - const point = secp256k1.Point.fromHex(secp256k1.getSharedSecret(privkey, pubkey)) + const point = secp256k1.ProjectivePoint.fromHex(secp256k1.getSharedSecret(privkey, pubkey)) return Buffer.from(sha256(point.toRawBytes(true))) } export function hmacHash(key: Buffer, input: Buffer) { @@ -103,7 +103,7 @@ export function createRandomPrivateKey(): string { export function validPublicKey(publicKey: string): boolean { try { - secp256k1.Point.fromHex(publicKey) + secp256k1.ProjectivePoint.fromHex(publicKey) return true } catch (e) { return false diff --git a/src/index.ts b/src/index.ts index c326894..084b127 100644 --- a/src/index.ts +++ b/src/index.ts @@ -89,6 +89,7 @@ class LnMessage { private _messageBuffer: BufferReader private _processingBuffer: boolean private _l: number | null + private _pingTimeout: NodeJS.Timeout | null constructor(options: LnWebSocketOptions) { validateInit(options) @@ -136,6 +137,7 @@ class LnMessage { this._readState = READ_STATE.READY_FOR_LEN this._processingBuffer = false this._l = null + this._pingTimeout = null this.decryptedMsgs$.subscribe((msg) => { this.handleDecryptedMessage(msg) @@ -187,6 +189,8 @@ class LnMessage { this.socket.send(msg) this._handshakeState = HANDSHAKE_STATE.AWAITING_RESPONDER_REPLY } + + this._pingTimeout = setTimeout(this._sendPingMessage.bind(this), 45 * 1000) } this.socket.onclose = async () => { @@ -223,6 +227,16 @@ class LnMessage { ) } + private _sendPingMessage() { + const pongMessage = new PingMessage().serialize() + const ping = this.noise.encryptMessage(pongMessage) + + if (this.socket) { + this._log('info', 'Sending a Ping message') + this.socket.send(ping) + } + } + private queueMessage(event: { data: ArrayBuffer }) { const { data } = event const message = Buffer.from(data) @@ -390,6 +404,10 @@ class LnMessage { } async handleDecryptedMessage(decrypted: Buffer) { + // reset ping timeout + this._pingTimeout && clearTimeout(this._pingTimeout) + this._pingTimeout = setTimeout(this._sendPingMessage.bind(this), 40 * 1000) + try { const reader = new BufferReader(decrypted) const type = reader.readUInt16BE() diff --git a/yarn.lock b/yarn.lock index 7c9b559..45e2076 100644 --- a/yarn.lock +++ b/yarn.lock @@ -41,15 +41,15 @@ resolved "https://registry.yarnpkg.com/@humanwhocodes/object-schema/-/object-schema-1.2.1.tgz#b520529ec21d8e5945a1851dfd1c32e94e39ff45" integrity sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA== -"@noble/hashes@^1.2.0": - version "1.2.0" - resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.2.0.tgz#a3150eeb09cc7ab207ebf6d7b9ad311a9bdbed12" - integrity sha512-FZfhjEDbT5GRswV3C6uvLPHMiVD6lQBmpoX5+eSiPaMTXte/IKqI5dykDxzZB/WBeK/CDuQRBWarPdi3FNY2zQ== +"@noble/hashes@^1.3.1": + version "1.3.1" + resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.3.1.tgz#8831ef002114670c603c458ab8b11328406953a9" + integrity sha512-EbqwksQwz9xDRGfDST86whPBgM65E0OH/pCgqW0GBVzO22bNE+NuIbeTb714+IfSjU3aRk47EUvXIb5bTsenKA== -"@noble/secp256k1@^1.7.1": - version "1.7.1" - resolved "https://registry.yarnpkg.com/@noble/secp256k1/-/secp256k1-1.7.1.tgz#b251c70f824ce3ca7f8dc3df08d58f005cc0507c" - integrity sha512-hOUk6AyBFmqVrv7k5WAw/LpszxVbj9gGN4JRkIX52fdFAj1UA61KXmZDvqVEm+pOyec3+fIeZB02LYa/pWOArw== +"@noble/secp256k1@^2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@noble/secp256k1/-/secp256k1-2.0.0.tgz#c214269d45e0233ad6a8ae5104655453636e253d" + integrity sha512-rUGBd95e2a45rlmFTqQJYEFA4/gdIARFfuTuTqLglz0PZ6AKyzyXsEZZq7UZn8hZsvaBgpCzKKBJizT2cJERXw== "@nodelib/fs.scandir@2.1.5": version "2.1.5" @@ -82,13 +82,6 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-18.14.0.tgz#94c47b9217bbac49d4a67a967fdcdeed89ebb7d0" integrity sha512-5EWrvLmglK+imbCJY0+INViFWUHg1AHel1sq4ZVSfdcNqGy9Edv3UB9IIzzg+xPaUcAgZYcfVs2fBcwDeZzU0A== -"@types/secp256k1@^4.0.3": - version "4.0.3" - resolved "https://registry.yarnpkg.com/@types/secp256k1/-/secp256k1-4.0.3.tgz#1b8e55d8e00f08ee7220b4d59a6abe89c37a901c" - integrity sha512-Da66lEIFeIz9ltsdMZcpQvmrmmoqrfju8pm1BH8WbYjZSwUgCwXLb9C+9XYogwBITnbsSaMdVPb2ekf7TV+03w== - dependencies: - "@types/node" "*" - "@types/ws@^8.5.4": version "8.5.4" resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.4.tgz#bb10e36116d6e570dd943735f86c933c1587b8a5" @@ -912,10 +905,10 @@ type-fest@^0.20.2: resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.20.2.tgz#1bf207f4b28f91583666cb5fbd327887301cd5f4" integrity sha512-Ne+eE4r0/iWnpAxD852z3A+N0Bt5RN//NjJwRd2VFHEmrywxf5vsZlh4R6lixl6B+wz/8d+maTSAkN1FIkI3LQ== -typescript@^4.8.2: - version "4.8.3" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.8.3.tgz#d59344522c4bc464a65a730ac695007fdb66dd88" - integrity sha512-goMHfm00nWPa8UvR/CPSvykqf6dVV8x/dp0c5mFTMTIu0u0FlGWRioyy7Nn0PGAdHxpJZnuO/ut+PpQ8UiHAig== +typescript@^5.1.6: + version "5.1.6" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.1.6.tgz#02f8ac202b6dad2c0dd5e0913745b47a37998274" + integrity sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA== uri-js@^4.2.2: version "4.4.1" @@ -941,10 +934,10 @@ wrappy@1: resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" integrity sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ== -ws@^8.12.1: - version "8.12.1" - resolved "https://registry.yarnpkg.com/ws/-/ws-8.12.1.tgz#c51e583d79140b5e42e39be48c934131942d4a8f" - integrity sha512-1qo+M9Ba+xNhPB+YTWUlK6M17brTut5EXbcBaMRN5pH5dFrXz7lzz1ChFSUq3bOUl8yEvSenhHmYUNJxFzdJew== +ws@^8.13.0: + version "8.13.0" + resolved "https://registry.yarnpkg.com/ws/-/ws-8.13.0.tgz#9a9fb92f93cf41512a0735c8f4dd09b8a1211cd0" + integrity sha512-x9vcZYTrFPC7aSIbj7sRCYo7L/Xb8Iy+pW0ng0wt2vCJv7M9HOMy0UoN3rr+IFC7hb7vXoqS+P9ktyLLLhO+LA== yallist@^4.0.0: version "4.0.0"