Skip to content

Commit 9548dd8

Browse files
committed
wip
1 parent 60ec3db commit 9548dd8

File tree

9 files changed

+669
-74
lines changed

9 files changed

+669
-74
lines changed

local-tests/tests/testUseTriaAuthAndWrappedKeysSessionSigsGen.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import {
66
getLitActionSessionSigsUsingIpfsId,
77
} from 'local-tests/setup/session-sigs/get-lit-action-session-sigs';
88
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';
9+
import { api, utils } from '@lit-protocol/wrapped-keys';
910

11+
import Hash from "typestub-ipfs-only-hash";
12+
import { AuthMethodScope } from '@lit-protocol/constants';
13+
14+
const { triaBatchGeneratePrivateKeys, exportPrivateKey } = api;
1015
/**
1116
* Test Commands:
1217
* ✅ NETWORK=datil-dev yarn test:local --filter=testUseTriaAuthAndWrappedKeysSessionSigsGen
@@ -57,6 +62,46 @@ export const testUseTriaAuthAndWrappedKeysSessionSigsGen = async (
5762
console.log(` - Capacity Token ID: ${capacityTokenId}`);
5863

5964
// -- Get the lit action code..
65+
const { litActionCode, litActionIpfsCid } = utils.getLitActionCodeOrCidCommon(
66+
'tria_batchGenerateEncryptedKeys'
67+
);
68+
69+
// -- detect which one we got
70+
const hashOfLitActionCode = litActionCode
71+
? await Hash.of(Buffer.from(litActionCode))
72+
: litActionIpfsCid;
73+
console.log(` ✅ Lit Action Code IPFS CID: ${hashOfLitActionCode}`);
74+
75+
console.log(`🔄 Adding permitted action...`);
76+
const permitTx = await devEnv.contractsClient.addPermittedAction({
77+
ipfsId: hashOfLitActionCode,
78+
pkpTokenId: pkp.tokenId,
79+
authMethodScopes: [AuthMethodScope.SignAnything]
80+
});
81+
console.log(` ✅ Permitted action added:`);
82+
console.log(` - Transaction Hash: ${permitTx.transactionHash}`);
83+
84+
const solanaMessageToSign = 'This is a test solana message';
85+
const evmMessageToSign = 'This is a test evm message';
86+
87+
const { results } = await triaBatchGeneratePrivateKeys({
88+
ipfsId: hashOfLitActionCode,
89+
pkpPublicKey: pkp.publicKey,
90+
actions: [
91+
{
92+
network: 'evm',
93+
signMessageParams: { messageToSign: evmMessageToSign },
94+
generateKeyParams: { memo: 'Test evm key' },
95+
},
96+
{
97+
network: 'solana',
98+
signMessageParams: { messageToSign: solanaMessageToSign },
99+
generateKeyParams: { memo: 'Test solana key' },
100+
},
101+
],
102+
litNodeClient: devEnv.litNodeClient,
103+
});
104+
60105

61106
process.exit();
62107

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import { log } from '@lit-protocol/misc';
2+
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';
3+
import { getPkpSessionSigs } from 'local-tests/setup/session-sigs/get-pkp-session-sigs';
4+
import nacl from 'tweetnacl';
5+
import bs58 from 'bs58';
6+
import { ethers } from 'ethers';
7+
import { BatchGeneratePrivateKeysActionResult } from '../../../packages/wrapped-keys/src/lib/types';
8+
import { triaBatchGeneratePrivateKeys } from 'packages/wrapped-keys/src/lib/api';
9+
10+
async function verifySolanaSignature(
11+
solanaResult: BatchGeneratePrivateKeysActionResult,
12+
solanaMessageToSign
13+
) {
14+
const {
15+
signMessage: { signature },
16+
generateEncryptedPrivateKey: { generatedPublicKey },
17+
} = solanaResult;
18+
const signatureIsValidForPublicKey = nacl.sign.detached.verify(
19+
Buffer.from(solanaMessageToSign),
20+
bs58.decode(signature),
21+
bs58.decode(generatedPublicKey)
22+
);
23+
24+
console.log({ signatureIsValidForPublicKey, signature });
25+
if (!signatureIsValidForPublicKey) {
26+
throw new Error(
27+
`signature: ${signature} doesn't validate for the Solana public key: ${generatedPublicKey}`
28+
);
29+
}
30+
}
31+
async function verifyEvmSignature(evmResult, messageToSign) {
32+
function verifyMessageSignature() {
33+
try {
34+
return ethers.utils.verifyMessage(
35+
messageToSign,
36+
evmResult.signMessage.signature
37+
);
38+
} catch (err) {
39+
throw new Error(
40+
`When validating signed Ethereum message is valid: ${err.message}`
41+
);
42+
}
43+
}
44+
45+
const walletAddress = ethers.utils.computeAddress(
46+
evmResult.generateEncryptedPrivateKey.generatedPublicKey
47+
);
48+
49+
const recoveredAddress = verifyMessageSignature();
50+
51+
console.log({
52+
recoveredAddress,
53+
walletAddress,
54+
signature: evmResult.signMessage.signature,
55+
});
56+
if (recoveredAddress !== walletAddress) {
57+
throw new Error(
58+
"Recovered address from verifyMessage doesn't match the wallet address"
59+
);
60+
}
61+
}
62+
63+
/**
64+
* Test Commands:
65+
* ✅ NETWORK=datil-dev yarn test:local --filter=testTriaBatchGeneratePrivateKeys
66+
* ✅ NETWORK=datil-test yarn test:local --filter=testTriaBatchGeneratePrivateKeys
67+
* ✅ NETWORK=localchain yarn test:local --filter=testTriaBatchGeneratePrivateKeys
68+
*/
69+
export const testTriaBatchGeneratePrivateKeys = async (
70+
devEnv: TinnyEnvironment
71+
) => {
72+
const alice = await devEnv.createRandomPerson();
73+
74+
const TEN_MINUTES_EXPIRY = new Date(
75+
Date.now() + 1000 * 60 * 10
76+
).toISOString();
77+
78+
// const litActionSessionSigs = devEnv.litNodeClient.getLitActionSessionSigs({
79+
80+
// pkpPublicKey: alice.authMethodOwnedPkp.publicKey,
81+
// resourceAbilityRequests: [
82+
// {
83+
// resource: new LitPKPResource('*'),
84+
// ability: LitAbility.PKPSigning,
85+
// },
86+
// {
87+
// resource: new LitActionResource('*'),
88+
// ability: LitAbility.LitActionExecution,
89+
// },
90+
// ],
91+
// jsParams: {
92+
// actions: [
93+
// {
94+
// network: 'evm',
95+
// signMessageParams: { messageToSign: evmMessageToSign },
96+
// generateKeyParams: { memo: 'Test evm key' },
97+
// },
98+
// {
99+
// network: 'solana',
100+
// signMessageParams: { messageToSign: solanaMessageToSign },
101+
// generateKeyParams: { memo: 'Test solana key' },
102+
// },
103+
// ],
104+
// authMethod: {
105+
// accessToken: ansonTestCredential.authMethod.accessToken,
106+
// authMethodType: ansonTestCredential.authMethod.authMethodType,
107+
// }
108+
// }
109+
// })
110+
111+
try {
112+
// const pkpSessionSigsSigning = await getPkpSessionSigs(
113+
// devEnv,
114+
// alice,
115+
// null,
116+
// TEN_MINUTES_EXPIRY
117+
// );
118+
119+
const solanaMessageToSign = 'This is a test solana message';
120+
const evmMessageToSign = 'This is a test evm message';
121+
const { results } = await triaBatchGeneratePrivateKeys({
122+
actions: [
123+
{
124+
network: 'evm',
125+
signMessageParams: { messageToSign: evmMessageToSign },
126+
generateKeyParams: { memo: 'Test evm key' },
127+
},
128+
{
129+
network: 'solana',
130+
signMessageParams: { messageToSign: solanaMessageToSign },
131+
generateKeyParams: { memo: 'Test solana key' },
132+
},
133+
],
134+
litNodeClient: devEnv.litNodeClient,
135+
authMethod: {
136+
accessToken: ansonTestCredential.authMethod.accessToken,
137+
authMethodType: ansonTestCredential.authMethod.authMethodType,
138+
},
139+
publicKey: alice.authMethodOwnedPkp.publicKey,
140+
// publicKey: ansonTestCredential.pubKey,
141+
// accessControlConditions: [] // would be done in the Lit Action
142+
});
143+
144+
if (results.length !== 2) {
145+
throw new Error(
146+
`Incorrect # of results; expected 2, got ${results.length}`
147+
);
148+
}
149+
150+
if (
151+
results[0].generateEncryptedPrivateKey.memo !== 'Test evm key' ||
152+
results[1].generateEncryptedPrivateKey.memo !== 'Test solana key'
153+
) {
154+
throw new Error(
155+
'Results not in order sent; expected evm as first result, solana as second'
156+
);
157+
}
158+
159+
if (
160+
!results[0].signMessage.signature ||
161+
!results[1].signMessage.signature
162+
) {
163+
throw new Error('Missing message signature in response');
164+
}
165+
166+
console.log('solana verify sig');
167+
await verifySolanaSignature(results[1], solanaMessageToSign);
168+
169+
console.log('evm verify sig');
170+
await verifyEvmSignature(results[0], evmMessageToSign);
171+
console.log('results', results);
172+
173+
log('✅ testBatchGenerateEncryptedKeys');
174+
} catch (err) {
175+
console.log(err.message, err, err.stack);
176+
throw err;
177+
} finally {
178+
devEnv.releasePrivateKeyFromUser(alice);
179+
}
180+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"tslib": "^2.3.0",
8080
"tweetnacl": "^1.0.3",
8181
"tweetnacl-util": "^0.15.1",
82+
"typestub-ipfs-only-hash": "^4.0.0",
8283
"uint8arrays": "^4.0.3"
8384
},
8485
"devDependencies": {

packages/wrapped-keys-lit-actions/src/lib/common/bespoke/tria_batchGenerateEncryptedKeys.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,28 @@ function validateParams(actions) {
130130
});
131131
}
132132

