@node-lightning/wire
Version:
Lightning Network Wire Protocol
72 lines • 1.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.fundingScript = void 0;
const bufio_1 = require("@node-lightning/bufio");
const crypto_1 = require("@node-lightning/crypto");
const OP_0 = 0;
const OP_CHECKMULTISIG = 174;
function fundingScript(pubkeys) {
const lower = lesser(pubkeys[0], pubkeys[1]);
let node1;
let node2;
if (lower === -1) {
node1 = pubkeys[0];
node2 = pubkeys[1];
}
else {
node1 = pubkeys[1];
node2 = pubkeys[0];
}
const script = p2msScript(2, 2, [node1, node2]);
const hash = crypto_1.sha256(script);
return p2wshScript(hash);
}
exports.fundingScript = fundingScript;
function lesser(a, b) {
for (let i = 0; i < a.length; i++) {
if (a[i] < b[i])
return -1;
if (a[i] > b[i])
return 1;
}
return 0;
}
function compileScript(chunks) {
let bufferSize = 0;
for (const chunk of chunks) {
if (Buffer.isBuffer(chunk)) {
bufferSize += 1; // length
bufferSize += chunk.length; // chunk length < 76 bytes
}
else {
bufferSize += 1; // opcode
}
}
const buffer = Buffer.alloc(bufferSize);
const writer = new bufio_1.BufferWriter(buffer);
for (const chunk of chunks) {
if (Buffer.isBuffer(chunk)) {
writer.writeUInt8(chunk.length); // chunk length < 76 bytes
writer.writeBytes(chunk);
}
else {
writer.writeUInt8(chunk);
}
}
return buffer;
}
function p2msScript(m, n, pubkeys) {
return compileScript([
80 + m,
...pubkeys,
80 + n,
OP_CHECKMULTISIG,
]); // prettier-ignore
}
function p2wshScript(hash160Script) {
return compileScript([
OP_0,
hash160Script,
]); // prettier-ignore
}
//# sourceMappingURL=ScriptUtils.js.map