UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

1 lines • 4.99 kB
{"version":3,"file":"signer.mjs","names":[],"sources":["../../../src/services/auth/signer.ts"],"sourcesContent":["import { Context, Layer } from \"effect\";\nimport { colors } from \"../../utils/theme\";\nimport type { NetworkId } from \"../registry/fastkv\";\nimport { ensureCustodyBalance, submitRegistryWriteCustody } from \"./custody-signer\";\nimport { ensureDelegateKey, submitRegistryWriteDelegated } from \"./delegate-signer\";\nimport { type SigningStrategy, submitRegistryWrite } from \"./near-signer\";\nimport type { CustodyWalletRecord, SessionCredential } from \"./session\";\n/**\n * The signing strategies as one Context.Service (ticket #423): the publish\n * train's custody / wallet-delegate / plain-key dispatch lives here instead\n * of inline in the train. The auth handles arrive pre-validated (preflight\n * guards once per run); the service dispatches and logs the strategy.\n */\n\nexport interface SignerServiceShape {\n submitRegistryWrite: typeof submitRegistryWrite;\n submitRegistryWriteDelegated: typeof submitRegistryWriteDelegated;\n submitRegistryWriteCustody: typeof submitRegistryWriteCustody;\n ensureDelegateKey: typeof ensureDelegateKey;\n ensureCustodyBalance: typeof ensureCustodyBalance;\n submit: (input: SubmitMode) => Promise<SubmitOutcome>;\n}\n\nexport type SubmitMode =\n | {\n mode: \"custody\";\n account: string;\n contract: string;\n args: Record<string, string>;\n custodyWallet: CustodyWalletRecord;\n idempotencyKey: string;\n }\n | {\n mode: \"wallet\";\n account: string;\n contract: string;\n network: NetworkId;\n args: Record<string, string>;\n credential: SessionCredential;\n configDir: string;\n }\n | {\n mode: \"key\";\n account: string;\n contract: string;\n network: NetworkId;\n args: Record<string, string>;\n privateKey?: string;\n strategy: SigningStrategy;\n };\n\nexport interface SubmitOutcome {\n success: boolean;\n txHash?: string;\n}\n\n/** The strategy dispatch, exported for the train's procedural deps. */\nexport const dispatchSubmit = (input: SubmitMode): Promise<SubmitOutcome> =>\n input.mode === \"custody\"\n ? (async () => {\n await ensureCustodyBalance(input.custodyWallet);\n console.log(\n ` Signing via ${colors.cyan(`OutLayer custody wallet ${input.custodyWallet.nearAccountId}`)}`,\n );\n return submitRegistryWriteCustody({\n contract: input.contract,\n args: input.args,\n custodyWallet: input.custodyWallet,\n idempotencyKey: input.idempotencyKey,\n });\n })()\n : input.mode === \"wallet\"\n ? (async () => {\n const record = await ensureDelegateKey({\n configDir: input.configDir,\n account: input.account,\n contract: input.contract,\n network: input.network,\n siteUrl: input.credential.siteUrl,\n });\n return submitRegistryWriteDelegated({\n account: input.account,\n contract: input.contract,\n network: input.network,\n args: input.args,\n delegatePrivateKey: record.privateKey,\n relayEndpoint: `${input.credential.siteUrl}/api/auth/near/relay`,\n apiKey: input.credential.apiKey,\n });\n })()\n : submitRegistryWrite(\n {\n account: input.account,\n contract: input.contract,\n method: \"__fastdata_kv\",\n args: input.args,\n network: input.network,\n privateKey: input.privateKey,\n },\n input.strategy,\n );\n\nexport class SignerService extends Context.Service<SignerService, SignerServiceShape>()(\n \"everything-dev/auth/signer\",\n) {\n static readonly Live = Layer.succeed(\n this,\n SignerService.of({\n submitRegistryWrite,\n submitRegistryWriteDelegated,\n submitRegistryWriteCustody,\n ensureDelegateKey,\n ensureCustodyBalance,\n submit: dispatchSubmit,\n }),\n );\n}\n"],"mappings":";;;;;;;;AAyDA,MAAa,kBAAkB,UAC7B,MAAM,SAAS,aACV,YAAY;CACX,MAAM,qBAAqB,MAAM,aAAa;CAC9C,QAAQ,IACN,iBAAiB,OAAO,KAAK,2BAA2B,MAAM,cAAc,eAAe,GAC7F;CACA,OAAO,2BAA2B;EAChC,UAAU,MAAM;EAChB,MAAM,MAAM;EACZ,eAAe,MAAM;EACrB,gBAAgB,MAAM;CACxB,CAAC;AACH,EAAC,CAAE,IACH,MAAM,SAAS,YACZ,YAAY;CACX,MAAM,SAAS,MAAM,kBAAkB;EACrC,WAAW,MAAM;EACjB,SAAS,MAAM;EACf,UAAU,MAAM;EAChB,SAAS,MAAM;EACf,SAAS,MAAM,WAAW;CAC5B,CAAC;CACD,OAAO,6BAA6B;EAClC,SAAS,MAAM;EACf,UAAU,MAAM;EAChB,SAAS,MAAM;EACf,MAAM,MAAM;EACZ,oBAAoB,OAAO;EAC3B,eAAe,GAAG,MAAM,WAAW,QAAQ;EAC3C,QAAQ,MAAM,WAAW;CAC3B,CAAC;AACH,EAAC,CAAE,IACH,oBACE;CACE,SAAS,MAAM;CACf,UAAU,MAAM;CAChB,QAAQ;CACR,MAAM,MAAM;CACZ,SAAS,MAAM;CACf,YAAY,MAAM;AACpB,GACA,MAAM,QACR;AAER,IAAa,gBAAb,MAAa,sBAAsB,QAAQ,QAA2C,CAAC,CACrF,4BACF,CAAC,CAAC;CACA,OAAgB,OAAO,MAAM,QAC3B,MACA,cAAc,GAAG;EACf;EACA;EACA;EACA;EACA;EACA,QAAQ;CACV,CAAC,CACH;AACF"}