summaryrefslogtreecommitdiff
path: root/app/lib/bitcoin-utils.hoon
blob: a23354c5804f041681306b2cc491f8a005c06c0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
::  lib/bitcoin-utils.hoon
::  Utilities for working with BTC data types and transactions
::
/-  *bitcoin
~%  %bitcoin-utils-lib  ..part  ~
|%
::
::  TODO: move this bit/byt stuff to zuse
::  bit/byte utilities
::
::
::  +blop: munge bit and byt sequences (cat, flip, take, drop)
::
++  blop
  ~/  %blop
  |_  =bloq
  +$  biyts  [wid=@ud dat=@]
  ++  cat
    |=  bs=(list biyts)
    ^-  biyts
    :-  (roll (turn bs |=(b=biyts -.b)) add)
    (can bloq (flop bs))
  ::  +flip: flip endianness while preserving lead/trail zeroes
  ::
  ++  flip
    |=  b=biyts
    ^-  biyts
    [wid.b (rev bloq b)]
  ::  +take: take n bloqs from front
  ::  pads front with extra zeroes if n is longer than input
  ::
  ++  take
    |=  [n=@ b=biyts]
    ^-  biyts
    ?:  (gth n wid.b)
      [n dat.b]
    [n (rsh [bloq (sub wid.b n)] dat.b)]
  ::  +drop: drop n bloqs from front
  ::  returns 0^0 if n >= width
  ::
  ++  drop
    |=  [n=@ b=biyts]
    ^-  biyts
    ?:  (gte n wid.b)
      0^0x0
    =+  n-take=(sub wid.b n)
    [n-take (end [bloq n-take] dat.b)]
  --
++  byt  ~(. blop 3)
::
++  bit
  ~/  %bit
  =/  bl    ~(. blop 0)
  |%
  ++  cat   cat:bl:bit
  ++  flip  flip:bl:bit
  ++  take  take:bl:bit
  ++  drop  drop:bl:bit
  ++  from-atoms
    |=  [bitwidth=@ digits=(list @)]
    ^-  bits
    %-  cat:bit
    %+  turn  digits
    |=  a=@
    ?>  (lte (met 0 a) bitwidth)
    [bitwidth `@ub`a]
  ::  +to-atoms: convert bits to atoms of bitwidth
  ::
   ++  to-atoms
    |=  [bitwidth=@ bs=bits]
    ^-  (list @)
    =|  res=(list @)
    ?>  =(0 (mod wid.bs bitwidth))
    |-
    ?:  =(0 wid.bs)  res
    %=  $
        res  (snoc res dat:(take:bit bitwidth bs))
        bs   (drop:bit bitwidth bs)
    ==
  --
::  big endian sha256: input and output are both MSB first (big endian)
::
++  sha256
  ~/  %sha256
  |=  =byts
  ^-  hexb
  %-  flip:byt
  [32 (shay (flip:byt byts))]
::
++  dsha256
  ~/  %dsha256
  |=  =byts
  (sha256 (sha256 byts))
::
++  hash-160
  ~/  %hash-160
  |=  val=byts
  ^-  hexb
  =,  ripemd:crypto
  :-  20
  %-  ripemd-160
  (sha256 val)

::
::  hxb: hex parsing utilities
::
++  hxb
  ~%  %hxb  ..blop  ~
  |%
  ++  from-cord
    ~/  %from-cord
    |=  h=@t
    ^-  hexb
    ?:  =('' h)  1^0x0
    ::  Add leading 00
    ::
    =+  (lsh [3 2] h)
    ::  Group by 4-size block
    ::
    =+  (rsh [3 2] -)
    ::  Parse hex to atom
    ::
    =/  a  (need (de:base16:mimes:html -))
    [-.a `@ux`+.a]
  ::
  ++  to-cord
    ~/  %to-cord
    |=  =hexb
    ^-  cord
    (en:base16:mimes:html hexb)
  --
::
::  +csiz: CompactSize integers (a Bitcoin-specific datatype)
::  https://btcinformation.org/en/developer-reference#compactsize-unsigned-integers
::   - encode: big endian to little endian
::   - decode: little endian to big endian
::
++  csiz
  ~%  %csiz  ..blop  ~
  |%
  ++  en
    ~/  %en
    |=  a=@
    ^-  hexb
    =/  l=@  (met 3 a)
    ?:  =(l 0)  1^a
    ?:  =(l 1)  1^a
    ?:  =(l 2)  (cat:byt ~[1^0xfd (flip:byt 2^a)])
    ?:  (lte l 4)  (cat:byt ~[1^0xfe (flip:byt 4^a)])
    ?:  (lte l 8)  (cat:byt ~[1^0xff (flip:byt 8^a)])
    ~|("Cannot encode CompactSize longer than 8 bytes" !!)
  ::
  ++  de
    ~/  %de
    |=  h=hexb
    ^-  [n=hexb rest=hexb]
    =/  s=@ux  dat:(take:byt 1 h)
    ?:  (lth s 0xfd)  [1^s (drop:byt 1 h)]
    ~|  "Invalid compact-size at start of {<h>}"
    =/  len=bloq
    ?+  s  !!
      %0xfd  1
      %0xfe  2
      %0xff  3
    ==
    :_  (drop:byt (add 1 (bex len)) h)
    %-  flip:byt
    (take:byt (bex len) (drop:byt 1 h))
  ::  +dea: atom instead of hexb for parsed CompactSize
  ::
  ++  dea
    |=  h=hexb
    ^-  [a=@ rest=hexb]
    =>  (de h)
    [dat.n rest]
  --
--