1- import { ethers } from 'ethers' ;
1+ import EC from 'elliptic' ;
2+ import { createHash } from 'crypto' ;
23
34import { log } from '@lit-protocol/misc' ;
45import { getEoaSessionSigs } from 'local-tests/setup/session-sigs/get-eoa-session-sigs' ;
56import { TinnyEnvironment } from 'local-tests/setup/tinny-environment' ;
67
78/**
89 * Test Commands:
9- * ✅ NETWORK=datil-dev yarn test:local --filter=testUseEoaSessionSigsToPkpSign
10- * ✅ NETWORK=datil-test yarn test:local --filter=testUseEoaSessionSigsToPkpSign
11- * ✅ NETWORK=custom yarn test:local --filter=testUseEoaSessionSigsToPkpSign
10+ * ✅ NETWORK=datil-dev yarn test:local --filter=testUseEoaSessionSigsToPkpSignP256
11+ * ✅ NETWORK=datil-test yarn test:local --filter=testUseEoaSessionSigsToPkpSignP256
12+ * ✅ NETWORK=custom yarn test:local --filter=testUseEoaSessionSigsToPkpSignP256
1213 */
13- export const testUseEoaSessionSigsToPkpSign = async (
14+ export const testUseEoaSessionSigsToPkpSignP256 = async (
1415 devEnv : TinnyEnvironment
1516) => {
1617 const alice = await devEnv . createRandomPerson ( ) ;
18+ const messageToSign = [ 1 , 2 , 3 , 4 , 5 ] ;
19+ const messageHash = createHash ( 'sha256' )
20+ . update ( Buffer . from ( messageToSign ) )
21+ . digest ( ) ;
1722
1823 const eoaSessionSigs = await getEoaSessionSigs ( devEnv , alice ) ;
1924 const runWithSessionSigs = await devEnv . litNodeClient . pkpSign ( {
20- toSign : alice . loveLetter ,
2125 pubKey : alice . pkp . publicKey ,
2226 sessionSigs : eoaSessionSigs ,
27+ toSign : messageHash ,
28+ signingScheme : 'EcdsaP256Sha256' ,
2329 } ) ;
2430
2531 devEnv . releasePrivateKeyFromUser ( alice ) ;
2632
27- // Expected output:
28- // {
29- // r: "25fc0d2fecde8ed801e9fee5ad26f2cf61d82e6f45c8ad1ad1e4798d3b747fd9",
30- // s: "549fe745b4a09536e6e7108d814cf7e44b93f1d73c41931b8d57d1b101833214",
31- // recid: 1,
32- // signature: "0x25fc0d2fecde8ed801e9fee5ad26f2cf61d82e6f45c8ad1ad1e4798d3b747fd9549fe745b4a09536e6e7108d814cf7e44b93f1d73c41931b8d57d1b1018332141c",
33- // publicKey: "04A3CD53CCF63597D3FFCD1DF1E8236F642C7DF8196F532C8104625635DC55A1EE59ABD2959077432FF635DF2CED36CC153050902B71291C4D4867E7DAAF964049",
34- // dataSigned: "7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4",
35- // }
36-
3733 // -- assertions
3834 // r, s, dataSigned, and public key should be present
3935 if ( ! runWithSessionSigs . r ) {
@@ -59,29 +55,33 @@ export const testUseEoaSessionSigsToPkpSign = async (
5955 throw new Error ( `Expected "recid" to be parseable as a number` ) ;
6056 }
6157
62- const signature = ethers . utils . joinSignature ( {
63- r : '0x' + runWithSessionSigs . r ,
64- s : '0x' + runWithSessionSigs . s ,
65- recoveryParam : runWithSessionSigs . recid ,
66- } ) ;
67- const recoveredPubKey = ethers . utils . recoverPublicKey (
68- alice . loveLetter ,
69- signature
70- ) ;
58+ const ec = new EC . ec ( 'p256' ) ;
7159
72- console . log ( 'recoveredPubKey:' , recoveredPubKey ) ;
60+ // Public key derived from message and signature
61+ const recoveredPubKey = ec . recoverPubKey (
62+ messageHash ,
63+ runWithSessionSigs ,
64+ runWithSessionSigs . recid
65+ ) ;
66+ // Public key returned from nodes
67+ const runWithSessionSigsUncompressedPublicKey = ec
68+ . keyFromPublic ( runWithSessionSigs . publicKey , 'hex' )
69+ . getPublic ( false , 'hex' ) ;
7370
74- // FIXME: Consider adding these assertions back after the v flipping PR is merged
75- // if (recoveredPubKey !== `0x${runWithSessionSigs.publicKey.toLowerCase()}`) {
76- // throw new Error(
77- // `Expected recovered public key to match runWithSessionSigs.publicKey`
78- // );
79- // }
80- // if (recoveredPubKey !== `0x${alice.pkp.publicKey.toLowerCase()}`) {
71+ if (
72+ runWithSessionSigsUncompressedPublicKey !==
73+ recoveredPubKey . encode ( 'hex' , false )
74+ ) {
75+ throw new Error (
76+ `Expected recovered public key to match runWithSessionSigsUncompressedPublicKey and recoveredPubKey.encode('hex', false)`
77+ ) ;
78+ }
79+ // PKP public key lives in k256, it cannot be directly compared
80+ // if (recoveredPubKey.encode('hex', false) !== alice.pkp.publicKey) {
8181 // throw new Error(
8282 // `Expected recovered public key to match alice.pkp.publicKey`
8383 // );
8484 // }
8585
86- log ( '✅ testUseEoaSessionSigsToPkpSign ' ) ;
86+ log ( '✅ testUseEoaSessionSigsToPkpSignP256 ' ) ;
8787} ;
0 commit comments