133-
(async () => {
134-
try {
135-
validateParams(actions);
136-
137-
const batchGeneratePrivateKeysActionResult = await processActions(actions);
138-
139-
Lit.Actions.setResponse({
140-
response: JSON.stringify(batchGeneratePrivateKeysActionResult),
141-
});
133+
// (async () => {
134+
// try {
135+
// validateParams(actions);
136+
137+
// const batchGeneratePrivateKeysActionResult = await processActions(actions);
138+
139+
// Lit.Actions.setResponse({
140+
// response: JSON.stringify(batchGeneratePrivateKeysActionResult),
141+
// });
142+
143+
// // 1. Generate both EVM and solana private keys
144+
// // 2. Run appropriate signMessage for each key _and_ encrypt the keys for persistence to wrapped-keys backend
145+
// // 3. Return results for both signMessage ops and both encrypted key payloads for persistence
146+
// } catch (err) {
147+
// Lit.Actions.setResponse({ response: `Error: ${err.message}` });
148+
// }
149+
// })();
150+
151+
const go = async () => {
152+
LitActions.setResponse({
153+
response: "(true, 'Something else is here!')",
154+
});
155+
};
142156

143-
// 1. Generate both EVM and solana private keys
144-
// 2. Run appropriate signMessage for each key _and_ encrypt the keys for persistence to wrapped-keys backend
145-
// 3. Return results for both signMessage ops and both encrypted key payloads for persistence
146-
} catch (err) {
147-
Lit.Actions.setResponse({ response: `Error: ${err.message}` });
148-
}
149-
})();
157+
go();

packages/wrapped-keys/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
LitActionCodeRepositoryCommon,
3030
LitActionCodeRepositoryEntry,
3131
} from './lib/lit-actions-client/types';
32+
import { getLitActionCodeOrCidCommon } from './lib/lit-actions-client/utils';
3233

3334
import type { SupportedNetworks } from './lib/service-client/types';
3435
import type {
@@ -74,13 +75,20 @@ export const api = {
7475
signTransactionWithEncryptedKey,
7576
storeEncryptedKey,
7677
batchGeneratePrivateKeys,
78+
79+
// bespoke
80+
triaBatchGeneratePrivateKeys,
7781
};
7882

7983
export const config = {
8084
setLitActionsCode,
8185
setLitActionsCodeCommon,
8286
};
8387

88+
export const utils = {
89+
getLitActionCodeOrCidCommon
90+
}
91+
8492
export {
8593
ApiParamsSupportedNetworks,
8694
BaseApiParams,

0 commit comments

Comments
 (0)