|
| 1 | +import { log } from '@lit-protocol/misc'; |
| 2 | +import { TinnyEnvironment } from 'local-tests/setup/tinny-environment'; |
| 3 | +import { api } from '@lit-protocol/wrapped-keys'; |
| 4 | +import { getPkpSessionSigs } from 'local-tests/setup/session-sigs/get-pkp-session-sigs'; |
| 5 | +import nacl from 'tweetnacl'; |
| 6 | +import bs58 from 'bs58'; |
| 7 | +import { ethers } from 'ethers'; |
| 8 | +import { BatchGeneratePrivateKeysActionResult } from '../../../packages/wrapped-keys/src/lib/types'; |
| 9 | + |
| 10 | +const { batchGeneratePrivateKeys } = api; |
| 11 | + |
| 12 | +async function verifySolanaSignature( |
| 13 | + solanaResult: BatchGeneratePrivateKeysActionResult, |
| 14 | + solanaMessageToSign |
| 15 | +) { |
| 16 | + const { |
| 17 | + signMessage: { signature }, |
| 18 | + generateEncryptedPrivateKey: { generatedPublicKey }, |
| 19 | + } = solanaResult; |
| 20 | + const signatureIsValidForPublicKey = nacl.sign.detached.verify( |
| 21 | + Buffer.from(solanaMessageToSign), |
| 22 | + bs58.decode(signature), |
| 23 | + bs58.decode(generatedPublicKey) |
| 24 | + ); |
| 25 | + |
| 26 | + console.log({ signatureIsValidForPublicKey, signature }); |
| 27 | + if (!signatureIsValidForPublicKey) { |
| 28 | + throw new Error( |
| 29 | + `signature: ${signature} doesn't validate for the Solana public key: ${generatedPublicKey}` |
| 30 | + ); |
| 31 | + } |
| 32 | +} |
| 33 | +async function verifyEvmSignature(evmResult, messageToSign) { |
| 34 | + function verifyMessageSignature() { |
| 35 | + try { |
| 36 | + return ethers.utils.verifyMessage( |
| 37 | + messageToSign, |
| 38 | + evmResult.signMessage.signature |
| 39 | + ); |
| 40 | + } catch (err) { |
| 41 | + throw new Error( |
| 42 | + `When validating signed Ethereum message is valid: ${err.message}` |
| 43 | + ); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + const walletAddress = ethers.utils.computeAddress( |
| 48 | + evmResult.generateEncryptedPrivateKey.generatedPublicKey |
| 49 | + ); |
| 50 | + |
| 51 | + const recoveredAddress = verifyMessageSignature(); |
| 52 | + |
| 53 | + console.log({ |
| 54 | + recoveredAddress, |
| 55 | + walletAddress, |
| 56 | + signature: evmResult.signMessage.signature, |
| 57 | + }); |
| 58 | + if (recoveredAddress !== walletAddress) { |
| 59 | + throw new Error( |
| 60 | + "Recovered address from verifyMessage doesn't match the wallet address" |
| 61 | + ); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Test Commands: |
| 67 | + * ✅ NETWORK=datil-dev yarn test:local --filter=testSignMessageWithSolanaEncryptedKey |
| 68 | + * ✅ NETWORK=datil-test yarn test:local --filter=testSignMessageWithSolanaEncryptedKey |
| 69 | + * ✅ NETWORK=localchain yarn test:local --filter=testSignMessageWithSolanaEncryptedKey |
| 70 | + */ |
| 71 | +export const testBatchGeneratePrivateKeys = async ( |
| 72 | + devEnv: TinnyEnvironment |
| 73 | +) => { |
| 74 | + const alice = await devEnv.createRandomPerson(); |
| 75 | + |
| 76 | + try { |
| 77 | + const pkpSessionSigsSigning = await getPkpSessionSigs( |
| 78 | + devEnv, |
| 79 | + alice, |
| 80 | + null, |
| 81 | + new Date(Date.now() + 1000 * 60 * 10).toISOString() |
| 82 | + ); // 10 mins expiry |
| 83 | + |
| 84 | + const solanaMessageToSign = 'This is a test solana message'; |
| 85 | + const evmMessageToSign = 'This is a test evm message'; |
| 86 | + const { results } = await batchGeneratePrivateKeys({ |
| 87 | + pkpSessionSigs: pkpSessionSigsSigning, |
| 88 | + actions: [ |
| 89 | + { |
| 90 | + network: 'evm', |
| 91 | + signMessageParams: { messageToSign: evmMessageToSign }, |
| 92 | + generateKeyParams: { memo: 'Test evm key' }, |
| 93 | + }, |
| 94 | + { |
| 95 | + network: 'solana', |
| 96 | + signMessageParams: { messageToSign: solanaMessageToSign }, |
| 97 | + generateKeyParams: { memo: 'Test solana key' }, |
| 98 | + }, |
| 99 | + ], |
| 100 | + litNodeClient: devEnv.litNodeClient, |
| 101 | + }); |
| 102 | + |
| 103 | + if (results.length !== 2) { |
| 104 | + throw new Error( |
| 105 | + `Incorrect # of results; expected 2, got ${results.length}` |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + if ( |
| 110 | + results[0].generateEncryptedPrivateKey.memo !== 'Test evm key' || |
| 111 | + results[1].generateEncryptedPrivateKey.memo !== 'Test solana key' |
| 112 | + ) { |
| 113 | + throw new Error( |
| 114 | + 'Results not in order sent; expected evm as first result, solana as second' |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + if ( |
| 119 | + !results[0].signMessage.signature || |
| 120 | + !results[1].signMessage.signature |
| 121 | + ) { |
| 122 | + throw new Error('Missing message signature in response'); |
| 123 | + } |
| 124 | + |
| 125 | + console.log('solana verify sig'); |
| 126 | + await verifySolanaSignature(results[1], solanaMessageToSign); |
| 127 | + |
| 128 | + console.log('evm verify sig'); |
| 129 | + await verifyEvmSignature(results[0], evmMessageToSign); |
| 130 | + console.log('results', results); |
| 131 | + |
| 132 | + log('✅ testBatchGenerateEncryptedKeys'); |
| 133 | + } catch (err) { |
| 134 | + console.log(err.message, err, err.stack); |
| 135 | + throw err; |
| 136 | + } finally { |
| 137 | + devEnv.releasePrivateKeyFromUser(alice); |
| 138 | + } |
| 139 | +}; |
0 commit comments