@metamask/keyring-api 11.0.0 → 12.0.0
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/CHANGELOG.md +21 -1
- package/dist/api/account.cjs +3 -4
- package/dist/api/account.cjs.map +1 -1
- package/dist/api/account.d.cts +1 -1
- package/dist/api/account.d.cts.map +1 -1
- package/dist/api/account.d.mts +1 -1
- package/dist/api/account.d.mts.map +1 -1
- package/dist/api/account.mjs +1 -2
- package/dist/api/account.mjs.map +1 -1
- package/dist/api/asset.cjs +82 -0
- package/dist/api/asset.cjs.map +1 -0
- package/dist/api/asset.d.cts +93 -0
- package/dist/api/asset.d.cts.map +1 -0
- package/dist/api/asset.d.mts +93 -0
- package/dist/api/asset.d.mts.map +1 -0
- package/dist/api/asset.mjs +79 -0
- package/dist/api/asset.mjs.map +1 -0
- package/dist/api/balance.cjs +3 -4
- package/dist/api/balance.cjs.map +1 -1
- package/dist/api/balance.d.cts +1 -1
- package/dist/api/balance.d.cts.map +1 -1
- package/dist/api/balance.d.mts +1 -1
- package/dist/api/balance.d.mts.map +1 -1
- package/dist/api/balance.mjs +1 -2
- package/dist/api/balance.mjs.map +1 -1
- package/dist/api/caip.cjs +3 -3
- package/dist/api/caip.cjs.map +1 -1
- package/dist/api/caip.d.cts.map +1 -1
- package/dist/api/caip.d.mts.map +1 -1
- package/dist/api/caip.mjs +1 -1
- package/dist/api/caip.mjs.map +1 -1
- package/dist/api/index.cjs +3 -0
- package/dist/api/index.cjs.map +1 -1
- package/dist/api/index.d.cts +4 -1
- package/dist/api/index.d.cts.map +1 -1
- package/dist/api/index.d.mts +4 -1
- package/dist/api/index.d.mts.map +1 -1
- package/dist/api/index.mjs +3 -0
- package/dist/api/index.mjs.map +1 -1
- package/dist/api/keyring.cjs +2 -0
- package/dist/api/keyring.cjs.map +1 -1
- package/dist/api/keyring.d.cts +14 -0
- package/dist/api/keyring.d.cts.map +1 -1
- package/dist/api/keyring.d.mts +14 -0
- package/dist/api/keyring.d.mts.map +1 -1
- package/dist/api/keyring.mjs +2 -0
- package/dist/api/keyring.mjs.map +1 -1
- package/dist/api/pagination.cjs +28 -0
- package/dist/api/pagination.cjs.map +1 -0
- package/dist/api/pagination.d.cts +59 -0
- package/dist/api/pagination.d.cts.map +1 -0
- package/dist/api/pagination.d.mts +59 -0
- package/dist/api/pagination.d.mts.map +1 -0
- package/dist/api/pagination.mjs +25 -0
- package/dist/api/pagination.mjs.map +1 -0
- package/dist/api/request.cjs +6 -7
- package/dist/api/request.cjs.map +1 -1
- package/dist/api/request.d.cts +5 -5
- package/dist/api/request.d.cts.map +1 -1
- package/dist/api/request.d.mts +5 -5
- package/dist/api/request.d.mts.map +1 -1
- package/dist/api/request.mjs +1 -2
- package/dist/api/request.mjs.map +1 -1
- package/dist/api/response.cjs +6 -6
- package/dist/api/response.cjs.map +1 -1
- package/dist/api/response.d.cts +1 -1
- package/dist/api/response.d.cts.map +1 -1
- package/dist/api/response.d.mts +1 -1
- package/dist/api/response.d.mts.map +1 -1
- package/dist/api/response.mjs +1 -1
- package/dist/api/response.mjs.map +1 -1
- package/dist/api/transaction.cjs +294 -0
- package/dist/api/transaction.cjs.map +1 -0
- package/dist/api/transaction.d.cts +773 -0
- package/dist/api/transaction.d.cts.map +1 -0
- package/dist/api/transaction.d.mts +773 -0
- package/dist/api/transaction.d.mts.map +1 -0
- package/dist/api/transaction.mjs +291 -0
- package/dist/api/transaction.mjs.map +1 -0
- package/dist/btc/types.cjs +2 -2
- package/dist/btc/types.cjs.map +1 -1
- package/dist/btc/types.d.cts +1 -1
- package/dist/btc/types.d.cts.map +1 -1
- package/dist/btc/types.d.mts +1 -1
- package/dist/btc/types.d.mts.map +1 -1
- package/dist/btc/types.mjs +2 -2
- package/dist/btc/types.mjs.map +1 -1
- package/dist/eth/erc4337/types.cjs +10 -11
- package/dist/eth/erc4337/types.cjs.map +1 -1
- package/dist/eth/erc4337/types.d.cts +4 -4
- package/dist/eth/erc4337/types.d.cts.map +1 -1
- package/dist/eth/erc4337/types.d.mts +4 -4
- package/dist/eth/erc4337/types.d.mts.map +1 -1
- package/dist/eth/erc4337/types.mjs +1 -2
- package/dist/eth/erc4337/types.mjs.map +1 -1
- package/dist/eth/types.cjs +6 -6
- package/dist/eth/types.cjs.map +1 -1
- package/dist/eth/types.d.cts +2 -2
- package/dist/eth/types.d.cts.map +1 -1
- package/dist/eth/types.d.mts +2 -2
- package/dist/eth/types.d.mts.map +1 -1
- package/dist/eth/types.mjs +1 -1
- package/dist/eth/types.mjs.map +1 -1
- package/dist/eth/utils.cjs.map +1 -1
- package/dist/eth/utils.d.cts.map +1 -1
- package/dist/eth/utils.d.mts.map +1 -1
- package/dist/eth/utils.mjs.map +1 -1
- package/dist/events.cjs +67 -1
- package/dist/events.cjs.map +1 -1
- package/dist/events.d.cts +161 -0
- package/dist/events.d.cts.map +1 -1
- package/dist/events.d.mts +161 -0
- package/dist/events.d.mts.map +1 -1
- package/dist/events.mjs +66 -0
- package/dist/events.mjs.map +1 -1
- package/dist/index.cjs +2 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -9
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -9
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -8
- package/dist/index.mjs.map +1 -1
- package/dist/{internal/api.cjs → rpc.cjs} +78 -38
- package/dist/rpc.cjs.map +1 -0
- package/dist/{internal/api.d.cts → rpc.d.cts} +402 -59
- package/dist/rpc.d.cts.map +1 -0
- package/dist/{internal/api.d.mts → rpc.d.mts} +402 -59
- package/dist/rpc.d.mts.map +1 -0
- package/dist/{internal/api.mjs → rpc.mjs} +44 -5
- package/dist/rpc.mjs.map +1 -0
- package/dist/sol/types.cjs +3 -3
- package/dist/sol/types.cjs.map +1 -1
- package/dist/sol/types.d.cts +1 -1
- package/dist/sol/types.d.cts.map +1 -1
- package/dist/sol/types.d.mts +1 -1
- package/dist/sol/types.d.mts.map +1 -1
- package/dist/sol/types.mjs +1 -1
- package/dist/sol/types.mjs.map +1 -1
- package/package.json +11 -14
- package/dist/JsonRpcRequest.cjs +0 -13
- package/dist/JsonRpcRequest.cjs.map +0 -1
- package/dist/JsonRpcRequest.d.cts +0 -17
- package/dist/JsonRpcRequest.d.cts.map +0 -1
- package/dist/JsonRpcRequest.d.mts +0 -17
- package/dist/JsonRpcRequest.d.mts.map +0 -1
- package/dist/JsonRpcRequest.mjs +0 -10
- package/dist/JsonRpcRequest.mjs.map +0 -1
- package/dist/KeyringClient.cjs +0 -124
- package/dist/KeyringClient.cjs.map +0 -1
- package/dist/KeyringClient.d.cts +0 -29
- package/dist/KeyringClient.d.cts.map +0 -1
- package/dist/KeyringClient.d.mts +0 -29
- package/dist/KeyringClient.d.mts.map +0 -1
- package/dist/KeyringClient.mjs +0 -120
- package/dist/KeyringClient.mjs.map +0 -1
- package/dist/KeyringSnapRpcClient.cjs +0 -68
- package/dist/KeyringSnapRpcClient.cjs.map +0 -1
- package/dist/KeyringSnapRpcClient.d.cts +0 -40
- package/dist/KeyringSnapRpcClient.d.cts.map +0 -1
- package/dist/KeyringSnapRpcClient.d.mts +0 -40
- package/dist/KeyringSnapRpcClient.d.mts.map +0 -1
- package/dist/KeyringSnapRpcClient.mjs +0 -63
- package/dist/KeyringSnapRpcClient.mjs.map +0 -1
- package/dist/internal/api.cjs.map +0 -1
- package/dist/internal/api.d.cts.map +0 -1
- package/dist/internal/api.d.mts.map +0 -1
- package/dist/internal/api.mjs.map +0 -1
- package/dist/internal/eth/EthKeyring.cjs +0 -3
- package/dist/internal/eth/EthKeyring.cjs.map +0 -1
- package/dist/internal/eth/EthKeyring.d.cts +0 -34
- package/dist/internal/eth/EthKeyring.d.cts.map +0 -1
- package/dist/internal/eth/EthKeyring.d.mts +0 -34
- package/dist/internal/eth/EthKeyring.d.mts.map +0 -1
- package/dist/internal/eth/EthKeyring.mjs +0 -2
- package/dist/internal/eth/EthKeyring.mjs.map +0 -1
- package/dist/internal/eth/index.cjs +0 -3
- package/dist/internal/eth/index.cjs.map +0 -1
- package/dist/internal/eth/index.d.cts +0 -2
- package/dist/internal/eth/index.d.cts.map +0 -1
- package/dist/internal/eth/index.d.mts +0 -2
- package/dist/internal/eth/index.d.mts.map +0 -1
- package/dist/internal/eth/index.mjs +0 -2
- package/dist/internal/eth/index.mjs.map +0 -1
- package/dist/internal/events.cjs +0 -72
- package/dist/internal/events.cjs.map +0 -1
- package/dist/internal/events.d.cts +0 -162
- package/dist/internal/events.d.cts.map +0 -1
- package/dist/internal/events.d.mts +0 -162
- package/dist/internal/events.d.mts.map +0 -1
- package/dist/internal/events.mjs +0 -69
- package/dist/internal/events.mjs.map +0 -1
- package/dist/internal/index.cjs +0 -21
- package/dist/internal/index.cjs.map +0 -1
- package/dist/internal/index.d.cts +0 -6
- package/dist/internal/index.d.cts.map +0 -1
- package/dist/internal/index.d.mts +0 -6
- package/dist/internal/index.d.mts.map +0 -1
- package/dist/internal/index.mjs +0 -5
- package/dist/internal/index.mjs.map +0 -1
- package/dist/internal/rpc.cjs +0 -33
- package/dist/internal/rpc.cjs.map +0 -1
- package/dist/internal/rpc.d.cts +0 -26
- package/dist/internal/rpc.d.cts.map +0 -1
- package/dist/internal/rpc.d.mts +0 -26
- package/dist/internal/rpc.d.mts.map +0 -1
- package/dist/internal/rpc.mjs +0 -29
- package/dist/internal/rpc.mjs.map +0 -1
- package/dist/internal/types.cjs +0 -52
- package/dist/internal/types.cjs.map +0 -1
- package/dist/internal/types.d.cts +0 -392
- package/dist/internal/types.d.cts.map +0 -1
- package/dist/internal/types.d.mts +0 -392
- package/dist/internal/types.d.mts.map +0 -1
- package/dist/internal/types.mjs +0 -49
- package/dist/internal/types.mjs.map +0 -1
- package/dist/rpc-handler.cjs +0 -136
- package/dist/rpc-handler.cjs.map +0 -1
- package/dist/rpc-handler.d.cts +0 -30
- package/dist/rpc-handler.d.cts.map +0 -1
- package/dist/rpc-handler.d.mts +0 -30
- package/dist/rpc-handler.d.mts.map +0 -1
- package/dist/rpc-handler.mjs +0 -131
- package/dist/rpc-handler.mjs.map +0 -1
- package/dist/snap-utils.cjs +0 -20
- package/dist/snap-utils.cjs.map +0 -1
- package/dist/snap-utils.d.cts +0 -12
- package/dist/snap-utils.d.cts.map +0 -1
- package/dist/snap-utils.d.mts +0 -12
- package/dist/snap-utils.d.mts.map +0 -1
- package/dist/snap-utils.mjs +0 -17
- package/dist/snap-utils.mjs.map +0 -1
- package/dist/superstruct.cjs +0 -80
- package/dist/superstruct.cjs.map +0 -1
- package/dist/superstruct.d.cts +0 -84
- package/dist/superstruct.d.cts.map +0 -1
- package/dist/superstruct.d.mts +0 -84
- package/dist/superstruct.d.mts.map +0 -1
- package/dist/superstruct.mjs +0 -74
- package/dist/superstruct.mjs.map +0 -1
- package/dist/utils/index.cjs +0 -19
- package/dist/utils/index.cjs.map +0 -1
- package/dist/utils/index.d.cts +0 -3
- package/dist/utils/index.d.cts.map +0 -1
- package/dist/utils/index.d.mts +0 -3
- package/dist/utils/index.d.mts.map +0 -1
- package/dist/utils/index.mjs +0 -3
- package/dist/utils/index.mjs.map +0 -1
- package/dist/utils/types.cjs +0 -29
- package/dist/utils/types.cjs.map +0 -1
- package/dist/utils/types.d.cts +0 -18
- package/dist/utils/types.d.cts.map +0 -1
- package/dist/utils/types.d.mts +0 -18
- package/dist/utils/types.d.mts.map +0 -1
- package/dist/utils/types.mjs +0 -26
- package/dist/utils/types.mjs.map +0 -1
- package/dist/utils/typing.cjs +0 -20
- package/dist/utils/typing.cjs.map +0 -1
- package/dist/utils/typing.d.cts +0 -33
- package/dist/utils/typing.d.cts.map +0 -1
- package/dist/utils/typing.d.mts +0 -33
- package/dist/utils/typing.d.mts.map +0 -1
- package/dist/utils/typing.mjs +0 -17
- package/dist/utils/typing.mjs.map +0 -1
package/dist/rpc-handler.d.mts
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
import type { Json } from "@metamask/utils";
|
2
|
-
import type { Keyring } from "./api/index.mjs";
|
3
|
-
import type { JsonRpcRequest } from "./JsonRpcRequest.mjs";
|
4
|
-
/**
|
5
|
-
* Error thrown when a keyring JSON-RPC method is not supported.
|
6
|
-
*/
|
7
|
-
export declare class MethodNotSupportedError extends Error {
|
8
|
-
constructor(method: string);
|
9
|
-
}
|
10
|
-
/**
|
11
|
-
* Handles a keyring JSON-RPC request.
|
12
|
-
*
|
13
|
-
* This function is meant to be used as a handler for Keyring JSON-RPC requests
|
14
|
-
* in an Accounts Snap.
|
15
|
-
*
|
16
|
-
* @param keyring - Keyring instance.
|
17
|
-
* @param request - Keyring JSON-RPC request.
|
18
|
-
* @returns A promise that resolves to the keyring response.
|
19
|
-
* @example
|
20
|
-
* ```ts
|
21
|
-
* export const onKeyringRequest: OnKeyringRequestHandler = async ({
|
22
|
-
* origin,
|
23
|
-
* request,
|
24
|
-
* }) => {
|
25
|
-
* return await handleKeyringRequest(keyring, request);
|
26
|
-
* };
|
27
|
-
* ```
|
28
|
-
*/
|
29
|
-
export declare function handleKeyringRequest(keyring: Keyring, request: JsonRpcRequest): Promise<Json | void>;
|
30
|
-
//# sourceMappingURL=rpc-handler.d.mts.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"rpc-handler.d.mts","sourceRoot":"","sources":["../src/rpc-handler.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAE5C,OAAO,KAAK,EAAE,OAAO,EAAE,wBAAc;AAiBrC,OAAO,KAAK,EAAE,cAAc,EAAE,6BAAyB;AAGvD;;GAEG;AACH,qBAAa,uBAAwB,SAAQ,KAAK;gBACpC,MAAM,EAAE,MAAM;CAG3B;AAkHD;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,CAWtB"}
|
package/dist/rpc-handler.mjs
DELETED
@@ -1,131 +0,0 @@
|
|
1
|
-
import { assert } from "@metamask/superstruct";
|
2
|
-
import { GetAccountRequestStruct, CreateAccountRequestStruct, ApproveRequestRequestStruct, DeleteAccountRequestStruct, ExportAccountRequestStruct, GetRequestRequestStruct, RejectRequestRequestStruct, SubmitRequestRequestStruct, UpdateAccountRequestStruct, FilterAccountChainsStruct, ListAccountsRequestStruct, ListRequestsRequestStruct, GetAccountBalancesRequestStruct } from "./internal/api.mjs";
|
3
|
-
import { KeyringRpcMethod } from "./internal/rpc.mjs";
|
4
|
-
import { JsonRpcRequestStruct } from "./JsonRpcRequest.mjs";
|
5
|
-
/**
|
6
|
-
* Error thrown when a keyring JSON-RPC method is not supported.
|
7
|
-
*/
|
8
|
-
export class MethodNotSupportedError extends Error {
|
9
|
-
constructor(method) {
|
10
|
-
super(`Method not supported: ${method}`);
|
11
|
-
}
|
12
|
-
}
|
13
|
-
/**
|
14
|
-
* Inner function that dispatches JSON-RPC request to the associated Keyring
|
15
|
-
* methods.
|
16
|
-
*
|
17
|
-
* @param keyring - Keyring instance.
|
18
|
-
* @param request - Keyring JSON-RPC request.
|
19
|
-
* @returns A promise that resolves to the keyring response.
|
20
|
-
*/
|
21
|
-
async function dispatchRequest(keyring, request) {
|
22
|
-
// We first have to make sure that the request is a valid JSON-RPC request so
|
23
|
-
// we can check its method name.
|
24
|
-
assert(request, JsonRpcRequestStruct);
|
25
|
-
switch (request.method) {
|
26
|
-
case `${KeyringRpcMethod.ListAccounts}`: {
|
27
|
-
assert(request, ListAccountsRequestStruct);
|
28
|
-
return keyring.listAccounts();
|
29
|
-
}
|
30
|
-
case `${KeyringRpcMethod.GetAccount}`: {
|
31
|
-
assert(request, GetAccountRequestStruct);
|
32
|
-
return keyring.getAccount(request.params.id);
|
33
|
-
}
|
34
|
-
case `${KeyringRpcMethod.CreateAccount}`: {
|
35
|
-
assert(request, CreateAccountRequestStruct);
|
36
|
-
return keyring.createAccount(request.params.options);
|
37
|
-
}
|
38
|
-
case `${KeyringRpcMethod.GetAccountBalances}`: {
|
39
|
-
if (keyring.getAccountBalances === undefined) {
|
40
|
-
throw new MethodNotSupportedError(request.method);
|
41
|
-
}
|
42
|
-
assert(request, GetAccountBalancesRequestStruct);
|
43
|
-
return keyring.getAccountBalances(request.params.id, request.params.assets);
|
44
|
-
}
|
45
|
-
case `${KeyringRpcMethod.FilterAccountChains}`: {
|
46
|
-
assert(request, FilterAccountChainsStruct);
|
47
|
-
return keyring.filterAccountChains(request.params.id, request.params.chains);
|
48
|
-
}
|
49
|
-
case `${KeyringRpcMethod.UpdateAccount}`: {
|
50
|
-
assert(request, UpdateAccountRequestStruct);
|
51
|
-
return keyring.updateAccount(request.params.account);
|
52
|
-
}
|
53
|
-
case `${KeyringRpcMethod.DeleteAccount}`: {
|
54
|
-
assert(request, DeleteAccountRequestStruct);
|
55
|
-
return keyring.deleteAccount(request.params.id);
|
56
|
-
}
|
57
|
-
case `${KeyringRpcMethod.ExportAccount}`: {
|
58
|
-
if (keyring.exportAccount === undefined) {
|
59
|
-
throw new MethodNotSupportedError(request.method);
|
60
|
-
}
|
61
|
-
assert(request, ExportAccountRequestStruct);
|
62
|
-
return keyring.exportAccount(request.params.id);
|
63
|
-
}
|
64
|
-
case `${KeyringRpcMethod.ListRequests}`: {
|
65
|
-
if (keyring.listRequests === undefined) {
|
66
|
-
throw new MethodNotSupportedError(request.method);
|
67
|
-
}
|
68
|
-
assert(request, ListRequestsRequestStruct);
|
69
|
-
return keyring.listRequests();
|
70
|
-
}
|
71
|
-
case `${KeyringRpcMethod.GetRequest}`: {
|
72
|
-
if (keyring.getRequest === undefined) {
|
73
|
-
throw new MethodNotSupportedError(request.method);
|
74
|
-
}
|
75
|
-
assert(request, GetRequestRequestStruct);
|
76
|
-
return keyring.getRequest(request.params.id);
|
77
|
-
}
|
78
|
-
case `${KeyringRpcMethod.SubmitRequest}`: {
|
79
|
-
assert(request, SubmitRequestRequestStruct);
|
80
|
-
return keyring.submitRequest(request.params);
|
81
|
-
}
|
82
|
-
case `${KeyringRpcMethod.ApproveRequest}`: {
|
83
|
-
if (keyring.approveRequest === undefined) {
|
84
|
-
throw new MethodNotSupportedError(request.method);
|
85
|
-
}
|
86
|
-
assert(request, ApproveRequestRequestStruct);
|
87
|
-
return keyring.approveRequest(request.params.id, request.params.data);
|
88
|
-
}
|
89
|
-
case `${KeyringRpcMethod.RejectRequest}`: {
|
90
|
-
if (keyring.rejectRequest === undefined) {
|
91
|
-
throw new MethodNotSupportedError(request.method);
|
92
|
-
}
|
93
|
-
assert(request, RejectRequestRequestStruct);
|
94
|
-
return keyring.rejectRequest(request.params.id);
|
95
|
-
}
|
96
|
-
default: {
|
97
|
-
throw new MethodNotSupportedError(request.method);
|
98
|
-
}
|
99
|
-
}
|
100
|
-
}
|
101
|
-
/**
|
102
|
-
* Handles a keyring JSON-RPC request.
|
103
|
-
*
|
104
|
-
* This function is meant to be used as a handler for Keyring JSON-RPC requests
|
105
|
-
* in an Accounts Snap.
|
106
|
-
*
|
107
|
-
* @param keyring - Keyring instance.
|
108
|
-
* @param request - Keyring JSON-RPC request.
|
109
|
-
* @returns A promise that resolves to the keyring response.
|
110
|
-
* @example
|
111
|
-
* ```ts
|
112
|
-
* export const onKeyringRequest: OnKeyringRequestHandler = async ({
|
113
|
-
* origin,
|
114
|
-
* request,
|
115
|
-
* }) => {
|
116
|
-
* return await handleKeyringRequest(keyring, request);
|
117
|
-
* };
|
118
|
-
* ```
|
119
|
-
*/
|
120
|
-
export async function handleKeyringRequest(keyring, request) {
|
121
|
-
try {
|
122
|
-
return await dispatchRequest(keyring, request);
|
123
|
-
}
|
124
|
-
catch (error) {
|
125
|
-
const message = error instanceof Error && typeof error.message === 'string'
|
126
|
-
? error.message
|
127
|
-
: 'An unknown error occurred while handling the keyring request';
|
128
|
-
throw new Error(message);
|
129
|
-
}
|
130
|
-
}
|
131
|
-
//# sourceMappingURL=rpc-handler.mjs.map
|
package/dist/rpc-handler.mjs.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,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AAM,CAAC,OAAO,EAAE,2BAA2B,CAAC,CAAC;YAC7C,OAAO,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,EAAE,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACxE,CAAC;QAED,KAAK,GAAG,gBAAgB,CAAC,aAAa,EAAE,CAAC,CAAC,CAAC;YACzC,IAAI,OAAO,CAAC,aAAa,KAAK,SAAS,EAAE,CAAC;gBACxC,MAAM,IAAI,uBAAuB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YACpD,CAAC;YACD,MAAM,CAAC,OAAO,EAAE,0BAA0B,CAAC,CAAC;YAC5C,OAAO,OAAO,CAAC,aAAa,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAClD,CAAC;QAED,OAAO,CAAC,CAAC,CAAC;YACR,MAAM,IAAI,uBAAuB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACpD,CAAC;IACH,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,OAAgB,EAChB,OAAuB;IAEvB,IAAI,CAAC;QACH,OAAO,MAAM,eAAe,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACjD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,OAAO,GACX,KAAK,YAAY,KAAK,IAAI,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ;YACzD,CAAC,CAAC,KAAK,CAAC,OAAO;YACf,CAAC,CAAC,8DAA8D,CAAC;QAErE,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;IAC3B,CAAC;AACH,CAAC","sourcesContent":["import { assert } from '@metamask/superstruct';\nimport type { Json } from '@metamask/utils';\n\nimport type { Keyring } from './api';\nimport {\n GetAccountRequestStruct,\n CreateAccountRequestStruct,\n ApproveRequestRequestStruct,\n DeleteAccountRequestStruct,\n ExportAccountRequestStruct,\n GetRequestRequestStruct,\n RejectRequestRequestStruct,\n SubmitRequestRequestStruct,\n UpdateAccountRequestStruct,\n FilterAccountChainsStruct,\n ListAccountsRequestStruct,\n ListRequestsRequestStruct,\n GetAccountBalancesRequestStruct,\n} from './internal/api';\nimport { KeyringRpcMethod } from './internal/rpc';\nimport type { JsonRpcRequest } from './JsonRpcRequest';\nimport { JsonRpcRequestStruct } from './JsonRpcRequest';\n\n/**\n * Error thrown when a keyring JSON-RPC method is not supported.\n */\nexport class MethodNotSupportedError extends Error {\n constructor(method: string) {\n super(`Method not supported: ${method}`);\n }\n}\n\n/**\n * Inner function that dispatches JSON-RPC request to the associated Keyring\n * methods.\n *\n * @param keyring - Keyring instance.\n * @param request - Keyring JSON-RPC request.\n * @returns A promise that resolves to the keyring response.\n */\nasync function dispatchRequest(\n keyring: Keyring,\n request: JsonRpcRequest,\n): Promise<Json | void> {\n // We first have to make sure that the request is a valid JSON-RPC request so\n // we can check its method name.\n assert(request, JsonRpcRequestStruct);\n\n switch (request.method) {\n case `${KeyringRpcMethod.ListAccounts}`: {\n assert(request, ListAccountsRequestStruct);\n return keyring.listAccounts();\n }\n\n case `${KeyringRpcMethod.GetAccount}`: {\n assert(request, GetAccountRequestStruct);\n return keyring.getAccount(request.params.id);\n }\n\n case `${KeyringRpcMethod.CreateAccount}`: {\n assert(request, CreateAccountRequestStruct);\n return keyring.createAccount(request.params.options);\n }\n\n case `${KeyringRpcMethod.GetAccountBalances}`: {\n if (keyring.getAccountBalances === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, GetAccountBalancesRequestStruct);\n return keyring.getAccountBalances(\n request.params.id,\n request.params.assets,\n );\n }\n\n case `${KeyringRpcMethod.FilterAccountChains}`: {\n assert(request, FilterAccountChainsStruct);\n return keyring.filterAccountChains(\n request.params.id,\n request.params.chains,\n );\n }\n\n case `${KeyringRpcMethod.UpdateAccount}`: {\n assert(request, UpdateAccountRequestStruct);\n return keyring.updateAccount(request.params.account);\n }\n\n case `${KeyringRpcMethod.DeleteAccount}`: {\n assert(request, DeleteAccountRequestStruct);\n return keyring.deleteAccount(request.params.id);\n }\n\n case `${KeyringRpcMethod.ExportAccount}`: {\n if (keyring.exportAccount === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, ExportAccountRequestStruct);\n return keyring.exportAccount(request.params.id);\n }\n\n case `${KeyringRpcMethod.ListRequests}`: {\n if (keyring.listRequests === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, ListRequestsRequestStruct);\n return keyring.listRequests();\n }\n\n case `${KeyringRpcMethod.GetRequest}`: {\n if (keyring.getRequest === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, GetRequestRequestStruct);\n return keyring.getRequest(request.params.id);\n }\n\n case `${KeyringRpcMethod.SubmitRequest}`: {\n assert(request, SubmitRequestRequestStruct);\n return keyring.submitRequest(request.params);\n }\n\n case `${KeyringRpcMethod.ApproveRequest}`: {\n if (keyring.approveRequest === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, ApproveRequestRequestStruct);\n return keyring.approveRequest(request.params.id, request.params.data);\n }\n\n case `${KeyringRpcMethod.RejectRequest}`: {\n if (keyring.rejectRequest === undefined) {\n throw new MethodNotSupportedError(request.method);\n }\n assert(request, RejectRequestRequestStruct);\n return keyring.rejectRequest(request.params.id);\n }\n\n default: {\n throw new MethodNotSupportedError(request.method);\n }\n }\n}\n\n/**\n * Handles a keyring JSON-RPC request.\n *\n * This function is meant to be used as a handler for Keyring JSON-RPC requests\n * in an Accounts Snap.\n *\n * @param keyring - Keyring instance.\n * @param request - Keyring JSON-RPC request.\n * @returns A promise that resolves to the keyring response.\n * @example\n * ```ts\n * export const onKeyringRequest: OnKeyringRequestHandler = async ({\n * origin,\n * request,\n * }) => {\n * return await handleKeyringRequest(keyring, request);\n * };\n * ```\n */\nexport async function handleKeyringRequest(\n keyring: Keyring,\n request: JsonRpcRequest,\n): Promise<Json | void> {\n try {\n return await dispatchRequest(keyring, request);\n } catch (error) {\n const message =\n error instanceof Error && typeof error.message === 'string'\n ? error.message\n : 'An unknown error occurred while handling the keyring request';\n\n throw new Error(message);\n }\n}\n"]}
|
package/dist/snap-utils.cjs
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.emitSnapKeyringEvent = emitSnapKeyringEvent;
|
4
|
-
/**
|
5
|
-
* Emit a keyring event from a snap.
|
6
|
-
*
|
7
|
-
* @param snap - The global snap object.
|
8
|
-
* @param event - The event name.
|
9
|
-
* @param data - The event data.
|
10
|
-
*/
|
11
|
-
async function emitSnapKeyringEvent(snap, event, data) {
|
12
|
-
await snap.request({
|
13
|
-
method: 'snap_manageAccounts',
|
14
|
-
params: {
|
15
|
-
method: event,
|
16
|
-
params: { ...data },
|
17
|
-
},
|
18
|
-
});
|
19
|
-
}
|
20
|
-
//# sourceMappingURL=snap-utils.cjs.map
|
package/dist/snap-utils.cjs.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"snap-utils.cjs","sourceRoot":"","sources":["../src/snap-utils.ts"],"names":[],"mappings":";;AAYA,oDAYC;AAnBD;;;;;;GAMG;AACI,KAAK,UAAU,oBAAoB,CACxC,IAAmB,EACnB,KAAmB,EACnB,IAA0B;IAE1B,MAAM,IAAI,CAAC,OAAO,CAAC;QACjB,MAAM,EAAE,qBAAqB;QAC7B,MAAM,EAAE;YACN,MAAM,EAAE,KAAK;YACb,MAAM,EAAE,EAAE,GAAG,IAAI,EAAE;SACpB;KACF,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { SnapsProvider } from '@metamask/snaps-sdk';\nimport type { Json } from '@metamask/utils';\n\nimport type { KeyringEvent } from './events';\n\n/**\n * Emit a keyring event from a snap.\n *\n * @param snap - The global snap object.\n * @param event - The event name.\n * @param data - The event data.\n */\nexport async function emitSnapKeyringEvent(\n snap: SnapsProvider,\n event: KeyringEvent,\n data: Record<string, Json>,\n): Promise<void> {\n await snap.request({\n method: 'snap_manageAccounts',\n params: {\n method: event,\n params: { ...data },\n },\n });\n}\n"]}
|
package/dist/snap-utils.d.cts
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
import type { SnapsProvider } from "@metamask/snaps-sdk";
|
2
|
-
import type { Json } from "@metamask/utils";
|
3
|
-
import type { KeyringEvent } from "./events.cjs";
|
4
|
-
/**
|
5
|
-
* Emit a keyring event from a snap.
|
6
|
-
*
|
7
|
-
* @param snap - The global snap object.
|
8
|
-
* @param event - The event name.
|
9
|
-
* @param data - The event data.
|
10
|
-
*/
|
11
|
-
export declare function emitSnapKeyringEvent(snap: SnapsProvider, event: KeyringEvent, data: Record<string, Json>): Promise<void>;
|
12
|
-
//# sourceMappingURL=snap-utils.d.cts.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"snap-utils.d.cts","sourceRoot":"","sources":["../src/snap-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,4BAA4B;AACzD,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAE5C,OAAO,KAAK,EAAE,YAAY,EAAE,qBAAiB;AAE7C;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,IAAI,EAAE,aAAa,EACnB,KAAK,EAAE,YAAY,EACnB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GACzB,OAAO,CAAC,IAAI,CAAC,CAQf"}
|
package/dist/snap-utils.d.mts
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
import type { SnapsProvider } from "@metamask/snaps-sdk";
|
2
|
-
import type { Json } from "@metamask/utils";
|
3
|
-
import type { KeyringEvent } from "./events.mjs";
|
4
|
-
/**
|
5
|
-
* Emit a keyring event from a snap.
|
6
|
-
*
|
7
|
-
* @param snap - The global snap object.
|
8
|
-
* @param event - The event name.
|
9
|
-
* @param data - The event data.
|
10
|
-
*/
|
11
|
-
export declare function emitSnapKeyringEvent(snap: SnapsProvider, event: KeyringEvent, data: Record<string, Json>): Promise<void>;
|
12
|
-
//# sourceMappingURL=snap-utils.d.mts.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"snap-utils.d.mts","sourceRoot":"","sources":["../src/snap-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,4BAA4B;AACzD,OAAO,KAAK,EAAE,IAAI,EAAE,wBAAwB;AAE5C,OAAO,KAAK,EAAE,YAAY,EAAE,qBAAiB;AAE7C;;;;;;GAMG;AACH,wBAAsB,oBAAoB,CACxC,IAAI,EAAE,aAAa,EACnB,KAAK,EAAE,YAAY,EACnB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GACzB,OAAO,CAAC,IAAI,CAAC,CAQf"}
|
package/dist/snap-utils.mjs
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Emit a keyring event from a snap.
|
3
|
-
*
|
4
|
-
* @param snap - The global snap object.
|
5
|
-
* @param event - The event name.
|
6
|
-
* @param data - The event data.
|
7
|
-
*/
|
8
|
-
export async function emitSnapKeyringEvent(snap, event, data) {
|
9
|
-
await snap.request({
|
10
|
-
method: 'snap_manageAccounts',
|
11
|
-
params: {
|
12
|
-
method: event,
|
13
|
-
params: { ...data },
|
14
|
-
},
|
15
|
-
});
|
16
|
-
}
|
17
|
-
//# sourceMappingURL=snap-utils.mjs.map
|
package/dist/snap-utils.mjs.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"snap-utils.mjs","sourceRoot":"","sources":["../src/snap-utils.ts"],"names":[],"mappings":"AAKA;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,IAAmB,EACnB,KAAmB,EACnB,IAA0B;IAE1B,MAAM,IAAI,CAAC,OAAO,CAAC;QACjB,MAAM,EAAE,qBAAqB;QAC7B,MAAM,EAAE;YACN,MAAM,EAAE,KAAK;YACb,MAAM,EAAE,EAAE,GAAG,IAAI,EAAE;SACpB;KACF,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { SnapsProvider } from '@metamask/snaps-sdk';\nimport type { Json } from '@metamask/utils';\n\nimport type { KeyringEvent } from './events';\n\n/**\n * Emit a keyring event from a snap.\n *\n * @param snap - The global snap object.\n * @param event - The event name.\n * @param data - The event data.\n */\nexport async function emitSnapKeyringEvent(\n snap: SnapsProvider,\n event: KeyringEvent,\n data: Record<string, Json>,\n): Promise<void> {\n await snap.request({\n method: 'snap_manageAccounts',\n params: {\n method: event,\n params: { ...data },\n },\n });\n}\n"]}
|
package/dist/superstruct.cjs
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.object = object;
|
4
|
-
exports.exactOptional = exactOptional;
|
5
|
-
exports.definePattern = definePattern;
|
6
|
-
exports.strictMask = strictMask;
|
7
|
-
const superstruct_1 = require("@metamask/superstruct");
|
8
|
-
/**
|
9
|
-
* Change the return type of a superstruct object struct to support exact
|
10
|
-
* optional properties.
|
11
|
-
*
|
12
|
-
* @param schema - The object schema.
|
13
|
-
* @returns A struct representing an object with a known set of properties.
|
14
|
-
*/
|
15
|
-
function object(schema) {
|
16
|
-
return (0, superstruct_1.object)(schema);
|
17
|
-
}
|
18
|
-
/**
|
19
|
-
* Check if the current property is present in its parent object.
|
20
|
-
*
|
21
|
-
* @param ctx - The context to check.
|
22
|
-
* @returns `true` if the property is present, `false` otherwise.
|
23
|
-
*/
|
24
|
-
function hasOptional(ctx) {
|
25
|
-
const property = ctx.path[ctx.path.length - 1];
|
26
|
-
const parent = ctx.branch[ctx.branch.length - 2];
|
27
|
-
return property in parent;
|
28
|
-
}
|
29
|
-
/**
|
30
|
-
* Augment a struct to allow exact-optional values. Exact-optional values can
|
31
|
-
* be omitted but cannot be `undefined`.
|
32
|
-
*
|
33
|
-
* ```ts
|
34
|
-
* const foo = object({ bar: exactOptional(string()) });
|
35
|
-
* type Foo = Infer<typeof foo>;
|
36
|
-
* // Foo = { bar?: string }
|
37
|
-
* ```
|
38
|
-
*
|
39
|
-
* @param struct - The struct to augment.
|
40
|
-
* @returns The augmented struct.
|
41
|
-
*/
|
42
|
-
function exactOptional(struct) {
|
43
|
-
return new superstruct_1.Struct({
|
44
|
-
...struct,
|
45
|
-
validator: (value, ctx) => !hasOptional(ctx) || struct.validator(value, ctx),
|
46
|
-
refiner: (value, ctx) => !hasOptional(ctx) || struct.refiner(value, ctx),
|
47
|
-
});
|
48
|
-
}
|
49
|
-
/**
|
50
|
-
* Defines a new string-struct matching a regular expression.
|
51
|
-
*
|
52
|
-
* Example:
|
53
|
-
*
|
54
|
-
* ```ts
|
55
|
-
* const EthAddressStruct = definePattern('EthAddress', /^0x[0-9a-f]{40}$/iu);
|
56
|
-
* ```
|
57
|
-
*
|
58
|
-
* @param name - Type name.
|
59
|
-
* @param pattern - Regular expression to match.
|
60
|
-
* @returns A new string-struct that matches the given pattern.
|
61
|
-
*/
|
62
|
-
function definePattern(name, pattern) {
|
63
|
-
return (0, superstruct_1.define)(name, (value) => typeof value === 'string' && pattern.test(value));
|
64
|
-
}
|
65
|
-
/**
|
66
|
-
* Assert that a value is valid according to a struct.
|
67
|
-
*
|
68
|
-
* It is similar to superstruct's mask function, but it does not ignore extra
|
69
|
-
* properties.
|
70
|
-
*
|
71
|
-
* @param value - Value to check.
|
72
|
-
* @param struct - Struct to validate the value against.
|
73
|
-
* @param message - Error message to throw if the value is not valid.
|
74
|
-
* @returns The value if it is valid.
|
75
|
-
*/
|
76
|
-
function strictMask(value, struct, message) {
|
77
|
-
(0, superstruct_1.assert)(value, struct, message);
|
78
|
-
return value;
|
79
|
-
}
|
80
|
-
//# sourceMappingURL=superstruct.cjs.map
|
package/dist/superstruct.cjs.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"superstruct.cjs","sourceRoot":"","sources":["../src/superstruct.ts"],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OAAgB;IAEhB,IAAA,oBAAM,EAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import {\n Struct,\n assert,\n define,\n object as stObject,\n} from '@metamask/superstruct';\nimport type {\n Infer,\n Context,\n ObjectSchema,\n OmitBy,\n Optionalize,\n PickBy,\n Simplify,\n} from '@metamask/superstruct';\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\ndeclare const ExactOptionalSymbol: unique symbol;\n\nexport type ExactOptionalTag = {\n type: typeof ExactOptionalSymbol;\n};\n\n/**\n * Exclude type `Type` from the properties of `Obj`.\n *\n * ```ts\n * type Foo = { a: string | null; b: number };\n * type Bar = ExcludeType<Foo, null>;\n * // Bar = { a: string, b: number }\n * ```\n */\nexport type ExcludeType<Obj, Type> = {\n [K in keyof Obj]: Exclude<Obj[K], Type>;\n};\n\n/**\n * Make optional all properties that have the `ExactOptionalTag` type.\n *\n * ```ts\n * type Foo = { a: string | ExactOptionalTag; b: number};\n * type Bar = ExactOptionalize<Foo>;\n * // Bar = { a?: string; b: number}\n * ```\n */\nexport type ExactOptionalize<Schema extends object> = OmitBy<\n Schema,\n ExactOptionalTag\n> &\n Partial<ExcludeType<PickBy<Schema, ExactOptionalTag>, ExactOptionalTag>>;\n\n/**\n * Infer a type from an superstruct object schema.\n */\nexport type ObjectType<Schema extends ObjectSchema> = Simplify<\n ExactOptionalize<Optionalize<{ [K in keyof Schema]: Infer<Schema[K]> }>>\n>;\n\n/**\n * Change the return type of a superstruct object struct to support exact\n * optional properties.\n *\n * @param schema - The object schema.\n * @returns A struct representing an object with a known set of properties.\n */\nexport function object<Schema extends ObjectSchema>(\n schema: Schema,\n): Struct<ObjectType<Schema>, Schema> {\n return stObject(schema) as any;\n}\n\n/**\n * Check if the current property is present in its parent object.\n *\n * @param ctx - The context to check.\n * @returns `true` if the property is present, `false` otherwise.\n */\nfunction hasOptional(ctx: Context): boolean {\n const property: string = ctx.path[ctx.path.length - 1];\n const parent: Record<string, unknown> = ctx.branch[ctx.branch.length - 2];\n\n return property in parent;\n}\n\n/**\n * Augment a struct to allow exact-optional values. Exact-optional values can\n * be omitted but cannot be `undefined`.\n *\n * ```ts\n * const foo = object({ bar: exactOptional(string()) });\n * type Foo = Infer<typeof foo>;\n * // Foo = { bar?: string }\n * ```\n *\n * @param struct - The struct to augment.\n * @returns The augmented struct.\n */\nexport function exactOptional<Type, Schema>(\n struct: Struct<Type, Schema>,\n): Struct<Type | ExactOptionalTag, Schema> {\n return new Struct({\n ...struct,\n\n validator: (value, ctx) =>\n !hasOptional(ctx) || struct.validator(value, ctx),\n\n refiner: (value, ctx) =>\n !hasOptional(ctx) || struct.refiner(value as Type, ctx),\n });\n}\n\n/**\n * Defines a new string-struct matching a regular expression.\n *\n * Example:\n *\n * ```ts\n * const EthAddressStruct = definePattern('EthAddress', /^0x[0-9a-f]{40}$/iu);\n * ```\n *\n * @param name - Type name.\n * @param pattern - Regular expression to match.\n * @returns A new string-struct that matches the given pattern.\n */\nexport function definePattern(\n name: string,\n pattern: RegExp,\n): Struct<string, null> {\n return define<string>(\n name,\n (value: unknown): boolean =>\n typeof value === 'string' && pattern.test(value),\n );\n}\n\n/**\n * Assert that a value is valid according to a struct.\n *\n * It is similar to superstruct's mask function, but it does not ignore extra\n * properties.\n *\n * @param value - Value to check.\n * @param struct - Struct to validate the value against.\n * @param message - Error message to throw if the value is not valid.\n * @returns The value if it is valid.\n */\nexport function strictMask<Type, Schema>(\n value: unknown,\n struct: Struct<Type, Schema>,\n message?: string,\n): Type {\n assert(value, struct, message);\n return value;\n}\n"]}
|
package/dist/superstruct.d.cts
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
import { Struct } from "@metamask/superstruct";
|
2
|
-
import type { Infer, ObjectSchema, OmitBy, Optionalize, PickBy, Simplify } from "@metamask/superstruct";
|
3
|
-
declare const ExactOptionalSymbol: unique symbol;
|
4
|
-
export type ExactOptionalTag = {
|
5
|
-
type: typeof ExactOptionalSymbol;
|
6
|
-
};
|
7
|
-
/**
|
8
|
-
* Exclude type `Type` from the properties of `Obj`.
|
9
|
-
*
|
10
|
-
* ```ts
|
11
|
-
* type Foo = { a: string | null; b: number };
|
12
|
-
* type Bar = ExcludeType<Foo, null>;
|
13
|
-
* // Bar = { a: string, b: number }
|
14
|
-
* ```
|
15
|
-
*/
|
16
|
-
export type ExcludeType<Obj, Type> = {
|
17
|
-
[K in keyof Obj]: Exclude<Obj[K], Type>;
|
18
|
-
};
|
19
|
-
/**
|
20
|
-
* Make optional all properties that have the `ExactOptionalTag` type.
|
21
|
-
*
|
22
|
-
* ```ts
|
23
|
-
* type Foo = { a: string | ExactOptionalTag; b: number};
|
24
|
-
* type Bar = ExactOptionalize<Foo>;
|
25
|
-
* // Bar = { a?: string; b: number}
|
26
|
-
* ```
|
27
|
-
*/
|
28
|
-
export type ExactOptionalize<Schema extends object> = OmitBy<Schema, ExactOptionalTag> & Partial<ExcludeType<PickBy<Schema, ExactOptionalTag>, ExactOptionalTag>>;
|
29
|
-
/**
|
30
|
-
* Infer a type from an superstruct object schema.
|
31
|
-
*/
|
32
|
-
export type ObjectType<Schema extends ObjectSchema> = Simplify<ExactOptionalize<Optionalize<{
|
33
|
-
[K in keyof Schema]: Infer<Schema[K]>;
|
34
|
-
}>>>;
|
35
|
-
/**
|
36
|
-
* Change the return type of a superstruct object struct to support exact
|
37
|
-
* optional properties.
|
38
|
-
*
|
39
|
-
* @param schema - The object schema.
|
40
|
-
* @returns A struct representing an object with a known set of properties.
|
41
|
-
*/
|
42
|
-
export declare function object<Schema extends ObjectSchema>(schema: Schema): Struct<ObjectType<Schema>, Schema>;
|
43
|
-
/**
|
44
|
-
* Augment a struct to allow exact-optional values. Exact-optional values can
|
45
|
-
* be omitted but cannot be `undefined`.
|
46
|
-
*
|
47
|
-
* ```ts
|
48
|
-
* const foo = object({ bar: exactOptional(string()) });
|
49
|
-
* type Foo = Infer<typeof foo>;
|
50
|
-
* // Foo = { bar?: string }
|
51
|
-
* ```
|
52
|
-
*
|
53
|
-
* @param struct - The struct to augment.
|
54
|
-
* @returns The augmented struct.
|
55
|
-
*/
|
56
|
-
export declare function exactOptional<Type, Schema>(struct: Struct<Type, Schema>): Struct<Type | ExactOptionalTag, Schema>;
|
57
|
-
/**
|
58
|
-
* Defines a new string-struct matching a regular expression.
|
59
|
-
*
|
60
|
-
* Example:
|
61
|
-
*
|
62
|
-
* ```ts
|
63
|
-
* const EthAddressStruct = definePattern('EthAddress', /^0x[0-9a-f]{40}$/iu);
|
64
|
-
* ```
|
65
|
-
*
|
66
|
-
* @param name - Type name.
|
67
|
-
* @param pattern - Regular expression to match.
|
68
|
-
* @returns A new string-struct that matches the given pattern.
|
69
|
-
*/
|
70
|
-
export declare function definePattern(name: string, pattern: RegExp): Struct<string, null>;
|
71
|
-
/**
|
72
|
-
* Assert that a value is valid according to a struct.
|
73
|
-
*
|
74
|
-
* It is similar to superstruct's mask function, but it does not ignore extra
|
75
|
-
* properties.
|
76
|
-
*
|
77
|
-
* @param value - Value to check.
|
78
|
-
* @param struct - Struct to validate the value against.
|
79
|
-
* @param message - Error message to throw if the value is not valid.
|
80
|
-
* @returns The value if it is valid.
|
81
|
-
*/
|
82
|
-
export declare function strictMask<Type, Schema>(value: unknown, struct: Struct<Type, Schema>, message?: string): Type;
|
83
|
-
export {};
|
84
|
-
//# sourceMappingURL=superstruct.d.cts.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"superstruct.d.cts","sourceRoot":"","sources":["../src/superstruct.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EAIP,8BAA8B;AAC/B,OAAO,KAAK,EACV,KAAK,EAEL,YAAY,EACZ,MAAM,EACN,WAAW,EACX,MAAM,EACN,QAAQ,EACT,8BAA8B;AAG/B,OAAO,CAAC,MAAM,mBAAmB,EAAE,OAAO,MAAM,CAAC;AAEjD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,IAAI,EAAE,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AAC,IAAI,EAAE,MAAM,CAAC,EAC5B,OAAO,CAAC,EAAE,MAAM,GACf,IAAI,CAGN"}
|
package/dist/superstruct.d.mts
DELETED
@@ -1,84 +0,0 @@
|
|
1
|
-
import { Struct } from "@metamask/superstruct";
|
2
|
-
import type { Infer, ObjectSchema, OmitBy, Optionalize, PickBy, Simplify } from "@metamask/superstruct";
|
3
|
-
declare const ExactOptionalSymbol: unique symbol;
|
4
|
-
export type ExactOptionalTag = {
|
5
|
-
type: typeof ExactOptionalSymbol;
|
6
|
-
};
|
7
|
-
/**
|
8
|
-
* Exclude type `Type` from the properties of `Obj`.
|
9
|
-
*
|
10
|
-
* ```ts
|
11
|
-
* type Foo = { a: string | null; b: number };
|
12
|
-
* type Bar = ExcludeType<Foo, null>;
|
13
|
-
* // Bar = { a: string, b: number }
|
14
|
-
* ```
|
15
|
-
*/
|
16
|
-
export type ExcludeType<Obj, Type> = {
|
17
|
-
[K in keyof Obj]: Exclude<Obj[K], Type>;
|
18
|
-
};
|
19
|
-
/**
|
20
|
-
* Make optional all properties that have the `ExactOptionalTag` type.
|
21
|
-
*
|
22
|
-
* ```ts
|
23
|
-
* type Foo = { a: string | ExactOptionalTag; b: number};
|
24
|
-
* type Bar = ExactOptionalize<Foo>;
|
25
|
-
* // Bar = { a?: string; b: number}
|
26
|
-
* ```
|
27
|
-
*/
|
28
|
-
export type ExactOptionalize<Schema extends object> = OmitBy<Schema, ExactOptionalTag> & Partial<ExcludeType<PickBy<Schema, ExactOptionalTag>, ExactOptionalTag>>;
|
29
|
-
/**
|
30
|
-
* Infer a type from an superstruct object schema.
|
31
|
-
*/
|
32
|
-
export type ObjectType<Schema extends ObjectSchema> = Simplify<ExactOptionalize<Optionalize<{
|
33
|
-
[K in keyof Schema]: Infer<Schema[K]>;
|
34
|
-
}>>>;
|
35
|
-
/**
|
36
|
-
* Change the return type of a superstruct object struct to support exact
|
37
|
-
* optional properties.
|
38
|
-
*
|
39
|
-
* @param schema - The object schema.
|
40
|
-
* @returns A struct representing an object with a known set of properties.
|
41
|
-
*/
|
42
|
-
export declare function object<Schema extends ObjectSchema>(schema: Schema): Struct<ObjectType<Schema>, Schema>;
|
43
|
-
/**
|
44
|
-
* Augment a struct to allow exact-optional values. Exact-optional values can
|
45
|
-
* be omitted but cannot be `undefined`.
|
46
|
-
*
|
47
|
-
* ```ts
|
48
|
-
* const foo = object({ bar: exactOptional(string()) });
|
49
|
-
* type Foo = Infer<typeof foo>;
|
50
|
-
* // Foo = { bar?: string }
|
51
|
-
* ```
|
52
|
-
*
|
53
|
-
* @param struct - The struct to augment.
|
54
|
-
* @returns The augmented struct.
|
55
|
-
*/
|
56
|
-
export declare function exactOptional<Type, Schema>(struct: Struct<Type, Schema>): Struct<Type | ExactOptionalTag, Schema>;
|
57
|
-
/**
|
58
|
-
* Defines a new string-struct matching a regular expression.
|
59
|
-
*
|
60
|
-
* Example:
|
61
|
-
*
|
62
|
-
* ```ts
|
63
|
-
* const EthAddressStruct = definePattern('EthAddress', /^0x[0-9a-f]{40}$/iu);
|
64
|
-
* ```
|
65
|
-
*
|
66
|
-
* @param name - Type name.
|
67
|
-
* @param pattern - Regular expression to match.
|
68
|
-
* @returns A new string-struct that matches the given pattern.
|
69
|
-
*/
|
70
|
-
export declare function definePattern(name: string, pattern: RegExp): Struct<string, null>;
|
71
|
-
/**
|
72
|
-
* Assert that a value is valid according to a struct.
|
73
|
-
*
|
74
|
-
* It is similar to superstruct's mask function, but it does not ignore extra
|
75
|
-
* properties.
|
76
|
-
*
|
77
|
-
* @param value - Value to check.
|
78
|
-
* @param struct - Struct to validate the value against.
|
79
|
-
* @param message - Error message to throw if the value is not valid.
|
80
|
-
* @returns The value if it is valid.
|
81
|
-
*/
|
82
|
-
export declare function strictMask<Type, Schema>(value: unknown, struct: Struct<Type, Schema>, message?: string): Type;
|
83
|
-
export {};
|
84
|
-
//# sourceMappingURL=superstruct.d.mts.map
|
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"superstruct.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AAI,GAAG,gBAAgB,EAAE,MAAM,CAAC,CAUzC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,GACd,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAMtB;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EACrC,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,EAC5B,OAAO,CAAC,EAAE,MAAM,GACf,IAAI,CAGN"}
|
package/dist/superstruct.mjs
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
import { Struct, assert, define, object as stObject } from "@metamask/superstruct";
|
2
|
-
/**
|
3
|
-
* Change the return type of a superstruct object struct to support exact
|
4
|
-
* optional properties.
|
5
|
-
*
|
6
|
-
* @param schema - The object schema.
|
7
|
-
* @returns A struct representing an object with a known set of properties.
|
8
|
-
*/
|
9
|
-
export function object(schema) {
|
10
|
-
return stObject(schema);
|
11
|
-
}
|
12
|
-
/**
|
13
|
-
* Check if the current property is present in its parent object.
|
14
|
-
*
|
15
|
-
* @param ctx - The context to check.
|
16
|
-
* @returns `true` if the property is present, `false` otherwise.
|
17
|
-
*/
|
18
|
-
function hasOptional(ctx) {
|
19
|
-
const property = ctx.path[ctx.path.length - 1];
|
20
|
-
const parent = ctx.branch[ctx.branch.length - 2];
|
21
|
-
return property in parent;
|
22
|
-
}
|
23
|
-
/**
|
24
|
-
* Augment a struct to allow exact-optional values. Exact-optional values can
|
25
|
-
* be omitted but cannot be `undefined`.
|
26
|
-
*
|
27
|
-
* ```ts
|
28
|
-
* const foo = object({ bar: exactOptional(string()) });
|
29
|
-
* type Foo = Infer<typeof foo>;
|
30
|
-
* // Foo = { bar?: string }
|
31
|
-
* ```
|
32
|
-
*
|
33
|
-
* @param struct - The struct to augment.
|
34
|
-
* @returns The augmented struct.
|
35
|
-
*/
|
36
|
-
export function exactOptional(struct) {
|
37
|
-
return new Struct({
|
38
|
-
...struct,
|
39
|
-
validator: (value, ctx) => !hasOptional(ctx) || struct.validator(value, ctx),
|
40
|
-
refiner: (value, ctx) => !hasOptional(ctx) || struct.refiner(value, ctx),
|
41
|
-
});
|
42
|
-
}
|
43
|
-
/**
|
44
|
-
* Defines a new string-struct matching a regular expression.
|
45
|
-
*
|
46
|
-
* Example:
|
47
|
-
*
|
48
|
-
* ```ts
|
49
|
-
* const EthAddressStruct = definePattern('EthAddress', /^0x[0-9a-f]{40}$/iu);
|
50
|
-
* ```
|
51
|
-
*
|
52
|
-
* @param name - Type name.
|
53
|
-
* @param pattern - Regular expression to match.
|
54
|
-
* @returns A new string-struct that matches the given pattern.
|
55
|
-
*/
|
56
|
-
export function definePattern(name, pattern) {
|
57
|
-
return define(name, (value) => typeof value === 'string' && pattern.test(value));
|
58
|
-
}
|
59
|
-
/**
|
60
|
-
* Assert that a value is valid according to a struct.
|
61
|
-
*
|
62
|
-
* It is similar to superstruct's mask function, but it does not ignore extra
|
63
|
-
* properties.
|
64
|
-
*
|
65
|
-
* @param value - Value to check.
|
66
|
-
* @param struct - Struct to validate the value against.
|
67
|
-
* @param message - Error message to throw if the value is not valid.
|
68
|
-
* @returns The value if it is valid.
|
69
|
-
*/
|
70
|
-
export function strictMask(value, struct, message) {
|
71
|
-
assert(value, struct, message);
|
72
|
-
return value;
|
73
|
-
}
|
74
|
-
//# sourceMappingURL=superstruct.mjs.map
|
package/dist/superstruct.mjs.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"file":"superstruct.mjs","sourceRoot":"","sources":["../src/superstruct.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,MAAM,EACN,MAAM,EACN,MAAM,IAAI,QAAQ,EACnB,8BAA8B;AAqD/B;;;;;;GAMG;AACH,MAAM,UAAU,MAAM,CACpB,MAAc;IAEd,OAAO,QAAQ,CAAC,MAAM,CAAQ,CAAC;AACjC,CA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ectSchema,\n OmitBy,\n Optionalize,\n PickBy,\n Simplify,\n} from '@metamask/superstruct';\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\ndeclare const ExactOptionalSymbol: unique symbol;\n\nexport type ExactOptionalTag = {\n type: typeof ExactOptionalSymbol;\n};\n\n/**\n * Exclude type `Type` from the properties of `Obj`.\n *\n * ```ts\n * type Foo = { a: string | null; b: number };\n * type Bar = ExcludeType<Foo, null>;\n * // Bar = { a: string, b: number }\n * ```\n */\nexport type ExcludeType<Obj, Type> = {\n [K in keyof Obj]: Exclude<Obj[K], Type>;\n};\n\n/**\n * Make optional all properties that have the `ExactOptionalTag` type.\n *\n * ```ts\n * type Foo = { a: string | ExactOptionalTag; b: number};\n * type Bar = ExactOptionalize<Foo>;\n * // Bar = { a?: string; b: number}\n * ```\n */\nexport type ExactOptionalize<Schema extends object> = OmitBy<\n Schema,\n ExactOptionalTag\n> &\n Partial<ExcludeType<PickBy<Schema, ExactOptionalTag>, ExactOptionalTag>>;\n\n/**\n * Infer a type from an superstruct object schema.\n */\nexport type ObjectType<Schema extends ObjectSchema> = Simplify<\n ExactOptionalize<Optionalize<{ [K in keyof Schema]: Infer<Schema[K]> }>>\n>;\n\n/**\n * Change the return type of a superstruct object struct to support exact\n * optional properties.\n *\n * @param schema - The object schema.\n * @returns A struct representing an object with a known set of properties.\n */\nexport function object<Schema extends ObjectSchema>(\n schema: Schema,\n): Struct<ObjectType<Schema>, Schema> {\n return stObject(schema) as any;\n}\n\n/**\n * Check if the current property is present in its parent object.\n *\n * @param ctx - The context to check.\n * @returns `true` if the property is present, `false` otherwise.\n */\nfunction hasOptional(ctx: Context): boolean {\n const property: string = ctx.path[ctx.path.length - 1];\n const parent: Record<string, unknown> = ctx.branch[ctx.branch.length - 2];\n\n return property in parent;\n}\n\n/**\n * Augment a struct to allow exact-optional values. Exact-optional values can\n * be omitted but cannot be `undefined`.\n *\n * ```ts\n * const foo = object({ bar: exactOptional(string()) });\n * type Foo = Infer<typeof foo>;\n * // Foo = { bar?: string }\n * ```\n *\n * @param struct - The struct to augment.\n * @returns The augmented struct.\n */\nexport function exactOptional<Type, Schema>(\n struct: Struct<Type, Schema>,\n): Struct<Type | ExactOptionalTag, Schema> {\n return new Struct({\n ...struct,\n\n validator: (value, ctx) =>\n !hasOptional(ctx) || struct.validator(value, ctx),\n\n refiner: (value, ctx) =>\n !hasOptional(ctx) || struct.refiner(value as Type, ctx),\n });\n}\n\n/**\n * Defines a new string-struct matching a regular expression.\n *\n * Example:\n *\n * ```ts\n * const EthAddressStruct = definePattern('EthAddress', /^0x[0-9a-f]{40}$/iu);\n * ```\n *\n * @param name - Type name.\n * @param pattern - Regular expression to match.\n * @returns A new string-struct that matches the given pattern.\n */\nexport function definePattern(\n name: string,\n pattern: RegExp,\n): Struct<string, null> {\n return define<string>(\n name,\n (value: unknown): boolean =>\n typeof value === 'string' && pattern.test(value),\n );\n}\n\n/**\n * Assert that a value is valid according to a struct.\n *\n * It is similar to superstruct's mask function, but it does not ignore extra\n * properties.\n *\n * @param value - Value to check.\n * @param struct - Struct to validate the value against.\n * @param message - Error message to throw if the value is not valid.\n * @returns The value if it is valid.\n */\nexport function strictMask<Type, Schema>(\n value: unknown,\n struct: Struct<Type, Schema>,\n message?: string,\n): Type {\n assert(value, struct, message);\n return value;\n}\n"]}
|
package/dist/utils/index.cjs
DELETED
@@ -1,19 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
-
if (k2 === undefined) k2 = k;
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
-
}
|
8
|
-
Object.defineProperty(o, k2, desc);
|
9
|
-
}) : (function(o, m, k, k2) {
|
10
|
-
if (k2 === undefined) k2 = k;
|
11
|
-
o[k2] = m[k];
|
12
|
-
}));
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
15
|
-
};
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
17
|
-
__exportStar(require("./types.cjs"), exports);
|
18
|
-
__exportStar(require("./typing.cjs"), exports);
|
19
|
-
//# sourceMappingURL=index.cjs.map
|