Module: p2wsh

This module provides functions and constants for the P2WSH address type.

Source:

Members

(static, constant) P2WSH :string

Address type constant for "pay-to-witness-script-hash" or (P2WSH) addresses.

Type:
  • string
Default Value:
  • P2WSH
Source:

Methods

(static) calculateBase(inputsCount, outputsCount) → {Number}

Calculates the size of the fields in a transaction which DO NOT get counted towards witness discount. Calculated as: version bytes (4) + locktime bytes (4) + input_len (1+) + txins (41+) + output_len (1+) + outputs (9+)

Parameters:
Name Type Description
inputsCount *

number of inputs in the tx

outputsCount *

number of outputs in the tx

Source:
Returns:

number of bytes in the tx without witness fields

Type
Number

(static) estimateMultisigP2WSHTransactionVSize(config) → {number}

Estimate the transaction virtual size (vsize) when spending inputs from the same multisig P2WSH address.

Parameters:
Name Type Description
config Object

configuration for the calculation

Properties
Name Type Description
numInputs number

number of m-of-n multisig P2SH inputs

numOutputs number

number of outputs

m number

required signers

n number

total signers

Source:
Returns:

estimated transaction virtual size in bytes

Type
number

(static) getRedeemScriptSize(n) → {Number}

calculates size of redeem script given n pubkeys. Calculation looks like: OP_M (1 byte) + size of each pubkey in redeem script (OP_DATA= 1 byte * N) + pubkey size (33 bytes * N) + OP_N (1 byte) + OP_CHECKMULTISIG (1 byte) => 1 + (1 * N) + (33 * N) + 1 + 1

Parameters:
Name Type Description
n Number

value of n in m-of-n for multisig script

Source:
Returns:

3 + 34 * N

Type
Number

(static) getWitnessSize(m, n) → {Number}

Calculates the value of a multisig witness given m-of-n values Calculation is of the following form: witness_items count (varint 1+) + null_data (1 byte) + size of each signature (1 byte * OP_M) + signatures (73 * M) + redeem script length (1 byte) + redeem script size (4 + 34 * N bytes)

Parameters:
Name Type Description
m Number

value of m in m-of-n for multisig script

n Number

value of n in m-of-n for multisig script

Source:
Returns:

6 + (74 * M) + (34 * N)

Type
Number

(inner) calculateVSize(baseSize, witnessSize) → {Number}

Calculate virtual bytes or "vsize". vsize is equal three times "base size" of a tx w/o witness data, plus the total size of all data, with the final result divided by scaling factor of 4 and round up to the next integer. For example, if a transaction is 200 bytes with new serialization, and becomes 99 bytes with marker, flag, and witness removed, the vsize is (99 * 3 + 200) / 4 = 125 with round up.

Parameters:
Name Type Description
baseSize Number

base size of transaction

witnessSize Number

size of witness fields

Source:
Returns:

virtual size of tx

Type
Number

(inner) txinSize() → {Number}

provides the size of single tx input for a segwit tx (i.e. empty script) Each input field will look like: prevhash (32 bytes) + prevIndex (4) + scriptsig (1) + sequence bytes (4)

Source:
Returns:

41 (always 41 for segwit inputs since script sig is in witness)

Type
Number

(inner) txoutSize(scriptPubkeySizeopt) → {Number}

Returns the approximate size of outputs in tx. Calculated by adding value field (8 bytes), field providing length scriptPubkey and the script pubkey itself

Parameters:
Name Type Attributes Default Description
scriptPubkeySize Number <optional>
34

size of script pubkey. Defaults to 34 which is the size of a P2WSH script pubkey and the largest possible standard

Source:
Returns:

size of tx output (default: 43)

Type
Number