mirror of
https://github.com/selfxyz/self.git
synced 2026-04-27 03:01:15 -04:00
upgrade sdk stability (#196)
This commit is contained in:
@@ -18,13 +18,12 @@ const selfBackendVerifier = new SelfBackendVerifier(
|
||||
## Setup
|
||||
You can setup which data you want to verify in this sdk
|
||||
```typescript
|
||||
// In default, verification will be done with latest identity commitment root, but if you have some other root in your mind, you can choose with timestamp
|
||||
selfBackendVerifier.setTargetRootTimestamp(0);
|
||||
// Set minimum age verification
|
||||
selfBackendVerifier.setMinimumAge(20);
|
||||
// Set nationality verification
|
||||
selfBackendVerifier.setNationality('France')
|
||||
// Set exclude countries verification
|
||||
// At most 40
|
||||
selfBackendVerifier.excludeCountries('Country Name1', 'Country Name2', 'Coutry Name3', 'etc...');
|
||||
// Enable if you want to do passport number ofac check
|
||||
// Default false
|
||||
@@ -108,6 +107,75 @@ export interface SelfVerificationResult {
|
||||
}
|
||||
```
|
||||
|
||||
## How to return the result in your api implementation
|
||||
This backend SDK is designed to be used with APIs managed by third parties, and it communicates with Self's managed relayer to enable smooth usage of applications provided by Self.
|
||||
|
||||
When using it:
|
||||
1. Set the endpoint of the API that imports this backend SDK in SelfAppBuilder
|
||||
```typescript
|
||||
const selfApp = new SelfAppBuilder({
|
||||
appName: "Application name",
|
||||
scope: "Application id",
|
||||
endpoint: "API endpoint which imports this backend sdk",
|
||||
logoBase64: logo,
|
||||
userId,
|
||||
disclosures: {
|
||||
name: true,
|
||||
nationality: true,
|
||||
date_of_birth: true,
|
||||
passport_number: true,
|
||||
minimumAge: 20,
|
||||
excludedCountries: [
|
||||
"Exclude countries which you want"
|
||||
],
|
||||
ofac: true,
|
||||
}
|
||||
}).build();
|
||||
```
|
||||
|
||||
2. This API needs to return values in the following format:
|
||||
```typescript
|
||||
response: {
|
||||
200: t.Object({
|
||||
status: t.String(),
|
||||
result: t.Boolean(),
|
||||
}),
|
||||
500: t.Object({
|
||||
status: t.String(),
|
||||
result: t.Boolean(),
|
||||
message: t.String(),
|
||||
}),
|
||||
},
|
||||
```
|
||||
Bit more explanation to the values in the return value.
|
||||
|
||||
status: Indicates that the API processing has completed successfully
|
||||
|
||||
result: Contains the verification result from the SelfBackendVerifier
|
||||
|
||||
message: Represents the error details when an error occurs
|
||||
|
||||
Here is the little example to implement the api.
|
||||
|
||||
```typescript
|
||||
try {
|
||||
const result = await selfBackendVerifier.verify(
|
||||
request.body.proof,
|
||||
request.body.publicSignals
|
||||
);
|
||||
return {
|
||||
status: "success",
|
||||
result: result.isValid,
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
status: "error",
|
||||
result: false,
|
||||
message: error instanceof Error ? error.message : "Unknown error",
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
# When you run the tests
|
||||
|
||||
First you need to copy the abi files to the sdk/core/src/abi folder.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@openpassport/core",
|
||||
"version": "0.0.23",
|
||||
"version": "0.0.24",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/zk-passport/openpassport"
|
||||
|
||||
@@ -2,7 +2,7 @@ import { registryAbi } from './abi/IdentityRegistryImplV1';
|
||||
import { verifyAllAbi } from './abi/VerifyAll';
|
||||
import { REGISTRY_ADDRESS, VERIFYALL_ADDRESS } from './constants/contractAddresses';
|
||||
import { ethers } from 'ethers';
|
||||
import { Groth16Proof, PublicSignals } from 'snarkjs';
|
||||
import { PublicSignals } from 'snarkjs';
|
||||
import {
|
||||
countryCodes,
|
||||
countryNames,
|
||||
@@ -52,8 +52,6 @@ export class SelfBackendVerifier {
|
||||
getCountryCode(country)
|
||||
);
|
||||
const forbiddenCountriesListPacked = packForbiddenCountriesList(excludedCountryCodes);
|
||||
const packedValue =
|
||||
forbiddenCountriesListPacked.length > 0 ? forbiddenCountriesListPacked : ['0','0','0','0'];
|
||||
|
||||
const isValidScope =
|
||||
this.scope ===
|
||||
@@ -67,7 +65,7 @@ export class SelfBackendVerifier {
|
||||
olderThanEnabled: this.minimumAge.enabled,
|
||||
olderThan: this.minimumAge.value,
|
||||
forbiddenCountriesEnabled: this.excludedCountries.enabled,
|
||||
forbiddenCountriesListPacked: packedValue,
|
||||
forbiddenCountriesListPacked: forbiddenCountriesListPacked,
|
||||
ofacEnabled: [this.passportNoOfac, this.nameAndDobOfac, this.nameAndYobOfac],
|
||||
vcAndDiscloseProof: {
|
||||
a: proof.a,
|
||||
@@ -91,13 +89,8 @@ export class SelfBackendVerifier {
|
||||
revealedDataTypes.name_and_yob_ofac,
|
||||
];
|
||||
|
||||
let timestamp;
|
||||
if (this.targetRootTimestamp.enabled) {
|
||||
timestamp = this.targetRootTimestamp.value;
|
||||
} else {
|
||||
const currentRoot = await this.registryContract.getIdentityCommitmentMerkleRoot();
|
||||
timestamp = await this.registryContract.rootTimestamps(currentRoot);
|
||||
}
|
||||
const currentRoot = await this.registryContract.getIdentityCommitmentMerkleRoot();
|
||||
const timestamp = await this.registryContract.rootTimestamps(currentRoot);
|
||||
|
||||
let result: any;
|
||||
try {
|
||||
@@ -121,6 +114,7 @@ export class SelfBackendVerifier {
|
||||
publicSignals: publicSignals,
|
||||
},
|
||||
},
|
||||
error: error
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,16 +160,12 @@ export class SelfBackendVerifier {
|
||||
publicSignals: publicSignals,
|
||||
},
|
||||
},
|
||||
error: result[2]
|
||||
};
|
||||
|
||||
return attestation;
|
||||
}
|
||||
|
||||
setTargetRootTimestamp(targetRootTimestamp: number): this {
|
||||
this.targetRootTimestamp = { enabled: true, value: targetRootTimestamp };
|
||||
return this;
|
||||
}
|
||||
|
||||
setMinimumAge(age: number): this {
|
||||
if (age < 10) {
|
||||
throw new Error('Minimum age must be at least 10 years old');
|
||||
@@ -192,12 +182,10 @@ export class SelfBackendVerifier {
|
||||
return this;
|
||||
}
|
||||
|
||||
discloseNationality(): this {
|
||||
this.setNationality('Any');
|
||||
return this;
|
||||
}
|
||||
|
||||
excludeCountries(...countries: (typeof countryNames)[number][]): this {
|
||||
if (countries.length > 40) {
|
||||
throw new Error('Number of excluded countries cannot exceed 40');
|
||||
}
|
||||
this.excludedCountries = { enabled: true, value: countries };
|
||||
return this;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user