UNPKG

@secux/app-btc

Version:
18 lines (15 loc) 4.88 kB
"use strict"; /*! Copyright 2022 SecuX Technology Inc Copyright Chen Wei-En Copyright Wu Tsung-Yu Licensed under the Apache License, Version 2.0 (the License); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */Object.defineProperty(exports,"__esModule",{value:!0}),exports.toStack=exports.encode=exports.decode=exports.decompile=exports.compile=void 0;const bip66=require("bip66"),coindef_1=require("./coindef"),scriptNumber=require("./script_number"),utility_1=require("@secux/utility"),ZERO=Buffer.alloc(1,0),logger=null===utility_1.Logger||void 0===utility_1.Logger?void 0:utility_1.Logger.child({id:"script"});function decompile(buffer){const chunks=[];let i=0;for(;i<buffer.length;){const opcode=buffer[i];if(opcode>coindef_1.OPCODES.OP_0&&opcode<=coindef_1.OPCODES.OP_PUSHDATA4){const d=pushdata.decode(buffer,i);if(null===d)return null==logger||logger.warn(`decompile error: reading a pushDataInt fail, got ${buffer.toString("binary")}, index:${i}`),[];if(i+=d.size,i+d.number>buffer.length)return null==logger||logger.warn(`decompile error: attempt to read too much data, got ${buffer.slice(i).toString("binary")}, desired length:${d.number}`),[];const data=buffer.slice(i,i+d.number);i+=d.number;const op=asMinimalOP(data);void 0!==op?chunks.push(op):chunks.push(data)}else chunks.push(opcode),i+=1}return chunks}function toDER(x){let i=0;for(;0===x[i];)++i;return i===x.length?ZERO:128&(x=x.slice(i))[0]?Buffer.concat([ZERO,x],1+x.length):x}function fromDER(x){0===x[0]&&(x=x.slice(1));const buffer=Buffer.alloc(32,0),bstart=Math.max(0,32-x.length);return x.copy(buffer,bstart),buffer}function asMinimalOP(buffer){return 0===buffer.length?coindef_1.OPCODES.OP_0:1===buffer.length?buffer[0]>=1&&buffer[0]<=16?coindef_1.OPCODES.OP_INT_BASE+buffer[0]:129===buffer[0]?coindef_1.OPCODES.OP_1NEGATE:void 0:void 0}exports.compile=function(chunks){const bufferSize=chunks.reduce(((accum,chunk)=>Buffer.isBuffer(chunk)?1===chunk.length&&void 0!==asMinimalOP(chunk)?accum+1:accum+pushdata.encodingLength(chunk.length)+chunk.length:accum+1),0),buffer=Buffer.allocUnsafe(bufferSize);let offset=0;for(const chunk of chunks)if(Buffer.isBuffer(chunk)){const opcode=asMinimalOP(chunk);if(void 0!==opcode){buffer.writeUInt8(opcode,offset),offset+=1;continue}offset+=pushdata.encode(buffer,chunk.length,offset),chunk.copy(buffer,offset),offset+=chunk.length}else buffer.writeUInt8(chunk,offset),offset+=1;if(offset!==buffer.length)throw new Error("Could not decode chunks");return buffer},exports.decompile=decompile,exports.decode=function(buffer){const hashType=buffer.readUInt8(buffer.length-1),hashTypeMod=-129&hashType;if(hashTypeMod<=0||hashTypeMod>=4)throw new Error("Invalid hashType "+hashType);const decoded=bip66.decode(buffer.slice(0,-1)),r=fromDER(decoded.r),s=fromDER(decoded.s);return{signature:Buffer.concat([r,s],64),hashType}},exports.encode=function(signature,hashType){const hashTypeBuffer=Buffer.allocUnsafe(1);hashTypeBuffer.writeUInt8(hashType,0);const r=toDER(signature.slice(0,32)),s=toDER(signature.slice(32,64));return Buffer.concat([bip66.encode(r,s),hashTypeBuffer])},exports.toStack=function(chunks){return Buffer.isBuffer(chunks)&&(chunks=decompile(chunks)),chunks.map((op=>Buffer.isBuffer(op)?op:op===coindef_1.OPCODES.OP_0?Buffer.allocUnsafe(0):scriptNumber.encode(op-coindef_1.OPCODES.OP_INT_BASE)))};class pushdata{static encodingLength(i){return i<coindef_1.OPCODES.OP_PUSHDATA1?1:i<=255?2:i<=65535?3:5}static encode(buffer,number,offset){const size=this.encodingLength(number);return 1===size?buffer.writeUInt8(number,offset):2===size?(buffer.writeUInt8(coindef_1.OPCODES.OP_PUSHDATA1,offset),buffer.writeUInt8(number,offset+1)):3===size?(buffer.writeUInt8(coindef_1.OPCODES.OP_PUSHDATA2,offset),buffer.writeUInt16LE(number,offset+1)):(buffer.writeUInt8(coindef_1.OPCODES.OP_PUSHDATA4,offset),buffer.writeUInt32LE(number,offset+1)),size}static decode(buffer,offset){const opcode=buffer.readUInt8(offset);let number,size;if(opcode<coindef_1.OPCODES.OP_PUSHDATA1)number=opcode,size=1;else if(opcode===coindef_1.OPCODES.OP_PUSHDATA1){if(offset+2>buffer.length)return null;number=buffer.readUInt8(offset+1),size=2}else if(opcode===coindef_1.OPCODES.OP_PUSHDATA2){if(offset+3>buffer.length)return null;number=buffer.readUInt16LE(offset+1),size=3}else{if(offset+5>buffer.length)return null;if(opcode!==coindef_1.OPCODES.OP_PUSHDATA4)throw new Error("Unexpected opcode");number=buffer.readUInt32LE(offset+1),size=5}return{opcode,number,size}}}