vulpemventures / ocean

:ocean: Elements/Liquid wallet daemon
MIT License
5 stars 7 forks source link

panic on SignPset #63

Closed tiero closed 6 months ago

tiero commented 6 months ago

Unsigned TX (from decoderawtransaction)

{
  "txid": "7c40ce676a0e5e4b0880827e033f73ac90e7e95e8a19c15948f686a8ab295bfd",
  "hash": "7c40ce676a0e5e4b0880827e033f73ac90e7e95e8a19c15948f686a8ab295bfd",
  "wtxid": "7c40ce676a0e5e4b0880827e033f73ac90e7e95e8a19c15948f686a8ab295bfd",
  "withash": "afbc43c5c8c011e98d2f50bc484794e0858d9546d5899ec0996fa70f8dcb5d8b",
  "version": 2,
  "size": 398,
  "vsize": 398,
  "weight": 1592,
  "locktime": 0,
  "vin": [
    {
      "txid": "113aca9009dab21a5df6b76552e8d63f737ef29b8e46876c99c94c53eb34938b",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "is_pegin": false,
      "sequence": 4294967295
    },
    {
      "txid": "9be958623a81cebe9b9cea0720d4652f31bd3b585751e30116d3853826cee7e5",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "is_pegin": false,
      "sequence": 4294967295
    },
    {
      "txid": "f17181213d869a2ae99710b83542839016c46b78d31648229625bc08ec4ddedc",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "is_pegin": false,
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 10.00000000,
      "asset": "0d86b2f6a8c3b02a8c7c8836b83a081e68b7e2b4bcdfc58981fc5486f59f7518",
      "commitmentnonce": "",
      "commitmentnonce_fully_valid": false,
      "n": 0,
      "scriptPubKey": {
        "asm": "0 d2d28d3770e6e471fe4cef4dbaba3f9723bedca1",
        "desc": "addr(tex1q6tfg6dmsumj8rljvaaxm4w3lju3mah9p95pz9j)#0yslnpdu",
        "hex": "0014d2d28d3770e6e471fe4cef4dbaba3f9723bedca1",
        "address": "tex1q6tfg6dmsumj8rljvaaxm4w3lju3mah9p95pz9j",
        "type": "witness_v0_keyhash"
      }
    },
    {
      "value": 10.00000000,
      "asset": "f3d1ec678811398cd2ae277cbe3849c6f6dbd72c74bc542f7c4b11ff0e820958",
      "commitmentnonce": "",
      "commitmentnonce_fully_valid": false,
      "n": 1,
      "scriptPubKey": {
        "asm": "0 a255130d64b6205ef8fed24d6e591aedc8c02463",
        "desc": "addr(tex1q5f23xrtykcs9a7876fxkukg6ahyvqfrr7k08f4)#f6alc2cg",
        "hex": "0014a255130d64b6205ef8fed24d6e591aedc8c02463",
        "address": "tex1q5f23xrtykcs9a7876fxkukg6ahyvqfrr7k08f4",
        "type": "witness_v0_keyhash"
      }
    },
    {
      "value": 0.00009500,
      "asset": "0d86b2f6a8c3b02a8c7c8836b83a081e68b7e2b4bcdfc58981fc5486f59f7518",
      "commitmentnonce": "",
      "commitmentnonce_fully_valid": false,
      "n": 2,
      "scriptPubKey": {
        "asm": "0 46e19e789db922c4c5fdc272c3c7d77149613b41",
        "desc": "addr(tex1qgmseu7yahy3vf30acfev837hw9ykzw6pvrse5d)#wvshsftq",
        "hex": "001446e19e789db922c4c5fdc272c3c7d77149613b41",
        "address": "tex1qgmseu7yahy3vf30acfev837hw9ykzw6pvrse5d",
        "type": "witness_v0_keyhash"
      }
    },
    {
      "value": 0.00000500,
      "asset": "0d86b2f6a8c3b02a8c7c8836b83a081e68b7e2b4bcdfc58981fc5486f59f7518",
      "commitmentnonce": "",
      "commitmentnonce_fully_valid": false,
      "n": 3,
      "scriptPubKey": {
        "asm": "0 46e19e789db922c4c5fdc272c3c7d77149613b41",
        "desc": "addr(tex1qgmseu7yahy3vf30acfev837hw9ykzw6pvrse5d)#wvshsftq",
        "hex": "001446e19e789db922c4c5fdc272c3c7d77149613b41",
        "address": "tex1qgmseu7yahy3vf30acfev837hw9ykzw6pvrse5d",
        "type": "witness_v0_keyhash"
      }
    }
  ],
  "fee": {
  }
}

PSET (from decodepsbt)

