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

Support Bitcoin scripts in Taproot constructs #13

Closed lgalabru closed 1 year ago

lgalabru commented 1 year ago

hey @paulmillr! First things first, thank you for this lib and your work in general! I'm trying to use to use micro-btc-signer and noticed that - unless I missed something when using / reading this lib - there's a limitation in the current implementation, when crafting taproot constructs. Per the code, it looks like we can only create CHECKSIGVERIFY based multisigs constructs and I was wondering why. If I try to craft a set of conditions involving a HTLC, the lib is aborting my construct. Reproducer:

const keypairFromSecret = (hexSecretKey: string): Keypair => {
    let secretKey = hex.decode(hexSecretKey);
    let schnorrPublicKey = secp.schnorr.getPublicKey(secretKey);
    return {
        schnorrPublicKey,
        secretKey,
    }
}

let aliceKeyPair = keypairFromSecret("2bd806c97f0e00af1a1fc3328fa763a9269723c8db8fac4f93af71db186d6e90");
let bobKeyPair = keypairFromSecret("81b637d8fcd2c6da6359e6963113a1170de795e4b725b84d1e0b4cfd9ec58ce9");
let internalKeyPair = keypairFromSecret("1229101a0fcf2104e8808dab35661134aa5903867d44deb73ce1c7e4eb925be8");
let preimage = sha256(hex.decode("107661134f21fc7c02223d50ab9eb3600bc3ffc3712423a1e47bb1f9a9dbf55f"))

let scriptAlice = new Uint8Array([0x02, 144, 0x00, btc.OP.CHECKSEQUENCEVERIFY, btc.OP.DROP, 0x20, ...aliceKeyPair.schnorrPublicKey, 0xac]);

let scriptBob = new Uint8Array([btc.OP.SHA256, 0x20, ...preimage, btc.OP.EQUALVERIFY, 0x20, ...bobKeyPair.schnorrPublicKey, 0xac]);

let taprootTree = btc.taprootListToTree([{
    script: scriptAlice,
    leafVersion: 0xc0
  },  {
    script: scriptBob,
    leafVersion: 0xc0 
  }
])

let taprootCommitment = btc.p2tr(internalKeyPair.schnorrPublicKey, taprootTree);
// Execution aborted with
// Error: Reader(): Error: OutScript.encode/tr_ns: wrong element

I could be miss using the lib, please let me know if I'm missing something. Thanks!

paulmillr commented 1 year ago

Fixed. please check the new version