|
| 1 | +import { BufferReader, BufferWriter } from "@node-lightning/bufio"; |
| 2 | +import { MessageType } from "../MessageType"; |
| 3 | +import { ChannelId } from "@node-lightning/core"; |
| 4 | +import { IWireMessage } from "./IWireMessage"; |
| 5 | + |
| 6 | +/** |
| 7 | + * ShutdownMessage represents the `shutdown` message defined in BOLT #2 of the Lightning |
| 8 | + * Specification. This message can be sent by either node. The scriptPubKey must be a valid P2WPKH, |
| 9 | + * P2WSH, P2SH-P2WPKH, P2SH-P2WSH, or any valid witness script if option_shutdown_anysegwit is |
| 10 | + * negotiated and it should be same as `shutdown_scriptpubkey` value if it was sent during |
| 11 | + * `'open/accept' channel message`. Therefore, if both conditions hold true resulting transaction |
| 12 | + * will propagate to miners. If shutdown is sent by either node, corresponding node should send |
| 13 | + * commitment_signed to commit any outstanding changes before replying shutdown. Once shutdown is |
| 14 | + * sent by both nodes no new HTLCs should be added or accepted by the channel. After successful |
| 15 | + * handshake of shutdown message, fee negotiation and signature sending can begin with |
| 16 | + * `closing_signed` message. |
| 17 | + */ |
| 18 | +export class ShutdownMessage implements IWireMessage { |
| 19 | + public static type = MessageType.Shutdown; |
| 20 | + |
| 21 | + /** |
| 22 | + * Deserializes a shutdown message |
| 23 | + * @param buf |
| 24 | + */ |
| 25 | + public static deserialize(buf: Buffer): ShutdownMessage { |
| 26 | + const instance = new ShutdownMessage(); |
| 27 | + const reader = new BufferReader(buf); |
| 28 | + |
| 29 | + reader.readUInt16BE(); // read type |
| 30 | + instance.channelId = new ChannelId(reader.readBytes(32)); |
| 31 | + const len = reader.readUInt16BE(); |
| 32 | + instance.scriptPubKey = reader.readBytes(len); |
| 33 | + |
| 34 | + return instance; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * The type for message. Shutdown = 38 |
| 39 | + */ |
| 40 | + public readonly type: MessageType = ShutdownMessage.type; |
| 41 | + |
| 42 | + /** |
| 43 | + * ChannelId generated from the funding transactions outpoint. |
| 44 | + */ |
| 45 | + public channelId: ChannelId; |
| 46 | + |
| 47 | + /** |
| 48 | + * scriptPubKey is used by the sender to get paid, it must be a valid P2WPKH, P2WSH, P2SH-P2WPKH, |
| 49 | + * P2SH-P2WSH, or any valid witness script if option_shutdown_anysegwit is negotiated and it |
| 50 | + * should be same as `shutdown_scriptpubkey` value if it was sent during `'open/accept' channel |
| 51 | + * message`. |
| 52 | + */ |
| 53 | + public scriptPubKey: Buffer; |
| 54 | + |
| 55 | + /** |
| 56 | + * Serializes the message into a Buffer |
| 57 | + */ |
| 58 | + public serialize(): Buffer { |
| 59 | + const writer = new BufferWriter(); |
| 60 | + writer.writeUInt16BE(this.type); |
| 61 | + writer.writeBytes(this.channelId.toBuffer()); |
| 62 | + writer.writeUInt16BE(this.scriptPubKey.length); |
| 63 | + writer.writeBytes(this.scriptPubKey); |
| 64 | + return writer.toBuffer(); |
| 65 | + } |
| 66 | +} |
0 commit comments