|
| 1 | +import { Fr } from "@aztec/aztec.js/fields"; |
| 2 | +import { type Wallet, WalletSchema } from "@aztec/aztec.js/wallet"; |
| 3 | +import { schemas } from "@aztec/stdlib/schemas"; |
| 4 | +import { z } from "zod"; |
| 5 | +import { type ApiSchemaFor } from "@aztec/stdlib/schemas"; |
| 6 | +import { AccountTypes, type AccountType } from "../wallet/database/wallet-db"; |
| 7 | +import type { |
| 8 | + WalletInteraction, |
| 9 | + WalletInteractionType, |
| 10 | +} from "../wallet/types/wallet-interaction"; |
| 11 | +import { WalletInteractionSchema } from "../wallet/types/wallet-interaction"; |
| 12 | +import type { |
| 13 | + AuthorizationRequest, |
| 14 | + AuthorizationResponse, |
| 15 | +} from "../wallet/types/authorization"; |
| 16 | +import type { InternalAccount } from "../wallet/core/internal-wallet"; |
| 17 | +import type { DecodedExecutionTrace } from "../wallet/decoding/tx-callstack-decoder"; |
| 18 | + |
| 19 | +type OnWalletUpdateListener = (interaction: WalletInteraction<any>) => void; |
| 20 | +type OnAuthorizationRequestListener = (request: AuthorizationRequest) => void; |
| 21 | + |
| 22 | +// Zod schema for execution trace components |
| 23 | +const ContractInfoSchema = z.object({ |
| 24 | + name: z.string(), |
| 25 | + address: z.string(), |
| 26 | +}); |
| 27 | + |
| 28 | +const ArgValueSchema = z.object({ |
| 29 | + name: z.string(), |
| 30 | + value: z.string(), |
| 31 | +}); |
| 32 | + |
| 33 | +const PublicEnqueueEventSchema: z.ZodType<any> = z.object({ |
| 34 | + type: z.literal("public-enqueue"), |
| 35 | + depth: z.number(), |
| 36 | + counter: z.number(), |
| 37 | + contract: ContractInfoSchema, |
| 38 | + function: z.string(), |
| 39 | + caller: ContractInfoSchema, |
| 40 | + isStaticCall: z.boolean(), |
| 41 | +}); |
| 42 | + |
| 43 | +const PrivateCallEventSchema: z.ZodType<any> = z.lazy(() => |
| 44 | + z.object({ |
| 45 | + type: z.literal("private-call"), |
| 46 | + depth: z.number(), |
| 47 | + counter: z.object({ |
| 48 | + start: z.number(), |
| 49 | + end: z.number(), |
| 50 | + }), |
| 51 | + contract: ContractInfoSchema, |
| 52 | + function: z.string(), |
| 53 | + caller: ContractInfoSchema, |
| 54 | + isStaticCall: z.boolean(), |
| 55 | + args: z.array(ArgValueSchema), |
| 56 | + returnValues: z.array(ArgValueSchema), |
| 57 | + nestedEvents: z.array( |
| 58 | + z.union([PrivateCallEventSchema, PublicEnqueueEventSchema]) |
| 59 | + ), |
| 60 | + }) |
| 61 | +); |
| 62 | + |
| 63 | +const DecodedExecutionTraceSchema = z.union([ |
| 64 | + // Full transaction trace |
| 65 | + z.object({ |
| 66 | + privateExecution: PrivateCallEventSchema, |
| 67 | + publicExecutionQueue: z.array(PublicEnqueueEventSchema), |
| 68 | + }), |
| 69 | + // Simplified utility trace |
| 70 | + z.object({ |
| 71 | + functionName: z.string(), |
| 72 | + args: z.any(), |
| 73 | + contractAddress: z.string(), |
| 74 | + contractName: z.string(), |
| 75 | + result: z.any(), |
| 76 | + isUtility: z.literal(true), |
| 77 | + }), |
| 78 | +]); |
| 79 | + |
| 80 | +// Internal wallet interface - extends external with internal-only methods |
| 81 | +export type InternalWalletInterface = Omit<Wallet, "getAccounts"> & { |
| 82 | + createAccount( |
| 83 | + alias: string, |
| 84 | + type: AccountType, |
| 85 | + secret: Fr, |
| 86 | + salt: Fr, |
| 87 | + signingKey: Buffer |
| 88 | + ): Promise<void>; |
| 89 | + getAccounts(): Promise<InternalAccount[]>; // Override with enriched type |
| 90 | + getInteractions(): Promise<WalletInteraction<WalletInteractionType>[]>; |
| 91 | + getExecutionTrace( |
| 92 | + interactionId: string |
| 93 | + ): Promise<DecodedExecutionTrace | undefined>; |
| 94 | + resolveAuthorization(response: AuthorizationResponse): void; |
| 95 | + onWalletUpdate(callback: OnWalletUpdateListener): void; |
| 96 | + onAuthorizationRequest(callback: OnAuthorizationRequestListener): void; |
| 97 | + // App authorization management |
| 98 | + listAuthorizedApps(): Promise<string[]>; |
| 99 | + getAppAuthorizations(appId: string): Promise<{ |
| 100 | + accounts: { alias: string; item: string }[]; |
| 101 | + simulations: Array<{ |
| 102 | + type: "simulateTx" | "simulateUtility"; |
| 103 | + payloadHash: string; |
| 104 | + title?: string; |
| 105 | + key: string; |
| 106 | + }>; |
| 107 | + otherMethods: string[]; |
| 108 | + }>; |
| 109 | + updateAccountAuthorization( |
| 110 | + appId: string, |
| 111 | + accounts: { alias: string; item: string }[] |
| 112 | + ): Promise<void>; |
| 113 | + revokeAuthorization(key: string): Promise<void>; |
| 114 | + revokeAppAuthorizations(appId: string): Promise<void>; |
| 115 | +}; |
| 116 | + |
| 117 | +export const InternalWalletInterfaceSchema: ApiSchemaFor<InternalWalletInterface> = |
| 118 | + { |
| 119 | + ...WalletSchema, |
| 120 | + // @ts-ignore Annoying zod error |
| 121 | + createAccount: z |
| 122 | + .function() |
| 123 | + .args( |
| 124 | + z.string(), |
| 125 | + z.enum(AccountTypes), |
| 126 | + schemas.Fr, |
| 127 | + schemas.Fr, |
| 128 | + schemas.Buffer |
| 129 | + ), |
| 130 | + // @ts-ignore - Type inference for enriched InternalAccount with type field |
| 131 | + getAccounts: z |
| 132 | + .function() |
| 133 | + .args() |
| 134 | + .returns( |
| 135 | + z.array( |
| 136 | + z.object({ |
| 137 | + alias: z.string(), |
| 138 | + item: schemas.AztecAddress, |
| 139 | + type: z.enum(AccountTypes), |
| 140 | + }) |
| 141 | + ) |
| 142 | + ), |
| 143 | + getInteractions: z |
| 144 | + .function() |
| 145 | + .args() |
| 146 | + .returns(z.array(WalletInteractionSchema)), |
| 147 | + // @ts-ignore |
| 148 | + getExecutionTrace: z |
| 149 | + .function() |
| 150 | + .args(z.string()) |
| 151 | + .returns(DecodedExecutionTraceSchema.optional()), |
| 152 | + // @ts-ignore |
| 153 | + resolveAuthorization: z.function().args( |
| 154 | + z.object({ |
| 155 | + id: z.string(), |
| 156 | + approved: z.boolean(), |
| 157 | + appId: z.string(), |
| 158 | + itemResponses: z.record(z.any()), |
| 159 | + }) |
| 160 | + ), |
| 161 | + // App authorization management |
| 162 | + listAuthorizedApps: z.function().args().returns(z.array(z.string())), |
| 163 | + // @ts-ignore |
| 164 | + getAppAuthorizations: z |
| 165 | + .function() |
| 166 | + .args(z.string()) |
| 167 | + .returns( |
| 168 | + z.object({ |
| 169 | + accounts: z.array(z.object({ alias: z.string(), item: z.string() })), |
| 170 | + simulations: z.array( |
| 171 | + z.object({ |
| 172 | + type: z.enum(["simulateTx", "simulateUtility"]), |
| 173 | + payloadHash: z.string(), |
| 174 | + title: z.string().optional(), |
| 175 | + key: z.string(), |
| 176 | + }) |
| 177 | + ), |
| 178 | + otherMethods: z.array(z.string()), |
| 179 | + }) |
| 180 | + ), |
| 181 | + // @ts-ignore |
| 182 | + updateAccountAuthorization: z |
| 183 | + .function() |
| 184 | + .args( |
| 185 | + z.string(), |
| 186 | + z.array(z.object({ alias: z.string(), item: z.string() })) |
| 187 | + ), |
| 188 | + // @ts-ignore |
| 189 | + revokeAuthorization: z.function().args(z.string()), |
| 190 | + // @ts-ignore |
| 191 | + revokeAppAuthorizations: z.function().args(z.string()), |
| 192 | + }; |
0 commit comments