Skip to content

Commit

Permalink
feat: new option for setting rootKey during agent creation (#918)
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock authored Aug 23, 2024
1 parent 5a21dae commit 458e243
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- feat: management canister interface updates for schnorr signatures
- feat: ensure that identity-secp256k1 seed phrase must produce a 64 byte seed
- docs: documentation and metadata for use-auth-client
- feat: adds optional `rootKey` to `HttpAgentOptions` to allow for a custom root key to be used for verifying signatures from other networks

### Changed
- feat: replaces hdkey and bip32 implementations with `@scure/bip39` and `@scure/bip32` due to vulnerability and lack of maintenance for `elliptic`
Expand Down
18 changes: 18 additions & 0 deletions e2e/node/basic/mainnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,21 @@ describe('call forwarding', () => {
expect(reply).toBeTruthy();
}, 15_000);
});


test('it should allow you to set an incorrect root key', async () => {
const agent = HttpAgent.createSync({
rootKey: new Uint8Array(31),
});
const idlFactory = ({ IDL }) =>
IDL.Service({
whoami: IDL.Func([], [IDL.Principal], ['query']),
});

const actor = Actor.createActor(idlFactory, {
agent,
canisterId: Principal.fromText('rrkah-fqaaa-aaaaa-aaaaq-cai'),
});

expect(actor.whoami).rejects.toThrowError(`Invalid certificate:`);
});
2 changes: 2 additions & 0 deletions packages/agent/src/agent/http/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,8 @@ test('retry requests that fail due to a network failure', async () => {
fetch: mockFetch,
});

agent.rootKey = new Uint8Array(32);

try {
await agent.call(Principal.managementCanister(), {
methodName: 'test',
Expand Down
8 changes: 7 additions & 1 deletion packages/agent/src/agent/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ export interface HttpAgentOptions {
* Whether to log to the console. Defaults to false.
*/
logToConsole?: boolean;

/**
* Alternate root key to use for verifying certificates. If not provided, the default IC root key will be used.
*/
rootKey?: ArrayBuffer;
}

function getDefaultFetch(): typeof fetch {
Expand Down Expand Up @@ -233,7 +238,7 @@ other computations so that this class can stay as simple as possible while
allowing extensions.
*/
export class HttpAgent implements Agent {
public rootKey = fromHex(IC_ROOT_KEY);
public rootKey: ArrayBuffer;
#identity: Promise<Identity> | null;
readonly #fetch: typeof fetch;
readonly #fetchOptions?: Record<string, unknown>;
Expand Down Expand Up @@ -275,6 +280,7 @@ export class HttpAgent implements Agent {
this.#fetch = options.fetch || getDefaultFetch() || fetch.bind(global);
this.#fetchOptions = options.fetchOptions;
this.#callOptions = options.callOptions;
this.rootKey = options.rootKey ? options.rootKey : fromHex(IC_ROOT_KEY);

const host = determineHost(options.host);
this.host = new URL(host);
Expand Down

0 comments on commit 458e243

Please sign in to comment.