1- import { ethers } from 'ethers ' ;
1+ import EC from 'elliptic ' ;
22import { createHash } from 'crypto' ;
33
44import { log } from '@lit-protocol/misc' ;
@@ -15,8 +15,10 @@ export const testUseEoaSessionSigsToPkpSignP384 = 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 ( 'sha384' ) . update ( messageToSign ) . digest ( ) ;
18+ const messageToSign = [ 1 , 2 , 3 , 4 , 5 ] ;
19+ const messageHash = createHash ( 'sha384' )
20+ . update ( Buffer . from ( messageToSign ) )
21+ . digest ( ) ;
2022
2123 const eoaSessionSigs = await getEoaSessionSigs ( devEnv , alice ) ;
2224 const runWithSessionSigs = await devEnv . litNodeClient . pkpSign ( {
@@ -28,16 +30,6 @@ export const testUseEoaSessionSigsToPkpSignP384 = async (
2830
2931 devEnv . releasePrivateKeyFromUser ( alice ) ;
3032
31- // Expected output:
32- // {
33- // r: "25fc0d2fecde8ed801e9fee5ad26f2cf61d82e6f45c8ad1ad1e4798d3b747fd9",
34- // s: "549fe745b4a09536e6e7108d814cf7e44b93f1d73c41931b8d57d1b101833214",
35- // recid: 1,
36- // signature: "0x25fc0d2fecde8ed801e9fee5ad26f2cf61d82e6f45c8ad1ad1e4798d3b747fd9549fe745b4a09536e6e7108d814cf7e44b93f1d73c41931b8d57d1b1018332141c",
37- // publicKey: "04A3CD53CCF63597D3FFCD1DF1E8236F642C7DF8196F532C8104625635DC55A1EE59ABD2959077432FF635DF2CED36CC153050902B71291C4D4867E7DAAF964049",
38- // dataSigned: "7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4",
39- // }
40-
4133 // -- assertions
4234 // r, s, dataSigned, and public key should be present
4335 if ( ! runWithSessionSigs . r ) {
@@ -63,29 +55,33 @@ export const testUseEoaSessionSigsToPkpSignP384 = async (
6355 throw new Error ( `Expected "recid" to be parseable as a number` ) ;
6456 }
6557
66- // TODO fix after fixing P256
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- ) ;
58+ const ec = new EC . ec ( 'p384' ) ;
7659
77- 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+ ) ; // Error: The recovery param is more than two bits
66+ // Public key returned from nodes
67+ const runWithSessionSigsUncompressedPublicKey = ec
68+ . keyFromPublic ( runWithSessionSigs . publicKey , 'hex' )
69+ . getPublic ( false , 'hex' ) ;
7870
79- if ( recoveredPubKey !== `0x${ runWithSessionSigs . publicKey . toLowerCase ( ) } ` ) {
71+ if (
72+ runWithSessionSigsUncompressedPublicKey !==
73+ recoveredPubKey . encode ( 'hex' , false )
74+ ) {
8075 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`
76+ `Expected recovered public key to match runWithSessionSigsUncompressedPublicKey and recoveredPubKey.encode('hex', false)`
8777 ) ;
8878 }
79+ // PKP public key lives in k256, it cannot be directly compared
80+ // if (recoveredPubKey.encode('hex', false) !== alice.pkp.publicKey) {
81+ // throw new Error(
82+ // `Expected recovered public key to match alice.pkp.publicKey`
83+ // );
84+ // }
8985
9086 log ( '✅ testUseEoaSessionSigsToPkpSignP384' ) ;
9187} ;
0 commit comments