Skip to content

Commit 3ef07f1

Browse files
Merge pull request #56 from LimeChain/refactor/bytereader
Refactor/bytereader
2 parents 8fde4ee + 30e8170 commit 3ef07f1

37 files changed

+699
-433
lines changed

assembly/AbstractInt.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ import { Bytes } from "./utils/Bytes";
1919
export abstract class AbstractInt<T extends number> implements Codec {
2020

2121
protected bitLength: i32;
22-
public readonly value: T;
22+
private _value: T;
23+
24+
get value(): T{
25+
return this._value;
26+
}
2327

2428
constructor (value: T, bitLength: i32) {
25-
this.value = value;
29+
this._value = value;
2630
this.bitLength = bitLength;
2731
}
2832

@@ -33,6 +37,16 @@ export abstract class AbstractInt<T extends number> implements Codec {
3337
return bytesEncoded;
3438
}
3539

40+
/**
41+
* @description Non-static constructor method used to populate defined properties of the model
42+
* @param bytes SCALE encoded bytes
43+
* @param index index to start decoding the bytes from
44+
*/
45+
public populateFromBytes(bytes: u8[], index: i32 = 0): void {
46+
assert(bytes.length - index > 0, "AbstractInt: Invalid bytes provided");
47+
this._value = Bytes.toUint<T>(bytes, this.bitLength, index);
48+
}
49+
3650
/**
3751
* @description Returns the string representation of the value
3852
*/

assembly/Arrays/AbstractArray.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import { Codec } from "../interfaces/Codec";
1616
import { Bytes } from "../utils/Bytes";
1717
import { BytesBuffer } from "../utils/BytesBuffer";
1818
import { DecodedData } from "../interfaces/DecodedData";
19+
import { CompactInt } from "..";
1920

20-
export abstract class AbstractArray<ScaleType extends Codec, NativeType> {
21+
export abstract class AbstractArray<ScaleType extends Codec, NativeType> implements Codec{
2122

2223
public values: Array<NativeType>;
23-
24-
constructor(input: NativeType[]) {
24+
constructor(input: NativeType[] = []) {
2525
this.values = new Array<NativeType>(input.length);
2626
Bytes.copy<NativeType>(input, this.values);
2727
}
@@ -41,6 +41,18 @@ export abstract class AbstractArray<ScaleType extends Codec, NativeType> {
4141
return bytesBuffer.bytes;
4242
}
4343

44+
/**
45+
* @description Returns encoded byte length of the type
46+
*/
47+
abstract encodedLength(): i32;
48+
49+
/**
50+
* @description Non-static constructor method used to populate defined properties of the model
51+
* @param bytes SCALE encoded bytes
52+
* @param index index to start decoding the bytes from
53+
*/
54+
abstract populateFromBytes(bytes: u8[], index: i32): void;
55+
4456
/**
4557
* @description Each child class has to provide decryption implementation for elements
4658
*/

assembly/Arrays/BoolArray.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import { Bool } from "./../Bool";
1616
import { AbstractArray } from "./AbstractArray"
1717

1818
import { DecodedData } from "../interfaces/DecodedData";
19-
import { Bytes } from "../utils/Bytes";
2019
import { ArrayUtils } from "../utils/Arrays";
20+
import { BytesReader, CompactInt } from "..";
2121

2222
// @ts-ignore
2323
export class BoolArray extends AbstractArray<Bool, bool> {
@@ -33,7 +33,28 @@ export class BoolArray extends AbstractArray<Bool, bool> {
3333
scaleBool.encodedLength()
3434
)
3535
}
36+
37+
/**
38+
* @description Returns encoded byte length of the type
39+
*/
40+
public encodedLength(): i32{
41+
return (new CompactInt(this.values.length).encodedLength()) + super.values.length;
42+
}
3643

44+
/**
45+
* @description Non-static constructor method used to populate defined properties of the model
46+
* @param bytes SCALE encoded bytes
47+
* @param index index to start decoding the bytes from
48+
*/
49+
populateFromBytes(bytes: u8[], index: i32 = 0): void{
50+
const bytesReader = new BytesReader(bytes.slice(index));
51+
const data = bytesReader.readInto<CompactInt>();
52+
53+
for(let i: i32 = 0; i < data.value - index; i++){
54+
const element = bytesReader.readInto<Bool>();
55+
this.values.push(element.value);
56+
}
57+
}
3758
/**
3859
* @description Instantiates ScaleBoolArray from u8[] SCALE encoded bytes (Decode)
3960
*/

assembly/Arrays/ByteArray.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { CompactInt } from "../Int";
1717
import { AbstractArray } from "./AbstractArray";
1818
import { DecodedData } from "../interfaces/DecodedData";
1919
import { ArrayUtils } from "../utils/Arrays";
20+
import { BytesReader } from "..";
2021

2122
// @ts-ignore
2223
export class ByteArray extends AbstractArray<Byte, u8> {
@@ -37,7 +38,6 @@ export class ByteArray extends AbstractArray<Byte, u8> {
3738

3839
return result;
3940
}
40-
4141
/**
4242
* @description BoolArray elements decryption implementation
4343
*/
@@ -51,12 +51,26 @@ export class ByteArray extends AbstractArray<Byte, u8> {
5151
}
5252

5353
/**
54-
* @description The length of encoded bytes of the ByteArray
54+
* @description Returns encoded byte length of the type
5555
*/
56-
public encodedLength (): i32 {
57-
return (new CompactInt(super.values.length).encodedLength()) + super.values.length;
56+
public encodedLength(): i32{
57+
return (new CompactInt(this.values.length).encodedLength()) + super.values.length;
5858
}
59+
60+
/**
61+
* @description Non-static constructor method used to populate defined properties of the model
62+
* @param bytes SCALE encoded bytes
63+
* @param index index to start decoding the bytes from
64+
*/
65+
populateFromBytes(bytes: u8[], index: i32 = 0): void {
66+
const bytesReader = new BytesReader(bytes.slice(index));
67+
const data = bytesReader.readInto<CompactInt>();
5968

69+
for(let i: i32 = 0; i < data.value; i++){
70+
const element: Byte = bytesReader.readInto<Byte>();
71+
this.values.push(element.value);
72+
}
73+
}
6074
/**
6175
* @description Instantiates ScaleByteArray from u8[] SCALE encoded bytes (Decode)
6276
*/

assembly/Arrays/IntArray.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { AbstractArray } from "./AbstractArray";
1717

1818
import { DecodedData } from "../interfaces/DecodedData";
1919
import { ArrayUtils } from "../utils/Arrays";
20+
import { BytesReader } from "..";
2021

2122
// @ts-ignore
2223
export class IntArray extends AbstractArray<CompactInt, i64> {
@@ -33,6 +34,31 @@ export class IntArray extends AbstractArray<CompactInt, i64> {
3334
)
3435
}
3536

37+
/**
38+
* @description Returns encoded byte length of the type
39+
*/
40+
public encodedLength(): i32{
41+
let len: i32 = new CompactInt(this.values.length).encodedLength();
42+
for (let i: i32 = 0; i < this.values.length; i++){
43+
const value = new CompactInt(this.values[i]);
44+
len += value.encodedLength();
45+
}
46+
return len;
47+
}
48+
/**
49+
* @description Non-static constructor method used to populate defined properties of the model
50+
* @param bytes SCALE encoded bytes
51+
* @param index index to start decoding the bytes from
52+
*/
53+
populateFromBytes(bytes: u8[], index: i32 = 0): void {
54+
const bytesReader = new BytesReader(bytes.slice(index));
55+
const data = bytesReader.readInto<CompactInt>();
56+
57+
for(let i: i32 = 0; i < data.value; i++){
58+
const element: CompactInt = bytesReader.readInto<CompactInt>();
59+
this.values.push(element.value);
60+
}
61+
}
3662
/**
3763
* @description Instantiates ScaleIntArray from u8[] SCALE encoded bytes (Decode)
3864
*/

assembly/Arrays/StringArray.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { ScaleString } from "../ScaleString";
1818

1919
import { DecodedData } from "../interfaces/DecodedData";
2020
import { ArrayUtils } from "../utils/Arrays";
21+
import { BytesReader, CompactInt } from "..";
2122

2223
// @ts-ignore
2324
export class StringArray extends AbstractArray<ScaleString, string>{
@@ -34,6 +35,33 @@ export class StringArray extends AbstractArray<ScaleString, string>{
3435
encodedStringLength
3536
)
3637
}
38+
39+
/**
40+
* @description Returns encoded byte length of the type
41+
*/
42+
public encodedLength(): i32{
43+
let len: i32 = new CompactInt(this.values.length).encodedLength();
44+
for (let i: i32 = 0; i < this.values.length; i++){
45+
const value = new ScaleString(this.values[i]);
46+
len += value.encodedLength();
47+
}
48+
return len;
49+
}
50+
51+
/**
52+
* @description Non-static constructor method used to populate defined properties of the model
53+
* @param bytes SCALE encoded bytes
54+
* @param index index to start decoding the bytes from
55+
*/
56+
populateFromBytes(bytes: u8[], index: i32 = 0): void {
57+
const bytesReader = new BytesReader(bytes.slice(index));
58+
const data = bytesReader.readInto<CompactInt>();
59+
60+
for(let i: i32 = 0; i < data.value; i++){
61+
const element: ScaleString = bytesReader.readInto<ScaleString>();
62+
this.values.push(element.valueStr);
63+
}
64+
}
3765

3866
/**
3967
* @description Instantiates ScaleStringArray from u8[] SCALE encoded bytes (Decode)

assembly/Arrays/UInt128Array.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { DecodedData } from "../interfaces/DecodedData";
1717
import { UInt128 } from "../UInt/UInt128";
1818
import { u128 } from "as-bignum";
1919
import { ArrayUtils } from "../utils/Arrays";
20+
import { BytesReader, CompactInt } from "..";
21+
import { BIT_LENGTH } from "../utils/Bytes";
2022

2123
// @ts-ignore
2224
export class UInt128Array extends AbstractArray<UInt128, u128> {
@@ -33,13 +35,34 @@ export class UInt128Array extends AbstractArray<UInt128, u128> {
3335
)
3436
}
3537

38+
/**
39+
* @description Non-static constructor method used to populate defined properties of the model
40+
* @param bytes SCALE encoded bytes
41+
* @param index index to start decoding the bytes from
42+
*/
43+
populateFromBytes(bytes: u8[], index: i32 = 0): void {
44+
const bytesReader = new BytesReader(bytes.slice(index));
45+
const data = bytesReader.readInto<CompactInt>();
46+
for(let i: i32 = 0; i < data.value; i++){
47+
const element: UInt128 = bytesReader.readInto<UInt128>();
48+
this.values.push(element.value);
49+
}
50+
}
51+
3652
/**
3753
* @description Instantiates ScaleIntArray from u8[] SCALE encoded bytes (Decode)
3854
*/
3955
static fromU8a (input: u8[]): UInt128Array {
4056
return AbstractArray.fromU8a<UInt128Array>(input);
4157
}
4258

59+
/**
60+
* @description Returns encoded byte length of the type
61+
*/
62+
public encodedLength(): i32{
63+
return (new CompactInt(this.values.length).encodedLength()) + super.values.length * BIT_LENGTH.INT_128;
64+
}
65+
4366
@inline @operator('==')
4467
static eq(a: UInt128Array, b: UInt128Array): bool {
4568
return ArrayUtils.areEqual(a, b);

assembly/Bool.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ import { Codec } from "./interfaces/Codec";
1717
/** Representation for a boolean value in the system. */
1818
export class Bool implements Codec {
1919

20-
public readonly value: bool;
20+
private _value: bool;
2121

22-
constructor (value: bool) {
23-
this.value = value;
22+
get value(): bool{
23+
return this._value;
24+
}
25+
26+
constructor (value: bool = false) {
27+
this._value = value;
2428
}
2529

2630
/** Encodes the value as u8[] as per the SCALE codec specification
@@ -33,6 +37,16 @@ export class Bool implements Codec {
3337
return bytesEncoded;
3438
}
3539

40+
/**
41+
* @description Non-static constructor method used to populate defined properties of the model.
42+
* @param bytes SCALE encoded bytes
43+
* @param index index to start decoding the bytes from
44+
*/
45+
public populateFromBytes(bytes: u8[], index: i32 = 0): void{
46+
assert(bytes.length > 0 && (bytes[index] == 1 || bytes[index] == 0), 'Bool: Cannot decode invalid input');
47+
this._value = bytes[index] == 1;
48+
}
49+
3650
/**
3751
* @description Returns the string representation of the value
3852
*/
@@ -49,7 +63,7 @@ export class Bool implements Codec {
4963

5064
/** Instantiates new Bool from u8[] SCALE encoded bytes */
5165
static fromU8a (value: u8[], index: i32 = 0): Bool {
52-
assert(value.length > 0 && (value[index] == 1 || value[index] == 0), 'Bool: Cannot decode invalid input');
66+
assert(value.length - index > 0 && (value[index] == 1 || value[index] == 0), 'Bool: Cannot decode invalid input');
5367

5468
return new Bool(value[index] == 1);
5569
}

assembly/Byte.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ import { BIT_LENGTH } from "./utils/Bytes";
1717

1818
export class Byte implements Codec {
1919

20-
public readonly value: u8;
20+
private _value: u8;
2121
protected bitLength: i32;
2222

23-
constructor (value: u8) {
24-
this.value = value;
23+
get value(): u8{
24+
return this._value;
25+
}
26+
27+
constructor (value: u8 = 0) {
28+
this._value = value;
2529
this.bitLength = BIT_LENGTH.INT_8;
2630
}
2731

@@ -31,6 +35,15 @@ export class Byte implements Codec {
3135
public toU8a (): u8[] {
3236
return [this.value];
3337
}
38+
/**
39+
* @description Non-static constructor method used to populate defined properties of the model
40+
* @param bytes SCALE encoded bytes
41+
* @param index index to start decoding the bytes from
42+
*/
43+
public populateFromBytes(bytes: u8[], index: i32 = 0): void{
44+
assert(bytes.length - index > 0, 'Bool: Cannot decode invalid input');
45+
this._value = bytes[index];
46+
}
3447

3548
/**
3649
* @description The length of Byte when the value is encoded
@@ -40,9 +53,9 @@ export class Byte implements Codec {
4053
}
4154

4255
/** Instantiates new Byte from u8[] SCALE encoded bytes */
43-
static fromU8a (value: u8[]): Byte {
44-
assert(value.length == 1, 'Byte: cannot decode invalid type');
45-
return new Byte(value[0]);
56+
static fromU8a (value: u8[], index: i32 = 0): Byte {
57+
assert(value.length - index > 0, 'Byte: cannot decode invalid type');
58+
return new Byte(value[index]);
4659
}
4760

4861
@inline @operator('==')

0 commit comments

Comments
 (0)