better-near-auth 1.7.0 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/client.js CHANGED
@@ -497,7 +497,10 @@ const siwnClient = (config) => {
497
497
  getRelayerInfo: async (params) => {
498
498
  return await $fetch("/near/relayer-info", {
499
499
  method: "POST",
500
- body: params ?? {}
500
+ body: {
501
+ ...params ?? {},
502
+ network: params?.network ?? activeNetwork.get()
503
+ }
501
504
  });
502
505
  },
503
506
  relayHistory: async () => {
@@ -506,7 +509,10 @@ const siwnClient = (config) => {
506
509
  createSubAccount: async (params) => {
507
510
  return await $fetch("/near/create-sub-account", {
508
511
  method: "POST",
509
- body: params
512
+ body: {
513
+ ...params,
514
+ network: params.network ?? activeNetwork.get()
515
+ }
510
516
  });
511
517
  },
512
518
  checkSubAccountAvailability: async (params) => {
@@ -520,7 +526,10 @@ const siwnClient = (config) => {
520
526
  };
521
527
  return await $fetch("/near/check-sub-account-availability", {
522
528
  method: "POST",
523
- body: params
529
+ body: {
530
+ ...params,
531
+ network: params.network ?? activeNetwork.get()
532
+ }
524
533
  });
525
534
  },
526
535
  setNetwork: (network) => {
@@ -1 +1 @@
1
- {"version":3,"file":"client.js","names":[],"sources":["../src/types.ts","../src/client.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { AccountIdSchema, type AccountId } from \"near-kit/schemas\";\nimport type { AccountState, Near, TransactionBuilder } from \"near-kit\";\n\nexport type { AccountId };\n\nexport interface NearAccount {\n\tid: string;\n\tuserId: string;\n\taccountId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n\tpublicKey: string;\n\tisPrimary: boolean;\n\tcreatedAt: Date;\n}\n\nexport interface ListedNearAccount extends NearAccount {\n\tproviderId: \"siwn\";\n\tisActive: boolean;\n\tisAvailable: boolean;\n}\n\nexport interface ListAccountsResponseT {\n\taccounts: ListedNearAccount[];\n\tactiveAccount: ListedNearAccount | null;\n\tavailableAccounts: ListedNearAccount[];\n}\n\nexport interface SetPrimaryAccountResponseT extends ListAccountsResponseT {\n\tsuccess: boolean;\n\taccountId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n\tmessage: string;\n}\n\nexport const socialImageSchema = z.object({\n\turl: z.string().optional(),\n\tipfs_cid: z.string().optional(),\n});\n\nexport const profileSchema = z.object({\n\tname: z.string().optional(),\n\tdescription: z.string().optional(),\n\timage: socialImageSchema.optional(),\n\tbackgroundImage: socialImageSchema.optional(),\n\tlinktree: z.record(z.string(), z.string()).optional(),\n});\n\nexport type SocialImage = z.infer<typeof socialImageSchema>;\nexport type Profile = z.infer<typeof profileSchema>;\n\nconst signedMessageSchema = z.object({\n\taccountId: z.string(),\n\tpublicKey: z.string(),\n\tsignature: z.string(),\n\tstate: z.string().optional(),\n});\n\nexport const LinkAccountRequest = z.object({\n\tsignedMessage: signedMessageSchema,\n\tmessage: z.string(),\n\trecipient: z.string(),\n\tnonce: z.string(),\n\taccountId: AccountIdSchema,\n\tcallbackUrl: z.string().optional(),\n});\n\nexport const SetPrimaryAccountRequest = z.object({\n\taccountId: AccountIdSchema,\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n});\n\nexport const NonceRequest = z.object({\n\taccountId: AccountIdSchema,\n\tnetworkId: z.union([z.literal(\"mainnet\"), z.literal(\"testnet\")])\n});\n\nexport const VerifyRequest = z.object({\n\tsignedMessage: signedMessageSchema,\n\tmessage: z.string(),\n\trecipient: z.string(),\n\tnonce: z.string(),\n\taccountId: AccountIdSchema,\n\tcallbackUrl: z.string().optional(),\n});\n\nexport const RelayRequest = z.object({\n\tpayload: z.string(),\n});\nexport type RelayRequestT = z.infer<typeof RelayRequest>;\n\nexport const RelayResponse = z.object({\n\ttxHash: z.string(),\n\tstatus: z.enum([\"pending\", \"completed\", \"failed\"]),\n});\nexport type RelayResponseT = z.infer<typeof RelayResponse>;\n\nexport const RelayStatusResponse = z.object({\n\tstatus: z.enum([\"pending\", \"completed\", \"failed\"]),\n\tgasUsed: z.string().optional(),\n\toutcome: z.unknown().optional(),\n});\nexport type RelayStatusResponseT = z.infer<typeof RelayStatusResponse>;\n\nexport const ViewContractRequest = z.object({\n\tcontractId: z.string(),\n\tmethodName: z.string(),\n\targs: z.record(z.string(), z.any()).optional(),\n});\nexport type ViewContractRequestT = z.infer<typeof ViewContractRequest>;\n\nexport const NonceResponse = z.object({ nonce: z.string() });\nexport const VerifyResponse = z.object({\n\ttoken: z.string(),\n\tsuccess: z.literal(true),\n\tuser: z.object({\n\t\tid: z.string(),\n\t\taccountId: AccountIdSchema,\n\t\tnetwork: z.union([z.literal(\"mainnet\"), z.literal(\"testnet\")]),\n\t}),\n});\nexport const ProfileResponse = profileSchema.nullable();\nexport const ViewContractResponse = z.object({ result: z.unknown() });\n\nexport const ProfileRequest = z.object({\n\taccountId: AccountIdSchema.optional(),\n});\nexport type ProfileRequestT = z.infer<typeof ProfileRequest>;\n\nexport type NonceRequestT = z.infer<typeof NonceRequest>;\nexport type NonceResponseT = z.infer<typeof NonceResponse>;\nexport type SetPrimaryAccountRequestT = z.infer<typeof SetPrimaryAccountRequest>;\nexport type VerifyRequestT = z.infer<typeof VerifyRequest>;\nexport type VerifyResponseT = z.infer<typeof VerifyResponse>;\nexport type ProfileResponseT = z.infer<typeof ProfileResponse>;\nexport type ViewContractResponseT = z.infer<typeof ViewContractResponse>;\n\nexport const RelayedTransactionSchema = z.object({\n\tid: z.string(),\n\tuserId: z.string(),\n\ttxHash: z.string(),\n\tsenderId: z.string(),\n\treceiverId: z.string(),\n\tnetwork: z.string(),\n\tstatus: z.string(),\n\tgasUsed: z.string().optional(),\n\tcreatedAt: z.string(),\n\tupdatedAt: z.string().optional(),\n});\nexport type RelayedTransactionT = z.infer<typeof RelayedTransactionSchema>;\n\nexport const RelayHistoryResponse = z.object({\n\ttransactions: z.array(RelayedTransactionSchema),\n});\nexport type RelayHistoryResponseT = z.infer<typeof RelayHistoryResponse>;\n\nexport interface RelayedTransactionRecord {\n\tid: string;\n\tuserId: string;\n\ttxHash: string;\n\tsenderId: string;\n\treceiverId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n\tstatus: string;\n\tgasUsed?: string;\n\tcreatedAt: Date;\n\tupdatedAt: Date;\n}\n\nexport interface RelayerInfo extends AccountState {\n\taccountId: string;\n\tmode: \"ephemeral\" | \"explicit\";\n\tnetwork: \"mainnet\" | \"testnet\";\n\tpublicKey: string;\n\thasKey: boolean;\n\tcreatedAt?: Date;\n\tlastUsedAt?: Date;\n\tparentAccount?: string;\n\tsubAccountAvailable?: boolean;\n}\n\nexport interface DualNetworkConfig<T> {\n\tmainnet: T;\n\ttestnet: T;\n}\n\nexport const GetRelayerInfoRequest = z.object({\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n});\nexport type GetRelayerInfoRequestT = z.infer<typeof GetRelayerInfoRequest>;\n\nexport interface SubAccountRelayerFCAKConfig {\n\treceiverId: string;\n\tmethodNames?: string[];\n\tallowance?: string;\n}\n\nexport interface SubAccountTxCtx {\n\tnewAccountId: string;\n\tparentAccount: string;\n\tuserPublicKey: string;\n\tuserAccountId: string;\n\tuserId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n}\n\nexport interface SubAccountLifecycleCtx extends SubAccountTxCtx {\n\tnear: Near;\n}\n\nexport interface SubAccountConfig {\n\tparentAccount?: string;\n\tminDeposit?: string;\n\tparentHasFullAccess?: boolean;\n\tdeploy?:\n\t\t| { wasm: Uint8Array }\n\t\t| { fromPublished: { accountId?: string; codeHash?: string } };\n\tinit?: {\n\t\tmethodName: string;\n\t\targs: object | ((ctx: SubAccountTxCtx) => object);\n\t};\n\textendTx?: (tx: TransactionBuilder, ctx: SubAccountTxCtx) => TransactionBuilder;\n\tonCreated?: (ctx: SubAccountLifecycleCtx) => Promise<void>;\n\tonRollback?: (ctx: SubAccountLifecycleCtx) => Promise<void>;\n\taddRelayerFCAK?: boolean;\n\trelayerFCAK?: SubAccountRelayerFCAKConfig;\n}\n\nexport const SUB_ACCOUNT_LABEL_REGEX = /^([a-z\\d]+[-_])*[a-z\\d]+$/;\n\nexport const SubAccountNameSchema = z\n\t.string()\n\t.min(2, \"Sub-account name must be at least 2 characters\")\n\t.max(64, \"Sub-account name must be at most 64 characters\")\n\t.regex(SUB_ACCOUNT_LABEL_REGEX, \"Sub-account name must contain only lowercase alphanumeric characters, hyphens, and underscores (no leading/trailing hyphens or underscores)\");\n\nexport const CreateSubAccountRequest = z.object({\n\tsubAccountName: SubAccountNameSchema,\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n\tpublicKey: z.string(),\n});\nexport type CreateSubAccountRequestT = z.infer<typeof CreateSubAccountRequest>;\n\nexport const CreateSubAccountResponse = z.object({\n\tsuccess: z.literal(true),\n\taccountId: z.string(),\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]),\n\tpublicKey: z.string(),\n\tmessage: z.string(),\n});\nexport type CreateSubAccountResponseT = z.infer<typeof CreateSubAccountResponse>;\n\nexport const CheckSubAccountAvailabilityRequest = z.object({\n\tsubAccountName: SubAccountNameSchema,\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n});\nexport type CheckSubAccountAvailabilityRequestT = z.infer<typeof CheckSubAccountAvailabilityRequest>;\n\nexport const CheckSubAccountAvailabilityResponse = z.object({\n\tavailable: z.boolean(),\n\taccountId: z.string(),\n\tparentAccount: z.string().optional(),\n\treason: z.enum([\"taken\", \"invalid\", \"too-long\", \"not-configured\"]).optional(),\n});\nexport type CheckSubAccountAvailabilityResponseT = z.infer<typeof CheckSubAccountAvailabilityResponse>;\n","import { Near, fromNearConnect, generateNonce, TransactionBuilder } from \"near-kit\";\nimport type { Near as NearType, SignedMessage } from \"near-kit\";\nimport type { EventMap } from \"@hot-labs/near-connect\";\nimport { hex } from \"@scure/base\";\nimport type { BetterAuthClientPlugin, BetterAuthClientOptions, BetterFetch, BetterFetchOption, BetterFetchResponse, ClientStore } from \"better-auth/client\";\nimport { atom } from \"nanostores\";\nimport type { siwn } from \"./index.js\";\nimport { type AccountId, type DualNetworkConfig, type NonceRequestT, type NonceResponseT, type ProfileResponseT, type VerifyRequestT, type VerifyResponseT, type RelayResponseT, type RelayStatusResponseT, type NearAccount, type ListAccountsResponseT, type SetPrimaryAccountRequestT, type SetPrimaryAccountResponseT, type ViewContractRequestT, type ViewContractResponseT, type RelayerInfo, type RelayHistoryResponseT, type GetRelayerInfoRequestT, type CreateSubAccountRequestT, type CreateSubAccountResponseT, type CheckSubAccountAvailabilityRequestT, type CheckSubAccountAvailabilityResponseT, SUB_ACCOUNT_LABEL_REGEX } from \"./types.js\";\n\nexport interface AuthCallbacks {\n\tonSuccess?: () => void;\n\tonError?: (error: Error & { status?: number; code?: string }) => void;\n}\n\nexport interface SIWNClientConfig {\n\trecipient?: string;\n\trecipients?: DualNetworkConfig<string>;\n\tnetworkId?: \"mainnet\" | \"testnet\";\n\tcspNonce?: string;\n}\n\ninterface SignWithWalletResult {\n\tsignedMessage: SignedMessage;\n\taccountId: string;\n\tpublicKey: string;\n\tnonceHex: string;\n}\n\nexport interface SIWNClientActions {\n\tnear: {\n\t\tnonce: (params: NonceRequestT) => Promise<BetterFetchResponse<NonceResponseT>>;\n\t\tverify: (params: VerifyRequestT) => Promise<BetterFetchResponse<VerifyResponseT>>;\n\t\tgetProfile: (accountId?: AccountId) => Promise<BetterFetchResponse<ProfileResponseT>>;\n\t\tview: (params: ViewContractRequestT) => Promise<BetterFetchResponse<ViewContractResponseT>>;\n\t\tgetAccountId: () => string | null;\n\t\tgetState: () => { accountId: string | null; publicKey: string | null; networkId: string } | null;\n\t\tisWalletConnected: () => boolean;\n\t\tensureConnected: () => Promise<boolean>;\n\t\tdisconnect: () => Promise<void>;\n\t\tlink: (callbacks?: AuthCallbacks) => Promise<void>;\n\t\tunlink: (params: { accountId: string; network?: \"mainnet\" | \"testnet\" }) => Promise<BetterFetchResponse<{ success: boolean; message: string }>>;\n\t\tlistAccounts: () => Promise<BetterFetchResponse<ListAccountsResponseT>>;\n\t\tsetPrimaryAccount: (params: SetPrimaryAccountRequestT) => Promise<BetterFetchResponse<SetPrimaryAccountResponseT>>;\n\t\tbuildSignedDelegateAction: (receiverId: string, buildActions: (builder: TransactionBuilder, receiverId: string) => TransactionBuilder) => Promise<string>;\n\t\trelayTransaction: (params: { payload: string }) => Promise<BetterFetchResponse<RelayResponseT>>;\n\t\tgetRelayStatus: (txHash: string) => Promise<BetterFetchResponse<RelayStatusResponseT>>;\n\t\tgetRelayerInfo: (params?: GetRelayerInfoRequestT) => Promise<BetterFetchResponse<RelayerInfo & { enabled: boolean }>>;\n\t\trelayHistory: () => Promise<BetterFetchResponse<RelayHistoryResponseT>>;\n\t\tcreateSubAccount: (params: CreateSubAccountRequestT) => Promise<BetterFetchResponse<CreateSubAccountResponseT>>;\n\t\tcheckSubAccountAvailability: (params: CheckSubAccountAvailabilityRequestT) => Promise<BetterFetchResponse<CheckSubAccountAvailabilityResponseT>>;\n\t\tsetNetwork: (network: \"mainnet\" | \"testnet\") => void;\n\t\tgetNetwork: () => \"mainnet\" | \"testnet\";\n\t\tgetSupportedNetworks: () => (\"mainnet\" | \"testnet\")[];\n\t\tgetRecipient: (network?: \"mainnet\" | \"testnet\") => string;\n\t\tclient: NearType;\n\t};\n\tsignIn: {\n\t\tnear: (callbacks?: AuthCallbacks) => Promise<void>;\n\t};\n}\n\nexport interface SIWNClientPlugin extends BetterAuthClientPlugin {\n\tid: \"siwn\";\n\t$InferServerPlugin: ReturnType<typeof siwn>;\n\tgetAtoms: ($fetch: BetterFetch) => {\n\t\tnearState: ReturnType<typeof atom<NearState>>;\n\t\twalletConnected: ReturnType<typeof atom<boolean>>;\n\t\tactiveNetwork: ReturnType<typeof atom<\"mainnet\" | \"testnet\">>;\n\t};\n\tgetActions: ($fetch: BetterFetch, $store: ClientStore, options: BetterAuthClientOptions | undefined) => SIWNClientActions;\n}\n\ntype NearState = { accountId: string | null; publicKey: string | null; networkId: string } | null;\n\nexport const siwnClient = (config: SIWNClientConfig): SIWNClientPlugin => {\n\tconst nearState = atom<NearState>(null);\n\tconst walletConnected = atom<boolean>(false);\n\tconst activeNetwork = atom<\"mainnet\" | \"testnet\">(config.networkId || \"mainnet\");\n\n\tconst getRecipient = (network?: \"mainnet\" | \"testnet\"): string => {\n\t\tconst net = network || activeNetwork.get();\n\t\tif (config.recipients) return config.recipients[net];\n\t\treturn config.recipient ?? \"\";\n\t};\n\n\tconst getSupportedNetworks = (): (\"mainnet\" | \"testnet\")[] => {\n\t\tif (config.recipients) return [\"mainnet\", \"testnet\"];\n\t\tif (config.recipient) return [config.recipient.endsWith(\".testnet\") ? \"testnet\" : \"mainnet\"];\n\t\treturn [\"mainnet\"];\n\t};\n\n\tlet connectors = new Map<\"mainnet\" | \"testnet\", InstanceType<typeof import(\"@hot-labs/near-connect\").NearConnector>>();\n\tlet nearClients = new Map<\"mainnet\" | \"testnet\", Near>();\n\tlet initializedNetworks = new Set<\"mainnet\" | \"testnet\">();\n\tlet connectorModulePromise: Promise<typeof import(\"@hot-labs/near-connect\")> | null = null;\n\tlet initPromises = new Map<\"mainnet\" | \"testnet\", Promise<boolean>>();\n\n\tconst loadConnector = async () => {\n\t\tconnectorModulePromise ??= import(\"@hot-labs/near-connect\");\n\t\tconst { NearConnector } = await connectorModulePromise;\n\t\treturn NearConnector;\n\t};\n\n\tconst handleAccountConnection = async (accountId: string, publicKey?: string | null, network?: \"mainnet\" | \"testnet\") => {\n\t\tif (!accountId) return;\n\t\tconst net = network || activeNetwork.get();\n\t\tnearState.set({\n\t\t\taccountId,\n\t\t\tpublicKey: publicKey || null,\n\t\t\tnetworkId: net,\n\t\t});\n\t\twalletConnected.set(true);\n\t};\n\n\tconst initClientForNetwork = async (network: \"mainnet\" | \"testnet\", $fetch?: BetterFetch): Promise<boolean> => {\n\t\tif (initializedNetworks.has(network)) return true;\n\t\tif (initPromises.has(network)) return initPromises.get(network)!;\n\t\tif (typeof (globalThis as any).window === \"undefined\") return false;\n\n\t\tconst initPromise = (async () => {\n\t\t\tconst NearConnector = await loadConnector();\n\t\t\tconst connector = new NearConnector({ network, cspNonce: config.cspNonce });\n\t\t\tconnectors.set(network, connector);\n\n\t\t\tconst near = new Near({\n\t\t\t\tnetwork,\n\t\t\t\twallet: fromNearConnect(connector),\n\t\t\t});\n\t\t\tnearClients.set(network, near);\n\n\t\t\tconnector.on(\"wallet:signIn\", async (data: EventMap[\"wallet:signIn\"]) => {\n\t\t\t\tconst accountId = data.accounts?.[0]?.accountId;\n\t\t\t\tconst publicKey = data.accounts?.[0]?.publicKey;\n\t\t\t\tif (accountId) {\n\t\t\t\t\tawait handleAccountConnection(accountId, publicKey, network);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tconnector.on(\"wallet:signOut\", () => {\n\t\t\t\twalletConnected.set(false);\n\t\t\t\tconst state = nearState.get();\n\t\t\t\tif (state) {\n\t\t\t\t\tnearState.set({ accountId: state.accountId, publicKey: null, networkId: state.networkId });\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tvoid connector.getConnectedWallet().then(({ accounts }) => {\n\t\t\t\tconst account = accounts?.[0];\n\t\t\t\tconst net = activeNetwork.get();\n\t\t\t\tif (account?.accountId && !nearState.get()) {\n\t\t\t\t\tnearState.set({\n\t\t\t\t\t\taccountId: account.accountId,\n\t\t\t\t\t\tpublicKey: account.publicKey ?? null,\n\t\t\t\t\t\tnetworkId: net,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tif (account?.accountId) {\n\t\t\t\t\twalletConnected.set(true);\n\t\t\t\t}\n\t\t\t}).catch(() => {});\n\n\t\t\tinitializedNetworks.add(network);\n\n\t\t\tif ($fetch) {\n\t\t\t\tvoid restoreFromSession($fetch);\n\t\t\t}\n\n\t\t\treturn true;\n\t\t})();\n\n\t\tinitPromises.set(network, initPromise);\n\n\t\ttry {\n\t\t\treturn await initPromise;\n\t\t} finally {\n\t\t\tinitPromises.delete(network);\n\t\t}\n\t};\n\n\tconst initClient = async ($fetch?: BetterFetch): Promise<boolean> => {\n\t\tconst network = activeNetwork.get();\n\t\treturn initClientForNetwork(network, $fetch);\n\t};\n\n\tlet sessionRestored = false;\n\n\tconst restoreFromSession = async ($fetch: BetterFetch) => {\n\t\tif (sessionRestored) return;\n\t\tconst state = nearState.get();\n\t\tif (state?.accountId) {\n\t\t\tsessionRestored = true;\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tconst res = await $fetch<ListAccountsResponseT>(\"/near/list-accounts\", { method: \"GET\" });\n\t\t\tconst accounts = res.data?.accounts;\n\t\t\tif (accounts?.length) {\n\t\t\t\tconst primary = res.data?.activeAccount || accounts.find((a: NearAccount) => a.isPrimary) || accounts[0];\n\t\t\t\tif (primary) {\n\t\t\t\t\tnearState.set({\n\t\t\t\t\t\taccountId: primary.accountId,\n\t\t\t\t\t\tpublicKey: primary.publicKey ?? null,\n\t\t\t\t\t\tnetworkId: primary.network,\n\t\t\t\t\t});\n\t\t\t\t\tactiveNetwork.set(primary.network);\n\t\t\t\t}\n\t\t\t}\n\t\t} catch {}\n\t\tsessionRestored = true;\n\t};\n\n\tconst requireConnector = async (network?: \"mainnet\" | \"testnet\") => {\n\t\tconst net = network || activeNetwork.get();\n\t\tawait initClientForNetwork(net);\n\t\tconst connector = connectors.get(net);\n\t\tif (!connector) throw new Error(`Wallet not initialized for ${net} — this operation requires a browser environment`);\n\t\treturn connector;\n\t};\n\n\tconst requireNear = (network?: \"mainnet\" | \"testnet\"): Near => {\n\t\tconst net = network || activeNetwork.get();\n\t\tconst client = nearClients.get(net);\n\t\tif (!client) throw new Error(`Wallet not initialized for ${net} — this operation requires a browser environment`);\n\t\treturn client;\n\t};\n\n\tconst ensureWalletConnected = async (network?: \"mainnet\" | \"testnet\"): Promise<boolean> => {\n\t\tconst net = network || activeNetwork.get();\n\t\tconst conn = await requireConnector(net);\n\t\tif (walletConnected.get()) {\n\t\t\ttry {\n\t\t\t\tconst { accounts } = await conn.getConnectedWallet();\n\t\t\t\tif (accounts?.length) return true;\n\t\t\t} catch {}\n\t\t}\n\n\t\treturn new Promise<boolean>((resolve) => {\n\t\t\tconst signInHandler = (data: EventMap[\"wallet:signIn\"]) => {\n\t\t\t\tconst accountId = data.accounts?.[0]?.accountId;\n\t\t\t\tconst publicKey = data.accounts?.[0]?.publicKey;\n\t\t\t\tif (accountId) {\n\t\t\t\t\thandleAccountConnection(accountId, publicKey, net);\n\t\t\t\t\tresolve(true);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tconn.on(\"wallet:signIn\", signInHandler);\n\n\t\t\tconn.connect().catch(() => {}).finally(() => {\n\t\t\t\tconn.off(\"wallet:signIn\", signInHandler);\n\t\t\t\tif (!walletConnected.get()) {\n\t\t\t\t\tresolve(false);\n\t\t\t\t}\n\t\t\t});\n\t\t});\n\t};\n\n\tconst signWithWallet = async (): Promise<SignWithWalletResult> => {\n\t\tconst net = activeNetwork.get();\n\t\tconst conn = await requireConnector(net);\n\t\tconst nearClient = requireNear(net);\n\t\tconst recipient = getRecipient(net);\n\n\t\tconst nonceBytes = generateNonce();\n\t\tconst nonceHex = hex.encode(nonceBytes);\n\t\tconst message = `Sign in to ${recipient}`;\n\n\t\tlet connectedWallet: Awaited<ReturnType<typeof conn.getConnectedWallet>> | null = null;\n\t\ttry {\n\t\t\tconnectedWallet = await conn.getConnectedWallet();\n\t\t} catch {}\n\n\t\tif (connectedWallet?.accounts?.length) {\n\t\t\tconst accountId: string = connectedWallet.accounts[0]!.accountId;\n\t\t\tconst isTestnetAccount = accountId.endsWith(\".testnet\");\n\t\t\tconst isExpectedNetwork = (net === \"testnet\") === isTestnetAccount;\n\t\t\tif (!isExpectedNetwork) {\n\t\t\t\tconnectedWallet = null;\n\t\t\t}\n\t\t}\n\n\t\tif (connectedWallet?.accounts?.length) {\n\t\t\tconst signedMessage = await nearClient.signMessage({\n\t\t\t\tmessage,\n\t\t\t\trecipient,\n\t\t\t\tnonce: nonceBytes,\n\t\t\t});\n\n\t\t\tif (!signedMessage?.accountId) {\n\t\t\t\tthrow new Error(\"Wallet sign-in was cancelled or failed\");\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tsignedMessage,\n\t\t\t\taccountId: signedMessage.accountId,\n\t\t\t\tpublicKey: signedMessage.publicKey,\n\t\t\t\tnonceHex,\n\t\t\t};\n\t\t}\n\n\t\tconst result: { value: { signedMessage: SignedMessage; accountId: string; publicKey: string } | null } = { value: null };\n\t\tconst handler = (data: EventMap[\"wallet:signInAndSignMessage\"]) => {\n\t\t\tconst account = data.accounts?.[0];\n\t\t\tif (account?.signedMessage) {\n\t\t\t\tresult.value = {\n\t\t\t\t\tsignedMessage: account.signedMessage,\n\t\t\t\t\taccountId: account.accountId,\n\t\t\t\t\tpublicKey: account.signedMessage.publicKey,\n\t\t\t\t};\n\t\t\t}\n\t\t};\n\n\t\tconn.on(\"wallet:signInAndSignMessage\", handler);\n\n\t\ttry {\n\t\t\tawait conn.connect({\n\t\t\t\tsignMessageParams: {\n\t\t\t\t\tmessage,\n\t\t\t\t\trecipient,\n\t\t\t\t\tnonce: nonceBytes,\n\t\t\t\t},\n\t\t\t});\n\t\t} finally {\n\t\t\tconn.off(\"wallet:signInAndSignMessage\", handler);\n\t\t}\n\n\t\tif (!result.value) {\n\t\t\tthrow new Error(\"Wallet sign-in was cancelled or failed\");\n\t\t}\n\n\t\treturn {\n\t\t\tsignedMessage: result.value.signedMessage,\n\t\t\taccountId: result.value.accountId,\n\t\t\tpublicKey: result.value.publicKey,\n\t\t\tnonceHex,\n\t\t};\n\t};\n\n\tconst buildSignedDelegateActionInternal = async (\n\t\treceiverId: string,\n\t\tbuildActions: (builder: TransactionBuilder, receiverId: string) => TransactionBuilder,\n\t): Promise<string> => {\n\t\tconst net = activeNetwork.get();\n\t\tconst state = nearState.get();\n\t\tif (!state?.accountId) {\n\t\t\tthrow new Error(\"No NEAR account found — please sign in with your NEAR wallet\");\n\t\t}\n\n\t\tif (!walletConnected.get()) {\n\t\t\tconst reconnected = await ensureWalletConnected(net);\n\t\t\tif (!reconnected) {\n\t\t\t\tthrow new Error(\"Wallet connection required — please approve the connection to sign\");\n\t\t\t}\n\t\t}\n\n\t\tconst nearClient = requireNear(net);\n\t\tconst builder = buildActions(nearClient.transaction(state.accountId), receiverId);\n\n\t\tconst { payload } = await builder.delegate();\n\t\treturn payload;\n\t};\n\n\tconst plugin: SIWNClientPlugin = {\n\t\tid: \"siwn\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof siwn>,\n\n\t\tgetAtoms: (_$fetch) => ({\n\t\t\tnearState,\n\t\t\twalletConnected,\n\t\t\tactiveNetwork,\n\t\t}),\n\n\t\tgetActions: ($fetch: BetterFetch, _$store: ClientStore, _options: BetterAuthClientOptions | undefined): SIWNClientActions => {\n\t\t\tvoid initClient($fetch);\n\n\t\t\treturn {\n\t\t\t\tnear: {\n\t\t\t\t\tnonce: async (params: NonceRequestT, fetchOptions?: BetterFetchOption) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/nonce\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: params,\n\t\t\t\t\t\t\t...fetchOptions\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tverify: async (params: VerifyRequestT, fetchOptions?: BetterFetchOption) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/verify\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: params,\n\t\t\t\t\t\t\t...fetchOptions\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tgetProfile: async (accountId?: AccountId, fetchOptions?: BetterFetchOption) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/profile\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: { accountId },\n\t\t\t\t\t\t\t...fetchOptions\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tview: async (params: ViewContractRequestT, fetchOptions?: BetterFetchOption) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/view\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: params,\n\t\t\t\t\t\t\t...fetchOptions\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tgetAccountId: () => {\n\t\t\t\t\t\tconst state = nearState.get();\n\t\t\t\t\t\treturn state?.accountId || null;\n\t\t\t\t\t},\n\t\t\t\t\tgetState: () => nearState.get(),\n\t\t\t\t\tisWalletConnected: () => walletConnected.get(),\n\t\t\t\t\tensureConnected: async () => {\n\t\t\t\t\t\tconst net = activeNetwork.get();\n\t\t\t\t\t\tif (!initializedNetworks.has(net)) {\n\t\t\t\t\t\t\tif (!(await initClientForNetwork(net))) return false;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (walletConnected.get()) {\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tconst conn = connectors.get(net);\n\t\t\t\t\t\t\t\tif (conn) {\n\t\t\t\t\t\t\t\t\tconst { accounts } = await conn.getConnectedWallet();\n\t\t\t\t\t\t\t\t\tif (accounts?.length) return true;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\t\tconsole.error(\"[siwn] restoreFromSession failed:\", err instanceof Error ? err.message : err);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn ensureWalletConnected(net);\n\t\t\t\t\t},\n\t\t\t\t\tdisconnect: async () => {\n\t\t\t\t\t\tfor (const [_net, conn] of connectors) {\n\t\t\t\t\t\t\tif (conn) {\n\t\t\t\t\t\t\t\ttry { await conn.disconnect(); } catch {}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\twalletConnected.set(false);\n\t\t\t\t\t\tnearState.set(null);\n\t\t\t\t\t},\n\t\t\t\t\tlink: async (callbacks?: AuthCallbacks) => {\n\t\t\t\t\t\tconst net = activeNetwork.get();\n\t\t\t\t\t\tconst recipient = getRecipient(net);\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst { signedMessage, accountId, nonceHex } = await signWithWallet();\n\t\t\t\t\t\t\tconst message = `Sign in to ${recipient}`;\n\n\t\t\t\t\t\t\tawait handleAccountConnection(accountId, signedMessage.publicKey, net);\n\n\t\t\t\t\t\t\tconst linkResponse = await $fetch<{ success: boolean; accountId: string; network: string; message: string }>(\"/near/link-account\", {\n\t\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\t\tsignedMessage,\n\t\t\t\t\t\t\t\t\tmessage,\n\t\t\t\t\t\t\t\t\trecipient,\n\t\t\t\t\t\t\t\t\tnonce: nonceHex,\n\t\t\t\t\t\t\t\t\taccountId,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tif (linkResponse.error) {\n\t\t\t\t\t\t\t\tthrow new Error(linkResponse.error.message || \"Failed to link NEAR account\");\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (!linkResponse?.data?.success) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Account linking failed\");\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tcallbacks?.onSuccess?.();\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\t\t\t\t\t\tcallbacks?.onError?.(err);\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\tunlink: async (\n\t\t\t\t\t\tparams: { accountId: string; network?: \"mainnet\" | \"testnet\" },\n\t\t\t\t\t\tfetchOptions?: BetterFetchOption\n\t\t\t\t\t) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/unlink-account\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: params,\n\t\t\t\t\t\t\t...fetchOptions\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tlistAccounts: async (): Promise<BetterFetchResponse<ListAccountsResponseT>> => {\n\t\t\t\t\t\treturn await $fetch(\"/near/list-accounts\", { method: \"GET\" });\n\t\t\t\t\t},\n\t\t\t\t\tsetPrimaryAccount: async (params: SetPrimaryAccountRequestT) => {\n\t\t\t\t\t\tconst response = await $fetch<SetPrimaryAccountResponseT>(\"/near/set-primary-account\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: params,\n\t\t\t\t\t\t});\n\t\t\t\t\t\tconst activeAccount = response.data?.activeAccount;\n\t\t\t\t\t\tif (activeAccount) {\n\t\t\t\t\t\t\tnearState.set({\n\t\t\t\t\t\t\t\taccountId: activeAccount.accountId,\n\t\t\t\t\t\t\t\tpublicKey: activeAccount.publicKey ?? null,\n\t\t\t\t\t\t\t\tnetworkId: activeAccount.network,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tactiveNetwork.set(activeAccount.network);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn response;\n\t\t\t\t\t},\n\t\t\t\t\tbuildSignedDelegateAction: async (\n\t\t\t\t\t\treceiverId: string,\n\t\t\t\t\t\tbuildActions: (builder: TransactionBuilder, receiverId: string) => TransactionBuilder,\n\t\t\t\t\t): Promise<string> => {\n\t\t\t\t\t\treturn buildSignedDelegateActionInternal(receiverId, buildActions);\n\t\t\t\t\t},\n\t\t\t\t\trelayTransaction: async (params: { payload: string }) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/relay\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: params,\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tgetRelayStatus: async (txHash: string) => {\n\t\t\t\t\t\treturn await $fetch(`/near/relay-status/${txHash}`, {\n\t\t\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tgetRelayerInfo: async (params?: GetRelayerInfoRequestT) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/relayer-info\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: params ?? {},\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\trelayHistory: async () => {\n\t\t\t\t\t\treturn await $fetch(\"/near/relay-history\", {\n\t\t\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tcreateSubAccount: async (params: CreateSubAccountRequestT) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/create-sub-account\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: params,\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\tcheckSubAccountAvailability: async (params: CheckSubAccountAvailabilityRequestT) => {\n\t\t\t\t\tif (!SUB_ACCOUNT_LABEL_REGEX.test(params.subAccountName) || params.subAccountName.length < 2) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tdata: { available: false, accountId: \"\", reason: \"invalid\" as const },\n\t\t\t\t\t\t\terror: null,\n\t\t\t\t\t\t} as BetterFetchResponse<CheckSubAccountAvailabilityResponseT>;\n\t\t\t\t\t}\n\t\t\t\t\treturn await $fetch(\"/near/check-sub-account-availability\", {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\tbody: params,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t\t\tsetNetwork: (network: \"mainnet\" | \"testnet\") => {\n\t\t\t\t\t\tconst prev = activeNetwork.get();\n\t\t\t\t\t\tif (prev !== network) {\n\t\t\t\t\t\t\tconst oldConn = connectors.get(prev);\n\t\t\t\t\t\t\tif (oldConn) {\n\t\t\t\t\t\t\t\tvoid oldConn.disconnect().catch(() => {});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\twalletConnected.set(false);\n\t\t\t\t\t\t\tnearState.set(null);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tactiveNetwork.set(network);\n\t\t\t\t\t\tvoid initClientForNetwork(network);\n\t\t\t\t\t},\n\t\t\t\t\tgetNetwork: () => activeNetwork.get(),\n\t\t\t\t\tgetSupportedNetworks: () => getSupportedNetworks(),\n\t\t\t\t\tgetRecipient: (network?: \"mainnet\" | \"testnet\") => getRecipient(network),\n\t\t\t\t\tget client(): NearType {\n\t\t\t\t\t\tconst net = activeNetwork.get();\n\t\t\t\t\t\tconst client = nearClients.get(net);\n\t\t\t\t\t\tif (!client) throw new Error(`Wallet not initialized for ${net} — this operation requires a browser environment`);\n\t\t\t\t\t\treturn client;\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\tsignIn: {\n\t\t\t\t\tnear: async (callbacks?: AuthCallbacks) => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst { signedMessage, accountId, nonceHex } = await signWithWallet();\n\t\t\t\t\t\t\tconst net = activeNetwork.get();\n\t\t\t\t\t\t\tconst recipient = getRecipient(net);\n\t\t\t\t\t\t\tconst message = `Sign in to ${recipient}`;\n\n\t\t\t\t\t\t\tawait handleAccountConnection(accountId, signedMessage.publicKey, net);\n\n\t\t\t\t\t\t\tconst verifyResponse: BetterFetchResponse<VerifyResponseT> = await $fetch(\"/near/verify\", {\n\t\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\t\tsignedMessage,\n\t\t\t\t\t\t\t\t\tmessage,\n\t\t\t\t\t\t\t\t\trecipient,\n\t\t\t\t\t\t\t\t\tnonce: nonceHex,\n\t\t\t\t\t\t\t\t\taccountId,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tif (verifyResponse.error) {\n\t\t\t\t\t\t\t\tthrow new Error(verifyResponse.error.message || \"Failed to verify signature\");\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (!verifyResponse?.data?.success) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Authentication verification failed\");\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tcallbacks?.onSuccess?.();\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\t\t\t\t\t\tcallbacks?.onError?.(err);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\t};\n\n\treturn plugin;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,MAAa,oBAAoB,EAAE,OAAO;CACzC,KAAK,EAAE,OAAO,EAAE,SAAS;CACzB,UAAU,EAAE,OAAO,EAAE,SAAS;AAC/B,CAAC;AAED,MAAa,gBAAgB,EAAE,OAAO;CACrC,MAAM,EAAE,OAAO,EAAE,SAAS;CAC1B,aAAa,EAAE,OAAO,EAAE,SAAS;CACjC,OAAO,kBAAkB,SAAS;CAClC,iBAAiB,kBAAkB,SAAS;CAC5C,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,OAAO,CAAC,EAAE,SAAS;AACrD,CAAC;AAKD,MAAM,sBAAsB,EAAE,OAAO;CACpC,WAAW,EAAE,OAAO;CACpB,WAAW,EAAE,OAAO;CACpB,WAAW,EAAE,OAAO;CACpB,OAAO,EAAE,OAAO,EAAE,SAAS;AAC5B,CAAC;AAEiC,EAAE,OAAO;CAC1C,eAAe;CACf,SAAS,EAAE,OAAO;CAClB,WAAW,EAAE,OAAO;CACpB,OAAO,EAAE,OAAO;CAChB,WAAW;CACX,aAAa,EAAE,OAAO,EAAE,SAAS;AAClC,CAAC;AAEuC,EAAE,OAAO;CAChD,WAAW;CACX,SAAS,EAAE,KAAK,CAAC,WAAW,SAAS,CAAC,EAAE,SAAS;AAClD,CAAC;AAE2B,EAAE,OAAO;CACpC,WAAW;CACX,WAAW,EAAE,MAAM,CAAC,EAAE,QAAQ,SAAS,GAAG,EAAE,QAAQ,SAAS,CAAC,CAAC;AAChE,CAAC;AAE4B,EAAE,OAAO;CACrC,eAAe;CACf,SAAS,EAAE,OAAO;CAClB,WAAW,EA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B,IAAI,SAAS,SAAS;OACrB,MAAM,UAAU,WAAW,IAAI,IAAI;OACnC,IAAI,SACH,QAAa,WAAW,EAAE,YAAY,CAAC,CAAC;OAEzC,gBAAgB,IAAI,KAAK;OACzB,UAAU,IAAI,IAAI;MACnB;MACA,cAAc,IAAI,OAAO;MACzB,qBAA0B,OAAO;KAClC;KACA,kBAAkB,cAAc,IAAI;KACpC,4BAA4B,qBAAqB;KACjD,eAAe,YAAoC,aAAa,OAAO;KACvE,IAAI,SAAmB;MACtB,MAAM,MAAM,cAAc,IAAI;MAC9B,MAAM,SAAS,YAAY,IAAI,GAAG;MAClC,IAAI,CAAC,QAAQ,MAAM,IAAI,MAAM,8BAA8B,IAAI,iDAAiD;MAChH,OAAO;KACR;IACD;IACA,QAAQ,EACP,MAAM,OAAO,cAA8B;KAC1C,IAAI;MACH,MAAM,EAAE,eAAe,WAAW,aAAa,MAAM,eAAe;MACpE,MAAM,MAAM,cAAc,IAAI;MAC9B,MAAM,YAAY,aAAa,GAAG;MAClC,MAAM,UAAU,cAAc;MAE9B,MAAM,wBAAwB,WAAW,cAAc,WAAW,GAAG;MAErE,MAAM,iBAAuD,MAAM,OAAO,gBAAgB;OACzF,QAAQ;OACR,MAAM;QACL;QACA;QACA;QACA,OAAO;QACP;OACD;MACD,CAAC;MAED,IAAI,eAAe,OAClB,MAAM,IAAI,MAAM,eAAe,MAAM,WAAW,4BAA4B;MAG7E,IAAI,CAAC,gBAAgB,MAAM,SAC1B,MAAM,IAAI,MAAM,oCAAoC;MAGrD,WAAW,YAAY;KACxB,SAAS,OAAO;MACf,MAAM,MAAM,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,OAAO,KAAK,CAAC;MACpE,WAAW,UAAU,GAAG;KACzB;IACD,EACD;GACD;EACD;CAGW;AACb"}
1
+ {"version":3,"file":"client.js","names":[],"sources":["../src/types.ts","../src/client.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { AccountIdSchema, type AccountId } from \"near-kit/schemas\";\nimport type { AccountState, Near, TransactionBuilder } from \"near-kit\";\n\nexport type { AccountId };\n\nexport interface NearAccount {\n\tid: string;\n\tuserId: string;\n\taccountId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n\tpublicKey: string;\n\tisPrimary: boolean;\n\tcreatedAt: Date;\n}\n\nexport interface ListedNearAccount extends NearAccount {\n\tproviderId: \"siwn\";\n\tisActive: boolean;\n\tisAvailable: boolean;\n}\n\nexport interface ListAccountsResponseT {\n\taccounts: ListedNearAccount[];\n\tactiveAccount: ListedNearAccount | null;\n\tavailableAccounts: ListedNearAccount[];\n}\n\nexport interface SetPrimaryAccountResponseT extends ListAccountsResponseT {\n\tsuccess: boolean;\n\taccountId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n\tmessage: string;\n}\n\nexport const socialImageSchema = z.object({\n\turl: z.string().optional(),\n\tipfs_cid: z.string().optional(),\n});\n\nexport const profileSchema = z.object({\n\tname: z.string().optional(),\n\tdescription: z.string().optional(),\n\timage: socialImageSchema.optional(),\n\tbackgroundImage: socialImageSchema.optional(),\n\tlinktree: z.record(z.string(), z.string()).optional(),\n});\n\nexport type SocialImage = z.infer<typeof socialImageSchema>;\nexport type Profile = z.infer<typeof profileSchema>;\n\nconst signedMessageSchema = z.object({\n\taccountId: z.string(),\n\tpublicKey: z.string(),\n\tsignature: z.string(),\n\tstate: z.string().optional(),\n});\n\nexport const LinkAccountRequest = z.object({\n\tsignedMessage: signedMessageSchema,\n\tmessage: z.string(),\n\trecipient: z.string(),\n\tnonce: z.string(),\n\taccountId: AccountIdSchema,\n\tcallbackUrl: z.string().optional(),\n});\n\nexport const SetPrimaryAccountRequest = z.object({\n\taccountId: AccountIdSchema,\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n});\n\nexport const NonceRequest = z.object({\n\taccountId: AccountIdSchema,\n\tnetworkId: z.union([z.literal(\"mainnet\"), z.literal(\"testnet\")])\n});\n\nexport const VerifyRequest = z.object({\n\tsignedMessage: signedMessageSchema,\n\tmessage: z.string(),\n\trecipient: z.string(),\n\tnonce: z.string(),\n\taccountId: AccountIdSchema,\n\tcallbackUrl: z.string().optional(),\n});\n\nexport const RelayRequest = z.object({\n\tpayload: z.string(),\n});\nexport type RelayRequestT = z.infer<typeof RelayRequest>;\n\nexport const RelayResponse = z.object({\n\ttxHash: z.string(),\n\tstatus: z.enum([\"pending\", \"completed\", \"failed\"]),\n});\nexport type RelayResponseT = z.infer<typeof RelayResponse>;\n\nexport const RelayStatusResponse = z.object({\n\tstatus: z.enum([\"pending\", \"completed\", \"failed\"]),\n\tgasUsed: z.string().optional(),\n\toutcome: z.unknown().optional(),\n});\nexport type RelayStatusResponseT = z.infer<typeof RelayStatusResponse>;\n\nexport const ViewContractRequest = z.object({\n\tcontractId: z.string(),\n\tmethodName: z.string(),\n\targs: z.record(z.string(), z.any()).optional(),\n});\nexport type ViewContractRequestT = z.infer<typeof ViewContractRequest>;\n\nexport const NonceResponse = z.object({ nonce: z.string() });\nexport const VerifyResponse = z.object({\n\ttoken: z.string(),\n\tsuccess: z.literal(true),\n\tuser: z.object({\n\t\tid: z.string(),\n\t\taccountId: AccountIdSchema,\n\t\tnetwork: z.union([z.literal(\"mainnet\"), z.literal(\"testnet\")]),\n\t}),\n});\nexport const ProfileResponse = profileSchema.nullable();\nexport const ViewContractResponse = z.object({ result: z.unknown() });\n\nexport const ProfileRequest = z.object({\n\taccountId: AccountIdSchema.optional(),\n});\nexport type ProfileRequestT = z.infer<typeof ProfileRequest>;\n\nexport type NonceRequestT = z.infer<typeof NonceRequest>;\nexport type NonceResponseT = z.infer<typeof NonceResponse>;\nexport type SetPrimaryAccountRequestT = z.infer<typeof SetPrimaryAccountRequest>;\nexport type VerifyRequestT = z.infer<typeof VerifyRequest>;\nexport type VerifyResponseT = z.infer<typeof VerifyResponse>;\nexport type ProfileResponseT = z.infer<typeof ProfileResponse>;\nexport type ViewContractResponseT = z.infer<typeof ViewContractResponse>;\n\nexport const RelayedTransactionSchema = z.object({\n\tid: z.string(),\n\tuserId: z.string(),\n\ttxHash: z.string(),\n\tsenderId: z.string(),\n\treceiverId: z.string(),\n\tnetwork: z.string(),\n\tstatus: z.string(),\n\tgasUsed: z.string().optional(),\n\tcreatedAt: z.string(),\n\tupdatedAt: z.string().optional(),\n});\nexport type RelayedTransactionT = z.infer<typeof RelayedTransactionSchema>;\n\nexport const RelayHistoryResponse = z.object({\n\ttransactions: z.array(RelayedTransactionSchema),\n});\nexport type RelayHistoryResponseT = z.infer<typeof RelayHistoryResponse>;\n\nexport interface RelayedTransactionRecord {\n\tid: string;\n\tuserId: string;\n\ttxHash: string;\n\tsenderId: string;\n\treceiverId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n\tstatus: string;\n\tgasUsed?: string;\n\tcreatedAt: Date;\n\tupdatedAt: Date;\n}\n\nexport interface RelayerInfo extends AccountState {\n\taccountId: string;\n\tmode: \"ephemeral\" | \"explicit\";\n\tnetwork: \"mainnet\" | \"testnet\";\n\tpublicKey: string;\n\thasKey: boolean;\n\tcreatedAt?: Date;\n\tlastUsedAt?: Date;\n\tparentAccount?: string;\n\tsubAccountAvailable?: boolean;\n}\n\nexport interface DualNetworkConfig<T> {\n\tmainnet: T;\n\ttestnet: T;\n}\n\nexport const GetRelayerInfoRequest = z.object({\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n});\nexport type GetRelayerInfoRequestT = z.infer<typeof GetRelayerInfoRequest>;\n\nexport interface SubAccountRelayerFCAKConfig {\n\treceiverId: string;\n\tmethodNames?: string[];\n\tallowance?: string;\n}\n\nexport interface SubAccountTxCtx {\n\tnewAccountId: string;\n\tparentAccount: string;\n\tuserPublicKey: string;\n\tuserAccountId: string;\n\tuserId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n}\n\nexport interface SubAccountLifecycleCtx extends SubAccountTxCtx {\n\tnear: Near;\n}\n\nexport interface SubAccountConfig {\n\tparentAccount?: string;\n\tminDeposit?: string;\n\tparentHasFullAccess?: boolean;\n\tdeploy?:\n\t\t| { wasm: Uint8Array }\n\t\t| { fromPublished: { accountId?: string; codeHash?: string } };\n\tinit?: {\n\t\tmethodName: string;\n\t\targs: object | ((ctx: SubAccountTxCtx) => object);\n\t};\n\textendTx?: (tx: TransactionBuilder, ctx: SubAccountTxCtx) => TransactionBuilder;\n\tonCreated?: (ctx: SubAccountLifecycleCtx) => Promise<void>;\n\tonRollback?: (ctx: SubAccountLifecycleCtx) => Promise<void>;\n\taddRelayerFCAK?: boolean;\n\trelayerFCAK?: SubAccountRelayerFCAKConfig;\n}\n\nexport const SUB_ACCOUNT_LABEL_REGEX = /^([a-z\\d]+[-_])*[a-z\\d]+$/;\n\nexport const SubAccountNameSchema = z\n\t.string()\n\t.min(2, \"Sub-account name must be at least 2 characters\")\n\t.max(64, \"Sub-account name must be at most 64 characters\")\n\t.regex(SUB_ACCOUNT_LABEL_REGEX, \"Sub-account name must contain only lowercase alphanumeric characters, hyphens, and underscores (no leading/trailing hyphens or underscores)\");\n\nexport const CreateSubAccountRequest = z.object({\n\tsubAccountName: SubAccountNameSchema,\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n\tpublicKey: z.string(),\n});\nexport type CreateSubAccountRequestT = z.infer<typeof CreateSubAccountRequest>;\n\nexport const CreateSubAccountResponse = z.object({\n\tsuccess: z.literal(true),\n\taccountId: z.string(),\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]),\n\tpublicKey: z.string(),\n\tmessage: z.string(),\n});\nexport type CreateSubAccountResponseT = z.infer<typeof CreateSubAccountResponse>;\n\nexport const CheckSubAccountAvailabilityRequest = z.object({\n\tsubAccountName: SubAccountNameSchema,\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n});\nexport type CheckSubAccountAvailabilityRequestT = z.infer<typeof CheckSubAccountAvailabilityRequest>;\n\nexport const CheckSubAccountAvailabilityResponse = z.object({\n\tavailable: z.boolean(),\n\taccountId: z.string(),\n\tparentAccount: z.string().optional(),\n\treason: z.enum([\"taken\", \"invalid\", \"too-long\", \"not-configured\"]).optional(),\n});\nexport type CheckSubAccountAvailabilityResponseT = z.infer<typeof CheckSubAccountAvailabilityResponse>;\n","import { Near, fromNearConnect, generateNonce, TransactionBuilder } from \"near-kit\";\nimport type { Near as NearType, SignedMessage } from \"near-kit\";\nimport type { EventMap } from \"@hot-labs/near-connect\";\nimport { hex } from \"@scure/base\";\nimport type { BetterAuthClientPlugin, BetterAuthClientOptions, BetterFetch, BetterFetchOption, BetterFetchResponse, ClientStore } from \"better-auth/client\";\nimport { atom } from \"nanostores\";\nimport type { siwn } from \"./index.js\";\nimport { type AccountId, type DualNetworkConfig, type NonceRequestT, type NonceResponseT, type ProfileResponseT, type VerifyRequestT, type VerifyResponseT, type RelayResponseT, type RelayStatusResponseT, type NearAccount, type ListAccountsResponseT, type SetPrimaryAccountRequestT, type SetPrimaryAccountResponseT, type ViewContractRequestT, type ViewContractResponseT, type RelayerInfo, type RelayHistoryResponseT, type GetRelayerInfoRequestT, type CreateSubAccountRequestT, type CreateSubAccountResponseT, type CheckSubAccountAvailabilityRequestT, type CheckSubAccountAvailabilityResponseT, SUB_ACCOUNT_LABEL_REGEX } from \"./types.js\";\n\nexport interface AuthCallbacks {\n\tonSuccess?: () => void;\n\tonError?: (error: Error & { status?: number; code?: string }) => void;\n}\n\nexport interface SIWNClientConfig {\n\trecipient?: string;\n\trecipients?: DualNetworkConfig<string>;\n\tnetworkId?: \"mainnet\" | \"testnet\";\n\tcspNonce?: string;\n}\n\ninterface SignWithWalletResult {\n\tsignedMessage: SignedMessage;\n\taccountId: string;\n\tpublicKey: string;\n\tnonceHex: string;\n}\n\nexport interface SIWNClientActions {\n\tnear: {\n\t\tnonce: (params: NonceRequestT) => Promise<BetterFetchResponse<NonceResponseT>>;\n\t\tverify: (params: VerifyRequestT) => Promise<BetterFetchResponse<VerifyResponseT>>;\n\t\tgetProfile: (accountId?: AccountId) => Promise<BetterFetchResponse<ProfileResponseT>>;\n\t\tview: (params: ViewContractRequestT) => Promise<BetterFetchResponse<ViewContractResponseT>>;\n\t\tgetAccountId: () => string | null;\n\t\tgetState: () => { accountId: string | null; publicKey: string | null; networkId: string } | null;\n\t\tisWalletConnected: () => boolean;\n\t\tensureConnected: () => Promise<boolean>;\n\t\tdisconnect: () => Promise<void>;\n\t\tlink: (callbacks?: AuthCallbacks) => Promise<void>;\n\t\tunlink: (params: { accountId: string; network?: \"mainnet\" | \"testnet\" }) => Promise<BetterFetchResponse<{ success: boolean; message: string }>>;\n\t\tlistAccounts: () => Promise<BetterFetchResponse<ListAccountsResponseT>>;\n\t\tsetPrimaryAccount: (params: SetPrimaryAccountRequestT) => Promise<BetterFetchResponse<SetPrimaryAccountResponseT>>;\n\t\tbuildSignedDelegateAction: (receiverId: string, buildActions: (builder: TransactionBuilder, receiverId: string) => TransactionBuilder) => Promise<string>;\n\t\trelayTransaction: (params: { payload: string }) => Promise<BetterFetchResponse<RelayResponseT>>;\n\t\tgetRelayStatus: (txHash: string) => Promise<BetterFetchResponse<RelayStatusResponseT>>;\n\t\tgetRelayerInfo: (params?: GetRelayerInfoRequestT) => Promise<BetterFetchResponse<RelayerInfo & { enabled: boolean }>>;\n\t\trelayHistory: () => Promise<BetterFetchResponse<RelayHistoryResponseT>>;\n\t\tcreateSubAccount: (params: CreateSubAccountRequestT) => Promise<BetterFetchResponse<CreateSubAccountResponseT>>;\n\t\tcheckSubAccountAvailability: (params: CheckSubAccountAvailabilityRequestT) => Promise<BetterFetchResponse<CheckSubAccountAvailabilityResponseT>>;\n\t\tsetNetwork: (network: \"mainnet\" | \"testnet\") => void;\n\t\tgetNetwork: () => \"mainnet\" | \"testnet\";\n\t\tgetSupportedNetworks: () => (\"mainnet\" | \"testnet\")[];\n\t\tgetRecipient: (network?: \"mainnet\" | \"testnet\") => string;\n\t\tclient: NearType;\n\t};\n\tsignIn: {\n\t\tnear: (callbacks?: AuthCallbacks) => Promise<void>;\n\t};\n}\n\nexport interface SIWNClientPlugin extends BetterAuthClientPlugin {\n\tid: \"siwn\";\n\t$InferServerPlugin: ReturnType<typeof siwn>;\n\tgetAtoms: ($fetch: BetterFetch) => {\n\t\tnearState: ReturnType<typeof atom<NearState>>;\n\t\twalletConnected: ReturnType<typeof atom<boolean>>;\n\t\tactiveNetwork: ReturnType<typeof atom<\"mainnet\" | \"testnet\">>;\n\t};\n\tgetActions: ($fetch: BetterFetch, $store: ClientStore, options: BetterAuthClientOptions | undefined) => SIWNClientActions;\n}\n\ntype NearState = { accountId: string | null; publicKey: string | null; networkId: string } | null;\n\nexport const siwnClient = (config: SIWNClientConfig): SIWNClientPlugin => {\n\tconst nearState = atom<NearState>(null);\n\tconst walletConnected = atom<boolean>(false);\n\tconst activeNetwork = atom<\"mainnet\" | \"testnet\">(config.networkId || \"mainnet\");\n\n\tconst getRecipient = (network?: \"mainnet\" | \"testnet\"): string => {\n\t\tconst net = network || activeNetwork.get();\n\t\tif (config.recipients) return config.recipients[net];\n\t\treturn config.recipient ?? \"\";\n\t};\n\n\tconst getSupportedNetworks = (): (\"mainnet\" | \"testnet\")[] => {\n\t\tif (config.recipients) return [\"mainnet\", \"testnet\"];\n\t\tif (config.recipient) return [config.recipient.endsWith(\".testnet\") ? \"testnet\" : \"mainnet\"];\n\t\treturn [\"mainnet\"];\n\t};\n\n\tlet connectors = new Map<\"mainnet\" | \"testnet\", InstanceType<typeof import(\"@hot-labs/near-connect\").NearConnector>>();\n\tlet nearClients = new Map<\"mainnet\" | \"testnet\", Near>();\n\tlet initializedNetworks = new Set<\"mainnet\" | \"testnet\">();\n\tlet connectorModulePromise: Promise<typeof import(\"@hot-labs/near-connect\")> | null = null;\n\tlet initPromises = new Map<\"mainnet\" | \"testnet\", Promise<boolean>>();\n\n\tconst loadConnector = async () => {\n\t\tconnectorModulePromise ??= import(\"@hot-labs/near-connect\");\n\t\tconst { NearConnector } = await connectorModulePromise;\n\t\treturn NearConnector;\n\t};\n\n\tconst handleAccountConnection = async (accountId: string, publicKey?: string | null, network?: \"mainnet\" | \"testnet\") => {\n\t\tif (!accountId) return;\n\t\tconst net = network || activeNetwork.get();\n\t\tnearState.set({\n\t\t\taccountId,\n\t\t\tpublicKey: publicKey || null,\n\t\t\tnetworkId: net,\n\t\t});\n\t\twalletConnected.set(true);\n\t};\n\n\tconst initClientForNetwork = async (network: \"mainnet\" | \"testnet\", $fetch?: BetterFetch): Promise<boolean> => {\n\t\tif (initializedNetworks.has(network)) return true;\n\t\tif (initPromises.has(network)) return initPromises.get(network)!;\n\t\tif (typeof (globalThis as any).window === \"undefined\") return false;\n\n\t\tconst initPromise = (async () => {\n\t\t\tconst NearConnector = await loadConnector();\n\t\t\tconst connector = new NearConnector({ network, cspNonce: config.cspNonce });\n\t\t\tconnectors.set(network, connector);\n\n\t\t\tconst near = new Near({\n\t\t\t\tnetwork,\n\t\t\t\twallet: fromNearConnect(connector),\n\t\t\t});\n\t\t\tnearClients.set(network, near);\n\n\t\t\tconnector.on(\"wallet:signIn\", async (data: EventMap[\"wallet:signIn\"]) => {\n\t\t\t\tconst accountId = data.accounts?.[0]?.accountId;\n\t\t\t\tconst publicKey = data.accounts?.[0]?.publicKey;\n\t\t\t\tif (accountId) {\n\t\t\t\t\tawait handleAccountConnection(accountId, publicKey, network);\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tconnector.on(\"wallet:signOut\", () => {\n\t\t\t\twalletConnected.set(false);\n\t\t\t\tconst state = nearState.get();\n\t\t\t\tif (state) {\n\t\t\t\t\tnearState.set({ accountId: state.accountId, publicKey: null, networkId: state.networkId });\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tvoid connector.getConnectedWallet().then(({ accounts }) => {\n\t\t\t\tconst account = accounts?.[0];\n\t\t\t\tconst net = activeNetwork.get();\n\t\t\t\tif (account?.accountId && !nearState.get()) {\n\t\t\t\t\tnearState.set({\n\t\t\t\t\t\taccountId: account.accountId,\n\t\t\t\t\t\tpublicKey: account.publicKey ?? null,\n\t\t\t\t\t\tnetworkId: net,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t\tif (account?.accountId) {\n\t\t\t\t\twalletConnected.set(true);\n\t\t\t\t}\n\t\t\t}).catch(() => {});\n\n\t\t\tinitializedNetworks.add(network);\n\n\t\t\tif ($fetch) {\n\t\t\t\tvoid restoreFromSession($fetch);\n\t\t\t}\n\n\t\t\treturn true;\n\t\t})();\n\n\t\tinitPromises.set(network, initPromise);\n\n\t\ttry {\n\t\t\treturn await initPromise;\n\t\t} finally {\n\t\t\tinitPromises.delete(network);\n\t\t}\n\t};\n\n\tconst initClient = async ($fetch?: BetterFetch): Promise<boolean> => {\n\t\tconst network = activeNetwork.get();\n\t\treturn initClientForNetwork(network, $fetch);\n\t};\n\n\tlet sessionRestored = false;\n\n\tconst restoreFromSession = async ($fetch: BetterFetch) => {\n\t\tif (sessionRestored) return;\n\t\tconst state = nearState.get();\n\t\tif (state?.accountId) {\n\t\t\tsessionRestored = true;\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tconst res = await $fetch<ListAccountsResponseT>(\"/near/list-accounts\", { method: \"GET\" });\n\t\t\tconst accounts = res.data?.accounts;\n\t\t\tif (accounts?.length) {\n\t\t\t\tconst primary = res.data?.activeAccount || accounts.find((a: NearAccount) => a.isPrimary) || accounts[0];\n\t\t\t\tif (primary) {\n\t\t\t\t\tnearState.set({\n\t\t\t\t\t\taccountId: primary.accountId,\n\t\t\t\t\t\tpublicKey: primary.publicKey ?? null,\n\t\t\t\t\t\tnetworkId: primary.network,\n\t\t\t\t\t});\n\t\t\t\t\tactiveNetwork.set(primary.network);\n\t\t\t\t}\n\t\t\t}\n\t\t} catch {}\n\t\tsessionRestored = true;\n\t};\n\n\tconst requireConnector = async (network?: \"mainnet\" | \"testnet\") => {\n\t\tconst net = network || activeNetwork.get();\n\t\tawait initClientForNetwork(net);\n\t\tconst connector = connectors.get(net);\n\t\tif (!connector) throw new Error(`Wallet not initialized for ${net} — this operation requires a browser environment`);\n\t\treturn connector;\n\t};\n\n\tconst requireNear = (network?: \"mainnet\" | \"testnet\"): Near => {\n\t\tconst net = network || activeNetwork.get();\n\t\tconst client = nearClients.get(net);\n\t\tif (!client) throw new Error(`Wallet not initialized for ${net} — this operation requires a browser environment`);\n\t\treturn client;\n\t};\n\n\tconst ensureWalletConnected = async (network?: \"mainnet\" | \"testnet\"): Promise<boolean> => {\n\t\tconst net = network || activeNetwork.get();\n\t\tconst conn = await requireConnector(net);\n\t\tif (walletConnected.get()) {\n\t\t\ttry {\n\t\t\t\tconst { accounts } = await conn.getConnectedWallet();\n\t\t\t\tif (accounts?.length) return true;\n\t\t\t} catch {}\n\t\t}\n\n\t\treturn new Promise<boolean>((resolve) => {\n\t\t\tconst signInHandler = (data: EventMap[\"wallet:signIn\"]) => {\n\t\t\t\tconst accountId = data.accounts?.[0]?.accountId;\n\t\t\t\tconst publicKey = data.accounts?.[0]?.publicKey;\n\t\t\t\tif (accountId) {\n\t\t\t\t\thandleAccountConnection(accountId, publicKey, net);\n\t\t\t\t\tresolve(true);\n\t\t\t\t}\n\t\t\t};\n\n\t\t\tconn.on(\"wallet:signIn\", signInHandler);\n\n\t\t\tconn.connect().catch(() => {}).finally(() => {\n\t\t\t\tconn.off(\"wallet:signIn\", signInHandler);\n\t\t\t\tif (!walletConnected.get()) {\n\t\t\t\t\tresolve(false);\n\t\t\t\t}\n\t\t\t});\n\t\t});\n\t};\n\n\tconst signWithWallet = async (): Promise<SignWithWalletResult> => {\n\t\tconst net = activeNetwork.get();\n\t\tconst conn = await requireConnector(net);\n\t\tconst nearClient = requireNear(net);\n\t\tconst recipient = getRecipient(net);\n\n\t\tconst nonceBytes = generateNonce();\n\t\tconst nonceHex = hex.encode(nonceBytes);\n\t\tconst message = `Sign in to ${recipient}`;\n\n\t\tlet connectedWallet: Awaited<ReturnType<typeof conn.getConnectedWallet>> | null = null;\n\t\ttry {\n\t\t\tconnectedWallet = await conn.getConnectedWallet();\n\t\t} catch {}\n\n\t\tif (connectedWallet?.accounts?.length) {\n\t\t\tconst accountId: string = connectedWallet.accounts[0]!.accountId;\n\t\t\tconst isTestnetAccount = accountId.endsWith(\".testnet\");\n\t\t\tconst isExpectedNetwork = (net === \"testnet\") === isTestnetAccount;\n\t\t\tif (!isExpectedNetwork) {\n\t\t\t\tconnectedWallet = null;\n\t\t\t}\n\t\t}\n\n\t\tif (connectedWallet?.accounts?.length) {\n\t\t\tconst signedMessage = await nearClient.signMessage({\n\t\t\t\tmessage,\n\t\t\t\trecipient,\n\t\t\t\tnonce: nonceBytes,\n\t\t\t});\n\n\t\t\tif (!signedMessage?.accountId) {\n\t\t\t\tthrow new Error(\"Wallet sign-in was cancelled or failed\");\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tsignedMessage,\n\t\t\t\taccountId: signedMessage.accountId,\n\t\t\t\tpublicKey: signedMessage.publicKey,\n\t\t\t\tnonceHex,\n\t\t\t};\n\t\t}\n\n\t\tconst result: { value: { signedMessage: SignedMessage; accountId: string; publicKey: string } | null } = { value: null };\n\t\tconst handler = (data: EventMap[\"wallet:signInAndSignMessage\"]) => {\n\t\t\tconst account = data.accounts?.[0];\n\t\t\tif (account?.signedMessage) {\n\t\t\t\tresult.value = {\n\t\t\t\t\tsignedMessage: account.signedMessage,\n\t\t\t\t\taccountId: account.accountId,\n\t\t\t\t\tpublicKey: account.signedMessage.publicKey,\n\t\t\t\t};\n\t\t\t}\n\t\t};\n\n\t\tconn.on(\"wallet:signInAndSignMessage\", handler);\n\n\t\ttry {\n\t\t\tawait conn.connect({\n\t\t\t\tsignMessageParams: {\n\t\t\t\t\tmessage,\n\t\t\t\t\trecipient,\n\t\t\t\t\tnonce: nonceBytes,\n\t\t\t\t},\n\t\t\t});\n\t\t} finally {\n\t\t\tconn.off(\"wallet:signInAndSignMessage\", handler);\n\t\t}\n\n\t\tif (!result.value) {\n\t\t\tthrow new Error(\"Wallet sign-in was cancelled or failed\");\n\t\t}\n\n\t\treturn {\n\t\t\tsignedMessage: result.value.signedMessage,\n\t\t\taccountId: result.value.accountId,\n\t\t\tpublicKey: result.value.publicKey,\n\t\t\tnonceHex,\n\t\t};\n\t};\n\n\tconst buildSignedDelegateActionInternal = async (\n\t\treceiverId: string,\n\t\tbuildActions: (builder: TransactionBuilder, receiverId: string) => TransactionBuilder,\n\t): Promise<string> => {\n\t\tconst net = activeNetwork.get();\n\t\tconst state = nearState.get();\n\t\tif (!state?.accountId) {\n\t\t\tthrow new Error(\"No NEAR account found — please sign in with your NEAR wallet\");\n\t\t}\n\n\t\tif (!walletConnected.get()) {\n\t\t\tconst reconnected = await ensureWalletConnected(net);\n\t\t\tif (!reconnected) {\n\t\t\t\tthrow new Error(\"Wallet connection required — please approve the connection to sign\");\n\t\t\t}\n\t\t}\n\n\t\tconst nearClient = requireNear(net);\n\t\tconst builder = buildActions(nearClient.transaction(state.accountId), receiverId);\n\n\t\tconst { payload } = await builder.delegate();\n\t\treturn payload;\n\t};\n\n\tconst plugin: SIWNClientPlugin = {\n\t\tid: \"siwn\",\n\t\t$InferServerPlugin: {} as ReturnType<typeof siwn>,\n\n\t\tgetAtoms: (_$fetch) => ({\n\t\t\tnearState,\n\t\t\twalletConnected,\n\t\t\tactiveNetwork,\n\t\t}),\n\n\t\tgetActions: ($fetch: BetterFetch, _$store: ClientStore, _options: BetterAuthClientOptions | undefined): SIWNClientActions => {\n\t\t\tvoid initClient($fetch);\n\n\t\t\treturn {\n\t\t\t\tnear: {\n\t\t\t\t\tnonce: async (params: NonceRequestT, fetchOptions?: BetterFetchOption) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/nonce\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: params,\n\t\t\t\t\t\t\t...fetchOptions\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tverify: async (params: VerifyRequestT, fetchOptions?: BetterFetchOption) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/verify\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: params,\n\t\t\t\t\t\t\t...fetchOptions\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tgetProfile: async (accountId?: AccountId, fetchOptions?: BetterFetchOption) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/profile\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: { accountId },\n\t\t\t\t\t\t\t...fetchOptions\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tview: async (params: ViewContractRequestT, fetchOptions?: BetterFetchOption) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/view\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: params,\n\t\t\t\t\t\t\t...fetchOptions\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tgetAccountId: () => {\n\t\t\t\t\t\tconst state = nearState.get();\n\t\t\t\t\t\treturn state?.accountId || null;\n\t\t\t\t\t},\n\t\t\t\t\tgetState: () => nearState.get(),\n\t\t\t\t\tisWalletConnected: () => walletConnected.get(),\n\t\t\t\t\tensureConnected: async () => {\n\t\t\t\t\t\tconst net = activeNetwork.get();\n\t\t\t\t\t\tif (!initializedNetworks.has(net)) {\n\t\t\t\t\t\t\tif (!(await initClientForNetwork(net))) return false;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (walletConnected.get()) {\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tconst conn = connectors.get(net);\n\t\t\t\t\t\t\t\tif (conn) {\n\t\t\t\t\t\t\t\t\tconst { accounts } = await conn.getConnectedWallet();\n\t\t\t\t\t\t\t\t\tif (accounts?.length) return true;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\t\t\tconsole.error(\"[siwn] restoreFromSession failed:\", err instanceof Error ? err.message : err);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn ensureWalletConnected(net);\n\t\t\t\t\t},\n\t\t\t\t\tdisconnect: async () => {\n\t\t\t\t\t\tfor (const [_net, conn] of connectors) {\n\t\t\t\t\t\t\tif (conn) {\n\t\t\t\t\t\t\t\ttry { await conn.disconnect(); } catch {}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\twalletConnected.set(false);\n\t\t\t\t\t\tnearState.set(null);\n\t\t\t\t\t},\n\t\t\t\t\tlink: async (callbacks?: AuthCallbacks) => {\n\t\t\t\t\t\tconst net = activeNetwork.get();\n\t\t\t\t\t\tconst recipient = getRecipient(net);\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst { signedMessage, accountId, nonceHex } = await signWithWallet();\n\t\t\t\t\t\t\tconst message = `Sign in to ${recipient}`;\n\n\t\t\t\t\t\t\tawait handleAccountConnection(accountId, signedMessage.publicKey, net);\n\n\t\t\t\t\t\t\tconst linkResponse = await $fetch<{ success: boolean; accountId: string; network: string; message: string }>(\"/near/link-account\", {\n\t\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\t\tsignedMessage,\n\t\t\t\t\t\t\t\t\tmessage,\n\t\t\t\t\t\t\t\t\trecipient,\n\t\t\t\t\t\t\t\t\tnonce: nonceHex,\n\t\t\t\t\t\t\t\t\taccountId,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tif (linkResponse.error) {\n\t\t\t\t\t\t\t\tthrow new Error(linkResponse.error.message || \"Failed to link NEAR account\");\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (!linkResponse?.data?.success) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Account linking failed\");\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tcallbacks?.onSuccess?.();\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\t\t\t\t\t\tcallbacks?.onError?.(err);\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\tunlink: async (\n\t\t\t\t\t\tparams: { accountId: string; network?: \"mainnet\" | \"testnet\" },\n\t\t\t\t\t\tfetchOptions?: BetterFetchOption\n\t\t\t\t\t) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/unlink-account\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: params,\n\t\t\t\t\t\t\t...fetchOptions\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tlistAccounts: async (): Promise<BetterFetchResponse<ListAccountsResponseT>> => {\n\t\t\t\t\t\treturn await $fetch(\"/near/list-accounts\", { method: \"GET\" });\n\t\t\t\t\t},\n\t\t\t\t\tsetPrimaryAccount: async (params: SetPrimaryAccountRequestT) => {\n\t\t\t\t\t\tconst response = await $fetch<SetPrimaryAccountResponseT>(\"/near/set-primary-account\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: params,\n\t\t\t\t\t\t});\n\t\t\t\t\t\tconst activeAccount = response.data?.activeAccount;\n\t\t\t\t\t\tif (activeAccount) {\n\t\t\t\t\t\t\tnearState.set({\n\t\t\t\t\t\t\t\taccountId: activeAccount.accountId,\n\t\t\t\t\t\t\t\tpublicKey: activeAccount.publicKey ?? null,\n\t\t\t\t\t\t\t\tnetworkId: activeAccount.network,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tactiveNetwork.set(activeAccount.network);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn response;\n\t\t\t\t\t},\n\t\t\t\t\tbuildSignedDelegateAction: async (\n\t\t\t\t\t\treceiverId: string,\n\t\t\t\t\t\tbuildActions: (builder: TransactionBuilder, receiverId: string) => TransactionBuilder,\n\t\t\t\t\t): Promise<string> => {\n\t\t\t\t\t\treturn buildSignedDelegateActionInternal(receiverId, buildActions);\n\t\t\t\t\t},\n\t\t\t\t\trelayTransaction: async (params: { payload: string }) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/relay\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: params,\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tgetRelayStatus: async (txHash: string) => {\n\t\t\t\t\t\treturn await $fetch(`/near/relay-status/${txHash}`, {\n\t\t\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tgetRelayerInfo: async (params?: GetRelayerInfoRequestT) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/relayer-info\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: { ...(params ?? {}), network: params?.network ?? activeNetwork.get() },\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\trelayHistory: async () => {\n\t\t\t\t\t\treturn await $fetch(\"/near/relay-history\", {\n\t\t\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\t\tcreateSubAccount: async (params: CreateSubAccountRequestT) => {\n\t\t\t\t\t\treturn await $fetch(\"/near/create-sub-account\", {\n\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\tbody: { ...params, network: params.network ?? activeNetwork.get() },\n\t\t\t\t\t\t});\n\t\t\t\t\t},\n\t\t\t\tcheckSubAccountAvailability: async (params: CheckSubAccountAvailabilityRequestT) => {\n\t\t\t\t\tif (!SUB_ACCOUNT_LABEL_REGEX.test(params.subAccountName) || params.subAccountName.length < 2) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tdata: { available: false, accountId: \"\", reason: \"invalid\" as const },\n\t\t\t\t\t\t\terror: null,\n\t\t\t\t\t\t} as BetterFetchResponse<CheckSubAccountAvailabilityResponseT>;\n\t\t\t\t\t}\n\t\t\t\t\treturn await $fetch(\"/near/check-sub-account-availability\", {\n\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\tbody: { ...params, network: params.network ?? activeNetwork.get() },\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t\t\tsetNetwork: (network: \"mainnet\" | \"testnet\") => {\n\t\t\t\t\t\tconst prev = activeNetwork.get();\n\t\t\t\t\t\tif (prev !== network) {\n\t\t\t\t\t\t\tconst oldConn = connectors.get(prev);\n\t\t\t\t\t\t\tif (oldConn) {\n\t\t\t\t\t\t\t\tvoid oldConn.disconnect().catch(() => {});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\twalletConnected.set(false);\n\t\t\t\t\t\t\tnearState.set(null);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tactiveNetwork.set(network);\n\t\t\t\t\t\tvoid initClientForNetwork(network);\n\t\t\t\t\t},\n\t\t\t\t\tgetNetwork: () => activeNetwork.get(),\n\t\t\t\t\tgetSupportedNetworks: () => getSupportedNetworks(),\n\t\t\t\t\tgetRecipient: (network?: \"mainnet\" | \"testnet\") => getRecipient(network),\n\t\t\t\t\tget client(): NearType {\n\t\t\t\t\t\tconst net = activeNetwork.get();\n\t\t\t\t\t\tconst client = nearClients.get(net);\n\t\t\t\t\t\tif (!client) throw new Error(`Wallet not initialized for ${net} — this operation requires a browser environment`);\n\t\t\t\t\t\treturn client;\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\tsignIn: {\n\t\t\t\t\tnear: async (callbacks?: AuthCallbacks) => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst { signedMessage, accountId, nonceHex } = await signWithWallet();\n\t\t\t\t\t\t\tconst net = activeNetwork.get();\n\t\t\t\t\t\t\tconst recipient = getRecipient(net);\n\t\t\t\t\t\t\tconst message = `Sign in to ${recipient}`;\n\n\t\t\t\t\t\t\tawait handleAccountConnection(accountId, signedMessage.publicKey, net);\n\n\t\t\t\t\t\t\tconst verifyResponse: BetterFetchResponse<VerifyResponseT> = await $fetch(\"/near/verify\", {\n\t\t\t\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\t\t\t\tbody: {\n\t\t\t\t\t\t\t\t\tsignedMessage,\n\t\t\t\t\t\t\t\t\tmessage,\n\t\t\t\t\t\t\t\t\trecipient,\n\t\t\t\t\t\t\t\t\tnonce: nonceHex,\n\t\t\t\t\t\t\t\t\taccountId,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tif (verifyResponse.error) {\n\t\t\t\t\t\t\t\tthrow new Error(verifyResponse.error.message || \"Failed to verify signature\");\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tif (!verifyResponse?.data?.success) {\n\t\t\t\t\t\t\t\tthrow new Error(\"Authentication verification failed\");\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tcallbacks?.onSuccess?.();\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\tconst err = error instanceof Error ? error : new Error(String(error));\n\t\t\t\t\t\t\tcallbacks?.onError?.(err);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t};\n\t\t}\n\t};\n\n\treturn plugin;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmCA,MAAa,oBAAoB,EAAE,OAAO;CACzC,KAAK,EAAE,OAAO,EAAE,SAAS;CACzB,UAAU,EAAE,OAAO,EAAE,SAAS;AAC/B,CAAC;AAED,MAAa,gBAAgB,EAAE,OAAO;CACrC,MAAM,EAAE,OAAO,EAAE,SAAS;CAC1B,aAAa,EAAE,OAAO,EAAE,SAAS;CACjC,OAAO,kBAAkB,SAAS;CAClC,iBAAiB,kBAAkB,SAAS;CAC5C,UAAU,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,OAAO,CAAC,EAAE,SAAS;AACrD,CAAC;AAKD,MAAM,sBAAsB,EAAE,OAAO;CACpC,WAAW,EAAE,OAAO;CACpB,WAAW,EAAE,OAAO;CACpB,WAAW,EAAE,OAAO;CACpB,OAAO,EAAE,OAAO,EAAE,SAAS;AAC5B,CAAC;AAEiC,EAAE,OAAO;C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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iCAPU,CAAC;iCAC3C,CAAC;;;;;;;;;6BAM8B,CAAC;;;;;;;;;;;;;;;;;;;iCAPU,CAAC;iCAC3C,CAAC;;;;;;;;;6BAM8B,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;iCAPU,CAAC;iCAC3C,CAAC;;;;;;;;;6BAM8B,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iCAPU,CAAC;iCAC3C,CAAC;;;;;;;;;6BAM8B,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iCAPU,CAAC;iCAC3C,CAAC;;;;;;;;;6BAM8B,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;iCAPU,CAAC;iCAC3C,CAAC;;;;;;;;;6BAM8B,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iCAPU,CAAC;iCAC3C,CAAC;;;;;;;;;6BAM8B,CAAC;;;;;;;;;;;CAgjDhC,CAAC"}
package/dist/index.js CHANGED
@@ -1404,10 +1404,17 @@ const siwn = (options) => {
1404
1404
  const parentKey = getParentKey(network);
1405
1405
  const rState = await ensureRelayer(ctx.context.adapter, ctx.context.secret, network);
1406
1406
  const { parentAccount, subAccountAvailable } = resolveParentAccount(subAccountCfg, parentKey, rState);
1407
- if (!subAccountAvailable || !parentAccount) throw new APIError("SERVICE_UNAVAILABLE", {
1408
- message: "Sub-account creation requires a named parent account. Configure subAccount.parentAccount in your SIWN plugin options, or use an explicit relayer with a named account.",
1409
- status: 503
1410
- });
1407
+ if (!subAccountAvailable || !parentAccount) {
1408
+ const configuredParent = subAccountCfg?.parentAccount;
1409
+ let msg;
1410
+ if (configuredParent && !parentKey) msg = `Sub-account creation unavailable on ${network}: parent key not configured for ${configuredParent}. Set NEAR_SUB_ACCOUNT_PARENT_KEY_${network.toUpperCase()} in your environment, or use an explicit relayer with a named account.`;
1411
+ else if (!configuredParent) msg = `Sub-account creation unavailable on ${network}: no parent account configured. Set subAccount.${network}.parentAccount in your SIWN plugin options, or use an explicit relayer with a named account.`;
1412
+ else msg = `Sub-account creation unavailable on ${network}: requires a named parent account with a parent key, or an explicit relayer with a named account.`;
1413
+ throw new APIError("SERVICE_UNAVAILABLE", {
1414
+ message: msg,
1415
+ status: 503
1416
+ });
1417
+ }
1411
1418
  const effectiveParentKey = getRawParentKey(network);
1412
1419
  if (parentAccount !== rState?.accountId && !effectiveParentKey) throw new APIError("SERVICE_UNAVAILABLE", {
1413
1420
  message: "Sub-account parent differs from relayer account. Provide secrets.parentKey to sign as the parent account.",
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["z"],"sources":["../src/profile.ts","../src/schema.ts","../src/types.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { AccountId, Profile, SocialImage } from \"./types.js\";\n\nconst FALLBACK_URL =\n\t\"https://ipfs.near.social/ipfs/bafkreidn5fb2oygegqaldx7ycdmhu4owcrmoxd7ekbzfmeakkobz2ja7qy\";\n\nfunction getNetworkFromAccountId(accountId: string): \"mainnet\" | \"testnet\" {\n\treturn accountId.endsWith('.testnet') ? 'testnet' : 'mainnet';\n}\n\nfunction getImageUrl(\n\timage: SocialImage | undefined,\n\tfallback?: string,\n): string {\n\tif (image?.url) return image.url;\n\tif (image?.ipfs_cid) return `https://ipfs.near.social/ipfs/${image.ipfs_cid}`;\n\treturn fallback || FALLBACK_URL;\n}\n\ninterface SocialApiResponse {\n\t[accountId: string]: {\n\t\tprofile?: Profile;\n\t};\n}\n\ninterface KvEntry {\n\tvalue: Record<string, unknown> | string;\n\tblock_height?: number;\n\tblock_hash?: string;\n\ttimestamp_nanosec?: string;\n}\n\ninterface KvResponse {\n\tentries: KvEntry[];\n}\n\nasync function defaultGetProfile(accountId: AccountId, apiKey?: string): Promise<Profile | null> {\n\tconst network = getNetworkFromAccountId(accountId);\n\n\ttry {\n\t\tconst kvUrl = network === \"testnet\"\n\t\t\t? \"https://kv.test.fastnear.com\"\n\t\t\t: \"https://kv.main.fastnear.com\";\n\n\t\tconst effectiveApiKey = apiKey || process.env.FASTNEAR_API_KEY;\n\t\tconst headers: Record<string, string> = { \"Content-Type\": \"application/json\" };\n\t\tif (effectiveApiKey) {\n\t\t\theaders[\"Authorization\"] = `Bearer ${effectiveApiKey}`;\n\t\t}\n\n\t\tconst response = await fetch(\n\t\t\t`${kvUrl}/v0/latest/social.near/${accountId}/profile/**`,\n\t\t\t{ headers },\n\t\t);\n\n\t\tif (response.ok) {\n\t\t\tconst data = await response.json() as KvResponse;\n\t\t\tconst entry = data?.entries?.[0];\n\t\t\tif (entry?.value) {\n\t\t\t\ttry {\n\t\t\t\t\tconst profile = typeof entry.value === \"string\"\n\t\t\t\t\t\t? JSON.parse(entry.value)\n\t\t\t\t\t\t: entry.value;\n\t\t\t\t\tif (profile?.name || profile?.description || profile?.image) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tname: profile.name,\n\t\t\t\t\t\t\tdescription: profile.description,\n\t\t\t\t\t\t\timage: profile.image,\n\t\t\t\t\t\t\tbackgroundImage: profile.backgroundImage,\n\t\t\t\t\t\t\tlinktree: profile.linktree,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t} catch {}\n\t\t\t}\n\t\t}\n\n\t\tconst apiBase = {\n\t\t\tmainnet: \"https://api.near.social\",\n\t\t\ttestnet: \"https://test.api.near.social\",\n\t\t}[network];\n\n\t\tconst keys = [`${accountId}/profile/**`];\n\n\t\tconst fallbackResponse = await fetch(`${apiBase}/get`, {\n\t\t\tmethod: \"POST\",\n\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\tbody: JSON.stringify({ keys })\n\t\t});\n\n\t\tif (!fallbackResponse.ok) {\n\t\t\tthrow new Error(`HTTP error! status: ${fallbackResponse.status}`);\n\t\t}\n\n\t\tconst fallbackData = await fallbackResponse.json() as SocialApiResponse;\n\t\tconst profile: Profile | undefined = fallbackData?.[accountId]?.profile;\n\n\t\tif (profile) {\n\t\t\treturn {\n\t\t\t\tname: profile.name,\n\t\t\t\tdescription: profile.description,\n\t\t\t\timage: profile.image,\n\t\t\t\tbackgroundImage: profile.backgroundImage,\n\t\t\t\tlinktree: profile.linktree\n\t\t\t};\n\t\t}\n\t\treturn null;\n\t} catch (error) {\n\t\treturn null;\n\t}\n}\n\nexport { defaultGetProfile, getImageUrl, getNetworkFromAccountId };\n","import type { BetterAuthPluginDBSchema } from \"better-auth/db\";\n\nexport const schema = {\n\tnearAccount: {\n\t\tfields: {\n\t\t\tuserId: {\n\t\t\t\ttype: \"string\",\n\t\t\t\treferences: {\n\t\t\t\t\tmodel: \"user\",\n\t\t\t\t\tfield: \"id\",\n\t\t\t\t},\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\taccountId: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tnetwork: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tpublicKey: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tisPrimary: {\n\t\t\t\ttype: \"boolean\",\n\t\t\t\tdefaultValue: false,\n\t\t\t},\n\t\t\tcreatedAt: {\n\t\t\t\ttype: \"date\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t},\n\t},\n\trelayedTransaction: {\n\t\tfields: {\n\t\t\tuserId: {\n\t\t\t\ttype: \"string\",\n\t\t\t\treferences: {\n\t\t\t\t\tmodel: \"user\",\n\t\t\t\t\tfield: \"id\",\n\t\t\t\t},\n\t\t\t},\n\t\t\ttxHash: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tsenderId: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\treceiverId: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tnetwork: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tstatus: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tgasUsed: {\n\t\t\t\ttype: \"string\",\n\t\t\t},\n\t\t\tcreatedAt: {\n\t\t\t\ttype: \"date\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tupdatedAt: {\n\t\t\t\ttype: \"date\",\n\t\t\t},\n\t\t},\n\t},\n\trelayerKey: {\n\t\tfields: {\n\t\t\taccountId: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tencryptedPrivateKey: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tiv: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tpublicKey: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tnetwork: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tcreatedAt: {\n\t\t\t\ttype: \"date\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tlastUsedAt: {\n\t\t\t\ttype: \"date\",\n\t\t\t},\n\t\t},\n\t},\n} satisfies BetterAuthPluginDBSchema;\n","import { z } from \"zod\";\nimport { AccountIdSchema, type AccountId } from \"near-kit/schemas\";\nimport type { AccountState, Near, TransactionBuilder } from \"near-kit\";\n\nexport type { AccountId };\n\nexport interface NearAccount {\n\tid: string;\n\tuserId: string;\n\taccountId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n\tpublicKey: string;\n\tisPrimary: boolean;\n\tcreatedAt: Date;\n}\n\nexport interface ListedNearAccount extends NearAccount {\n\tproviderId: \"siwn\";\n\tisActive: boolean;\n\tisAvailable: boolean;\n}\n\nexport interface ListAccountsResponseT {\n\taccounts: ListedNearAccount[];\n\tactiveAccount: ListedNearAccount | null;\n\tavailableAccounts: ListedNearAccount[];\n}\n\nexport interface SetPrimaryAccountResponseT extends ListAccountsResponseT {\n\tsuccess: boolean;\n\taccountId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n\tmessage: string;\n}\n\nexport const socialImageSchema = z.object({\n\turl: z.string().optional(),\n\tipfs_cid: z.string().optional(),\n});\n\nexport const profileSchema = z.object({\n\tname: z.string().optional(),\n\tdescription: z.string().optional(),\n\timage: socialImageSchema.optional(),\n\tbackgroundImage: socialImageSchema.optional(),\n\tlinktree: z.record(z.string(), z.string()).optional(),\n});\n\nexport type SocialImage = z.infer<typeof socialImageSchema>;\nexport type Profile = z.infer<typeof profileSchema>;\n\nconst signedMessageSchema = z.object({\n\taccountId: z.string(),\n\tpublicKey: z.string(),\n\tsignature: z.string(),\n\tstate: z.string().optional(),\n});\n\nexport const LinkAccountRequest = z.object({\n\tsignedMessage: signedMessageSchema,\n\tmessage: z.string(),\n\trecipient: z.string(),\n\tnonce: z.string(),\n\taccountId: AccountIdSchema,\n\tcallbackUrl: z.string().optional(),\n});\n\nexport const SetPrimaryAccountRequest = z.object({\n\taccountId: AccountIdSchema,\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n});\n\nexport const NonceRequest = z.object({\n\taccountId: AccountIdSchema,\n\tnetworkId: z.union([z.literal(\"mainnet\"), z.literal(\"testnet\")])\n});\n\nexport const VerifyRequest = z.object({\n\tsignedMessage: signedMessageSchema,\n\tmessage: z.string(),\n\trecipient: z.string(),\n\tnonce: z.string(),\n\taccountId: AccountIdSchema,\n\tcallbackUrl: z.string().optional(),\n});\n\nexport const RelayRequest = z.object({\n\tpayload: z.string(),\n});\nexport type RelayRequestT = z.infer<typeof RelayRequest>;\n\nexport const RelayResponse = z.object({\n\ttxHash: z.string(),\n\tstatus: z.enum([\"pending\", \"completed\", \"failed\"]),\n});\nexport type RelayResponseT = z.infer<typeof RelayResponse>;\n\nexport const RelayStatusResponse = z.object({\n\tstatus: z.enum([\"pending\", \"completed\", \"failed\"]),\n\tgasUsed: z.string().optional(),\n\toutcome: z.unknown().optional(),\n});\nexport type RelayStatusResponseT = z.infer<typeof RelayStatusResponse>;\n\nexport const ViewContractRequest = z.object({\n\tcontractId: z.string(),\n\tmethodName: z.string(),\n\targs: z.record(z.string(), z.any()).optional(),\n});\nexport type ViewContractRequestT = z.infer<typeof ViewContractRequest>;\n\nexport const NonceResponse = z.object({ nonce: z.string() });\nexport const VerifyResponse = z.object({\n\ttoken: z.string(),\n\tsuccess: z.literal(true),\n\tuser: z.object({\n\t\tid: z.string(),\n\t\taccountId: AccountIdSchema,\n\t\tnetwork: z.union([z.literal(\"mainnet\"), z.literal(\"testnet\")]),\n\t}),\n});\nexport const ProfileResponse = profileSchema.nullable();\nexport const ViewContractResponse = z.object({ result: z.unknown() });\n\nexport const ProfileRequest = z.object({\n\taccountId: AccountIdSchema.optional(),\n});\nexport type ProfileRequestT = z.infer<typeof ProfileRequest>;\n\nexport type NonceRequestT = z.infer<typeof NonceRequest>;\nexport type NonceResponseT = z.infer<typeof NonceResponse>;\nexport type SetPrimaryAccountRequestT = z.infer<typeof SetPrimaryAccountRequest>;\nexport type VerifyRequestT = z.infer<typeof VerifyRequest>;\nexport type VerifyResponseT = z.infer<typeof VerifyResponse>;\nexport type ProfileResponseT = z.infer<typeof ProfileResponse>;\nexport type ViewContractResponseT = z.infer<typeof ViewContractResponse>;\n\nexport const RelayedTransactionSchema = z.object({\n\tid: z.string(),\n\tuserId: z.string(),\n\ttxHash: z.string(),\n\tsenderId: z.string(),\n\treceiverId: z.string(),\n\tnetwork: z.string(),\n\tstatus: z.string(),\n\tgasUsed: z.string().optional(),\n\tcreatedAt: z.string(),\n\tupdatedAt: z.string().optional(),\n});\nexport type RelayedTransactionT = z.infer<typeof RelayedTransactionSchema>;\n\nexport const RelayHistoryResponse = z.object({\n\ttransactions: z.array(RelayedTransactionSchema),\n});\nexport type RelayHistoryResponseT = z.infer<typeof RelayHistoryResponse>;\n\nexport interface RelayedTransactionRecord {\n\tid: string;\n\tuserId: string;\n\ttxHash: string;\n\tsenderId: string;\n\treceiverId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n\tstatus: string;\n\tgasUsed?: string;\n\tcreatedAt: Date;\n\tupdatedAt: Date;\n}\n\nexport interface RelayerInfo extends AccountState {\n\taccountId: string;\n\tmode: \"ephemeral\" | \"explicit\";\n\tnetwork: \"mainnet\" | \"testnet\";\n\tpublicKey: string;\n\thasKey: boolean;\n\tcreatedAt?: Date;\n\tlastUsedAt?: Date;\n\tparentAccount?: string;\n\tsubAccountAvailable?: boolean;\n}\n\nexport interface DualNetworkConfig<T> {\n\tmainnet: T;\n\ttestnet: T;\n}\n\nexport const GetRelayerInfoRequest = z.object({\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n});\nexport type GetRelayerInfoRequestT = z.infer<typeof GetRelayerInfoRequest>;\n\nexport interface SubAccountRelayerFCAKConfig {\n\treceiverId: string;\n\tmethodNames?: string[];\n\tallowance?: string;\n}\n\nexport interface SubAccountTxCtx {\n\tnewAccountId: string;\n\tparentAccount: string;\n\tuserPublicKey: string;\n\tuserAccountId: string;\n\tuserId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n}\n\nexport interface SubAccountLifecycleCtx extends SubAccountTxCtx {\n\tnear: Near;\n}\n\nexport interface SubAccountConfig {\n\tparentAccount?: string;\n\tminDeposit?: string;\n\tparentHasFullAccess?: boolean;\n\tdeploy?:\n\t\t| { wasm: Uint8Array }\n\t\t| { fromPublished: { accountId?: string; codeHash?: string } };\n\tinit?: {\n\t\tmethodName: string;\n\t\targs: object | ((ctx: SubAccountTxCtx) => object);\n\t};\n\textendTx?: (tx: TransactionBuilder, ctx: SubAccountTxCtx) => TransactionBuilder;\n\tonCreated?: (ctx: SubAccountLifecycleCtx) => Promise<void>;\n\tonRollback?: (ctx: SubAccountLifecycleCtx) => Promise<void>;\n\taddRelayerFCAK?: boolean;\n\trelayerFCAK?: SubAccountRelayerFCAKConfig;\n}\n\nexport const SUB_ACCOUNT_LABEL_REGEX = /^([a-z\\d]+[-_])*[a-z\\d]+$/;\n\nexport const SubAccountNameSchema = z\n\t.string()\n\t.min(2, \"Sub-account name must be at least 2 characters\")\n\t.max(64, \"Sub-account name must be at most 64 characters\")\n\t.regex(SUB_ACCOUNT_LABEL_REGEX, \"Sub-account name must contain only lowercase alphanumeric characters, hyphens, and underscores (no leading/trailing hyphens or underscores)\");\n\nexport const CreateSubAccountRequest = z.object({\n\tsubAccountName: SubAccountNameSchema,\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n\tpublicKey: z.string(),\n});\nexport type CreateSubAccountRequestT = z.infer<typeof CreateSubAccountRequest>;\n\nexport const CreateSubAccountResponse = z.object({\n\tsuccess: z.literal(true),\n\taccountId: z.string(),\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]),\n\tpublicKey: z.string(),\n\tmessage: z.string(),\n});\nexport type CreateSubAccountResponseT = z.infer<typeof CreateSubAccountResponse>;\n\nexport const CheckSubAccountAvailabilityRequest = z.object({\n\tsubAccountName: SubAccountNameSchema,\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n});\nexport type CheckSubAccountAvailabilityRequestT = z.infer<typeof CheckSubAccountAvailabilityRequest>;\n\nexport const CheckSubAccountAvailabilityResponse = z.object({\n\tavailable: z.boolean(),\n\taccountId: z.string(),\n\tparentAccount: z.string().optional(),\n\treason: z.enum([\"taken\", \"invalid\", \"too-long\", \"not-configured\"]).optional(),\n});\nexport type CheckSubAccountAvailabilityResponseT = z.infer<typeof CheckSubAccountAvailabilityResponse>;\n","export function bytesToBase64(bytes: Uint8Array): string {\n\treturn btoa(String.fromCharCode(...bytes));\n}\n\nfunction base64ToBytes(base64: string): Uint8Array {\n\treturn Uint8Array.from(atob(base64), c => c.charCodeAt(0));\n}\n\nexport function bytesToHex(bytes: Uint8Array): string {\n\treturn Array.from(bytes).map(b => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nexport function hexToBytes(hex: string): Uint8Array {\n\tconst bytes = new Uint8Array(hex.length / 2);\n\tfor (let i = 0; i < hex.length; i += 2) {\n\t\tbytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);\n\t}\n\treturn bytes;\n}\n\nasync function deriveAesKey(secret: string): Promise<CryptoKey> {\n\tconst keyMaterial = await crypto.subtle.importKey(\n\t\t\"raw\",\n\t\tnew TextEncoder().encode(secret),\n\t\t{ name: \"HKDF\" },\n\t\tfalse,\n\t\t[\"deriveKey\"],\n\t);\n\treturn crypto.subtle.deriveKey(\n\t\t{\n\t\t\tname: \"HKDF\",\n\t\t\thash: \"SHA-256\",\n\t\t\tsalt: new TextEncoder().encode(\"better-near-auth-relayer\"),\n\t\t\tinfo: new Uint8Array(0),\n\t\t},\n\t\tkeyMaterial,\n\t\t{ name: \"AES-GCM\", length: 256 },\n\t\tfalse,\n\t\t[\"encrypt\", \"decrypt\"],\n\t);\n}\n\nexport async function encryptPrivateKey(\n\tprivateKey: Uint8Array,\n\tsecret: string,\n): Promise<{ encrypted: string; iv: string }> {\n\tconst aesKey = await deriveAesKey(secret);\n\tconst iv = crypto.getRandomValues(new Uint8Array(12));\n\tconst encrypted = await crypto.subtle.encrypt(\n\t\t{ name: \"AES-GCM\", iv },\n\t\taesKey,\n\t\tprivateKey as Uint8Array<ArrayBuffer>,\n\t);\n\treturn {\n\t\tencrypted: bytesToBase64(new Uint8Array(encrypted)),\n\t\tiv: bytesToBase64(iv),\n\t};\n}\n\nexport async function decryptPrivateKey(\n\tencrypted: string,\n\tiv: string,\n\tsecret: string,\n): Promise<Uint8Array> {\n\tconst aesKey = await deriveAesKey(secret);\n\tconst decrypted = await crypto.subtle.decrypt(\n\t\t{ name: \"AES-GCM\", iv: base64ToBytes(iv) as Uint8Array<ArrayBuffer> },\n\t\taesKey,\n\t\tbase64ToBytes(encrypted) as Uint8Array<ArrayBuffer>,\n\t);\n\treturn new Uint8Array(decrypted);\n}\n","import { APIError, createAuthEndpoint, createAuthMiddleware, sessionMiddleware } from \"better-auth/api\";\nimport { setSessionCookie } from \"better-auth/cookies\";\nimport type { Account, User, DBAdapter } from \"better-auth/types\";\n// Explicit type imports for declaration emit\nimport type {} from \"@better-auth/core/env\";\nimport type {} from \"@better-auth/core/oauth2\";\nimport type {} from \"better-call\";\n\nimport { Near, generateNonce, generateKey, parseKey, verifyNep413Signature, decodeSignedDelegateAction, InMemoryKeyStore, RotatingKeyStore } from \"near-kit\";\nimport type { SignedDelegateAction, PrivateKey } from \"near-kit\";\nimport { hex, base58 } from \"@scure/base\";\nimport z from \"zod\";\nimport { defaultGetProfile, getImageUrl, getNetworkFromAccountId } from \"./profile.js\";\nimport { schema } from \"./schema.js\";\nimport {\n\ttype AccountId,\n\ttype DualNetworkConfig,\n\ttype ListAccountsResponseT,\n\ttype ListedNearAccount,\n\ttype NearAccount,\n\ttype Profile,\n\ttype RelayerInfo,\n\ttype RelayedTransactionRecord,\n\ttype SubAccountConfig,\n\tLinkAccountRequest,\n\tNonceRequest,\n\tNonceResponse,\n\tProfileRequest,\n\tProfileResponse,\n\tVerifyRequest,\n\tVerifyResponse,\n\tRelayRequest,\n\tRelayResponse,\n\tRelayStatusResponse,\n\tSetPrimaryAccountRequest,\n\tViewContractRequest,\n\tViewContractResponse,\n\tGetRelayerInfoRequest,\n\tCreateSubAccountRequest,\n\tCreateSubAccountResponse,\n\tCheckSubAccountAvailabilityRequest,\n\tCheckSubAccountAvailabilityResponse,\n\ttype SubAccountTxCtx,\n\ttype SubAccountLifecycleCtx,\n} from \"./types.js\";\nexport * from \"./types.js\";\nimport {\n\tbytesToHex,\n\tencryptPrivateKey,\n\tdecryptPrivateKey,\n} from \"./utils.js\";\n\nasync function hashNonce(nonce: Uint8Array): Promise<string> {\n\tconst data = new TextEncoder().encode(hex.encode(nonce));\n\tconst hashBuffer = await crypto.subtle.digest('SHA-256', data);\n\treturn hex.encode(new Uint8Array(hashBuffer));\n}\n\nfunction deriveEmail(accountId: string, recipient: string): string {\n\tif (accountId.endsWith(\".near\")) {\n\t\tconst localPart = accountId.slice(0, -5);\n\t\tif (!localPart.includes(\".\")) {\n\t\t\treturn `${localPart}@near.email`;\n\t\t}\n\t}\n\tconst randomId = crypto.randomUUID().slice(0, 8);\n\treturn `temp-${randomId}@${recipient}`;\n}\n\nfunction nearAccountKey(account: Pick<NearAccount, \"accountId\" | \"network\">): string {\n\treturn `${account.accountId}:${account.network}`;\n}\n\nfunction getCreatedAtTime(account: NearAccount): number {\n\treturn account.createdAt instanceof Date\n\t\t? account.createdAt.getTime()\n\t\t: new Date(account.createdAt).getTime();\n}\n\nfunction buildListAccountsResponse(nearAccounts: NearAccount[]): ListAccountsResponseT {\n\tconst activeAccount = nearAccounts.find((account) => account.isPrimary) ?? nearAccounts[0] ?? null;\n\tconst activeKey = activeAccount ? nearAccountKey(activeAccount) : null;\n\tconst accounts: ListedNearAccount[] = nearAccounts\n\t\t.map((account) => {\n\t\t\tconst isActive = activeKey === nearAccountKey(account);\n\t\t\treturn {\n\t\t\t\t...account,\n\t\t\t\tproviderId: \"siwn\" as const,\n\t\t\t\tisActive,\n\t\t\t\tisAvailable: !isActive,\n\t\t\t};\n\t\t})\n\t\t.sort((a, b) => {\n\t\t\tif (a.isActive !== b.isActive) return a.isActive ? -1 : 1;\n\t\t\treturn getCreatedAtTime(a) - getCreatedAtTime(b);\n\t\t});\n\tconst listedActiveAccount = accounts.find((account) => account.isActive) ?? null;\n\n\treturn {\n\t\taccounts,\n\t\tactiveAccount: listedActiveAccount ? { ...listedActiveAccount } : null,\n\t\tavailableAccounts: accounts\n\t\t\t.filter((account) => account.isAvailable)\n\t\t\t.map((account) => ({ ...account })),\n\t};\n}\n\nexport interface RelayerConfig {\n\taccountId?: string;\n\tprivateKey?: string;\n\tprivateKeys?: string[];\n\twhitelistedContracts?: string[];\n\tmaxGasPerTransaction?: string;\n\tmaxDepositPerTransaction?: string;\n}\n\ninterface RelayerState {\n\tnear: Near;\n\taccountId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n\tmode: \"ephemeral\" | \"explicit\";\n\tpublicKey: string;\n\tcreatedAt?: Date;\n\tlastUsedAt?: Date;\n}\n\nfunction createNear(\n\tnetwork: \"mainnet\" | \"testnet\",\n\theaders: Record<string, string>,\n\trpcUrl?: string,\n\tkeyStore?: InMemoryKeyStore | RotatingKeyStore,\n): Near {\n\tconst config: ConstructorParameters<typeof Near>[0] = { headers };\n\tif (rpcUrl) {\n\t\tconfig.network = { rpcUrl, networkId: network };\n\t} else {\n\t\tconfig.network = network;\n\t}\n\tif (keyStore) {\n\t\tconfig.keyStore = keyStore;\n\t}\n\treturn new Near(config);\n}\n\nasync function initRelayer(\n\trelayerConfig: RelayerConfig | undefined,\n\tnetwork: \"mainnet\" | \"testnet\",\n\tadapter: DBAdapter,\n\tsecret: string | undefined,\n\tapiKey?: string,\n\trpcUrl?: string,\n): Promise<RelayerState | null> {\n\tif (!relayerConfig) return null;\n\n\tconst headers: Record<string, string> = {};\n\tif (apiKey) {\n\t\theaders[\"Authorization\"] = `Bearer ${apiKey}`;\n\t}\n\n\tif (relayerConfig.accountId && (relayerConfig.privateKey || relayerConfig.privateKeys)) {\n\t\tconst keys = relayerConfig.privateKeys ?? (relayerConfig.privateKey ? [relayerConfig.privateKey] : []);\n\t\tlet keyStore: InMemoryKeyStore | RotatingKeyStore;\n\n\t\tif (keys.length === 1) {\n\t\t\tkeyStore = new InMemoryKeyStore({\n\t\t\t\t[relayerConfig.accountId]: keys[0]!,\n\t\t\t});\n\t\t} else {\n\t\t\tkeyStore = new RotatingKeyStore({\n\t\t\t\t[relayerConfig.accountId]: keys as string[],\n\t\t\t});\n\t\t}\n\n\t\tconst near = createNear(network, headers, rpcUrl, keyStore);\n\t\tconst explicitPublicKey = parseKey(keys[0]!);\n\t\treturn {\n\t\t\tnear,\n\t\t\taccountId: relayerConfig.accountId,\n\t\t\tnetwork,\n\t\t\tmode: \"explicit\",\n\t\t\tpublicKey: explicitPublicKey.publicKey.toString(),\n\t\t};\n\t}\n\n\tconst existing = await adapter.findOne<{ encryptedPrivateKey: string; iv: string; createdAt: Date; lastUsedAt: Date }>({\n\t\tmodel: \"relayerKey\",\n\t\twhere: [{ field: \"network\", operator: \"eq\", value: network }],\n\t});\n\n\tif (existing) {\n\t\tif (!secret) throw new Error(\"BETTER_AUTH_SECRET required for relayer key decryption\");\n\t\tconst privateKeyBytes = await decryptPrivateKey(existing.encryptedPrivateKey, existing.iv, secret);\n\t\tconst keyPair = parseKey(`ed25519:${base58.encode(privateKeyBytes)}`);\n\t\tconst accountId = bytesToHex(keyPair.publicKey.data);\n\n\t\tconsole.log(`[siwn] Relayer recovered: ${accountId} (${network})`);\n\n\t\tconst keyStore = new InMemoryKeyStore();\n\t\tawait keyStore.add(accountId, keyPair);\n\n\t\tconst near = createNear(network, headers, rpcUrl, keyStore);\n\t\treturn {\n\t\t\tnear,\n\t\t\taccountId,\n\t\t\tnetwork,\n\t\t\tmode: \"ephemeral\",\n\t\t\tpublicKey: keyPair.publicKey.toString(),\n\t\t\tcreatedAt: existing.createdAt,\n\t\t\tlastUsedAt: existing.lastUsedAt,\n\t\t};\n\t}\n\n\tconst keyPair = generateKey();\n\tif (!secret) throw new Error(\"BETTER_AUTH_SECRET required for relayer key encryption\");\n\tconst privateKeyBytes = keyPair.secretKey.startsWith(\"ed25519:\")\n\t\t? base58.decode(keyPair.secretKey.slice(8))\n\t\t: new Uint8Array(0);\n\n\tconst publicKeyBase58 = keyPair.publicKey.toString().replace(\"ed25519:\", \"\");\n\tconst accountId = bytesToHex(keyPair.publicKey.data);\n\tconst createdAt = new Date();\n\n\tconst { encrypted, iv } = await encryptPrivateKey(privateKeyBytes, secret);\n\n\tawait adapter.create({\n\t\t\tmodel: \"relayerKey\",\n\t\t\tdata: {\n\t\t\t\taccountId,\n\t\t\t\tencryptedPrivateKey: encrypted,\n\t\t\t\tiv,\n\t\t\t\tpublicKey: `ed25519:${publicKeyBase58}`,\n\t\t\t\tnetwork,\n\t\t\t\tcreatedAt,\n\t\t\t\tlastUsedAt: createdAt,\n\t\t\t},\n\t\t});\n\n\tconsole.log(`[siwn] Relayer created in EPHEMERAL mode: ${accountId} (${network})`);\n\tconsole.log(`[siwn] Fund this account with NEAR to enable gasless relay`);\n\tconsole.log(`[siwn] Private key is encrypted in DB — persists across restarts`);\n\n\tconst keyStore = new InMemoryKeyStore();\n\tawait keyStore.add(accountId, keyPair);\n\n\tconst near = createNear(network, headers, rpcUrl, keyStore);\n\treturn {\n\t\tnear,\n\t\taccountId,\n\t\tnetwork,\n\t\tmode: \"ephemeral\",\n\t\tpublicKey: keyPair.publicKey.toString(),\n\t\tcreatedAt,\n\t\tlastUsedAt: createdAt,\n\t};\n}\n\nasync function relayOnChain(\n\tpayload: string,\n\trelayerState: RelayerState,\n): Promise<{ txHash: string }> {\n\tconst userAction = decodeSignedDelegateAction(payload);\n\n\tconst result = await relayerState.near\n\t\t.transaction(relayerState.accountId)\n\t\t.signedDelegateAction(userAction)\n\t\t.send({ waitUntil: \"EXECUTED\" });\n\n\treturn { txHash: result.transaction.hash };\n}\n\nasync function defaultValidateLimitedAccessKey(\n\taccountId: string,\n\tpublicKey: string,\n\trecipient: string,\n\tnear: Near,\n): Promise<boolean> {\n\tconst key = await near.getAccessKey(accountId, publicKey);\n\tif (!key) return false;\n\tif (key.permission === \"FullAccess\") return true;\n\tif (\"FunctionCall\" in key.permission) {\n\t\treturn key.permission.FunctionCall.receiver_id === recipient;\n\t}\n\treturn false;\n}\n\nfunction isImplicitAccount(accountId: string): boolean {\n\treturn /^[0-9a-f]{64}$/.test(accountId);\n}\n\nfunction resolveParentAccount(\n\tsubAccountCfg: SubAccountConfig | undefined,\n\tparentKey: string | undefined,\n\trState: RelayerState | null,\n): { parentAccount: string | null; subAccountAvailable: boolean } {\n\tconst parent = subAccountCfg?.parentAccount ?? rState?.accountId;\n\tif (!parent || isImplicitAccount(parent)) {\n\t\treturn { parentAccount: null, subAccountAvailable: false };\n\t}\n\tif (!rState && !parentKey) {\n\t\treturn { parentAccount: null, subAccountAvailable: false };\n\t}\n\treturn { parentAccount: parent, subAccountAvailable: true };\n}\n\nexport interface SIWNPluginOptions {\n\trecipient?: string;\n\trecipients?: DualNetworkConfig<string>;\n\trequireFullAccessKey?: boolean;\n\tgetNonce?: () => Promise<Uint8Array>;\n\tgetProfile?: (accountId: AccountId) => Promise<Profile | null>;\n\tvalidateLimitedAccessKey?: (args: {\n\t\taccountId: AccountId;\n\t\tpublicKey: string;\n\t\trecipient?: string;\n\t}) => Promise<boolean>;\n\tapiKey?: string;\n\trpcUrl?: string | DualNetworkConfig<string>;\n\trelayer?: RelayerConfig | DualNetworkConfig<RelayerConfig>;\n\tsecrets?: {\n\t\tparentKey?: string | DualNetworkConfig<string>;\n\t};\n\tsubAccount?: SubAccountConfig | DualNetworkConfig<SubAccountConfig>;\n}\n\nexport const siwn = (options: SIWNPluginOptions) => {\n\tif (!options.recipient && !options.recipients) {\n\t\tthrow new Error(\"Either 'recipient' or 'recipients' must be provided to siwn plugin\");\n\t}\n\n\tconst apiKey = options.apiKey;\n\n\tconst getRecipient = (network: \"mainnet\" | \"testnet\"): string => {\n\t\tif (options.recipients) return options.recipients[network];\n\t\treturn options.recipient!;\n\t};\n\n\tconst getSupportedNetworks = (): (\"mainnet\" | \"testnet\")[] => {\n\t\tif (options.recipients) return [\"mainnet\", \"testnet\"];\n\t\tif (options.recipient) return [getNetworkFromAccountId(options.recipient)];\n\t\treturn [\"mainnet\"];\n\t};\n\n\tconst getRelayerConfig = (network: \"mainnet\" | \"testnet\"): RelayerConfig | undefined => {\n\t\tif (!options.relayer) return undefined;\n\t\tif (\"mainnet\" in options.relayer && typeof options.relayer.mainnet === \"object\") {\n\t\t\treturn (options.relayer as DualNetworkConfig<RelayerConfig>)[network];\n\t\t}\n\t\treturn options.relayer as RelayerConfig;\n\t};\n\n\tconst getRpcUrl = (network: \"mainnet\" | \"testnet\"): string | undefined => {\n\t\tif (!options.rpcUrl) return undefined;\n\t\tif (typeof options.rpcUrl === \"object\") return (options.rpcUrl as DualNetworkConfig<string>)[network];\n\t\treturn options.rpcUrl as string;\n\t};\n\n\tconst getSubAccountConfig = (network: \"mainnet\" | \"testnet\"): SubAccountConfig | undefined => {\n\t\tif (!options.subAccount) return undefined;\n\t\tif (\"mainnet\" in options.subAccount && typeof options.subAccount.mainnet === \"object\") {\n\t\t\treturn (options.subAccount as DualNetworkConfig<SubAccountConfig>)[network];\n\t\t}\n\t\treturn options.subAccount as SubAccountConfig;\n\t};\n\n\tconst getParentKey = (network: \"mainnet\" | \"testnet\"): string | undefined => {\n\t\tif (!options.secrets?.parentKey) return undefined;\n\t\tif (typeof options.secrets.parentKey === \"object\") {\n\t\t\treturn (options.secrets.parentKey as DualNetworkConfig<string>)[network];\n\t\t}\n\t\treturn options.secrets.parentKey as string;\n\t};\n\n\tconst getRawParentKey = (network: \"mainnet\" | \"testnet\"): string | undefined => {\n\t\tconst fromSecrets = getParentKey(network);\n\t\tif (fromSecrets) return fromSecrets;\n\t\tconst relayerCfg = getRelayerConfig(network);\n\t\tconst subAccountCfg = getSubAccountConfig(network);\n\t\tconst parent = subAccountCfg?.parentAccount ?? relayerCfg?.accountId;\n\t\tif (parent && parent === relayerCfg?.accountId) {\n\t\t\treturn relayerCfg.privateKey ?? relayerCfg.privateKeys?.[0];\n\t\t}\n\t\treturn undefined;\n\t};\n\n\tconst primaryNetwork = getSupportedNetworks()[0];\n\tconst relayerStates = new Map<\"mainnet\" | \"testnet\", RelayerState | null>();\n\tconst relayerInitPromises = new Map<\"mainnet\" | \"testnet\", Promise<RelayerState | null>>();\n\n\tconst headers: Record<string, string> = {};\n\tif (apiKey) {\n\t\theaders[\"Authorization\"] = `Bearer ${apiKey}`;\n\t}\n\n\tconst ensureRelayer = async (adapter: DBAdapter, secret: string | undefined, network: \"mainnet\" | \"testnet\") => {\n\t\tif (relayerStates.has(network)) return relayerStates.get(network) ?? null;\n\n\t\tconst existingInit = relayerInitPromises.get(network);\n\t\tif (existingInit) return existingInit;\n\n\t\tconst relayerCfg = getRelayerConfig(network);\n\t\tconst networkRpcUrl = getRpcUrl(network);\n\n\t\tconst initPromise = initRelayer(relayerCfg, network, adapter, secret, apiKey, networkRpcUrl)\n\t\t\t.then((state) => {\n\t\t\t\trelayerStates.set(network, state);\n\t\t\t\trelayerInitPromises.delete(network);\n\t\t\t\treturn state;\n\t\t\t})\n\t\t\t.catch((error) => {\n\t\t\t\trelayerInitPromises.delete(network);\n\t\t\t\tthrow error;\n\t\t\t});\n\n\t\trelayerInitPromises.set(network, initPromise);\n\t\treturn initPromise;\n\t};\n\n\tconst getNear = (network: \"mainnet\" | \"testnet\") => {\n\t\tconst networkRpcUrl = getRpcUrl(network);\n\t\tif (networkRpcUrl) {\n\t\t\treturn new Near({ network: { rpcUrl: networkRpcUrl, networkId: network }, headers: apiKey ? { Authorization: `Bearer ${apiKey}` } : undefined });\n\t\t}\n\t\treturn new Near({ network, headers: apiKey ? { Authorization: `Bearer ${apiKey}` } : undefined });\n\t};\n\n\treturn ({\n\t\tid: \"siwn\",\n\t\tschema,\n\t\thooks: {\n\t\t\tafter: [\n\t\t\t\t{\n\t\t\t\t\tmatcher: (context: { path?: string; method?: string }) => context.path === \"/auth/session\" && context.method === \"GET\",\n\t\t\t\t\thandler: createAuthMiddleware(async (ctx) => {\n\t\t\t\t\t\tconst session = ctx.context.session;\n\t\t\t\t\t\tif (session) {\n\t\t\t\t\t\t\tconst nearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t\t\t{ field: \"isPrimary\", operator: \"eq\", value: true },\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tif (nearAccount) {\n\t\t\t\t\t\t\t\tctx.context.session = {\n\t\t\t\t\t\t\t\t\t...session,\n\t\t\t\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\t\t\t\t...session.user,\n\t\t\t\t\t\t\t\t\t\tnearAccount: nearAccount\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn { context: ctx };\n\t\t\t\t\t}),\n\t\t\t\t},\n\t\t\t],\n\t\t},\n\t\tendpoints: {\n\t\t\tlinkNearAccount: createAuthEndpoint(\n\t\t\t\t\"/near/link-account\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: LinkAccountRequest,\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t\trequireRequest: true,\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { signedMessage, message, recipient, nonce, accountId, callbackUrl } = ctx.body;\n\t\t\t\t\tconst network = getNetworkFromAccountId(accountId);\n\t\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\t\tif (!session) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"Must be logged in to link NEAR account\",\n\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst near = getNear(network);\n\t\t\t\t\t\tconst nonceBytes = hex.decode(nonce);\n\n\t\t\t\t\t\tconst isValid = await verifyNep413Signature(\n\t\t\t\t\t\tsignedMessage,\n\t\t\t\t\t\t{ message, recipient, nonce: nonceBytes, callbackUrl },\n\t\t\t\t\t\t\t{ near, maxAge: 15 * 60 * 1000 },\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif (!isValid) {\n\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\tmessage: \"Unauthorized: Invalid signature\",\n\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (signedMessage.accountId !== accountId) {\n\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\tmessage: \"Unauthorized: Account ID mismatch\",\n\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst publicKey = signedMessage.publicKey;\n\n\t\t\t\t\t\tif (!options.requireFullAccessKey && options.validateLimitedAccessKey) {\n\t\t\t\t\t\t\tconst isValidKey = await options.validateLimitedAccessKey({\n\t\t\t\t\t\t\t\taccountId: accountId,\n\t\t\t\t\t\t\t\tpublicKey: publicKey,\n\t\t\t\t\t\t\t\trecipient: options.recipient\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tif (!isValidKey) {\n\t\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\t\tmessage: \"Unauthorized: Invalid function call access key\",\n\t\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst existingNearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"eq\", value: accountId },\n\t\t\t\t\t\t\t\t{ field: \"network\", operator: \"eq\", value: network },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tif (existingNearAccount) {\n\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\tmessage: \"This NEAR account is already linked to another user\",\n\t\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst existingPrimaryAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t\t{ field: \"isPrimary\", operator: \"eq\", value: true },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tawait ctx.context.adapter.create({\n\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\tuserId: session.user.id,\n\t\t\t\t\t\t\t\taccountId,\n\t\t\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\t\t\tpublicKey: publicKey,\n\t\t\t\t\t\t\t\tisPrimary: !existingPrimaryAccount,\n\t\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tawait ctx.context.internalAdapter.createAccount({\n\t\t\t\t\t\t\tuserId: session.user.id,\n\t\t\t\t\t\t\tproviderId: \"siwn\",\n\t\t\t\t\t\t\taccountId: `${accountId}:${network}`,\n\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\t\taccountId,\n\t\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\t\tmessage: \"NEAR account successfully linked\"\n\t\t\t\t\t\t});\n\t\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\t\tif (error instanceof APIError) throw error;\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"Something went wrong. Please try again later.\",\n\t\t\t\t\t\t\terror: error instanceof Error ? error.message : \"Unknown error\",\n\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\tunlinkNearAccount: createAuthEndpoint(\n\t\t\t\t\"/near/unlink-account\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: z.object({\n\t\t\t\t\t\taccountId: z.string(),\n\t\t\t\t\t\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n\t\t\t\t\t}),\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { accountId, network: providedNetwork } = ctx.body;\n\t\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\t\tif (!session) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"Must be logged in to unlink NEAR account\",\n\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst network = providedNetwork || getNetworkFromAccountId(accountId);\n\n\t\t\t\t\tconst nearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"eq\", value: accountId },\n\t\t\t\t\t\t\t{ field: \"network\", operator: \"eq\", value: network },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tif (!nearAccount) {\n\t\t\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\t\t\tmessage: \"NEAR account not found or not linked to your user\",\n\t\t\t\t\t\t\tstatus: 404,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst accounts = await ctx.context.adapter.findMany({\n\t\t\t\t\t\tmodel: \"account\",\n\t\t\t\t\t\twhere: [{ field: \"userId\", operator: \"eq\", value: session.user.id }],\n\t\t\t\t\t});\n\n\t\t\t\t\tif (accounts.length <= 1) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"Cannot unlink last authentication method. Link another account first.\",\n\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tif (nearAccount.isPrimary) {\n\t\t\t\t\t\tconst otherNearAccounts: NearAccount[] = await ctx.context.adapter.findMany({\n\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"ne\", value: accountId },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tif (otherNearAccounts.length > 0) {\n\t\t\t\t\t\t\tawait ctx.context.adapter.update({\n\t\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t\t{ field: \"id\", operator: \"eq\", value: otherNearAccounts[0]!.id },\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tupdate: { isPrimary: true },\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tawait ctx.context.adapter.delete({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"eq\", value: accountId },\n\t\t\t\t\t\t\t{ field: \"network\", operator: \"eq\", value: network },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tconst accountToDelete: Account | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\tmodel: \"account\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t{ field: \"providerId\", operator: \"eq\", value: \"siwn\" },\n\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"eq\", value: `${accountId}:${network}` },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tif (accountToDelete) {\n\t\t\t\t\t\tawait ctx.context.internalAdapter.deleteAccount(accountToDelete.id);\n\t\t\t\t\t}\n\n\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\taccountId,\n\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\tmessage: \"NEAR account successfully unlinked\"\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t),\n\t\t\tlistNearAccounts: createAuthEndpoint(\n\t\t\t\t\"/near/list-accounts\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\t\tconst nearAccounts: NearAccount[] = await ctx.context.adapter.findMany({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [{ field: \"userId\", operator: \"eq\", value: session.user.id }],\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json(buildListAccountsResponse(nearAccounts));\n\t\t\t\t},\n\t\t\t),\n\t\t\tsetPrimaryNearAccount: createAuthEndpoint(\n\t\t\t\t\"/near/set-primary-account\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: SetPrimaryAccountRequest,\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { accountId, network: providedNetwork } = ctx.body;\n\t\t\t\t\tconst session = ctx.context.session;\n\t\t\t\t\tconst network = providedNetwork || getNetworkFromAccountId(accountId);\n\n\t\t\t\t\tconst targetAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"eq\", value: accountId },\n\t\t\t\t\t\t\t{ field: \"network\", operator: \"eq\", value: network },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tif (!targetAccount) {\n\t\t\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\t\t\tmessage: \"NEAR account not found or not linked to your user\",\n\t\t\t\t\t\t\tstatus: 404,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst nearAccounts: NearAccount[] = await ctx.context.adapter.findMany({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [{ field: \"userId\", operator: \"eq\", value: session.user.id }],\n\t\t\t\t\t});\n\n\t\t\t\t\tawait Promise.all(nearAccounts.map((account) => ctx.context.adapter.update({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [{ field: \"id\", operator: \"eq\", value: account.id }],\n\t\t\t\t\t\tupdate: { isPrimary: nearAccountKey(account) === nearAccountKey(targetAccount) },\n\t\t\t\t\t})));\n\n\t\t\t\t\tconst updatedNearAccounts: NearAccount[] = await ctx.context.adapter.findMany({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [{ field: \"userId\", operator: \"eq\", value: session.user.id }],\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\taccountId,\n\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\tmessage: \"Primary NEAR account updated\",\n\t\t\t\t\t\t...buildListAccountsResponse(updatedNearAccounts),\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t),\n\t\t\tgetSiwnNonce: createAuthEndpoint(\n\t\t\t\t\"/near/nonce\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: NonceRequest,\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { accountId, networkId } = ctx.body;\n\t\t\t\t\tconst network = getNetworkFromAccountId(accountId);\n\n\t\t\t\t\tif (networkId !== network) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"Network ID mismatch with account ID\",\n\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst near = getNear(network);\n\t\t\t\t\tconst exists = await near.accountExists(accountId);\n\t\t\t\t\tif (!exists) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"Account does not exist on-chain\",\n\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst nonce = options.getNonce ? await options.getNonce() : generateNonce();\n\n\t\t\t\t\tconst nonceString = hex.encode(nonce);\n\n\t\t\t\t\tawait ctx.context.internalAdapter.createVerificationValue({\n\t\t\t\t\t\tidentifier: `siwn:${accountId}:${network}`,\n\t\t\t\t\t\tvalue: nonceString,\n\t\t\t\t\t\texpiresAt: new Date(Date.now() + 15 * 60 * 1000),\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json(NonceResponse.parse({ nonce: nonceString }));\n\t\t\t\t},\n\t\t\t),\n\t\t\tgetSiwnProfile: createAuthEndpoint(\n\t\t\t\t\"/near/profile\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: ProfileRequest,\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { accountId } = ctx.body;\n\t\t\t\t\tlet targetAccountId = accountId;\n\n\t\t\t\t\tif (!targetAccountId) {\n\t\t\t\t\t\tconst session = ctx.context.session;\n\t\t\t\t\t\tif (!session) {\n\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\tmessage: \"Session required when no accountId provided\",\n\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst nearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t\t{ field: \"isPrimary\", operator: \"eq\", value: true },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tif (!nearAccount) {\n\t\t\t\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\t\t\t\tmessage: \"No NEAR account found for user\",\n\t\t\t\t\t\t\t\tstatus: 404,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\ttargetAccountId = nearAccount.accountId;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst profile = await (options.getProfile || ((id: AccountId) => defaultGetProfile(id, apiKey)))(targetAccountId);\n\t\t\t\t\treturn ctx.json(ProfileResponse.parse(profile));\n\t\t\t\t},\n\t\t\t),\n\t\t\tverifySiwnMessage: createAuthEndpoint(\n\t\t\t\t\"/near/verify\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: VerifyRequest,\n\t\t\t\t\trequireRequest: true,\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst {\n\t\t\t\t\t\tsignedMessage,\n\t\t\t\t\t\tmessage,\n\t\t\t\t\t\trecipient,\n\t\t\t\t\t\tnonce,\n\t\t\t\t\t\taccountId,\n\t\t\t\t\t\tcallbackUrl,\n\t\t\t\t\t} = ctx.body;\n\t\t\t\t\tconst network = getNetworkFromAccountId(accountId);\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst near = getNear(network);\n\t\t\t\t\t\tconst nonceBytes = hex.decode(nonce);\n\n\t\t\t\t\t\tconst isValid = await verifyNep413Signature(\n\t\t\t\t\t\tsignedMessage,\n\t\t\t\t\t\t{ message, recipient, nonce: nonceBytes, callbackUrl },\n\t\t\t\t\t\t\t{ near, maxAge: 15 * 60 * 1000 },\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif (!isValid) {\n\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\tmessage: \"Unauthorized: Invalid signature\",\n\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (signedMessage.accountId !== accountId) {\n\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\tmessage: \"Unauthorized: Account ID mismatch\",\n\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst publicKey = signedMessage.publicKey;\n\n\t\t\t\t\t\tconst nonceHash = await hashNonce(nonceBytes);\n\n\t\t\t\t\t\tconst existingNonce = await ctx.context.internalAdapter.findVerificationValue(\n\t\t\t\t\t\t\t`siwn-nonce:${nonceHash}`\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif (existingNonce) {\n\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\tmessage: \"Unauthorized: Nonce already used (replay attack detected)\",\n\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t\tcode: \"UNAUTHORIZED_NONCE_REPLAY\",\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tawait ctx.context.internalAdapter.createVerificationValue({\n\t\t\t\t\t\t\tidentifier: `siwn-nonce:${nonceHash}`,\n\t\t\t\t\t\t\tvalue: \"used\",\n\t\t\t\t\t\t\texpiresAt: new Date(Date.now() + 15 * 60 * 1000),\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tif (!options.requireFullAccessKey) {\n\t\t\t\t\t\t\tconst validateKey = options.validateLimitedAccessKey\n\t\t\t\t\t\t\t\t|| ((args: { accountId: string; publicKey: string; recipient?: string }) =>\n\t\t\t\t\t\t\t\t\tdefaultValidateLimitedAccessKey(args.accountId, args.publicKey, args.recipient || getRecipient(network), near));\n\n\t\t\t\t\t\t\tconst isValidKey = await validateKey({\n\t\t\t\t\t\t\t\taccountId: accountId,\n\t\t\t\t\t\t\t\tpublicKey: publicKey,\n\t\t\t\t\t\t\t\trecipient: options.recipient\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tif (!isValidKey) {\n\t\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\t\tmessage: \"Unauthorized: Invalid function call access key\",\n\t\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tlet user: User | null = null;\n\n\t\t\t\t\t\tconst existingNearAccount: NearAccount | null =\n\t\t\t\t\t\t\tawait ctx.context.adapter.findOne({\n\t\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"eq\", value: accountId },\n\t\t\t\t\t\t\t\t\t{ field: \"network\", operator: \"eq\", value: network },\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\tif (existingNearAccount) {\n\t\t\t\t\t\t\tuser = await ctx.context.adapter.findOne({\n\t\t\t\t\t\t\t\tmodel: \"user\",\n\t\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tfield: \"id\",\n\t\t\t\t\t\t\t\t\t\toperator: \"eq\",\n\t\t\t\t\t\t\t\t\t\tvalue: existingNearAccount.userId,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tconst anyNearAccount: NearAccount | null =\n\t\t\t\t\t\t\t\tawait ctx.context.adapter.findOne({\n\t\t\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"eq\", value: accountId },\n\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tif (anyNearAccount) {\n\t\t\t\t\t\t\t\tuser = await ctx.context.adapter.findOne({\n\t\t\t\t\t\t\t\t\tmodel: \"user\",\n\t\t\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tfield: \"id\",\n\t\t\t\t\t\t\t\t\t\t\toperator: \"eq\",\n\t\t\t\t\t\t\t\t\t\t\tvalue: anyNearAccount.userId,\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (!user) {\n\t\t\t\t\t\t\tconst userEmail = deriveEmail(accountId, getRecipient(network));\n\n\t\t\t\t\t\t\tconst profile = await (options.getProfile || ((id: AccountId) => defaultGetProfile(id, apiKey)))(accountId);\n\n\t\t\t\t\t\t\tuser = await ctx.context.internalAdapter.createUser({\n\t\t\t\t\t\t\t\tname: profile?.name ?? accountId,\n\t\t\t\t\t\t\t\temail: userEmail,\n\t\t\t\t\t\t\t\timage: profile?.image ? getImageUrl(profile.image) : \"\",\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tawait ctx.context.adapter.create({\n\t\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\t\tuserId: user.id,\n\t\t\t\t\t\t\t\t\taccountId,\n\t\t\t\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\t\t\t\tpublicKey: publicKey,\n\t\t\t\t\t\t\t\t\tisPrimary: true,\n\t\t\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tawait ctx.context.internalAdapter.createAccount({\n\t\t\t\t\t\t\t\tuserId: user.id,\n\t\t\t\t\t\t\t\tproviderId: \"siwn\",\n\t\t\t\t\t\t\t\taccountId: `${accountId}:${network}`,\n\t\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tif (!existingNearAccount) {\n\t\t\t\t\t\t\t\tawait ctx.context.adapter.create({\n\t\t\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\t\t\tuserId: user.id,\n\t\t\t\t\t\t\t\t\t\taccountId,\n\t\t\t\t\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\t\t\t\t\tpublicKey: publicKey,\n\t\t\t\t\t\t\t\t\t\tisPrimary: false,\n\t\t\t\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\tawait ctx.context.internalAdapter.createAccount({\n\t\t\t\t\t\t\t\t\tuserId: user.id,\n\t\t\t\t\t\t\t\t\tproviderId: \"siwn\",\n\t\t\t\t\t\t\t\t\taccountId: `${accountId}:${network}`,\n\t\t\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tawait ensureRelayer(ctx.context.adapter, ctx.context.secret, network);\n\n\t\t\t\t\t\tconst session = await ctx.context.internalAdapter.createSession(\n\t\t\t\t\t\t\tuser.id\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif (!session) {\n\t\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\t\tmessage: \"Internal Server Error\",\n\t\t\t\t\t\t\t\tstatus: 500,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tawait setSessionCookie(ctx, { session, user });\n\n\t\t\t\t\t\treturn ctx.json(VerifyResponse.parse({\n\t\t\t\t\t\t\ttoken: session.token,\n\t\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\t\tid: user.id,\n\t\t\t\t\t\t\t\taccountId,\n\t\t\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t}));\n\t\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\t\tif (error instanceof APIError) throw error;\n\t\t\t\t\t\tconst msg = error instanceof Error ? error.message : \"Unknown error\";\n\t\t\t\t\t\tconst at = error instanceof Error && error.stack ? error.stack.split(\"\\n\").slice(1, 3).map(s => s.trim()).join(\" <- \") : \"\";\n\t\t\t\t\t\tconsole.error(`[siwn] Verify error: ${msg}${at ? ` (${at})` : \"\"}`);\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"Something went wrong. Please try again later.\",\n\t\t\t\t\t\t\terror: msg,\n\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\trelayNearTransaction: createAuthEndpoint(\n\t\t\t\t\"/near/relay\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: RelayRequest,\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { payload } = ctx.body;\n\t\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\t\tif (!session) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"Must be authenticated to relay transactions\",\n\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst nearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t{ field: \"isPrimary\", operator: \"eq\", value: true },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tif (!nearAccount) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"No NEAR account linked to session\",\n\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst network = nearAccount.network as \"mainnet\" | \"testnet\";\n\t\t\t\t\tconst rState = await ensureRelayer(ctx.context.adapter, ctx.context.secret, network);\n\n\t\t\t\t\tif (!rState) {\n\t\t\t\t\t\tthrow new APIError(\"SERVICE_UNAVAILABLE\", {\n\t\t\t\t\t\t\tmessage: \"Relayer not configured\",\n\t\t\t\t\t\t\tstatus: 503,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst decoded: SignedDelegateAction = decodeSignedDelegateAction(payload);\n\t\t\t\t\t\tconst delegateAction = decoded.signedDelegate.delegateAction;\n\n\t\t\t\t\t\tif (delegateAction.senderId !== nearAccount.accountId) {\n\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\tmessage: \"Delegate action sender does not match session account\",\n\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst relayerConfigForNetwork = getRelayerConfig(network);\n\t\t\t\t\t\tif (relayerConfigForNetwork?.whitelistedContracts?.length) {\n\t\t\t\t\t\t\tif (!relayerConfigForNetwork.whitelistedContracts.includes(delegateAction.receiverId)) {\n\t\t\t\t\t\t\t\tthrow new APIError(\"FORBIDDEN\", {\n\t\t\t\t\t\t\t\t\tmessage: `Contract ${delegateAction.receiverId} is not whitelisted for relay`,\n\t\t\t\t\t\t\t\t\tstatus: 403,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (relayerConfigForNetwork?.maxGasPerTransaction) {\n\t\t\t\t\t\t\tconst totalGas = delegateAction.actions.reduce((sum: bigint, a) => {\n\t\t\t\t\t\t\t\treturn sum + (\"functionCall\" in a ? a.functionCall.gas : 0n);\n\t\t\t\t\t\t\t}, 0n);\n\t\t\t\t\t\t\tif (totalGas > BigInt(relayerConfigForNetwork.maxGasPerTransaction)) {\n\t\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\t\tmessage: `Transaction gas (${totalGas}) exceeds relayer limit (${relayerConfigForNetwork.maxGasPerTransaction})`,\n\t\t\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (relayerConfigForNetwork?.maxDepositPerTransaction) {\n\t\t\t\t\t\t\tconst totalDeposit = delegateAction.actions.reduce((sum: bigint, a) => {\n\t\t\t\t\t\t\t\tif (\"functionCall\" in a) return sum + a.functionCall.deposit;\n\t\t\t\t\t\t\t\tif (\"transfer\" in a) return sum + a.transfer.deposit;\n\t\t\t\t\t\t\t\treturn sum;\n\t\t\t\t\t\t\t}, 0n);\n\t\t\t\t\t\t\tif (totalDeposit > BigInt(relayerConfigForNetwork.maxDepositPerTransaction)) {\n\t\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\t\tmessage: `Transaction deposit (${totalDeposit}) exceeds relayer limit (${relayerConfigForNetwork.maxDepositPerTransaction})`,\n\t\t\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst result = await relayOnChain(payload, rState);\n\n\t\t\t\t\t\tawait ctx.context.adapter.create({\n\t\t\t\t\t\t\tmodel: \"relayedTransaction\",\n\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\tuserId: session.user.id,\n\t\t\t\t\t\t\t\ttxHash: result.txHash,\n\t\t\t\t\t\t\t\tsenderId: delegateAction.senderId,\n\t\t\t\t\t\t\t\treceiverId: delegateAction.receiverId,\n\t\t\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\t\t\tstatus: \"pending\",\n\t\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\n\t\t\t\t\tif (rState.mode === \"ephemeral\") {\n\t\t\t\t\t\tawait ctx.context.adapter.update({\n\t\t\t\t\t\t\tmodel: \"relayerKey\",\n\t\t\t\t\t\t\twhere: [{ field: \"network\", operator: \"eq\", value: network }],\n\t\t\t\t\t\t\tupdate: { lastUsedAt: new Date() },\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\t\treturn ctx.json(RelayResponse.parse({\n\t\t\t\t\t\t\ttxHash: result.txHash,\n\t\t\t\t\t\t\tstatus: \"pending\",\n\t\t\t\t\t\t}));\n\t\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\t\tif (error instanceof APIError) throw error;\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: error instanceof Error ? error.message : \"Relay failed\",\n\t\t\t\t\t\t\tstatus: 500,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\tgetRelayStatus: createAuthEndpoint(\n\t\t\t\t\"/near/relay-status/:txHash\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst txHash = (ctx.params as Record<string, string>)?.txHash;\n\t\t\t\t\tif (!txHash) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"Transaction hash required\",\n\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\t\tconst relayedTx = await ctx.context.adapter.findOne<RelayedTransactionRecord>({\n\t\t\t\t\t\tmodel: \"relayedTransaction\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"txHash\", operator: \"eq\", value: txHash },\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tif (!relayedTx) {\n\t\t\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\t\t\tmessage: \"Transaction not found or not owned by this user\",\n\t\t\t\t\t\t\tstatus: 404,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst network = relayedTx.network;\n\t\t\t\t\tconst senderId = relayedTx.senderId;\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst near = getNear(network);\n\t\t\t\t\t\tconst txResult = await near.getTransactionStatus(txHash, senderId);\n\n\t\t\t\t\t\tconst txStatus = txResult.status;\n\t\t\t\t\t\tif (txStatus && typeof txStatus === \"object\") {\n\t\t\t\t\t\t\tconst hasSuccess = \"SuccessValue\" in txStatus || \"SuccessReceiptId\" in txStatus;\n\t\t\t\t\t\t\tconst hasFailure = \"Failure\" in txStatus;\n\t\t\t\t\t\t\tconst status = hasSuccess ? \"completed\" : hasFailure ? \"failed\" : \"pending\";\n\n\t\t\t\t\t\t\tif (status !== \"pending\") {\n\t\t\t\t\t\t\t\tawait ctx.context.adapter.update({\n\t\t\t\t\t\t\t\t\tmodel: \"relayedTransaction\",\n\t\t\t\t\t\t\t\t\twhere: [{ field: \"txHash\", operator: \"eq\", value: txHash }],\n\t\t\t\t\t\t\t\t\tupdate: { status },\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst gasUsed = txResult.transaction_outcome?.outcome?.gas_burnt?.toString();\n\n\t\t\t\t\t\t\treturn ctx.json(RelayStatusResponse.parse({\n\t\t\t\t\t\t\t\tstatus,\n\t\t\t\t\t\t\t\tgasUsed,\n\t\t\t\t\t\t\t\toutcome: txResult,\n\t\t\t\t\t\t\t}));\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn ctx.json(RelayStatusResponse.parse({ status: \"pending\" }));\n\t\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\t\treturn ctx.json(RelayStatusResponse.parse({ status: \"pending\" }));\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\tgetRelayerInfo: createAuthEndpoint(\n\t\t\t\t\"/near/relayer-info\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: GetRelayerInfoRequest,\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { network: requestedNetwork } = ctx.body;\n\t\t\t\t\tconst targetNetwork = (requestedNetwork ?? primaryNetwork) as \"mainnet\" | \"testnet\";\n\t\t\t\t\tconst rState = await ensureRelayer(ctx.context.adapter, ctx.context.secret, targetNetwork);\n\n\t\t\t\t\tif (!rState) {\n\t\t\t\t\t\treturn ctx.json({ enabled: false, subAccountAvailable: false } satisfies Partial<RelayerInfo> & { enabled: boolean });\n\t\t\t\t\t}\n\n\t\t\t\t\tconst subAccountCfg = getSubAccountConfig(targetNetwork);\n\t\t\t\t\tconst parentKey = getParentKey(targetNetwork);\nconst { parentAccount, subAccountAvailable } = resolveParentAccount(subAccountCfg, parentKey, rState);\n\n\t\t\t\t\tconst near = getNear(rState.network);\n\t\t\t\t\tlet account;\n\t\t\t\t\ttry {\n\t\t\t\t\t\taccount = await near.getAccount(rState.accountId);\n\t\t\t\t\t} catch {\n\t\t\t\t\t\taccount = {\n\t\t\t\t\t\t\tbalance: \"0\",\n\t\t\t\t\t\t\tavailable: \"0\",\n\t\t\t\t\t\t\tstaked: \"0\",\n\t\t\t\t\t\t\tstorageUsage: \"0\",\n\t\t\t\t\t\t\tstorageBytes: 0,\n\t\t\t\t\t\t\thasContract: false,\n\t\t\t\t\t\t\tcodeHash: \"\",\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\tenabled: true,\n\t\t\t\t\t\taccountId: rState.accountId,\n\t\t\t\t\t\tmode: rState.mode,\n\t\t\t\t\t\tnetwork: rState.network,\n\t\t\t\t\t\tpublicKey: rState.publicKey,\n\t\t\t\t\t\tbalance: account.balance,\n\t\t\t\t\t\tavailable: account.available,\n\t\t\t\t\t\tstaked: account.staked,\n\t\t\t\t\t\tstorageUsage: account.storageUsage,\n\t\t\t\t\t\tstorageBytes: account.storageBytes,\n\t\t\t\t\t\thasContract: account.hasContract,\n\t\t\t\t\t\thasKey: true,\n\t\t\t\t\t\tcreatedAt: rState.createdAt,\n\t\t\t\t\t\tlastUsedAt: rState.lastUsedAt,\n\t\t\t\t\t\tparentAccount: parentAccount ?? undefined,\n\t\t\t\t\t\tsubAccountAvailable,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t),\n\t\t\tgetRelayHistory: createAuthEndpoint(\n\t\t\t\t\"/near/relay-history\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\t\tlet transactions: RelayedTransactionRecord[] = [];\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst result = await ctx.context.adapter.findMany<RelayedTransactionRecord>({\n\t\t\t\t\t\t\tmodel: \"relayedTransaction\",\n\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t});\n\t\t\t\t\t\ttransactions = result || [];\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\tconsole.error(\"relay-history findMany error:\", err);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst sorted = transactions.sort((a, b) => {\n\t\t\t\t\t\tconst aTime = a.createdAt instanceof Date ? a.createdAt.getTime() : new Date(a.createdAt ?? 0).getTime();\n\t\t\t\t\t\tconst bTime = b.createdAt instanceof Date ? b.createdAt.getTime() : new Date(b.createdAt ?? 0).getTime();\n\t\t\t\t\t\treturn bTime - aTime;\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\ttransactions: sorted.map((tx) => ({\n\t\t\t\t\t\t\tid: String(tx.id ?? \"\"),\n\t\t\t\t\t\t\tuserId: String(tx.userId ?? \"\"),\n\t\t\t\t\t\t\ttxHash: String(tx.txHash ?? \"\"),\n\t\t\t\t\t\t\tsenderId: String(tx.senderId ?? \"\"),\n\t\t\t\t\t\t\treceiverId: String(tx.receiverId ?? \"\"),\n\t\t\t\t\t\t\tnetwork: String(tx.network ?? \"mainnet\"),\n\t\t\t\t\t\t\tstatus: String(tx.status ?? \"pending\"),\n\t\t\t\t\t\t\tgasUsed: tx.gasUsed ? String(tx.gasUsed) : undefined,\n\t\t\t\t\t\t\tcreatedAt: tx.createdAt instanceof Date ? tx.createdAt.toISOString() : tx.createdAt ? String(tx.createdAt) : new Date().toISOString(),\n\t\t\t\t\t\t\tupdatedAt: tx.updatedAt instanceof Date ? tx.updatedAt.toISOString() : tx.updatedAt ? String(tx.updatedAt) : undefined,\n\t\t\t\t\t\t})),\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t),\n\t\t\tviewContract: createAuthEndpoint(\n\t\t\t\t\"/near/view\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: ViewContractRequest,\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { contractId, methodName, args } = ctx.body;\n\t\t\t\t\tconst session = ctx.context.session;\n\t\t\t\t\tconst nearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t{ field: \"isPrimary\", operator: \"eq\", value: true },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tconst network = (nearAccount?.network || primaryNetwork) as \"mainnet\" | \"testnet\";\n\t\t\t\t\tconst near = getNear(network);\n\t\t\t\t\tlet result: unknown;\n\t\t\t\t\ttry {\n\t\t\t\t\t\tresult = await near.view(contractId, methodName, args ?? {});\n\t\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\t\tif (error instanceof APIError) throw error;\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: error instanceof Error ? error.message : \"Unknown error\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\treturn ctx.json(ViewContractResponse.parse({ result }));\n\t\t\t\t},\n\t\t\t),\n\t\t\tcreateSubAccount: createAuthEndpoint(\n\t\t\t\t\"/near/create-sub-account\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: CreateSubAccountRequest,\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { subAccountName, network: providedNetwork, publicKey } = ctx.body;\n\t\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\t\tif (!session) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"Must be authenticated to create a sub-account\",\n\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst nearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t{ field: \"isPrimary\", operator: \"eq\", value: true },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tconst network = (providedNetwork || nearAccount?.network || primaryNetwork) as \"mainnet\" | \"testnet\";\n\t\t\t\t\tconst subAccountCfg = getSubAccountConfig(network);\n\t\t\t\t\tconst userAccountId = nearAccount?.accountId ?? \"\";\n\t\t\t\t\tconst parentKey = getParentKey(network);\n\t\t\t\t\tconst rState = await ensureRelayer(ctx.context.adapter, ctx.context.secret, network);\n\t\t\t\t\tconst { parentAccount, subAccountAvailable } = resolveParentAccount(subAccountCfg, parentKey, rState);\n\n\t\t\t\t\tif (!subAccountAvailable || !parentAccount) {\n\t\t\t\t\t\tthrow new APIError(\"SERVICE_UNAVAILABLE\", {\n\t\t\t\t\t\t\tmessage: \"Sub-account creation requires a named parent account. Configure subAccount.parentAccount in your SIWN plugin options, or use an explicit relayer with a named account.\",\n\t\t\t\t\t\t\tstatus: 503,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst effectiveParentKey = getRawParentKey(network);\n\t\t\t\t\tif (parentAccount !== rState?.accountId && !effectiveParentKey) {\n\t\t\t\t\t\tthrow new APIError(\"SERVICE_UNAVAILABLE\", {\n\t\t\t\t\t\t\tmessage: \"Sub-account parent differs from relayer account. Provide secrets.parentKey to sign as the parent account.\",\n\t\t\t\t\t\t\tstatus: 503,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst newAccountId = `${subAccountName}.${parentAccount}`;\n\t\t\t\t\tconst minDeposit = subAccountCfg?.minDeposit || \"0.1 NEAR\";\n\n\t\t\t\t\tconst near = getNear(network);\n\t\t\t\t\tconst exists = await near.accountExists(newAccountId);\n\t\t\t\t\tif (exists) {\n\t\t\t\t\t\tthrow new APIError(\"CONFLICT\", {\n\t\t\t\t\t\t\tmessage: `Account ${newAccountId} already exists on ${network}`,\n\t\t\t\t\t\t\tstatus: 409,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst txCtx: SubAccountTxCtx = {\n\t\t\t\t\t\tnewAccountId,\n\t\t\t\t\t\tparentAccount,\n\t\t\t\t\t\tuserPublicKey: publicKey,\n\t\t\t\t\t\tuserAccountId,\n\t\t\t\t\t\tuserId: session.user.id,\n\t\t\t\t\t\tnetwork,\n\t\t\t\t\t};\n\n\t\t\t\t\tconst signingNear = rState?.near ?? near;\n\t\t\t\t\tlet txBuilder = signingNear\n\t\t\t\t\t\t.transaction(parentAccount)\n\t\t\t\t\t\t.createAccount(newAccountId)\n\t\t\t\t\t\t.addKey(publicKey, { type: \"fullAccess\" });\n\n\t\t\t\t\tif (subAccountCfg?.parentHasFullAccess) {\n\t\t\t\t\t\tconst parentPublicKey = parentAccount === rState?.accountId\n\t\t\t\t\t\t\t? rState.publicKey\n\t\t\t\t\t\t\t: parseKey(effectiveParentKey!).publicKey.toString();\n\t\t\t\t\t\ttxBuilder = txBuilder.addKey(parentPublicKey, { type: \"fullAccess\" });\n\t\t\t\t\t}\n\n\t\t\t\t\tif (subAccountCfg?.addRelayerFCAK !== false && subAccountCfg?.relayerFCAK && rState) {\n\t\t\t\t\t\tconst fcakPermission: { type: \"functionCall\"; receiverId: string; methodNames?: string[]; allowance?: string } = {\n\t\t\t\t\t\t\ttype: \"functionCall\",\n\t\t\t\t\t\t\treceiverId: subAccountCfg.relayerFCAK.receiverId,\n\t\t\t\t\t\t};\n\t\t\t\t\t\tif (subAccountCfg.relayerFCAK.methodNames) {\n\t\t\t\t\t\t\tfcakPermission.methodNames = subAccountCfg.relayerFCAK.methodNames;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (subAccountCfg.relayerFCAK.allowance) {\n\t\t\t\t\t\t\tfcakPermission.allowance = subAccountCfg.relayerFCAK.allowance as `${number} NEAR` | `${bigint} yocto`;\n\t\t\t\t\t\t}\n\t\t\t\t\t\ttxBuilder.addKey(rState.publicKey, fcakPermission as any);\n\t\t\t\t\t}\n\n\t\t\t\t\ttxBuilder.transfer(newAccountId, minDeposit as `${number} NEAR`);\n\n\t\t\t\t\tif (subAccountCfg?.deploy) {\n\t\t\t\t\t\tif (\"wasm\" in subAccountCfg.deploy) {\n\t\t\t\t\t\t\ttxBuilder = txBuilder.deployContract(newAccountId, subAccountCfg.deploy.wasm);\n\t\t\t\t\t\t} else if (\"fromPublished\" in subAccountCfg.deploy) {\n\t\t\t\t\t\t\ttxBuilder = txBuilder.deployFromPublished(subAccountCfg.deploy.fromPublished as any);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (subAccountCfg?.init) {\n\t\t\t\t\t\tconst initArgs = typeof subAccountCfg.init.args === \"function\"\n\t\t\t\t\t\t\t? subAccountCfg.init.args(txCtx)\n\t\t\t\t\t\t\t: subAccountCfg.init.args;\n\t\t\t\t\t\ttxBuilder = txBuilder.functionCall(newAccountId, subAccountCfg.init.methodName, initArgs);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (subAccountCfg?.extendTx) {\n\t\t\t\t\t\ttxBuilder = subAccountCfg.extendTx(txBuilder, txCtx) as typeof txBuilder;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (effectiveParentKey) {\n\t\t\t\t\t\ttxBuilder = txBuilder.signWith(effectiveParentKey as PrivateKey);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst cleanupDb = async () => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst existing = await ctx.context.adapter.findOne<{ id: string }>({\n\t\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\t\twhere: [{ field: \"accountId\", operator: \"eq\", value: newAccountId }],\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tif (existing) {\n\t\t\t\t\t\t\t\tawait ctx.context.adapter.delete({ model: \"nearAccount\", where: [{ field: \"id\", operator: \"eq\", value: existing.id }] });\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} catch { }\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tawait ctx.context.internalAdapter.deleteAccount(`${newAccountId}:${network}`);\n\t\t\t\t\t\t} catch { }\n\t\t\t\t\t};\n\n\t\t\t\t\tconst lifecycleCtx: SubAccountLifecycleCtx = { ...txCtx, near: signingNear };\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait txBuilder.send({ waitUntil: \"EXECUTED\" });\n\t\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\t\tconst msg = error instanceof Error ? error.message : \"Unknown error\";\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: `Failed to create sub-account: ${msg}`,\n\t\t\t\t\t\t\tstatus: 500,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait ctx.context.adapter.create({\n\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\tuserId: session.user.id,\n\t\t\t\t\t\t\t\taccountId: newAccountId,\n\t\t\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\t\t\tpublicKey,\n\t\t\t\t\t\t\t\tisPrimary: false,\n\t\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tawait ctx.context.internalAdapter.createAccount({\n\t\t\t\t\t\t\tuserId: session.user.id,\n\t\t\t\t\t\t\tproviderId: \"siwn\",\n\t\t\t\t\t\t\taccountId: `${newAccountId}:${network}`,\n\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tif (subAccountCfg?.onCreated) {\n\t\t\t\t\t\t\tawait subAccountCfg.onCreated(lifecycleCtx);\n\t\t\t\t\t\t}\n\t\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\t\tawait cleanupDb();\n\t\t\t\t\t\tif (subAccountCfg?.onRollback) {\n\t\t\t\t\t\t\ttry { await subAccountCfg.onRollback(lifecycleCtx); } catch { }\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tconst key = getRawParentKey(network);\n\t\t\t\t\t\t\t\tif (key) {\n\t\t\t\t\t\t\t\t\tconst rollbackNear = getNear(network);\n\t\t\t\t\t\t\t\t\tawait rollbackNear\n\t\t\t\t\t\t\t\t\t\t.transaction(newAccountId)\n\t\t\t\t\t\t\t\t\t\t.deleteAccount({ beneficiary: parentAccount })\n\t\t\t\t\t\t\t\t\t\t.signWith(key as PrivateKey)\n\t\t\t\t\t\t\t\t\t\t.send({ waitUntil: \"EXECUTED\" });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} catch { }\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst msg = error instanceof Error ? error.message : \"Unknown error\";\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: `Sub-account created but post-creation failed: ${msg}`,\n\t\t\t\t\t\t\tstatus: 500,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn ctx.json(CreateSubAccountResponse.parse({\n\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\taccountId: newAccountId,\n\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\tpublicKey,\n\t\t\t\t\t\tmessage: `Sub-account ${newAccountId} created on ${network}`,\n\t\t\t\t\t}));\n\t\t\t\t},\n\t\t\t),\n\t\tcheckSubAccountAvailability: createAuthEndpoint(\n\t\t\t\"/near/check-sub-account-availability\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tbody: CheckSubAccountAvailabilityRequest,\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tconst { subAccountName, network: providedNetwork } = ctx.body;\n\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\tif (!session) {\n\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\tmessage: \"Must be authenticated to check sub-account availability\",\n\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst nearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\twhere: [\n\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t{ field: \"isPrimary\", operator: \"eq\", value: true },\n\t\t\t\t\t],\n\t\t\t\t});\n\n\t\t\t\tif (!nearAccount) {\n\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\tmessage: \"Must have a linked NEAR wallet to check sub-account availability\",\n\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst network = (providedNetwork || nearAccount.network || primaryNetwork) as \"mainnet\" | \"testnet\";\n\t\t\t\tconst subAccountCfg = getSubAccountConfig(network);\n\t\t\t\tconst parentAccount = subAccountCfg?.parentAccount ?? null;\n\n\t\t\t\tif (!parentAccount || isImplicitAccount(parentAccount)) {\n\t\t\t\t\treturn ctx.json(CheckSubAccountAvailabilityResponse.parse({\n\t\t\t\t\t\tavailable: false,\n\t\t\t\t\t\taccountId: \"\",\n\t\t\t\t\t\treason: \"not-configured\",\n\t\t\t\t\t}));\n\t\t\t\t}\n\n\t\t\t\tconst accountId = `${subAccountName}.${parentAccount}`;\n\n\t\t\t\tif (accountId.length > 64) {\n\t\t\t\t\treturn ctx.json(CheckSubAccountAvailabilityResponse.parse({\n\t\t\t\t\t\tavailable: false,\n\t\t\t\t\t\taccountId,\n\t\t\t\t\t\tparentAccount,\n\t\t\t\t\t\treason: \"too-long\",\n\t\t\t\t\t}));\n\t\t\t\t}\n\n\t\t\t\tconst near = getNear(network);\n\t\t\t\tconst exists = await near.accountExists(accountId);\n\n\t\t\t\treturn ctx.json(CheckSubAccountAvailabilityResponse.parse({\n\t\t\t\t\tavailable: !exists,\n\t\t\t\t\taccountId,\n\t\t\t\t\tparentAccount,\n\t\t\t\t\treason: exists ? \"taken\" : undefined,\n\t\t\t\t}));\n\t\t\t},\n\t\t),\n\t\t},\n\t});\n};\n"],"mappings":";;;;;;;AAEA,MAAM,eACL;AAED,SAAS,wBAAwB,WAA0C;CAC1E,OAAO,UAAU,SAAS,UAAU,IAAI,YAAY;AACrD;AAEA,SAAS,YACR,OACA,UACS;CACT,IAAI,OAAO,KAAK,OAAO,MAAM;CAC7B,IAAI,OAAO,UAAU,OAAO,iCAAiC,MAAM;CACnE,OAAO,YAAY;AACpB;AAmBA,eAAe,kBAAkB,WAAsB,QAA0C;CAChG,MAAM,UAAU,wBAAwB,SAAS;CAEjD,IAAI;EACH,MAAM,QAAQ,YAAY,YACvB,iCACA;EAEH,MAAM,kBAAkB,UAAU,QAAQ,IAAI;EAC9C,MAAM,UAAkC,EAAE,gBAAgB,mBAAmB;EAC7E,IAAI,iBACH,QAAQ,mBAAmB,UAAU;EAGtC,MAAM,WAAW,MAAM,MACtB,GAAG,MAAM,yBAAyB,UAAU,cAC5C,EAAE,QAAQ,CACX;EAEA,IAAI,SA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KAAK,oCAAoC,MAAM;KACzD,WAAW;KACX;KACA;KACA,QAAQ;IACT,CAAC,CAAC;IAIH,MAAM,SAAS,MADF,QAAQ,OACG,EAAE,cAAc,SAAS;IAEjD,OAAO,IAAI,KAAK,oCAAoC,MAAM;KACzD,WAAW,CAAC;KACZ;KACA;KACA,QAAQ,SAAS,UAAU,KAAA;IAC5B,CAAC,CAAC;GACH,CACD;EACA;CACD;AACD"}
1
+ {"version":3,"file":"index.js","names":["z"],"sources":["../src/profile.ts","../src/schema.ts","../src/types.ts","../src/utils.ts","../src/index.ts"],"sourcesContent":["import type { AccountId, Profile, SocialImage } from \"./types.js\";\n\nconst FALLBACK_URL =\n\t\"https://ipfs.near.social/ipfs/bafkreidn5fb2oygegqaldx7ycdmhu4owcrmoxd7ekbzfmeakkobz2ja7qy\";\n\nfunction getNetworkFromAccountId(accountId: string): \"mainnet\" | \"testnet\" {\n\treturn accountId.endsWith('.testnet') ? 'testnet' : 'mainnet';\n}\n\nfunction getImageUrl(\n\timage: SocialImage | undefined,\n\tfallback?: string,\n): string {\n\tif (image?.url) return image.url;\n\tif (image?.ipfs_cid) return `https://ipfs.near.social/ipfs/${image.ipfs_cid}`;\n\treturn fallback || FALLBACK_URL;\n}\n\ninterface SocialApiResponse {\n\t[accountId: string]: {\n\t\tprofile?: Profile;\n\t};\n}\n\ninterface KvEntry {\n\tvalue: Record<string, unknown> | string;\n\tblock_height?: number;\n\tblock_hash?: string;\n\ttimestamp_nanosec?: string;\n}\n\ninterface KvResponse {\n\tentries: KvEntry[];\n}\n\nasync function defaultGetProfile(accountId: AccountId, apiKey?: string): Promise<Profile | null> {\n\tconst network = getNetworkFromAccountId(accountId);\n\n\ttry {\n\t\tconst kvUrl = network === \"testnet\"\n\t\t\t? \"https://kv.test.fastnear.com\"\n\t\t\t: \"https://kv.main.fastnear.com\";\n\n\t\tconst effectiveApiKey = apiKey || process.env.FASTNEAR_API_KEY;\n\t\tconst headers: Record<string, string> = { \"Content-Type\": \"application/json\" };\n\t\tif (effectiveApiKey) {\n\t\t\theaders[\"Authorization\"] = `Bearer ${effectiveApiKey}`;\n\t\t}\n\n\t\tconst response = await fetch(\n\t\t\t`${kvUrl}/v0/latest/social.near/${accountId}/profile/**`,\n\t\t\t{ headers },\n\t\t);\n\n\t\tif (response.ok) {\n\t\t\tconst data = await response.json() as KvResponse;\n\t\t\tconst entry = data?.entries?.[0];\n\t\t\tif (entry?.value) {\n\t\t\t\ttry {\n\t\t\t\t\tconst profile = typeof entry.value === \"string\"\n\t\t\t\t\t\t? JSON.parse(entry.value)\n\t\t\t\t\t\t: entry.value;\n\t\t\t\t\tif (profile?.name || profile?.description || profile?.image) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tname: profile.name,\n\t\t\t\t\t\t\tdescription: profile.description,\n\t\t\t\t\t\t\timage: profile.image,\n\t\t\t\t\t\t\tbackgroundImage: profile.backgroundImage,\n\t\t\t\t\t\t\tlinktree: profile.linktree,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t} catch {}\n\t\t\t}\n\t\t}\n\n\t\tconst apiBase = {\n\t\t\tmainnet: \"https://api.near.social\",\n\t\t\ttestnet: \"https://test.api.near.social\",\n\t\t}[network];\n\n\t\tconst keys = [`${accountId}/profile/**`];\n\n\t\tconst fallbackResponse = await fetch(`${apiBase}/get`, {\n\t\t\tmethod: \"POST\",\n\t\t\theaders: { \"Content-Type\": \"application/json\" },\n\t\t\tbody: JSON.stringify({ keys })\n\t\t});\n\n\t\tif (!fallbackResponse.ok) {\n\t\t\tthrow new Error(`HTTP error! status: ${fallbackResponse.status}`);\n\t\t}\n\n\t\tconst fallbackData = await fallbackResponse.json() as SocialApiResponse;\n\t\tconst profile: Profile | undefined = fallbackData?.[accountId]?.profile;\n\n\t\tif (profile) {\n\t\t\treturn {\n\t\t\t\tname: profile.name,\n\t\t\t\tdescription: profile.description,\n\t\t\t\timage: profile.image,\n\t\t\t\tbackgroundImage: profile.backgroundImage,\n\t\t\t\tlinktree: profile.linktree\n\t\t\t};\n\t\t}\n\t\treturn null;\n\t} catch (error) {\n\t\treturn null;\n\t}\n}\n\nexport { defaultGetProfile, getImageUrl, getNetworkFromAccountId };\n","import type { BetterAuthPluginDBSchema } from \"better-auth/db\";\n\nexport const schema = {\n\tnearAccount: {\n\t\tfields: {\n\t\t\tuserId: {\n\t\t\t\ttype: \"string\",\n\t\t\t\treferences: {\n\t\t\t\t\tmodel: \"user\",\n\t\t\t\t\tfield: \"id\",\n\t\t\t\t},\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\taccountId: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tnetwork: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tpublicKey: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tisPrimary: {\n\t\t\t\ttype: \"boolean\",\n\t\t\t\tdefaultValue: false,\n\t\t\t},\n\t\t\tcreatedAt: {\n\t\t\t\ttype: \"date\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t},\n\t},\n\trelayedTransaction: {\n\t\tfields: {\n\t\t\tuserId: {\n\t\t\t\ttype: \"string\",\n\t\t\t\treferences: {\n\t\t\t\t\tmodel: \"user\",\n\t\t\t\t\tfield: \"id\",\n\t\t\t\t},\n\t\t\t},\n\t\t\ttxHash: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tsenderId: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\treceiverId: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tnetwork: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tstatus: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tgasUsed: {\n\t\t\t\ttype: \"string\",\n\t\t\t},\n\t\t\tcreatedAt: {\n\t\t\t\ttype: \"date\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tupdatedAt: {\n\t\t\t\ttype: \"date\",\n\t\t\t},\n\t\t},\n\t},\n\trelayerKey: {\n\t\tfields: {\n\t\t\taccountId: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tencryptedPrivateKey: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tiv: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tpublicKey: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tnetwork: {\n\t\t\t\ttype: \"string\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tcreatedAt: {\n\t\t\t\ttype: \"date\",\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\tlastUsedAt: {\n\t\t\t\ttype: \"date\",\n\t\t\t},\n\t\t},\n\t},\n} satisfies BetterAuthPluginDBSchema;\n","import { z } from \"zod\";\nimport { AccountIdSchema, type AccountId } from \"near-kit/schemas\";\nimport type { AccountState, Near, TransactionBuilder } from \"near-kit\";\n\nexport type { AccountId };\n\nexport interface NearAccount {\n\tid: string;\n\tuserId: string;\n\taccountId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n\tpublicKey: string;\n\tisPrimary: boolean;\n\tcreatedAt: Date;\n}\n\nexport interface ListedNearAccount extends NearAccount {\n\tproviderId: \"siwn\";\n\tisActive: boolean;\n\tisAvailable: boolean;\n}\n\nexport interface ListAccountsResponseT {\n\taccounts: ListedNearAccount[];\n\tactiveAccount: ListedNearAccount | null;\n\tavailableAccounts: ListedNearAccount[];\n}\n\nexport interface SetPrimaryAccountResponseT extends ListAccountsResponseT {\n\tsuccess: boolean;\n\taccountId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n\tmessage: string;\n}\n\nexport const socialImageSchema = z.object({\n\turl: z.string().optional(),\n\tipfs_cid: z.string().optional(),\n});\n\nexport const profileSchema = z.object({\n\tname: z.string().optional(),\n\tdescription: z.string().optional(),\n\timage: socialImageSchema.optional(),\n\tbackgroundImage: socialImageSchema.optional(),\n\tlinktree: z.record(z.string(), z.string()).optional(),\n});\n\nexport type SocialImage = z.infer<typeof socialImageSchema>;\nexport type Profile = z.infer<typeof profileSchema>;\n\nconst signedMessageSchema = z.object({\n\taccountId: z.string(),\n\tpublicKey: z.string(),\n\tsignature: z.string(),\n\tstate: z.string().optional(),\n});\n\nexport const LinkAccountRequest = z.object({\n\tsignedMessage: signedMessageSchema,\n\tmessage: z.string(),\n\trecipient: z.string(),\n\tnonce: z.string(),\n\taccountId: AccountIdSchema,\n\tcallbackUrl: z.string().optional(),\n});\n\nexport const SetPrimaryAccountRequest = z.object({\n\taccountId: AccountIdSchema,\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n});\n\nexport const NonceRequest = z.object({\n\taccountId: AccountIdSchema,\n\tnetworkId: z.union([z.literal(\"mainnet\"), z.literal(\"testnet\")])\n});\n\nexport const VerifyRequest = z.object({\n\tsignedMessage: signedMessageSchema,\n\tmessage: z.string(),\n\trecipient: z.string(),\n\tnonce: z.string(),\n\taccountId: AccountIdSchema,\n\tcallbackUrl: z.string().optional(),\n});\n\nexport const RelayRequest = z.object({\n\tpayload: z.string(),\n});\nexport type RelayRequestT = z.infer<typeof RelayRequest>;\n\nexport const RelayResponse = z.object({\n\ttxHash: z.string(),\n\tstatus: z.enum([\"pending\", \"completed\", \"failed\"]),\n});\nexport type RelayResponseT = z.infer<typeof RelayResponse>;\n\nexport const RelayStatusResponse = z.object({\n\tstatus: z.enum([\"pending\", \"completed\", \"failed\"]),\n\tgasUsed: z.string().optional(),\n\toutcome: z.unknown().optional(),\n});\nexport type RelayStatusResponseT = z.infer<typeof RelayStatusResponse>;\n\nexport const ViewContractRequest = z.object({\n\tcontractId: z.string(),\n\tmethodName: z.string(),\n\targs: z.record(z.string(), z.any()).optional(),\n});\nexport type ViewContractRequestT = z.infer<typeof ViewContractRequest>;\n\nexport const NonceResponse = z.object({ nonce: z.string() });\nexport const VerifyResponse = z.object({\n\ttoken: z.string(),\n\tsuccess: z.literal(true),\n\tuser: z.object({\n\t\tid: z.string(),\n\t\taccountId: AccountIdSchema,\n\t\tnetwork: z.union([z.literal(\"mainnet\"), z.literal(\"testnet\")]),\n\t}),\n});\nexport const ProfileResponse = profileSchema.nullable();\nexport const ViewContractResponse = z.object({ result: z.unknown() });\n\nexport const ProfileRequest = z.object({\n\taccountId: AccountIdSchema.optional(),\n});\nexport type ProfileRequestT = z.infer<typeof ProfileRequest>;\n\nexport type NonceRequestT = z.infer<typeof NonceRequest>;\nexport type NonceResponseT = z.infer<typeof NonceResponse>;\nexport type SetPrimaryAccountRequestT = z.infer<typeof SetPrimaryAccountRequest>;\nexport type VerifyRequestT = z.infer<typeof VerifyRequest>;\nexport type VerifyResponseT = z.infer<typeof VerifyResponse>;\nexport type ProfileResponseT = z.infer<typeof ProfileResponse>;\nexport type ViewContractResponseT = z.infer<typeof ViewContractResponse>;\n\nexport const RelayedTransactionSchema = z.object({\n\tid: z.string(),\n\tuserId: z.string(),\n\ttxHash: z.string(),\n\tsenderId: z.string(),\n\treceiverId: z.string(),\n\tnetwork: z.string(),\n\tstatus: z.string(),\n\tgasUsed: z.string().optional(),\n\tcreatedAt: z.string(),\n\tupdatedAt: z.string().optional(),\n});\nexport type RelayedTransactionT = z.infer<typeof RelayedTransactionSchema>;\n\nexport const RelayHistoryResponse = z.object({\n\ttransactions: z.array(RelayedTransactionSchema),\n});\nexport type RelayHistoryResponseT = z.infer<typeof RelayHistoryResponse>;\n\nexport interface RelayedTransactionRecord {\n\tid: string;\n\tuserId: string;\n\ttxHash: string;\n\tsenderId: string;\n\treceiverId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n\tstatus: string;\n\tgasUsed?: string;\n\tcreatedAt: Date;\n\tupdatedAt: Date;\n}\n\nexport interface RelayerInfo extends AccountState {\n\taccountId: string;\n\tmode: \"ephemeral\" | \"explicit\";\n\tnetwork: \"mainnet\" | \"testnet\";\n\tpublicKey: string;\n\thasKey: boolean;\n\tcreatedAt?: Date;\n\tlastUsedAt?: Date;\n\tparentAccount?: string;\n\tsubAccountAvailable?: boolean;\n}\n\nexport interface DualNetworkConfig<T> {\n\tmainnet: T;\n\ttestnet: T;\n}\n\nexport const GetRelayerInfoRequest = z.object({\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n});\nexport type GetRelayerInfoRequestT = z.infer<typeof GetRelayerInfoRequest>;\n\nexport interface SubAccountRelayerFCAKConfig {\n\treceiverId: string;\n\tmethodNames?: string[];\n\tallowance?: string;\n}\n\nexport interface SubAccountTxCtx {\n\tnewAccountId: string;\n\tparentAccount: string;\n\tuserPublicKey: string;\n\tuserAccountId: string;\n\tuserId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n}\n\nexport interface SubAccountLifecycleCtx extends SubAccountTxCtx {\n\tnear: Near;\n}\n\nexport interface SubAccountConfig {\n\tparentAccount?: string;\n\tminDeposit?: string;\n\tparentHasFullAccess?: boolean;\n\tdeploy?:\n\t\t| { wasm: Uint8Array }\n\t\t| { fromPublished: { accountId?: string; codeHash?: string } };\n\tinit?: {\n\t\tmethodName: string;\n\t\targs: object | ((ctx: SubAccountTxCtx) => object);\n\t};\n\textendTx?: (tx: TransactionBuilder, ctx: SubAccountTxCtx) => TransactionBuilder;\n\tonCreated?: (ctx: SubAccountLifecycleCtx) => Promise<void>;\n\tonRollback?: (ctx: SubAccountLifecycleCtx) => Promise<void>;\n\taddRelayerFCAK?: boolean;\n\trelayerFCAK?: SubAccountRelayerFCAKConfig;\n}\n\nexport const SUB_ACCOUNT_LABEL_REGEX = /^([a-z\\d]+[-_])*[a-z\\d]+$/;\n\nexport const SubAccountNameSchema = z\n\t.string()\n\t.min(2, \"Sub-account name must be at least 2 characters\")\n\t.max(64, \"Sub-account name must be at most 64 characters\")\n\t.regex(SUB_ACCOUNT_LABEL_REGEX, \"Sub-account name must contain only lowercase alphanumeric characters, hyphens, and underscores (no leading/trailing hyphens or underscores)\");\n\nexport const CreateSubAccountRequest = z.object({\n\tsubAccountName: SubAccountNameSchema,\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n\tpublicKey: z.string(),\n});\nexport type CreateSubAccountRequestT = z.infer<typeof CreateSubAccountRequest>;\n\nexport const CreateSubAccountResponse = z.object({\n\tsuccess: z.literal(true),\n\taccountId: z.string(),\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]),\n\tpublicKey: z.string(),\n\tmessage: z.string(),\n});\nexport type CreateSubAccountResponseT = z.infer<typeof CreateSubAccountResponse>;\n\nexport const CheckSubAccountAvailabilityRequest = z.object({\n\tsubAccountName: SubAccountNameSchema,\n\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n});\nexport type CheckSubAccountAvailabilityRequestT = z.infer<typeof CheckSubAccountAvailabilityRequest>;\n\nexport const CheckSubAccountAvailabilityResponse = z.object({\n\tavailable: z.boolean(),\n\taccountId: z.string(),\n\tparentAccount: z.string().optional(),\n\treason: z.enum([\"taken\", \"invalid\", \"too-long\", \"not-configured\"]).optional(),\n});\nexport type CheckSubAccountAvailabilityResponseT = z.infer<typeof CheckSubAccountAvailabilityResponse>;\n","export function bytesToBase64(bytes: Uint8Array): string {\n\treturn btoa(String.fromCharCode(...bytes));\n}\n\nfunction base64ToBytes(base64: string): Uint8Array {\n\treturn Uint8Array.from(atob(base64), c => c.charCodeAt(0));\n}\n\nexport function bytesToHex(bytes: Uint8Array): string {\n\treturn Array.from(bytes).map(b => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nexport function hexToBytes(hex: string): Uint8Array {\n\tconst bytes = new Uint8Array(hex.length / 2);\n\tfor (let i = 0; i < hex.length; i += 2) {\n\t\tbytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);\n\t}\n\treturn bytes;\n}\n\nasync function deriveAesKey(secret: string): Promise<CryptoKey> {\n\tconst keyMaterial = await crypto.subtle.importKey(\n\t\t\"raw\",\n\t\tnew TextEncoder().encode(secret),\n\t\t{ name: \"HKDF\" },\n\t\tfalse,\n\t\t[\"deriveKey\"],\n\t);\n\treturn crypto.subtle.deriveKey(\n\t\t{\n\t\t\tname: \"HKDF\",\n\t\t\thash: \"SHA-256\",\n\t\t\tsalt: new TextEncoder().encode(\"better-near-auth-relayer\"),\n\t\t\tinfo: new Uint8Array(0),\n\t\t},\n\t\tkeyMaterial,\n\t\t{ name: \"AES-GCM\", length: 256 },\n\t\tfalse,\n\t\t[\"encrypt\", \"decrypt\"],\n\t);\n}\n\nexport async function encryptPrivateKey(\n\tprivateKey: Uint8Array,\n\tsecret: string,\n): Promise<{ encrypted: string; iv: string }> {\n\tconst aesKey = await deriveAesKey(secret);\n\tconst iv = crypto.getRandomValues(new Uint8Array(12));\n\tconst encrypted = await crypto.subtle.encrypt(\n\t\t{ name: \"AES-GCM\", iv },\n\t\taesKey,\n\t\tprivateKey as Uint8Array<ArrayBuffer>,\n\t);\n\treturn {\n\t\tencrypted: bytesToBase64(new Uint8Array(encrypted)),\n\t\tiv: bytesToBase64(iv),\n\t};\n}\n\nexport async function decryptPrivateKey(\n\tencrypted: string,\n\tiv: string,\n\tsecret: string,\n): Promise<Uint8Array> {\n\tconst aesKey = await deriveAesKey(secret);\n\tconst decrypted = await crypto.subtle.decrypt(\n\t\t{ name: \"AES-GCM\", iv: base64ToBytes(iv) as Uint8Array<ArrayBuffer> },\n\t\taesKey,\n\t\tbase64ToBytes(encrypted) as Uint8Array<ArrayBuffer>,\n\t);\n\treturn new Uint8Array(decrypted);\n}\n","import { APIError, createAuthEndpoint, createAuthMiddleware, sessionMiddleware } from \"better-auth/api\";\nimport { setSessionCookie } from \"better-auth/cookies\";\nimport type { Account, User, DBAdapter } from \"better-auth/types\";\n// Explicit type imports for declaration emit\nimport type {} from \"@better-auth/core/env\";\nimport type {} from \"@better-auth/core/oauth2\";\nimport type {} from \"better-call\";\n\nimport { Near, generateNonce, generateKey, parseKey, verifyNep413Signature, decodeSignedDelegateAction, InMemoryKeyStore, RotatingKeyStore } from \"near-kit\";\nimport type { SignedDelegateAction, PrivateKey } from \"near-kit\";\nimport { hex, base58 } from \"@scure/base\";\nimport z from \"zod\";\nimport { defaultGetProfile, getImageUrl, getNetworkFromAccountId } from \"./profile.js\";\nimport { schema } from \"./schema.js\";\nimport {\n\ttype AccountId,\n\ttype DualNetworkConfig,\n\ttype ListAccountsResponseT,\n\ttype ListedNearAccount,\n\ttype NearAccount,\n\ttype Profile,\n\ttype RelayerInfo,\n\ttype RelayedTransactionRecord,\n\ttype SubAccountConfig,\n\tLinkAccountRequest,\n\tNonceRequest,\n\tNonceResponse,\n\tProfileRequest,\n\tProfileResponse,\n\tVerifyRequest,\n\tVerifyResponse,\n\tRelayRequest,\n\tRelayResponse,\n\tRelayStatusResponse,\n\tSetPrimaryAccountRequest,\n\tViewContractRequest,\n\tViewContractResponse,\n\tGetRelayerInfoRequest,\n\tCreateSubAccountRequest,\n\tCreateSubAccountResponse,\n\tCheckSubAccountAvailabilityRequest,\n\tCheckSubAccountAvailabilityResponse,\n\ttype SubAccountTxCtx,\n\ttype SubAccountLifecycleCtx,\n} from \"./types.js\";\nexport * from \"./types.js\";\nimport {\n\tbytesToHex,\n\tencryptPrivateKey,\n\tdecryptPrivateKey,\n} from \"./utils.js\";\n\nasync function hashNonce(nonce: Uint8Array): Promise<string> {\n\tconst data = new TextEncoder().encode(hex.encode(nonce));\n\tconst hashBuffer = await crypto.subtle.digest('SHA-256', data);\n\treturn hex.encode(new Uint8Array(hashBuffer));\n}\n\nfunction deriveEmail(accountId: string, recipient: string): string {\n\tif (accountId.endsWith(\".near\")) {\n\t\tconst localPart = accountId.slice(0, -5);\n\t\tif (!localPart.includes(\".\")) {\n\t\t\treturn `${localPart}@near.email`;\n\t\t}\n\t}\n\tconst randomId = crypto.randomUUID().slice(0, 8);\n\treturn `temp-${randomId}@${recipient}`;\n}\n\nfunction nearAccountKey(account: Pick<NearAccount, \"accountId\" | \"network\">): string {\n\treturn `${account.accountId}:${account.network}`;\n}\n\nfunction getCreatedAtTime(account: NearAccount): number {\n\treturn account.createdAt instanceof Date\n\t\t? account.createdAt.getTime()\n\t\t: new Date(account.createdAt).getTime();\n}\n\nfunction buildListAccountsResponse(nearAccounts: NearAccount[]): ListAccountsResponseT {\n\tconst activeAccount = nearAccounts.find((account) => account.isPrimary) ?? nearAccounts[0] ?? null;\n\tconst activeKey = activeAccount ? nearAccountKey(activeAccount) : null;\n\tconst accounts: ListedNearAccount[] = nearAccounts\n\t\t.map((account) => {\n\t\t\tconst isActive = activeKey === nearAccountKey(account);\n\t\t\treturn {\n\t\t\t\t...account,\n\t\t\t\tproviderId: \"siwn\" as const,\n\t\t\t\tisActive,\n\t\t\t\tisAvailable: !isActive,\n\t\t\t};\n\t\t})\n\t\t.sort((a, b) => {\n\t\t\tif (a.isActive !== b.isActive) return a.isActive ? -1 : 1;\n\t\t\treturn getCreatedAtTime(a) - getCreatedAtTime(b);\n\t\t});\n\tconst listedActiveAccount = accounts.find((account) => account.isActive) ?? null;\n\n\treturn {\n\t\taccounts,\n\t\tactiveAccount: listedActiveAccount ? { ...listedActiveAccount } : null,\n\t\tavailableAccounts: accounts\n\t\t\t.filter((account) => account.isAvailable)\n\t\t\t.map((account) => ({ ...account })),\n\t};\n}\n\nexport interface RelayerConfig {\n\taccountId?: string;\n\tprivateKey?: string;\n\tprivateKeys?: string[];\n\twhitelistedContracts?: string[];\n\tmaxGasPerTransaction?: string;\n\tmaxDepositPerTransaction?: string;\n}\n\ninterface RelayerState {\n\tnear: Near;\n\taccountId: string;\n\tnetwork: \"mainnet\" | \"testnet\";\n\tmode: \"ephemeral\" | \"explicit\";\n\tpublicKey: string;\n\tcreatedAt?: Date;\n\tlastUsedAt?: Date;\n}\n\nfunction createNear(\n\tnetwork: \"mainnet\" | \"testnet\",\n\theaders: Record<string, string>,\n\trpcUrl?: string,\n\tkeyStore?: InMemoryKeyStore | RotatingKeyStore,\n): Near {\n\tconst config: ConstructorParameters<typeof Near>[0] = { headers };\n\tif (rpcUrl) {\n\t\tconfig.network = { rpcUrl, networkId: network };\n\t} else {\n\t\tconfig.network = network;\n\t}\n\tif (keyStore) {\n\t\tconfig.keyStore = keyStore;\n\t}\n\treturn new Near(config);\n}\n\nasync function initRelayer(\n\trelayerConfig: RelayerConfig | undefined,\n\tnetwork: \"mainnet\" | \"testnet\",\n\tadapter: DBAdapter,\n\tsecret: string | undefined,\n\tapiKey?: string,\n\trpcUrl?: string,\n): Promise<RelayerState | null> {\n\tif (!relayerConfig) return null;\n\n\tconst headers: Record<string, string> = {};\n\tif (apiKey) {\n\t\theaders[\"Authorization\"] = `Bearer ${apiKey}`;\n\t}\n\n\tif (relayerConfig.accountId && (relayerConfig.privateKey || relayerConfig.privateKeys)) {\n\t\tconst keys = relayerConfig.privateKeys ?? (relayerConfig.privateKey ? [relayerConfig.privateKey] : []);\n\t\tlet keyStore: InMemoryKeyStore | RotatingKeyStore;\n\n\t\tif (keys.length === 1) {\n\t\t\tkeyStore = new InMemoryKeyStore({\n\t\t\t\t[relayerConfig.accountId]: keys[0]!,\n\t\t\t});\n\t\t} else {\n\t\t\tkeyStore = new RotatingKeyStore({\n\t\t\t\t[relayerConfig.accountId]: keys as string[],\n\t\t\t});\n\t\t}\n\n\t\tconst near = createNear(network, headers, rpcUrl, keyStore);\n\t\tconst explicitPublicKey = parseKey(keys[0]!);\n\t\treturn {\n\t\t\tnear,\n\t\t\taccountId: relayerConfig.accountId,\n\t\t\tnetwork,\n\t\t\tmode: \"explicit\",\n\t\t\tpublicKey: explicitPublicKey.publicKey.toString(),\n\t\t};\n\t}\n\n\tconst existing = await adapter.findOne<{ encryptedPrivateKey: string; iv: string; createdAt: Date; lastUsedAt: Date }>({\n\t\tmodel: \"relayerKey\",\n\t\twhere: [{ field: \"network\", operator: \"eq\", value: network }],\n\t});\n\n\tif (existing) {\n\t\tif (!secret) throw new Error(\"BETTER_AUTH_SECRET required for relayer key decryption\");\n\t\tconst privateKeyBytes = await decryptPrivateKey(existing.encryptedPrivateKey, existing.iv, secret);\n\t\tconst keyPair = parseKey(`ed25519:${base58.encode(privateKeyBytes)}`);\n\t\tconst accountId = bytesToHex(keyPair.publicKey.data);\n\n\t\tconsole.log(`[siwn] Relayer recovered: ${accountId} (${network})`);\n\n\t\tconst keyStore = new InMemoryKeyStore();\n\t\tawait keyStore.add(accountId, keyPair);\n\n\t\tconst near = createNear(network, headers, rpcUrl, keyStore);\n\t\treturn {\n\t\t\tnear,\n\t\t\taccountId,\n\t\t\tnetwork,\n\t\t\tmode: \"ephemeral\",\n\t\t\tpublicKey: keyPair.publicKey.toString(),\n\t\t\tcreatedAt: existing.createdAt,\n\t\t\tlastUsedAt: existing.lastUsedAt,\n\t\t};\n\t}\n\n\tconst keyPair = generateKey();\n\tif (!secret) throw new Error(\"BETTER_AUTH_SECRET required for relayer key encryption\");\n\tconst privateKeyBytes = keyPair.secretKey.startsWith(\"ed25519:\")\n\t\t? base58.decode(keyPair.secretKey.slice(8))\n\t\t: new Uint8Array(0);\n\n\tconst publicKeyBase58 = keyPair.publicKey.toString().replace(\"ed25519:\", \"\");\n\tconst accountId = bytesToHex(keyPair.publicKey.data);\n\tconst createdAt = new Date();\n\n\tconst { encrypted, iv } = await encryptPrivateKey(privateKeyBytes, secret);\n\n\tawait adapter.create({\n\t\t\tmodel: \"relayerKey\",\n\t\t\tdata: {\n\t\t\t\taccountId,\n\t\t\t\tencryptedPrivateKey: encrypted,\n\t\t\t\tiv,\n\t\t\t\tpublicKey: `ed25519:${publicKeyBase58}`,\n\t\t\t\tnetwork,\n\t\t\t\tcreatedAt,\n\t\t\t\tlastUsedAt: createdAt,\n\t\t\t},\n\t\t});\n\n\tconsole.log(`[siwn] Relayer created in EPHEMERAL mode: ${accountId} (${network})`);\n\tconsole.log(`[siwn] Fund this account with NEAR to enable gasless relay`);\n\tconsole.log(`[siwn] Private key is encrypted in DB — persists across restarts`);\n\n\tconst keyStore = new InMemoryKeyStore();\n\tawait keyStore.add(accountId, keyPair);\n\n\tconst near = createNear(network, headers, rpcUrl, keyStore);\n\treturn {\n\t\tnear,\n\t\taccountId,\n\t\tnetwork,\n\t\tmode: \"ephemeral\",\n\t\tpublicKey: keyPair.publicKey.toString(),\n\t\tcreatedAt,\n\t\tlastUsedAt: createdAt,\n\t};\n}\n\nasync function relayOnChain(\n\tpayload: string,\n\trelayerState: RelayerState,\n): Promise<{ txHash: string }> {\n\tconst userAction = decodeSignedDelegateAction(payload);\n\n\tconst result = await relayerState.near\n\t\t.transaction(relayerState.accountId)\n\t\t.signedDelegateAction(userAction)\n\t\t.send({ waitUntil: \"EXECUTED\" });\n\n\treturn { txHash: result.transaction.hash };\n}\n\nasync function defaultValidateLimitedAccessKey(\n\taccountId: string,\n\tpublicKey: string,\n\trecipient: string,\n\tnear: Near,\n): Promise<boolean> {\n\tconst key = await near.getAccessKey(accountId, publicKey);\n\tif (!key) return false;\n\tif (key.permission === \"FullAccess\") return true;\n\tif (\"FunctionCall\" in key.permission) {\n\t\treturn key.permission.FunctionCall.receiver_id === recipient;\n\t}\n\treturn false;\n}\n\nfunction isImplicitAccount(accountId: string): boolean {\n\treturn /^[0-9a-f]{64}$/.test(accountId);\n}\n\nfunction resolveParentAccount(\n\tsubAccountCfg: SubAccountConfig | undefined,\n\tparentKey: string | undefined,\n\trState: RelayerState | null,\n): { parentAccount: string | null; subAccountAvailable: boolean } {\n\tconst parent = subAccountCfg?.parentAccount ?? rState?.accountId;\n\tif (!parent || isImplicitAccount(parent)) {\n\t\treturn { parentAccount: null, subAccountAvailable: false };\n\t}\n\tif (!rState && !parentKey) {\n\t\treturn { parentAccount: null, subAccountAvailable: false };\n\t}\n\treturn { parentAccount: parent, subAccountAvailable: true };\n}\n\nexport interface SIWNPluginOptions {\n\trecipient?: string;\n\trecipients?: DualNetworkConfig<string>;\n\trequireFullAccessKey?: boolean;\n\tgetNonce?: () => Promise<Uint8Array>;\n\tgetProfile?: (accountId: AccountId) => Promise<Profile | null>;\n\tvalidateLimitedAccessKey?: (args: {\n\t\taccountId: AccountId;\n\t\tpublicKey: string;\n\t\trecipient?: string;\n\t}) => Promise<boolean>;\n\tapiKey?: string;\n\trpcUrl?: string | DualNetworkConfig<string>;\n\trelayer?: RelayerConfig | DualNetworkConfig<RelayerConfig>;\n\tsecrets?: {\n\t\tparentKey?: string | DualNetworkConfig<string>;\n\t};\n\tsubAccount?: SubAccountConfig | DualNetworkConfig<SubAccountConfig>;\n}\n\nexport const siwn = (options: SIWNPluginOptions) => {\n\tif (!options.recipient && !options.recipients) {\n\t\tthrow new Error(\"Either 'recipient' or 'recipients' must be provided to siwn plugin\");\n\t}\n\n\tconst apiKey = options.apiKey;\n\n\tconst getRecipient = (network: \"mainnet\" | \"testnet\"): string => {\n\t\tif (options.recipients) return options.recipients[network];\n\t\treturn options.recipient!;\n\t};\n\n\tconst getSupportedNetworks = (): (\"mainnet\" | \"testnet\")[] => {\n\t\tif (options.recipients) return [\"mainnet\", \"testnet\"];\n\t\tif (options.recipient) return [getNetworkFromAccountId(options.recipient)];\n\t\treturn [\"mainnet\"];\n\t};\n\n\tconst getRelayerConfig = (network: \"mainnet\" | \"testnet\"): RelayerConfig | undefined => {\n\t\tif (!options.relayer) return undefined;\n\t\tif (\"mainnet\" in options.relayer && typeof options.relayer.mainnet === \"object\") {\n\t\t\treturn (options.relayer as DualNetworkConfig<RelayerConfig>)[network];\n\t\t}\n\t\treturn options.relayer as RelayerConfig;\n\t};\n\n\tconst getRpcUrl = (network: \"mainnet\" | \"testnet\"): string | undefined => {\n\t\tif (!options.rpcUrl) return undefined;\n\t\tif (typeof options.rpcUrl === \"object\") return (options.rpcUrl as DualNetworkConfig<string>)[network];\n\t\treturn options.rpcUrl as string;\n\t};\n\n\tconst getSubAccountConfig = (network: \"mainnet\" | \"testnet\"): SubAccountConfig | undefined => {\n\t\tif (!options.subAccount) return undefined;\n\t\tif (\"mainnet\" in options.subAccount && typeof options.subAccount.mainnet === \"object\") {\n\t\t\treturn (options.subAccount as DualNetworkConfig<SubAccountConfig>)[network];\n\t\t}\n\t\treturn options.subAccount as SubAccountConfig;\n\t};\n\n\tconst getParentKey = (network: \"mainnet\" | \"testnet\"): string | undefined => {\n\t\tif (!options.secrets?.parentKey) return undefined;\n\t\tif (typeof options.secrets.parentKey === \"object\") {\n\t\t\treturn (options.secrets.parentKey as DualNetworkConfig<string>)[network];\n\t\t}\n\t\treturn options.secrets.parentKey as string;\n\t};\n\n\tconst getRawParentKey = (network: \"mainnet\" | \"testnet\"): string | undefined => {\n\t\tconst fromSecrets = getParentKey(network);\n\t\tif (fromSecrets) return fromSecrets;\n\t\tconst relayerCfg = getRelayerConfig(network);\n\t\tconst subAccountCfg = getSubAccountConfig(network);\n\t\tconst parent = subAccountCfg?.parentAccount ?? relayerCfg?.accountId;\n\t\tif (parent && parent === relayerCfg?.accountId) {\n\t\t\treturn relayerCfg.privateKey ?? relayerCfg.privateKeys?.[0];\n\t\t}\n\t\treturn undefined;\n\t};\n\n\tconst primaryNetwork = getSupportedNetworks()[0];\n\tconst relayerStates = new Map<\"mainnet\" | \"testnet\", RelayerState | null>();\n\tconst relayerInitPromises = new Map<\"mainnet\" | \"testnet\", Promise<RelayerState | null>>();\n\n\tconst headers: Record<string, string> = {};\n\tif (apiKey) {\n\t\theaders[\"Authorization\"] = `Bearer ${apiKey}`;\n\t}\n\n\tconst ensureRelayer = async (adapter: DBAdapter, secret: string | undefined, network: \"mainnet\" | \"testnet\") => {\n\t\tif (relayerStates.has(network)) return relayerStates.get(network) ?? null;\n\n\t\tconst existingInit = relayerInitPromises.get(network);\n\t\tif (existingInit) return existingInit;\n\n\t\tconst relayerCfg = getRelayerConfig(network);\n\t\tconst networkRpcUrl = getRpcUrl(network);\n\n\t\tconst initPromise = initRelayer(relayerCfg, network, adapter, secret, apiKey, networkRpcUrl)\n\t\t\t.then((state) => {\n\t\t\t\trelayerStates.set(network, state);\n\t\t\t\trelayerInitPromises.delete(network);\n\t\t\t\treturn state;\n\t\t\t})\n\t\t\t.catch((error) => {\n\t\t\t\trelayerInitPromises.delete(network);\n\t\t\t\tthrow error;\n\t\t\t});\n\n\t\trelayerInitPromises.set(network, initPromise);\n\t\treturn initPromise;\n\t};\n\n\tconst getNear = (network: \"mainnet\" | \"testnet\") => {\n\t\tconst networkRpcUrl = getRpcUrl(network);\n\t\tif (networkRpcUrl) {\n\t\t\treturn new Near({ network: { rpcUrl: networkRpcUrl, networkId: network }, headers: apiKey ? { Authorization: `Bearer ${apiKey}` } : undefined });\n\t\t}\n\t\treturn new Near({ network, headers: apiKey ? { Authorization: `Bearer ${apiKey}` } : undefined });\n\t};\n\n\treturn ({\n\t\tid: \"siwn\",\n\t\tschema,\n\t\thooks: {\n\t\t\tafter: [\n\t\t\t\t{\n\t\t\t\t\tmatcher: (context: { path?: string; method?: string }) => context.path === \"/auth/session\" && context.method === \"GET\",\n\t\t\t\t\thandler: createAuthMiddleware(async (ctx) => {\n\t\t\t\t\t\tconst session = ctx.context.session;\n\t\t\t\t\t\tif (session) {\n\t\t\t\t\t\t\tconst nearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t\t\t{ field: \"isPrimary\", operator: \"eq\", value: true },\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tif (nearAccount) {\n\t\t\t\t\t\t\t\tctx.context.session = {\n\t\t\t\t\t\t\t\t\t...session,\n\t\t\t\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\t\t\t\t...session.user,\n\t\t\t\t\t\t\t\t\t\tnearAccount: nearAccount\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn { context: ctx };\n\t\t\t\t\t}),\n\t\t\t\t},\n\t\t\t],\n\t\t},\n\t\tendpoints: {\n\t\t\tlinkNearAccount: createAuthEndpoint(\n\t\t\t\t\"/near/link-account\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: LinkAccountRequest,\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t\trequireRequest: true,\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { signedMessage, message, recipient, nonce, accountId, callbackUrl } = ctx.body;\n\t\t\t\t\tconst network = getNetworkFromAccountId(accountId);\n\t\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\t\tif (!session) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"Must be logged in to link NEAR account\",\n\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst near = getNear(network);\n\t\t\t\t\t\tconst nonceBytes = hex.decode(nonce);\n\n\t\t\t\t\t\tconst isValid = await verifyNep413Signature(\n\t\t\t\t\t\tsignedMessage,\n\t\t\t\t\t\t{ message, recipient, nonce: nonceBytes, callbackUrl },\n\t\t\t\t\t\t\t{ near, maxAge: 15 * 60 * 1000 },\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif (!isValid) {\n\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\tmessage: \"Unauthorized: Invalid signature\",\n\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (signedMessage.accountId !== accountId) {\n\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\tmessage: \"Unauthorized: Account ID mismatch\",\n\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst publicKey = signedMessage.publicKey;\n\n\t\t\t\t\t\tif (!options.requireFullAccessKey && options.validateLimitedAccessKey) {\n\t\t\t\t\t\t\tconst isValidKey = await options.validateLimitedAccessKey({\n\t\t\t\t\t\t\t\taccountId: accountId,\n\t\t\t\t\t\t\t\tpublicKey: publicKey,\n\t\t\t\t\t\t\t\trecipient: options.recipient\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tif (!isValidKey) {\n\t\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\t\tmessage: \"Unauthorized: Invalid function call access key\",\n\t\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst existingNearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"eq\", value: accountId },\n\t\t\t\t\t\t\t\t{ field: \"network\", operator: \"eq\", value: network },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tif (existingNearAccount) {\n\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\tmessage: \"This NEAR account is already linked to another user\",\n\t\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst existingPrimaryAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t\t{ field: \"isPrimary\", operator: \"eq\", value: true },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tawait ctx.context.adapter.create({\n\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\tuserId: session.user.id,\n\t\t\t\t\t\t\t\taccountId,\n\t\t\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\t\t\tpublicKey: publicKey,\n\t\t\t\t\t\t\t\tisPrimary: !existingPrimaryAccount,\n\t\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tawait ctx.context.internalAdapter.createAccount({\n\t\t\t\t\t\t\tuserId: session.user.id,\n\t\t\t\t\t\t\tproviderId: \"siwn\",\n\t\t\t\t\t\t\taccountId: `${accountId}:${network}`,\n\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\t\taccountId,\n\t\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\t\tmessage: \"NEAR account successfully linked\"\n\t\t\t\t\t\t});\n\t\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\t\tif (error instanceof APIError) throw error;\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"Something went wrong. Please try again later.\",\n\t\t\t\t\t\t\terror: error instanceof Error ? error.message : \"Unknown error\",\n\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\tunlinkNearAccount: createAuthEndpoint(\n\t\t\t\t\"/near/unlink-account\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: z.object({\n\t\t\t\t\t\taccountId: z.string(),\n\t\t\t\t\t\tnetwork: z.enum([\"mainnet\", \"testnet\"]).optional(),\n\t\t\t\t\t}),\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { accountId, network: providedNetwork } = ctx.body;\n\t\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\t\tif (!session) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"Must be logged in to unlink NEAR account\",\n\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst network = providedNetwork || getNetworkFromAccountId(accountId);\n\n\t\t\t\t\tconst nearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"eq\", value: accountId },\n\t\t\t\t\t\t\t{ field: \"network\", operator: \"eq\", value: network },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tif (!nearAccount) {\n\t\t\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\t\t\tmessage: \"NEAR account not found or not linked to your user\",\n\t\t\t\t\t\t\tstatus: 404,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst accounts = await ctx.context.adapter.findMany({\n\t\t\t\t\t\tmodel: \"account\",\n\t\t\t\t\t\twhere: [{ field: \"userId\", operator: \"eq\", value: session.user.id }],\n\t\t\t\t\t});\n\n\t\t\t\t\tif (accounts.length <= 1) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"Cannot unlink last authentication method. Link another account first.\",\n\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tif (nearAccount.isPrimary) {\n\t\t\t\t\t\tconst otherNearAccounts: NearAccount[] = await ctx.context.adapter.findMany({\n\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"ne\", value: accountId },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tif (otherNearAccounts.length > 0) {\n\t\t\t\t\t\t\tawait ctx.context.adapter.update({\n\t\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t\t{ field: \"id\", operator: \"eq\", value: otherNearAccounts[0]!.id },\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\tupdate: { isPrimary: true },\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tawait ctx.context.adapter.delete({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"eq\", value: accountId },\n\t\t\t\t\t\t\t{ field: \"network\", operator: \"eq\", value: network },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tconst accountToDelete: Account | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\tmodel: \"account\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t{ field: \"providerId\", operator: \"eq\", value: \"siwn\" },\n\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"eq\", value: `${accountId}:${network}` },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tif (accountToDelete) {\n\t\t\t\t\t\tawait ctx.context.internalAdapter.deleteAccount(accountToDelete.id);\n\t\t\t\t\t}\n\n\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\taccountId,\n\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\tmessage: \"NEAR account successfully unlinked\"\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t),\n\t\t\tlistNearAccounts: createAuthEndpoint(\n\t\t\t\t\"/near/list-accounts\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\t\tconst nearAccounts: NearAccount[] = await ctx.context.adapter.findMany({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [{ field: \"userId\", operator: \"eq\", value: session.user.id }],\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json(buildListAccountsResponse(nearAccounts));\n\t\t\t\t},\n\t\t\t),\n\t\t\tsetPrimaryNearAccount: createAuthEndpoint(\n\t\t\t\t\"/near/set-primary-account\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: SetPrimaryAccountRequest,\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { accountId, network: providedNetwork } = ctx.body;\n\t\t\t\t\tconst session = ctx.context.session;\n\t\t\t\t\tconst network = providedNetwork || getNetworkFromAccountId(accountId);\n\n\t\t\t\t\tconst targetAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"eq\", value: accountId },\n\t\t\t\t\t\t\t{ field: \"network\", operator: \"eq\", value: network },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tif (!targetAccount) {\n\t\t\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\t\t\tmessage: \"NEAR account not found or not linked to your user\",\n\t\t\t\t\t\t\tstatus: 404,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst nearAccounts: NearAccount[] = await ctx.context.adapter.findMany({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [{ field: \"userId\", operator: \"eq\", value: session.user.id }],\n\t\t\t\t\t});\n\n\t\t\t\t\tawait Promise.all(nearAccounts.map((account) => ctx.context.adapter.update({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [{ field: \"id\", operator: \"eq\", value: account.id }],\n\t\t\t\t\t\tupdate: { isPrimary: nearAccountKey(account) === nearAccountKey(targetAccount) },\n\t\t\t\t\t})));\n\n\t\t\t\t\tconst updatedNearAccounts: NearAccount[] = await ctx.context.adapter.findMany({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [{ field: \"userId\", operator: \"eq\", value: session.user.id }],\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\taccountId,\n\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\tmessage: \"Primary NEAR account updated\",\n\t\t\t\t\t\t...buildListAccountsResponse(updatedNearAccounts),\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t),\n\t\t\tgetSiwnNonce: createAuthEndpoint(\n\t\t\t\t\"/near/nonce\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: NonceRequest,\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { accountId, networkId } = ctx.body;\n\t\t\t\t\tconst network = getNetworkFromAccountId(accountId);\n\n\t\t\t\t\tif (networkId !== network) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"Network ID mismatch with account ID\",\n\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst near = getNear(network);\n\t\t\t\t\tconst exists = await near.accountExists(accountId);\n\t\t\t\t\tif (!exists) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"Account does not exist on-chain\",\n\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst nonce = options.getNonce ? await options.getNonce() : generateNonce();\n\n\t\t\t\t\tconst nonceString = hex.encode(nonce);\n\n\t\t\t\t\tawait ctx.context.internalAdapter.createVerificationValue({\n\t\t\t\t\t\tidentifier: `siwn:${accountId}:${network}`,\n\t\t\t\t\t\tvalue: nonceString,\n\t\t\t\t\t\texpiresAt: new Date(Date.now() + 15 * 60 * 1000),\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json(NonceResponse.parse({ nonce: nonceString }));\n\t\t\t\t},\n\t\t\t),\n\t\t\tgetSiwnProfile: createAuthEndpoint(\n\t\t\t\t\"/near/profile\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: ProfileRequest,\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { accountId } = ctx.body;\n\t\t\t\t\tlet targetAccountId = accountId;\n\n\t\t\t\t\tif (!targetAccountId) {\n\t\t\t\t\t\tconst session = ctx.context.session;\n\t\t\t\t\t\tif (!session) {\n\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\tmessage: \"Session required when no accountId provided\",\n\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst nearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t\t{ field: \"isPrimary\", operator: \"eq\", value: true },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tif (!nearAccount) {\n\t\t\t\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\t\t\t\tmessage: \"No NEAR account found for user\",\n\t\t\t\t\t\t\t\tstatus: 404,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\ttargetAccountId = nearAccount.accountId;\n\t\t\t\t\t}\n\n\t\t\t\t\tconst profile = await (options.getProfile || ((id: AccountId) => defaultGetProfile(id, apiKey)))(targetAccountId);\n\t\t\t\t\treturn ctx.json(ProfileResponse.parse(profile));\n\t\t\t\t},\n\t\t\t),\n\t\t\tverifySiwnMessage: createAuthEndpoint(\n\t\t\t\t\"/near/verify\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: VerifyRequest,\n\t\t\t\t\trequireRequest: true,\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst {\n\t\t\t\t\t\tsignedMessage,\n\t\t\t\t\t\tmessage,\n\t\t\t\t\t\trecipient,\n\t\t\t\t\t\tnonce,\n\t\t\t\t\t\taccountId,\n\t\t\t\t\t\tcallbackUrl,\n\t\t\t\t\t} = ctx.body;\n\t\t\t\t\tconst network = getNetworkFromAccountId(accountId);\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst near = getNear(network);\n\t\t\t\t\t\tconst nonceBytes = hex.decode(nonce);\n\n\t\t\t\t\t\tconst isValid = await verifyNep413Signature(\n\t\t\t\t\t\tsignedMessage,\n\t\t\t\t\t\t{ message, recipient, nonce: nonceBytes, callbackUrl },\n\t\t\t\t\t\t\t{ near, maxAge: 15 * 60 * 1000 },\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif (!isValid) {\n\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\tmessage: \"Unauthorized: Invalid signature\",\n\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (signedMessage.accountId !== accountId) {\n\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\tmessage: \"Unauthorized: Account ID mismatch\",\n\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst publicKey = signedMessage.publicKey;\n\n\t\t\t\t\t\tconst nonceHash = await hashNonce(nonceBytes);\n\n\t\t\t\t\t\tconst existingNonce = await ctx.context.internalAdapter.findVerificationValue(\n\t\t\t\t\t\t\t`siwn-nonce:${nonceHash}`\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif (existingNonce) {\n\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\tmessage: \"Unauthorized: Nonce already used (replay attack detected)\",\n\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t\tcode: \"UNAUTHORIZED_NONCE_REPLAY\",\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tawait ctx.context.internalAdapter.createVerificationValue({\n\t\t\t\t\t\t\tidentifier: `siwn-nonce:${nonceHash}`,\n\t\t\t\t\t\t\tvalue: \"used\",\n\t\t\t\t\t\t\texpiresAt: new Date(Date.now() + 15 * 60 * 1000),\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tif (!options.requireFullAccessKey) {\n\t\t\t\t\t\t\tconst validateKey = options.validateLimitedAccessKey\n\t\t\t\t\t\t\t\t|| ((args: { accountId: string; publicKey: string; recipient?: string }) =>\n\t\t\t\t\t\t\t\t\tdefaultValidateLimitedAccessKey(args.accountId, args.publicKey, args.recipient || getRecipient(network), near));\n\n\t\t\t\t\t\t\tconst isValidKey = await validateKey({\n\t\t\t\t\t\t\t\taccountId: accountId,\n\t\t\t\t\t\t\t\tpublicKey: publicKey,\n\t\t\t\t\t\t\t\trecipient: options.recipient\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tif (!isValidKey) {\n\t\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\t\tmessage: \"Unauthorized: Invalid function call access key\",\n\t\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tlet user: User | null = null;\n\n\t\t\t\t\t\tconst existingNearAccount: NearAccount | null =\n\t\t\t\t\t\t\tawait ctx.context.adapter.findOne({\n\t\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"eq\", value: accountId },\n\t\t\t\t\t\t\t\t\t{ field: \"network\", operator: \"eq\", value: network },\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\tif (existingNearAccount) {\n\t\t\t\t\t\t\tuser = await ctx.context.adapter.findOne({\n\t\t\t\t\t\t\t\tmodel: \"user\",\n\t\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\tfield: \"id\",\n\t\t\t\t\t\t\t\t\t\toperator: \"eq\",\n\t\t\t\t\t\t\t\t\t\tvalue: existingNearAccount.userId,\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tconst anyNearAccount: NearAccount | null =\n\t\t\t\t\t\t\t\tawait ctx.context.adapter.findOne({\n\t\t\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t\t\t{ field: \"accountId\", operator: \"eq\", value: accountId },\n\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tif (anyNearAccount) {\n\t\t\t\t\t\t\t\tuser = await ctx.context.adapter.findOne({\n\t\t\t\t\t\t\t\t\tmodel: \"user\",\n\t\t\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\tfield: \"id\",\n\t\t\t\t\t\t\t\t\t\t\toperator: \"eq\",\n\t\t\t\t\t\t\t\t\t\t\tvalue: anyNearAccount.userId,\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (!user) {\n\t\t\t\t\t\t\tconst userEmail = deriveEmail(accountId, getRecipient(network));\n\n\t\t\t\t\t\t\tconst profile = await (options.getProfile || ((id: AccountId) => defaultGetProfile(id, apiKey)))(accountId);\n\n\t\t\t\t\t\t\tuser = await ctx.context.internalAdapter.createUser({\n\t\t\t\t\t\t\t\tname: profile?.name ?? accountId,\n\t\t\t\t\t\t\t\temail: userEmail,\n\t\t\t\t\t\t\t\timage: profile?.image ? getImageUrl(profile.image) : \"\",\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tawait ctx.context.adapter.create({\n\t\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\t\tuserId: user.id,\n\t\t\t\t\t\t\t\t\taccountId,\n\t\t\t\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\t\t\t\tpublicKey: publicKey,\n\t\t\t\t\t\t\t\t\tisPrimary: true,\n\t\t\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\tawait ctx.context.internalAdapter.createAccount({\n\t\t\t\t\t\t\t\tuserId: user.id,\n\t\t\t\t\t\t\t\tproviderId: \"siwn\",\n\t\t\t\t\t\t\t\taccountId: `${accountId}:${network}`,\n\t\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tif (!existingNearAccount) {\n\t\t\t\t\t\t\t\tawait ctx.context.adapter.create({\n\t\t\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\t\t\tuserId: user.id,\n\t\t\t\t\t\t\t\t\t\taccountId,\n\t\t\t\t\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\t\t\t\t\tpublicKey: publicKey,\n\t\t\t\t\t\t\t\t\t\tisPrimary: false,\n\t\t\t\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t});\n\n\t\t\t\t\t\t\t\tawait ctx.context.internalAdapter.createAccount({\n\t\t\t\t\t\t\t\t\tuserId: user.id,\n\t\t\t\t\t\t\t\t\tproviderId: \"siwn\",\n\t\t\t\t\t\t\t\t\taccountId: `${accountId}:${network}`,\n\t\t\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tawait ensureRelayer(ctx.context.adapter, ctx.context.secret, network);\n\n\t\t\t\t\t\tconst session = await ctx.context.internalAdapter.createSession(\n\t\t\t\t\t\t\tuser.id\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif (!session) {\n\t\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\t\tmessage: \"Internal Server Error\",\n\t\t\t\t\t\t\t\tstatus: 500,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tawait setSessionCookie(ctx, { session, user });\n\n\t\t\t\t\t\treturn ctx.json(VerifyResponse.parse({\n\t\t\t\t\t\t\ttoken: session.token,\n\t\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\t\tuser: {\n\t\t\t\t\t\t\t\tid: user.id,\n\t\t\t\t\t\t\t\taccountId,\n\t\t\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t}));\n\t\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\t\tif (error instanceof APIError) throw error;\n\t\t\t\t\t\tconst msg = error instanceof Error ? error.message : \"Unknown error\";\n\t\t\t\t\t\tconst at = error instanceof Error && error.stack ? error.stack.split(\"\\n\").slice(1, 3).map(s => s.trim()).join(\" <- \") : \"\";\n\t\t\t\t\t\tconsole.error(`[siwn] Verify error: ${msg}${at ? ` (${at})` : \"\"}`);\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"Something went wrong. Please try again later.\",\n\t\t\t\t\t\t\terror: msg,\n\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\trelayNearTransaction: createAuthEndpoint(\n\t\t\t\t\"/near/relay\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: RelayRequest,\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { payload } = ctx.body;\n\t\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\t\tif (!session) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"Must be authenticated to relay transactions\",\n\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst nearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t{ field: \"isPrimary\", operator: \"eq\", value: true },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tif (!nearAccount) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"No NEAR account linked to session\",\n\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst network = nearAccount.network as \"mainnet\" | \"testnet\";\n\t\t\t\t\tconst rState = await ensureRelayer(ctx.context.adapter, ctx.context.secret, network);\n\n\t\t\t\t\tif (!rState) {\n\t\t\t\t\t\tthrow new APIError(\"SERVICE_UNAVAILABLE\", {\n\t\t\t\t\t\t\tmessage: \"Relayer not configured\",\n\t\t\t\t\t\t\tstatus: 503,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst decoded: SignedDelegateAction = decodeSignedDelegateAction(payload);\n\t\t\t\t\t\tconst delegateAction = decoded.signedDelegate.delegateAction;\n\n\t\t\t\t\t\tif (delegateAction.senderId !== nearAccount.accountId) {\n\t\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\t\tmessage: \"Delegate action sender does not match session account\",\n\t\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst relayerConfigForNetwork = getRelayerConfig(network);\n\t\t\t\t\t\tif (relayerConfigForNetwork?.whitelistedContracts?.length) {\n\t\t\t\t\t\t\tif (!relayerConfigForNetwork.whitelistedContracts.includes(delegateAction.receiverId)) {\n\t\t\t\t\t\t\t\tthrow new APIError(\"FORBIDDEN\", {\n\t\t\t\t\t\t\t\t\tmessage: `Contract ${delegateAction.receiverId} is not whitelisted for relay`,\n\t\t\t\t\t\t\t\t\tstatus: 403,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (relayerConfigForNetwork?.maxGasPerTransaction) {\n\t\t\t\t\t\t\tconst totalGas = delegateAction.actions.reduce((sum: bigint, a) => {\n\t\t\t\t\t\t\t\treturn sum + (\"functionCall\" in a ? a.functionCall.gas : 0n);\n\t\t\t\t\t\t\t}, 0n);\n\t\t\t\t\t\t\tif (totalGas > BigInt(relayerConfigForNetwork.maxGasPerTransaction)) {\n\t\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\t\tmessage: `Transaction gas (${totalGas}) exceeds relayer limit (${relayerConfigForNetwork.maxGasPerTransaction})`,\n\t\t\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (relayerConfigForNetwork?.maxDepositPerTransaction) {\n\t\t\t\t\t\t\tconst totalDeposit = delegateAction.actions.reduce((sum: bigint, a) => {\n\t\t\t\t\t\t\t\tif (\"functionCall\" in a) return sum + a.functionCall.deposit;\n\t\t\t\t\t\t\t\tif (\"transfer\" in a) return sum + a.transfer.deposit;\n\t\t\t\t\t\t\t\treturn sum;\n\t\t\t\t\t\t\t}, 0n);\n\t\t\t\t\t\t\tif (totalDeposit > BigInt(relayerConfigForNetwork.maxDepositPerTransaction)) {\n\t\t\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\t\t\tmessage: `Transaction deposit (${totalDeposit}) exceeds relayer limit (${relayerConfigForNetwork.maxDepositPerTransaction})`,\n\t\t\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst result = await relayOnChain(payload, rState);\n\n\t\t\t\t\t\tawait ctx.context.adapter.create({\n\t\t\t\t\t\t\tmodel: \"relayedTransaction\",\n\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\tuserId: session.user.id,\n\t\t\t\t\t\t\t\ttxHash: result.txHash,\n\t\t\t\t\t\t\t\tsenderId: delegateAction.senderId,\n\t\t\t\t\t\t\t\treceiverId: delegateAction.receiverId,\n\t\t\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\t\t\tstatus: \"pending\",\n\t\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\n\t\t\t\t\tif (rState.mode === \"ephemeral\") {\n\t\t\t\t\t\tawait ctx.context.adapter.update({\n\t\t\t\t\t\t\tmodel: \"relayerKey\",\n\t\t\t\t\t\t\twhere: [{ field: \"network\", operator: \"eq\", value: network }],\n\t\t\t\t\t\t\tupdate: { lastUsedAt: new Date() },\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\t\treturn ctx.json(RelayResponse.parse({\n\t\t\t\t\t\t\ttxHash: result.txHash,\n\t\t\t\t\t\t\tstatus: \"pending\",\n\t\t\t\t\t\t}));\n\t\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\t\tif (error instanceof APIError) throw error;\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: error instanceof Error ? error.message : \"Relay failed\",\n\t\t\t\t\t\t\tstatus: 500,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\tgetRelayStatus: createAuthEndpoint(\n\t\t\t\t\"/near/relay-status/:txHash\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst txHash = (ctx.params as Record<string, string>)?.txHash;\n\t\t\t\t\tif (!txHash) {\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: \"Transaction hash required\",\n\t\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\t\tconst relayedTx = await ctx.context.adapter.findOne<RelayedTransactionRecord>({\n\t\t\t\t\t\tmodel: \"relayedTransaction\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"txHash\", operator: \"eq\", value: txHash },\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tif (!relayedTx) {\n\t\t\t\t\t\tthrow new APIError(\"NOT_FOUND\", {\n\t\t\t\t\t\t\tmessage: \"Transaction not found or not owned by this user\",\n\t\t\t\t\t\t\tstatus: 404,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst network = relayedTx.network;\n\t\t\t\t\tconst senderId = relayedTx.senderId;\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst near = getNear(network);\n\t\t\t\t\t\tconst txResult = await near.getTransactionStatus(txHash, senderId);\n\n\t\t\t\t\t\tconst txStatus = txResult.status;\n\t\t\t\t\t\tif (txStatus && typeof txStatus === \"object\") {\n\t\t\t\t\t\t\tconst hasSuccess = \"SuccessValue\" in txStatus || \"SuccessReceiptId\" in txStatus;\n\t\t\t\t\t\t\tconst hasFailure = \"Failure\" in txStatus;\n\t\t\t\t\t\t\tconst status = hasSuccess ? \"completed\" : hasFailure ? \"failed\" : \"pending\";\n\n\t\t\t\t\t\t\tif (status !== \"pending\") {\n\t\t\t\t\t\t\t\tawait ctx.context.adapter.update({\n\t\t\t\t\t\t\t\t\tmodel: \"relayedTransaction\",\n\t\t\t\t\t\t\t\t\twhere: [{ field: \"txHash\", operator: \"eq\", value: txHash }],\n\t\t\t\t\t\t\t\t\tupdate: { status },\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst gasUsed = txResult.transaction_outcome?.outcome?.gas_burnt?.toString();\n\n\t\t\t\t\t\t\treturn ctx.json(RelayStatusResponse.parse({\n\t\t\t\t\t\t\t\tstatus,\n\t\t\t\t\t\t\t\tgasUsed,\n\t\t\t\t\t\t\t\toutcome: txResult,\n\t\t\t\t\t\t\t}));\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn ctx.json(RelayStatusResponse.parse({ status: \"pending\" }));\n\t\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\t\treturn ctx.json(RelayStatusResponse.parse({ status: \"pending\" }));\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t),\n\t\t\tgetRelayerInfo: createAuthEndpoint(\n\t\t\t\t\"/near/relayer-info\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: GetRelayerInfoRequest,\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { network: requestedNetwork } = ctx.body;\n\t\t\t\t\tconst targetNetwork = (requestedNetwork ?? primaryNetwork) as \"mainnet\" | \"testnet\";\n\t\t\t\t\tconst rState = await ensureRelayer(ctx.context.adapter, ctx.context.secret, targetNetwork);\n\n\t\t\t\t\tif (!rState) {\n\t\t\t\t\t\treturn ctx.json({ enabled: false, subAccountAvailable: false } satisfies Partial<RelayerInfo> & { enabled: boolean });\n\t\t\t\t\t}\n\n\t\t\t\t\tconst subAccountCfg = getSubAccountConfig(targetNetwork);\n\t\t\t\t\tconst parentKey = getParentKey(targetNetwork);\nconst { parentAccount, subAccountAvailable } = resolveParentAccount(subAccountCfg, parentKey, rState);\n\n\t\t\t\t\tconst near = getNear(rState.network);\n\t\t\t\t\tlet account;\n\t\t\t\t\ttry {\n\t\t\t\t\t\taccount = await near.getAccount(rState.accountId);\n\t\t\t\t\t} catch {\n\t\t\t\t\t\taccount = {\n\t\t\t\t\t\t\tbalance: \"0\",\n\t\t\t\t\t\t\tavailable: \"0\",\n\t\t\t\t\t\t\tstaked: \"0\",\n\t\t\t\t\t\t\tstorageUsage: \"0\",\n\t\t\t\t\t\t\tstorageBytes: 0,\n\t\t\t\t\t\t\thasContract: false,\n\t\t\t\t\t\t\tcodeHash: \"\",\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\tenabled: true,\n\t\t\t\t\t\taccountId: rState.accountId,\n\t\t\t\t\t\tmode: rState.mode,\n\t\t\t\t\t\tnetwork: rState.network,\n\t\t\t\t\t\tpublicKey: rState.publicKey,\n\t\t\t\t\t\tbalance: account.balance,\n\t\t\t\t\t\tavailable: account.available,\n\t\t\t\t\t\tstaked: account.staked,\n\t\t\t\t\t\tstorageUsage: account.storageUsage,\n\t\t\t\t\t\tstorageBytes: account.storageBytes,\n\t\t\t\t\t\thasContract: account.hasContract,\n\t\t\t\t\t\thasKey: true,\n\t\t\t\t\t\tcreatedAt: rState.createdAt,\n\t\t\t\t\t\tlastUsedAt: rState.lastUsedAt,\n\t\t\t\t\t\tparentAccount: parentAccount ?? undefined,\n\t\t\t\t\t\tsubAccountAvailable,\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t),\n\t\t\tgetRelayHistory: createAuthEndpoint(\n\t\t\t\t\"/near/relay-history\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"GET\",\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\t\tlet transactions: RelayedTransactionRecord[] = [];\n\t\t\t\t\ttry {\n\t\t\t\t\t\tconst result = await ctx.context.adapter.findMany<RelayedTransactionRecord>({\n\t\t\t\t\t\t\tmodel: \"relayedTransaction\",\n\t\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t],\n\t\t\t\t\t\t});\n\t\t\t\t\t\ttransactions = result || [];\n\t\t\t\t\t} catch (err) {\n\t\t\t\t\t\tconsole.error(\"relay-history findMany error:\", err);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst sorted = transactions.sort((a, b) => {\n\t\t\t\t\t\tconst aTime = a.createdAt instanceof Date ? a.createdAt.getTime() : new Date(a.createdAt ?? 0).getTime();\n\t\t\t\t\t\tconst bTime = b.createdAt instanceof Date ? b.createdAt.getTime() : new Date(b.createdAt ?? 0).getTime();\n\t\t\t\t\t\treturn bTime - aTime;\n\t\t\t\t\t});\n\n\t\t\t\t\treturn ctx.json({\n\t\t\t\t\t\ttransactions: sorted.map((tx) => ({\n\t\t\t\t\t\t\tid: String(tx.id ?? \"\"),\n\t\t\t\t\t\t\tuserId: String(tx.userId ?? \"\"),\n\t\t\t\t\t\t\ttxHash: String(tx.txHash ?? \"\"),\n\t\t\t\t\t\t\tsenderId: String(tx.senderId ?? \"\"),\n\t\t\t\t\t\t\treceiverId: String(tx.receiverId ?? \"\"),\n\t\t\t\t\t\t\tnetwork: String(tx.network ?? \"mainnet\"),\n\t\t\t\t\t\t\tstatus: String(tx.status ?? \"pending\"),\n\t\t\t\t\t\t\tgasUsed: tx.gasUsed ? String(tx.gasUsed) : undefined,\n\t\t\t\t\t\t\tcreatedAt: tx.createdAt instanceof Date ? tx.createdAt.toISOString() : tx.createdAt ? String(tx.createdAt) : new Date().toISOString(),\n\t\t\t\t\t\t\tupdatedAt: tx.updatedAt instanceof Date ? tx.updatedAt.toISOString() : tx.updatedAt ? String(tx.updatedAt) : undefined,\n\t\t\t\t\t\t})),\n\t\t\t\t\t});\n\t\t\t\t},\n\t\t\t),\n\t\t\tviewContract: createAuthEndpoint(\n\t\t\t\t\"/near/view\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: ViewContractRequest,\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { contractId, methodName, args } = ctx.body;\n\t\t\t\t\tconst session = ctx.context.session;\n\t\t\t\t\tconst nearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t{ field: \"isPrimary\", operator: \"eq\", value: true },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tconst network = (nearAccount?.network || primaryNetwork) as \"mainnet\" | \"testnet\";\n\t\t\t\t\tconst near = getNear(network);\n\t\t\t\t\tlet result: unknown;\n\t\t\t\t\ttry {\n\t\t\t\t\t\tresult = await near.view(contractId, methodName, args ?? {});\n\t\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\t\tif (error instanceof APIError) throw error;\n\t\t\t\t\t\tthrow new APIError(\"BAD_REQUEST\", {\n\t\t\t\t\t\t\tmessage: error instanceof Error ? error.message : \"Unknown error\",\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\treturn ctx.json(ViewContractResponse.parse({ result }));\n\t\t\t\t},\n\t\t\t),\n\t\t\tcreateSubAccount: createAuthEndpoint(\n\t\t\t\t\"/near/create-sub-account\",\n\t\t\t\t{\n\t\t\t\t\tmethod: \"POST\",\n\t\t\t\t\tbody: CreateSubAccountRequest,\n\t\t\t\t\tuse: [sessionMiddleware],\n\t\t\t\t},\n\t\t\t\tasync (ctx) => {\n\t\t\t\t\tconst { subAccountName, network: providedNetwork, publicKey } = ctx.body;\n\t\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\t\tif (!session) {\n\t\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\t\tmessage: \"Must be authenticated to create a sub-account\",\n\t\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst nearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\twhere: [\n\t\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t\t{ field: \"isPrimary\", operator: \"eq\", value: true },\n\t\t\t\t\t\t],\n\t\t\t\t\t});\n\n\t\t\t\t\tconst network = (providedNetwork || nearAccount?.network || primaryNetwork) as \"mainnet\" | \"testnet\";\n\t\t\t\t\tconst subAccountCfg = getSubAccountConfig(network);\n\t\t\t\t\tconst userAccountId = nearAccount?.accountId ?? \"\";\n\t\t\t\t\tconst parentKey = getParentKey(network);\n\t\t\t\t\tconst rState = await ensureRelayer(ctx.context.adapter, ctx.context.secret, network);\n\t\t\t\t\tconst { parentAccount, subAccountAvailable } = resolveParentAccount(subAccountCfg, parentKey, rState);\n\n\t\t\t\t\tif (!subAccountAvailable || !parentAccount) {\n\t\t\t\t\t\tconst configuredParent = subAccountCfg?.parentAccount;\n\t\t\t\t\t\tlet msg: string;\n\t\t\t\t\t\tif (configuredParent && !parentKey) {\n\t\t\t\t\t\t\tmsg = `Sub-account creation unavailable on ${network}: parent key not configured for ${configuredParent}. Set NEAR_SUB_ACCOUNT_PARENT_KEY_${network.toUpperCase()} in your environment, or use an explicit relayer with a named account.`;\n\t\t\t\t\t\t} else if (!configuredParent) {\n\t\t\t\t\t\t\tmsg = `Sub-account creation unavailable on ${network}: no parent account configured. Set subAccount.${network}.parentAccount in your SIWN plugin options, or use an explicit relayer with a named account.`;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tmsg = `Sub-account creation unavailable on ${network}: requires a named parent account with a parent key, or an explicit relayer with a named account.`;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthrow new APIError(\"SERVICE_UNAVAILABLE\", {\n\t\t\t\t\t\t\tmessage: msg,\n\t\t\t\t\t\t\tstatus: 503,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst effectiveParentKey = getRawParentKey(network);\n\t\t\t\t\tif (parentAccount !== rState?.accountId && !effectiveParentKey) {\n\t\t\t\t\t\tthrow new APIError(\"SERVICE_UNAVAILABLE\", {\n\t\t\t\t\t\t\tmessage: \"Sub-account parent differs from relayer account. Provide secrets.parentKey to sign as the parent account.\",\n\t\t\t\t\t\t\tstatus: 503,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst newAccountId = `${subAccountName}.${parentAccount}`;\n\t\t\t\t\tconst minDeposit = subAccountCfg?.minDeposit || \"0.1 NEAR\";\n\n\t\t\t\t\tconst near = getNear(network);\n\t\t\t\t\tconst exists = await near.accountExists(newAccountId);\n\t\t\t\t\tif (exists) {\n\t\t\t\t\t\tthrow new APIError(\"CONFLICT\", {\n\t\t\t\t\t\t\tmessage: `Account ${newAccountId} already exists on ${network}`,\n\t\t\t\t\t\t\tstatus: 409,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\tconst txCtx: SubAccountTxCtx = {\n\t\t\t\t\t\tnewAccountId,\n\t\t\t\t\t\tparentAccount,\n\t\t\t\t\t\tuserPublicKey: publicKey,\n\t\t\t\t\t\tuserAccountId,\n\t\t\t\t\t\tuserId: session.user.id,\n\t\t\t\t\t\tnetwork,\n\t\t\t\t\t};\n\n\t\t\t\t\tconst signingNear = rState?.near ?? near;\n\t\t\t\t\tlet txBuilder = signingNear\n\t\t\t\t\t\t.transaction(parentAccount)\n\t\t\t\t\t\t.createAccount(newAccountId)\n\t\t\t\t\t\t.addKey(publicKey, { type: \"fullAccess\" });\n\n\t\t\t\t\tif (subAccountCfg?.parentHasFullAccess) {\n\t\t\t\t\t\tconst parentPublicKey = parentAccount === rState?.accountId\n\t\t\t\t\t\t\t? rState.publicKey\n\t\t\t\t\t\t\t: parseKey(effectiveParentKey!).publicKey.toString();\n\t\t\t\t\t\ttxBuilder = txBuilder.addKey(parentPublicKey, { type: \"fullAccess\" });\n\t\t\t\t\t}\n\n\t\t\t\t\tif (subAccountCfg?.addRelayerFCAK !== false && subAccountCfg?.relayerFCAK && rState) {\n\t\t\t\t\t\tconst fcakPermission: { type: \"functionCall\"; receiverId: string; methodNames?: string[]; allowance?: string } = {\n\t\t\t\t\t\t\ttype: \"functionCall\",\n\t\t\t\t\t\t\treceiverId: subAccountCfg.relayerFCAK.receiverId,\n\t\t\t\t\t\t};\n\t\t\t\t\t\tif (subAccountCfg.relayerFCAK.methodNames) {\n\t\t\t\t\t\t\tfcakPermission.methodNames = subAccountCfg.relayerFCAK.methodNames;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (subAccountCfg.relayerFCAK.allowance) {\n\t\t\t\t\t\t\tfcakPermission.allowance = subAccountCfg.relayerFCAK.allowance as `${number} NEAR` | `${bigint} yocto`;\n\t\t\t\t\t\t}\n\t\t\t\t\t\ttxBuilder.addKey(rState.publicKey, fcakPermission as any);\n\t\t\t\t\t}\n\n\t\t\t\t\ttxBuilder.transfer(newAccountId, minDeposit as `${number} NEAR`);\n\n\t\t\t\t\tif (subAccountCfg?.deploy) {\n\t\t\t\t\t\tif (\"wasm\" in subAccountCfg.deploy) {\n\t\t\t\t\t\t\ttxBuilder = txBuilder.deployContract(newAccountId, subAccountCfg.deploy.wasm);\n\t\t\t\t\t\t} else if (\"fromPublished\" in subAccountCfg.deploy) {\n\t\t\t\t\t\t\ttxBuilder = txBuilder.deployFromPublished(subAccountCfg.deploy.fromPublished as any);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tif (subAccountCfg?.init) {\n\t\t\t\t\t\tconst initArgs = typeof subAccountCfg.init.args === \"function\"\n\t\t\t\t\t\t\t? subAccountCfg.init.args(txCtx)\n\t\t\t\t\t\t\t: subAccountCfg.init.args;\n\t\t\t\t\t\ttxBuilder = txBuilder.functionCall(newAccountId, subAccountCfg.init.methodName, initArgs);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (subAccountCfg?.extendTx) {\n\t\t\t\t\t\ttxBuilder = subAccountCfg.extendTx(txBuilder, txCtx) as typeof txBuilder;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (effectiveParentKey) {\n\t\t\t\t\t\ttxBuilder = txBuilder.signWith(effectiveParentKey as PrivateKey);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst cleanupDb = async () => {\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tconst existing = await ctx.context.adapter.findOne<{ id: string }>({\n\t\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\t\twhere: [{ field: \"accountId\", operator: \"eq\", value: newAccountId }],\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tif (existing) {\n\t\t\t\t\t\t\t\tawait ctx.context.adapter.delete({ model: \"nearAccount\", where: [{ field: \"id\", operator: \"eq\", value: existing.id }] });\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t} catch { }\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tawait ctx.context.internalAdapter.deleteAccount(`${newAccountId}:${network}`);\n\t\t\t\t\t\t} catch { }\n\t\t\t\t\t};\n\n\t\t\t\t\tconst lifecycleCtx: SubAccountLifecycleCtx = { ...txCtx, near: signingNear };\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait txBuilder.send({ waitUntil: \"EXECUTED\" });\n\t\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\t\tconst msg = error instanceof Error ? error.message : \"Unknown error\";\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: `Failed to create sub-account: ${msg}`,\n\t\t\t\t\t\t\tstatus: 500,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\ttry {\n\t\t\t\t\t\tawait ctx.context.adapter.create({\n\t\t\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\tuserId: session.user.id,\n\t\t\t\t\t\t\t\taccountId: newAccountId,\n\t\t\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\t\t\tpublicKey,\n\t\t\t\t\t\t\t\tisPrimary: false,\n\t\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tawait ctx.context.internalAdapter.createAccount({\n\t\t\t\t\t\t\tuserId: session.user.id,\n\t\t\t\t\t\t\tproviderId: \"siwn\",\n\t\t\t\t\t\t\taccountId: `${newAccountId}:${network}`,\n\t\t\t\t\t\t\tcreatedAt: new Date(),\n\t\t\t\t\t\t\tupdatedAt: new Date(),\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tif (subAccountCfg?.onCreated) {\n\t\t\t\t\t\t\tawait subAccountCfg.onCreated(lifecycleCtx);\n\t\t\t\t\t\t}\n\t\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\t\tawait cleanupDb();\n\t\t\t\t\t\tif (subAccountCfg?.onRollback) {\n\t\t\t\t\t\t\ttry { await subAccountCfg.onRollback(lifecycleCtx); } catch { }\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\t\tconst key = getRawParentKey(network);\n\t\t\t\t\t\t\t\tif (key) {\n\t\t\t\t\t\t\t\t\tconst rollbackNear = getNear(network);\n\t\t\t\t\t\t\t\t\tawait rollbackNear\n\t\t\t\t\t\t\t\t\t\t.transaction(newAccountId)\n\t\t\t\t\t\t\t\t\t\t.deleteAccount({ beneficiary: parentAccount })\n\t\t\t\t\t\t\t\t\t\t.signWith(key as PrivateKey)\n\t\t\t\t\t\t\t\t\t\t.send({ waitUntil: \"EXECUTED\" });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t} catch { }\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst msg = error instanceof Error ? error.message : \"Unknown error\";\n\t\t\t\t\t\tthrow new APIError(\"INTERNAL_SERVER_ERROR\", {\n\t\t\t\t\t\t\tmessage: `Sub-account created but post-creation failed: ${msg}`,\n\t\t\t\t\t\t\tstatus: 500,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn ctx.json(CreateSubAccountResponse.parse({\n\t\t\t\t\t\tsuccess: true,\n\t\t\t\t\t\taccountId: newAccountId,\n\t\t\t\t\t\tnetwork,\n\t\t\t\t\t\tpublicKey,\n\t\t\t\t\t\tmessage: `Sub-account ${newAccountId} created on ${network}`,\n\t\t\t\t\t}));\n\t\t\t\t},\n\t\t\t),\n\t\tcheckSubAccountAvailability: createAuthEndpoint(\n\t\t\t\"/near/check-sub-account-availability\",\n\t\t\t{\n\t\t\t\tmethod: \"POST\",\n\t\t\t\tbody: CheckSubAccountAvailabilityRequest,\n\t\t\t\tuse: [sessionMiddleware],\n\t\t\t},\n\t\t\tasync (ctx) => {\n\t\t\t\tconst { subAccountName, network: providedNetwork } = ctx.body;\n\t\t\t\tconst session = ctx.context.session;\n\n\t\t\t\tif (!session) {\n\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\tmessage: \"Must be authenticated to check sub-account availability\",\n\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst nearAccount: NearAccount | null = await ctx.context.adapter.findOne({\n\t\t\t\t\tmodel: \"nearAccount\",\n\t\t\t\t\twhere: [\n\t\t\t\t\t\t{ field: \"userId\", operator: \"eq\", value: session.user.id },\n\t\t\t\t\t\t{ field: \"isPrimary\", operator: \"eq\", value: true },\n\t\t\t\t\t],\n\t\t\t\t});\n\n\t\t\t\tif (!nearAccount) {\n\t\t\t\t\tthrow new APIError(\"UNAUTHORIZED\", {\n\t\t\t\t\t\tmessage: \"Must have a linked NEAR wallet to check sub-account availability\",\n\t\t\t\t\t\tstatus: 401,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst network = (providedNetwork || nearAccount.network || primaryNetwork) as \"mainnet\" | \"testnet\";\n\t\t\t\tconst subAccountCfg = getSubAccountConfig(network);\n\t\t\t\tconst parentAccount = subAccountCfg?.parentAccount ?? null;\n\n\t\t\t\tif (!parentAccount || isImplicitAccount(parentAccount)) {\n\t\t\t\t\treturn ctx.json(CheckSubAccountAvailabilityResponse.parse({\n\t\t\t\t\t\tavailable: false,\n\t\t\t\t\t\taccountId: \"\",\n\t\t\t\t\t\treason: \"not-configured\",\n\t\t\t\t\t}));\n\t\t\t\t}\n\n\t\t\t\tconst accountId = `${subAccountName}.${parentAccount}`;\n\n\t\t\t\tif (accountId.length > 64) {\n\t\t\t\t\treturn ctx.json(CheckSubAccountAvailabilityResponse.parse({\n\t\t\t\t\t\tavailable: false,\n\t\t\t\t\t\taccountId,\n\t\t\t\t\t\tparentAccount,\n\t\t\t\t\t\treason: \"too-long\",\n\t\t\t\t\t}));\n\t\t\t\t}\n\n\t\t\t\tconst near = getNear(network);\n\t\t\t\tconst exists = await near.accountExists(accountId);\n\n\t\t\t\treturn ctx.json(CheckSubAccountAvailabilityResponse.parse({\n\t\t\t\t\tavailable: !exists,\n\t\t\t\t\taccountId,\n\t\t\t\t\tparentAccount,\n\t\t\t\t\treason: exists ? \"taken\" : undefined,\n\t\t\t\t}));\n\t\t\t},\n\t\t),\n\t\t},\n\t});\n};\n"],"mappings":";;;;;;;AAEA,MAAM,eACL;AAED,SAAS,wBAAwB,WAA0C;CAC1E,OAAO,UAAU,SAAS,UAAU,IAAI,YAAY;AACrD;AAEA,SAAS,YACR,OACA,UACS;CACT,IAAI,OAAO,KAAK,OAAO,MAAM;CAC7B,IAAI,OAAO,UAAU,OAAO,iCAAiC,MAAM;CACnE,OAAO,YAAY;AACpB;AAmBA,eAAe,kBAAkB,WAAsB,QAA0C;CAChG,MAAM,U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ACzE,OAAO;KACP,OAAO,CACN;MAAE,OAAO;MAAU,UAAU;MAAM,OAAO,QAAQ,KAAK;KAAG,GAC1D;MAAE,OAAO;MAAa,UAAU;MAAM,OAAO;KAAK,CACnD;IACD,CAAC;IAED,IAAI,CAAC,aACJ,MAAM,IAAI,SAAS,gBAAgB;KAClC,SAAS;KACT,QAAQ;IACT,CAAC;IAGF,MAAM,UAAW,mBAAmB,YAAY,WAAW;IAE3D,MAAM,gBADgB,oBAAoB,OACR,GAAG,iBAAiB;IAEtD,IAAI,CAAC,iBAAiB,kBAAkB,aAAa,GACpD,OAAO,IAAI,KAAK,oCAAoC,MAAM;KACzD,WAAW;KACX,WAAW;KACX,QAAQ;IACT,CAAC,CAAC;IAGH,MAAM,YAAY,GAAG,eAAe,GAAG;IAEvC,IAAI,UAAU,SAAS,IACtB,OAAO,IAAI,KAAK,oCAAoC,MAAM;KACzD,WAAW;KACX;KACA;KACA,QAAQ;IACT,CAAC,CAAC;IAIH,MAAM,SAAS,MADF,QAAQ,OACG,EAAE,cAAc,SAAS;IAEjD,OAAO,IAAI,KAAK,oCAAoC,MAAM;KACzD,WAAW,CAAC;KACZ;KACA;KACA,QAAQ,SAAS,UAAU,KAAA;IAC5B,CAAC,CAAC;GACH,CACD;EACA;CACD;AACD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "better-near-auth",
3
- "version": "1.7.0",
3
+ "version": "1.7.1",
4
4
  "description": "Sign in with NEAR (SIWN) plugin for Better Auth",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -6,9 +6,11 @@ description: >
6
6
  client into the UI with siwnClient/passkey/API-key/organization plugins,
7
7
  protect routes with session checks, compose with the auth plugin in-process
8
8
  via createPlugin.withPlugins, and use the auth context (getContext) in your
9
- own oRPC middleware. Load when adding auth to an everything.dev app,
10
- configuring SIWN recipients from runtime config, calling auth endpoints from
11
- another plugin, or debugging auth context resolution.
9
+ own oRPC middleware. Sub-account creation is supported in bos.config.json
10
+ for scalar fields (parentHasFullAccess, minDeposit, deploy.fromPublished,
11
+ init with static args, addRelayerFCAK, relayerFCAK). Load when adding auth
12
+ to an everything.dev app, configuring SIWN recipients from runtime config,
13
+ calling auth endpoints from another plugin, or debugging auth context resolution.
12
14
  requires:
13
15
  - siwn
14
16
  - client
@@ -35,7 +37,7 @@ sources:
35
37
 
36
38
  The `@everything-dev/auth-plugin` wraps `better-near-auth` and Better Auth into an everything-plugin (oRPC + Effect + Module Federation). It provides session management, NEAR SIWN, passkeys, OAuth, phone OTP, anonymous accounts, organizations, API keys, and a NEAR relayer — all behind a typed oRPC contract.
37
39
 
38
- This skill covers **consuming** the plugin: registration, UI auth client, route protection, in-process composition, and the auth context. See also `better-near-auth#siwn`, `better-near-auth#client`, `better-near-auth#relay` for the underlying plugin config.
40
+ This skill covers **consuming** the plugin: registration, UI auth client, route protection, in-process composition, and the auth context. See also `better-near-auth#siwn`, `better-near-auth#client`, `better-near-auth#relay`, `better-near-auth#subaccount` for the underlying plugin config.
39
41
 
40
42
  ## Registering in bos.config.json
41
43
 
@@ -57,10 +59,22 @@ Deployed under `app.auth` as a Module Federation container:
57
59
  "mainnet": "auth.everything.near",
58
60
  "testnet": "dev.allthethings.testnet"
59
61
  },
60
- "relayer": { "accountId": "" }
62
+ "relayer": { "accountId": "" },
63
+ "subAccount": {
64
+ "mainnet": {
65
+ "parentAccount": "myapp.near",
66
+ "parentHasFullAccess": true,
67
+ "minDeposit": "0.1 NEAR",
68
+ "deploy": { "fromPublished": { "accountId": "myapp.near" } },
69
+ "init": { "methodName": "init", "args": { "owner": "myapp.near" } }
70
+ },
71
+ "testnet": {
72
+ "parentAccount": "dev.myapp.testnet"
73
+ }
74
+ }
61
75
  }
62
76
  },
63
- "secrets": ["AUTH_DATABASE_URL", "BETTER_AUTH_SECRET", "GITHUB_CLIENT_SECRET", "FASTNEAR_API_KEY"]
77
+ "secrets": ["AUTH_DATABASE_URL", "BETTER_AUTH_SECRET", "GITHUB_CLIENT_SECRET", "FASTNEAR_API_KEY", "NEAR_SUB_ACCOUNT_PARENT_KEY_MAINNET", "NEAR_SUB_ACCOUNT_PARENT_KEY_TESTNET"]
64
78
  }
65
79
  }
66
80
  }
@@ -114,6 +128,17 @@ _authenticated layout pattern: use `sessionQueryOptions` to fetch session, `ensu
114
128
 
115
129
  The `auth.near.*`, `auth.organization.*`, `auth.passkey.*`, `auth.apiKey.*` namespaces are available. See `better-near-auth#client` for `authClient.near.*` API.
116
130
 
131
+ ### Sub-Account Creation
132
+
133
+ The plugin supports sub-account creation and availability checks through the Better Auth client and the oRPC contract:
134
+
135
+ | Operation | Client call | oRPC route |
136
+ |-----------|-------------|------------|
137
+ | Check availability | `auth.near.checkSubAccountAvailability({ subAccountName })` | `POST /v1/near/check-sub-account-availability` |
138
+ | Create sub-account | `auth.near.createSubAccount({ subAccountName, publicKey })` | `POST /v1/near/create-sub-account` |
139
+
140
+ Pass `network: "mainnet" | "testnet"` optionally. Both require authentication. Sub-account config (`parentAccount`, `parentHasFullAccess`, `minDeposit`, etc.) is set via `bos.config.json` `variables.siwn.subAccount` — see `better-near-auth#subaccount` for the full config reference and which fields are serializable through JSON config.
141
+
117
142
  ## Contract and Types
118
143
 
119
144
  Import the contract for type-safe calls: `@everything-dev/auth-plugin/contract` (exports `ContractType`, `InferOutput`, `InferInput`). Auth instance types from `@everything-dev/auth-plugin` (exports `Auth`, `AuthServices`, `AuthConfig`).
@@ -9,7 +9,7 @@ description: >
9
9
  metadata:
10
10
  type: core
11
11
  library: better-near-auth
12
- library_version: "1.7.0"
12
+ library_version: "1.7.1"
13
13
  sources:
14
14
  - "elliotBraem/better-near-auth:src/client.ts"
15
15
  - "elliotBraem/better-near-auth:src/types.ts"
@@ -9,7 +9,7 @@ description: >
9
9
  metadata:
10
10
  type: core
11
11
  library: better-near-auth
12
- library_version: "1.7.0"
12
+ library_version: "1.7.1"
13
13
  sources:
14
14
  - "elliotBraem/better-near-auth:src/index.ts"
15
15
  - "elliotBraem/better-near-auth:src/utils.ts"
@@ -9,7 +9,7 @@ description: >
9
9
  metadata:
10
10
  type: core
11
11
  library: better-near-auth
12
- library_version: "1.7.0"
12
+ library_version: "1.7.1"
13
13
  sources:
14
14
  - "elliotBraem/better-near-auth:src/index.ts"
15
15
  - "elliotBraem/better-near-auth:src/profile.ts"
@@ -318,6 +318,61 @@ const result = await authClient.near.createSubAccount({
318
318
  console.log(result.data.accountId); // myapp.parent.near
319
319
  ```
320
320
 
321
+ ## Plugin Config (everything.dev)
322
+
323
+ When using the `@everything-dev/auth-plugin`, the following `SubAccountConfig` fields can be set through `bos.config.json` `variables.siwn.subAccount`. Fields that accept functions (`extendTx`, `onCreated`, `onRollback`, dynamic `init.args`) or binary data (`deploy.wasm`) are not serializable through JSON config — use the raw `better-near-auth` library for those.
324
+
325
+ ```json
326
+ {
327
+ "app": {
328
+ "auth": {
329
+ "variables": {
330
+ "siwn": {
331
+ "subAccount": {
332
+ "mainnet": {
333
+ "parentAccount": "myapp.near",
334
+ "parentHasFullAccess": true,
335
+ "minDeposit": "0.1 NEAR",
336
+ "deploy": { "fromPublished": { "accountId": "myapp.near" } },
337
+ "init": { "methodName": "init", "args": { "owner": "myapp.near" } },
338
+ "addRelayerFCAK": true,
339
+ "relayerFCAK": {
340
+ "receiverId": "myapp.near",
341
+ "methodNames": ["*"],
342
+ "allowance": "0.25 NEAR"
343
+ }
344
+ },
345
+ "testnet": {
346
+ "parentAccount": "dev.myapp.testnet"
347
+ }
348
+ }
349
+ }
350
+ },
351
+ "secrets": [
352
+ "NEAR_SUB_ACCOUNT_PARENT_KEY_MAINNET",
353
+ "NEAR_SUB_ACCOUNT_PARENT_KEY_TESTNET"
354
+ ]
355
+ }
356
+ }
357
+ }
358
+ ```
359
+
360
+ The plugin's server code forwards these to the `siwn()` plugin's `subAccount` config automatically. The parent keys go in `secrets` — see `better-near-auth#auth-plugin` for plugin registration details.
361
+
362
+ ### Available vs. unavailable fields via plugin config
363
+
364
+ | Scalar (available in bos.config.json) | Non-scalar (not serializable) |
365
+ |---|---|
366
+ | `parentAccount` | `deploy.wasm` |
367
+ | `parentHasFullAccess` | `init.args` (dynamic — function) |
368
+ | `minDeposit` | `extendTx` |
369
+ | `deploy.fromPublished` | `onCreated` |
370
+ | `init` (static `args` object) | `onRollback` |
371
+ | `addRelayerFCAK` | |
372
+ | `relayerFCAK` | |
373
+
374
+ For non-scalar fields, configure `siwn()` directly on the server instead of using the plugin config.
375
+
321
376
  ## Common Mistakes
322
377
 
323
378
  ### HIGH Not setting parentHasFullAccess when parent needs recovery
@@ -13,7 +13,7 @@ requires:
13
13
  metadata:
14
14
  type: framework
15
15
  library: better-near-auth
16
- library_version: "1.7.0"
16
+ library_version: "1.7.1"
17
17
  sources:
18
18
  - "elliotBraem/better-near-auth:src/client.ts"
19
19
  - "elliotBraem/better-near-auth:examples/auth.everything.dev/ui/src/lib/auth.ts"