Skip to content

Commit

Permalink
add decode_psbt script
Browse files Browse the repository at this point in the history
  • Loading branch information
SachinMeier committed Oct 1, 2024
1 parent b9f6482 commit a8992b9
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions scripts/decode_psbt.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
alias Bitcoinex.{PSBT, Script, Transaction}

psbt_path = System.argv() |> List.first()
# psbt_path ="/Users/sachinmeier/Downloads/oct-2024-por.psbt"

{:ok, psbt} = PSBT.from_file(psbt_path)
%PSBT{global: %PSBT.Global{unsigned_tx: tx}, inputs: inputs} = psbt
%Bitcoinex.Transaction{outputs: outputs} = tx

inputs = Enum.map(inputs, fn input ->
%PSBT.In{witness_utxo: %Transaction.Out{
value: value,
script_pub_key: script_pub_key
}} = input

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

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

outputs = Enum.map(outputs, fn output ->
%Transaction.Out{
value: value,
script_pub_key: script_pub_key
} = output

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

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

IO.puts("Inputs:")
Enum.each(inputs, fn input ->
IO.puts(" #{input.address}: #{input.value}")
end)

IO.puts("Outputs:")
Enum.each(outputs, fn output ->
IO.puts(" #{output.address}: #{output.value}")
end)

0 comments on commit a8992b9

Please sign in to comment.