From 4136244f8d37aacd406a3bd629f1e2017647d240 Mon Sep 17 00:00:00 2001 From: Hans Larsen Date: Tue, 7 Jul 2020 12:23:50 -0700 Subject: [PATCH] fix --- src/agent/javascript/src/canisterId.ts | 29 ++++++++++++++------------ src/bootstrap/src/host.ts | 12 +++++++---- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/agent/javascript/src/canisterId.ts b/src/agent/javascript/src/canisterId.ts index 5df710e533..a0ea53d8a8 100644 --- a/src/agent/javascript/src/canisterId.ts +++ b/src/agent/javascript/src/canisterId.ts @@ -9,25 +9,28 @@ function getCrc(hex: string): string { export class CanisterId { public static fromText(text: string): CanisterId { if (text.startsWith('ic:')) { - text = text.toUpperCase(); - const hex = text.slice(3); - if (hex.length >= 2 && hex.length % 2 === 0 && /^[0-9A-F]+$/.test(hex)) { - const id = hex.slice(0, -2); - const checksum = hex.slice(-2); - if (checksum !== getCrc(id)) { - throw new Error('Illegal CanisterId: ' + text); - } - return this.fromHex(id); - } else { - throw new Error('Cannot parse CanisterId: ' + text); - } + return this.fromHexWithChecksum(text.slice(3)); } else { throw new Error('CanisterId is not a "ic:" url: ' + text); } } + public static fromHexWithChecksum(hexWithChecksum: string): CanisterId { + const hex = hexWithChecksum.toUpperCase(); + if (hex.length >= 2 && hex.length % 2 === 0 && /^[0-9A-F]+$/.test(hex)) { + const id = hex.slice(0, -2); + const checksum = hex.slice(-2); + if (checksum !== getCrc(id)) { + throw new Error(`Invalid checksum for CanisterId: "ic:${hexWithChecksum}"`); + } + return new this(id); + } else { + throw new Error('Cannot parse CanisterId: ' + hexWithChecksum); + } + } + public static fromHex(hex: string): CanisterId { - return new this(hex); + return new this(hex.toUpperCase()); } public static fromBlob(blob: BinaryBlob): CanisterId { diff --git a/src/bootstrap/src/host.ts b/src/bootstrap/src/host.ts index 81cb27f027..e092dcf909 100644 --- a/src/bootstrap/src/host.ts +++ b/src/bootstrap/src/host.ts @@ -73,17 +73,21 @@ export class SiteInfo { const subdomain = components.slice(0, -3).join('.'); if (maybeIc0 === 'ic0' && maybeApp === 'app') { - return new SiteInfo(DomainKind.Ic0, CanisterId.fromHex(maybeCId), subdomain); + return new SiteInfo(DomainKind.Ic0, CanisterId.fromHexWithChecksum(maybeCId), subdomain); } else if (maybeIc0 === 'lvh' && maybeApp === 'me') { - return new SiteInfo(DomainKind.Lvh, CanisterId.fromHex(maybeCId), subdomain); + return new SiteInfo(DomainKind.Lvh, CanisterId.fromHexWithChecksum(maybeCId), subdomain); } else if (maybeIc0 === 'localhost' && maybeApp === undefined) { /// Allow subdomain of localhost. - return new SiteInfo(DomainKind.Localhost, CanisterId.fromHex(maybeCId), subdomain); + return new SiteInfo( + DomainKind.Localhost, + CanisterId.fromHexWithChecksum(maybeCId), + subdomain, + ); } else if (maybeApp === 'localhost') { /// Allow subdomain of localhost, but maybeIc0 is the canister ID. return new SiteInfo( DomainKind.Localhost, - CanisterId.fromHex(maybeIc0), + CanisterId.fromHexWithChecksum(maybeIc0), `${maybeCId}.${subdomain}`, ); } else {