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.
Files changed (103) hide show
  1. package/README.md +892 -0
  2. package/dist/core/APIClient.d.ts +71 -0
  3. package/dist/core/APIClient.d.ts.map +1 -0
  4. package/dist/core/APIClient.js +92 -0
  5. package/dist/core/APIClient.js.map +1 -0
  6. package/dist/core/ContractClient.d.ts +38 -0
  7. package/dist/core/ContractClient.d.ts.map +1 -0
  8. package/dist/core/ContractClient.js +209 -0
  9. package/dist/core/ContractClient.js.map +1 -0
  10. package/dist/core/NoahSDK.d.ts +43 -0
  11. package/dist/core/NoahSDK.d.ts.map +1 -0
  12. package/dist/core/NoahSDK.js +93 -0
  13. package/dist/core/NoahSDK.js.map +1 -0
  14. package/dist/core/WalletAdapter.d.ts +188 -0
  15. package/dist/core/WalletAdapter.d.ts.map +1 -0
  16. package/dist/core/WalletAdapter.js +425 -0
  17. package/dist/core/WalletAdapter.js.map +1 -0
  18. package/dist/hooks/index.d.ts +18 -0
  19. package/dist/hooks/index.d.ts.map +1 -0
  20. package/dist/hooks/index.js +15 -0
  21. package/dist/hooks/index.js.map +1 -0
  22. package/dist/hooks/useCredentials.d.ts +136 -0
  23. package/dist/hooks/useCredentials.d.ts.map +1 -0
  24. package/dist/hooks/useCredentials.js +217 -0
  25. package/dist/hooks/useCredentials.js.map +1 -0
  26. package/dist/hooks/useProtocol.d.ts +117 -0
  27. package/dist/hooks/useProtocol.d.ts.map +1 -0
  28. package/dist/hooks/useProtocol.js +165 -0
  29. package/dist/hooks/useProtocol.js.map +1 -0
  30. package/dist/hooks/useUser.d.ts +159 -0
  31. package/dist/hooks/useUser.d.ts.map +1 -0
  32. package/dist/hooks/useUser.js +188 -0
  33. package/dist/hooks/useUser.js.map +1 -0
  34. package/dist/index.d.ts +28 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +26 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/issuer/IssuerClient.d.ts +98 -0
  39. package/dist/issuer/IssuerClient.d.ts.map +1 -0
  40. package/dist/issuer/IssuerClient.js +159 -0
  41. package/dist/issuer/IssuerClient.js.map +1 -0
  42. package/dist/protocol/ProtocolClient.d.ts +124 -0
  43. package/dist/protocol/ProtocolClient.d.ts.map +1 -0
  44. package/dist/protocol/ProtocolClient.js +265 -0
  45. package/dist/protocol/ProtocolClient.js.map +1 -0
  46. package/dist/protocol/RequirementsManager.d.ts +9 -0
  47. package/dist/protocol/RequirementsManager.d.ts.map +1 -0
  48. package/dist/protocol/RequirementsManager.js +9 -0
  49. package/dist/protocol/RequirementsManager.js.map +1 -0
  50. package/dist/user/ProofGenerator.d.ts +49 -0
  51. package/dist/user/ProofGenerator.d.ts.map +1 -0
  52. package/dist/user/ProofGenerator.js +80 -0
  53. package/dist/user/ProofGenerator.js.map +1 -0
  54. package/dist/user/UserClient.d.ts +191 -0
  55. package/dist/user/UserClient.d.ts.map +1 -0
  56. package/dist/user/UserClient.js +338 -0
  57. package/dist/user/UserClient.js.map +1 -0
  58. package/dist/utils/credentials.d.ts +47 -0
  59. package/dist/utils/credentials.d.ts.map +1 -0
  60. package/dist/utils/credentials.js +99 -0
  61. package/dist/utils/credentials.js.map +1 -0
  62. package/dist/utils/identity.d.ts +22 -0
  63. package/dist/utils/identity.d.ts.map +1 -0
  64. package/dist/utils/identity.js +35 -0
  65. package/dist/utils/identity.js.map +1 -0
  66. package/dist/utils/jurisdiction.d.ts +21 -0
  67. package/dist/utils/jurisdiction.d.ts.map +1 -0
  68. package/dist/utils/jurisdiction.js +64 -0
  69. package/dist/utils/jurisdiction.js.map +1 -0
  70. package/dist/utils/mrz.d.ts +16 -0
  71. package/dist/utils/mrz.d.ts.map +1 -0
  72. package/dist/utils/mrz.js +91 -0
  73. package/dist/utils/mrz.js.map +1 -0
  74. package/dist/utils/ocr.d.ts +31 -0
  75. package/dist/utils/ocr.d.ts.map +1 -0
  76. package/dist/utils/ocr.js +69 -0
  77. package/dist/utils/ocr.js.map +1 -0
  78. package/dist/utils/types.d.ts +122 -0
  79. package/dist/utils/types.d.ts.map +1 -0
  80. package/dist/utils/types.js +2 -0
  81. package/dist/utils/types.js.map +1 -0
  82. package/package.json +53 -0
  83. package/src/core/APIClient.ts +165 -0
  84. package/src/core/ContractClient.ts +266 -0
  85. package/src/core/NoahSDK.ts +123 -0
  86. package/src/core/WalletAdapter.ts +546 -0
  87. package/src/hooks/index.ts +31 -0
  88. package/src/hooks/types.d.ts +18 -0
  89. package/src/hooks/useCredentials.ts +359 -0
  90. package/src/hooks/useProtocol.ts +284 -0
  91. package/src/hooks/useUser.ts +331 -0
  92. package/src/index.ts +80 -0
  93. package/src/issuer/IssuerClient.ts +209 -0
  94. package/src/protocol/ProtocolClient.ts +330 -0
  95. package/src/protocol/RequirementsManager.ts +16 -0
  96. package/src/user/ProofGenerator.ts +113 -0
  97. package/src/user/UserClient.ts +440 -0
  98. package/src/utils/credentials.ts +122 -0
  99. package/src/utils/identity.ts +46 -0
  100. package/src/utils/jurisdiction.ts +83 -0
  101. package/src/utils/mrz.ts +113 -0
  102. package/src/utils/ocr.ts +84 -0
  103. package/src/utils/types.ts +144 -0
