lightningnetwork / lnd

Lightning Network Daemon ⚡️
MIT License
7.65k stars 2.07k forks source link

Requesting a partial signature on a vanilla Taproot funded PSBT results in panic #6567

Closed alexbosworth closed 2 years ago

alexbosworth commented 2 years ago

Background

When calling SignPsbt on a PSBT that was created referencing a P2TR UTXO but absent Taproot specific fields, LND will terminate with ?panic: runtime error: invalid memory address or nil pointer dereference

Your environment

Steps to reproduce

Expected behavior

Actual behavior

?panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x2f4b48]

goroutine 1224 [running]:
github.com/btcsuite/btcd/txscript.NewTxSigHashes(0x4002338080, {0x19be168, 0x4000120ee8})
    github.com/btcsuite/btcd@v0.22.0-beta.0.20220413172512-bf64c8bdbbbf/txscript/hashcache.go:243 +0x158
github.com/lightningnetwork/lnd/lnwallet/btcwallet.(*BtcWallet).SignPsbt(0x0?, 0x400230f9a0)
    github.com/lightningnetwork/lnd/lnwallet/btcwallet/psbt.go:115 +0x64
github.com/lightningnetwork/lnd/lnrpc/walletrpc.(*WalletKit).SignPsbt(0x400055a4c8, {0x15c4660?, 0x0?}, 0x40014e5f00)
    github.com/lightningnetwork/lnd/lnrpc/walletrpc/walletkit_server.go:1240 +0x1f8
github.com/lightningnetwork/lnd/lnrpc/walletrpc._WalletKit_SignPsbt_Handler.func1({0x19d3c20, 0x4002303f50}, {0x1461500?, 0x40014e5f00})
    github.com/lightningnetwork/lnd/lnrpc/walletrpc/walletkit_grpc.pb.go:993 +0x78
    ithub.com/grpc-ecosystem/go-grpc-prometheus.(*ServerMetrics).UnaryServerInterceptor.func1({0x19d3c20, 0x4002303f50}, {0x1461500, 0x40014e5f00}, 0x4000434bc0?, 0x400277b998)
    github.com/grpc-ecosystem/go-grpc-prometheus@v1.2.0/server_metrics.go:107 +0x78
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1.1({0x19d3c20?, 0x4002303f50?}, {0x1461500?, 0x40014e5f00?})
    github.com/grpc-ecosystem/go-grpc-middleware@v1.3.0/chain.go:25 +0x40
github.com/lightningnetwork/lnd/rpcperms.(*InterceptorChain).middlewareUnaryServerInterceptor.func1({0x19d3c20, 0x4002303f50}, {0x1461500, 0x40014e5f00}, 0x40002a8200, 0x40002a8220)
    github.com/lightningnetwork/lnd/rpcperms/interceptor.go:789 +0x1a0
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1.1({0x19d3c20?, 0x4002303f50?}, {0x1461500?, 0x40014e5f00?})
    github.com/grpc-ecosystem/go-grpc-middleware@v1.3.0/chain.go:25 +0x40
github.com/lightningnetwork/lnd/rpcperms.(*InterceptorChain).MacaroonUnaryServerInterceptor.func1({0x19d3c20, 0x4002303f50}, {0x1461500, 0x40014e5f00}, 0x40014ab320?, 0x40002a8240)
    github.com/lightningnetwork/lnd/rpcperms/interceptor.go:663 +0x94
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1.1({0x19d3c20?, 0x4002303f50?}, {0x1461500?, 0x40014e5f00?})
    github.com/grpc-ecosystem/go-grpc-middleware@v1.3.0/chain.go:25 +0x40
github.com/lightningnetwork/lnd/rpcperms.(*InterceptorChain).rpcStateUnaryServerInterceptor.func1({0x19d3c20, 0x4002303f50}, {0x1461500, 0x40014e5f00}, 0x400149f998?, 0x40002a8260)
    github.com/lightningnetwork/lnd/rpcperms/interceptor.go:753 +0x88
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1.1({0x19d3c20?, 0x4002303f50?}, {0x1461500?, 0x40014e5f00?})
    github.com/grpc-ecosystem/go-grpc-middleware@v1.3.0/chain.go:25 +0x40
github.com/lightningnetwork/lnd/rpcperms.errorLogUnaryServerInterceptor.func1({0x19d3c20?, 0x4002303f50?}, {0x1461500?, 0x40014e5f00?}, 0x40002a8200, 0x40002a8200?)
    github.com/lightningnetwork/lnd/rpcperms/interceptor.go:579 +0x4c
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1.1.1({0x19d3c20?, 0x4002303f50?}, {0x1461500?, 0x40014e5f00?})
    github.com/grpc-ecosystem/go-grpc-middleware@v1.3.0/chain.go:25 +0x40
github.com/grpc-ecosystem/go-grpc-middleware.ChainUnaryServer.func1({0x19d3c20, 0x4002303f50}?, {0x1461500, 0x40014e5f00}, 0x4002a2fab8?, 0x78cd30?)
    github.com/grpc-ecosystem/go-grpc-middleware@v1.3.0/chain.go:34 +0xbc
github.com/lightningnetwork/lnd/lnrpc/walletrpc._WalletKit_SignPsbt_Handler({0x15c4660?, 0x400011d730}, {0x19d3c20, 0x4002303f50}, 0x400087bda0, 0x40002dbf50)
    github.com/lightningnetwork/lnd/lnrpc/walletrpc/walletkit_grpc.pb.go:995 +0x138
google.golang.org/grpc.(*Server).processUnaryRPC(0x4000174c40, {0x19e1008, 0x400056c180}, 0x4002316120, 0x40002f26f0, 0x2707530, 0x0)
    google.golang.org/grpc@v1.38.0/server.go:1286 +0xad4
google.golang.org/grpc.(*Server).handleStream(0x4000174c40, {0x19e1008, 0x400056c180}, 0x4002316120, 0x0)
    google.golang.org/grpc@v1.38.0/server.go:1609 +0x854
google.golang.org/grpc.(*Server).serveStreams.func1.2()
    google.golang.org/grpc@v1.38.0/server.go:934 +0x88
created by google.golang.org/grpc.(*Server).serveStreams.func1
    google.golang.org/grpc@v1.38.0/server.go:932 +0x29c
Roasbeef commented 2 years ago

Looks like we might be missing a check in this routine: https://github.com/lightningnetwork/lnd/blob/master/lnwallet/btcwallet/psbt.go#L221

Roasbeef commented 2 years ago

So it's panicing here: https://github.com/btcsuite/btcd/blob/master/txscript/hashcache.go#L239-L244 because wallet.PsbtPrevOutputFetcher is missing information it needs to populate things.

Roasbeef commented 2 years ago

on a PSBT that was created referencing a P2TR UTXO but absent Taproot specific fields, LND will terminate with

Is it that the taproot specific derivation info wasn't added, or that the base input UTXO fields weren't added?

alexbosworth commented 2 years ago

I think it could be without the base UTXO fields, I have to take another look at how I structured it, I think I tried it with the old bip32 derivation fields though and I need to convert them to the new taproot bip32 derivation fields