{
  "global_xpubs": [
  ],
  "tx_version": 2,
  "input_count": 3,
  "output_count": 3,
  "inputs_modifiable": true,
  "outputs_modifiable": true,
  "has_sighash_single": false,
  "psbt_version": 2,
  "proprietary": [
  ],
  "fees": {
    "bitcoin": 0.00000000
  },
  "unknown": {
  },
  "inputs": [
    {
      "previous_txid": "113aca9009dab21a5df6b76552e8d63f737ef29b8e46876c99c94c53eb34938b",
      "previous_vout": 0,
      "sequence": 4294967295,
      "unknown": {
        "15c450929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0d5a2589c1cbc3db20cad2eac522bd5df6eb05bdb481dbf214eefd47e357f9d30": "00d1020233881f676a30c2c1251fd1bc5dc7f9b465b6d80d4c545a4223057edec1f9d62aaef68800ce7521f3d1ec678811398cd2ae277cbe3849c6f6dbd72c74bc542f7c4b11ff0e820958018800cf750800ca9a3b0000000087c4"
      }
    },
    {
      "sighash": "ALL",
      "previous_txid": "9be958623a81cebe9b9cea0720d4652f31bd3b585751e30116d3853826cee7e5",
      "previous_vout": 0,
      "sequence": 4294967295
    },
    {
      "sighash": "ALL",
      "previous_txid": "f17181213d869a2ae99710b83542839016c46b78d31648229625bc08ec4ddedc",
      "previous_vout": 0,
      "sequence": 4294967295
    }
  ],
  "outputs": [
    {
      "amount": 10.00000000,
      "script": {
        "asm": "0 8360d2d3249176a10b498931d8f0b78649a3e200",
        "desc": "addr(tex1qsdsd95eyj9m2zz6f3yca3u9hsey68csqumkpnd)#38tsjf7n",
        "hex": "00148360d2d3249176a10b498931d8f0b78649a3e200",
        "address": "tex1qsdsd95eyj9m2zz6f3yca3u9hsey68csqumkpnd",
        "type": "witness_v0_keyhash"
      },
      "asset": "0d86b2f6a8c3b02a8c7c8836b83a081e68b7e2b4bcdfc58981fc5486f59f7518",
      "blinder_index": 0
    },
    {
      "amount": 10.00000000,
      "script": {
        "asm": "0 f071e1b5cf3f5429a37ef319378346c88645b341",
        "desc": "addr(tex1q7pc7rdw08a2zngm77vvn0q6xezrytv6pd0028h)#rdf55tyd",
        "hex": "0014f071e1b5cf3f5429a37ef319378346c88645b341",
        "address": "tex1q7pc7rdw08a2zngm77vvn0q6xezrytv6pd0028h",
        "type": "witness_v0_keyhash"
      },
      "asset": "f3d1ec678811398cd2ae277cbe3849c6f6dbd72c74bc542f7c4b11ff0e820958",
      "blinder_index": 0
    },
    {
      "amount": 0.00009500,
      "script": {
        "asm": "0 495fbff0bb25b38931a10f01d89b615352b600f3",
        "desc": "addr(tex1qf90mlu9mykecjvdppuqa3xmp2dftvq8n7ac8xg)#5th8mw6g",
        "hex": "0014495fbff0bb25b38931a10f01d89b615352b600f3",
        "address": "tex1qf90mlu9mykecjvdppuqa3xmp2dftvq8n7ac8xg",
        "type": "witness_v0_keyhash"
      },
      "asset": "0d86b2f6a8c3b02a8c7c8836b83a081e68b7e2b4bcdfc58981fc5486f59f7518",
      "blinder_index": 0
    },
    {
      "amount": 0.00000500,
      "script": {
        "asm": "0 495fbff0bb25b38931a10f01d89b615352b600f3",
        "desc": "addr(tex1qf90mlu9mykecjvdppuqa3xmp2dftvq8n7ac8xg)#5th8mw6g",
        "hex": "0014495fbff0bb25b38931a10f01d89b615352b600f3",
        "address": "tex1qf90mlu9mykecjvdppuqa3xmp2dftvq8n7ac8xg",
        "type": "witness_v0_keyhash"
      },
      "asset": "0d86b2f6a8c3b02a8c7c8836b83a081e68b7e2b4bcdfc58981fc5486f59f7518",
      "blinder_index": 0
    }
  ]
}

Logs

OCEAN_NETWORK=testnet OCEAN_NO_TLS=true OCEAN_ELECTRUM_URL=ssl://blockstream.info:465 OCEAN_DB_TYPE=badger OCEAN_UTXO_EXPIRY_DURATION_IN_SECONDS=10 oceand
INFO[0000] profiler: start at url http://localhost:18001/debug/pprof/
INFO[0000] All 3 tables opened in 0s
INFO[0000] Discard stats nextEmptySlot: 0
INFO[0000] Set nextTxnTs to 34
INFO[0000] Deleting empty file: /Users/tiero/Library/Application Support/Oceand/testnet/db/wallet/000005.vlog
INFO[0000] All 3 tables opened in 0s
INFO[0000] Discard stats nextEmptySlot: 0
INFO[0000] Set nextTxnTs to 17
INFO[0000] Deleting empty file: /Users/tiero/Library/Application Support/Oceand/testnet/db/utxos/000005.vlog
INFO[0000] All 2 tables opened in 0s
INFO[0000] Discard stats nextEmptySlot: 0
INFO[0000] Set nextTxnTs to 9
INFO[0000] Deleting empty file: /Users/tiero/Library/Application Support/Oceand/testnet/db/txs/000005.vlog
INFO[0000] service: registered wallet handler on public interface
INFO[0000] service: registered account handler on public interface
INFO[0000] service: registered transaction handler on public interface
INFO[0000] service: registered notification handler on public interface
INFO[0000] service: started blockchain scanner
INFO[0000] service: start listening on :18000
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x30 pc=0x102aa9044]

