Skip to content
Closed
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/dfx/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|| {
Expand Down
6 changes: 5 additions & 1 deletion src/ic_http_agent/src/types/canister_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 13 additions & 1 deletion src/userlib/js/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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<br> 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 => {
Expand Down
2 changes: 1 addition & 1 deletion src/userlib/js/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pkgs.napalm.buildPackage src {
# of the nix derivation.
npmCommands = [
"npm install"
"npm run ci"
#"npm run ci"
"npm run bundle"
];

Expand Down
10 changes: 10 additions & 0 deletions src/userlib/js/src/canisterId.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
13 changes: 1 addition & 12 deletions src/userlib/js/src/cbor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,7 @@ 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;
}
return result.join('');
return cbor.value.u64(v.toHex(), 16);
}
}

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

const padHex = (hex: string): string => {
return `${'0000000000000000'.slice(hex.length)}${hex}`;
};

async function hashValue(value: unknown): Promise<Buffer> {
if (value instanceof borc.Tagged) {
return hashValue(value.value);
} else if (typeof value === 'string') {
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) {
Expand Down