|
| 1 | +import { Actor, HttpAgent, type HttpAgentOptions, type ActorConfig, type Agent, type ActorSubclass } from "@icp-sdk/core/agent"; |
| 2 | +import type { Principal } from "@icp-sdk/core/principal"; |
| 3 | +import { idlFactory, type _SERVICE } from "./declarations/malicious_doc.did"; |
| 4 | +export interface Some<T> { |
| 5 | + __kind__: "Some"; |
| 6 | + value: T; |
| 7 | +} |
| 8 | +export interface None { |
| 9 | + __kind__: "None"; |
| 10 | +} |
| 11 | +export type Option<T> = Some<T> | None; |
| 12 | +function some<T>(value: T): Some<T> { |
| 13 | + return { |
| 14 | + __kind__: "Some", |
| 15 | + value: value |
| 16 | + }; |
| 17 | +} |
| 18 | +function none(): None { |
| 19 | + return { |
| 20 | + __kind__: "None" |
| 21 | + }; |
| 22 | +} |
| 23 | +function isNone<T>(option: Option<T>): option is None { |
| 24 | + return option.__kind__ === "None"; |
| 25 | +} |
| 26 | +function isSome<T>(option: Option<T>): option is Some<T> { |
| 27 | + return option.__kind__ === "Some"; |
| 28 | +} |
| 29 | +function unwrap<T>(option: Option<T>): T { |
| 30 | + if (isNone(option)) { |
| 31 | + throw new Error("unwrap: none"); |
| 32 | + } |
| 33 | + return option.value; |
| 34 | +} |
| 35 | +function candid_some<T>(value: T): [T] { |
| 36 | + return [ |
| 37 | + value |
| 38 | + ]; |
| 39 | +} |
| 40 | +function candid_none<T>(): [] { |
| 41 | + return []; |
| 42 | +} |
| 43 | +function record_opt_to_undefined<T>(arg: T | null): T | undefined { |
| 44 | + return arg == null ? undefined : arg; |
| 45 | +} |
| 46 | +export interface MaliciousType { |
| 47 | + /** |
| 48 | + * Doc comment for field with *\/ malicious code /* in it |
| 49 | + */ |
| 50 | + field: string; |
| 51 | +} |
| 52 | +/** |
| 53 | + * Service with *\/ malicious *\/ doc |
| 54 | + */ |
| 55 | +export interface malicious_docInterface { |
| 56 | + /** |
| 57 | + * Method with *\/ in doc comment |
| 58 | + */ |
| 59 | + get(): Promise<MaliciousType>; |
| 60 | +} |
| 61 | +export class Malicious_doc implements malicious_docInterface { |
| 62 | + constructor(private actor: ActorSubclass<_SERVICE>){} |
| 63 | + async get(): Promise<MaliciousType> { |
| 64 | + const result = await this.actor.get(); |
| 65 | + return result; |
| 66 | + } |
| 67 | +} |
| 68 | +export interface CreateActorOptions { |
| 69 | + agent?: Agent; |
| 70 | + agentOptions?: HttpAgentOptions; |
| 71 | + actorOptions?: ActorConfig; |
| 72 | +} |
| 73 | +export function createActor(canisterId: string, options: CreateActorOptions = {}): Malicious_doc { |
| 74 | + const agent = options.agent || HttpAgent.createSync({ |
| 75 | + ...options.agentOptions |
| 76 | + }); |
| 77 | + if (options.agent && options.agentOptions) { |
| 78 | + console.warn("Detected both agent and agentOptions passed to createActor. Ignoring agentOptions and proceeding with the provided agent."); |
| 79 | + } |
| 80 | + const actor = Actor.createActor<_SERVICE>(idlFactory, { |
| 81 | + agent, |
| 82 | + canisterId: canisterId, |
| 83 | + ...options.actorOptions |
| 84 | + }); |
| 85 | + return new Malicious_doc(actor); |
| 86 | +} |
0 commit comments