Reputation: 387
I'm trying to write an integration test to call a method on a Near Protocol contract.
I've got as far as creating the account object, and now need to set the key pair to be able to make the call.
I can see that I can get the private key value by logging in on the near-cli and getting the key from the .nearcredentials folder. But it seems like I need to encode this before setting the key in the store.
const keyStore = new keyStores.InMemoryKeyStore();
const config = {
keyStore,
networkId: "testnet",
nodeUrl: "https://rpc.testnet.near.org",
};
const near = await connect(config);
const account = await near.account("my_account.testnet");
const keyPair = ???
await keyStore.setKey(config.networkId, "my_account.testnet", keyPair);
const result = await account.functionCall({
contractId: "nft-example.my_account.testnet",
methodName: "nft_metadata"
})
Upvotes: 1
Views: 398
Reputation: 387
I've figured out how to generate the KeyPair from the private key, full code example is here, but it's basically the first line of the example using the near utils function
const { connect, keyStores, utils, Contract } = require("near-api-js");
const keyPair = new utils.key_pair.KeyPairEd25519(process.env.TEST_ACCOUNT_PRIVATE_KEY);
const keyStore = new keyStores.InMemoryKeyStore();
await keyStore.setKey(networkId, "my_account.testnet", keyPair);
const near = await connect({
keyStore,
networkId,
nodeUrl: "https://rpc.testnet.near.org",
});
const account = await near.account("my_account.testnet");
const contract = new Contract(
account,
'nft-example.my_account.testnet',
{
viewMethods: ['nft_metadata'],
changeMethods: []
}
);
const result = await contract.nft_metadata();
expect(result.spec).toBe("nft-1.0.0");
Upvotes: 1