vbakke / trytes

Converting between trytes and bytes
6 stars 1 forks source link

Padding value fails if not a uint8 #5

Closed vbakke closed 6 years ago

vbakke commented 6 years ago

The algorithm for the padding 255 byte fails if it is not a byte (uint8).

bytes = Trytes.encodeTryteStringAsBytes("99ZZ");     // -> Uint8Array(4) [0, 240, 8, 255]
trytes = Trytes.decodeTryteStringFromBytes([0, 240, 8, 255]);                    // -> "99ZZ9"
trytes = Trytes.decodeTryteStringFromBytes(new Uint8Array([0, 240, 8, 255]));    // -> "99ZZ"
vbakke commented 6 years ago

Having the padding counting relative to -1, was not such a good idea after all.

It is now changed to count relative to the max value of the smaller unit (i.e. 243 for tryte5.) This will work for regardless of the data type, and should work dynamically for any conversion of trits.

Previously, removing 1 unit, was encoded as 255, now it is 244. Previously, removing 2 unit, was encoded as 254, now it is 245.

The only problem is that it break any previously encryption in vbakke/tryte-encrypt. I do not believe anyone has done so yet, but I you have it will work, by using vbakke/trytes v1.2.0.