Skip to content
This repository has been archived by the owner on Aug 8, 2022. It is now read-only.

fix(Hash): fix equaliy and toString #13

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions assembly/Hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export class Hash implements UnwrappableCodec<Array<u8>> {
* @description Returns the inner native value
*/
@inline
public unwrap(): Array<u8> {
unwrap(): Array<u8> {
return this._values;
}

/**
* @description Encodes Hash as u8[] as per the SCALE codec specification
*/
public toU8a(): u8[] {
toU8a(): u8[] {
const result: u8[] = new Array<u8>(this.encodedLength());
Bytes.copy<u8>(this._values, result);

Expand All @@ -46,7 +46,7 @@ export class Hash implements UnwrappableCodec<Array<u8>> {
* @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<u8>(32);
Bytes.copy(bytes, this._values, 0, index);
Expand All @@ -56,8 +56,19 @@ export class Hash implements UnwrappableCodec<Array<u8>> {
/**
* @description Return string representation of Hash
*/
public toString(): string {
return "0x" + this._values.join("");
toString(): string {
return (
"0x" +
this._values
.map<string>((v) => {
let res = v.toString(16);
if (res.length == 1) {
res = "0" + res;
}
return res;
})
.join("")
);
}

/**
Expand All @@ -78,7 +89,7 @@ export class Hash implements UnwrappableCodec<Array<u8>> {
* @description The length of encoded Hash
*/
@inline
public encodedLength(): i32 {
encodedLength(): i32 {
return 32;
}

Expand All @@ -92,31 +103,19 @@ export class Hash implements UnwrappableCodec<Array<u8>> {
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);
}
}
6 changes: 3 additions & 3 deletions assembly/__tests__/Hash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,12 +703,12 @@ describe("Hash", () => {
it("should return hex representation of a hash", () => {
let hash: Hash = new Hash([0xff]);
expect<string>(hash.toString()).toStrictEqual(
"0x2550000000000000000000000000000000"
"0xff00000000000000000000000000000000000000000000000000000000000000"
);

hash = new Hash([0xff, 0x00, 0xff]);
expect<string>(hash.toString()).toStrictEqual(
"0x255025500000000000000000000000000000"
"0xff00ff0000000000000000000000000000000000000000000000000000000000"
);

hash = new Hash([
Expand Down Expand Up @@ -746,7 +746,7 @@ describe("Hash", () => {
0xff,
]);
expect<string>(hash.toString()).toStrictEqual(
"0x255000000000000000000000000000000255"
"0xff000000000000000000000000000000000000000000000000000000000000ff"
);
});

Expand Down