paulmillr / scure-btc-signer

Audited & minimal library for creating, signing & decoding Bitcoin transactions.
https://paulmillr.com/noble/#scure
MIT License
151 stars 39 forks source link

Bug: incorrect weight estimation for wph, wsh, and tr transactions #41

Closed mahnunchik closed 1 year ago

mahnunchik commented 1 year ago

Incorrect weight estimation for wph, wsh, and tr transactions.

import * as btc from '@scure/btc-signer';
import assert from 'assert/strict';
import bitcoinjs from 'bitcoinjs-lib';
import { hex } from '@scure/base';
import { secp256k1 } from '@noble/curves/secp256k1';

const privKey = hex.decode('0101010101010101010101010101010101010101010101010101010101010101');
const txP2WPKH = new btc.Transaction();
txP2WPKH.addInput({
  txid: '0101010101010101010101010101010101010101010101010101010101010101',
  index: 0,
  witnessUtxo: {
    amount: 1234567890n,
    script: btc.p2wpkh(secp256k1.getPublicKey(privKey, true)).script,
  },
});

txP2WPKH.addOutputAddress(btc.p2wpkh(secp256k1.getPublicKey(privKey, true)).address, 1n);

txP2WPKH.sign(privKey);
txP2WPKH.finalize();

// 274 !== 438
assert.equal(txP2WPKH.weight, bitcoinjs.Transaction.fromHex(txP2WPKH.hex).weight());
mahnunchik commented 1 year ago

There are tests for weight https://github.com/paulmillr/scure-btc-signer/blob/main/test/bitcoinjs-test/btcjs.test.js#L29 but it seems they are false positive.

mahnunchik commented 1 year ago

Could you please clarify the ETA for the next release?

paulmillr commented 1 year ago

should be coming any minute