bitcoinerlab / descriptors

A TypeScript library for parsing Bitcoin Descriptors, including Miniscript-based ones. Streamlines creating Partially Signed Bitcoin Transactions (PSBTs) from Descriptors. Features BIP32, single-signature, and Hardware Wallet signing capabilities, and facilitates finalizing transactions.
https://bitcoinerlab.com/modules/descriptors
41 stars 14 forks source link

New Weight Calculation Functionalities and General Optimizations in Descriptors #32

Closed landabaso closed 7 months ago

landabaso commented 7 months ago

This Pull Request introduces a series of enhancements and optimizations to the descriptor methods in the @bitcoinerlab/descriptors package. Key updates include the addition of new methods for weight calculation, improvements in memoization, and updates to dependencies and configurations. These changes are aimed at improving the performance and functionality of the module, especially in scenarios involving coinselector algorithms.

Key Changes:

  1. New Descriptor Methods:

    • guessOutput: Identifies the output type (PKH, WPKH, SH) based on a given script.
    • inputWeight: Calculates the Weight Unit contributions of an Output as an input in a transaction.
    • outputWeight: Computes the Weight Unit contributions of an Output as an output in a transaction.
  2. Memoization Implementation:

    • Applied memoization to guessOutput, getSequence, getLockTime, getScriptSatisfaction, inputWeight, and outputWeight for enhanced performance and reduced redundant computations.
  3. Behavioral Changes:

    • The isSegwit() method now assumes addr(SH_TYPE_ADDRESS) descriptors as Segwit SH_WPKH. For non-standard script inputs, the format sh(MINISCRIPT) should be used.
  4. Dependency and Configuration Updates:

    • Updated package version from "2.0.4" to "2.1.0".
    • Modified the build:test script to include --resolveJsonModule.
    • Added lodash.memoize for memoization functionality.
    • Included varuint-bitcoin explicitelly.
    • Enhanced TypeScript configuration to allow importing JSON files, supporting new test fixtures.
  5. Additional Improvements:

    • Fixed formatting issues with Prettier.
    • Utilized explicit varuint-bitcoin functions from the official npm package.
  6. Testing:

    • Added comprehensive tests for the new methods: inputWeight, outputWeight, and guessOutput.

Rationale for Changes: The relocation of guessOutput, inputWeight, and outputWeight from @bitcoinerlab/coinselect to @bitcoinerlab/descriptors aligns these methods more closely with the module's purpose. The implementation of a caching strategy and memoization significantly boosts performance, particularly for algorithms that frequently access these weight calculation methods.

Documentation: