Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/executeJS array normalizing #249

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions e2e-nodejs/group-lit-actions/test-lit-action-1-sig.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export async function main() {
authSig: LITCONFIG.CONTROLLER_AUTHSIG,
code: `(async () => {
const sigShare = await LitActions.signEcdsa({
toSign,
toSign: dataToSign,
publicKey,
sigName: "sig",
});
})();`,
authMethods: [],
jsParams: {
toSign: TO_SIGN,
dataToSign: TO_SIGN,
publicKey: LITCONFIG.PKP_PUBKEY,
},
});
Expand Down
4 changes: 2 additions & 2 deletions e2e-nodejs/group-lit-actions/test-lit-action-2-sigs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ export async function main() {
return sigShares;
}

const sigShares = await signMultipleSigs(numberOfSigs, toSign, publicKey);
const sigShares = await signMultipleSigs(numberOfSigs, signatureData, publicKey);

})();`,
authMethods: [],
jsParams: {
numberOfSigs: 2,
toSign: TO_SIGN,
signatureData: TO_SIGN,
publicKey: LITCONFIG.PKP_PUBKEY,
sigName: 'fooSig',
},
Expand Down
42 changes: 42 additions & 0 deletions e2e-nodejs/group-lit-actions/test-lit-action-no-params.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import path from 'path';
import { success, fail, testThis } from '../../tools/scripts/utils.mjs';
import LITCONFIG from '../../lit.config.json' assert { type: 'json' };
import { client } from '../00-setup.mjs';
import { ethers } from 'ethers';

// NOTE: you need to hash data before you send it in.
// If you send something that isn't 32 bytes, the nodes will return an error.
const TO_SIGN = ethers.utils.arrayify(ethers.utils.keccak256([1, 2, 3, 4, 5]));

export async function main() {
// ==================== Test Logic ====================
const res = await client.executeJs({
authSig: LITCONFIG.CONTROLLER_AUTHSIG,
code: `(async () => {
LitActions.setResponse({
response: JSON.stringify({success: true})
});
})();`,
authMethods: [],
});

// ==================== Post-Validation ====================
let jsonRes;

try {
jsonRes = JSON.parse(res.response);
} catch (e) {
return fail(`response should be a valid JSON`);
}

if (jsonRes.success !== true) {
return fail(
`jsonRes.success should be true but received "${jsonRes.success}"`
);
}

// ==================== Success ====================
return success('Lit action should return response with no params given');
}

await testThis({ name: path.basename(import.meta.url), fn: main });
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,67 @@ describe('LitNodeClientNodeJs', () => {

expect(expiration).toContain('T');
});

describe('normalizeParams', () => {
it('should normalise params', () => {
// Setup
const buffer = new ArrayBuffer(2);
const view = new Uint8Array(buffer);
view[0] = 1;
view[1] = 2;
let params = { jsParams: { bufferArray: view } };

// Action
params = LitNodeClientNodeJs.normalizeParams(params);

// Assert
expect(params.jsParams.bufferArray).toEqual([1, 2]);
})

it('should leave normal arrays unchanged', () => {
// Setup
let params = { jsParams: { normalArray: [1, 2, 3] } };

// Action
params = LitNodeClientNodeJs.normalizeParams(params);

// Assert
expect(params.jsParams.normalArray).toEqual([1, 2, 3]);
});

it('should ignore non-array and non-ArrayBuffer properties', () => {
// Setup
let params = { jsParams: { number: 123, string: 'test' } };

// Action
params = LitNodeClientNodeJs.normalizeParams(params);

// Assert
expect(params.jsParams.number).toEqual(123);
expect(params.jsParams.string).toEqual('test');
});

it('should handle multiple properties', () => {
// Setup
const buffer = new ArrayBuffer(2);
const view = new Uint8Array(buffer);
view[0] = 1;
view[1] = 2;
let params = {
jsParams: {
bufferArray: view,
normalArray: [3, 4, 5]
}
};

// Action
params = LitNodeClientNodeJs.normalizeParams(params);

// Assert
expect(params.jsParams.bufferArray).toEqual([1, 2]);
expect(params.jsParams.normalArray).toEqual([3, 4, 5]);
});

});

});
35 changes: 24 additions & 11 deletions packages/lit-node-client-nodejs/src/lib/lit-node-client-nodejs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,25 @@ export class LitNodeClientNodeJs extends LitCore {

// ========== Scoped Business Logics ==========

// Normalize the data to a basic array
public static normalizeParams(params: ExecuteJsProps): ExecuteJsProps {
if (!params.jsParams) {
params.jsParams = {};
return params;
}

for (const key of Object.keys(params.jsParams)) {
if (Array.isArray(params.jsParams[key]) || ArrayBuffer.isView(params.jsParams[key])) {
let arr = [];
for (let i = 0; i < params.jsParams[key].length; i++) {
arr.push((params.jsParams[key] as Buffer)[i]);
}
params.jsParams[key] = arr;
}
}
return params;
}

/**
*
* Execute JS on the nodes and combine and return any resulting signatures
Expand Down Expand Up @@ -1333,15 +1352,9 @@ export class LitNodeClientNodeJs extends LitCore {
});
}

// the nodes will only accept a normal array type as a paramater due to serizalization issues with ArrayBuffer type.
// this loop below is to normalize the data to a basic array.
if (jsParams.toSign) {
let arr = [];
for (let i = 0; i < jsParams.toSign.length; i++) {
arr.push((jsParams.toSign as Buffer)[i]);
}
jsParams.toSign = arr;
}

// Call the normalizeParams function to normalize the parameters
params = LitNodeClientNodeJs.normalizeParams(params);

let res;
// -- only run on a single node
Expand Down Expand Up @@ -2309,8 +2322,8 @@ export class LitNodeClientNodeJs extends LitCore {
const sessionCapabilityObject = params.sessionCapabilityObject
? params.sessionCapabilityObject
: this.generateSessionCapabilityObjectWithWildcards(
params.resourceAbilityRequests.map((r) => r.resource)
);
params.resourceAbilityRequests.map((r) => r.resource)
);
let expiration = params.expiration || LitNodeClientNodeJs.getExpiration();

// -- (TRY) to get the wallet signature
Expand Down
Loading