src/coins/compress.js
function compressScript(script, bw) {
  // Attempt to compress the output scripts.
  // We can _only_ ever compress them if
  // they are serialized as minimaldata, as
  // we need to recreate them when we read
  // them.

  // P2PKH -> 0 | key-hash
  // Saves 5 bytes.
  const pkh = script.getPubkeyhash(true);
  if (pkh) {
    bw.writeU8(0);
    bw.writeBytes(pkh);
    return bw;
  }

  // P2SH -> 1 | script-hash
  // Saves 3 bytes.
  const sh = script.getScripthash();
  if (sh) {
    bw.writeU8(1);
    bw.writeBytes(sh);
    return bw;
  }

  // P2PK -> 2-5 | compressed-key
  // Only works if the key is valid.
  // Saves up to 35 bytes.
  const pk = script.getPubkey(true);
  if (pk) {
    if (publicKeyVerify(pk)) {
      const key = compressKey(pk);
      bw.writeBytes(key);
      return bw;
    }
  }

  // Raw -> varlen + 10 | script
  bw.writeVarint(script.raw.length + COMPRESS_TYPES);
  bw.writeBytes(script.raw);

  return bw;
}
function compressScript(script, bw) {
  // Attempt to compress the output scripts.
  // We can _only_ ever compress them if
  // they are serialized as minimaldata, as
  // we need to recreate them when we read
  // them.

  // P2PKH -> 0 | key-hash
  // Saves 5 bytes.
  const pkh = script.getPubkeyhash(true);
  if (pkh) {
    bw.writeU8(0);
    bw.writeBytes(pkh);
    return bw;
  }

  // P2SH -> 1 | script-hash
  // Saves 3 bytes.
  const sh = script.getScripthash();
  if (sh) {
    bw.writeU8(1);
    bw.writeBytes(sh);
    return bw;
  }

  // P2PK -> 2-5 | compressed-key
  // Only works if the key is valid.
  // Saves up to 35 bytes.
  const pk = script.getPubkey(true);
  if (pk) {
    if (publicKeyVerify(pk)) {
      const key = compressKey(pk);
      bw.writeBytes(key);
      return bw;
    }
  }

  // Raw -> varlen + 10 | script
  bw.writeVarint(script.raw.length + COMPRESS_TYPES);
  bw.writeBytes(script.raw);

  return bw;
}
function compressScript ( script , bw ) { // Attempt to compress the output scripts. // We can _only_ ever compress them if // they are serialized as minimaldata, as // we need to recreate them when we read // them. // P2PKH -> 0 | key-hash // Saves 5 bytes. const pkh = script . getPubkeyhash ( true ) ; if ( pkh ) { bw . writeU8 ( 0 ) ; bw . writeBytes ( pkh ) ; return bw ; } // P2SH -> 1 | script-hash // Saves 3 bytes. const sh = script . getScripthash ( ) ; if ( sh ) { bw . writeU8 ( 1 ) ; bw . writeBytes ( sh ) ; return bw ; } // P2PK -> 2-5 | compressed-key // Only works if the key is valid. // Saves up to 35 bytes. const pk = script . getPubkey ( true ) ; if ( pk ) { if ( publicKeyVerify ( pk ) ) { const key = compressKey ( pk ) ; bw . writeBytes ( key ) ; return bw ; } } // Raw -> varlen + 10 | script bw . writeVarint ( script . raw . length + COMPRESS_TYPES ) ; bw . writeBytes ( script . raw ) ; return bw ; }
Compress a script, write directly to the buffer.
@param {Script} script
@param {BufferWriter} bw
Compress a script write directly to the buffer .