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