Skip to content

Commit

Permalink
Merge branch #61 test/execute-js
Browse files Browse the repository at this point in the history
  • Loading branch information
Ansonhkg committed Mar 28, 2023
2 parents 18ec545 + f2b073e commit bb7afc1
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 161 deletions.
1 change: 1 addition & 0 deletions packages/lit-node-client/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export default {
'node_modules/ipfs-unixfs-importer/dist/index.min.js',
'^blockstore-core': 'node_modules/blockstore-core/dist/index.min.js',
},
setupFilesAfterEnv: ['../../jest.setup.js'],
};
16 changes: 0 additions & 16 deletions packages/lit-node-client/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,6 @@
"jestConfig": "packages/lit-node-client/jest.config.ts",
"passWithNoTests": true
}
},
"testWatch": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/packages/lit-node-client"],
"options": {
"jestConfig": "packages/lit-node-client/jest.config.ts",
"passWithNoTests": true,
"watch": true
}
},
"yalcPublish": {
"executor": "nx:run-commands",
"options": {
"commands": ["yalc publish --push"],
"parallel": false
}
}
},
"tags": []
Expand Down
212 changes: 68 additions & 144 deletions packages/lit-node-client/src/lib/lit-node-client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,167 +1,91 @@
// @ts-nocheck
import { TextEncoder, TextDecoder } from 'util';
global.TextEncoder = TextEncoder;
// @ts-ignore
global.TextDecoder = TextDecoder;
global.jestTesting = true;
import fetch from 'node-fetch';
import { LitNodeClient } from './lit-node-client';
import { hashResourceIdForSigning } from '@lit-protocol/access-control-conditions';

globalThis.fetch = fetch;
import { nacl } from '@lit-protocol/nacl';
globalThis.nacl = nacl;

