Skip to content

Commit

Permalink
feat: add litActionIpfsId to param
Browse files Browse the repository at this point in the history
  • Loading branch information
Ansonhkg committed May 3, 2024
1 parent d5925ad commit 45276bd
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 4 deletions.
44 changes: 44 additions & 0 deletions local-tests/setup/session-sigs/get-lit-action-session-sigs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const INVALID_SESSION_SIG_LIT_ACTION_CODE = `
})();
`;

export const VALID_IPFS_ID = 'QmNZQXmY2VijUPfNrkC6zWykBnEniDouAeUpFi9r6aaqNz';

export const getLitActionSessionSigs = async (
devEnv: TinnyEnvironment,
alice: TinnyPerson,
Expand Down Expand Up @@ -65,6 +67,48 @@ export const getLitActionSessionSigs = async (
return litActionSessionSigs;
};

export const getLitActionSessionSigsUsingIpfsId = async (
devEnv: TinnyEnvironment,
alice: TinnyPerson,
resourceAbilityRequests?: LitResourceAbilityRequest[]
) => {
if (devEnv.litNodeClient.config.litNetwork === LitNetwork.Manzano) {
console.warn(
'Manzano network detected. Adding capacityDelegationAuthSig to litActionSessionSigs'
);
}

// Use default resourceAbilityRequests if not provided
const _resourceAbilityRequests = resourceAbilityRequests || [
{
resource: new LitPKPResource('*'),
ability: LitAbility.PKPSigning,
},
{
resource: new LitActionResource('*'),
ability: LitAbility.LitActionExecution,
},
];

const litActionSessionSigs = await devEnv.litNodeClient.getPkpSessionSigs({
pkpPublicKey: alice.authMethodOwnedPkp.publicKey,
authMethods: [alice.authMethod],
resourceAbilityRequests: _resourceAbilityRequests,
litActionIpfsId: VALID_IPFS_ID,
jsParams: {
publicKey: alice.authMethodOwnedPkp.publicKey,
sigName: 'unified-auth-sig',
},

// -- only add this for manzano network
...(devEnv.litNodeClient.config.litNetwork === LitNetwork.Manzano
? { capacityDelegationAuthSig: devEnv.superCapacityDelegationAuthSig }
: {}),
});

return litActionSessionSigs;
};

export const getInvalidLitActionSessionSigs = async (
devEnv: TinnyEnvironment,
alice: TinnyPerson
Expand Down
4 changes: 4 additions & 0 deletions local-tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { testUsePkpSessionSigsToExecuteJsSigning } from './tests/testUsePkpSessi
import { testUsePkpSessionSigsToPkpSign } from './tests/testUsePkpSessionSigsToPkpSign';
import { testUseValidLitActionCodeGeneratedSessionSigsToPkpSign } from './tests/testUseValidLitActionCodeGeneratedSessionSigsToPkpSign';
import { testUseValidLitActionCodeGeneratedSessionSigsToExecuteJsSigning } from './tests/testUseValidLitActionCodeGeneratedSessionSigsToExecuteJsSigning';
import { testUseValidLitActionIpfsCodeGeneratedSessionSigsToExecuteJsSigning } from './tests/testUseValidLitActionIpfsCodeGeneratedSessionSigsToExecuteJsSigning';
import { testUseEoaSessionSigsToExecuteJsSigningInParallel } from './tests/testUseEoaSessionSigsToExecuteJsSigningInParallel';
import { testDelegatingCapacityCreditsNFTToAnotherWalletToExecuteJs } from './tests/testDelegatingCapacityCreditsNFTToAnotherWalletToExecuteJs';
import { testDelegatingCapacityCreditsNFTToAnotherWalletToPkpSign } from './tests/testDelegatingCapacityCreditsNFTToAnotherWalletToPkpSign';
Expand Down Expand Up @@ -42,6 +43,7 @@ import { testUseValidLitActionCodeGeneratedSessionSigsToExecuteJsJsonResponse }
import { testUseValidLitActionCodeGeneratedSessionSigsToExecuteJsConsoleLog } from './tests/testUseValidLitActionCodeGeneratedSessionSigsToExecuteJsConsoleLog';
import { testUseValidLitActionCodeGeneratedSessionSigsToEncryptDecryptFile } from './tests/testUseValidLitActionCodeGeneratedSessionSigsToEncryptDecryptFile';
import { testUseValidLitActionCodeGeneratedSessionSigsToEncryptDecryptZip } from './tests/testUseValidLitActionCodeGeneratedSessionSigsToEncryptDecryptZip';
import { testUseValidLitActionIpfsCodeGeneratedSessionSigsToPkpSign } from './tests/testUseValidLitActionIpfsCodeGeneratedSessionSigsToPkpSign';

(async () => {
console.log('[𐬺🧪 Tinny𐬺] Running tests...');
Expand Down Expand Up @@ -89,6 +91,8 @@ import { testUseValidLitActionCodeGeneratedSessionSigsToEncryptDecryptZip } from
};

const litActionSessionSigsTests = {
testUseValidLitActionIpfsCodeGeneratedSessionSigsToPkpSign,
testUseValidLitActionIpfsCodeGeneratedSessionSigsToExecuteJsSigning,
testUseValidLitActionCodeGeneratedSessionSigsToExecuteJsSigning,
testUseValidLitActionCodeGeneratedSessionSigsToPkpSign,
testUseValidLitActionCodeGeneratedSessionSigsToExecuteJsSigningInParallel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';

/**
* Test Commands:
* ❌ NOT AVAILABLE IN CAYENNE
* ✅ NETWORK=cayenne yarn test:local --filter=testUseValidLitActionCodeGeneratedSessionSigsToExecuteJsSigning
* ❌ NOT AVAILABLE IN HABANERO
* ✅ NETWORK=localchain yarn test:local --filter=testUseValidLitActionCodeGeneratedSessionSigsToExecuteJsSigning
*/
export const testUseValidLitActionCodeGeneratedSessionSigsToExecuteJsSigning =
async (devEnv: TinnyEnvironment) => {
devEnv.setUnavailable(LIT_TESTNET.CAYENNE);
devEnv.setUnavailable(LIT_TESTNET.MANZANO);
// devEnv.setUnavailable(LIT_TESTNET.CAYENNE);
// devEnv.setUnavailable(LIT_TESTNET.MANZANO);

const alice = await devEnv.createRandomPerson();
const litActionSessionSigs = await getLitActionSessionSigs(devEnv, alice, [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { LitActionResource, LitPKPResource } from '@lit-protocol/auth-helpers';
import { log } from '@lit-protocol/misc';
import { LitAbility } from '@lit-protocol/types';
import { getLitActionSessionSigsUsingIpfsId } from 'local-tests/setup/session-sigs/get-lit-action-session-sigs';
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';

/**
* Test Commands:
* ✅ NETWORK=cayenne yarn test:local --filter=testUseValidLitActionIpfsCodeGeneratedSessionSigsToExecuteJsSigning
* ❌ NOT AVAILABLE IN HABANERO
* ✅ NETWORK=localchain yarn test:local --filter=testUseValidLitActionIpfsCodeGeneratedSessionSigsToExecuteJsSigning
*/
export const testUseValidLitActionIpfsCodeGeneratedSessionSigsToExecuteJsSigning =
async (devEnv: TinnyEnvironment) => {
const alice = await devEnv.createRandomPerson();
const litActionSessionSigs = await getLitActionSessionSigsUsingIpfsId(
devEnv,
alice,
[
{
resource: new LitPKPResource('*'),
ability: LitAbility.PKPSigning,
},
{
resource: new LitActionResource('*'),
ability: LitAbility.LitActionExecution,
},
]
);

const res = await devEnv.litNodeClient.executeJs({
sessionSigs: litActionSessionSigs,
code: `(async () => {
const sigShare = await LitActions.signEcdsa({
toSign: dataToSign,
publicKey,
sigName: "sig",
});
})();`,
jsParams: {
dataToSign: alice.loveLetter,
publicKey: alice.authMethodOwnedPkp.publicKey,
},
});

// -- Expected output:
// {
// claims: {},
// signatures: {
// sig: {
// r: "6d5ce6f948ff763939c204fc0f1b750fa0267ed567ed59581082d0cbf283feef",
// s: "4957ece75c60388500c4b7aa38a5fbafb7c20427db181aff7806af54c16ee145",
// recid: 1,
// signature: "0x6d5ce6f948ff763939c204fc0f1b750fa0267ed567ed59581082d0cbf283feef4957ece75c60388500c4b7aa38a5fbafb7c20427db181aff7806af54c16ee1451c",
// publicKey: "04D10D941B04491FDC99B048E2252A69137333254C482511D6CCDD401C080AF4F51BF65D9AE2413FCE066E326D7F0CED9C139DD9BA2D1C6334FD8C14CA4DD7F3D0",
// dataSigned: "7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4",
// },
// },
// decryptions: [],
// response: undefined,
// logs: "",
// }

// -- assertions
if (!res.signatures.sig.r) {
throw new Error(`Expected "r" in res.signatures.sig`);
}
if (!res.signatures.sig.s) {
throw new Error(`Expected "s" in res.signatures.sig`);
}

if (!res.signatures.sig.dataSigned) {
throw new Error(`Expected "dataSigned" in res.signatures.sig`);
}

if (!res.signatures.sig.publicKey) {
throw new Error(`Expected "publicKey" in res.signatures.sig`);
}

log('✅ res:', res);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { log } from '@lit-protocol/misc';
import { getLitActionSessionSigsUsingIpfsId } from 'local-tests/setup/session-sigs/get-lit-action-session-sigs';
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';

/**
* Test Commands:
* ✅ NETWORK=cayenne yarn test:local --filter=testUseValidLitActionIpfsCodeGeneratedSessionSigsToPkpSign
* ❌ NOT AVAILABLE IN HABANERO
* ✅ NETWORK=localchain yarn test:local --filter=testUseValidLitActionIpfsCodeGeneratedSessionSigsToPkpSign
*
**/
export const testUseValidLitActionIpfsCodeGeneratedSessionSigsToPkpSign =
async (devEnv: TinnyEnvironment) => {
const alice = await devEnv.createRandomPerson();
const litActionSessionSigs = await getLitActionSessionSigsUsingIpfsId(
devEnv,
alice
);

const res = await devEnv.litNodeClient.pkpSign({
toSign: alice.loveLetter,
pubKey: alice.authMethodOwnedPkp.publicKey,
sessionSigs: litActionSessionSigs,
});

// -- Expected output:
// {
// r: "ab2cef959db920d56f001c3b05637ee49af4c4441f2867ea067c413594a4c87b",
// s: "4bf11e17b4bb618aa6ed75cbf0406e6babfd953c5b201da697077c5fbf5b542e",
// recid: 1,
// signature: "0xab2cef959db920d56f001c3b05637ee49af4c4441f2867ea067c413594a4c87b4bf11e17b4bb618aa6ed75cbf0406e6babfd953c5b201da697077c5fbf5b542e1c",
// publicKey: "04400AD53C2F8BA11EBC69F05D1076D5BEE4EAE668CD66BABADE2E0770F756FDEEFC2C1D20F9A698EA3FEC6E9C944FF9FAFC2DC339B8E9392AFB9CC8AE75C5E5EC",
// dataSigned: "7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4",
// }

// -- assertions
// r, s, dataSigned, and public key should be present
if (!res.r) {
throw new Error(`Expected "r" in res`);
}
if (!res.s) {
throw new Error(`Expected "s" in res`);
}
if (!res.dataSigned) {
throw new Error(`Expected "dataSigned" in res`);
}
if (!res.publicKey) {
throw new Error(`Expected "publicKey" in res`);
}

// signature must start with 0x
if (!res.signature.startsWith('0x')) {
throw new Error(`Expected "signature" to start with 0x`);
}

// recid must be parseable as a number
if (isNaN(res.recid)) {
throw new Error(`Expected "recid" to be parseable as a number`);
}

log('✅ res:', res);
};
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,9 @@ export class LitNodeClientNodeJs
curveType: LIT_CURVE.BLS,

// -- custom auths
...(params?.litActionIpfsId && {
litActionIpfsId: params.litActionIpfsId,
}),
...(params?.litActionCode && { code: params.litActionCode }),
...(params?.jsParams && { jsParams: params.jsParams }),
...(this.currentEpochNumber && { epoch: this.currentEpochNumber }),
Expand Down
9 changes: 8 additions & 1 deletion packages/types/src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,12 @@ export interface JsonSignSessionKeyRequestV1 {
// authSig?: AuthSig;
siweMessage: string;
curveType: 'BLS';
epoch?: number;

// custom auth params
code?: string;
litActionIpfsId?: string;
jsParams?: any;
epoch?: number;
}

// [
Expand Down Expand Up @@ -1009,6 +1011,10 @@ export interface SignSessionKeyProp {
*/
resourceAbilityRequests?: LitResourceAbilityRequest[];

/**
* The js code on ipfs
*/
litActionIpfsId?: string;
/**
* The js code to run on the nodes
*/
Expand Down Expand Up @@ -1776,6 +1782,7 @@ export interface SignerLike {
export interface GetPkpSessionSigs extends GetSessionSigsProps {
pkpPublicKey: string;
authMethods: AuthMethod[];
litActionIpfsId?: string;
litActionCode?: string;
jsParams?: {
publicKey?: string;
Expand Down

0 comments on commit 45276bd

Please sign in to comment.