Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/userlib/js/src/canisterId.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Canister IDs are represented as u64 in the HTTP handler of the client.
// Canister IDs are represented as an array of bytes in the HTTP handler of the client.
export class CanisterId {
public static fromText(hex: string): CanisterId {
if (hex.startsWith('ic:')) {
Expand Down
9 changes: 8 additions & 1 deletion src/userlib/js/src/cbor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ test('round trip', () => {
const { c: outputC, e: outputE, f: outputF, ...outputRest } = output;

expect(blobToHex(outputC)).toBe(blobToHex(inputC));
expect(((outputE as any) as BigNumber).toString(16).toUpperCase()).toBe(inputE.toHex());
expect(buf2hex((outputE as any) as Uint8Array).toUpperCase()).toBe(inputE.toHex());
expect(outputRest).toEqual(inputRest);
});

function buf2hex(buffer: Uint8Array) {
// Construct an array such that each number is translated to the
// hexadecimal equivalent, ensure it is a string and padded then
// join the elements.
return Array.prototype.map.call(buffer, x => ('00' + x.toString(16)).slice(-2)).join('');
}
15 changes: 4 additions & 11 deletions src/userlib/js/src/cbor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,11 @@ class CanisterIdEncoder implements CborEncoder<CanisterId> {
}

public encode(v: CanisterId): cbor.CborValue {
return cbor.value.u64(this.changeEndianness(v.toHex()), 16);
}

// Drop once we use https://github.com/dfinity-lab/dfinity/pull/2286
private changeEndianness(str: string): string {
const result = [];
let len = str.length - 2;
while (len >= 0) {
result.push(str.substr(len, 2));
len -= 2;
const h = v.toHex().match(/.{1,2}/g);
if (!h) {
throw new Error('Provided Canister id is not a array of bytes');
}
return result.join('');
return cbor.value.bytes(new Uint8Array(h.map(a => parseInt(a, 16))));
}
}

Expand Down
12 changes: 1 addition & 11 deletions src/userlib/js/src/request_id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,6 @@ export async function hash(data: BinaryBlob): Promise<BinaryBlob> {
return Buffer.from(hashed) as BinaryBlob;
}

const changeEndianness = (str: string): string => {
const result = [];
let len = str.length - 2;
while (len >= 0) {
result.push(str.substr(len, 2));
len -= 2;
}
return result.join('');
};

async function hashValue(value: unknown): Promise<Buffer> {
if (value instanceof borc.Tagged) {
return hashValue(value.value);
Expand All @@ -36,7 +26,7 @@ async function hashValue(value: unknown): Promise<Buffer> {
} else if (value instanceof CanisterId) {
// HTTP handler expects canister_id to be an u64 & hashed in this way.
// work-around for endianness problem until we switch to blobs
return hash(blobFromHex(changeEndianness(value.toHex())));
return hash(blobFromHex(value.toHex()));
} else if (value instanceof Buffer) {
return hash(new Uint8Array(value) as BinaryBlob);
} else if (value instanceof Uint8Array || value instanceof ArrayBuffer) {
Expand Down