From 005a0fbde199efe7034b0da266b4d832fdd844e2 Mon Sep 17 00:00:00 2001 From: Mahmoud Aboelenein Date: Mon, 28 Oct 2024 10:22:48 +0300 Subject: [PATCH] implement only necessary types --- src/request/types/permissions/index.ts | 45 ----------- .../types/permissions/resources/account.ts | 62 ---------------- .../types/permissions/resources/common.ts | 11 --- .../types/permissions/resources/index.ts | 43 ----------- .../types/permissions/resources/wallet.ts | 28 ------- src/request/types/permissions/store.ts | 74 ------------------- .../types/permissions/utils/account-id.ts | 36 --------- src/request/types/permissions/utils/index.ts | 7 -- src/request/types/walletMethods.ts | 40 +++++++--- 9 files changed, 31 insertions(+), 315 deletions(-) delete mode 100644 src/request/types/permissions/index.ts delete mode 100644 src/request/types/permissions/resources/account.ts delete mode 100644 src/request/types/permissions/resources/common.ts delete mode 100644 src/request/types/permissions/resources/index.ts delete mode 100644 src/request/types/permissions/resources/wallet.ts delete mode 100644 src/request/types/permissions/store.ts delete mode 100644 src/request/types/permissions/utils/account-id.ts delete mode 100644 src/request/types/permissions/utils/index.ts diff --git a/src/request/types/permissions/index.ts b/src/request/types/permissions/index.ts deleted file mode 100644 index 5ed341b..0000000 --- a/src/request/types/permissions/index.ts +++ /dev/null @@ -1,45 +0,0 @@ -import { account, common, wallet } from './resources/index'; -export const resources = { - account, - common, - wallet, -}; -export type * as Resources from './resources/index'; - -import { account as accountUtils } from './utils/index'; -export const utils = { - account: accountUtils, -}; -export type * as Utils from './utils/index'; - -import { - clientId, - client, - clientMetadata, - clientMetadataTable, - clientsTable, - permission, - permissionsStore, - permissionsTable, - resource, - resourcesTable, -} from './store'; -export const store = { - clientId, - client, - clientMetadata, - clientMetadataTable, - clientsTable, - resource, - resourcesTable, - permission, - permissionsTable, - permissionsStore, -}; -export type * as Store from './store'; - -export const permissions = { - resources, - utils, - store, -}; diff --git a/src/request/types/permissions/resources/account.ts b/src/request/types/permissions/resources/account.ts deleted file mode 100644 index b6bc800..0000000 --- a/src/request/types/permissions/resources/account.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { sha256 } from '@noble/hashes/sha256'; -import { bytesToHex } from '@noble/hashes/utils'; -import * as v from 'valibot'; -import { actionDescriptionSchema } from './common'; -import { AccountId, accountIdSchema } from '../utils/account-id'; -import { BitcoinNetworkType } from 'src/types'; - -export type AccountResourceIdArgs = { - masterPubKey: string; - accountId: AccountId; - networkType: BitcoinNetworkType; -}; - -export const accountResourceIdBrandName = 'AccountResourceId'; - -const sha256HexStringRegex = /^[\da-f]{64}$/; -export const accountResourceIdSchema = v.pipe( - v.string(), - v.check((input) => sha256HexStringRegex.test(input)), - v.brand('AccountResourceId') -); - -export type AccountResourceId = v.InferOutput; - -export function makeAccountResourceId(accountId: AccountId) { - return v.parse(accountResourceIdSchema, bytesToHex(sha256(`account-resource-${accountId}`))); -} - -export const accountResourceSchema = v.object({ - type: v.literal('account'), - id: accountResourceIdSchema, - accountId: accountIdSchema, - name: v.string(), -}); -export type AccountResource = v.InferOutput; - -export function makeAccountResource(args: AccountResourceIdArgs): AccountResource { - return { - type: 'account', - id: makeAccountResourceId(args.accountId), - accountId: args.accountId, - name: `Account ${args.accountId}, ${args.masterPubKey.slice(0, 6)}...(${args.networkType})`, - }; -} - -export const accountActionsSchema = v.object({ - read: v.optional(v.boolean()), - autoSign: v.optional(v.boolean()), - rename: v.optional(v.boolean()), -}); -export const accountActionsDescriptionSchema = v.record( - v.keyof(accountActionsSchema), - actionDescriptionSchema -); -export type AccountActionsDescription = v.InferOutput; -export const accountPermissionSchema = v.object({ - type: v.literal('account'), - resourceId: accountResourceIdSchema, - clientId: v.string(), - - actions: accountActionsSchema, -}); diff --git a/src/request/types/permissions/resources/common.ts b/src/request/types/permissions/resources/common.ts deleted file mode 100644 index 5b26b8b..0000000 --- a/src/request/types/permissions/resources/common.ts +++ /dev/null @@ -1,11 +0,0 @@ -import * as v from 'valibot'; - -export const actionDescriptionSchema = v.object({ - name: v.string(), - description: v.variant('type', [ - v.object({ type: v.literal('single'), value: v.string() }), - v.object({ type: v.literal('multiple'), values: v.array(v.string()) }), - ]), -}); - -export type ActionDescription = v.InferOutput; diff --git a/src/request/types/permissions/resources/index.ts b/src/request/types/permissions/resources/index.ts deleted file mode 100644 index ac7c2fe..0000000 --- a/src/request/types/permissions/resources/index.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { - accountResourceIdBrandName, - accountResourceIdSchema, - accountResourceSchema, - accountActionsSchema, - accountActionsDescriptionSchema, - accountPermissionSchema, - makeAccountResourceId, - makeAccountResource, -} from './account'; -export const account = { - accountResourceIdBrandName, - accountResourceIdSchema, - accountResourceSchema, - accountActionsSchema, - accountActionsDescriptionSchema, - accountPermissionSchema, - makeAccountResourceId, - makeAccountResource, -}; -export type * as Account from './account'; - -import { actionDescriptionSchema } from './common'; -export const common = { - actionDescriptionSchema, -}; -export type * as Common from './common'; - -import { - walletResourceSchema, - walletActionsSchema, - walletIdSchema, - walletPermissionSchema, - walletActionsDescriptionSchema, -} from './wallet'; -export const wallet = { - walletResourceSchema, - walletActionsSchema, - walletIdSchema, - walletPermissionSchema, - walletActionsDescriptionSchema, -}; -export type * as Wallet from './wallet'; diff --git a/src/request/types/permissions/resources/wallet.ts b/src/request/types/permissions/resources/wallet.ts deleted file mode 100644 index 0f923d2..0000000 --- a/src/request/types/permissions/resources/wallet.ts +++ /dev/null @@ -1,28 +0,0 @@ -import * as v from 'valibot'; -import { actionDescriptionSchema } from './common'; - -export const walletResourceSchema = v.object({ - type: v.literal('wallet'), - id: v.literal('wallet'), - name: v.literal('Wallet'), -}); - -export const walletActionsSchema = v.object({ - addPrivateKey: v.optional(v.boolean()), - openPopup: v.optional(v.boolean()), - openFullPage: v.optional(v.boolean()), - readAllAccounts: v.optional(v.boolean()), -}); -export const walletActionsDescriptionSchema = v.record( - v.keyof(walletActionsSchema), - actionDescriptionSchema -); -export type AccountActionsDescription = v.InferOutput; -export const walletIdSchema = v.literal('wallet'); -export const walletPermissionSchema = v.object({ - type: v.literal('wallet'), - resourceId: walletIdSchema, - clientId: v.string(), - - actions: walletActionsSchema, -}); diff --git a/src/request/types/permissions/store.ts b/src/request/types/permissions/store.ts deleted file mode 100644 index 35bc798..0000000 --- a/src/request/types/permissions/store.ts +++ /dev/null @@ -1,74 +0,0 @@ -import * as v from 'valibot'; -import { accountPermissionSchema, accountResourceSchema } from './resources/account'; -import { walletPermissionSchema, walletResourceSchema } from './resources/wallet'; - -// Clients - -export const clientId = v.pipe(v.string(), v.url(), v.brand('ClientId')); - -export const client = v.object({ - id: clientId, - origin: v.string(), - name: v.optional(v.string()), - description: v.optional(v.string()), -}); - -export type Client = v.InferOutput; - -export const clientsTable = v.array(client); - -export type ClientsTable = v.InferOutput; - -/** - * @public - */ -export const clientMetadata = v.object({ - clientId: client.entries.id, - lastUsed: v.optional(v.number()), -}); - -/** - * @public - */ -export type ClientMetadata = v.InferOutput; - -/** - * @public - */ -export const clientMetadataTable = v.array(clientMetadata); - -/** - * @public - */ -export type ClientMetadataTable = v.InferOutput; - -// Resources - -export const resource = v.variant('type', [accountResourceSchema, walletResourceSchema]); - -export type Resource = v.InferOutput; - -export const resourcesTable = v.array(resource); - -export type ResourcesTable = v.InferOutput; - -// Permissions - -export const permission = v.variant('type', [accountPermissionSchema, walletPermissionSchema]); -export type Permission = v.InferOutput; - -export const permissionsTable = v.array(permission); - -export type PermissionsTable = v.InferOutput; - -// Permissions Store - -export const permissionsStore = v.object({ - version: v.literal(4), - clients: clientsTable, - clientMetadata: clientMetadataTable, - resources: resourcesTable, - permissions: permissionsTable, -}); - -export type PermissionsStore = v.InferOutput; diff --git a/src/request/types/permissions/utils/account-id.ts b/src/request/types/permissions/utils/account-id.ts deleted file mode 100644 index 9108cea..0000000 --- a/src/request/types/permissions/utils/account-id.ts +++ /dev/null @@ -1,36 +0,0 @@ -// This module contains utilities to work with account IDs. Notably, the IDs are -// generated using the master public key, making them unique across seeds. -// -// The IDs are branded with the intention of preventing random strings from -// being used as IDs. If branding proves be inconvenient, it can be removed. - -import { sha256 } from '@noble/hashes/sha256'; -import { bytesToHex } from '@noble/hashes/utils'; -import { BitcoinNetworkType } from 'src/types'; -import * as v from 'valibot'; - -export type MakeAccountIdOptions = { - masterPubKey: string; - accountId: string; - networkType: BitcoinNetworkType; -}; - -export const accountIdBrandName = 'AccountId'; - -export const accountIdSchema = v.pipe(v.string(), v.brand(accountIdBrandName)); - -export type AccountId = v.InferOutput; - -export function makeAccountId(options: MakeAccountIdOptions) { - return v.parse( - accountIdSchema, - bytesToHex( - sha256( - `account-${options.masterPubKey}-${ - // NOTE: This "account ID" is actually the ~account~ address index from BIP-44. - options.accountId - }-${options.networkType}` - ) - ) - ); -} diff --git a/src/request/types/permissions/utils/index.ts b/src/request/types/permissions/utils/index.ts deleted file mode 100644 index 5bc6fc4..0000000 --- a/src/request/types/permissions/utils/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { makeAccountId, accountIdBrandName, accountIdSchema } from './account-id'; -export const account = { - makeAccountId, - accountIdBrandName, - accountIdSchema, -}; -export type * as Account from './account-id'; diff --git a/src/request/types/walletMethods.ts b/src/request/types/walletMethods.ts index c2042a8..3bde7d4 100644 --- a/src/request/types/walletMethods.ts +++ b/src/request/types/walletMethods.ts @@ -1,27 +1,49 @@ import { MethodParamsAndResult, rpcRequestMessageSchema } from '../../types'; import * as v from 'valibot'; import { walletTypeSchema } from './common'; -import { permissions } from './permissions'; import { AddressPurpose, addressSchema } from '../../addresses'; +export const accountActionsSchema = v.object({ + read: v.optional(v.boolean()), +}); + +export const walletActionsSchema = v.object({}); + +export const accountPermissionSchema = v.object({ + type: v.literal('account'), + resourceId: v.string(), + clientId: v.string(), + actions: accountActionsSchema, +}); + +export const walletPermissionSchema = v.object({ + type: v.literal('wallet'), + resourceId: v.string(), + clientId: v.string(), + actions: walletActionsSchema, +}); + /** * Permissions with the clientId field omitted and optional actions. Used for * permission requests, since the wallet performs authentication based on the * client's tab origin and should not rely on the client authenticating * themselves. */ -export const permissionTemplate = v.variant('type', [ +export const PermissionRequestParams = v.variant('type', [ v.object({ - ...v.omit(permissions.resources.account.accountPermissionSchema, ['clientId']).entries, + ...v.omit(accountPermissionSchema, ['clientId']).entries, }), v.object({ - ...v.omit(permissions.resources.wallet.walletPermissionSchema, ['clientId']).entries, + ...v.omit(walletPermissionSchema, ['clientId']).entries, }), ]); -export type PermissionWithoutClientId = v.InferOutput; + +export const permission = v.variant('type', [accountPermissionSchema, walletPermissionSchema]); + +export type PermissionWithoutClientId = v.InferOutput; export const requestPermissionsMethodName = 'wallet_requestPermissions'; -export const requestPermissionsParamsSchema = v.nullish(v.array(permissionTemplate)); +export const requestPermissionsParamsSchema = v.nullish(v.array(PermissionRequestParams)); export type RequestPermissionsParams = v.InferOutput; export const requestPermissionsResultSchema = v.literal(true); export type RequestPermissionsResult = v.InferOutput; @@ -102,7 +124,7 @@ export type GetWalletType = MethodParamsAndResult; -export const getCurrentPermissionsResultSchema = v.array(permissions.store.permission); +export const getCurrentPermissionsResultSchema = v.array(permission); export type GetCurrentPermissionsResult = v.InferOutput; export const getCurrentPermissionsRequestMessageSchema = v.object({ ...rpcRequestMessageSchema.entries, @@ -124,7 +146,7 @@ export const getAccountMethodName = 'wallet_getAccount'; export const getAccountParamsSchema = v.nullish(v.null()); export type GetAccountParams = v.InferOutput; export const getAccountResultSchema = v.object({ - id: permissions.utils.account.accountIdSchema, + id: v.string(), addresses: v.array(addressSchema), walletType: walletTypeSchema, }); @@ -143,7 +165,7 @@ export type GetAccount = MethodParamsAndResult