compress: minor refactor.

This commit is contained in:
Christopher Jeffrey 2017-07-18 06:04:21 -07:00
parent 723c99d15e
commit 70a2f64de6
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -30,8 +30,6 @@ const EMPTY_BUFFER = Buffer.alloc(0);
*/ */
function compressScript(script, bw) { function compressScript(script, bw) {
let data;
// Attempt to compress the output scripts. // Attempt to compress the output scripts.
// We can _only_ ever compress them if // We can _only_ ever compress them if
// they are serialized as minimaldata, as // they are serialized as minimaldata, as
@ -41,18 +39,18 @@ function compressScript(script, bw) {
// P2PKH -> 0 | key-hash // P2PKH -> 0 | key-hash
// Saves 5 bytes. // Saves 5 bytes.
if (script.isPubkeyhash(true)) { if (script.isPubkeyhash(true)) {
data = script.code[2].data; let hash = script.code[2].data;
bw.writeU8(0); bw.writeU8(0);
bw.writeBytes(data); bw.writeBytes(hash);
return bw; return bw;
} }
// P2SH -> 1 | script-hash // P2SH -> 1 | script-hash
// Saves 3 bytes. // Saves 3 bytes.
if (script.isScripthash()) { if (script.isScripthash()) {
data = script.code[1].data; let hash = script.code[1].data;
bw.writeU8(1); bw.writeU8(1);
bw.writeBytes(data); bw.writeBytes(hash);
return bw; return bw;
} }
@ -60,10 +58,10 @@ function compressScript(script, bw) {
// Only works if the key is valid. // Only works if the key is valid.
// Saves up to 35 bytes. // Saves up to 35 bytes.
if (script.isPubkey(true)) { if (script.isPubkey(true)) {
data = script.code[0].data; let key = script.code[0].data;
if (publicKeyVerify(data)) { if (publicKeyVerify(key)) {
data = compressKey(data); key = compressKey(key);
bw.writeBytes(data); bw.writeBytes(key);
return bw; return bw;
} }
} }
@ -129,7 +127,7 @@ function decompressScript(script, br) {
*/ */
function sizeScript(script) { function sizeScript(script) {
let size, data; let size;
if (script.isPubkeyhash(true)) if (script.isPubkeyhash(true))
return 21; return 21;
@ -138,8 +136,8 @@ function sizeScript(script) {
return 21; return 21;
if (script.isPubkey(true)) { if (script.isPubkey(true)) {
data = script.code[0].data; let key = script.code[0].data;
if (publicKeyVerify(data)) if (publicKeyVerify(key))
return 33; return 33;
} }