hashing prevouts/sequences/outputs successfully

This commit is contained in:
timlucmiptev 2020-10-16 13:15:51 +03:00 committed by ixv
parent 9332a749d8
commit e2bd7bc006
3 changed files with 22 additions and 21 deletions

View File

@ -186,16 +186,13 @@ Using [BIP 143](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#P
Native P2WPKH
```
=btcs -build-file %/sur/btc/hoon
=input0 (input:tx:btcs [[32 0xfff7.f788.1a80.99af.a694.0d42.d1e7.f636.2bec.3817.1ea3.edf4.3354.1db4.e4ad.969f] 0 0 [35 0x21.03c9.f483.6b9a.4f77.fc0d.81f7.bcb0.1b7f.1b35.9168.64b9.476c.241c.e9fc.198b.d254.32ac] ~ ~ 625.000.000])
=input1 (input:tx:btcs [[32 0xef51.e1b8.04cc.89d1.82d2.7965.5c3a.a89e.815b.1b30.9fe2.87d9.b2b5.5d57.b90e.c68a] 0 1 [22 0x14.1d0f.172a.0ecb.48ae.e1be.1f26.87d2.963a.e33f.71a1] ~ `[33 0x2.5476.c2e8.3188.368d.a1ff.3e29.2e7a.cafc.db35.66bb.0ad2.53f6.2fc7.0f07.aeee.6357] 600.000.000])
=input0 (input:tx:btcs [[32 0xfff7.f788.1a80.99af.a694.0d42.d1e7.f636.2bec.3817.1ea3.edf4.3354.1db4.e4ad.969f] 0 0 [4 0xeeff.ffff] [35 0x21.03c9.f483.6b9a.4f77.fc0d.81f7.bcb0.1b7f.1b35.9168.64b9.476c.241c.e9fc.198b.d254.32ac] ~ ~ 625.000.000])
=input1 (input:tx:btcs [[32 0xef51.e1b8.04cc.89d1.82d2.7965.5c3a.a89e.815b.1b30.9fe2.87d9.b2b5.5d57.b90e.c68a] 0 1 [4 0xffff.ffff] [22 0x14.1d0f.172a.0ecb.48ae.e1be.1f26.87d2.963a.e33f.71a1] ~ `[33 0x2.5476.c2e8.3188.368d.a1ff.3e29.2e7a.cafc.db35.66bb.0ad2.53f6.2fc7.0f07.aeee.6357] 600.000.000])
=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]])
```
0x3bde.42db.ee7e.4dbe.6a21.b2d5.0ce2.f016.7faa.8159
## cutting off the last 20 bytes (for bech32 address outputs):
```

View File

@ -3,8 +3,7 @@
:: big endian sha256: input and output are both MSB first (big endian)
::
++ sha256
|= =byts
^- ^byts
|= =byts ^- hash
:: if there are leading 0s, lshift by their amount after flip to little endian to preserve
=/ pad=@ (sub wid.byts (met 3 dat.byts))
=/ little-endian=@
@ -18,7 +17,7 @@
(sha256 (sha256 byts))
::
++ hash-160
|= pubkey=@ux ^- byts
|= pubkey=@ux ^- hash
=, ripemd:crypto
:- 20
%- ripemd-160
@ -69,20 +68,24 @@
++ unsigned-tx
=, buffer
|_ ut=unsigned:tx
++ prevout-buffer
++ prevouts-buffer
|= =input:tx ^- buffer:tx
%+ weld
(from-byts tx-hash.input)
(from-atom-le 4 witness-ver.input)
::
++ output-buffer
++ sequence-buffer
|= =input:tx ^- buffer:tx
(from-byts sequence.input)
::
++ outputs-buffer
|= =output:tx ^- buffer:tx
%+ weld
(from-atom-le 4 value.output)
(from-atom-le 8 value.output)
(address-to-script-pubkey address.output)
::
++ sighash
|= input-index=@ ^- byts
|= input-index=@ ^- hash
?: (gte input-index (lent inputs.ut))
~|("Input index out of range" !!)
=/ =input:tx (snag input-index inputs.ut)
@ -91,21 +94,20 @@
(sighash-legacy input)
::
++ sighash-witness
|= =input:tx ^- byts
|= =input:tx ^- hash
=/ prevouts=byts
%- concat-as-byts (turn inputs.ut prevout-buffer)
%- concat-as-byts (turn inputs.ut prevouts-buffer)
=/ sequences=byts
%- concat-as-byts
%+ reap (lent inputs.ut)
(from-byts [4 0xffff.ffff])
(turn inputs.ut sequence-buffer)
=/ outputs=byts
%- concat-as-byts (turn outputs.ut output-buffer)
~& > `[@ @ux]`prevouts
(dsha256 prevouts)
%- concat-as-byts (turn outputs.ut outputs-buffer)
~& > [prevouts=(dsha256 prevouts) sequences=(dsha256 sequences) outputs=(dsha256 outputs)]
[0 0x0]
::
++ sighash-legacy
|= =input:tx ^- byts
[0 0]
|= =input:tx ^- hash
[0 0x0]
--
::
:: Converts a list of bits to a list of n-bit numbers

View File

@ -4,6 +4,7 @@
+$ bech32-address $%([%bech32 cord])
+$ address ?(legacy-address bech32-address)
+$ sats @ud
+$ hash [wid=@ dat=@ux]
++ tx
|%
+$ buffer (list @ux)
@ -15,6 +16,7 @@
$: tx-hash=byts
tx-index=@
witness-ver=@
sequence=byts
script-pubkey=byts
redeem-script=(unit byts)
pubkey=(unit byts)