Skip to content

Commit

Permalink
split P-384 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daveroga committed Jan 31, 2025
1 parent 76ffe62 commit c758890
Showing 1 changed file with 88 additions and 73 deletions.
161 changes: 88 additions & 73 deletions test/onchain-identity/identity-trusted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { deployPoseidons } from "../../helpers/PoseidonDeployHelper";
import { AbiCoder } from "ethers";
import { hexToBytes } from "@0xpolygonid/js-sdk";

describe("Next tests reproduce identity life cycle", function () {
describe("Next tests reproduce identity trusted life cycle", function () {
this.timeout(10000);

let identity;
Expand Down Expand Up @@ -36,8 +36,66 @@ describe("Next tests reproduce identity life cycle", function () {
);
});

describe("create identity", function () {
it("deploy state and identity", async function () {
expect(await identity.getIsOldStateGenesis()).to.be.equal(true);
});

it("validate identity's id", async function () {
const id = await identity.getId();
expect(id).to.be.equal(identityId);
});
});

describe("validate initial identity", function () {
let initialClaimTreeRoot, initialRevocationTreeRoot, initialRootOfRootsTreeRoot: any;
before(async function () {
initialClaimTreeRoot = await identity.getClaimsTreeRoot();
initialRevocationTreeRoot = await identity.getRevocationsTreeRoot();
initialRootOfRootsTreeRoot = await identity.getRootsTreeRoot();
});

it("trees should be empty", async function () {
expect(initialClaimTreeRoot).be.equal(0);
expect(initialRevocationTreeRoot).to.be.equal(0);
expect(initialRootOfRootsTreeRoot).to.be.equal(0);
});

it("last roots should be empty", async function () {
const lastClaimTreeRoot = await identity.getLatestPublishedClaimsRoot();
const lastRevocationTreeRoot = await identity.getLatestPublishedRevocationsRoot();
const lastRootOfRootsTreeRoot = await identity.getLatestPublishedRootsRoot();

expect(lastClaimTreeRoot).to.be.equal(initialClaimTreeRoot);
expect(lastRevocationTreeRoot).to.be.equal(0);
expect(lastRootOfRootsTreeRoot).to.be.equal(0);
});
it("since the identity did not perform the transition - the isGenesis flag should be true", async function () {
const isOldStateGenesis = await identity.getIsOldStateGenesis();
expect(isOldStateGenesis).to.be.true;
});
it("latest identity state should be empty", async function () {
latestSavedState = await identity.getLatestPublishedState();
expect(latestSavedState).to.be.equal(0);
});
it("getClaimProofWithStateInfo should return non-existence proof", async function () {
const proof = await identity.getClaimProofWithStateInfo(1);
expect(proof[0].existence).to.be.false;
});
it("getRevocationProofWithStateInfo should return non-existence proof", async function () {
const proof = await identity.getRevocationProofWithStateInfo(1);
expect(proof[0].existence).to.be.false;
});
it("getRootProofWithStateInfo should return non-existence proof", async function () {
const proof = await identity.getRootProofWithStateInfo(1);
expect(proof[0].existence).to.be.false;
});
});

describe.only("test P-384", function () {
it("Generate signature and verify P-384", async function () {
let publicKeyCK, publicKey, signature, message, messageHash, messageBuffer;

before(async function () {
/* const keyPair = await crypto.subtle.generateKey(
{
name: "ECDSA",
Expand Down Expand Up @@ -78,15 +136,15 @@ describe("Next tests reproduce identity life cycle", function () {
// const publicKeyExtracted = await crypto.subtle.exportKey("jwk", publicKey);
const publicKeyX = Buffer.from(jwkPublicKey.x!, "base64").toString("hex");
const publicKeyY = Buffer.from(jwkPublicKey.y!, "base64").toString("hex");
const pubKey = `0x${publicKeyX}${publicKeyY}`;
publicKey = `0x${publicKeyX}${publicKeyY}`;

// create claim and sign it
const hashIndex = 1;
const hashValue = 2;

const encoder = new AbiCoder();
const messageEncoded = encoder.encode(["uint256", "uint256"], [hashIndex, hashValue]);
const messageBuffer = hexToBytes(messageEncoded);
messageBuffer = hexToBytes(messageEncoded);
const messageHashBuffer = await crypto.subtle.digest("SHA-384", messageBuffer);

/*
Expand All @@ -99,7 +157,7 @@ describe("Next tests reproduce identity life cycle", function () {
["sign"],
);
const publicKey = await crypto.subtle.importKey(
const publicKeyCK = await crypto.subtle.importKey(
"jwk",
jwkPublicKey,
{ name: "ECDSA", namedCurve: "P-384" },
Expand All @@ -124,7 +182,7 @@ describe("Next tests reproduce identity life cycle", function () {
name: "ECDSA",
hash: "SHA-384",
},
publicKey,
publicKeyCK,
signatureBuffer,
messageBuffer,
);
Expand All @@ -133,109 +191,66 @@ describe("Next tests reproduce identity life cycle", function () {
const signature = `0x${Buffer.from(signatureBuffer).toString("hex")}`; */

// Valid signature
const signature = `0x14e5ea76ab2b676b3e32d23c16e6b20d28cd6d2d34969ac4b506d54ad1ff552f3bf70a2cc4c04399f0702a1a9c5bfb8f5f102ec62b9b22274f52ed334bac46ca9967a0004d8af454f14ac3caa3f9d563b681b5719ef43f11a07bd7816ed81ac5`;
signature = `0x14e5ea76ab2b676b3e32d23c16e6b20d28cd6d2d34969ac4b506d54ad1ff552f3bf70a2cc4c04399f0702a1a9c5bfb8f5f102ec62b9b22274f52ed334bac46ca9967a0004d8af454f14ac3caa3f9d563b681b5719ef43f11a07bd7816ed81ac5`;
// Not valid signature
// const signature = `0x9c07874a88b737dc3b1b9fb104049bf5c0ff22769583d7c379ca182b577919f12481632ac16b9dd125733ead052d2d31c917feb7b7523804f966931d9217bbdd18ea92ea424973fbf418b4495af2a03e82a819a8cfb00372fa921e4644c56e46`;
const message = messageEncoded; // `0x${Buffer.from(messageBuffer).toString("hex")}`;
const messageHash = `0x${Buffer.from(messageHashBuffer).toString("hex")}`;
// signature = `0x9c07874a88b737dc3b1b9fb104049bf5c0ff22769583d7c379ca182b577919f12481632ac16b9dd125733ead052d2d31c917feb7b7523804f966931d9217bbdd18ea92ea424973fbf418b4495af2a03e82a819a8cfb00372fa921e4644c56e46`;
message = messageEncoded; // `0x${Buffer.from(messageBuffer).toString("hex")}`;
messageHash = `0x${Buffer.from(messageHashBuffer).toString("hex")}`;

console.log(message, signature, pubKey, messageHash);
// console.log(message, signature, pubKey, messageHash);

const publicKey = await crypto.subtle.importKey(
publicKeyCK = await crypto.subtle.importKey(
"jwk",
jwkPublicKey,
{ name: "ECDSA", namedCurve: "P-384" },
true,
["verify"],
);
});

it("Verify signature P-384 with library", async function () {
const verified = await crypto.subtle.verify(
{
name: "ECDSA",
hash: "SHA-384",
},
publicKey,
publicKeyCK,
hexToBytes(signature),
messageBuffer,
);
expect(verified).to.be.true;
console.log("verified with lib:", verified);
});

it("Verify signature P-384 with Identity SC", async function () {
// Test SC SECP384r1 verification functions
const verifiedSC = await identity.verifySECP384r1(message, signature, pubKey);
const verifiedSC = await identity.verifySECP384r1(message, signature, publicKey);
expect(verifiedSC).to.be.true;

const verifiedSC2 = await identity.verifySECP384r1WithoutHashing(
messageHash,
signature,
pubKey,
publicKey,
);
expect(verifiedSC2).to.be.true;
});

it("Add claim with signature P-384", async function () {
await expect(
identity.addClaimHashWithSignature(1, 2, `0x${signature.slice(4)}`),
).to.be.revertedWithCustomError(identity, "InvalidSignatureLength");

await expect(
identity.addClaimHashWithSignature(1, 2, `0x00${signature.slice(4)}`),
).to.be.revertedWithCustomError(identity, "InvalidSignature");

// Test adding claim with signature
await identity.addClaimHashWithSignature(1, 2, signature);
await expect(identity.addClaimHashWithSignature(1, 2, signature)).not.to.be.reverted;
const proof = await identity.getClaimProof(1);
expect(proof).to.be.not.null;
expect(proof.existence).to.be.false;
});
});

describe("create identity", function () {
it("deploy state and identity", async function () {
expect(await identity.getIsOldStateGenesis()).to.be.equal(true);
});

it("validate identity's id", async function () {
const id = await identity.getId();
expect(id).to.be.equal(identityId);
});
});

describe("validate initial identity", function () {
let initialClaimTreeRoot, initialRevocationTreeRoot, initialRootOfRootsTreeRoot: any;
before(async function () {
initialClaimTreeRoot = await identity.getClaimsTreeRoot();
initialRevocationTreeRoot = await identity.getRevocationsTreeRoot();
initialRootOfRootsTreeRoot = await identity.getRootsTreeRoot();
});

it("trees should be empty", async function () {
expect(initialClaimTreeRoot).be.equal(0);
expect(initialRevocationTreeRoot).to.be.equal(0);
expect(initialRootOfRootsTreeRoot).to.be.equal(0);
});

it("last roots should be empty", async function () {
const lastClaimTreeRoot = await identity.getLatestPublishedClaimsRoot();
const lastRevocationTreeRoot = await identity.getLatestPublishedRevocationsRoot();
const lastRootOfRootsTreeRoot = await identity.getLatestPublishedRootsRoot();

expect(lastClaimTreeRoot).to.be.equal(initialClaimTreeRoot);
expect(lastRevocationTreeRoot).to.be.equal(0);
expect(lastRootOfRootsTreeRoot).to.be.equal(0);
});
it("since the identity did not perform the transition - the isGenesis flag should be true", async function () {
const isOldStateGenesis = await identity.getIsOldStateGenesis();
expect(isOldStateGenesis).to.be.true;
});
it("latest identity state should be empty", async function () {
latestSavedState = await identity.getLatestPublishedState();
expect(latestSavedState).to.be.equal(0);
});
it("getClaimProofWithStateInfo should return non-existence proof", async function () {
const proof = await identity.getClaimProofWithStateInfo(1);
expect(proof[0].existence).to.be.false;
});
it("getRevocationProofWithStateInfo should return non-existence proof", async function () {
const proof = await identity.getRevocationProofWithStateInfo(1);
expect(proof[0].existence).to.be.false;
});
it("getRootProofWithStateInfo should return non-existence proof", async function () {
const proof = await identity.getRootProofWithStateInfo(1);
expect(proof[0].existence).to.be.false;
});
});

describe("add claim", function () {
let initialClaimTreeRoot,
initialRevocationTreeRoot,
Expand Down

0 comments on commit c758890

Please sign in to comment.