diff --git a/assembly/Hash.ts b/assembly/Hash.ts index e59f109..96e9242 100644 --- a/assembly/Hash.ts +++ b/assembly/Hash.ts @@ -27,14 +27,14 @@ export class Hash implements UnwrappableCodec> { * @description Returns the inner native value */ @inline - public unwrap(): Array { + unwrap(): Array { return this._values; } /** * @description Encodes Hash as u8[] as per the SCALE codec specification */ - public toU8a(): u8[] { + toU8a(): u8[] { const result: u8[] = new Array(this.encodedLength()); Bytes.copy(this._values, result); @@ -46,7 +46,7 @@ export class Hash implements UnwrappableCodec> { * @param bytes SCALE encoded bytes * @param index index to start decoding the bytes from */ - public populateFromBytes(bytes: u8[], index: i32 = 0): i32 { + populateFromBytes(bytes: u8[], index: i32 = 0): i32 { assert(bytes.length - index >= 0, "Hash: Empty bytes array provided"); this._values = new Array(32); Bytes.copy(bytes, this._values, 0, index); @@ -56,8 +56,19 @@ export class Hash implements UnwrappableCodec> { /** * @description Return string representation of Hash */ - public toString(): string { - return "0x" + this._values.join(""); + toString(): string { + return ( + "0x" + + this._values + .map((v) => { + let res = v.toString(16); + if (res.length == 1) { + res = "0" + res; + } + return res; + }) + .join("") + ); } /** @@ -78,7 +89,7 @@ export class Hash implements UnwrappableCodec> { * @description The length of encoded Hash */ @inline - public encodedLength(): i32 { + encodedLength(): i32 { return 32; } @@ -92,31 +103,19 @@ export class Hash implements UnwrappableCodec> { return new Hash(input.slice(index)); } - eq(other: Hash): bool { - let areEqual = true; + @operator("==") + eq(other: this): bool { for (let i = 0; i < this.unwrap().length; i++) { - if (this.unwrap()[i] != other.unwrap()[i]) { - areEqual = false; - break; + if (this._values[i] != other._values[i]) { + return false; } } - return areEqual; - } - - @inline - notEq(other: Hash): bool { - return !this.eq(other); - } - - @inline - @operator("==") - static eq(a: Hash, b: Hash): bool { - return a.eq(b); + return true; } @inline @operator("!=") - static notEq(a: Hash, b: Hash): bool { - return a.notEq(b); + notEq(other: this): bool { + return !this.eq(other); } } diff --git a/assembly/__tests__/Hash.spec.ts b/assembly/__tests__/Hash.spec.ts index 3edf6e6..af1947b 100644 --- a/assembly/__tests__/Hash.spec.ts +++ b/assembly/__tests__/Hash.spec.ts @@ -703,12 +703,12 @@ describe("Hash", () => { it("should return hex representation of a hash", () => { let hash: Hash = new Hash([0xff]); expect(hash.toString()).toStrictEqual( - "0x2550000000000000000000000000000000" + "0xff00000000000000000000000000000000000000000000000000000000000000" ); hash = new Hash([0xff, 0x00, 0xff]); expect(hash.toString()).toStrictEqual( - "0x255025500000000000000000000000000000" + "0xff00ff0000000000000000000000000000000000000000000000000000000000" ); hash = new Hash([ @@ -746,7 +746,7 @@ describe("Hash", () => { 0xff, ]); expect(hash.toString()).toStrictEqual( - "0x255000000000000000000000000000000255" + "0xff000000000000000000000000000000000000000000000000000000000000ff" ); });