Skip to content

Commit

Permalink
chore: document store doesn t allow mix with token registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebulis committed Dec 26, 2019
1 parent 72654fa commit badaa65
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 73 deletions.
4 changes: 2 additions & 2 deletions src/common/smartContract/documentToSmartContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export const getDocumentStoreSmartContract = (
if (isWrappedV2Document(document)) {
const documentData = getData(document);
return (documentData.issuers || []).map(issuer => {
if (issuer.certificateStore && issuer.documentStore) {
throw new Error("Document store is invalid");
if (!issuer.certificateStore && !issuer.documentStore) {
throw new Error(`No document store for issuer "${issuer.name}"`);
}
const address = issuer.documentStore || issuer.certificateStore || "";
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,33 @@ describe("openAttestationEthereumDocumentStoreIssued", () => {
status: "VALID"
});
});
it("should return an error fragment when document mixes document store and other verifier method", async () => {
const fragment = await openAttestationEthereumDocumentStoreIssued.verify(
{
...v2documentRopstenValidWithDocumentStore,
data: {
...v2documentRopstenValidWithDocumentStore.data,
issuers: [
v2documentRopstenValidWithDocumentStore.data.issuers[0],
{
identityProof: v2documentRopstenValidWithDocumentStore.data.issuers[0].identityProof,
name: "Other Issuer"
}
]
}
},
{
network: "ropsten"
}
);
expect(fragment).toStrictEqual({
name: "OpenAttestationEthereumDocumentStoreIssued",
type: "DOCUMENT_STATUS",
data: new Error(`No document store for issuer "Other Issuer"`),
message: `No document store for issuer "Other Issuer"`,
status: "ERROR"
});
});
});
describe("v3", () => {
it("should return an invalid fragment when document with document store has not been issued", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,35 @@ export const openAttestationEthereumDocumentStoreIssued: Verifier<
return documentData.proof.method === v3.Method.DocumentStore;
}
const documentData = getData(document);
return documentData.issuers.every(issuer => "documentStore" in issuer || "certificateStore" in issuer);
return documentData.issuers.some(issuer => "documentStore" in issuer || "certificateStore" in issuer);
},
verify: async (document, options) => {
const smartContracts = getDocumentStoreSmartContract(document, options);
const status = await verifyIssued(document, smartContracts);
if (!status.issuedOnAll) {
try {
const smartContracts = getDocumentStoreSmartContract(document, options);
const status = await verifyIssued(document, smartContracts);
if (!status.issuedOnAll) {
return {
name,
type,
data: status,
message: "Certificate has not been issued",
status: "INVALID"
};
}
return {
name,
type,
data: status,
message: "Certificate has not been issued",
status: "INVALID"
status: "VALID"
};
} catch (e) {
return {
name,
type,
data: e,
message: e.message,
status: "ERROR"
};
}
return {
name,
type,
data: status,
status: "VALID"
};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { documentRopstenRevokedWithCertificateStore } from "../../../test/fixtur
import { documentRopstenNotIssuedWithCertificateStore } from "../../../test/fixtures/v2/documentRopstenNotIssuedWithCertificateStore";
import { documentRopstenValidWithDocumentStore as v3documentRopstenValidWithDocumentStore } from "../../../test/fixtures/v3/documentRopstenValid";
import { documentRopstenRevoked } from "../../../test/fixtures/v3/documentRopstenRevoked";
import { openAttestationEthereumDocumentStoreIssued } from "../documentStoreIssued/openAttestationEthereumDocumentStoreIssued";
import { documentRopstenValidWithDocumentStore as v2documentRopstenValidWithDocumentStore } from "../../../test/fixtures/v2/documentRopstenValidWithDocumentStore";

describe("openAttestationEthereumDocumentStoreRevoked", () => {
describe("v2", () => {
Expand Down Expand Up @@ -98,6 +100,33 @@ describe("openAttestationEthereumDocumentStoreRevoked", () => {
status: "VALID"
});
});
it("should return an error fragment when document mixes document store and other verifier method", async () => {
const fragment = await openAttestationEthereumDocumentStoreIssued.verify(
{
...v2documentRopstenValidWithDocumentStore,
data: {
...v2documentRopstenValidWithDocumentStore.data,
issuers: [
v2documentRopstenValidWithDocumentStore.data.issuers[0],
{
identityProof: v2documentRopstenValidWithDocumentStore.data.issuers[0].identityProof,
name: "Foo Issuer"
}
]
}
},
{
network: "ropsten"
}
);
expect(fragment).toStrictEqual({
name: "OpenAttestationEthereumDocumentStoreIssued",
type: "DOCUMENT_STATUS",
data: new Error(`No document store for issuer "Foo Issuer"`),
message: `No document store for issuer "Foo Issuer"`,
status: "ERROR"
});
});
});
describe("v3", () => {
it("should return an invalid fragment when document with document store has been revoked", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,32 @@ export const openAttestationEthereumDocumentStoreRevoked: Verifier<
return documentData.issuers.every(issuer => "documentStore" in issuer || "certificateStore" in issuer);
},
verify: async (document, options) => {
const smartContracts = getDocumentStoreSmartContract(document, options);
const status = await verifyRevoked(document, smartContracts);
if (status.revokedOnAny) {
try {
const smartContracts = getDocumentStoreSmartContract(document, options);
const status = await verifyRevoked(document, smartContracts);
if (status.revokedOnAny) {
return {
name,
type,
data: status,
message: "Certificate has been revoked",
status: "INVALID"
};
}
return {
name,
type,
data: status,
message: "Certificate has been revoked",
status: "INVALID"
status: "VALID"
};
} catch (e) {
return {
name,
type,
data: e,
message: e.message,
status: "ERROR"
};
}
return {
name,
type,
data: status,
status: "VALID"
};
}
};
96 changes: 48 additions & 48 deletions src/verifiers/openAttestationEthereumTokenRegistryMinted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,54 @@ describe("openAttestationEthereumTokenRegistryIssued", () => {
status: "VALID"
});
});
it("should return an error fragment when document has 2 issuers with token registry", async () => {
const fragment = await openAttestationEthereumTokenRegistryMinted.verify(
{
...documentRopstenValidWithToken,
data: {
...documentRopstenValidWithToken.data,
issuers: [documentRopstenValidWithToken.data.issuers[0], documentRopstenValidWithToken.data.issuers[0]]
}
},
{
network: "ropsten"
}
);
expect(fragment).toStrictEqual({
name: "OpenAttestationEthereumTokenRegistryMinted",
type: "DOCUMENT_STATUS",
data: new Error("Only one token registry is allowed. Found 2"),
message: "Only one token registry is allowed. Found 2",
status: "ERROR"
});
});
it("should return an error fragment when document uses 2 different verification method", async () => {
const fragment = await openAttestationEthereumTokenRegistryMinted.verify(
{
...documentRopstenValidWithToken,
data: {
...documentRopstenValidWithToken.data,
issuers: [
documentRopstenValidWithToken.data.issuers[0],
{
identityProof: documentRopstenValidWithToken.data.issuers[0].identityProof,
name: "Second Issuer"
}
]
}
},
{
network: "ropsten"
}
);
expect(fragment).toStrictEqual({
name: "OpenAttestationEthereumTokenRegistryMinted",
type: "DOCUMENT_STATUS",
data: new Error(`No token registry for issuer "Second Issuer"`),
message: `No token registry for issuer "Second Issuer"`,
status: "ERROR"
});
});
});
describe("v3", () => {
it("should return an invalid fragment when document with token registry has not been minted", async () => {
Expand Down Expand Up @@ -99,53 +147,5 @@ describe("openAttestationEthereumTokenRegistryIssued", () => {
status: "VALID"
});
});
it("should return an error fragment when document has 2 issuers with token registry", async () => {
const fragment = await openAttestationEthereumTokenRegistryMinted.verify(
{
...documentRopstenValidWithToken,
data: {
...documentRopstenValidWithToken.data,
issuers: [documentRopstenValidWithToken.data.issuers[0], documentRopstenValidWithToken.data.issuers[0]]
}
},
{
network: "ropsten"
}
);
expect(fragment).toStrictEqual({
name: "OpenAttestationEthereumTokenRegistryMinted",
type: "DOCUMENT_STATUS",
data: new Error("Only one token registry is allowed. Found 2"),
message: "Only one token registry is allowed. Found 2",
status: "ERROR"
});
});
it("should return an error fragment when document uses 2 different verification method", async () => {
const fragment = await openAttestationEthereumTokenRegistryMinted.verify(
{
...documentRopstenValidWithToken,
data: {
...documentRopstenValidWithToken.data,
issuers: [
documentRopstenValidWithToken.data.issuers[0],
{
identityProof: documentRopstenValidWithToken.data.issuers[0].identityProof,
name: "Second Issuer"
}
]
}
},
{
network: "ropsten"
}
);
expect(fragment).toStrictEqual({
name: "OpenAttestationEthereumTokenRegistryMinted",
type: "DOCUMENT_STATUS",
data: new Error(`No token registry for issuer "Second Issuer"`),
message: `No token registry for issuer "Second Issuer"`,
status: "ERROR"
});
});
});
});

0 comments on commit badaa65

Please sign in to comment.