Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add fetchCandid() function to @dfinity/agent (#630)
* Add fetchCandid() helper function to @dfinity/agent * Update changelog * Remove extraneous file from tsconfig * Use canister metadata in fetchCandid() * Add 'isLocal()' method to 'HttpAgent'
- Loading branch information
Showing
5 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { fetchCandid, HttpAgent } from '.'; | ||
import { IDL } from '@dfinity/candid'; | ||
import * as cbor from './cbor'; | ||
|
||
test('simulate fetching a Candid interface', async () => { | ||
const mockFetch = jest.fn().mockImplementation((/*resource, init*/) => { | ||
return Promise.resolve( | ||
new Response( | ||
cbor.encode({ | ||
status: 'replied', | ||
reply: { | ||
arg: IDL.encode([IDL.Text], ['service {}']), | ||
}, | ||
}), | ||
{ | ||
status: 200, | ||
}, | ||
), | ||
); | ||
}); | ||
|
||
const agent = new HttpAgent({ fetch: mockFetch, host: 'http://localhost' }); | ||
|
||
const candid = await fetchCandid('ryjl3-tyaaa-aaaaa-aaaba-cai', agent); | ||
|
||
expect(candid).toMatch(/service/); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Actor, ActorSubclass, CanisterStatus, HttpAgent } from '.'; | ||
import { Principal } from '@dfinity/principal'; | ||
import { IDL } from '@dfinity/candid'; | ||
|
||
/** | ||
* Retrieves the Candid interface for the specified canister. | ||
* | ||
* @param agent The agent to use for the request (usually an `HttpAgent`) | ||
* @param canisterId A string corresponding to the canister ID | ||
* @returns Candid source code | ||
*/ | ||
export async function fetchCandid(canisterId: string, agent?: HttpAgent): Promise<string> { | ||
if (!agent) { | ||
// Create an anonymous `HttpAgent` (adapted from Candid UI) | ||
agent = new HttpAgent(); | ||
if (agent.isLocal()) { | ||
agent.fetchRootKey(); | ||
} | ||
} | ||
|
||
// Attempt to use canister metadata | ||
const status = await CanisterStatus.request({ | ||
agent, | ||
canisterId: Principal.fromText(canisterId), | ||
paths: ['candid'], | ||
}); | ||
const candid = status.get('candid') as string | undefined; | ||
if (candid) { | ||
return candid; | ||
} | ||
|
||
// Use `__get_candid_interface_tmp_hack` for canisters without Candid metadata | ||
const tmpHackInterface: IDL.InterfaceFactory = ({ IDL }) => | ||
IDL.Service({ | ||
__get_candid_interface_tmp_hack: IDL.Func([], [IDL.Text], ['query']), | ||
}); | ||
const actor: ActorSubclass = Actor.createActor(tmpHackInterface, { agent, canisterId }); | ||
return (await actor.__get_candid_interface_tmp_hack()) as string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters