@oma3/omatrust 0.1.0-alpha.4 → 0.1.0-alpha.6
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 +21 -7
- package/dist/{eip712-C4a-JGko.d.cts → eip712-CevLiOcD.d.cts} +32 -1
- package/dist/{eip712-C4a-JGko.d.ts → eip712-CevLiOcD.d.ts} +32 -1
- package/dist/index-BsUwj4r5.d.cts +103 -0
- package/dist/index-pYFRyitI.d.ts +103 -0
- package/dist/index.cjs +188 -54
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +189 -55
- package/dist/index.js.map +1 -1
- package/dist/reputation/index.browser.cjs +188 -54
- package/dist/reputation/index.browser.cjs.map +1 -1
- package/dist/reputation/index.browser.d.cts +2 -2
- package/dist/reputation/index.browser.d.ts +2 -2
- package/dist/reputation/index.browser.js +187 -56
- package/dist/reputation/index.browser.js.map +1 -1
- package/dist/reputation/index.cjs +188 -54
- package/dist/reputation/index.cjs.map +1 -1
- package/dist/reputation/index.d.cts +2 -2
- package/dist/reputation/index.d.ts +2 -2
- package/dist/reputation/index.js +187 -56
- package/dist/reputation/index.js.map +1 -1
- package/package.json +2 -1
- package/dist/index-BDeQNCi_.d.ts +0 -96
- package/dist/index-DP6IIpIh.d.cts +0 -96
|
@@ -57,6 +57,13 @@ function encodeAttestationData(schema, data) {
|
|
|
57
57
|
if (!data || typeof data !== "object") {
|
|
58
58
|
throw new OmaTrustError("INVALID_INPUT", "data must be an object");
|
|
59
59
|
}
|
|
60
|
+
const validationErrors = validateAttestationData(schema, data);
|
|
61
|
+
if (validationErrors.length > 0) {
|
|
62
|
+
const summary = validationErrors.map((error) => `Field "${error.schemaFieldName}" (${error.expectedType}) got ${error.providedType}`).join("; ");
|
|
63
|
+
throw new OmaTrustError("INVALID_INPUT", `Attestation data validation failed: ${summary}`, {
|
|
64
|
+
errors: validationErrors
|
|
65
|
+
});
|
|
66
|
+
}
|
|
60
67
|
const fields = normalizeSchema(schema);
|
|
61
68
|
const schemaString = schemaToString(fields);
|
|
62
69
|
const encoder = new easSdk.SchemaEncoder(schemaString);
|
|
@@ -69,6 +76,99 @@ function encodeAttestationData(schema, data) {
|
|
|
69
76
|
);
|
|
70
77
|
return encoded;
|
|
71
78
|
}
|
|
79
|
+
function getActualType(value) {
|
|
80
|
+
if (value === null) {
|
|
81
|
+
return "null";
|
|
82
|
+
}
|
|
83
|
+
if (value === void 0) {
|
|
84
|
+
return "undefined";
|
|
85
|
+
}
|
|
86
|
+
if (Array.isArray(value)) {
|
|
87
|
+
return "array";
|
|
88
|
+
}
|
|
89
|
+
if (typeof value === "number") {
|
|
90
|
+
return Number.isFinite(value) ? "number" : "non-finite-number";
|
|
91
|
+
}
|
|
92
|
+
return typeof value;
|
|
93
|
+
}
|
|
94
|
+
function isNumericValue(value, allowNegative) {
|
|
95
|
+
if (typeof value === "bigint") {
|
|
96
|
+
return allowNegative || value >= 0n;
|
|
97
|
+
}
|
|
98
|
+
if (typeof value === "number") {
|
|
99
|
+
if (!Number.isFinite(value) || !Number.isInteger(value)) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
return allowNegative || value >= 0;
|
|
103
|
+
}
|
|
104
|
+
if (typeof value === "string") {
|
|
105
|
+
if (value.length === 0) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
const pattern = allowNegative ? /^-?\d+$/ : /^\d+$/;
|
|
109
|
+
return pattern.test(value);
|
|
110
|
+
}
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
function isHex(value) {
|
|
114
|
+
return typeof value === "string" && /^0x[0-9a-fA-F]*$/.test(value);
|
|
115
|
+
}
|
|
116
|
+
function validateFieldValue(type, value) {
|
|
117
|
+
const normalizedType = type.trim().toLowerCase();
|
|
118
|
+
const bytesNMatch = normalizedType.match(/^bytes([1-9]|[12]\d|3[0-2])$/);
|
|
119
|
+
if (/^uint\d*$/.test(normalizedType)) {
|
|
120
|
+
return isNumericValue(value, false);
|
|
121
|
+
}
|
|
122
|
+
if (/^int\d*$/.test(normalizedType)) {
|
|
123
|
+
return isNumericValue(value, true);
|
|
124
|
+
}
|
|
125
|
+
if (normalizedType === "string") {
|
|
126
|
+
return typeof value === "string";
|
|
127
|
+
}
|
|
128
|
+
if (normalizedType === "string[]") {
|
|
129
|
+
return Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
130
|
+
}
|
|
131
|
+
if (normalizedType === "bool") {
|
|
132
|
+
return typeof value === "boolean";
|
|
133
|
+
}
|
|
134
|
+
if (normalizedType === "address") {
|
|
135
|
+
return typeof value === "string" && ethers.isAddress(value);
|
|
136
|
+
}
|
|
137
|
+
if (normalizedType === "bytes") {
|
|
138
|
+
return isHex(value) && (value.length - 2) % 2 === 0;
|
|
139
|
+
}
|
|
140
|
+
if (bytesNMatch) {
|
|
141
|
+
const expectedBytes = Number(bytesNMatch[1]);
|
|
142
|
+
return isHex(value) && value.length === 2 + expectedBytes * 2;
|
|
143
|
+
}
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
function validateAttestationData(schema, data) {
|
|
147
|
+
if (!data || typeof data !== "object") {
|
|
148
|
+
return [{
|
|
149
|
+
schemaFieldName: "data",
|
|
150
|
+
expectedType: "object",
|
|
151
|
+
providedType: getActualType(data),
|
|
152
|
+
providedValue: data
|
|
153
|
+
}];
|
|
154
|
+
}
|
|
155
|
+
const fields = normalizeSchema(schema);
|
|
156
|
+
const errors = [];
|
|
157
|
+
for (const field of fields) {
|
|
158
|
+
const value = data[field.name];
|
|
159
|
+
const isValid = validateFieldValue(field.type, value);
|
|
160
|
+
if (isValid) {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
errors.push({
|
|
164
|
+
schemaFieldName: field.name,
|
|
165
|
+
expectedType: field.type,
|
|
166
|
+
providedType: getActualType(value),
|
|
167
|
+
providedValue: value
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return errors;
|
|
171
|
+
}
|
|
72
172
|
function decodeAttestationData(schema, encodedData) {
|
|
73
173
|
if (typeof encodedData !== "string" || !encodedData.startsWith("0x")) {
|
|
74
174
|
throw new OmaTrustError("INVALID_INPUT", "encodedData must be a hex string");
|
|
@@ -89,22 +189,11 @@ function decodeAttestationData(schema, encodedData) {
|
|
|
89
189
|
return result;
|
|
90
190
|
}
|
|
91
191
|
function extractExpirationTime(data) {
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
if (typeof value === "bigint") {
|
|
99
|
-
return value;
|
|
100
|
-
}
|
|
101
|
-
if (typeof value === "number") {
|
|
102
|
-
return value;
|
|
103
|
-
}
|
|
104
|
-
if (typeof value === "string" && /^\d+$/.test(value)) {
|
|
105
|
-
return BigInt(value);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
192
|
+
const value = data["expiresAt"];
|
|
193
|
+
if (value === void 0 || value === null) return void 0;
|
|
194
|
+
if (typeof value === "bigint") return value;
|
|
195
|
+
if (typeof value === "number") return value;
|
|
196
|
+
if (typeof value === "string" && /^\d+$/.test(value)) return BigInt(value);
|
|
108
197
|
return void 0;
|
|
109
198
|
}
|
|
110
199
|
|
|
@@ -417,6 +506,34 @@ async function submitAttestation(params) {
|
|
|
417
506
|
throw new OmaTrustError("NETWORK_ERROR", "Failed to submit attestation", { err });
|
|
418
507
|
}
|
|
419
508
|
}
|
|
509
|
+
async function revokeAttestation(params) {
|
|
510
|
+
if (!params || typeof params !== "object") {
|
|
511
|
+
throw new OmaTrustError("INVALID_INPUT", "params must be provided");
|
|
512
|
+
}
|
|
513
|
+
if (!params.signer) {
|
|
514
|
+
throw new OmaTrustError("INVALID_INPUT", "signer is required");
|
|
515
|
+
}
|
|
516
|
+
const eas = new easSdk.EAS(params.easContractAddress);
|
|
517
|
+
eas.connect(params.signer);
|
|
518
|
+
try {
|
|
519
|
+
const tx = await eas.revoke({
|
|
520
|
+
schema: params.schemaUid,
|
|
521
|
+
data: {
|
|
522
|
+
uid: params.uid,
|
|
523
|
+
value: toBigIntOrDefault(params.value, 0n)
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
await tx.wait();
|
|
527
|
+
const txHash = getEasTransactionHash(tx) ?? ZERO_UID;
|
|
528
|
+
const receipt = getEasTransactionReceipt(tx);
|
|
529
|
+
return {
|
|
530
|
+
txHash,
|
|
531
|
+
receipt
|
|
532
|
+
};
|
|
533
|
+
} catch (err) {
|
|
534
|
+
throw new OmaTrustError("NETWORK_ERROR", "Failed to revoke attestation", { err });
|
|
535
|
+
}
|
|
536
|
+
}
|
|
420
537
|
function buildDelegatedTypedData(params) {
|
|
421
538
|
return {
|
|
422
539
|
domain: {
|
|
@@ -564,7 +681,14 @@ async function submitDelegatedAttestation(params) {
|
|
|
564
681
|
var EAS_EVENT_ABI = [
|
|
565
682
|
"event Attested(address indexed recipient, address indexed attester, bytes32 uid, bytes32 indexed schemaUID)"
|
|
566
683
|
];
|
|
567
|
-
function
|
|
684
|
+
function parseEventTxHash(event) {
|
|
685
|
+
const txHash = event.transactionHash;
|
|
686
|
+
if (typeof txHash !== "string") {
|
|
687
|
+
return void 0;
|
|
688
|
+
}
|
|
689
|
+
return /^0x[0-9a-fA-F]{64}$/.test(txHash) ? txHash : void 0;
|
|
690
|
+
}
|
|
691
|
+
function parseAttestation(attestation, schema, txHash) {
|
|
568
692
|
const rawData = attestation.data ?? "0x";
|
|
569
693
|
const decoded = schema ? decodeAttestationData(schema, rawData) : {};
|
|
570
694
|
const toBigIntSafe = (value) => {
|
|
@@ -584,6 +708,7 @@ function parseAttestation(attestation, schema) {
|
|
|
584
708
|
schema: attestation.schema,
|
|
585
709
|
attester: attestation.attester,
|
|
586
710
|
recipient: attestation.recipient,
|
|
711
|
+
txHash,
|
|
587
712
|
revocable: Boolean(attestation.revocable),
|
|
588
713
|
revocationTime: toBigIntSafe(attestation.revocationTime),
|
|
589
714
|
expirationTime: toBigIntSafe(attestation.expirationTime),
|
|
@@ -609,21 +734,18 @@ async function getAttestation(params) {
|
|
|
609
734
|
throw new OmaTrustError("NETWORK_ERROR", "Failed to read attestation", { err });
|
|
610
735
|
}
|
|
611
736
|
}
|
|
612
|
-
async function
|
|
613
|
-
const
|
|
614
|
-
const
|
|
615
|
-
const toBlock = params.toBlock ?? await provider.getBlockNumber();
|
|
616
|
-
const fromBlock = params.fromBlock ?? Math.max(0, toBlock - 5e4);
|
|
617
|
-
const filter = contract.filters.Attested(didToAddress(params.did));
|
|
737
|
+
async function queryAttestationEvents(easContractAddress, provider, fromBlock, toBlock, options) {
|
|
738
|
+
const contract = new ethers.Contract(easContractAddress, EAS_EVENT_ABI, provider);
|
|
739
|
+
const filter = contract.filters.Attested(options?.recipient ?? null, options?.attester ?? null);
|
|
618
740
|
let events;
|
|
619
741
|
try {
|
|
620
742
|
events = await contract.queryFilter(filter, fromBlock, toBlock);
|
|
621
743
|
} catch (err) {
|
|
622
744
|
throw new OmaTrustError("NETWORK_ERROR", "Failed to query attestation events", { err });
|
|
623
745
|
}
|
|
624
|
-
const eas = new easSdk.EAS(
|
|
746
|
+
const eas = new easSdk.EAS(easContractAddress);
|
|
625
747
|
eas.connect(provider);
|
|
626
|
-
const schemaFilter =
|
|
748
|
+
const schemaFilter = options?.schemas?.map((s) => s.toLowerCase());
|
|
627
749
|
const results = [];
|
|
628
750
|
for (const event of events) {
|
|
629
751
|
if (!("args" in event && Array.isArray(event.args))) {
|
|
@@ -642,11 +764,39 @@ async function getAttestationsForDid(params) {
|
|
|
642
764
|
if (!attestation || !attestation.uid) {
|
|
643
765
|
continue;
|
|
644
766
|
}
|
|
645
|
-
results.push(parseAttestation(attestation));
|
|
767
|
+
results.push(parseAttestation(attestation, void 0, parseEventTxHash(event)));
|
|
646
768
|
}
|
|
647
769
|
results.sort((a, b) => Number(b.time - a.time));
|
|
648
770
|
return results;
|
|
649
771
|
}
|
|
772
|
+
async function getAttestationsForDid(params) {
|
|
773
|
+
const provider = params.provider;
|
|
774
|
+
const toBlock = params.toBlock ?? await provider.getBlockNumber();
|
|
775
|
+
const fromBlock = params.fromBlock ?? Math.max(0, toBlock - 5e4);
|
|
776
|
+
return queryAttestationEvents(
|
|
777
|
+
params.easContractAddress,
|
|
778
|
+
provider,
|
|
779
|
+
fromBlock,
|
|
780
|
+
toBlock,
|
|
781
|
+
{ recipient: didToAddress(params.did), schemas: params.schemas }
|
|
782
|
+
);
|
|
783
|
+
}
|
|
784
|
+
async function getAttestationsByAttester(params) {
|
|
785
|
+
const provider = params.provider;
|
|
786
|
+
const toBlock = params.toBlock ?? await provider.getBlockNumber();
|
|
787
|
+
const fromBlock = params.fromBlock ?? Math.max(0, toBlock - 5e4);
|
|
788
|
+
if (params.limit !== void 0 && params.limit <= 0) {
|
|
789
|
+
return [];
|
|
790
|
+
}
|
|
791
|
+
const results = await queryAttestationEvents(
|
|
792
|
+
params.easContractAddress,
|
|
793
|
+
provider,
|
|
794
|
+
fromBlock,
|
|
795
|
+
toBlock,
|
|
796
|
+
{ attester: params.attester, schemas: params.schemas }
|
|
797
|
+
);
|
|
798
|
+
return params.limit !== void 0 ? results.slice(0, params.limit) : results;
|
|
799
|
+
}
|
|
650
800
|
async function listAttestations(params) {
|
|
651
801
|
const limit = params.limit ?? 20;
|
|
652
802
|
if (limit <= 0) {
|
|
@@ -657,34 +807,15 @@ async function listAttestations(params) {
|
|
|
657
807
|
}
|
|
658
808
|
async function getLatestAttestations(params) {
|
|
659
809
|
const provider = params.provider;
|
|
660
|
-
const
|
|
661
|
-
const
|
|
662
|
-
const
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
if (!("args" in event && Array.isArray(event.args))) {
|
|
670
|
-
continue;
|
|
671
|
-
}
|
|
672
|
-
const args = event.args;
|
|
673
|
-
const uid = args?.[2];
|
|
674
|
-
const schemaUid = args?.[3];
|
|
675
|
-
if (!uid || !schemaUid) {
|
|
676
|
-
continue;
|
|
677
|
-
}
|
|
678
|
-
if (schemaFilter && !schemaFilter.includes(schemaUid.toLowerCase())) {
|
|
679
|
-
continue;
|
|
680
|
-
}
|
|
681
|
-
const attestation = await eas.getAttestation(uid);
|
|
682
|
-
if (!attestation || !attestation.uid) {
|
|
683
|
-
continue;
|
|
684
|
-
}
|
|
685
|
-
results.push(parseAttestation(attestation));
|
|
686
|
-
}
|
|
687
|
-
results.sort((a, b) => Number(b.time - a.time));
|
|
810
|
+
const toBlock = await provider.getBlockNumber();
|
|
811
|
+
const fromBlock = params.fromBlock ?? Math.max(0, toBlock - 5e4);
|
|
812
|
+
const results = await queryAttestationEvents(
|
|
813
|
+
params.easContractAddress,
|
|
814
|
+
provider,
|
|
815
|
+
fromBlock,
|
|
816
|
+
toBlock,
|
|
817
|
+
{ schemas: params.schemas }
|
|
818
|
+
);
|
|
688
819
|
return results.slice(0, params.limit ?? 20);
|
|
689
820
|
}
|
|
690
821
|
function deduplicateReviews(attestations) {
|
|
@@ -1511,6 +1642,7 @@ exports.fetchDidDocument = fetchDidDocument;
|
|
|
1511
1642
|
exports.formatSchemaUid = formatSchemaUid;
|
|
1512
1643
|
exports.formatTransferAmount = formatTransferAmount;
|
|
1513
1644
|
exports.getAttestation = getAttestation;
|
|
1645
|
+
exports.getAttestationsByAttester = getAttestationsByAttester;
|
|
1514
1646
|
exports.getAttestationsForDid = getAttestationsForDid;
|
|
1515
1647
|
exports.getChainConstants = getChainConstants;
|
|
1516
1648
|
exports.getExplorerAddressUrl = getExplorerAddressUrl;
|
|
@@ -1526,10 +1658,12 @@ exports.listAttestations = listAttestations;
|
|
|
1526
1658
|
exports.normalizeSchema = normalizeSchema;
|
|
1527
1659
|
exports.parseDnsTxtRecord = parseDnsTxtRecord;
|
|
1528
1660
|
exports.prepareDelegatedAttestation = prepareDelegatedAttestation;
|
|
1661
|
+
exports.revokeAttestation = revokeAttestation;
|
|
1529
1662
|
exports.schemaToString = schemaToString;
|
|
1530
1663
|
exports.splitSignature = splitSignature;
|
|
1531
1664
|
exports.submitAttestation = submitAttestation;
|
|
1532
1665
|
exports.submitDelegatedAttestation = submitDelegatedAttestation;
|
|
1666
|
+
exports.validateAttestationData = validateAttestationData;
|
|
1533
1667
|
exports.verifyAttestation = verifyAttestation;
|
|
1534
1668
|
exports.verifyDidDocumentControllerDid = verifyDidDocumentControllerDid;
|
|
1535
1669
|
exports.verifyDnsTxtControllerDid = verifyDnsTxtControllerDid;
|