mirror of
https://github.com/ilyakooo0/urbit.git
synced 2024-11-13 08:38:43 +03:00
zeros-brip improvements and hash-160 handles 0s
This commit is contained in:
parent
a7415aba4a
commit
2ed13a1bde
@ -153,8 +153,11 @@ The below code also shows how to convert from bech32 back to a 20-byte hash. Thi
|
||||
(encode-pubkey:bech32:btc %main pubkey)
|
||||
:: should be [~ "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"]
|
||||
|
||||
(hash-160:btc pubkey)
|
||||
:: should be 0x751e.76e8.1991.96d4.5494.1c45.d1b3.a323.f143.3bd6
|
||||
`[@ @ux]`(hash-160:btc pubkey)
|
||||
:: gives [20 0x751e.76e8.1991.96d4.5494.1c45.d1b3.a323.f143.3bd6]
|
||||
|
||||
(encode-hash-160:bech32:btc %main (hash-160:btc pubkey))
|
||||
:: gives [~ "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"]
|
||||
```
|
||||
|
||||
### Decode Bech32 to hex
|
||||
@ -173,24 +176,24 @@ From seed mnemonic:
|
||||
process child keen cargo design install parrot hold pole unveil dance reason drink cash fix
|
||||
|
||||
0x2.88b5.a58a.5c26.6cef.d41b.f329.9165.46cc.1703.c4d9.a32e.1ea3.ef3d.1823.c493.05ac
|
||||
0x3.289a.4e24.4381.8992.fe20.0831.3551.a3af.2266.ef3d.2038.5df9.6daa.92e3.4df2.16c4
|
||||
0x3.289a.4e24.4381.8992.fe20.0831.3551.a3af.2266.ef3d.2038.5df9.6daa.g92e3.4df2.16c4
|
||||
0x3.109a.2082.eaa6.8925.1465.5393.d635.7fb9.d9b5.e191.3826.8837.69cd.db88.7a4b.b4f0
|
||||
```
|
||||
|
||||
## Unsigned Transactions
|
||||
Using [BIP 143](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#P2SHP2WPKH) as a reference.
|
||||
|
||||
## Signing Scratch
|
||||
=unsigned 0x100.0000.015a.800f.d903.679a.cfe9.d4e2.fedb.752b.24c4.d7a5.74b4.c82a.113d.fb99.3b38.64b7.7201.0000.0019.76a9.1495.3de6.57be.4b30.5f60.6d9a.9fbd.35b0.70a6.8247.5788.acff.ffff.ff01.c09e.e605.0000.0000.1976.a914.dd6c.ce9f.255a.8cc1.7bda.8ba0.373d.f8e8.61cb.866e.88ac.0000.0000.0100.0000
|
||||
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])
|
||||
|
||||
=txhash (shay (met 3 unsigned) (swp 3 unsigned))
|
||||
output0=[[%bech32 'bc1qs2qtxl0n0rdenan0shy457p6w6k85m2e36f7ze']]
|
||||
```
|
||||
0x8280.b37d.f378.db99.f66f.85c9.5a78.3a76.ac7a.6d59
|
||||
|
||||
=privkey 0xdc57.c6d0.6737.6c36.bbed.632c.9d00.f037.6786.7f33.7d5a.86b5.b030.8a60.004f.08ee
|
||||
|
||||
0x7f5a.997b.83f5.1f79.3b89.10be.9950.8b00.a136.f922
|
||||
|
||||
0xbd9.a59c.4ffc.92d3.f5dd.8ba3.a2ed.93b3.d1d9.f9cf.91cf.0982.db70.9be7.0a37.db76
|
||||
|
||||
|
||||
### cutting off the last 20 bytes (for bech32 address outputs):
|
||||
## cutting off the last 20 bytes (for bech32 address outputs):
|
||||
```
|
||||
`@ux`(end 3 2 0x14.6655)
|
||||
:: gives 0x6655
|
||||
|
117
lib/btc.hoon
117
lib/btc.hoon
@ -12,66 +12,115 @@
|
||||
:- 32
|
||||
%+ swp 3
|
||||
(shay wid.byts little-endian)
|
||||
::
|
||||
++ dsha256
|
||||
|= =byts
|
||||
(sha256 (sha256 byts))
|
||||
::
|
||||
++ hash-160
|
||||
|= pubkey=@ux
|
||||
^- @ux
|
||||
|= pubkey=@ux ^- byts
|
||||
=, ripemd:crypto
|
||||
:- 20
|
||||
%- ripemd-160
|
||||
%- sha256 [(met 3 pubkey) pubkey]
|
||||
::
|
||||
++ address-to-script-pubkey
|
||||
|= =address ^- buffer:tx
|
||||
?. ?=(%bech32 -.address)
|
||||
~|("Only bech32 addressess supported right now" !!)
|
||||
=/ hex=byts (to-hex:bech32 (trip +.address))
|
||||
?. =(wid.hex 20)
|
||||
~|("Only 20-byte P2WPKH bech32 supported" !!)
|
||||
%- zing
|
||||
:~ ~[0x19 0x76 0xa9 0x14]
|
||||
(from-byts:buffer hex)
|
||||
~[0x88 0xac]
|
||||
==
|
||||
::
|
||||
:: list of @ux that is big endian for hashing purposes
|
||||
:: used to preserve 0s when concatenating byte sequences
|
||||
::
|
||||
++ buffer
|
||||
|%
|
||||
:: from-byts: converts byts to list, preserving leading 0s
|
||||
::
|
||||
++ from-byts
|
||||
|= =byts
|
||||
^- buffer:tx
|
||||
|= =byts ^- buffer:tx
|
||||
=/ b=(list @ux)
|
||||
(flop (rip 3 dat.byts))
|
||||
=/ pad=@ (sub wid.byts (lent b))
|
||||
(weld (reap pad 0x0) b)
|
||||
:: converts an atom to a little endian buffer with wid length (trailing 0s)
|
||||
:: atom 1 with wid=4 becomes ~[0x1 0x0 0x0 0x0]
|
||||
:: 0xff11 with wid=8 becomes ~[0x11 0xff 0x0 0x0 0x0 0x0 0x0 0x0]
|
||||
::
|
||||
++ from-atom-le
|
||||
|= [wid=@ a=@] ^- buffer:tx
|
||||
=/ b=(list @ux) (rip 3 a)
|
||||
=/ pad=@ (sub wid (lent b))
|
||||
(weld b (reap pad 0x0))
|
||||
::
|
||||
++ to-byts
|
||||
|= b=buffer:tx
|
||||
^- byts
|
||||
|= b=buffer:tx ^- byts
|
||||
[(lent b) (rep 3 (flop b))]
|
||||
++ concat-as-byts
|
||||
|= bs=(list buffer:tx) ^- byts
|
||||
%- to-byts (zing bs)
|
||||
--
|
||||
::
|
||||
++ payments
|
||||
|%
|
||||
++ p2pkh
|
||||
|= script=@ux
|
||||
:: ^- @ux
|
||||
^- (list @ux)
|
||||
=/ chunks=(list (list @))
|
||||
:~ ~[op-dup:ops]
|
||||
~[op-hash160:ops]
|
||||
~[(met 3 script)]
|
||||
::TODO FLOP rip in HERE
|
||||
~[op-equalverify:ops]
|
||||
~[op-checksig:ops]
|
||||
==
|
||||
(zing chunks)
|
||||
++ unsigned-tx
|
||||
=, buffer
|
||||
|_ ut=unsigned:tx
|
||||
++ prevout-buffer
|
||||
|= =input:tx ^- buffer:tx
|
||||
%+ weld
|
||||
(from-byts tx-hash.input)
|
||||
(from-atom-le 8 witness-ver.input)
|
||||
::
|
||||
++ output-buffer
|
||||
|= =output:tx ^- buffer:tx
|
||||
%+ weld
|
||||
(from-atom-le 4 value.output)
|
||||
(address-to-script-pubkey address.output)
|
||||
::
|
||||
++ sighash
|
||||
|= input-index=@ ^- byts
|
||||
?: (gte input-index (lent inputs.ut))
|
||||
~|("Input index out of range" !!)
|
||||
=/ =input:tx (snag input-index inputs.ut)
|
||||
?: =(1 witness-ver.input)
|
||||
(sighash-witness input)
|
||||
(sighash-legacy input)
|
||||
::
|
||||
++ sighash-witness
|
||||
|= =input:tx ^- byts
|
||||
=/ prevouts=byts
|
||||
%- concat-as-byts (turn inputs.ut prevout-buffer)
|
||||
=/ sequences=byts
|
||||
%- concat-as-byts
|
||||
%+ reap (lent inputs.ut)
|
||||
(from-byts [4 0xffff.ffff])
|
||||
=/ outputs=byts
|
||||
%- concat-as-byts (turn outputs.ut output-buffer)
|
||||
(dsha256 prevouts)
|
||||
::
|
||||
++ sighash-legacy
|
||||
|= =input:tx ^- byts
|
||||
[0 0]
|
||||
--
|
||||
::
|
||||
:: Converts a list of bits to a list of n-bit numbers
|
||||
:: input-bits should be big-endian
|
||||
::
|
||||
++ bits
|
||||
|%
|
||||
:: rip atom of bitwidth wordlen. Preserve leading 0s, big endian
|
||||
:: rip atom a with num-bits. Preserve leading 0s, big endian
|
||||
:: returns a list of bits
|
||||
::
|
||||
++ zeros-brip
|
||||
|= [bitwidth=@ a=@]
|
||||
|= [num-bits=@ a=@]
|
||||
^- (list @)
|
||||
=/ bits=(list @) (flop (rip 0 a))
|
||||
=/ r=@ (mod (lent bits) bitwidth)
|
||||
?: ?&(=(0 r) (gth (lent bits) 0)) :: no remainder & more than 0 bits
|
||||
bits
|
||||
(weld (reap (sub bitwidth r) 0) bits)
|
||||
=/ pad=@ (sub num-bits (lent bits))
|
||||
(weld (reap pad 0) bits)
|
||||
:: converts from bit list to a list of atoms each with bitwidth d(est)
|
||||
::
|
||||
++ convert
|
||||
@ -232,7 +281,15 @@
|
||||
?~ prefix ~
|
||||
:- ~
|
||||
%+ encode-raw u.prefix
|
||||
(weld ~[0] (convert:bits 5 (zeros-brip:bits 8 (hash-160 pubkey))))
|
||||
[0 (convert:bits 5 (zeros-brip:bits 160 dat:(hash-160 pubkey)))]
|
||||
++ encode-hash-160
|
||||
|= [=network h160=byts]
|
||||
^- (unit tape)
|
||||
=/ prefix (~(get by prefixes) network)
|
||||
?~ prefix ~
|
||||
:- ~
|
||||
%+ encode-raw u.prefix
|
||||
[0 (convert:bits 5 (zeros-brip:bits 160 dat.h160))]
|
||||
--
|
||||
::
|
||||
--
|
||||
|
10
sur/btc.hoon
10
sur/btc.hoon
@ -3,12 +3,13 @@
|
||||
+$ legacy-address $%([%legacy @uc])
|
||||
+$ bech32-address $%([%bech32 cord])
|
||||
+$ address ?(legacy-address bech32-address)
|
||||
+$ sats @ud
|
||||
++ tx
|
||||
|%
|
||||
+$ buffer (list @ux)
|
||||
+$ unsigned
|
||||
$: (list input)
|
||||
(list output)
|
||||
$: inputs=(list input)
|
||||
outputs=(list output)
|
||||
==
|
||||
+$ input
|
||||
$: tx-hash=byts
|
||||
@ -16,11 +17,12 @@
|
||||
witness-ver=@
|
||||
script-pubkey=byts
|
||||
redeem-script=(unit byts)
|
||||
pubkey=byts
|
||||
pubkey=(unit byts)
|
||||
value=sats
|
||||
==
|
||||
+$ output
|
||||
$: =address
|
||||
value=@
|
||||
value=sats
|
||||
==
|
||||
--
|
||||
++ ops
|
||||
|
Loading…
Reference in New Issue
Block a user