import crypto, { createHash } from 'crypto';
import { getSessionKeyUri } from '@lit-protocol/auth-browser';
Object.defineProperty(global.self, 'crypto', {
value: {
getRandomValues: (arr: any) => crypto.randomBytes(arr.length),
subtle: {
digest: (algorithm: string, data: Uint8Array) => {
return new Promise((resolve, reject) =>
resolve(
createHash(algorithm.toLowerCase().replace('-', ''))
.update(data)
.digest()
)
);
},
},
const CONFIG = {
MNEUMONIC:
'island arrow object divide umbrella snap essay seminar top develop oyster success',
RPC_ENDPOINT: 'https://cosmos-mainnet-rpc.allthatnode.com:26657',
RECIPIENT: 'cosmos1jyz3m6gxuwceq63e44fqpgyw2504ux85ta8vma',
DENOM: 'uatom',
AMOUNT: 1, // CHANGE THIS TO 1 IF YOU WANT TO SEND 1 ATOM
DEFAULT_GAS: 0.025,
CONTROLLER_AUTHSIG: {
sig: '0x7dd45119cb1fde7fb0b891d3de7262e5d564e64ec46b873163a373a4645626a328bceb089209ac68bdd429185a30f32e7bbf2eb71820c94c5f0547716526884c1c',
derivedVia: 'web3.eth.personal.sign',
signedMessage:
'localhost:1209 wants you to sign in with your Ethereum account:\n0x18f987D15a973776f6a60652B838688a1833fE95\n\n\nURI: http://localhost:1209/auth\nVersion: 1\nChain ID: 1\nNonce: 1IbQokxUmJZ1opsTY\nIssued At: 2023-03-20T15:40:48.024Z\nExpiration Time: 2023-03-27T15:40:48.010Z',
address: '0x18f987d15a973776f6a60652b838688a1833fe95',
},
});

const isClass = (v) => {
return typeof v === 'function' && /^\s*class\s+/.test(v.toString());
PKP_PUBKEY:
'0x04cd5fc4b661a2ae2dc425aa42abbfeaa187c07063928322a8c748ebb7611868144c0ff28b1910faeafedea914ec8a23baa579b6ff7f03efa322e7eb098e62dd8f',
};
let client: LitNodeClient;

describe('litNodeClient', () => {
// --global
let litNodeClient;

// -- start
it('imported { LitNodeClient } is a class', async () => {
expect(isClass(LitNodeClient)).toBe(true);
});

it('should be able to instantiate a new LitNodeClient', async () => {
const litNodeClient = new LitNodeClient();
expect(litNodeClient).toBeDefined();
});

it('should be able to instantiate a new LitNodeClient to serrano', async () => {
const litNodeClient = new LitNodeClient({
it('should create an instance', async () => {
client = new LitNodeClient({
litNetwork: 'serrano',
debug: false,
});
await litNodeClient.connect();
expect(litNodeClient.config.litNetwork).toBe('serrano');
});

it('should be able to instantiate a new LitNodeClient to jalapeno', async () => {
const litNodeClient = new LitNodeClient({
litNetwork: 'jalapeno',
});
await litNodeClient.connect();
expect(litNodeClient.config.litNetwork).toBe('jalapeno');
});
await client.connect();

it('should be able to instantiate a new LitNodeClient to localhost', async () => {
const litNodeClient = new LitNodeClient({
litNetwork: 'localhost',
});
expect(litNodeClient).toBeDefined();
expect(client.ready).toBe(true);
});
});

it('should connect to lit nodes', async () => {
litNodeClient = new LitNodeClient({
litNetwork: 'serrano',
describe('Lit Actions', () => {
it('lit action log should return hello world', async () => {
const res = await client.executeJs({
authSig: CONFIG.CONTROLLER_AUTHSIG,
code: `(async () => {
console.log('hello world')
})();`,
jsParams: {
publicKey: CONFIG.PKP_PUBKEY,
},
});

await litNodeClient.connect();

expect(litNodeClient).toBeDefined();
});

it('hashes a resource id', async () => {
const path = '/bglyaysu8rvblxlk7x0ksn';

let resourceId = {
baseUrl: 'my-dynamic-content-server.com',
path,
orgId: '',
role: '',
extraData: '',
};

let hashedResourceId = await hashResourceIdForSigning(resourceId);

expect(hashedResourceId).toBe(
'd3b7c933579ff8cce79a9db8f135cf93d8e4b1d206129cbe28405ed81dad7cb1'
);
expect(res.logs).toContain('hello world');
});

// it('gets session key', () => {
// let sessionKey = litNodeClient.getSessionKey();

// // sessionKey has 'publicKey' property
// expect(sessionKey).toHaveProperty('publicKey');
// });

// it('gets sessionKeyUri', () => {
// let sessionKey = litNodeClient.getSessionKey();
// sessionKey.publicKey =
// '41cf12fde8e8dd8ba7e1f9f443bbf8a60be6aae4771109083f0378511f9ca599';
// let sessionKeyUri = getSessionKeyUri({ publicKey: sessionKey.publicKey });

// expect(sessionKeyUri).toBe(
// 'lit:session:41cf12fde8e8dd8ba7e1f9f443bbf8a60be6aae4771109083f0378511f9ca599'
// );
// });

it('gets capabilities', async () => {
const path = '/bglyaysu8rvblxlk7x0ksn';

let resourceId = {
baseUrl: 'my-dynamic-content-server.com',
path,
orgId: '',
role: '',
extraData: '',
};

let hashedResourceId = await hashResourceIdForSigning(resourceId);

let resources = [`litSigningCondition://${hashedResourceId}`];
it('lit action response should return json {hello: "world"}', async () => {
const res = await client.executeJs({
authSig: CONFIG.CONTROLLER_AUTHSIG,
code: `(async () => {
LitActions.setResponse({
response: JSON.stringify({hello: 'world'})
});
})();`,
jsParams: {
publicKey: CONFIG.PKP_PUBKEY,
},
});

let capabilities = litNodeClient.getSessionCapabilities([], resources);
expect(capabilities[0]).toBe('litSigningConditionCapability://*');
expect(res.response).toEqual({ hello: 'world' });
});

it('gets expiration', () => {
const expiration = litNodeClient.getExpiration();
it('lit action should sign a message', async () => {
const res = await client.executeJs({
authSig: CONFIG.CONTROLLER_AUTHSIG,
code: `(async () => {
const sigShare = await LitActions.signEcdsa({
toSign,
publicKey,
sigName
});
})();`,
jsParams: {
// hello world in Uint8Array
toSign: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100],
publicKey: CONFIG.PKP_PUBKEY,
sigName: 'hello-world-sig',
},
});

// expect expiration to contains 'T'
expect(expiration).toContain('T');
expect(res.signatures['hello-world-sig']).toBeDefined();
expect(res.signatures['hello-world-sig'].publicKey).toEqual(
'04cd5fc4b661a2ae2dc425aa42abbfeaa187c07063928322a8c748ebb7611868144c0ff28b1910faeafedea914ec8a23baa579b6ff7f03efa322e7eb098e62dd8f'
);
});

// it('hashes a resource id', async () => {
// const path = '/bglyaysu8rvblxlk7x0ksn';

// let resourceId = {
// baseUrl: 'my-dynamic-content-server.com',
// path,
// orgId: '',
// role: '',
// extraData: '',
// };

// let hashedResourceId = await hashResourceIdForSigning(resourceId);

// let sessionSigs = await litNodeClient.getSessionSigs({
// chain: 'ethereum',
// resources: [`litSigningCondition://${hashedResourceId}`],
// });

// expect(sessionSigs).toBe(1);
// });
});
3 changes: 2 additions & 1 deletion packages/lit-node-client/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
"types": ["jest", "node"],
"allowJs": true
},
"include": ["jest.config.ts", "**/*.test.ts", "**/*.spec.ts", "**/*.d.ts", "../auth-browser/src/lib/chains/lit-connect-modal.d.ts"]
}

0 comments on commit bb7afc1

Please sign in to comment.