@swisstronik/sdi-react-sdk
Version:
Swisstronik Digital Identity React SDK. Verify once, use anywhere.
1,794 lines (1,790 loc) • 507 kB
JavaScript
import { QueryClient, QueryClientProvider, useQuery, useQueryClient, useMutation } from '@tanstack/react-query';
import { ethAddressToBech32, bech32toEthAddress } from '@swisstronik/utils';
import { http, createPublicClient, bytesToHex, decodeAbiParameters } from 'viem';
import { SwisstronikStargateClient, verificationTypes, VerificationTypeEnum } from '@swisstronik/sdk';
import { createContext, useState, useEffect, useCallback, useContext, Fragment as Fragment$1, useRef } from 'react';
import { deriveSecretScalar, derivePublicKey } from '@zk-kit/eddsa-poseidon';
import { inCurve, packPoint } from '@zk-kit/baby-jubjub';
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
import { Transition, Dialog } from '@headlessui/react';
import { CountryDropdown } from 'react-country-region-selector';
import { AnimatePresence, motion } from 'framer-motion';
import { IDKitWidget, VerificationLevel } from '@worldcoin/idkit';
import { createAuthAdapter, createConfig, ComPilotProvider, useOpenWidget, useCustomerStatus } from '@compilot/react-sdk';
import QRCode from 'react-qr-code';
import { Buffer } from 'buffer';
// src/index.tsx
var ZK_SIGNATURE_MESSAGE = "I am creating a new private key for Swisstronik Digital Identity. I am aware that if it is exposed, it can lead to my onchain verifications being used by others.";
var SDIContext = createContext({
config: {
requiredVerifications: [],
cosmosRPCAddress: "",
swtrProxyContractAddress: "0xb4646662b94F94DdbfCCa75f1b57602E1C8A28F1"
},
isVerificationComplete: false,
setIsVerificationComplete: () => void 0,
userAddress: void 0,
setUserAddress: () => void 0,
messages: void 0,
setMessages: () => void 0,
showSnackbar: () => void 0,
swisstronikClient: void 0,
setSwisstronikClient: () => void 0,
isMainModalOpen: false,
setIsMainModalOpen: () => void 0,
modalContentStep: "mainContent",
setModalContentStep: () => void 0,
issuersList: [],
setIssuersList: () => void 0,
verificationsList: [],
setVerificationsList: () => void 0,
addVerificationSelectedIssuerType: "VT_UNSPECIFIED",
setAddVerificationSelectedIssuerType: () => void 0,
addVerificationSelectedIssuer: void 0,
setAddVerificationSelectedIssuer: () => void 0,
addVerificationStatusResult: { status: "pending" },
setAddVerificationStatusResult: () => void 0,
finalVerificationStatus: { status: "pending" },
setFinalVerificationStatus: () => void 0,
verificationMappings: {},
setVerificationForRequirement: () => void 0,
requiredVerifications: [],
setRequiredVerifications: () => void 0,
selectVerificationRequirement: void 0,
setSelectVerificationRequirement: () => void 0,
addVerificationSignature: "",
setAddVerificationSignature: () => void 0,
addVerificationZKVerificationEnabled: true,
setAddVerificationZKVerificationEnabled: () => void 0,
requiredSignatureType: void 0,
setRequiredSignatureType: () => void 0,
zkVerificationSignature: "",
setZkVerificationSignature: () => void 0,
ready: false,
setReady: () => void 0,
userCountry: "",
setUserCountry: () => void 0,
eip1193Provider: void 0,
setEip1193Provider: () => void 0,
redirectToSuccessAndSelectVerification: async () => void 0
});
var useSDIContext = () => {
const context = useContext(SDIContext);
if (!context) {
throw new Error("useSDI must be used within a SDIContextProvider");
}
return context;
};
var context_default = SDIContext;
// src/utils/SWTRProxyABI.json
var SWTRProxyABI_default = [
{
inputs: [],
name: "InvalidInitialization",
type: "error"
},
{
inputs: [],
name: "NotInitializing",
type: "error"
},
{
inputs: [
{
internalType: "address",
name: "owner",
type: "address"
}
],
name: "OwnableInvalidOwner",
type: "error"
},
{
inputs: [
{
internalType: "address",
name: "account",
type: "address"
}
],
name: "OwnableUnauthorizedAccount",
type: "error"
},
{
inputs: [
{
internalType: "bytes",
name: "_data",
type: "bytes"
}
],
name: "PrecompileError",
type: "error"
},
{
anonymous: false,
inputs: [
{
indexed: false,
internalType: "uint64",
name: "version",
type: "uint64"
}
],
name: "Initialized",
type: "event"
},
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "previousOwner",
type: "address"
},
{
indexed: true,
internalType: "address",
name: "newOwner",
type: "address"
}
],
name: "OwnershipTransferred",
type: "event"
},
{
inputs: [
{
internalType: "string[]",
name: "name",
type: "string[]"
},
{
internalType: "uint32[]",
name: "version",
type: "uint32[]"
},
{
internalType: "address[]",
name: "issuerAddress",
type: "address[]"
}
],
name: "addIssuersRecord",
outputs: [],
stateMutability: "nonpayable",
type: "function"
},
{
inputs: [
{
internalType: "bytes",
name: "originalData",
type: "bytes"
}
],
name: "decodeCompilotV1OriginalData",
outputs: [
{
internalType: "string",
name: "riskScore",
type: "string"
},
{
internalType: "uint32",
name: "createdAt",
type: "uint32"
},
{
internalType: "string",
name: "status",
type: "string"
}
],
stateMutability: "pure",
type: "function"
},
{
inputs: [
{
internalType: "bytes",
name: "originalData",
type: "bytes"
}
],
name: "decodeQuadrataPassportV1OriginalData",
outputs: [
{
internalType: "uint8",
name: "aml",
type: "uint8"
},
{
internalType: "string",
name: "country",
type: "string"
},
{
internalType: "string",
name: "did",
type: "string"
},
{
internalType: "bool",
name: "isBusiness",
type: "bool"
},
{
internalType: "bool",
name: "investorStatus",
type: "bool"
}
],
stateMutability: "pure",
type: "function"
},
{
inputs: [
{
internalType: "bytes",
name: "originalData",
type: "bytes"
}
],
name: "decodeWorldcoinV1OriginalData",
outputs: [
{
internalType: "string",
name: "merkle_root",
type: "string"
},
{
internalType: "string",
name: "nullifier_hash",
type: "string"
},
{
internalType: "string",
name: "proof",
type: "string"
},
{
internalType: "string",
name: "verification_level",
type: "string"
}
],
stateMutability: "pure",
type: "function"
},
{
inputs: [
{
internalType: "bytes",
name: "originalData",
type: "bytes"
}
],
name: "decodeZeronymV1OriginalData",
outputs: [
{
internalType: "bool",
name: "isFromUs",
type: "bool"
},
{
internalType: "uint256",
name: "actionId",
type: "uint256"
},
{
internalType: "uint256",
name: "output1",
type: "uint256"
},
{
internalType: "uint256",
name: "output2",
type: "uint256"
}
],
stateMutability: "pure",
type: "function"
},
{
inputs: [],
name: "getIssuanceRoot",
outputs: [
{
internalType: "uint256",
name: "_root",
type: "uint256"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "string",
name: "name",
type: "string"
},
{
internalType: "uint32[]",
name: "version",
type: "uint32[]"
}
],
name: "getIssuerAddressesByNameAndVersions",
outputs: [
{
internalType: "address[]",
name: "",
type: "address[]"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "issuerAddress",
type: "address"
}
],
name: "getIssuerRecordByAddress",
outputs: [
{
components: [
{
internalType: "string",
name: "name",
type: "string"
},
{
internalType: "uint32",
name: "version",
type: "uint32"
},
{
internalType: "address",
name: "issuerAddress",
type: "address"
}
],
internalType: "struct ISWTRProxy.Issuer",
name: "",
type: "tuple"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [],
name: "getRevocationRoot",
outputs: [
{
internalType: "uint256",
name: "_root",
type: "uint256"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "userAddress",
type: "address"
},
{
internalType: "address",
name: "issuerAddress",
type: "address"
},
{
internalType: "enum ISWTRProxy.VerificationType",
name: "verificationType",
type: "uint8"
}
],
name: "getVerificationCountry",
outputs: [
{
internalType: "string",
name: "",
type: "string"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "userAddress",
type: "address"
},
{
internalType: "address",
name: "issuerAddress",
type: "address"
},
{
internalType: "bytes",
name: "verificationId",
type: "bytes"
}
],
name: "getVerificationDataById",
outputs: [
{
components: [
{
internalType: "uint32",
name: "verificationType",
type: "uint32"
},
{
internalType: "bytes",
name: "verificationId",
type: "bytes"
},
{
internalType: "address",
name: "issuerAddress",
type: "address"
},
{
internalType: "string",
name: "originChain",
type: "string"
},
{
internalType: "uint32",
name: "issuanceTimestamp",
type: "uint32"
},
{
internalType: "uint32",
name: "expirationTimestamp",
type: "uint32"
},
{
internalType: "bytes",
name: "originalData",
type: "bytes"
},
{
internalType: "string",
name: "schema",
type: "string"
},
{
internalType: "string",
name: "issuerVerificationId",
type: "string"
},
{
internalType: "uint32",
name: "version",
type: "uint32"
}
],
internalType: "struct ISWTRProxy.VerificationData",
name: "",
type: "tuple"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "_initialOwner",
type: "address"
}
],
name: "initialize",
outputs: [],
stateMutability: "nonpayable",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "userAddress",
type: "address"
},
{
internalType: "address",
name: "issuerAddress",
type: "address"
}
],
name: "isUserHuman",
outputs: [
{
internalType: "bool",
name: "",
type: "bool"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "userAddress",
type: "address"
},
{
internalType: "uint32",
name: "expirationTimestamp",
type: "uint32"
},
{
internalType: "enum ISWTRProxy.VerificationType",
name: "verificationType",
type: "uint8"
}
],
name: "isUserVerified",
outputs: [
{
internalType: "bool",
name: "",
type: "bool"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "userAddress",
type: "address"
},
{
internalType: "uint32",
name: "expirationTimestamp",
type: "uint32"
},
{
internalType: "enum ISWTRProxy.VerificationType",
name: "verificationType",
type: "uint8"
},
{
internalType: "address[]",
name: "allowedIssuers",
type: "address[]"
}
],
name: "isUserVerifiedBy",
outputs: [
{
internalType: "bool",
name: "",
type: "bool"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "string",
name: "name",
type: "string"
},
{
internalType: "uint32",
name: "version",
type: "uint32"
}
],
name: "issuerAddressByNameAndVersion",
outputs: [
{
internalType: "address",
name: "",
type: "address"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "",
type: "address"
}
],
name: "issuerByAddress",
outputs: [
{
internalType: "string",
name: "name",
type: "string"
},
{
internalType: "uint32",
name: "version",
type: "uint32"
},
{
internalType: "address",
name: "issuerAddress",
type: "address"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "",
type: "address"
}
],
name: "issuerIndex",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [],
name: "issuerRecordCount",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "uint256",
name: "",
type: "uint256"
}
],
name: "issuers",
outputs: [
{
internalType: "string",
name: "name",
type: "string"
},
{
internalType: "uint32",
name: "version",
type: "uint32"
},
{
internalType: "address",
name: "issuerAddress",
type: "address"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "uint256",
name: "start",
type: "uint256"
},
{
internalType: "uint256",
name: "end",
type: "uint256"
}
],
name: "listIssuersRecord",
outputs: [
{
components: [
{
internalType: "string",
name: "name",
type: "string"
},
{
internalType: "uint32",
name: "version",
type: "uint32"
},
{
internalType: "address",
name: "issuerAddress",
type: "address"
}
],
internalType: "struct ISWTRProxy.Issuer[]",
name: "",
type: "tuple[]"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "userAddress",
type: "address"
},
{
internalType: "address",
name: "issuerAddress",
type: "address"
}
],
name: "listVerificationData",
outputs: [
{
components: [
{
internalType: "uint32",
name: "verificationType",
type: "uint32"
},
{
internalType: "bytes",
name: "verificationId",
type: "bytes"
},
{
internalType: "address",
name: "issuerAddress",
type: "address"
},
{
internalType: "string",
name: "originChain",
type: "string"
},
{
internalType: "uint32",
name: "issuanceTimestamp",
type: "uint32"
},
{
internalType: "uint32",
name: "expirationTimestamp",
type: "uint32"
},
{
internalType: "bytes",
name: "originalData",
type: "bytes"
},
{
internalType: "string",
name: "schema",
type: "string"
},
{
internalType: "string",
name: "issuerVerificationId",
type: "string"
},
{
internalType: "uint32",
name: "version",
type: "uint32"
}
],
internalType: "struct ISWTRProxy.VerificationData[]",
name: "",
type: "tuple[]"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [],
name: "owner",
outputs: [
{
internalType: "address",
name: "",
type: "address"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "userAddress",
type: "address"
},
{
internalType: "address",
name: "issuerAddress",
type: "address"
},
{
internalType: "enum ISWTRProxy.VerificationType",
name: "verificationType",
type: "uint8"
}
],
name: "passedVerificationType",
outputs: [
{
internalType: "bool",
name: "",
type: "bool"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [
{
internalType: "string",
name: "name",
type: "string"
},
{
internalType: "uint32",
name: "version",
type: "uint32"
}
],
name: "removeIssuerRecord",
outputs: [],
stateMutability: "nonpayable",
type: "function"
},
{
inputs: [],
name: "renounceOwnership",
outputs: [],
stateMutability: "nonpayable",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "newOwner",
type: "address"
}
],
name: "transferOwnership",
outputs: [],
stateMutability: "nonpayable",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "userAddress",
type: "address"
},
{
internalType: "address",
name: "issuerAddress",
type: "address"
}
],
name: "walletPassedAML",
outputs: [
{
internalType: "bool",
name: "",
type: "bool"
}
],
stateMutability: "view",
type: "function"
}
];
// src/utils/BaseIssuerAdapterABI.json
var BaseIssuerAdapterABI_default = [
{
inputs: [],
stateMutability: "nonpayable",
type: "constructor"
},
{
inputs: [
{
internalType: "address",
name: "owner",
type: "address"
}
],
name: "OwnableInvalidOwner",
type: "error"
},
{
inputs: [
{
internalType: "address",
name: "account",
type: "address"
}
],
name: "OwnableUnauthorizedAccount",
type: "error"
},
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: "address",
name: "previousOwner",
type: "address"
},
{
indexed: true,
internalType: "address",
name: "newOwner",
type: "address"
}
],
name: "OwnershipTransferred",
type: "event"
},
{
anonymous: false,
inputs: [
{
indexed: false,
internalType: "address",
name: "userAddress",
type: "address"
},
{
indexed: false,
internalType: "bytes",
name: "verificationId",
type: "bytes"
}
],
name: "UserVerified",
type: "event"
},
{
anonymous: false,
inputs: [
{
indexed: false,
internalType: "address",
name: "userAddress",
type: "address"
},
{
indexed: false,
internalType: "bytes",
name: "verificationId",
type: "bytes"
}
],
name: "VerificationRevoked",
type: "event"
},
{
inputs: [],
name: "getSupportedTypes",
outputs: [
{
internalType: "enum ISWTRProxy.VerificationType[]",
name: "",
type: "uint8[]"
}
],
stateMutability: "pure",
type: "function"
},
{
inputs: [
{
components: [
{
internalType: "address",
name: "userAddress",
type: "address"
},
{
internalType: "string",
name: "id",
type: "string"
},
{
internalType: "bytes",
name: "proofData",
type: "bytes"
},
{
internalType: "uint32",
name: "expirationTimestamp",
type: "uint32"
},
{
internalType: "enum ISWTRProxy.VerificationType",
name: "verificationType",
type: "uint8"
},
{
internalType: "bytes32",
name: "publicKey",
type: "bytes32"
},
{
internalType: "string",
name: "originChain",
type: "string"
},
{
internalType: "string",
name: "schema",
type: "string"
},
{
internalType: "uint32",
name: "version",
type: "uint32"
}
],
internalType: "struct IssuerAdapter.VerificationParams",
name: "params",
type: "tuple"
}
],
name: "markAsVerified",
outputs: [
{
internalType: "bytes",
name: "verificationId",
type: "bytes"
}
],
stateMutability: "nonpayable",
type: "function"
},
{
inputs: [],
name: "owner",
outputs: [
{
internalType: "address",
name: "",
type: "address"
}
],
stateMutability: "view",
type: "function"
},
{
inputs: [],
name: "renounceOwnership",
outputs: [],
stateMutability: "nonpayable",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "userAddress",
type: "address"
},
{
internalType: "bytes",
name: "verificationId",
type: "bytes"
}
],
name: "revokeVerification",
outputs: [],
stateMutability: "nonpayable",
type: "function"
},
{
inputs: [
{
internalType: "address",
name: "newOwner",
type: "address"
}
],
name: "transferOwnership",
outputs: [],
stateMutability: "nonpayable",
type: "function"
},
{
inputs: [],
name: "withdraw",
outputs: [],
stateMutability: "payable",
type: "function"
}
];
var queryAddressDetails = async (address, client) => {
const res = await client?.queryAddressDetails(address);
return res;
};
var convertTypeToText = (type) => {
switch (type) {
case "VT_HUMANITY":
return "Proof of Humanity";
case "VT_AML":
return "AML";
case "VT_ADDRESS":
return "Proof of Address";
case "VT_KYC":
return "KYC";
case "VT_CUSTOM":
return "Custom";
case "VT_KYB":
return "KYB";
case "VT_KYW":
return "Proof of Funds";
case "VT_CREDIT_SCORE":
return "Credit Score";
case "VT_BIOMETRIC":
return "Biometric";
case "UNRECOGNIZED":
return "Unknown";
case "VT_UNSPECIFIED":
return "Unknown";
default:
return "Unknown";
}
};
var getIconForVerificationType = (type) => {
switch (type) {
case "VT_UNSPECIFIED":
return "unspecifiedVerification";
case "VT_KYC":
return "kyc";
case "VT_AML":
return "aml";
case "VT_ADDRESS":
return "proofOfAddress";
case "VT_HUMANITY":
return "proofOfHumanity";
case "VT_KYB":
return "kyb";
case "VT_CREDIT_SCORE":
return "creditScore";
case "VT_KYW":
return "proofOfFunds";
case "VT_CUSTOM":
return "customVerification";
case "VT_BIOMETRIC":
return "kyc";
default:
return "unspecifiedVerification";
}
};
var getVerificationFilteredForRequirement = (requirement, verifications) => {
return verifications.filter((verification) => {
const isNotExpired = verification.expirationTimestamp ? verification.expirationTimestamp > Math.floor(requirement.expirationTimestamp / 1e3) : true;
const isCorrectType = requirement.requiredType === verification.type;
const adjustedAddress = verification.issuerAddress ? bech32toEthAddress(verification.issuerAddress) : "";
const isInAddressList = requirement.issuerAddresses.map((x) => x.toLowerCase()).includes(adjustedAddress.toLowerCase());
return isCorrectType && isInAddressList && isNotExpired;
});
};
var getIssuerByAddress = (address, issuersList) => {
return issuersList.find((issuer) => issuer.issuerAddress.toLowerCase() === address.toLowerCase());
};
var truncateMiddle = (str) => {
if (!str) {
return "";
}
if (str.length <= 11) return str;
return str.slice(0, 4) + "..." + str.slice(-4);
};
var formatVerificationDatePair = (issuanceTimestamp, expirationTimestamp) => {
const issuanceDate = new Date(issuanceTimestamp * 1e3);
const issuanceStr = issuanceDate.toLocaleDateString();
if (!expirationTimestamp) {
return `${issuanceStr} - Never`;
}
const expirationDate = new Date(expirationTimestamp * 1e3);
const expirationStr = expirationDate.toLocaleDateString();
return `${issuanceStr} - ${expirationStr}`;
};
var getSignMsgParams = (contractAddress, domainName, messageContents) => {
return {
domain: {
chainId: 1848,
name: domainName,
verifyingContract: contractAddress,
version: "1"
},
message: {
contents: messageContents
},
primaryType: "Obj",
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" }
],
Obj: [{ name: "contents", type: "string" }]
}
};
};
var privyExternalSignZKIdentityData = async (provider, userAddress, contractAddress) => {
const params = {
domain: {
chainId: 1848,
name: "ZK_SDI",
verifyingContract: contractAddress,
version: "1"
},
message: {
action: ZK_SIGNATURE_MESSAGE
},
primaryType: "ZKSDI",
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" }
],
ZKSDI: [
{ name: "action", type: "string" }
]
}
};
console.log("Requesting ZK sign for ", params);
const res = await provider.request({
method: "eth_signTypedData_v4",
params: [userAddress, JSON.stringify(params)]
});
return res;
};
var generateZKKeypairFromSignature = (signature) => {
deriveSecretScalar(signature);
const publicKey = derivePublicKey(signature);
if (!inCurve(publicKey)) {
throw Error("public key is not on curve");
}
const compressedKey = packPoint(publicKey);
return compressedKey.toString(16);
};
var privyExternalSignTypedData = async (provider, userAddress, contractAddress, domainName, messageContents) => {
const params = getSignMsgParams(contractAddress, domainName, messageContents);
console.log("Requesting sign for ", params);
const res = await provider.request({
method: "eth_signTypedData_v4",
params: [userAddress, JSON.stringify(params)]
});
return res;
};
var getVerificationDetails = async (setVerificationsList, address, swisstronikClient) => {
if (!address || address == "") {
console.warn("Can't fetch verifications - address is undefined or empty");
return;
}
if (!swisstronikClient) {
console.warn("Can't fetch user verifications - swisstronikClient is not ready");
return;
}
const convertedAddress = ethAddressToBech32(address);
try {
const addressDetails = await queryAddressDetails(convertedAddress, swisstronikClient);
if (addressDetails?.verifications && addressDetails?.verifications.length > 0) {
const details = await Promise.all(addressDetails.verifications.map(async (verification) => {
const newDetes = await swisstronikClient.queryVerificationDetails(verification.verificationId);
return { ...newDetes, verificationID: verification.verificationId };
}));
const filteredDetails = details.filter((x) => !x.isRevoked);
setVerificationsList(filteredDetails);
return filteredDetails;
} else {
console.log("Verifications list is zero or undefined");
}
} catch (e) {
console.error("Error while fetching verification list", e);
}
};
var swisstronikMainnetChain = {
id: 1848,
name: "Swisstronik Mainnet",
testnet: false,
nativeCurrency: {
name: "SWTR",
symbol: "SWTR",
decimals: 18
},
rpcUrls: {
default: {
http: ["https://json-rpc.mainnet.swisstronik.com/unencrypted/"]
}
},
blockExplorers: {
default: {
name: "Swisstronik Explorer",
url: "https://explorer-evm.mainnet.swisstronik.com"
}
}
};
var convertVerificationTypeToContract = (vt) => {
return VerificationTypeEnum[vt];
};
var verifySelectedVerificationsOnchain = async (userAddress, mapping, swtrProxyAddress, customRpc) => {
console.log("verifySelectedVerificationsOnchain");
const walletClient = getViemPublicClient(customRpc);
let notMatchingRequirement = void 0;
for (const { requirement, details } of Object.values(mapping)) {
if (!details) {
console.log("Details null for requirement", requirement);
notMatchingRequirement = requirement;
break;
}
console.log("preparing to read contract ", requirement.requiredType);
try {
const resp = await walletClient.readContract({
address: swtrProxyAddress,
abi: SWTRProxyABI_default,
functionName: "isUserVerifiedBy",
args: [userAddress, requirement.expirationTimestamp, convertVerificationTypeToContract(requirement.requiredType), requirement.issuerAddresses]
});
if (!resp) {
notMatchingRequirement = requirement;
break;
}
} catch (error) {
console.log("read contract err", error);
notMatchingRequirement = requirement;
break;
}
}
return notMatchingRequirement;
};
function decodeCompilotV1OriginalData(originalData) {
const decoded = decodeAbiParameters(
[
{ name: "riskScore", type: "string" },
{ name: "createdAt", type: "uint32" },
{ name: "status", type: "string" }
],
originalData
);
return {
riskScore: decoded[0],
createdAt: decoded[1],
status: decoded[2]
};
}
function decodeCrystalV1OriginalData(originalData) {
const decoded = decodeAbiParameters(
[
{ name: "riskScore", type: "uint8" },
{ name: "blocklist", type: "bool" }
],
originalData
);
return {
riskScore: decoded[0],
blocklist: decoded[1]
};
}
var getLatestIssuersByNames = async (wantIssuers, swtrProxyAddress, customRpc) => {
console.log("getIssuersByNamesAndVersions", wantIssuers);
const walletClient = getViemPublicClient(customRpc);
try {
const issuerCount = await walletClient.readContract({
address: swtrProxyAddress,
abi: SWTRProxyABI_default,
functionName: "issuerRecordCount",
args: []
});
const issuersList = await walletClient.readContract({
address: swtrProxyAddress,
abi: SWTRProxyABI_default,
functionName: "listIssuersRecord",
args: [0, issuerCount]
});
const versionMap = /* @__PURE__ */ new Map();
issuersList.forEach((issuer) => {
const currentVersion = versionMap.get(issuer.issuerAddress) || 0;
if (wantIssuers.includes(issuer.name) && issuer.version > currentVersion) {
versionMap.set(issuer.issuerAddress, issuer.version);
}
});
const res = Array.from(versionMap.keys());
console.log("Got issuers", res);
return res;
} catch (error) {
console.log("getLatestIssuersByNames read error", error);
return [];
}
};
var supportedTypesCache = /* @__PURE__ */ new Map();
var getSupportedTypeByIssuerAddress = async (issuerAddress, customRpc) => {
if (supportedTypesCache.has(issuerAddress)) {
return supportedTypesCache.get(issuerAddress);
}
const walletClient = getViemPublicClient(customRpc);
try {
const resp = await walletClient.readContract({
address: issuerAddress,
abi: BaseIssuerAdapterABI_default,
functionName: "getSupportedTypes",
args: []
});
const types = resp.map((i) => verificationTypes[i]);
supportedTypesCache.set(issuerAddress, types);
return types;
} catch (error) {
console.log("getSupportedTypes read error", error);
return [];
}
};
function compareArraysByPropertyUnordered(arr1, arr2, key) {
if (arr1.length !== arr2.length) {
return false;
}
const values1 = arr1.map((item) => item[key]).sort();
const values2 = arr2.map((item) => item[key]).sort();
return values1.every((value, index) => value === values2[index]);
}
function getViemPublicClient(customRPC) {
const rpcUrl = customRPC || "https://json-rpc.mainnet.swisstronik.com/unencrypted/";
const transport = http(rpcUrl);
return createPublicClient({
transport,
chain: swisstronikMainnetChain
});
}
var createInitialVerificationMappings = (requirements) => {
return requirements.reduce((acc, requirement) => ({
...acc,
[requirement.id]: {
requirement,
details: null
}
}), {});
};
var ADD_VERIFICATION_SIGNATURE_STORAGE_KEY = "sdi-sdk-addVerificationSignature";
var ZK_VERIFICATION_SIGNATURE_STORAGE_KEY = "sdi-sdk-zkVerificationSignature";
var SDIContextProvider = ({ children, config }) => {
const [userAddress, setUserAddress] = useState(void 0);
const [messages, setMessages] = useState(void 0);
const [snackbarOpen, setSnackbarOpen] = useState(false);
const [swisstronikClient, setSwisstronikClient] = useState(void 0);
const [isMainModalOpen, setIsMainModalOpen] = useState(false);
const [modalContentStep, setModalContentStep] = useState("mainContent");
const [issuersList, setIssuersList] = useState([]);
const [verificationsList, setVerificationsList] = useState([]);
const [addVerificationSelectedIssuerType, setAddVerificationSelectedIssuerType] = useState("VT_UNSPECIFIED");
const [addVerificationSelectedIssuer, setAddVerificationSelectedIssuer] = useState(void 0);
const [addVerificationStatusResult, setAddVerificationStatusResult] = useState({ status: "pending" });
const [finalVerificationStatus, setFinalVerificationStatus] = useState({ status: "pending" });
const [verificationMappings, setVerificationMappings] = useState({});
const [requiredVerifications, setRequiredVerifications] = useState([]);
const [selectVerificationRequirement, setSelectVerificationRequirement] = useState(void 0);
const [isVerificationComplete, setIsVerificationComplete] = useState(false);
const [userCountry, setUserCountry] = useState("");
const [eip1193Provider, setEip1193Provider] = useState(void 0);
const [addVerificationSignature, setAddVerificationSignature] = useState("");
const [zkVerificationSignature, setZkVerificationSignature] = useState("");
const [ready, setReady] = useState(false);
useEffect(() => {
if (userAddress) {
const addVerificationSignatureData = localStorage.getItem(ADD_VERIFICATION_SIGNATURE_STORAGE_KEY);
if (addVerificationSignatureData) {
try {
const b = JSON.parse(addVerificationSignatureData);
if (b.address == userAddress) {
setAddVerificationSignature(b.signature);
} else {
console.log(`addVerificationSignature address doesnt match ('${userAddress}'), resetting..`);
setAddVerificationSignature("");
localStorage.removeItem(ADD_VERIFICATION_SIGNATURE_STORAGE_KEY);
}
} catch (e) {
console.error("Error while parsing addVerification signature from localstorage", e);
setAddVerificationSignature("");
localStorage.removeItem(ADD_VERIFICATION_SIGNATURE_STORAGE_KEY);
}
}
const zkVerificationSignatureData = localStorage.getItem(ZK_VERIFICATION_SIGNATURE_STORAGE_KEY);
if (zkVerificationSignatureData) {
try {
const b = JSON.parse(zkVerificationSignatureData);
if (b.address == userAddress) {
setZkVerificationSignature(b.signature);
} else {
console.log(`zkVerificationSignature address doesnt match ('${userAddress}'), resetting..`);
setZkVerificationSignature("");
localStorage.removeItem(ZK_VERIFICATION_SIGNATURE_STORAGE_KEY);
}
} catch (e) {
console.error("Error while parsing zkVerfication signature from localstorage", e);
setZkVerificationSignature("");
localStorage.removeItem(ZK_VERIFICATION_SIGNATURE_STORAGE_KEY);
}
}
}
}, [userAddress]);
const [addVerificationZKVerificationEnabled, setAddVerificationZKVerificationEnabled] = useState(true);
const [requiredSignatureType, setRequiredSignatureType] = useState(void 0);
const setVerificationForRequirement = (requirement, details) => {
setVerificationMappings((prev) => ({
...prev,
[requirement.id]: {
requirement,
details
}
}));
};
useEffect(() => {
console.log("Verification mappings", verificationMappings);
}, [verificationMappings]);
useEffect(() => {
console.log("Required verifications", requiredVerifications);
}, [requiredVerifications]);
const redirectToSuccessAndSelectVerification = async (verificationId, queryClient, redirect = true) => {
if (swisstronikClient && verificationId) {
try {
if (selectVerificationRequirement) {
const details = await swisstronikClient.queryVerificationDetails(verificationId);
setVerificationForRequirement(selectVerificationRequirement, { ...details, verificationID: verificationId });
} else {
console.error("Select Verification is undefined in redirectToSuccessAndSelectVerification should not happen!");
}
} catch (e) {
console.error(`Error while querying verification details for ${verificationId}`, e);
}
} else {
console.error("SwisstronikClient or VerificationID is undefined in redirectToSuccessAndSelectVerification should not happen!", swisstronikClient, verificationId);
}
await queryClient.invalidateQueries({ queryKey: ["verificationsList", userAddress] });
if (redirect) {
setAddVerificationStatusResult({ status: "success" });
setModalContentStep("addVerificationResult");
}
};
const showSnackbar = useCallback((message) => {
setMessages(message);
setSnackbarOpen(true);
setTimeout(() => {
setSnackbarOpen(false);
setMessages(void 0);
}, 6e3);
}, []);
useEffect(() => {
const connectClient = async () => {
const client = await SwisstronikStargateClient.connect(config.cosmosRPCAddress);
setSwisstronikClient(client);
};
connectClient();
}, [config.cosmosRPCAddress]);
useEffect(() => {
const setRequirements = async () => {
const allRequirements = [];
for (let i = 0; i < config.requiredVerifications.length; i++) {
const requiredVerification = config.requiredVerifications[i];
const issuersAddresses = await getLatestIssuersByNames(requiredVerification.issuerNames, config.swtrProxyContractAddress, config.evmRPCAddress);
allRequirements.push({
expirationTimestamp: requiredVerification.expirationTimestamp,
id: i + 1,
issuerAddresses: issuersAddresses,
requiredType: requiredVerification.requiredType
});
}
setRequiredVerifications(allRequirements);
setVerificationMappings(createInitialVerificationMappings(allRequirements));
};
if (config.requiredVerifications.length > 0) {
setRequirements();
}
}, [config.requiredVerifications, config.evmRPCAddress]);
const provideObject = {
config,
isVerificationComplete,
setIsVerificationComplete,
userAddress,
setUserAddress,
messages,
setMessages,
snackbarOpen,
showSnackbar,
swisstronikClient,
setSwisstronikClient,
isMainModalOpen,
setIsMainModalOpen,
modalContentStep,
setModalContentStep,
issuersList,
setIssuersList,
verificationsList,
setVerificationsList,
addVerificationSelectedIssuerType,
setAddVerificationSelectedIssuerType,
addVerificationSelectedIssuer,
setAddVerificationSelectedIssuer,
addVerificationStatusResult,
setAddVerificationStatusResult,
finalVerificationStatus,
setFinalVerificationStatus,
verificationMappings,
setVerificationForRequirement,
requiredVerifications,
setRequiredVerifications,
selectVerificationRequirement,
setSelectVerificationRequirement,
addVerificationSignature,
setAddVerificationSignature,
addVerificationZKVerificationEnabled,
setAddVerificationZKVerificationEnabled,
requiredSignatureType,
setRequiredSignatureType,
zkVerificationSignature,
setZkVerificationSignature,
ready,
setReady,
userCountry,
setUserCountry,
eip1193Provider,
setEip1193Provider,
redirectToSuccessAndSelectVerification
};
return /* @__PURE__ */ jsx(context_default.Provider, { value: provideObject, children });
};
var store_default = SDIContextProvider;
// src/hooks/useSDI.ts
var useSDI = () => {
return useSDIContext();
};
function IconsComponent({
color,
size = 16,
name,
width,
height,
className = "",
ratio = 1,
strokeWidth = 1.5
}) {
const icons = {
info: /* @__PURE__ */ jsxs(
"svg",
{
width: size,
height: size,
viewBox: "0 0 20 20",
fill: `none`,
className,
xmlns: "http://www.w3.org/2000/svg",
children: [
/* @__PURE__ */ jsx(
"path",
{
d: "M10 18.3334C14.6024 18.3334 18.3333 14.6024 18.3333 10C18.3333 5.39765 14.6024 1.66669 10 1.66669C5.39763 1.66669 1.66667 5.39765 1.66667 10C1.66667 14.6024 5.39763 18.3334 10 18.3334Z",
className: `stroke-current text-${color}`,
strokeWidth,
strokeLinecap: "round",
strokeLinejoin: "round"
}
),
/* @__PURE__ */ jsx(
"path",
{
d: "M10 13.3333V10",
className: `stroke-current text-${color}`,
strokeWidth,
strokeLinecap: "round",
strokeLinejoin: "round"
}
),
/* @__PURE__ */ jsx(
"path",
{
d: "M10 6.66669H10.0083",
className: `stroke-current text-${color}`,
strokeWidth,
strokeLinecap: "round",
strokeLinejoin: "round"
}
)
]
}
),
infoSecond: /* @__PURE__ */ jsx(
"svg",
{
width: "24",
height: "24",
viewBox: "0 0 24 24",
fill: "none",
xmlns: "http://www.w3.org/2000/svg",
children: /* @__PURE__ */ jsx(
"path",
{
d: "M12 2.25C10.0716 2.25 8.18657 2.82183 6.58319 3.89317C4.97982 4.96451 3.73013 6.48726 2.99218 8.26884C2.25422 10.0504 2.06114 12.0108 2.43735 13.9021C2.81355 15.7934 3.74215 17.5307 5.10571 18.8943C6.46928 20.2579 8.20656 21.1865 10.0979 21.5627C11.9892 21.9389 13.9496 21.7458 15.7312 21.0078C17.5127 20.2699 19.0355 19.0202 20.1068 17.4168C21.1782 15.8134 21.75 13.9284 21.75 12C21.7473 9.41498 20.7192 6.93661 18.8913 5.10872C17.0634 3.28084 14.585 2.25273 12 2.25ZM12 20.25C10.3683 20.25 8.77326 19.7661 7.41655 18.8596C6.05984 17.9531 5.00242 16.6646 4.378 15.1571C3.75358 13.6496 3.5902 11.9908 3.90853 10.3905C4.22685 8.79016 5.01259 7.32015 6.16637 6.16637C7.32016 5.01259 8.79017 4.22685 10.3905 3.90852C11.9909 3.59019 13.6497 3.75357 15.1571 4.37799C16.6646 5.00242 17.9531 6.05984 18.8596 7.41655C19.7661 8.77325 20.25 10.3683 20.25 12C20.2475 14.1873 19.3775 16.2843 17.8309 17.8309C16.2843 19.3775 14.1873 20.2475 12 20.25ZM13.5 16.5C13.5 16.6989 13.421 16.8897 13.2803 17.0303C13.1397 17.171 12.9489 17.25 12.75 17.25C12.3522 17.25 11.9706 17.092 11.6893 16.8107C11.408 16.5294 11.25 16.1478 11.25 15.75V12C11.0511 12 10.8603 11.921 10.7197 11.7803C10.579 11.6397 10.5 11.4489 10.5 11.25C10.5 11.0511 10.579 10.8603 10.7197 10.7197C10.8603 10.579 11.0511 10.5 11.25 10.5C11.6478 10.5 12.0294 10.658 12.3107 10.9393C12.592 11.2206 12.75 11.6022 12.75 12V15.75C12.9489 15.75 13.1397 15.829 13.2803 15.9697C13.421 16.1103 13.5 16.3011 13.5 16.5ZM10.5 7.875C10.5 7.6525 10.566 7.43499 10.6896 7.24998C10.8132 7.06498 10.9889 6.92078 11.1945 6.83564C11.4001 6.75049 11.6263 6.72821 11.8445 6.77162C12.0627 6.81502 12.2632 6.92217 12.4205 7.0795C12.5778 7.23684 12.685 7.43729 12.7284 7.65552C12.7718 7.87375 12.7495 8.09995 12.6644 8.30552C12.5792 8.51109 12.435 8.68679 12.25 8.8104C12.065 8.93402 11.8475 9 11.625 9C11.3266 9 11.0405 8.88147 10.8295 8.6705C10.6185 8.45952 10.5 8.17337 10.5 7.875Z",
fill: "#8F8F8F"
}
)
}
),
progress: /* @__PURE__ */ jsx(
"svg",
{
width: "48",
height: "48",
viewBox: "0 0 48 48",
fill: "none",
xmlns: "http://www.w3.org/2000/svg",
children: /* @__PURE__ */ jsx(
"path",
{
d: "M25.5 6V12C25.5 12.3978 25.342 12.7794 25.0607 13.0607C24.7794 13.342 24.3978 13.5 24 13.5C23.6022 13.5 23.2206 13.342 22.9393 13.0607C22.658 12.7794 22.5 12.3978 22.5 12V6C22.5 5.60218 22.658 5.22064 22.9393 4.93934C23.2206 4.65804 23.6022 4.5 24 4.5C24.3978 4.5 24.7794 4.65804 25.0607 4.93934C25.342 5.22064 25.5 5.60218 25.5 6ZM42 22.5H36C35.6022 22.5 35.2206 22.658 34.9393 22.9393C34.658 23.2206 34.5 23.6022 34.5 24C34.5 24.3978 34.658 24.7794 34.9393 25.0607C35.2206 25.342 35.6022 25.5 36 25.5H42C42.3978 25.5 42.7794 25.342 43.0607 25.0607C43.342 24.7794 43.5 24.3978 43.5 24C43.5 23.6022 43.342 23.2206 43.0607 22.9393C42.7794 22.658 42.3978 22.5 42 22.5ZM33.5456 31.425C33.262 31.1556 32.8844 31.0076 32.4933 31.0126C32.1021 31.0176 31.7284 31.1752 31.4518 31.4518C31.1752 31.7284 31.0176 32.1021 31.0126 32.4933C31.0076 32.8844 31.1556 33.262 31.425 33.5456L35.6663 37.7887C35.9477 38.0702 36.3295 38.2283 36.7275 38.2283C37.1255 38.2283 37.5073 38.0702 37.7887 37.7887C38.0702 37.5073 38.2283 37.1255 38.2283 36.7275C38.2283 36.3295 38.0702 35.9477 37.7887 35.6663L33.5456 31.425ZM24 34.5C23.6022 34.5 23.2206 34.658 22.9393 34.9393C22.658 35.2206 22.5 35.6022 22.5 36V42C22.5 42.3978 22.658 42.7794 22.9393 43.0607C23.2206 43.342 23.6022 43.5 24 43.5C24.3978 43.5 24.7794 43.342 25.0607 43.0607C25.342 42.7794 25.5 42.3978 25.5 42V36C25.5 35.6022 25.342 35.2206 25.0607 34.9393C24.7794 34.658 24.3978 34.5 24 34.5ZM14.4544 31.425L10.2112 35.6663C9.92979 35.9477 9.77167 36.3295 9.77167 36.7275C9.77167 37.1255 9.92979 37.5073 10.2112 37.7887C10.4927 38.0702 10.8745 38.2283 11.2725 38.2283C11.6705 38.2283 12.0523 38.0702 12.3337 37.7887L16.575 33.5456C16.8444 33.262 16.9924 32.8844 16.9874 32.4933C16.9824 32.1021 16.8248 31.7284 16.5482 31.4518C16.2716 31.1752 15.8979 31.0176 15.5067 31.0126C15.1156 31.0076 14.738 31.1556 14.4544 31.425ZM13.5 24C13.5 23.6022 13.342 23.2206 13.0607 22.9393C12.7794 22.658 12.3978 22.5 12 22.5H6C5.60218 22.5 5.22064 22.658 4.93934 22.9393C4.65804 23.2206 4.5 23.6022 4.5 24C4.5 24.3978 4.65804 24.7794 4.93934 25.0607C5.22064 25.342 5.60218 25.5 6 25.5H12C12.3978 25.5 12.7794 25.342 13.0607 25.0607C13.342 24.7794 13.5 24.3978 13.5 24ZM12.3337 10.2112C12.0523 9.92979 11.6705 9.77167 11.2725 9.77167C10.8745 9.77167 10.4927 9.92979 10.2112 10.2112C9.92979 10.4927 9.77167 10.8745 9.77167 11.2725C9.77167 11.6705 9.92979 12.0523 10.2112 12.3337L14.4544 16.575C14.738 16.8444 15.1156 16.9924 15.5067 16.9874C15.8979 16.9824 16.2716 16.8248 16.5482 16.5482C16.8248 16.2716 16.9824