itering/scale.go

`FixedU8` codec not matched for `[4;u8]`

Closed this issue · 4 comments

First of all, thank you for this project, it really helps me glue golang app with rpc endpoint consuming scale encoded values.

What I've found so far is that it really makes difference whether [4;u8] or [4; u8] type is used for encoding.

How currently types and codecs are correlated:
[4;u8] -> FixedArray
[4; u8] -> FixedU8

Encoding implication:
[4;u8] -> types.Encode("[u8;4]", []byte{2, 2, 2, 2}) -> 3032303230323032
[4; u8] -> types.Encode("[u8; 4]", []byte{2, 2, 2, 2}) -> 02020202

How I would expect it to be:
[4;u8] -> FixedU8
[4; u8] -> FixedU8

@kziemianek Can you provide test code? I can't reproduce it. In addition, I don’t quite understand the meaning of [4;u8]. Fixed u8 normally looks like [u8; 4].

@freehere107 actually it works a little bit different than I described - in second case I need to pass hex encoded string because if byte array is passed then it panics.

	byteArr := []byte{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
	encoded := types.Encode("[u8;16]", byteArr)
	fmt.Println(encoded)

result:
00000000000000000000000000000000
	byteArr := []byte{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
	encoded := types.Encode("[u8; 16]", byteArr)
	fmt.Println(encoded)
result:
panic: reflect: Call using []uint8 as type string
	byteArr := []byte{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
	encoded := types.Encode("[u8; 16]", utiles.BytesToHex(byteArr))
	fmt.Println(encoded)
result:
02020202020202020202020202020202

[u8;16] and [u8; 16] are almost equal, there is only space after the semicolon. From my understanding this space should be irrelevant but it turns out it has huge impact. Is the result in first case valid? It also expects different types to be passed in both cases (hexencoded string and byte array). Maybe I'm using this Encode function wrongly but my impression is that it's quiet confusing.

I think that expected is: no matter if [u8;16] or [u8; 16] is used, Encode function should produce valid scale encoded value if byte array is passed. What do you think?

@kziemianek Make sense. Thanks for you feedback. I will update it as soon.

@kziemianek This issue has been fixed.