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
package/src/utils/mrz.ts
ADDED
|
@@ -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
|
+
}
|
package/src/utils/ocr.ts
ADDED
|
@@ -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
|
+
}
|