Skip to content

Commit 2ddecf6

Browse files
authored
fix: canisterStatus returns full list of controllers (#799)
* fix: canisterStatus returns full list of controllers * updating snapshots
1 parent 963ef63 commit 2ddecf6

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

docs/generated/changelog.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ <h1>Agent-JS Changelog</h1>
1212
<section>
1313
<h2>Version x.x.x</h2>
1414
<ul>
15+
<li>fix: canisterStatus returns full list of controllers</li>
1516
<ul>
1617
<strong>feat!: node signature verification</strong
1718
><br />

e2e/node/basic/mainnet.test.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Actor, AnonymousIdentity, HttpAgent, Identity } from '@dfinity/agent';
1+
import { Actor, AnonymousIdentity, HttpAgent, Identity, CanisterStatus } from '@dfinity/agent';
22
import { IDL } from '@dfinity/candid';
33
import { Ed25519KeyIdentity } from '@dfinity/identity';
44
import { Principal } from '@dfinity/principal';
5-
import { describe, it, expect, vi } from 'vitest';
5+
import { describe, it, expect, test, vi } from 'vitest';
66
import { makeAgent } from '../utils/agent';
77

88
const createWhoamiActor = async (identity: Identity) => {
@@ -113,3 +113,50 @@ describe('certified query', () => {
113113
`);
114114
});
115115
});
116+
117+
describe('controllers', () => {
118+
it('should return the controllers of a canister with multiple controllers', async () => {
119+
const agent = new HttpAgent({ host: 'https://icp-api.io' });
120+
const status = await CanisterStatus.request({
121+
// Whoami canister
122+
canisterId: Principal.from('ivcos-eqaaa-aaaab-qablq-cai'),
123+
agent: agent,
124+
paths: ['controllers'],
125+
});
126+
expect((status.get('controllers') as Principal[]).map(p => p.toText())).toMatchInlineSnapshot(`
127+
[
128+
"hgfyw-myaaa-aaaab-qaaoa-cai",
129+
"b73qn-rqaaa-aaaap-aazvq-cai",
130+
"aux4w-bi6yf-a3bhr-zydhx-qvf2p-ymdeg-ddvg6-gmobi-ct4dk-wf4xd-nae",
131+
"jhnlf-yu2dz-v7beb-c77gl-76tj7-shaqo-5qfvi-htvel-gzamb-bvzx6-yqe",
132+
]
133+
`);
134+
});
135+
it('should return the controllers of a canister with one controller', async () => {
136+
const agent = new HttpAgent({ host: 'https://icp-api.io' });
137+
const status = await CanisterStatus.request({
138+
// NNS Governance Canister
139+
canisterId: Principal.from('rrkah-fqaaa-aaaaa-aaaaq-cai'),
140+
agent: agent,
141+
paths: ['controllers'],
142+
});
143+
// Should be root canister
144+
expect((status.get('controllers') as Principal[]).map(p => p.toText())).toMatchInlineSnapshot(`
145+
[
146+
"r7inp-6aaaa-aaaaa-aaabq-cai",
147+
]
148+
`);
149+
});
150+
it('should return the controllers of a canister with no controllers', async () => {
151+
const agent = new HttpAgent({ host: 'https://icp-api.io' });
152+
const status = await CanisterStatus.request({
153+
// nomeata's capture-the-ic-token canister
154+
canisterId: Principal.from('fj6bh-taaaa-aaaab-qaacq-cai'),
155+
agent: agent,
156+
paths: ['controllers'],
157+
});
158+
expect((status.get('controllers') as Principal[]).map(p => p.toText())).toMatchInlineSnapshot(`
159+
[]
160+
`);
161+
});
162+
});

packages/agent/src/canisterStatus/__snapshots__/index.test.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
exports[`Canister Status utility should query canister controllers 1`] = `
44
Array [
5+
Object {
6+
"__principal__": "2vxsx-fae",
7+
},
58
Object {
69
"__principal__": "rwlgt-iiaaa-aaaaa-aaaaa-cai",
710
},
@@ -24,6 +27,9 @@ exports[`Canister Status utility should support multiple requests 1`] = `2022-05
2427

2528
exports[`Canister Status utility should support multiple requests 2`] = `
2629
Array [
30+
Object {
31+
"__principal__": "2vxsx-fae",
32+
},
2733
Object {
2834
"__principal__": "rwlgt-iiaaa-aaaaa-aaaaa-cai",
2935
},

packages/agent/src/canisterStatus/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,10 @@ const decodeUtf8 = (buf: ArrayBuffer): string => {
349349
return new TextDecoder().decode(buf);
350350
};
351351

352-
// Controllers are CBOR-encoded buffers, starting with a Tag we don't need
352+
// Controllers are CBOR-encoded buffers
353353
const decodeControllers = (buf: ArrayBuffer): Principal[] => {
354354
// eslint-disable-next-line @typescript-eslint/no-unused-vars
355-
const [tag, ...controllersRaw] = decodeCbor(buf);
355+
const controllersRaw = decodeCbor(buf);
356356
return controllersRaw.map((buf: ArrayBuffer) => {
357357
return Principal.fromUint8Array(new Uint8Array(buf));
358358
});

0 commit comments

Comments
 (0)