near / near-workspaces-js

Write tests once, run them both on NEAR TestNet and a controlled NEAR Sandbox local environment
https://near.github.io/near-workspaces-js/
GNU General Public License v3.0
42 stars 21 forks source link

Unable to sign transactions with InMemoryKeyStore - Class PublicKey is missing in schema #103

Open adsick opened 2 years ago

adsick commented 2 years ago

Hi, I've been trying to test near-api-js/examples/cookbook/* scripts for quite a while and now I'm stuck on a problem that looks like a bug to me.

Note, it might not be the workspaces-js's problem but it appears in the context of testing so I report here but not in near-api-js.

Here is the recipe script I am testing, it is slightly modified because we want to test it: https://github.com/adsick/near-api-js/blob/36afea8c3bcc44066c711dfeb44e76fbb12aa64f/examples/cookbook/utils/calculate-gas.js

And here is the test code: https://github.com/adsick/near-api-js/blob/36afea8c3bcc44066c711dfeb44e76fbb12aa64f/examples/cookbook/tests/calculate-gas.ava.ts

stuff with config is mostly needed to get the local node's url address (b.t.w. it is not convenient to access it that way, I guess there should be a method for that)

The Important thing is that we create a new InMemoryKeyStore on the [line 22](https://github.com/adsick/near-api-js/blob/36afea8c3bcc44066c711dfeb44e76fbb12aa64f/examples/cookbook/tests/calculate-gas.ava.ts#L22) and add alice's key to that keystore we've just created. Then we pass the keystore to the config, setup a few things and finally calling the exported calculateGas function, logging the result.

But the problem is that

 // this will fail with some weird borsh error
 // ...Class PublicKey is missing in schema: publicKey
 const result = await account.functionCall(

 { contractId, methodName, args, gas: exports.MAX_GAS, attachedDeposit: utils.format.parseNearAmount(depositAmount), } 

);

this code can't sign transactions for some reason, the error is:

 Rejected promise returned by test. Reason:

 Error (BorshError) 

 { fieldPath: [ 'publicKey', ], originalMessage: 'Class PublicKey is missing in schema', message: 'Class PublicKey is missing in schema: publicKey', } 

To reproduce this on your system you can clone my fork, cd examples/cookbook, comment the line 27 of utils/calculate-gas.js and then run yarn test -m "calculate gas"

mikedotexe commented 2 years ago

Thank you for the clear ticket, will look into this

mikedotexe commented 2 years ago

Hey @adsick I think you're on the right track. I think you ran into an issue where we're trying to load keyStores from different places, and the way we build some of our libraries has trouble with that.

This is not the final solution, but I think you can get past this hurdle by adding this to the bottom of calculate-gas.js:

exports.calculateGas = calculateGas;
exports.keyStores = keyStores;

and changing a line in the calculate-gas-.ava.ts to be:

- const { calculateGas } = module
+ const { calculateGas, keyStores } = module 

and removing the import of keyStores at the top of that test file. That will get you farther and onto the next problem 👿