From 6cad1c3897c29dfca81378823e525f41734eba70 Mon Sep 17 00:00:00 2001 From: Kyle Peacock Date: Fri, 29 Sep 2023 10:12:34 -0700 Subject: [PATCH] more cleanup --- packages/agent/src/agent/http/http.test.ts | 14 -------------- packages/agent/src/canisterStatus/index.ts | 12 ++++++++---- packages/agent/src/certificate.ts | 7 ++++--- 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/packages/agent/src/agent/http/http.test.ts b/packages/agent/src/agent/http/http.test.ts index c7377066d..1df6284c7 100644 --- a/packages/agent/src/agent/http/http.test.ts +++ b/packages/agent/src/agent/http/http.test.ts @@ -811,17 +811,3 @@ describe('default host', () => { } }); }); - -describe('fetchSubnetKeys', () => { - it.skip('should fetch node keys on application canisters for mainnet.', async () => { - jest.useFakeTimers(); - // const abbbb = (await import('isomorphic-fetch')).default; - // const agent = new HttpAgent({ fetch: abbbb, host: 'https://icp-api.io' }); - // await agent.fetchRootKey(); - // const subnetKeys = await agent.fetchSubnetKeys('erxue-5aaaa-aaaab-qaagq-cai'); //? - // const subnetKeys = await agent.fetchSubnetKeys('ryjl3-tyaaa-aaaaa-aaaba-cai'); //? - - // docs bkyz2-fmaaa-aaaaa-qaaaq-cai - // docs system bnz7o-iuaaa-aaaaa-qaaaa-cai - }); -}); diff --git a/packages/agent/src/canisterStatus/index.ts b/packages/agent/src/canisterStatus/index.ts index b40253ccb..7c92b1da0 100644 --- a/packages/agent/src/canisterStatus/index.ts +++ b/packages/agent/src/canisterStatus/index.ts @@ -2,7 +2,12 @@ import { Principal } from '@dfinity/principal'; import { AgentError } from '../errors'; import { HttpAgent } from '../agent/http'; -import { Certificate, CreateCertificateOptions, SubnetStatus } from '../certificate'; +import { + Certificate, + CreateCertificateOptions, + SubnetStatus, + lookupResultToBuffer, +} from '../certificate'; import { toHex } from '../utils/buffer'; import * as Cbor from '../cbor'; import { decodeLeb128, decodeTime } from '../utils/leb'; @@ -116,7 +121,7 @@ export const request = async (options: { } else { return { path: path, - data: cert.lookup(encodePath(path, canisterId)), + data: lookupResultToBuffer(cert.lookup(encodePath(path, canisterId))), }; } }; @@ -131,7 +136,7 @@ export const request = async (options: { } else { status.set(path.key, null); } - } else if (!Array.isArray(data)) { + } else { switch (path) { case 'time': { status.set(path, decodeTime(data)); @@ -181,7 +186,6 @@ export const request = async (options: { } } } catch (error) { - error; // Break on signature verification errors if ((error as AgentError)?.message?.includes('Invalid certificate')) { throw new AgentError((error as AgentError).message); diff --git a/packages/agent/src/certificate.ts b/packages/agent/src/certificate.ts index 5b193fc1e..3682c9b9b 100644 --- a/packages/agent/src/certificate.ts +++ b/packages/agent/src/certificate.ts @@ -208,6 +208,7 @@ export class Certificate { } public lookup(path: Array): ArrayBuffer | undefined { + // constrain the type of the result, so that empty HashTree is undefined return lookupResultToBuffer(lookup_path(path, this.cert.tree)); } @@ -262,7 +263,7 @@ export class Certificate { let sigVer = false; const lookupTime = this.lookup(['time']); - if (!lookupTime || !(lookupTime instanceof ArrayBuffer)) { + if (!lookupTime) { // Should never happen - time is always present in IC certificates throw new CertificateVerificationError('Certificate does not contain a time'); } @@ -316,7 +317,7 @@ export class Certificate { }); const rangeLookup = cert.lookup(['subnet', d.subnet_id, 'canister_ranges']); - if (!rangeLookup || !(rangeLookup instanceof ArrayBuffer)) { + if (!rangeLookup) { throw new CertificateVerificationError( `Could not find canister ranges for subnet 0x${toHex(d.subnet_id)}`, ); @@ -338,7 +339,7 @@ export class Certificate { ); } const publicKeyLookup = cert.lookup(['subnet', d.subnet_id, 'public_key']); - if (!publicKeyLookup || !(publicKeyLookup instanceof ArrayBuffer)) { + if (!publicKeyLookup) { throw new Error(`Could not find subnet key for subnet 0x${toHex(d.subnet_id)}`); } return publicKeyLookup;