Skip to content

Commit

Permalink
feat: add tests over the recovered public key from pkpSign signature
Browse files Browse the repository at this point in the history
  • Loading branch information
FedericoAmura committed Dec 27, 2024
1 parent 236ca19 commit 224fb3e
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ethers } from 'ethers';

import { getEoaSessionSigsWithCapacityDelegations } from 'local-tests/setup/session-sigs/get-eoa-session-sigs';
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';

Expand Down Expand Up @@ -92,5 +94,21 @@ export const testDelegatingCapacityCreditsNFTToAnotherWalletToPkpSign = async (
throw new Error(`Expected "recid" to be parseable as a number`);
}

const signature = ethers.utils.joinSignature({
r: '0x' + res.r,
s: '0x' + res.s,
recoveryParam: res.recid,
});
const recoveredPubKey = ethers.utils.recoverPublicKey(
alice.loveLetter,
signature
);
if (recoveredPubKey !== `0x${res.publicKey.toLowerCase()}`) {
throw new Error(`Expected recovered public key to match res.publicKey`);
}
if (recoveredPubKey !== `0x${bob.pkp.publicKey.toLowerCase()}`) {
throw new Error(`Expected recovered public key to match bob.pkp.publicKey`);
}

console.log('✅ res:', res);
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ethers } from 'ethers';

import { getEoaSessionSigsWithCapacityDelegations } from 'local-tests/setup/session-sigs/get-eoa-session-sigs';
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';

Expand Down Expand Up @@ -94,5 +96,23 @@ export const testUseCapacityDelegationAuthSigWithUnspecifiedCapacityTokenIdToPkp
throw new Error(`Expected "recid" to be parseable as a number`);
}

const signature = ethers.utils.joinSignature({
r: '0x' + res.r,
s: '0x' + res.s,
recoveryParam: res.recid,
});
const recoveredPubKey = ethers.utils.recoverPublicKey(
alice.loveLetter,
signature
);
if (recoveredPubKey !== `0x${res.publicKey.toLowerCase()}`) {
throw new Error(`Expected recovered public key to match res.publicKey`);
}
if (recoveredPubKey !== `0x${bob.pkp.publicKey.toLowerCase()}`) {
throw new Error(
`Expected recovered public key to match bob.pkp.publicKey`
);
}

console.log('✅ res:', res);
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ethers } from 'ethers';

import { getEoaSessionSigsWithCapacityDelegations } from 'local-tests/setup/session-sigs/get-eoa-session-sigs';
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';

Expand Down Expand Up @@ -86,6 +88,26 @@ export const testUseCapacityDelegationAuthSigWithUnspecifiedDelegateesToPkpSign
throw new Error(`Expected "signature" to start with 0x`);
}

const signature = ethers.utils.joinSignature({
r: '0x' + runWithSessionSigs.r,
s: '0x' + runWithSessionSigs.s,
recoveryParam: runWithSessionSigs.recid,
});
const recoveredPubKey = ethers.utils.recoverPublicKey(
alice.loveLetter,
signature
);
if (recoveredPubKey !== `0x${runWithSessionSigs.publicKey.toLowerCase()}`) {
throw new Error(
`Expected recovered public key to match runWithSessionSigs.publicKey`
);
}
if (recoveredPubKey !== `0x${bob.pkp.publicKey.toLowerCase()}`) {
throw new Error(
`Expected recovered public key to match bob.pkp.publicKey`
);
}

// recid must be parseable as a number
if (isNaN(runWithSessionSigs.recid)) {
throw new Error(`Expected "recid" to be parseable as a number`);
Expand Down
22 changes: 22 additions & 0 deletions local-tests/tests/testUseEoaSessionSigsToPkpSign.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ethers } from 'ethers';

import { log } from '@lit-protocol/misc';
import { getEoaSessionSigs } from 'local-tests/setup/session-sigs/get-eoa-session-sigs';
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';
Expand Down Expand Up @@ -58,5 +60,25 @@ export const testUseEoaSessionSigsToPkpSign = async (
throw new Error(`Expected "recid" to be parseable as a number`);
}

