All files / lib ScriptUtils.ts

97.3% Statements 36/37
100% Branches 10/10
100% Functions 5/5
97.06% Lines 33/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 721x 1x   1x 1x   1x 138x     138x 3x 3x   135x 135x     138x 138x 138x       138x 273x 270x           276x 276x 966x 414x 414x   552x       276x 276x   276x 966x 414x 414x   552x     276x       138x                 138x          
import { BufferWriter } from "@node-lightning/bufio";
import { sha256 } from "@node-lightning/crypto";
 
const OP_0 = 0;
const OP_CHECKMULTISIG = 174;
 
export function fundingScript(pubkeys: Buffer[]) {
    const lower = lesser(pubkeys[0], pubkeys[1]);
    let node1: Buffer;
    let node2: Buffer;
    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 = sha256(script);
    return p2wshScript(hash);
}
 
function lesser(a: Buffer, b: Buffer) {
    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: Array<Buffer | number>): Buffer {
    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 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: number, n: number, pubkeys: Buffer[]): Buffer {
    return compileScript([
    80 + m,
    ...pubkeys,
    80 + n,
    OP_CHECKMULTISIG,
  ]); // prettier-ignore
}
 
function p2wshScript(hash160Script: Buffer): Buffer {
    return compileScript([
    OP_0,
    hash160Script,
  ]); // prettier-ignore
}