Skip to content

Commit

Permalink
Add wallet type
Browse files Browse the repository at this point in the history
  • Loading branch information
aryzing authored Jul 11, 2024
1 parent 1a36a31 commit 81d5375
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
24 changes: 21 additions & 3 deletions src/request/types/btcMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import { AddressPurpose, addressSchema } from '../../addresses';
import { MethodParamsAndResult, rpcRequestMessageSchema } from '../../types';
import * as v from 'valibot';
import { walletTypeSchema } from './common';

export const getInfoMethodName = 'getInfo';
export const getInfoParamsSchema = v.nullish(v.null());
Expand Down Expand Up @@ -178,9 +179,27 @@ export type SignPsbtResult = {
export type SignPsbt = MethodParamsAndResult<SignPsbtParams, SignPsbtResult>;

export const getAccountsMethodName = 'getAccounts';
export const getAccountsParamsSchema = getAddressesParamsSchema;
export const getAccountsParamsSchema = v.object({
/**
* The purposes for which to generate addresses. See
* {@linkcode AddressPurpose} for available purposes.
*/
purposes: v.array(v.enum(AddressPurpose)),
/**
* A message to be displayed to the user in the request prompt.
*/
message: v.optional(v.string()),
});
export type GetAccountsParams = v.InferOutput<typeof getAccountsParamsSchema>;
export const getAccountsResultSchema = v.array(addressSchema);

export const getAccountsResultSchema = v.array(
v.object({
...addressSchema.entries,
...v.object({
walletType: walletTypeSchema,
}).entries,
})
);
export type GetAccountsResult = v.InferOutput<typeof getAccountsResultSchema>;
export const getAccountsRequestMessageSchema = v.object({
...rpcRequestMessageSchema.entries,
Expand All @@ -196,7 +215,6 @@ export type GetAccounts = MethodParamsAndResult<
v.InferOutput<typeof getAccountsResultSchema>
>;

// Get the balance of the current Bitcoin account.
export const getBalanceMethodName = 'getBalance';
export const getBalanceParamsSchema = v.nullish(v.null());
export const getBalanceResultSchema = v.object({
Expand Down
5 changes: 5 additions & 0 deletions src/request/types/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as v from 'valibot';

export const walletTypes = ['software', 'ledger'] as const;
export const walletTypeSchema = v.picklist(walletTypes);
export type WalletType = v.InferOutput<typeof walletTypeSchema>;
16 changes: 11 additions & 5 deletions src/request/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type * as BtcMethods from './btcMethods';
import { GetInscriptions } from './ordinalsMethods';
import type * as RunesMethods from './runesMethods';
import type * as StxMethods from './stxMethods';
import { RequestPermissions, RenouncePermissions } from './walletMethods';
import type * as WalletMethods from './walletMethods';

export interface StxRequests {
stx_callContract: StxMethods.StxCallContract;
Expand Down Expand Up @@ -48,12 +48,17 @@ export interface OrdinalsRequests {

export type OrdinalsRequestMethod = keyof OrdinalsRequests;

export interface WalletMethods {
wallet_requestPermissions: RequestPermissions;
wallet_renouncePermissions: RenouncePermissions;
export interface WalletRequests {
wallet_requestPermissions: WalletMethods.RequestPermissions;
wallet_renouncePermissions: WalletMethods.RenouncePermissions;
wallet_getWalletType: WalletMethods.GetWalletType;
}

export type Requests = BtcRequests & StxRequests & RunesRequests & WalletMethods & OrdinalsRequests;
export type Requests = BtcRequests &
StxRequests &
RunesRequests &
WalletRequests &
OrdinalsRequests;

export type Return<Method> = Method extends keyof Requests ? Requests[Method]['result'] : never;
export type Params<Method> = Method extends keyof Requests ? Requests[Method]['params'] : never;
Expand All @@ -63,3 +68,4 @@ export * from './btcMethods';
export * from './walletMethods';
export * from './runesMethods';
export * from './ordinalsMethods';
export * from './common';
16 changes: 16 additions & 0 deletions src/request/types/walletMethods.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MethodParamsAndResult, rpcRequestMessageSchema } from '../../types';
import * as v from 'valibot';
import { walletTypeSchema } from './common';

export const requestPermissionsMethodName = 'wallet_requestPermissions';
export const requestPermissionsParamsSchema = v.undefined();
Expand Down Expand Up @@ -32,3 +33,18 @@ export type RenouncePermissions = MethodParamsAndResult<
v.InferOutput<typeof renouncePermissionsParamsSchema>,
v.InferOutput<typeof renouncePermissionsResultSchema>
>;

export const getWalletTypeMethodName = 'wallet_getWalletType';
export const getWalletTypeParamsSchema = v.nullish(v.null());
export const getWalletTypeResultSchema = walletTypeSchema;
export const getWalletTypeRequestMessageSchema = v.object({
...rpcRequestMessageSchema.entries,
...v.object({
method: v.literal(getWalletTypeMethodName),
id: v.string(),
}).entries,
});
export type GetWalletType = MethodParamsAndResult<
v.InferOutput<typeof getWalletTypeParamsSchema>,
v.InferOutput<typeof getWalletTypeResultSchema>
>;

0 comments on commit 81d5375

Please sign in to comment.