Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ async function connect() {
remoteNodePublicKey: '02df5ffe895c778e10f7742a6c5b8a0cefbe9465df58b92fadeb883752c8107c8f',
// Optional WebSocket proxy endpoint to connect through (see WebSocket Proxy section)
wsProxy: 'wss://<WEBSOCKET_PROXY>',
// Optional TCP Socket to connect through (either use wsProxy OR tcpSocket)
tcpSocket: new net.Socket(),
// The IP address of the node
ip: '35.232.170.67',
// The port of the node, defaults to 9735
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lnmessage",
"version": "0.2.1",
"version": "0.2.1-0.0.1",
"description": "Talk to Lightning nodes from your browser",
"main": "dist/index.js",
"type": "module",
Expand Down
19 changes: 13 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class LnMessage {
}

this.socket.onopen = async () => {
this._log('info', 'WebSocket is connected')
this._log('info', 'WebSocket is connected at ' + new Date().toISOString())
this._log('info', 'Creating Act1 message')

const msg = this.noise.initiatorAct1(Buffer.from(this.remoteNodePublicKey, 'hex'))
Expand All @@ -190,7 +190,7 @@ class LnMessage {
}

this.socket.onclose = async () => {
this._log('error', 'WebSocket is closed')
this._log('error', 'WebSocket is closed at ' + new Date().toISOString())

this.connectionStatus$.next('disconnected')
this.connected$.next(false)
Expand Down Expand Up @@ -287,9 +287,10 @@ class LnMessage {
}
} while (readMore)
} catch (err) {
// Terminate on failures as we won't be able to recovery
// Terminate on failures as we won't be able to recover
// since the noise state has rotated nonce and we won't
// be able to any more data without additional errors.
this._log('error', `Noise state has rotated nonce: ${err}`)
this.disconnect()
}

Expand Down Expand Up @@ -516,10 +517,14 @@ class LnMessage {
// write the id
writer.writeBytes(Buffer.from(reqId, 'hex'))

// Unique request id with prefix, method and id
const detailedReqId = `lnmessage:${method}#${reqId}`

// write the request
writer.writeBytes(
Buffer.from(
JSON.stringify({
id: detailedReqId, // Adding id for easier debugging with commando
rune,
method,
params
Expand All @@ -534,7 +539,7 @@ class LnMessage {
this._log('info', 'Sending commando message')
this.socket.send(message)

this._log('info', 'Message sent and awaiting response')
this._log('info', `Message sent with id ${detailedReqId} and awaiting response`)

const { response } = await firstValueFrom(
this._commandoMsgs$.pipe(filter((commandoMsg) => commandoMsg.id === reqId))
Expand All @@ -545,7 +550,9 @@ class LnMessage {

this._log(
'info',
result ? 'Successful response received' : `Error response received: ${error.message}`
result
? `Successful response received for ID: ${response.id}`
: `Error response received: ${error.message}`
)

if (error) throw error
Expand All @@ -558,7 +565,7 @@ class LnMessage {

_log(level: keyof Logger, msg: string) {
if (this._logger && this._logger[level]) {
this._logger[level](`[${level.toUpperCase()} - ${new Date().toLocaleTimeString()}]: ${msg}`)
this._logger[level](`[${level.toUpperCase()} - ${new Date().toISOString()}]: ${msg}`)
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/noise-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ export class NoiseState {
* Nonce incremented when receiving messages. Initialized to zero in Act3.
*/
public rn: Buffer
/**
* Chaining key for sending. Initialized to ck in Act 3.
*/
public sck: Buffer;
/**
* Chaining key for receiving. Initialized to ck in Act 3.
*/
public rck: Buffer;
/**
* Intermediate key 1. Used to encrypt or decrypt the zero-length AEAD
* payload in the corresponding initiator or receiver act.
Expand Down Expand Up @@ -184,6 +192,7 @@ export class NoiseState {
const tempK3 = hkdf(ss, 64, this.ck)
this.ck = tempK3.subarray(0, 32)
this.tempK3 = Buffer.from(tempK3.subarray(32))
this.rck = this.sck = this.ck;
// 5. t = encryptWithAD(temp_k3, 0, h, zero)
const t = ccpEncrypt(this.tempK3, Buffer.alloc(12), this.h, Buffer.alloc(0))
// 6. sk, rk = hkdf(ck, zero)
Expand Down Expand Up @@ -354,16 +363,16 @@ export class NoiseState {
}

private _rotateSendingKeys() {
const result = hkdf(this.sk, 64, this.ck)
const result = hkdf(this.sk, 64, this.sck)
this.sk = result.subarray(32)
this.ck = result.subarray(0, 32)
this.sck = result.subarray(0, 32)
this.sn = Buffer.alloc(12)
}

private _rotateRecievingKeys() {
const result = hkdf(this.rk, 64, this.ck)
const result = hkdf(this.rk, 64, this.rck)
this.rk = result.subarray(32)
this.ck = result.subarray(0, 32)
this.rck = result.subarray(0, 32)
this.rn = Buffer.alloc(12)
}
}