noah-avalanche-sdk 0.1.2
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/README.md +892 -0
- package/dist/core/APIClient.d.ts +71 -0
- package/dist/core/APIClient.d.ts.map +1 -0
- package/dist/core/APIClient.js +92 -0
- package/dist/core/APIClient.js.map +1 -0
- package/dist/core/ContractClient.d.ts +38 -0
- package/dist/core/ContractClient.d.ts.map +1 -0
- package/dist/core/ContractClient.js +209 -0
- package/dist/core/ContractClient.js.map +1 -0
- package/dist/core/NoahSDK.d.ts +43 -0
- package/dist/core/NoahSDK.d.ts.map +1 -0
- package/dist/core/NoahSDK.js +93 -0
- package/dist/core/NoahSDK.js.map +1 -0
- package/dist/core/WalletAdapter.d.ts +188 -0
- package/dist/core/WalletAdapter.d.ts.map +1 -0
- package/dist/core/WalletAdapter.js +425 -0
- package/dist/core/WalletAdapter.js.map +1 -0
- package/dist/hooks/index.d.ts +18 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +15 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/useCredentials.d.ts +136 -0
- package/dist/hooks/useCredentials.d.ts.map +1 -0
- package/dist/hooks/useCredentials.js +217 -0
- package/dist/hooks/useCredentials.js.map +1 -0
- package/dist/hooks/useProtocol.d.ts +117 -0
- package/dist/hooks/useProtocol.d.ts.map +1 -0
- package/dist/hooks/useProtocol.js +165 -0
- package/dist/hooks/useProtocol.js.map +1 -0
- package/dist/hooks/useUser.d.ts +159 -0
- package/dist/hooks/useUser.d.ts.map +1 -0
- package/dist/hooks/useUser.js +188 -0
- package/dist/hooks/useUser.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/issuer/IssuerClient.d.ts +98 -0
- package/dist/issuer/IssuerClient.d.ts.map +1 -0
- package/dist/issuer/IssuerClient.js +159 -0
- package/dist/issuer/IssuerClient.js.map +1 -0
- package/dist/protocol/ProtocolClient.d.ts +124 -0
- package/dist/protocol/ProtocolClient.d.ts.map +1 -0
- package/dist/protocol/ProtocolClient.js +265 -0
- package/dist/protocol/ProtocolClient.js.map +1 -0
- package/dist/protocol/RequirementsManager.d.ts +9 -0
- package/dist/protocol/RequirementsManager.d.ts.map +1 -0
- package/dist/protocol/RequirementsManager.js +9 -0
- package/dist/protocol/RequirementsManager.js.map +1 -0
- package/dist/user/ProofGenerator.d.ts +49 -0
- package/dist/user/ProofGenerator.d.ts.map +1 -0
- package/dist/user/ProofGenerator.js +80 -0
- package/dist/user/ProofGenerator.js.map +1 -0
- package/dist/user/UserClient.d.ts +191 -0
- package/dist/user/UserClient.d.ts.map +1 -0
- package/dist/user/UserClient.js +338 -0
- package/dist/user/UserClient.js.map +1 -0
- package/dist/utils/credentials.d.ts +47 -0
- package/dist/utils/credentials.d.ts.map +1 -0
- package/dist/utils/credentials.js +99 -0
- package/dist/utils/credentials.js.map +1 -0
- package/dist/utils/identity.d.ts +22 -0
- package/dist/utils/identity.d.ts.map +1 -0
- package/dist/utils/identity.js +35 -0
- package/dist/utils/identity.js.map +1 -0
- package/dist/utils/jurisdiction.d.ts +21 -0
- package/dist/utils/jurisdiction.d.ts.map +1 -0
- package/dist/utils/jurisdiction.js +64 -0
- package/dist/utils/jurisdiction.js.map +1 -0
- package/dist/utils/mrz.d.ts +16 -0
- package/dist/utils/mrz.d.ts.map +1 -0
- package/dist/utils/mrz.js +91 -0
- package/dist/utils/mrz.js.map +1 -0
- package/dist/utils/ocr.d.ts +31 -0
- package/dist/utils/ocr.d.ts.map +1 -0
- package/dist/utils/ocr.js +69 -0
- package/dist/utils/ocr.js.map +1 -0
- package/dist/utils/types.d.ts +122 -0
- package/dist/utils/types.d.ts.map +1 -0
- package/dist/utils/types.js +2 -0
- package/dist/utils/types.js.map +1 -0
- package/package.json +53 -0
- package/src/core/APIClient.ts +165 -0
- package/src/core/ContractClient.ts +266 -0
- package/src/core/NoahSDK.ts +123 -0
- package/src/core/WalletAdapter.ts +546 -0
- package/src/hooks/index.ts +31 -0
- package/src/hooks/types.d.ts +18 -0
- package/src/hooks/useCredentials.ts +359 -0
- package/src/hooks/useProtocol.ts +284 -0
- package/src/hooks/useUser.ts +331 -0
- package/src/index.ts +80 -0
- package/src/issuer/IssuerClient.ts +209 -0
- package/src/protocol/ProtocolClient.ts +330 -0
- package/src/protocol/RequirementsManager.ts +16 -0
- package/src/user/ProofGenerator.ts +113 -0
- package/src/user/UserClient.ts +440 -0
- package/src/utils/credentials.ts +122 -0
- package/src/utils/identity.ts +46 -0
- package/src/utils/jurisdiction.ts +83 -0
- package/src/utils/mrz.ts +113 -0
- package/src/utils/ocr.ts +84 -0
- package/src/utils/types.ts +144 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import type { Signer } from 'ethers';
|
|
2
|
+
import type { TransactionResult } from '../utils/types';
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for useCredentials hook
|
|
5
|
+
*/
|
|
6
|
+
export interface UseCredentialsOptions {
|
|
7
|
+
/** User address to get credentials for */
|
|
8
|
+
userAddress?: string;
|
|
9
|
+
/** Issuer address to get credentials from */
|
|
10
|
+
issuerAddress?: string;
|
|
11
|
+
/** Credential hash to check */
|
|
12
|
+
credentialHash?: string;
|
|
13
|
+
/** Whether to enable automatic refetching */
|
|
14
|
+
enabled?: boolean;
|
|
15
|
+
/** React Query configuration options */
|
|
16
|
+
queryOptions?: {
|
|
17
|
+
refetchInterval?: number;
|
|
18
|
+
staleTime?: number;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Credential information structure
|
|
23
|
+
*/
|
|
24
|
+
export interface CredentialInfo {
|
|
25
|
+
credentialHash: string;
|
|
26
|
+
userAddress: string;
|
|
27
|
+
issuerAddress: string;
|
|
28
|
+
isValid: boolean;
|
|
29
|
+
isRevoked: boolean;
|
|
30
|
+
timestamp?: bigint;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Issuer information structure
|
|
34
|
+
*/
|
|
35
|
+
export interface IssuerInfo {
|
|
36
|
+
isTrusted: boolean;
|
|
37
|
+
name: string;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Return type for useCredentials hook
|
|
41
|
+
*/
|
|
42
|
+
export interface UseCredentialsReturn {
|
|
43
|
+
credentialInfo: CredentialInfo | undefined;
|
|
44
|
+
isLoadingCredential: boolean;
|
|
45
|
+
credentialError: Error | null;
|
|
46
|
+
refetchCredential: () => void;
|
|
47
|
+
issuerInfo: IssuerInfo | undefined;
|
|
48
|
+
isLoadingIssuer: boolean;
|
|
49
|
+
issuerError: Error | null;
|
|
50
|
+
refetchIssuer: () => void;
|
|
51
|
+
userCredentials: CredentialInfo[] | undefined;
|
|
52
|
+
isLoadingUserCredentials: boolean;
|
|
53
|
+
userCredentialsError: Error | null;
|
|
54
|
+
refetchUserCredentials: () => void;
|
|
55
|
+
issuerCredentials: CredentialInfo[] | undefined;
|
|
56
|
+
isLoadingIssuerCredentials: boolean;
|
|
57
|
+
issuerCredentialsError: Error | null;
|
|
58
|
+
refetchIssuerCredentials: () => void;
|
|
59
|
+
registerCredential: {
|
|
60
|
+
mutate: (params: {
|
|
61
|
+
credentialHash: string;
|
|
62
|
+
userAddress: string;
|
|
63
|
+
}) => void;
|
|
64
|
+
mutateAsync: (params: {
|
|
65
|
+
credentialHash: string;
|
|
66
|
+
userAddress: string;
|
|
67
|
+
}) => Promise<TransactionResult>;
|
|
68
|
+
isLoading: boolean;
|
|
69
|
+
error: Error | null;
|
|
70
|
+
reset: () => void;
|
|
71
|
+
};
|
|
72
|
+
revokeCredential: {
|
|
73
|
+
mutate: (params: {
|
|
74
|
+
credentialHash: string;
|
|
75
|
+
}) => void;
|
|
76
|
+
mutateAsync: (params: {
|
|
77
|
+
credentialHash: string;
|
|
78
|
+
}) => Promise<TransactionResult>;
|
|
79
|
+
isLoading: boolean;
|
|
80
|
+
error: Error | null;
|
|
81
|
+
reset: () => void;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* React hook for credential management operations
|
|
86
|
+
*
|
|
87
|
+
* Provides easy access to credential-related functionality including:
|
|
88
|
+
* - Checking credential validity
|
|
89
|
+
* - Getting issuer information
|
|
90
|
+
* - Listing user and issuer credentials
|
|
91
|
+
* - Registering and revoking credentials (for issuers)
|
|
92
|
+
*
|
|
93
|
+
* @param signer - Ethers signer instance (from wallet, required for write operations)
|
|
94
|
+
* @param options - Configuration options
|
|
95
|
+
* @returns Hook return object with credential operations
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```tsx
|
|
99
|
+
* import { useCredentials } from '@noah-protocol/sdk/hooks';
|
|
100
|
+
* import { useSigner, useAccount } from 'wagmi';
|
|
101
|
+
*
|
|
102
|
+
* function CredentialManager() {
|
|
103
|
+
* const { data: signer } = useSigner();
|
|
104
|
+
* const { address } = useAccount();
|
|
105
|
+
* const {
|
|
106
|
+
* credentialInfo,
|
|
107
|
+
* registerCredential,
|
|
108
|
+
* revokeCredential
|
|
109
|
+
* } = useCredentials(signer, {
|
|
110
|
+
* credentialHash: '0x...',
|
|
111
|
+
* userAddress: address
|
|
112
|
+
* });
|
|
113
|
+
*
|
|
114
|
+
* const handleRegister = async () => {
|
|
115
|
+
* await registerCredential.mutateAsync({
|
|
116
|
+
* credentialHash: '0x...',
|
|
117
|
+
* userAddress: '0x...'
|
|
118
|
+
* });
|
|
119
|
+
* };
|
|
120
|
+
*
|
|
121
|
+
* return (
|
|
122
|
+
* <div>
|
|
123
|
+
* {credentialInfo?.isValid ? (
|
|
124
|
+
* <p>Credential is valid</p>
|
|
125
|
+
* ) : (
|
|
126
|
+
* <button onClick={handleRegister}>
|
|
127
|
+
* Register Credential
|
|
128
|
+
* </button>
|
|
129
|
+
* )}
|
|
130
|
+
* </div>
|
|
131
|
+
* );
|
|
132
|
+
* }
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export declare function useCredentials(signer: Signer | null | undefined, options?: UseCredentialsOptions): UseCredentialsReturn;
|
|
136
|
+
//# sourceMappingURL=useCredentials.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCredentials.d.ts","sourceRoot":"","sources":["../../src/hooks/useCredentials.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAExD;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,0CAA0C;IAC1C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,6CAA6C;IAC7C,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,+BAA+B;IAC/B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,6CAA6C;IAC7C,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,wCAAwC;IACxC,YAAY,CAAC,EAAE;QACb,eAAe,CAAC,EAAE,MAAM,CAAC;QACzB,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB,CAAC;CACH;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,cAAc,EAAE,MAAM,CAAC;IACvB,WAAW,EAAE,MAAM,CAAC;IACpB,aAAa,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,OAAO,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,SAAS,EAAE,OAAO,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IAEnC,cAAc,EAAE,cAAc,GAAG,SAAS,CAAC;IAC3C,mBAAmB,EAAE,OAAO,CAAC;IAC7B,eAAe,EAAE,KAAK,GAAG,IAAI,CAAC;IAC9B,iBAAiB,EAAE,MAAM,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CAAC;CACH;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EACjC,OAAO,GAAE,qBAA0B,GAClC,oBAAoB,CA+MtB"}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
2
|
+
import { useMemo } from 'react';
|
|
3
|
+
/**
|
|
4
|
+
* React hook for credential management operations
|
|
5
|
+
*
|
|
6
|
+
* Provides easy access to credential-related functionality including:
|
|
7
|
+
* - Checking credential validity
|
|
8
|
+
* - Getting issuer information
|
|
9
|
+
* - Listing user and issuer credentials
|
|
10
|
+
* - Registering and revoking credentials (for issuers)
|
|
11
|
+
*
|
|
12
|
+
* @param signer - Ethers signer instance (from wallet, required for write operations)
|
|
13
|
+
* @param options - Configuration options
|
|
14
|
+
* @returns Hook return object with credential operations
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* import { useCredentials } from '@noah-protocol/sdk/hooks';
|
|
19
|
+
* import { useSigner, useAccount } from 'wagmi';
|
|
20
|
+
*
|
|
21
|
+
* function CredentialManager() {
|
|
22
|
+
* const { data: signer } = useSigner();
|
|
23
|
+
* const { address } = useAccount();
|
|
24
|
+
* const {
|
|
25
|
+
* credentialInfo,
|
|
26
|
+
* registerCredential,
|
|
27
|
+
* revokeCredential
|
|
28
|
+
* } = useCredentials(signer, {
|
|
29
|
+
* credentialHash: '0x...',
|
|
30
|
+
* userAddress: address
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* const handleRegister = async () => {
|
|
34
|
+
* await registerCredential.mutateAsync({
|
|
35
|
+
* credentialHash: '0x...',
|
|
36
|
+
* userAddress: '0x...'
|
|
37
|
+
* });
|
|
38
|
+
* };
|
|
39
|
+
*
|
|
40
|
+
* return (
|
|
41
|
+
* <div>
|
|
42
|
+
* {credentialInfo?.isValid ? (
|
|
43
|
+
* <p>Credential is valid</p>
|
|
44
|
+
* ) : (
|
|
45
|
+
* <button onClick={handleRegister}>
|
|
46
|
+
* Register Credential
|
|
47
|
+
* </button>
|
|
48
|
+
* )}
|
|
49
|
+
* </div>
|
|
50
|
+
* );
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function useCredentials(signer, options = {}) {
|
|
55
|
+
const { userAddress, issuerAddress, credentialHash, enabled = true, queryOptions = {}, } = options;
|
|
56
|
+
const queryClient = useQueryClient();
|
|
57
|
+
// Get ContractClient instance (assuming it's available)
|
|
58
|
+
const contractClient = useMemo(() => {
|
|
59
|
+
try {
|
|
60
|
+
const { ContractClient } = require('../core/ContractClient');
|
|
61
|
+
return new ContractClient();
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
console.warn('ContractClient not available:', error);
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}, []);
|
|
68
|
+
// Initialize contract client with provider if signer is available
|
|
69
|
+
useMemo(() => {
|
|
70
|
+
if (contractClient && signer) {
|
|
71
|
+
const provider = signer.provider;
|
|
72
|
+
if (provider) {
|
|
73
|
+
contractClient.initialize(provider);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}, [contractClient, signer]);
|
|
77
|
+
// Query: Get credential info
|
|
78
|
+
const { data: credentialInfo, isLoading: isLoadingCredential, error: credentialError, refetch: refetchCredential, } = useQuery({
|
|
79
|
+
queryKey: ['credentials', 'info', credentialHash],
|
|
80
|
+
queryFn: async () => {
|
|
81
|
+
if (!contractClient || !credentialHash) {
|
|
82
|
+
throw new Error('Contract client or credential hash not available');
|
|
83
|
+
}
|
|
84
|
+
const isValid = await contractClient.isCredentialValid(credentialHash);
|
|
85
|
+
// Get issuer info if available
|
|
86
|
+
let issuerAddress = '';
|
|
87
|
+
try {
|
|
88
|
+
// This would require additional contract methods
|
|
89
|
+
// For now, we'll return basic info
|
|
90
|
+
issuerAddress = '';
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
// Ignore if not available
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
credentialHash,
|
|
97
|
+
userAddress: userAddress || '',
|
|
98
|
+
issuerAddress,
|
|
99
|
+
isValid,
|
|
100
|
+
isRevoked: !isValid,
|
|
101
|
+
};
|
|
102
|
+
},
|
|
103
|
+
enabled: enabled && !!contractClient && !!credentialHash,
|
|
104
|
+
...queryOptions,
|
|
105
|
+
});
|
|
106
|
+
// Query: Get issuer info
|
|
107
|
+
const { data: issuerInfo, isLoading: isLoadingIssuer, error: issuerError, refetch: refetchIssuer, } = useQuery({
|
|
108
|
+
queryKey: ['credentials', 'issuer', issuerAddress],
|
|
109
|
+
queryFn: async () => {
|
|
110
|
+
if (!contractClient || !issuerAddress) {
|
|
111
|
+
throw new Error('Contract client or issuer address not available');
|
|
112
|
+
}
|
|
113
|
+
return contractClient.getIssuerInfo(issuerAddress);
|
|
114
|
+
},
|
|
115
|
+
enabled: enabled && !!contractClient && !!issuerAddress,
|
|
116
|
+
...queryOptions,
|
|
117
|
+
});
|
|
118
|
+
// Query: Get user credentials (would need API endpoint or contract method)
|
|
119
|
+
const { data: userCredentials, isLoading: isLoadingUserCredentials, error: userCredentialsError, refetch: refetchUserCredentials, } = useQuery({
|
|
120
|
+
queryKey: ['credentials', 'user', userAddress],
|
|
121
|
+
queryFn: async () => {
|
|
122
|
+
if (!userAddress) {
|
|
123
|
+
throw new Error('User address not available');
|
|
124
|
+
}
|
|
125
|
+
// This would typically call an API endpoint or contract method
|
|
126
|
+
// For now, return empty array
|
|
127
|
+
return [];
|
|
128
|
+
},
|
|
129
|
+
enabled: enabled && !!userAddress,
|
|
130
|
+
...queryOptions,
|
|
131
|
+
});
|
|
132
|
+
// Query: Get issuer credentials (would need API endpoint or contract method)
|
|
133
|
+
const { data: issuerCredentials, isLoading: isLoadingIssuerCredentials, error: issuerCredentialsError, refetch: refetchIssuerCredentials, } = useQuery({
|
|
134
|
+
queryKey: ['credentials', 'issuer-list', issuerAddress],
|
|
135
|
+
queryFn: async () => {
|
|
136
|
+
if (!issuerAddress) {
|
|
137
|
+
throw new Error('Issuer address not available');
|
|
138
|
+
}
|
|
139
|
+
// This would typically call an API endpoint
|
|
140
|
+
// For now, return empty array
|
|
141
|
+
return [];
|
|
142
|
+
},
|
|
143
|
+
enabled: enabled && !!issuerAddress,
|
|
144
|
+
...queryOptions,
|
|
145
|
+
});
|
|
146
|
+
// Mutation: Register credential (requires issuer signer)
|
|
147
|
+
const registerCredentialMutation = useMutation({
|
|
148
|
+
mutationFn: async (params) => {
|
|
149
|
+
if (!contractClient || !signer) {
|
|
150
|
+
throw new Error('Contract client or signer not available');
|
|
151
|
+
}
|
|
152
|
+
return contractClient.registerCredential(signer, params.credentialHash, params.userAddress);
|
|
153
|
+
},
|
|
154
|
+
onSuccess: () => {
|
|
155
|
+
// Invalidate credential queries
|
|
156
|
+
queryClient.invalidateQueries({
|
|
157
|
+
queryKey: ['credentials'],
|
|
158
|
+
});
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
// Mutation: Revoke credential (requires issuer signer)
|
|
162
|
+
const revokeCredentialMutation = useMutation({
|
|
163
|
+
mutationFn: async (params) => {
|
|
164
|
+
if (!contractClient || !signer) {
|
|
165
|
+
throw new Error('Contract client or signer not available');
|
|
166
|
+
}
|
|
167
|
+
return contractClient.revokeCredential(signer, params.credentialHash);
|
|
168
|
+
},
|
|
169
|
+
onSuccess: () => {
|
|
170
|
+
// Invalidate credential queries
|
|
171
|
+
queryClient.invalidateQueries({
|
|
172
|
+
queryKey: ['credentials'],
|
|
173
|
+
});
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
return {
|
|
177
|
+
credentialInfo,
|
|
178
|
+
isLoadingCredential,
|
|
179
|
+
credentialError: credentialError,
|
|
180
|
+
refetchCredential: () => {
|
|
181
|
+
refetchCredential();
|
|
182
|
+
},
|
|
183
|
+
issuerInfo,
|
|
184
|
+
isLoadingIssuer,
|
|
185
|
+
issuerError: issuerError,
|
|
186
|
+
refetchIssuer: () => {
|
|
187
|
+
refetchIssuer();
|
|
188
|
+
},
|
|
189
|
+
userCredentials,
|
|
190
|
+
isLoadingUserCredentials,
|
|
191
|
+
userCredentialsError: userCredentialsError,
|
|
192
|
+
refetchUserCredentials: () => {
|
|
193
|
+
refetchUserCredentials();
|
|
194
|
+
},
|
|
195
|
+
issuerCredentials,
|
|
196
|
+
isLoadingIssuerCredentials,
|
|
197
|
+
issuerCredentialsError: issuerCredentialsError,
|
|
198
|
+
refetchIssuerCredentials: () => {
|
|
199
|
+
refetchIssuerCredentials();
|
|
200
|
+
},
|
|
201
|
+
registerCredential: {
|
|
202
|
+
mutate: registerCredentialMutation.mutate,
|
|
203
|
+
mutateAsync: registerCredentialMutation.mutateAsync,
|
|
204
|
+
isLoading: registerCredentialMutation.isPending,
|
|
205
|
+
error: registerCredentialMutation.error,
|
|
206
|
+
reset: registerCredentialMutation.reset,
|
|
207
|
+
},
|
|
208
|
+
revokeCredential: {
|
|
209
|
+
mutate: revokeCredentialMutation.mutate,
|
|
210
|
+
mutateAsync: revokeCredentialMutation.mutateAsync,
|
|
211
|
+
isLoading: revokeCredentialMutation.isPending,
|
|
212
|
+
error: revokeCredentialMutation.error,
|
|
213
|
+
reset: revokeCredentialMutation.reset,
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
//# sourceMappingURL=useCredentials.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCredentials.js","sourceRoot":"","sources":["../../src/hooks/useCredentials.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC9E,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAgGhC;;;;;;;;;;;;;;;;;;;;;;;;;;;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D,SAAS,EAAE,0BAA0B,CAAC,SAAS;YAC/C,KAAK,EAAE,0BAA0B,CAAC,KAAqB;YACvD,KAAK,EAAE,0BAA0B,CAAC,KAAK;SACxC;QACD,gBAAgB,EAAE;YAChB,MAAM,EAAE,wBAAwB,CAAC,MAAM;YACvC,WAAW,EAAE,wBAAwB,CAAC,WAAW;YACjD,SAAS,EAAE,wBAAwB,CAAC,SAAS;YAC7C,KAAK,EAAE,wBAAwB,CAAC,KAAqB;YACrD,KAAK,EAAE,wBAAwB,CAAC,KAAK;SACtC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { Signer } from 'ethers';
|
|
2
|
+
import { ProtocolClient } from '../protocol/ProtocolClient';
|
|
3
|
+
import type { Requirements, TransactionResult } from '../utils/types';
|
|
4
|
+
/**
|
|
5
|
+
* Configuration options for useProtocol hook
|
|
6
|
+
*/
|
|
7
|
+
export interface UseProtocolOptions {
|
|
8
|
+
/** Protocol contract address (optional, defaults to signer address) */
|
|
9
|
+
protocolAddress?: string;
|
|
10
|
+
/** User address to check access for */
|
|
11
|
+
userAddress?: string;
|
|
12
|
+
/** Whether to enable automatic refetching */
|
|
13
|
+
enabled?: boolean;
|
|
14
|
+
/** React Query configuration options */
|
|
15
|
+
queryOptions?: {
|
|
16
|
+
refetchInterval?: number;
|
|
17
|
+
staleTime?: number;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Return type for useProtocol hook
|
|
22
|
+
*/
|
|
23
|
+
export interface UseProtocolReturn {
|
|
24
|
+
protocol: ProtocolClient | null;
|
|
25
|
+
requirements: Requirements | undefined;
|
|
26
|
+
isLoadingRequirements: boolean;
|
|
27
|
+
requirementsError: Error | null;
|
|
28
|
+
refetchRequirements: () => void;
|
|
29
|
+
hasAccess: boolean | undefined;
|
|
30
|
+
isLoadingAccess: boolean;
|
|
31
|
+
accessError: Error | null;
|
|
32
|
+
refetchAccess: () => void;
|
|
33
|
+
setRequirements: {
|
|
34
|
+
mutate: (params: {
|
|
35
|
+
minAge: number;
|
|
36
|
+
jurisdictions: string[];
|
|
37
|
+
requireAccredited: boolean;
|
|
38
|
+
}) => void;
|
|
39
|
+
mutateAsync: (params: {
|
|
40
|
+
minAge: number;
|
|
41
|
+
jurisdictions: string[];
|
|
42
|
+
requireAccredited: boolean;
|
|
43
|
+
}) => Promise<TransactionResult>;
|
|
44
|
+
isLoading: boolean;
|
|
45
|
+
error: Error | null;
|
|
46
|
+
reset: () => void;
|
|
47
|
+
};
|
|
48
|
+
verifyAndGrantAccess: {
|
|
49
|
+
mutate: (params: {
|
|
50
|
+
proof: {
|
|
51
|
+
a: [string, string];
|
|
52
|
+
b: [[string, string], [string, string]];
|
|
53
|
+
c: [string, string];
|
|
54
|
+
};
|
|
55
|
+
publicSignals: string[];
|
|
56
|
+
credentialHash: string;
|
|
57
|
+
userAddress: string;
|
|
58
|
+
}) => void;
|
|
59
|
+
mutateAsync: (params: {
|
|
60
|
+
proof: {
|
|
61
|
+
a: [string, string];
|
|
62
|
+
b: [[string, string], [string, string]];
|
|
63
|
+
c: [string, string];
|
|
64
|
+
};
|
|
65
|
+
publicSignals: string[];
|
|
66
|
+
credentialHash: string;
|
|
67
|
+
userAddress: string;
|
|
68
|
+
}) => Promise<TransactionResult>;
|
|
69
|
+
isLoading: boolean;
|
|
70
|
+
error: Error | null;
|
|
71
|
+
reset: () => void;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* React hook for protocol operations
|
|
76
|
+
*
|
|
77
|
+
* Provides easy access to protocol-related functionality including:
|
|
78
|
+
* - Getting and setting protocol requirements
|
|
79
|
+
* - Checking user access
|
|
80
|
+
* - Verifying proofs and granting access
|
|
81
|
+
*
|
|
82
|
+
* @param signer - Ethers signer instance (from wallet)
|
|
83
|
+
* @param options - Configuration options
|
|
84
|
+
* @returns Hook return object with protocol operations
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```tsx
|
|
88
|
+
* import { useProtocol } from '@noah-protocol/sdk/hooks';
|
|
89
|
+
* import { useSigner } from 'wagmi';
|
|
90
|
+
*
|
|
91
|
+
* function ProtocolDashboard() {
|
|
92
|
+
* const { data: signer } = useSigner();
|
|
93
|
+
* const {
|
|
94
|
+
* requirements,
|
|
95
|
+
* setRequirements,
|
|
96
|
+
* hasAccess
|
|
97
|
+
* } = useProtocol(signer, {
|
|
98
|
+
* protocolAddress: '0x...',
|
|
99
|
+
* userAddress: '0x...'
|
|
100
|
+
* });
|
|
101
|
+
*
|
|
102
|
+
* return (
|
|
103
|
+
* <div>
|
|
104
|
+
* <button onClick={() => setRequirements.mutate({
|
|
105
|
+
* minAge: 21,
|
|
106
|
+
* jurisdictions: ['US', 'UK'],
|
|
107
|
+
* requireAccredited: true
|
|
108
|
+
* })}>
|
|
109
|
+
* Set Requirements
|
|
110
|
+
* </button>
|
|
111
|
+
* </div>
|
|
112
|
+
* );
|
|
113
|
+
* }
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
export declare function useProtocol(signer: Signer | null | undefined, options?: UseProtocolOptions): UseProtocolReturn;
|
|
117
|
+
//# sourceMappingURL=useProtocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useProtocol.d.ts","sourceRoot":"","sources":["../../src/hooks/useProtocol.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACrC,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAC5D,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEtE;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,uEAAuE;IACvE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,uCAAuC;IACvC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,6CAA6C;IAC7C,OAAO,CAAC,EAA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AC,CAAC;gBACxC,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;aACrB,CAAC;YACF,aAAa,EAAE,MAAM,EAAE,CAAC;YACxB,cAAc,EAAE,MAAM,CAAC;YACvB,WAAW,EAAE,MAAM,CAAC;SACrB,KAAK,IAAI,CAAC;QACX,WAAW,EAAE,CAAC,MAAM,EAAE;YACpB,KAAK,EAAE;gBACL,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;gBACpB,CAAC,EAAE,CAAC,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;gBACxC,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;aACrB,CAAC;YACF,aAAa,EAAE,MAAM,EAAE,CAAC;YACxB,cAAc,EAAE,MAAM,CAAC;YACvB,WAAW,EAAE,MAAM,CAAC;SACrB,KAAK,OAAO,CAAC,iBAAiB,CAAC,CAAC;QACjC,SAAS,EAAE,OAAO,CAAC;QACnB,KAAK,EAAE,KAAK,GAAG,IAAI,CAAC;QACpB,KAAK,EAAE,MAAM,IAAI,CAAC;KACnB,CAAC;CACH;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EACjC,OAAO,GAAE,kBAAuB,GAC/B,iBAAiB,CAuJnB"}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
2
|
+
import { useMemo, useState, useEffect } from 'react';
|
|
3
|
+
import { ProtocolClient } from '../protocol/ProtocolClient';
|
|
4
|
+
/**
|
|
5
|
+
* React hook for protocol operations
|
|
6
|
+
*
|
|
7
|
+
* Provides easy access to protocol-related functionality including:
|
|
8
|
+
* - Getting and setting protocol requirements
|
|
9
|
+
* - Checking user access
|
|
10
|
+
* - Verifying proofs and granting access
|
|
11
|
+
*
|
|
12
|
+
* @param signer - Ethers signer instance (from wallet)
|
|
13
|
+
* @param options - Configuration options
|
|
14
|
+
* @returns Hook return object with protocol operations
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* import { useProtocol } from '@noah-protocol/sdk/hooks';
|
|
19
|
+
* import { useSigner } from 'wagmi';
|
|
20
|
+
*
|
|
21
|
+
* function ProtocolDashboard() {
|
|
22
|
+
* const { data: signer } = useSigner();
|
|
23
|
+
* const {
|
|
24
|
+
* requirements,
|
|
25
|
+
* setRequirements,
|
|
26
|
+
* hasAccess
|
|
27
|
+
* } = useProtocol(signer, {
|
|
28
|
+
* protocolAddress: '0x...',
|
|
29
|
+
* userAddress: '0x...'
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* return (
|
|
33
|
+
* <div>
|
|
34
|
+
* <button onClick={() => setRequirements.mutate({
|
|
35
|
+
* minAge: 21,
|
|
36
|
+
* jurisdictions: ['US', 'UK'],
|
|
37
|
+
* requireAccredited: true
|
|
38
|
+
* })}>
|
|
39
|
+
* Set Requirements
|
|
40
|
+
* </button>
|
|
41
|
+
* </div>
|
|
42
|
+
* );
|
|
43
|
+
* }
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export function useProtocol(signer, options = {}) {
|
|
47
|
+
const { protocolAddress, userAddress, enabled = true, queryOptions = {}, } = options;
|
|
48
|
+
const queryClient = useQueryClient();
|
|
49
|
+
// Create protocol client instance
|
|
50
|
+
const protocol = useMemo(() => {
|
|
51
|
+
if (!signer)
|
|
52
|
+
return null;
|
|
53
|
+
try {
|
|
54
|
+
return new ProtocolClient(signer);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
console.warn('ProtocolClient not available:', error);
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
}, [signer]);
|
|
61
|
+
// Get current protocol address (use signer address if not provided)
|
|
62
|
+
const [currentProtocolAddress, setCurrentProtocolAddress] = useState(protocolAddress);
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
if (protocolAddress) {
|
|
65
|
+
setCurrentProtocolAddress(protocolAddress);
|
|
66
|
+
}
|
|
67
|
+
else if (signer) {
|
|
68
|
+
signer.getAddress()
|
|
69
|
+
.then(address => setCurrentProtocolAddress(address))
|
|
70
|
+
.catch(() => setCurrentProtocolAddress(undefined));
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
setCurrentProtocolAddress(undefined);
|
|
74
|
+
}
|
|
75
|
+
}, [protocolAddress, signer]);
|
|
76
|
+
// Query: Get protocol requirements
|
|
77
|
+
const { data: requirements, isLoading: isLoadingRequirements, error: requirementsError, refetch: refetchRequirements, } = useQuery({
|
|
78
|
+
queryKey: ['protocol', 'requirements', currentProtocolAddress],
|
|
79
|
+
queryFn: async () => {
|
|
80
|
+
if (!protocol || !currentProtocolAddress) {
|
|
81
|
+
throw new Error('Protocol client or address not available');
|
|
82
|
+
}
|
|
83
|
+
return protocol.getRequirements(currentProtocolAddress);
|
|
84
|
+
},
|
|
85
|
+
enabled: enabled && !!protocol && !!currentProtocolAddress,
|
|
86
|
+
...queryOptions,
|
|
87
|
+
});
|
|
88
|
+
// Query: Check user access
|
|
89
|
+
const { data: hasAccess, isLoading: isLoadingAccess, error: accessError, refetch: refetchAccess, } = useQuery({
|
|
90
|
+
queryKey: ['protocol', 'access', currentProtocolAddress, userAddress],
|
|
91
|
+
queryFn: async () => {
|
|
92
|
+
if (!protocol || !currentProtocolAddress || !userAddress) {
|
|
93
|
+
throw new Error('Protocol client, address, or user address not available');
|
|
94
|
+
}
|
|
95
|
+
return protocol.checkUserAccess(currentProtocolAddress, userAddress);
|
|
96
|
+
},
|
|
97
|
+
enabled: enabled && !!protocol && !!currentProtocolAddress && !!userAddress,
|
|
98
|
+
...queryOptions,
|
|
99
|
+
});
|
|
100
|
+
// Mutation: Set requirements
|
|
101
|
+
const setRequirementsMutation = useMutation({
|
|
102
|
+
mutationFn: async (params) => {
|
|
103
|
+
if (!protocol) {
|
|
104
|
+
throw new Error('Protocol client not available');
|
|
105
|
+
}
|
|
106
|
+
return protocol.setRequirements(params);
|
|
107
|
+
},
|
|
108
|
+
onSuccess: () => {
|
|
109
|
+
// Invalidate and refetch requirements
|
|
110
|
+
queryClient.invalidateQueries({
|
|
111
|
+
queryKey: ['protocol', 'requirements'],
|
|
112
|
+
});
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
// Mutation: Verify and grant access
|
|
116
|
+
const verifyAndGrantAccessMutation = useMutation({
|
|
117
|
+
mutationFn: async (params) => {
|
|
118
|
+
if (!protocol || !currentProtocolAddress) {
|
|
119
|
+
throw new Error('Protocol client or address not available');
|
|
120
|
+
}
|
|
121
|
+
return protocol.verifyUserAccess({
|
|
122
|
+
userAddress: params.userAddress,
|
|
123
|
+
proof: params.proof,
|
|
124
|
+
publicSignals: params.publicSignals,
|
|
125
|
+
credentialHash: params.credentialHash,
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
onSuccess: () => {
|
|
129
|
+
// Invalidate and refetch access status
|
|
130
|
+
queryClient.invalidateQueries({
|
|
131
|
+
queryKey: ['protocol', 'access'],
|
|
132
|
+
});
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
return {
|
|
136
|
+
protocol,
|
|
137
|
+
requirements,
|
|
138
|
+
isLoadingRequirements,
|
|
139
|
+
requirementsError: requirementsError,
|
|
140
|
+
refetchRequirements: () => {
|
|
141
|
+
refetchRequirements();
|
|
142
|
+
},
|
|
143
|
+
hasAccess,
|
|
144
|
+
isLoadingAccess,
|
|
145
|
+
accessError: accessError,
|
|
146
|
+
refetchAccess: () => {
|
|
147
|
+
refetchAccess();
|
|
148
|
+
},
|
|
149
|
+
setRequirements: {
|
|
150
|
+
mutate: setRequirementsMutation.mutate,
|
|
151
|
+
mutateAsync: setRequirementsMutation.mutateAsync,
|
|
152
|
+
isLoading: setRequirementsMutation.isPending,
|
|
153
|
+
error: setRequirementsMutation.error,
|
|
154
|
+
reset: setRequirementsMutation.reset,
|
|
155
|
+
},
|
|
156
|
+
verifyAndGrantAccess: {
|
|
157
|
+
mutate: verifyAndGrantAccessMutation.mutate,
|
|
158
|
+
mutateAsync: verifyAndGrantAccessMutation.mutateAsync,
|
|
159
|
+
isLoading: verifyAndGrantAccessMutation.isPending,
|
|
160
|
+
error: verifyAndGrantAccessMutation.error,
|
|
161
|
+
reset: verifyAndGrantAccessMutation.reset,
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=useProtocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useProtocol.js","sourceRoot":"","sources":["../../src/hooks/useProtocol.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC9E,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAErD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAmF5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,MAAM,UAAU,WAAW,CACzB,MAAiC,EACjC,UAA8B,EAAE;IAEhC,MAAM,EACJ,eAAe,EACf,WAAW,EACX,OAAO,GAAG,IAAI,EACd,YAAY,GAAG,EAAE,GAClB,GAAG,OAAO,CAAC;IAEZ,MAAM,WAAW,GAAG,cAAc,EAAE,CAAC;IAErC,kCAAkC;IAClC,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,EAAE;QAC5B,IAAI,CAAC,MAAM;YAAE,OAAO,IAAI,CAAC;QACzB,IAAI,CAAC;YACH,OAAO,IAAI,cAAc,CAAC,MAAM,CAAC,CAAC;QACpC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,IAAI,CAAC,+BAA+B,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