Skip to content

Commit 57e2d4f

Browse files
committed
fix: pkpSign with K256 and P256 signing schemes tests by using elliptic
1 parent 6033f34 commit 57e2d4f

File tree

4 files changed

+56
-59
lines changed

4 files changed

+56
-59
lines changed

local-tests/tests/testUseEoaSessionSigsToPkpSignK256.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ethers } from 'ethers';
1+
import EC from 'elliptic';
2+
import { createHash } from 'crypto';
23

34
import { log } from '@lit-protocol/misc';
45
import { getEoaSessionSigs } from 'local-tests/setup/session-sigs/get-eoa-session-sigs';
@@ -15,9 +16,7 @@ export const testUseEoaSessionSigsToPkpSignK256 = async (
1516
) => {
1617
const alice = await devEnv.createRandomPerson();
1718
const messageToSign = [1, 2, 3, 4, 5];
18-
const messageHash = ethers.utils.arrayify(
19-
ethers.utils.keccak256(messageToSign)
20-
);
19+
const messageHash = createHash('sha256').update(Buffer.from(messageToSign)).digest();
2120

2221
const eoaSessionSigs = await getEoaSessionSigs(devEnv, alice);
2322
const runWithSessionSigs = await devEnv.litNodeClient.pkpSign({
@@ -54,27 +53,25 @@ export const testUseEoaSessionSigsToPkpSignK256 = async (
5453
throw new Error(`Expected "recid" to be parseable as a number`);
5554
}
5655

57-
const signature = ethers.utils.joinSignature({
58-
r: '0x' + runWithSessionSigs.r,
59-
s: '0x' + runWithSessionSigs.s,
60-
recoveryParam: runWithSessionSigs.recid,
61-
});
62-
const recoveredPubKey = ethers.utils.recoverPublicKey(messageHash, signature);
63-
64-
console.log('recoveredPubKey:', recoveredPubKey);
56+
const ec = new EC.ec('secp256k1');
6557

66-
const runWithSessionSigsUncompressedPublicKey = ethers.utils.computePublicKey(
67-
'0x' + runWithSessionSigs.publicKey
58+
// Public key derived from message and signature
59+
const recoveredPubKey = ec.recoverPubKey(
60+
messageHash,
61+
runWithSessionSigs,
62+
runWithSessionSigs.recid
6863
);
69-
if (
70-
runWithSessionSigsUncompressedPublicKey !==
71-
`0x${alice.pkp.publicKey.toLowerCase()}`
72-
) {
64+
// Public key returned from nodes
65+
const runWithSessionSigsUncompressedPublicKey = ec
66+
.keyFromPublic(runWithSessionSigs.publicKey, 'hex')
67+
.getPublic(false, 'hex');
68+
69+
if (runWithSessionSigsUncompressedPublicKey !== recoveredPubKey.encode('hex', false)) {
7370
throw new Error(
74-
`Expected recovered public key to match runWithSessionSigsUncompressedPublicKey and alice.pkp.publicKey`
71+
`Expected recovered public key to match runWithSessionSigsUncompressedPublicKey and recoveredPubKey.encode('hex', false)`
7572
);
7673
}
77-
if (recoveredPubKey !== `0x${alice.pkp.publicKey.toLowerCase()}`) {
74+
if (recoveredPubKey.encode('hex', false) !== alice.pkp.publicKey) {
7875
throw new Error(
7976
`Expected recovered public key to match alice.pkp.publicKey`
8077
);
Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ethers } from 'ethers';
1+
import EC from 'elliptic';
22
import { createHash } from 'crypto';
33

44
import { log } from '@lit-protocol/misc';
@@ -15,8 +15,8 @@ export const testUseEoaSessionSigsToPkpSignP256 = async (
1515
devEnv: TinnyEnvironment
1616
) => {
1717
const alice = await devEnv.createRandomPerson();
18-
const messageToSign = new Uint8Array([1, 2, 3, 4, 5]);
19-
const messageHash = createHash('sha256').update(messageToSign).digest();
18+
const messageToSign = [1, 2, 3, 4, 5];
19+
const messageHash = createHash('sha256').update(Buffer.from(messageToSign)).digest();
2020

2121
const eoaSessionSigs = await getEoaSessionSigs(devEnv, alice);
2222
const runWithSessionSigs = await devEnv.litNodeClient.pkpSign({
@@ -28,16 +28,6 @@ export const testUseEoaSessionSigsToPkpSignP256 = async (
2828

2929
devEnv.releasePrivateKeyFromUser(alice);
3030

31-
// Expected output:
32-
// {
33-
// r: "25fc0d2fecde8ed801e9fee5ad26f2cf61d82e6f45c8ad1ad1e4798d3b747fd9",
34-
// s: "549fe745b4a09536e6e7108d814cf7e44b93f1d73c41931b8d57d1b101833214",
35-
// recid: 1,
36-
// signature: "0x25fc0d2fecde8ed801e9fee5ad26f2cf61d82e6f45c8ad1ad1e4798d3b747fd9549fe745b4a09536e6e7108d814cf7e44b93f1d73c41931b8d57d1b1018332141c",
37-
// publicKey: "04A3CD53CCF63597D3FFCD1DF1E8236F642C7DF8196F532C8104625635DC55A1EE59ABD2959077432FF635DF2CED36CC153050902B71291C4D4867E7DAAF964049",
38-
// dataSigned: "7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4",
39-
// }
40-
4131
// -- assertions
4232
// r, s, dataSigned, and public key should be present
4333
if (!runWithSessionSigs.r) {
@@ -63,29 +53,30 @@ export const testUseEoaSessionSigsToPkpSignP256 = async (
6353
throw new Error(`Expected "recid" to be parseable as a number`);
6454
}
6555

66-
// TODO fix after fixing K256
67-
const signature = ethers.utils.joinSignature({
68-
r: '0x' + runWithSessionSigs.r,
69-
s: '0x' + runWithSessionSigs.s,
70-
recoveryParam: runWithSessionSigs.recid,
71-
});
72-
const recoveredPubKey = ethers.utils.recoverPublicKey(
73-
alice.loveLetter,
74-
signature
75-
);
56+
const ec = new EC.ec('p256');
7657

77-
console.log('recoveredPubKey:', recoveredPubKey);
58+
// Public key derived from message and signature
59+
const recoveredPubKey = ec.recoverPubKey(
60+
messageHash,
61+
runWithSessionSigs,
62+
runWithSessionSigs.recid
63+
);
64+
// Public key returned from nodes
65+
const runWithSessionSigsUncompressedPublicKey = ec
66+
.keyFromPublic(runWithSessionSigs.publicKey, 'hex')
67+
.getPublic(false, 'hex');
7868

79-
if (recoveredPubKey !== `0x${runWithSessionSigs.publicKey.toLowerCase()}`) {
69+
if (runWithSessionSigsUncompressedPublicKey !== recoveredPubKey.encode('hex', false)) {
8070
throw new Error(
81-
`Expected recovered public key to match runWithSessionSigs.publicKey`
82-
);
83-
}
84-
if (recoveredPubKey !== `0x${alice.pkp.publicKey.toLowerCase()}`) {
85-
throw new Error(
86-
`Expected recovered public key to match alice.pkp.publicKey`
71+
`Expected recovered public key to match runWithSessionSigsUncompressedPublicKey and recoveredPubKey.encode('hex', false)`
8772
);
8873
}
74+
// PKP public key lives in k256, it cannot be directly compared
75+
// if (recoveredPubKey.encode('hex', false) !== alice.pkp.publicKey) {
76+
// throw new Error(
77+
// `Expected recovered public key to match alice.pkp.publicKey`
78+
// );
79+
// }
8980

9081
log('✅ testUseEoaSessionSigsToPkpSignP256');
9182
};

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"cross-fetch": "3.1.8",
6262
"date-and-time": "^2.4.1",
6363
"depd": "^2.0.0",
64+
"elliptic": "^6.6.1",
6465
"ethers": "^5.7.1",
6566
"jose": "^4.14.4",
6667
"micromodal": "^0.4.10",
@@ -87,6 +88,7 @@
8788
"@nx/web": "17.3.0",
8889
"@solana/web3.js": "1.95.3",
8990
"@types/depd": "^1.1.36",
91+
"@types/elliptic": "^6.4.18",
9092
"@types/events": "^3.0.3",
9193
"@types/jest": "27.4.1",
9294
"@types/node": "18.19.18",

yarn.lock

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5111,20 +5111,20 @@
51115111
dependencies:
51125112
"@babel/types" "^7.20.7"
51135113

5114+
"@types/bn.js@*", "@types/bn.js@^5.1.0", "@types/bn.js@^5.1.1":
5115+
version "5.1.6"
5116+
resolved "https://registry.yarnpkg.com/@types/bn.js/-/bn.js-5.1.6.tgz#9ba818eec0c85e4d3c679518428afdf611d03203"
5117+
integrity sha512-Xh8vSwUeMKeYYrj3cX4lGQgFSF/N03r+tv4AiLl1SucqV+uTQpxRcnM8AkXKHwYP9ZPXOYXRr2KPXpVlIvqh9w==
5118+
dependencies:
5119+
"@types/node" "*"
5120+
51145121
"@types/bn.js@^4.11.3":
51155122
version "4.11.6"
51165123
resolved "https://registry.yarnpkg.com/@types/bn.js/-/bn.js-4.11.6.tgz#c306c70d9358aaea33cd4eda092a742b9505967c"
51175124
integrity sha512-pqr857jrp2kPuO9uRjZ3PwnJTjoQy+fcdxvBTvHm6dkmEL9q+hDD/2j/0ELOBPtPnS8LjCX0gI9nbl8lVkadpg==
51185125
dependencies:
51195126
"@types/node" "*"
51205127

5121-
"@types/bn.js@^5.1.0", "@types/bn.js@^5.1.1":
5122-
version "5.1.6"
5123-
resolved "https://registry.yarnpkg.com/@types/bn.js/-/bn.js-5.1.6.tgz#9ba818eec0c85e4d3c679518428afdf611d03203"
5124-
integrity sha512-Xh8vSwUeMKeYYrj3cX4lGQgFSF/N03r+tv4AiLl1SucqV+uTQpxRcnM8AkXKHwYP9ZPXOYXRr2KPXpVlIvqh9w==
5125-
dependencies:
5126-
"@types/node" "*"
5127-
51285128
"@types/cacheable-request@^6.0.1", "@types/cacheable-request@^6.0.2":
51295129
version "6.0.3"
51305130
resolved "https://registry.yarnpkg.com/@types/cacheable-request/-/cacheable-request-6.0.3.tgz#a430b3260466ca7b5ca5bfd735693b36e7a9d183"
@@ -5149,6 +5149,13 @@
51495149
dependencies:
51505150
"@types/node" "*"
51515151

5152+
"@types/elliptic@^6.4.18":
5153+
version "6.4.18"
5154+
resolved "https://registry.yarnpkg.com/@types/elliptic/-/elliptic-6.4.18.tgz#bc96e26e1ccccbabe8b6f0e409c85898635482e1"
5155+
integrity sha512-UseG6H5vjRiNpQvrhy4VF/JXdA3V/Fp5amvveaL+fs28BZ6xIKJBPnUPRlEaZpysD9MbpfaLi8lbl7PGUAkpWw==
5156+
dependencies:
5157+
"@types/bn.js" "*"
5158+
51525159
"@types/events@^3.0.3":
51535160
version "3.0.3"
51545161
resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.3.tgz#a8ef894305af28d1fc6d2dfdfc98e899591ea529"
@@ -10452,7 +10459,7 @@ [email protected]:
1045210459
minimalistic-assert "^1.0.1"
1045310460
minimalistic-crypto-utils "^1.0.1"
1045410461

10455-
elliptic@^6.4.0, elliptic@^6.5.2, elliptic@^6.5.3, elliptic@^6.5.4, elliptic@^6.5.5, elliptic@^6.5.7:
10462+
elliptic@^6.4.0, elliptic@^6.5.2, elliptic@^6.5.3, elliptic@^6.5.4, elliptic@^6.5.5, elliptic@^6.5.7, elliptic@^6.6.1:
1045610463
version "6.6.1"
1045710464
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.6.1.tgz#3b8ffb02670bf69e382c7f65bf524c97c5405c06"
1045810465
integrity sha512-RaddvvMatK2LJHqFJ+YA4WysVN5Ita9E35botqIYspQ4TkRAlCicdzKOjlyv/1Za5RyTNn7di//eEV0uTAfe3g==

0 commit comments

Comments
 (0)