@@ -0,0 +1,113 @@
1
+ export interface MRZData {
2
+ documentType: string;
3
+ issuingState: string;
4
+ lastName: string;
5
+ firstName: string;
6
+ passportNumber: string;
7
+ nationality: string;
8
+ dateOfBirth: Date;
9
+ gender: string;
10
+ expiryDate: Date;
11
+ personalNumber: string;
12
+ age: number;
13
+ }
14
+
15
+ export function validateCheckDigit(str: string, checkDigit: string): boolean {
16
+ const weights = [7, 3, 1];
17
+ let sum = 0;
18
+ for (let i = 0; i < str.length; i++) {
19
+ const char = str[i];
20
+ let value = 0;
21
+ if (char === '<') {
22
+ value = 0;
23
+ } else if (/[0-9]/.test(char)) {
24
+ value = parseInt(char);
25
+ } else if (/[A-Z]/.test(char)) {
26
+ value = char.charCodeAt(0) - 65 + 10;
27
+ }
28
+ sum += value * weights[i % 3];
29
+ }
30
+ return (sum % 10) === parseInt(checkDigit);
31
+ }
32
+
33
+ function parseDate(str: string, isDOB: boolean = false): Date {
34
+ const yearStr = str.substring(0, 2);
35
+ const month = parseInt(str.substring(2, 4)) - 1;
36
+ const day = parseInt(str.substring(4, 6));
37
+
38
+ let year = parseInt(yearStr);
39
+ const currentYear = new Date().getFullYear() % 100;
40
+
41
+ if (isDOB) {
42
+ if (year > currentYear) {
43
+ year += 1900;
44
+ } else {
45
+ year += 2000;
46
+ }
47
+ } else {
48
+ // For expiry date, assume 20xx
49
+ year += 2000;
50
+ }
51
+
52
+ return new Date(year, month, day);
53
+ }
54
+
55
+ export function parseTD3(line1: string, line2: string): MRZData {
56
+ if (line1.length !== 44 || line2.length !== 44) {
57
+ throw new Error('Invalid TD3 MRZ length');
58
+ }
59
+
60
+ const documentType = line1.substring(0, 2).replace(/</g, '');
61
+ const issuingState = line1.substring(2, 5).replace(/</g, '');
62
+
63
+ const namesPart = line1.substring(5);
64
+ const [lastNamePart, firstNamePart] = namesPart.split('<<');
65
+ const lastName = lastNamePart.replace(/</g, ' ').trim();
66
+ const firstName = (firstNamePart || '').replace(/</g, ' ').trim();
67
+
68
+ const passportNumber = line2.substring(0, 9).replace(/</g, '');
69
+ const passportCheck = line2.substring(9, 10);
70
+ if (!validateCheckDigit(line2.substring(0, 9), passportCheck)) {
71
+ throw new Error('Invalid passport number check digit');
72
+ }
73
+
74
+ const nationality = line2.substring(10, 13).replace(/</g, '');
75
+ const dobStr = line2.substring(13, 19);
76
+ const dobCheck = line2.substring(19, 20);
77
+ if (!validateCheckDigit(dobStr, dobCheck)) {
78
+ throw new Error('Invalid date of birth check digit');
79
+ }
80
+ const dateOfBirth = parseDate(dobStr, true);
81
+
82
+ const gender = line2.substring(20, 21);
83
+ const expiryStr = line2.substring(21, 27);
84
+ const expiryCheck = line2.substring(27, 28);
85
+ if (!validateCheckDigit(expiryStr, expiryCheck)) {
86
+ throw new Error('Invalid expiry date check digit');
87
+ }
88
+ const expiryDate = parseDate(expiryStr, false);
89
+
90
+ const personalNumber = line2.substring(28, 42).replace(/</g, '');
91
+
92
+ // Calculate age
93
+ const today = new Date();
94
+ let age = today.getFullYear() - dateOfBirth.getFullYear();
95
+ const m = today.getMonth() - dateOfBirth.getMonth();
96
+ if (m < 0 || (m === 0 && today.getDate() < dateOfBirth.getDate())) {
97
+ age--;
98
+ }
99
+
100
+ return {
101
+ documentType,
102
+ issuingState,
103
+ lastName,
104
+ firstName,
105
+ passportNumber,
106
+ nationality,
107
+ dateOfBirth,
108
+ gender,
109
+ expiryDate,
110
+ personalNumber,
111
+ age,
112
+ };
113
+ }
@@ -0,0 +1,84 @@
1
+ import { createWorker, type Worker } from 'tesseract.js';
2
+
3
+ export interface OCROutput {
4
+ rawText: string;
5
+ mrzLines: string[];
6
+ confidence: number;
7
+ }
8
+
9
+ /**
10
+ * OCR Extractor for processing identity documents in the browser
11
+ */
12
+ export class OCRExtractor {
13
+ private worker: Worker | null = null;
14
+ private initialized: boolean = false;
15
+
16
+ /**
17
+ * Initialize the Tesseract worker
18
+ */
19
+ async initialize() {
20
+ if (this.initialized) return;
21
+
22
+ this.worker = await createWorker('eng'); // MRZ is always Latin characters
23
+ this.initialized = true;
24
+ }
25
+
26
+ /**
27
+ * Extract MRZ data from an image
28
+ * @param imageSource - URL, File, or Blob of the document image
29
+ * @returns Extracted text and MRZ lines
30
+ */
31
+ async extractMRZ(imageSource: string | File | Blob): Promise<OCROutput> {
32
+ await this.initialize();
33
+
34
+ if (!this.worker) {
35
+ throw new Error('OCR Worker not initialized');
36
+ }
37
+
38
+ // Set parameters to optimize for MRZ
39
+ // MRZ uses a specific OCR-B font, but standard 'eng' is usually sufficient
40
+ // We can restrict characters to A-Z, 0-9, and '<'
41
+ await this.worker.setParameters({
42
+ tessedit_char_whitelist: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<',
43
+ });
44
+
45
+ const { data: { text, confidence } } = await this.worker.recognize(imageSource);
46
+
47
+ const mrzLines = this.filterMRZLines(text);
48
+
49
+ return {
50
+ rawText: text,
51
+ mrzLines,
52
+ confidence,
53
+ };
54
+ }
55
+
56
+ /**
57
+ * Filter and clean MRZ lines from raw OCR text
58
+ */
59
+ private filterMRZLines(text: string): string[] {
60
+ const lines = text.split('\n').map(l => l.trim().replace(/\s/g, ''));
61
+
62
+ // TD3 MRZ (Passport) is 2 lines of 44 characters
63
+ // TD1 (ID Card) is 3 lines of 30 characters
64
+ // We look for lines containing multiple '<' characters
65
+ return lines.filter(line => {
66
+ const charCount = line.length;
67
+ const chevronCount = (line.match(/</g) || []).length;
68
+
69
+ // Heuristic: MRZ lines are long and have many chevrons
70
+ return (charCount >= 30 && chevronCount >= 2);
71
+ });
72
+ }
73
+
74
+ /**
75
+ * Terminate the worker to free resources
76
+ */
77
+ async terminate() {
78
+ if (this.worker) {
79
+ await this.worker.terminate();
80
+ this.worker = null;
81
+ this.initialized = false;
82
+ }
83
+ }
84
+ }
@@ -0,0 +1,144 @@
1
+ import type {
2
+ Provider,
3
+ ContractTransactionReceipt,
4
+ EventLog,
5
+ Log
6
+ } from 'ethers';
7
+
8
+ /**
9
+ * Contract addresses configuration
10
+ */
11
+ export interface ContractAddresses {
12
+ CredentialRegistry: string;
13
+ ZKVerifier: string;
14
+ ProtocolAccessControl: string;
15
+ }
16
+
17
+ /**
18
+ * Protocol requirements structure
19
+ */
20
+ export interface Requirements {
21
+ minAge: number;
22
+ allowedJurisdictions: string[];
23
+ requireAccredited: boolean;
24
+ isSet?: boolean;
25
+ }
26
+
27
+ /**
28
+ * Issuer information structure
29
+ */
30
+ export interface IssuerInfo {
31
+ isTrusted: boolean;
32
+ name: string;
33
+ }
34
+
35
+ /**
36
+ * ZK Proof structure (Groth16 format)
37
+ */
38
+ export interface Proof {
39
+ a: [string, string];
40
+ b: [[string, string], [string, string]];
41
+ c: [string, string];
42
+ }
43
+
44
+ /**
45
+ * ZK Proof structure (alias for compatibility)
46
+ */
47
+ export type ZKProof = Proof;
48
+
49
+ /**
50
+ * Transaction result
51
+ */
52
+ export interface TransactionResult {
53
+ transactionHash: string;
54
+ receipt: ContractTransactionReceipt | null;
55
+ }
56
+
57
+ /**
58
+ * Contract client configuration options
59
+ */
60
+ export interface ContractClientConfig {
61
+ provider?: Provider;
62
+ contractAddresses?: ContractAddresses;
63
+ rpcUrl?: string;
64
+ }
65
+
66
+ /**
67
+ * Protocol client configuration
68
+ */
69
+ export interface ProtocolClientConfig {
70
+ protocolAccessControlAddress?: string;
71
+ provider?: Provider;
72
+ }
73
+
74
+ /**
75
+ * Set requirements parameters
76
+ */
77
+ export interface SetRequirementsParams {
78
+ minAge: number;
79
+ jurisdictions: string[] | number[];
80
+ requireAccredited: boolean;
81
+ }
82
+
83
+ /**
84
+ * Verify user access parameters
85
+ */
86
+ export interface VerifyUserAccessParams {
87
+ userAddress: string;
88
+ proof: Proof;
89
+ publicSignals: string[] | number[];
90
+ credentialHash: string;
91
+ }
92
+
93
+ /**
94
+ * Credential issued event payload
95
+ */
96
+ export interface CredentialIssuedEvent {
97
+ user: string;
98
+ credentialHash: string;
99
+ issuer: string;
100
+ timestamp: bigint;
101
+ }
102
+
103
+ /**
104
+ * Access granted event payload
105
+ */
106
+ export interface AccessGrantedEvent {
107
+ user: string;
108
+ protocol: string;
109
+ credentialHash: string;
110
+ timestamp: bigint;
111
+ }
112
+
113
+ /**
114
+ * Contract event listener callback type
115
+ */
116
+ export type EventCallback<T = any> = (
117
+ ...args: T[]
118
+ ) => void | Promise<void>;
119
+
120
+ /**
121
+ * Contract event payload type
122
+ */
123
+ export type ContractEventPayload = EventLog | Log;
124
+
125
+ /**
126
+ * Credential data structure for hash generation
127
+ */
128
+ export interface CredentialData {
129
+ userAddress: string;
130
+ age: number;
131
+ jurisdiction: string;
132
+ accredited: boolean;
133
+ timestamp?: number;
134
+ }
135
+
136
+ /**
137
+ * Credential hash generation result
138
+ */
139
+ export interface CredentialHashResult {
140
+ credentialHash: string;
141
+ jurisdictionHash: string;
142
+ credentialData: string;
143
+ timestamp: number;
144
+ }