goroutine 968 [running]:
github.com/vulpemventures/go-elements/psetv2.(*Input).GetUtxo(...)
    /home/runner/go/pkg/mod/github.com/vulpemventures/go-elements@v0.4.5/psetv2/input.go:283
github.com/vulpemventures/ocean/pkg/wallet/single-sig.(*Wallet).SignPset(0x140114f7590?, {{0x140001f4c00?, 0x140116c2d80?}, 0x140114f7560?, 0x3c0?})
    /home/runner/work/ocean/ocean/pkg/wallet/single-sig/sign.go:138 +0x104
github.com/vulpemventures/ocean/internal/core/application.(*TransactionService).SignPset(0x140000f58a8?, {0x1033a0008, 0x1401246cd50}, {0x140001f4c00, 0x3c0}, 0x3204c20?)
    /home/runner/work/ocean/ocean/internal/core/application/transaction_service.go:320 +0x20c
github.com/vulpemventures/ocean/internal/interfaces/grpc/handler.(*transaction).SignPset(0x140000f58d8?, {0x1033a0008?, 0x1401246cd50?}, 0x103204c20?)
    /home/runner/work/ocean/ocean/internal/interfaces/grpc/handler/transaction.go:203 +0xbc
github.com/vulpemventures/ocean/api-spec/protobuf/gen/go/ocean/v1._TransactionService_SignPset_Handler.func1({0x1033a0008, 0x1401246cd50}, {0x1032f6ec0?, 0x1401246ecc0})
    /home/runner/work/ocean/ocean/api-spec/protobuf/gen/go/ocean/v1/transaction_grpc.pb.go:466 +0x74
github.com/vulpemventures/ocean/internal/interfaces/grpc/interceptor.unaryLogger({0x1033a0008, 0x1401246cd50}, {0x1032f6ec0, 0x1401246ecc0}, 0x103a6fbe0?, 0x140114fe3c0)
    /home/runner/work/ocean/ocean/internal/interfaces/grpc/interceptor/logger.go:17 +0x9c
github.com/vulpemventures/ocean/internal/interfaces/grpc.(*service).start.UnaryInterceptor.ChainUnaryServer.func2.1.1({0x1033a0008?, 0x1401246cd50?}, {0x1032f6ec0?, 0x1401246ecc0?})
    /home/runner/go/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.3.0/chain.go:25 +0x40
github.com/vulpemventures/ocean/internal/interfaces/grpc.(*service).start.UnaryInterceptor.ChainUnaryServer.func2({0x1033a0008, 0x1401246cd50}, {0x1032f6ec0, 0x1401246ecc0}, 0x140005f5a38?, 0x102eb2064?)
    /home/runner/go/pkg/mod/github.com/grpc-ecosystem/go-grpc-middleware@v1.3.0/chain.go:34 +0xb8
github.com/vulpemventures/ocean/api-spec/protobuf/gen/go/ocean/v1._TransactionService_SignPset_Handler({0x10334ce20?, 0x140005e8338}, {0x1033a0008, 0x1401246cd50}, 0x140114e95e0, 0x140002e1980)
    /home/runner/work/ocean/ocean/api-spec/protobuf/gen/go/ocean/v1/transaction_grpc.p:468 +0x12c
google.golang.org/grpc.(*Server).processUnaryRPC(0x14000332b40, {0x1033a4d20, 0x1401153d860}, 0x14000000b40, 0x140105ec420, 0x103a24460, 0x0)
    /home/runner/go/pkg/mod/google.golang.org/grpc@v1.53.0/server.go:1336 +0xaec
google.golang.org/grpc.(*Server).handleStream(0x14000332b40, {0x1033a4d20, 0x1401153d860}, 0x14000000b40, 0x0)
    /home/runner/go/pkg/mod/google.golang.org/grpc@v1.53.0/server.go:1704 +0x7cc
google.golang.org/grpc.(*Server).serveStreams.func1.2()
    /home/runner/go/pkg/mod/google.golang.org/grpc@v1.53.0/server.go:965 +0x84
created by google.golang.org/grpc.(*Server).serveStreams.func1 in goroutine 902
    /home/runner/go/pkg/mod/google.golang.org/grpc@v1.53.0/server.go:963 +0x24c
altafan commented 6 months ago

Ocean panics here because the inputs of the partial tx are missing the (non-)witness utxo.

For sure we must add some validation check before signing to prevent this, but you should solve easily by adding the prevout to each input of the partial tx that you want ocean to sign.