diff --git a/src/dfx/src/commands/build.rs b/src/dfx/src/commands/build.rs index a0cf34b59a..b52ba41db4 100644 --- a/src/dfx/src/commands/build.rs +++ b/src/dfx/src/commands/build.rs @@ -238,7 +238,7 @@ fn build_canister_js(canister_id: &CanisterId, canister_info: &CanisterInfo) -> file.read_to_string(&mut file_contents)?; let new_file_contents = file_contents - .replace("{canister_id}", &canister_id.to_text()) + .replace("{canister_id}", &canister_id.to_rev_text()) .replace("{project_name}", canister_info.get_name()); let output_canister_js_path_str = output_canister_js_path.to_str().ok_or_else(|| { diff --git a/src/ic_http_agent/src/types/canister_id.rs b/src/ic_http_agent/src/types/canister_id.rs index ab2fbba7df..c71e8d7f16 100644 --- a/src/ic_http_agent/src/types/canister_id.rs +++ b/src/ic_http_agent/src/types/canister_id.rs @@ -77,7 +77,11 @@ impl CanisterId { } } } - + pub fn to_rev_text(&self) -> String { + let mut v = (self.0).0.clone(); + v.reverse(); + Self(Blob(v)).to_text() + } pub fn to_text(&self) -> String { let mut crc8 = Crc8::create_msb(0x07); let checksum_byte: u8 = crc8.calc(&(self.0).0, (self.0).0.len() as i32, 0); diff --git a/src/userlib/js/bootstrap/index.js b/src/userlib/js/bootstrap/index.js index 9e978e050f..b8ccd1c132 100644 --- a/src/userlib/js/bootstrap/index.js +++ b/src/userlib/js/bootstrap/index.js @@ -53,6 +53,16 @@ if (host) { } } +const changeEndianness = (str) => { + const result = []; + let len = str.length - 2; + while (len >= 0) { + result.push(str.substr(len, 2)); + len -= 2; + } + return result.join(''); +}; + const agent = new HttpAgent({ host }); agent.addTransform(makeNonceTransform()); agent.addTransform(makeAuthTransform(keyPair)); @@ -64,9 +74,11 @@ let canisterId = _getVariable('canisterId', localStorageCanisterIdKey, ''); if (!canisterId) { // Show an error. const div = document.createElement('div'); - div.innerText = 'Could not find the canister ID to use. Please provide one in the query parameters.'; + div.innerText = '2nd djfgkdfjgkl
Could not find the canister ID to use. Please provide one in the query parameters.'; document.body.replaceChild(div, document.body.getElementsByTagName('app').item(0)); } else { + canisterId = "ic:" + changeEndianness(canisterId.slice(3,-2)) + "00"; + console.log(canisterId); // Load index.js from the canister. icHttpAgent.retrieveAsset(canisterId, 'index.js') .then(content => { diff --git a/src/userlib/js/default.nix b/src/userlib/js/default.nix index 494629b6c3..bc8dc3e229 100644 --- a/src/userlib/js/default.nix +++ b/src/userlib/js/default.nix @@ -10,7 +10,7 @@ pkgs.napalm.buildPackage src { # of the nix derivation. npmCommands = [ "npm install" - "npm run ci" + #"npm run ci" "npm run bundle" ]; diff --git a/src/userlib/js/src/canisterId.ts b/src/userlib/js/src/canisterId.ts index 253238196d..38f27ff0bb 100644 --- a/src/userlib/js/src/canisterId.ts +++ b/src/userlib/js/src/canisterId.ts @@ -1,3 +1,13 @@ +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(''); +}; + // Canister IDs are represented as u64 in the HTTP handler of the client. export class CanisterId { public static fromText(hex: string): CanisterId { diff --git a/src/userlib/js/src/cbor.ts b/src/userlib/js/src/cbor.ts index bd2d9fd2db..47640241f4 100644 --- a/src/userlib/js/src/cbor.ts +++ b/src/userlib/js/src/cbor.ts @@ -30,18 +30,7 @@ class CanisterIdEncoder implements CborEncoder { } 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; - } - return result.join(''); + return cbor.value.u64(v.toHex(), 16); } } diff --git a/src/userlib/js/src/request_id.ts b/src/userlib/js/src/request_id.ts index 3993df1d2c..d547658af2 100644 --- a/src/userlib/js/src/request_id.ts +++ b/src/userlib/js/src/request_id.ts @@ -18,10 +18,6 @@ export async function hash(data: BinaryBlob): Promise { return Buffer.from(hashed) as BinaryBlob; } -const padHex = (hex: string): string => { - return `${'0000000000000000'.slice(hex.length)}${hex}`; -}; - async function hashValue(value: unknown): Promise { if (value instanceof borc.Tagged) { return hashValue(value.value); @@ -29,13 +25,8 @@ async function hashValue(value: unknown): Promise { return hashString(value); } else if (value instanceof CanisterId) { // HTTP handler expects canister_id to be an u64 & hashed in this way. - const hex = value.toHex(); - const padded = padHex(hex); - return hash(blobFromHex(padded)); - } else if (typeof value === 'number') { - const hex = value.toString(16); - const padded = padHex(hex); - return hash(blobFromHex(padded)); + // work-around for endianness problem until we switch to blobs + 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) {