Skip to content

Commit

Permalink
Add Sighash check to DecodePSBT scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
SachinMeier committed Oct 14, 2024
1 parent 76a9ee5 commit 2a7fd60
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions scripts/decode_psbt.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ defmodule DecodePSBT do

IO.puts("\nInputs:")
Enum.each(inputs, fn input ->
if input.note != nil do
IO.puts(input.note)
end
IO.puts(" #{input.address}: #{sats_to_btc_str(input.value)} BTC")
end)

Expand Down Expand Up @@ -92,18 +95,40 @@ defmodule DecodePSBT do
%PSBT.In{witness_utxo: %Transaction.Out{
value: value,
script_pub_key: script_pub_key
}} = input
}, sighash_type: sighash_type} = input

{:ok, script} = Script.parse_script(script_pub_key)
{:ok, address} = Script.to_address(script, :mainnet)

note =
if sighash_type != nil and sighash_type != 0x01 do
"🚨🚨🚨 WARNING: NON-STANDARD SIGHASH TYPE: #{sighash_name(sighash_type)} 🚨🚨🚨"
else
nil
end

%{
address: address,
value: value
value: value,
note: note
}
end)
end

# map between a sighash's int and a name
@spec sighash_name(non_neg_integer)
defp sighash_name(n) do
case n do
0x00 -> "SIGHASH_DEFAULT" # for Segwit v1 (taproot) inputs only
0x01 -> "SIGHASH_ALL"
0x02 -> "SIGHASH_NONE"
0x03 -> "SIGHASH_SINGLE"
0x81 -> "SIGHASH_ALL/ANYONECANPAY"
0x82 -> "SIGHASH_NONE/ANYONECANPAY"
0x82 -> "SIGHASH_SINGLE/ANYONECANPAY"
end
end

@doc """
Parse the outputs of the PSBT.
"""
Expand Down

0 comments on commit 2a7fd60

Please sign in to comment.