Skip to content

Commit

Permalink
feat: Add get network method [ENG-6213] (#59)
Browse files Browse the repository at this point in the history
* Add get network method

* Refactor network type

* Bump version
  • Loading branch information
aryzing authored Jan 21, 2025
1 parent 276fce0 commit f7a4f57
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sats-connect/core",
"version": "0.5.2",
"version": "0.5.3",
"main": "dist/index.mjs",
"module": "dist/index.mjs",
"types": "dist/index.d.mts",
Expand Down
1 change: 1 addition & 0 deletions src/request/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface WalletRequests {
wallet_getWalletType: WalletMethods.GetWalletType;
wallet_renouncePermissions: WalletMethods.RenouncePermissions;
wallet_requestPermissions: WalletMethods.RequestPermissions;
wallet_getNetwork: WalletMethods.GetNetwork;
}

export type Requests = BtcRequests &
Expand Down
37 changes: 35 additions & 2 deletions src/request/types/walletMethods.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { MethodParamsAndResult, rpcRequestMessageSchema } from '../../types';
import { BitcoinNetworkType, MethodParamsAndResult, rpcRequestMessageSchema } from '../../types';
import * as v from 'valibot';
import { walletTypeSchema } from './common';
import { AddressPurpose, addressSchema } from '../../addresses';

// NOTE: These next 4 values are copied from xverse-core to avoid having it as a
// dependency. It has side effects and doesn't support tree-shaking, and would
// make sats-connect-core too heavy.

export const accountActionsSchema = v.object({
read: v.optional(v.boolean()),
});

export const walletActionsSchema = v.object({});
export const walletActionsSchema = v.object({
readNetwork: v.optional(v.boolean()),
});

export const accountPermissionSchema = v.object({
type: v.literal('account'),
Expand Down Expand Up @@ -186,3 +192,30 @@ export const connectRequestMessageSchema = v.object({
});
export type ConnectRequestMessage = v.InferOutput<typeof connectRequestMessageSchema>;
export type Connect = MethodParamsAndResult<ConnectParams, ConnectResult>;

export const getNetworkMethodName = 'wallet_getNetwork';
export const getNetworkParamsSchema = v.nullish(v.null());
export type GetNetworkParams = v.InferOutput<typeof getNetworkParamsSchema>;
// NOTE: This next value is copied from xverse-core to avoid having it as a
// dependency. It has side effects and doesn't support tree-shaking, and would
// make sats-connect-core too heavy.
const networkType = ['Mainnet', 'Testnet', 'Testnet4', 'Signet', 'Regtest'] as const;
export const getNetworkResultSchema = v.object({
bitcoin: v.object({
name: v.picklist(networkType),
}),
stacks: v.object({
name: v.string(),
}),
});
export type GetNetworkResult = v.InferOutput<typeof getNetworkResultSchema>;
export const getNetworkRequestMessageSchema = v.object({
...rpcRequestMessageSchema.entries,
...v.object({
method: v.literal(getNetworkMethodName),
params: getNetworkParamsSchema,
id: v.string(),
}).entries,
});
export type GetNetworkRequestMessage = v.InferOutput<typeof getNetworkRequestMessageSchema>;
export type GetNetwork = MethodParamsAndResult<GetNetworkParams, GetNetworkResult>;

0 comments on commit f7a4f57

Please sign in to comment.