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

UnencryptedFileSystemKeyStore can't find Keypair #1451

Open
3 of 4 tasks
Morgandri1 opened this issue Jan 28, 2025 · 6 comments
Open
3 of 4 tasks

UnencryptedFileSystemKeyStore can't find Keypair #1451

Morgandri1 opened this issue Jan 28, 2025 · 6 comments
Labels
bug Something isn't working

Comments

@Morgandri1
Copy link

Prerequisites

  • I'm using the latest version of near-api-js.
  • I have tried to start with a fresh project and reproduce the defect with minimal code changes.
  • I have read the console error messages carefully (if applicable).

Description

When running some test code for my NEAR DEX app, I ran into some issues with signing and sending transactions with an UnencryptedFileSystemKeyStore object.

Reproducible demo

No response

Steps to reproduce

  1. Setup:
const keystore = new keyStores.UnencryptedFileSystemKeyStore(`${homedir()}/.near-credentials/`)
const nearTest = await connect({
    networkId: 'testnet',
    keyStore: keystore,
    nodeUrl: 'https://rpc.testnet.near.org',
    helperUrl: 'https://helper.nearprotocol.com'
  })
const account = await nearTest.account(TEST_ACCOUNT_ID);
let res = await account.signAndSendTransaction({...});
  1. run near create TEST_ACCOUNT_ID --useFaucet
  2. verify key exists manually
  3. run code

Expected behavior

transaction should execute and res should resolve to a transaction result

Actual behavior

instead, Near spits out this error:

/Users/metatron/Documents/degendevs/node_modules/@near-js/accounts/lib/commonjs/account.cjs:50
            throw new types_1.TypedError(`Can not sign transactions for account ${this.accountId} on network ${this.connection.networkId}, no matching key pair exists for this account`, 'KeyNotFound');
                  ^

TypedError: Can not sign transactions for account bta-251494586 on network testnet, no matching key pair exists for this account
    at Account.signTransaction (/Users/metatron/Documents/degendevs/node_modules/@near-js/accounts/lib/commonjs/account.cjs:50:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /Users/metatron/Documents/degendevs/node_modules/@near-js/accounts/lib/commonjs/account.cjs:72:34
    at async exponentialBackoff (/Users/metatron/Documents/degendevs/node_modules/@near-js/providers/lib/commonjs/exponential-backoff.cjs:8:24)
    at async Account.signAndSendTransaction (/Users/metatron/Documents/degendevs/node_modules/@near-js/accounts/lib/commonjs/account.cjs:71:24)
    at async /Users/metatron/Documents/degendevs/index.js:92:16
    at async Promise.all (index 1)
    at async Object.swapToken (/Users/metatron/Documents/degendevs/index.js:91:15)
    at async main (/Users/metatron/Documents/degendevs/test.js:61:18) {
  type: 'KeyNotFound',
  context: undefined
}

again, i have verified the key file exists at $HOME/.near-credentials/testnet/bta-251494586.json and is valid.

Your environment

  • NEAR JavaScript API version used: 5.0.1
  • Frontend framework (if applicable): N/A
  • Relevant dependencies (if applicable): N/A

Self-service

  • I'd be willing to fix this bug myself.
@Morgandri1 Morgandri1 added the bug Something isn't working label Jan 28, 2025
@github-project-automation github-project-automation bot moved this to NEW❗ in DevTools Jan 28, 2025
@Morgandri1
Copy link
Author

Dug in a little and found here that the access key query is most likely the point of failure, as I got to my current code after the provided DEX SDK code ran into access key query faults. Considering the error is also coming from an issue thought to be closed in #618.

@Morgandri1
Copy link
Author

i have verified the key exists, here, so i'm not really sure what to do.

@AlexKushnir1
Copy link
Contributor

Just tested sending transaction in your way and didn"t catch any problems. My code looks like this:

test('testUnencryptedFileSystemKeyStore', async () => {
    const keyStore = new keyStores.UnencryptedFileSystemKeyStore(`${homedir()}/.near-credentials/`);
    console.info(keyStore);
    const nearTest = await connect(<ConnectConfig>{
        networkId: 'testnet',
        keyStore: keyStore,
        nodeUrl: 'https://rpc.testnet.near.org',
        helperUrl: 'https://helper.nearprotocol.com'
    })
    console.info(nearTest);
    const account = await nearTest.account('true-pull.testnet');
    console.info(account);
    let resp = await account.signAndSendTransaction({
        receiverId: 'alexkushnir.testnet',
        actions: [functionCall("sign",
            Buffer.from(JSON.stringify(args),
            BigInt(300000000000000),
            BigInt(1))]
    });
    console.info(resp);
}, 30000);

I would delve into this, if you see any differences between our steps please contact me. My advise is to double check the existing json, pass the logs to the code

@Morgandri1
Copy link
Author

@AlexKushnir1 What should the key file look like? I've verified its existence, how can I verify its validity? I created the account using near cli so I don't know if that has something to do with it.

@Morgandri1
Copy link
Author

just for sanity checks, I verified the accounts existence in accounts.json, and verified it can be used as a signer.

I also verified the keys contents manually, again.

{
"account_id":"bta-251494586",
"public_key":"ed25519:4uvK9gBfTqhf2GUNMhttz6JfFgNctz1zk7gU7t5n4PFq",
"private_key":"ed25519:2jm...eaf"
}

@Morgandri1
Copy link
Author

I was able to get this working by loading the key directly from the keystore, then defining a new, memory based keystore and loading the extracted key into it like such:

const filekeystore = new keyStores.UnencryptedFileSystemKeyStore(`${homedir()}/.near-credentials/`)
let key = await filekeystore.getKey("testnet", "bta-251494586")
console.log("getKey", !!key ? "pass" : "fail")
let keystore = new keyStores.InMemoryKeyStore()
keystore.setKey("testnet", "bta-251494586.testnet", key)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: NEW❗
Development

No branches or pull requests

2 participants