const signature = ethers.utils.joinSignature({
r: '0x' + runWithSessionSigs.r,
s: '0x' + runWithSessionSigs.s,
recoveryParam: runWithSessionSigs.recid,
});
const recoveredPubKey = ethers.utils.recoverPublicKey(
alice.loveLetter,
signature
);
if (recoveredPubKey !== `0x${runWithSessionSigs.publicKey.toLowerCase()}`) {
throw new Error(
`Expected recovered public key to match runWithSessionSigs.publicKey`
);
}
if (recoveredPubKey !== `0x${alice.pkp.publicKey.toLowerCase()}`) {
throw new Error(
`Expected recovered public key to match alice.pkp.publicKey`
);
}

log('✅ testUseEoaSessionSigsToPkpSign');
};
22 changes: 22 additions & 0 deletions local-tests/tests/testUsePkpSessionSigsToPkpSign.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ethers } from 'ethers';

import { log } from '@lit-protocol/misc';
import { getPkpSessionSigs } from 'local-tests/setup/session-sigs/get-pkp-session-sigs';
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';
Expand Down Expand Up @@ -58,5 +60,25 @@ export const testUsePkpSessionSigsToPkpSign = async (
throw new Error(`Expected "recid" to be parseable as a number`);
}

const signature = ethers.utils.joinSignature({
r: '0x' + res.r,
s: '0x' + res.s,
recoveryParam: res.recid,
});
const recoveredPubKey = ethers.utils.recoverPublicKey(
alice.loveLetter,
signature
);
if (recoveredPubKey !== `0x${res.publicKey.toLowerCase()}`) {
throw new Error(`Expected recovered public key to match res.publicKey`);
}
if (
recoveredPubKey !== `0x${alice.authMethodOwnedPkp.publicKey.toLowerCase()}`
) {
throw new Error(
`Expected recovered public key to match alice.authMethodOwnedPkp.publicKey`
);
}

log('✅ res:', res);
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ethers } from 'ethers';

import { log } from '@lit-protocol/misc';
import { LIT_NETWORK } from '@lit-protocol/constants';
import { getLitActionSessionSigs } from 'local-tests/setup/session-sigs/get-lit-action-session-sigs';
Expand Down Expand Up @@ -58,5 +60,25 @@ export const testUseValidLitActionCodeGeneratedSessionSigsToPkpSign = async (
throw new Error(`Expected "recid" to be parseable as a number`);
}

const signature = ethers.utils.joinSignature({
r: '0x' + res.r,
s: '0x' + res.s,
recoveryParam: res.recid,
});
const recoveredPubKey = ethers.utils.recoverPublicKey(
alice.loveLetter,
signature
);
if (recoveredPubKey !== `0x${res.publicKey.toLowerCase()}`) {
throw new Error(`Expected recovered public key to match res.publicKey`);
}
if (
recoveredPubKey !== `0x${alice.authMethodOwnedPkp.publicKey.toLowerCase()}`
) {
throw new Error(
`Expected recovered public key to match alice.authMethodOwnedPkp.publicKey`
);
}

log('✅ res:', res);
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ethers } from 'ethers';

import { log } from '@lit-protocol/misc';
import { getLitActionSessionSigsUsingIpfsId } from 'local-tests/setup/session-sigs/get-lit-action-session-sigs';
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';
Expand Down Expand Up @@ -66,5 +68,26 @@ export const testUseValidLitActionIpfsCodeGeneratedSessionSigsToPkpSign =
throw new Error(`Expected "recid" to be parseable as a number`);
}

const signature = ethers.utils.joinSignature({
r: '0x' + res.r,
s: '0x' + res.s,
recoveryParam: res.recid,
});
const recoveredPubKey = ethers.utils.recoverPublicKey(
alice.loveLetter,
signature
);
if (recoveredPubKey !== `0x${res.publicKey.toLowerCase()}`) {
throw new Error(`Expected recovered public key to match res.publicKey`);
}
if (
recoveredPubKey !==
`0x${alice.authMethodOwnedPkp.publicKey.toLowerCase()}`
) {
throw new Error(
`Expected recovered public key to match alice.authMethodOwnedPkp.publicKey`
);
}

log('✅ res:', res);
};

0 comments on commit 224fb3e

Please sign in to comment.