to-n-bits works

This commit is contained in:
timlucmiptev 2020-10-04 17:16:35 +03:00 committed by ixv
parent f55908c87e
commit ffa34f53a8
2 changed files with 33 additions and 9 deletions

View File

@ -42,12 +42,20 @@ The below requires norsyr's fix to `decompress-point` in order to work.
=bip173 -build-file %/lib/bip173/hoon
```
### BTC pubkey -> address hashing
### Bech32 Algo
- hash = hash160(pubkey)
- words = convert(hash, 8bitTo5bit)
- encode('bc', [0x00 words])
### BTC pubkey -> address hashing (Hash-160)
Uses the example data here:
https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses
That one starts with pubkey below. The following runs it through sha256 and ripemd160 to yield the hash:
`0xf54a.5851.e937.2b87.810a.8e60.cdd2.e7cf.d80b.6e31`
```
0xf54a.5851.e937.2b87.810a.8e60.cdd2.e7cf.d80b.6e31
```
Use `@uc` to make the Hash-160 into a BTC P2PKH address
```
=pubkey 0x2.5086.3ad6.4a87.ae8a.2fe8.3c1a.f1a8.403c.b53f.53e4.86d8.511d.ad8a.0488.7e5b.2352

View File

@ -4,6 +4,29 @@
::
++ charset "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
::
++ hash-160
|= pubkey=@ux
^- @ux
=/ sha (swp 3 (shax (swp 3 pubkey)))
%- ripemd-160:ripemd:crypto
[(met 3 sha) sha]
:: Converts an atom to a list of n-bit numbers, flop to preserve big-endian
::
++ to-n-bit
|= [n=@ input=@]
^- (list @)
=/ bits (flop (rip 0 input))
=| ret=(list @)
|- ?~ bits ret
=/ n-bits (scag n ((list @) bits))
:: left-shift the "missing" number of bits
=/ num=@
%: lsh 0
(sub n (lent n-bits))
(rep 0 (flop n-bits))
==
$(ret (snoc ret num), bits (slag n ((list @) bits)))
::
++ polymod
|= values=(list @)
=/ gen=(list @ux)
@ -72,13 +95,6 @@
=/ checksum-pos (sub (lent data-and-checksum) 6)
`[hrp (scag checksum-pos data-and-checksum) (slag checksum-pos data-and-checksum)]
::
++ hash-160
|= pubkey=@ux
^- @ux
=/ sha (swp 3 (shax (swp 3 pubkey)))
%- ripemd-160:ripemd:crypto
[(met 3 sha) sha]
::
++ decode-segwit
|= segwit=tape
^- (unit [witver=@ witprog=@ux])