cleaner compact-size

This commit is contained in:
timlucmiptev 2021-02-09 09:02:46 +04:00 committed by ixv
parent 56b7f09559
commit 04eee9f287
3 changed files with 47 additions and 14 deletions

View File

@ -172,7 +172,7 @@
++ parse-filter
|= filter=hexb
^- [n=@ux gcs-set=bits]
=+ n=n:(compact-size filter)
=+ n=n:(de:csiz filter)
=/ lead=@ ?:(=(1 wid.n) 1 +(wid.n))
:- dat.n
[(mul 8 (sub wid.filter lead)) `@ub`dat:(drop:byt lead filter)]

View File

@ -79,24 +79,37 @@
:- 20
%- ripemd-160
(sha256 val)
:: +compact-size: parse compactSize int from head of hex and return result and remainder
:: - input "h" is little endian, output "n" is big endian
:: +csiz: handle compact-size integers
:: - encode: big endian to little endian
:: - decode: little endian to big endian
::
++ compact-size
|= 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
++ csiz
|%
++ en
|= a=@
^- hexb
=/ l=@ (met 3 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
|= 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
%0xfd 1
%0xfe 2
%0xff 3
==
:_ (drop:byt (add 1 len) h)
%- flip:byt
(take:byt (bex len) (drop:byt 1 h))
--
::
++ pubkey-to-address
|= [=bipt =network pubkey=hexb]
@ -161,7 +174,7 @@
1^0x87
==
==
:: +txu: tx utility functions
:: +txu: transaction utility core3
::
++ txu
|%
@ -259,6 +272,7 @@
::
++ encode
|= =data:tx
:: TODO: below is wrong because it doesn't use compact-size
^- hexb
%- cat:byt
%- zing

View File

@ -4,6 +4,7 @@
|%
+$ chyg ?(%0 %1)
+$ bits-vector [bitwidth=@ atoms=(list @) =bits]
+$ compact-size-vector @ux
+$ tx-vector [hex-cord=@t txid=hexb]
+$ psbt-vector
$: =hdkey
@ -27,6 +28,16 @@
==
==
::
++ compact-size-vectors
^- (list compact-size-vector)
:~ 0x98
0x302
0xaa.bbcc
0xaabb.ccdd
0xaa.bbcc.ddee
0xaabb.ccdd.eeff.1122
==
::
++ tx-vectors
^- (list tx-vector)
:~ :* '0200000002ab0949a08c5af7c49b8212f417e2f15ab3f5c33dcf153821a8139f877a5b7be40000000000feffffffab0949a08c5af7c49b8212f417e2f15ab3f5c33dcf153821a8139f877a5b7be40100000000feffffff02603bea0b000000001976a914768a40bbd740cbe81d988e71de2a4d5c71396b1d88ac8e240000000000001976a9146f4620b553fa095e721b9ee0efe9fa039cca459788ac00000000'
@ -156,6 +167,8 @@
|^ ;: weld
%+ category "bit manipulation"
(zing (turn bits-vectors check-bits))
%+ category "compact-size en/decoding"
(zing (turn compact-size-vectors check-compact-size))
%+ category "check PSBT"
(zing (turn psbt-vectors check-psbt))
%+ category "xpub parsing"
@ -179,6 +192,12 @@
!>((to-atoms:bit bitwidth.v bits.v))
==
::
++ check-compact-size
|= v=compact-size-vector
%+ expect-eq
!>(v)
!>(dat:n:(de:csiz (en:csiz v)))
::
++ check-psbt
|= v=psbt-vector
=/ key=hexb