From efdcdf0fdc56673aeab114f0ec8fa100b98eaa0b Mon Sep 17 00:00:00 2001 From: timlucmiptev Date: Fri, 16 Oct 2020 14:32:48 +0300 Subject: [PATCH] sighash-witness works --- bippy-scratch.md | 5 ++++- lib/btc.hoon | 30 +++++++++++++++++++++++++++--- sur/btc.hoon | 5 ++++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/bippy-scratch.md b/bippy-scratch.md index 79fdf0c759..ddb19ead5b 100644 --- a/bippy-scratch.md +++ b/bippy-scratch.md @@ -191,7 +191,10 @@ Native P2WPKH =output0 (output:tx:btcs [[%bech32 'bc1qs2qtxl0n0rdenan0shy457p6w6k85m2e36f7ze'] 112.340.000]) =output1 (output:tx:btcs [[%bech32 'bc1q800y9klw0exmu63pkt2sechszel64q2enddkt4'] 223.450.000]) -=utx (unsigned:tx:btcs [~[input0 input1] ~[output0 output1]]) +=utx (unsigned:tx:btcs [1 0x11 ~[input0 input1] ~[output0 output1]]) + +(~(sighash unsigned-tx:btc utx) 1) +:: gives [wid=32 dat=0xc37a.f311.16d1.b27c.af68.aae9.e3ac.82f1.4779.2901.4d5b.9176.57d0.eb49.478c.b670] ``` ## cutting off the last 20 bytes (for bech32 address outputs): diff --git a/lib/btc.hoon b/lib/btc.hoon index 23187e9bc3..9ffc960d29 100644 --- a/lib/btc.hoon +++ b/lib/btc.hoon @@ -104,15 +104,39 @@ (turn inputs.ut sequence-buffer) =/ outputs=byts %- concat-as-byts (turn outputs.ut outputs-buffer) - :: all the variables + :: Hash inputs in order, as per BIP143 examples + :: + =/ n-version=^buffer (from-atom-le 4 version.ut) + =/ hash-prevouts=^buffer + %- from-byts (dsha256 prevouts) + =/ hash-sequence=^buffer + %- from-byts (dsha256 sequences) + =/ outpoint=^buffer + %+ weld (from-byts tx-hash.input) + (from-atom-le 4 witness-ver.input) =/ script-code=^buffer %- to-script-pubkey (slag 2 (from-byts script-pubkey.input)) =/ amount=^buffer (from-atom-le 8 value.input) =/ n-sequence=^buffer (sequence-buffer input) - ~& > [prevouts=(dsha256 prevouts) sequences=(dsha256 sequences) outputs=(dsha256 outputs)] - [0 0x0] + =/ hash-outputs=^buffer + %- from-byts (dsha256 outputs) + =/ n-locktime=^buffer (from-atom-le 4 locktime.ut) + =/ n-hashtype=^buffer (from-atom-le 4 1) + %- dsha256 + %- concat-as-byts + :~ n-version + hash-prevouts + hash-sequence + outpoint + script-code + amount + n-sequence + hash-outputs + n-locktime + n-hashtype + == :: ++ sighash-legacy |= =input:tx ^- hash diff --git a/sur/btc.hoon b/sur/btc.hoon index c6a77a32c2..4aff779c41 100644 --- a/sur/btc.hoon +++ b/sur/btc.hoon @@ -9,7 +9,9 @@ ++ tx |% +$ unsigned - $: inputs=(list input) + $: version=@ + locktime=@ + inputs=(list input) outputs=(list output) == +$ input @@ -35,3 +37,4 @@ ++ op-checksig 172 -- -- +