@@ -3,7 +3,7 @@ use blake2::{
33 Blake2b , Blake2bMac , Digest ,
44} ;
55use byteorder:: { BigEndian , WriteBytesExt } ;
6- use compact_encoding:: State ;
6+ use compact_encoding:: { as_array , to_encoded_bytes , EncodingError , FixedWidthEncoding } ;
77use ed25519_dalek:: VerifyingKey ;
88use merkle_tree_stream:: Node as NodeTrait ;
99use std:: convert:: AsRef ;
@@ -124,10 +124,9 @@ impl Hash {
124124
125125 /// Hash data
126126 pub ( crate ) fn data ( data : & [ u8 ] ) -> Self {
127- let ( mut state, mut size) = State :: new_with_size ( 8 ) ;
128- state
129- . encode_u64 ( data. len ( ) as u64 , & mut size)
130- . expect ( "Encoding u64 should not fail" ) ;
127+ let size =
128+ ( || Ok :: < _ , EncodingError > ( to_encoded_bytes ! ( ( data. len( ) as u64 ) . as_fixed_width( ) ) ) ) ( )
129+ . expect ( "Encoding u64 should not fail" ) ;
131130
132131 let mut hasher = Blake2b256 :: new ( ) ;
133132 hasher. update ( LEAF_TYPE ) ;
@@ -147,10 +146,10 @@ impl Hash {
147146 ( right, left)
148147 } ;
149148
150- let ( mut state , mut size ) = State :: new_with_size ( 8 ) ;
151- state
152- . encode_u64 ( node1 . length + node2 . length , & mut size )
153- . expect ( "Encoding u64 should not fail" ) ;
149+ let len = node1 . length + node2 . length ;
150+ let size : Box < [ u8 ] > =
151+ ( || Ok :: < _ , EncodingError > ( to_encoded_bytes ! ( len . as_fixed_width ( ) ) ) ) ( )
152+ . expect ( "Encoding u64 should not fail" ) ;
154153
155154 let mut hasher = Blake2b256 :: new ( ) ;
156155 hasher. update ( PARENT_TYPE ) ;
@@ -170,13 +169,13 @@ impl Hash {
170169
171170 for node in roots {
172171 let node = node. as_ref ( ) ;
173- let ( mut state , mut buffer) = State :: new_with_size ( 16 ) ;
174- state
175- . encode_u64 ( node. index ( ) , & mut buffer )
176- . expect ( "Encoding u64 should not fail" ) ;
177- state
178- . encode_u64 ( node . len ( ) , & mut buffer )
179- . expect ( "Encoding u64 should not fail" ) ;
172+ let buffer = ( || {
173+ Ok :: < _ , EncodingError > ( to_encoded_bytes ! (
174+ node. index( ) . as_fixed_width ( ) ,
175+ node . len ( ) . as_fixed_width ( )
176+ ) )
177+ } ) ( )
178+ . expect ( "Encoding u64 should not fail" ) ;
180179
181180 hasher. update ( node. hash ( ) ) ;
182181 hasher. update ( & buffer[ ..8 ] ) ;
@@ -212,20 +211,15 @@ impl DerefMut for Hash {
212211/// Create a signable buffer for tree. This is treeSignable in Javascript.
213212/// See https://github.com/hypercore-protocol/hypercore/blob/70b271643c4e4b1e5ecae5bb579966dfe6361ff3/lib/caps.js#L17
214213pub ( crate ) fn signable_tree ( hash : & [ u8 ] , length : u64 , fork : u64 ) -> Box < [ u8 ] > {
215- let ( mut state, mut buffer) = State :: new_with_size ( 80 ) ;
216- state
217- . encode_fixed_32 ( & TREE , & mut buffer)
218- . expect ( "Should be able " ) ;
219- state
220- . encode_fixed_32 ( hash, & mut buffer)
221- . expect ( "Encoding fixed 32 bytes should not fail" ) ;
222- state
223- . encode_u64 ( length, & mut buffer)
224- . expect ( "Encoding u64 should not fail" ) ;
225- state
226- . encode_u64 ( fork, & mut buffer)
227- . expect ( "Encoding u64 should not fail" ) ;
228- buffer
214+ ( || {
215+ Ok :: < _ , EncodingError > ( to_encoded_bytes ! (
216+ & TREE ,
217+ as_array:: <32 >( hash) ?,
218+ length. as_fixed_width( ) ,
219+ fork. as_fixed_width( )
220+ ) )
221+ } ) ( )
222+ . expect ( "Encoding should not fail" )
229223}
230224
231225#[ cfg( test) ]
0 commit comments