From a8992b9a8d143c1559b8f6b6b75b6667eb02f565 Mon Sep 17 00:00:00 2001 From: Sachin Meier Date: Tue, 1 Oct 2024 17:21:21 -0400 Subject: [PATCH] add decode_psbt script --- scripts/decode_psbt.exs | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 scripts/decode_psbt.exs diff --git a/scripts/decode_psbt.exs b/scripts/decode_psbt.exs new file mode 100644 index 0000000..f20520d --- /dev/null +++ b/scripts/decode_psbt.exs @@ -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)