Skip to content

Commit

Permalink
Revert "Revert "Add message schema validators"" (#23)
Browse files Browse the repository at this point in the history
* Add "build:watch" script

* Add getBalance method schema

* Add access denied error code

* Switch to bigint

* Add getBalance to BtcRequests

* Add RPC response schemas

* Update getBalance result schema

* Set result and error to non-optional

* Update types

* Use string instead of bigint

* Rename method names

* Fix method name

* added getInscriptions request

* remove comments

* Use same address definition for Stacks

* Support nullish param

* Add more types

* Revert "Revert "Add message schema validators""

---------

Co-authored-by: Eduard Bardají Puig <[email protected]>
Co-authored-by: Mahmoud Aboelenein <[email protected]>
  • Loading branch information
3 people authored Jul 3, 2024
1 parent 900375f commit 0ce8569
Show file tree
Hide file tree
Showing 12 changed files with 428 additions and 152 deletions.
31 changes: 23 additions & 8 deletions package-lock.json

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

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"test": "jest",
"build-debug": "webpack --mode development",
"build": "npm run clean && tsup src/index.ts --format esm --dts",
"build:watch": "npm run clean && tsup src/index.ts --format esm --dts --watch",
"clean": "rimraf dist",
"lint": "prettier --write .",
"prepare": "husky install"
Expand All @@ -30,6 +31,9 @@
"jsontokens": "4.0.1",
"lodash.omit": "4.5.0"
},
"peerDependencies": {
"valibot": "0.33.2"
},
"devDependencies": {
"@types/jest": "^29.2.6",
"@types/lodash.omit": "4.5.9",
Expand All @@ -42,7 +46,7 @@
"ts-jest": "^29.0.5",
"ts-loader": "^9.4.1",
"tsup": "^8.0.2",
"typescript": "^4.9.4",
"typescript": "5.4.5",
"util": "^0.12.4",
"vm-browserify": "^1.1.2",
"webpack": "^5.74.0",
Expand Down
14 changes: 8 additions & 6 deletions src/addresses/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as v from 'valibot';
import type { RequestOptions, RequestPayload } from '../types';

export enum AddressPurpose {
Expand All @@ -20,12 +21,13 @@ export enum AddressType {
stacks = 'stacks',
}

export interface Address {
address: string;
publicKey: string;
purpose?: AddressPurpose;
addressType?: AddressType;
}
export const addressSchema = v.object({
address: v.string(),
publicKey: v.string(),
purpose: v.enum(AddressPurpose),
addressType: v.enum(AddressType),
});
export type Address = v.InferOutput<typeof addressSchema>;

export interface GetAddressResponse {
addresses: Address[];
Expand Down
3 changes: 2 additions & 1 deletion src/provider/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
BtcRequestMethod,
OrdinalsRequestMethod,
Params,
Requests,
RunesRequestMethod,
Expand Down Expand Up @@ -46,7 +47,7 @@ export interface Provider {
mozillaAddOnsUrl?: string;
googlePlayStoreUrl?: string;
iOSAppStoreUrl?: string;
methods?: (StxRequestMethod | BtcRequestMethod | RunesRequestMethod)[];
methods?: (StxRequestMethod | BtcRequestMethod | RunesRequestMethod | OrdinalsRequestMethod)[];
}

export interface SupportedWallet extends Provider {
Expand Down
33 changes: 22 additions & 11 deletions src/request/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { getProviderById } from '../provider';
import { RpcBase, RpcResult, RpcSuccessResponse } from '../types';
import { Params, Requests } from './types';
import {
RpcErrorCode,
RpcResult,
rpcErrorResponseMessageSchema,
rpcSuccessResponseMessageSchema,
} from '../types';
import * as v from 'valibot';
import { Params, Requests, Return } from './types';

export const request = async <Method extends keyof Requests>(
method: Method,
Expand All @@ -20,23 +26,28 @@ export const request = async <Method extends keyof Requests>(

const response = await provider.request(method, params);

if (isRpcSuccessResponse<Method>(response)) {
if (v.is(rpcErrorResponseMessageSchema, response)) {
return {
status: 'error',
error: response.error,
};
}

if (v.is(rpcSuccessResponseMessageSchema, response)) {
return {
status: 'success',
result: response.result,
result: response.result as Return<Method>,
};
}

return {
status: 'error',
error: response.error,
error: {
code: RpcErrorCode.INTERNAL_ERROR,
message: 'Received unknown response from provider.',
data: response,
},
};
};

const isRpcSuccessResponse = <Method extends keyof Requests>(
response: RpcBase
): response is RpcSuccessResponse<Method> => {
return Object.hasOwn(response, 'result') && !!(response as RpcSuccessResponse<Method>).result;
};

export * from './types';
Loading

0 comments on commit 0ce8569

Please sign in to comment.