src/encoder.js
function encodeInputs(inputs, add_address_to_previous_output_index) {
    //Initialize buffer and offset
    let offset = 0;
    var buffer = Buffer.allocUnsafe(100000);
    //Write number of inputs
    offset += bufferutils.writeVarInt(buffer, inputs.length, offset);

    inputs.forEach((input, index) => {
        //Write reversed hash
        offset += Buffer.from(input.previous_output.hash, 'hex').reverse().copy(buffer, offset);
        //Index
        offset = buffer.writeUInt32LE(input.previous_output.index, offset);
        if (add_address_to_previous_output_index !== undefined) {
            if (index == add_address_to_previous_output_index) {
                let lockregex = /^\[\ ([a-f0-9]+)\ \]\ numequalverify dup\ hash160\ \[ [a-f0-9]+\ \]\ equalverify\ checksig$/gi;
                if (input.previous_output.script && input.previous_output.script.match(lockregex)) {
                    let locktime = lockregex.exec(input.previous_output.script.match(lockregex)[0])[1];
                    offset = writeScriptLockedPayToPubKeyHash(input.previous_output.address, locktime, buffer, offset);
                } else {
                    if (Script.hasAttenuationModel(input.previous_output.script)) {
                        let params = Script.getAttenuationParams(input.previous_output.script);
                        offset = writeAttenuationScript(params.model, (params.hash !== '0000000000000000000000000000000000000000000000000000000000000000') ? params.hash : undefined, (params.index >= 0) ? params.index : undefined, input.previous_output.address, buffer, offset);
                    } else {
                        if (Script.isP2SH(input.previous_output.script)) {
                            let script_buffer = Buffer.from(input.redeem, 'hex');
                            offset += bufferutils.writeVarInt(buffer, script_buffer.length, offset);
                            offset += script_buffer.copy(buffer, offset);
                        } else {
                            if (input.previous_output.script) {
                                let script_buffer = Script.fromASM(input.previous_output.script).toBuffer()
                                offset += bufferutils.writeVarInt(buffer, script_buffer.length, offset);
                                offset += script_buffer.copy(buffer, offset);
                            } else {
                                offset = writeScriptPayToPubKeyHash(input.previous_output.address, buffer, offset);
                            }
                        }
                    }
                }
            } else {
                offset = buffer.writeUInt8(0, offset);
            }
        } else {
            //input script
            let script_buffer = encodeInputScript(input.script);
            offset += bufferutils.writeVarInt(buffer, script_buffer.length, offset);
            offset += script_buffer.copy(buffer, offset);
        }

        offset = buffer.writeUInt32LE(input.sequence, offset);
    });
    return buffer.slice(0, offset);
}
function encodeInputs(inputs, add_address_to_previous_output_index) {
    //Initialize buffer and offset
    let offset = 0;
    var buffer = Buffer.allocUnsafe(100000);
    //Write number of inputs
    offset += bufferutils.writeVarInt(buffer, inputs.length, offset);

    inputs.forEach((input, index) => {
        //Write reversed hash
        offset += Buffer.from(input.previous_output.hash, 'hex').reverse().copy(buffer, offset);
        //Index
        offset = buffer.writeUInt32LE(input.previous_output.index, offset);
        if (add_address_to_previous_output_index !== undefined) {
            if (index == add_address_to_previous_output_index) {
                let lockregex = /^\[\ ([a-f0-9]+)\ \]\ numequalverify dup\ hash160\ \[ [a-f0-9]+\ \]\ equalverify\ checksig$/gi;
                if (input.previous_output.script && input.previous_output.script.match(lockregex)) {
                    let locktime = lockregex.exec(input.previous_output.script.match(lockregex)[0])[1];
                    offset = writeScriptLockedPayToPubKeyHash(input.previous_output.address, locktime, buffer, offset);
                } else {
                    if (Script.hasAttenuationModel(input.previous_output.script)) {
                        let params = Script.getAttenuationParams(input.previous_output.script);
                        offset = writeAttenuationScript(params.model, (params.hash !== '0000000000000000000000000000000000000000000000000000000000000000') ? params.hash : undefined, (params.index >= 0) ? params.index : undefined, input.previous_output.address, buffer, offset);
                    } else {
                        if (Script.isP2SH(input.previous_output.script)) {
                            let script_buffer = Buffer.from(input.redeem, 'hex');
                            offset += bufferutils.writeVarInt(buffer, script_buffer.length, offset);
                            offset += script_buffer.copy(buffer, offset);
                        } else {
                            if (input.previous_output.script) {
                                let script_buffer = Script.fromASM(input.previous_output.script).toBuffer()
                                offset += bufferutils.writeVarInt(buffer, script_buffer.length, offset);
                                offset += script_buffer.copy(buffer, offset);
                            } else {
                                offset = writeScriptPayToPubKeyHash(input.previous_output.address, buffer, offset);
                            }
                        }
                    }
                }
            } else {
                offset = buffer.writeUInt8(0, offset);
            }
        } else {
            //input script
            let script_buffer = encodeInputScript(input.script);
            offset += bufferutils.writeVarInt(buffer, script_buffer.length, offset);
            offset += script_buffer.copy(buffer, offset);
        }

        offset = buffer.writeUInt32LE(input.sequence, offset);
    });
    return buffer.slice(0, offset);
}
function encodeInputs ( inputs , add_address_to_previous_output_index ) { //Initialize buffer and offset let offset = 0 ; var buffer = Buffer . allocUnsafe ( 100000 ) ; //Write number of inputs offset += bufferutils . writeVarInt ( buffer , inputs . length , offset ) ; inputs . forEach ( ( input , index ) => { //Write reversed hash offset += Buffer . from ( input . previous_output . hash , 'hex' ) . reverse ( ) . copy ( buffer , offset ) ; //Index offset = buffer . writeUInt32LE ( input . previous_output . index , offset ) ; if ( add_address_to_previous_output_index !== undefined ) { if ( index == add_address_to_previous_output_index ) { let lockregex = / ^\[\ ([a-f0-9]+)\ \]\ numequalverify dup\ hash160\ \[ [a-f0-9]+\ \]\ equalverify\ checksig$ / gi ; if ( input . previous_output . script && input . previous_output . script . match ( lockregex ) ) { let locktime = lockregex . exec ( input . previous_output . script . match ( lockregex ) [ 0 ] ) [ 1 ] ; offset = writeScriptLockedPayToPubKeyHash ( input . previous_output . address , locktime , buffer , offset ) ; } else { if ( Script . hasAttenuationModel ( input . previous_output . script ) ) { let params = Script . getAttenuationParams ( input . previous_output . script ) ; offset = writeAttenuationScript ( params . model , ( params . hash !== '0000000000000000000000000000000000000000000000000000000000000000' ) ? params . hash : undefined , ( params . index >= 0 ) ? params . index : undefined , input . previous_output . address , buffer , offset ) ; } else { if ( Script . isP2SH ( input . previous_output . script ) ) { let script_buffer = Buffer . from ( input . redeem , 'hex' ) ; offset += bufferutils . writeVarInt ( buffer , script_buffer . length , offset ) ; offset += script_buffer . copy ( buffer , offset ) ; } else { if ( input . previous_output . script ) { let script_buffer = Script . fromASM ( input . previous_output . script ) . toBuffer ( ) offset += bufferutils . writeVarInt ( buffer , script_buffer . length , offset ) ; offset += script_buffer . copy ( buffer , offset ) ; } else { offset = writeScriptPayToPubKeyHash ( input . previous_output . address , buffer , offset ) ; } } } } } else { offset = buffer . writeUInt8 ( 0 , offset ) ; } } else { //input script let script_buffer = encodeInputScript ( input . script ) ; offset += bufferutils . writeVarInt ( buffer , script_buffer . length , offset ) ; offset += script_buffer . copy ( buffer , offset ) ; } offset = buffer . writeUInt32LE ( input . sequence , offset ) ; } ) ; return buffer . slice ( 0 , offset ) ; }
Encode raw transactions inputs
@param {Array<input>} inputs
@param {Number} add_address_to_previous_output_index (optional) Index of an input thats previous output address should be added (needed for signing).
@returns {Buffer}
@throws {Error}
Encode raw transactions inputs