question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement UnencryptedJsonKeystore & UnencryptedJsonFileKeystore

See original GitHub issue

Describe the bug This is a bug that reveals that for the default UnencryptedFileSystemKeystore the KeyDir assumes a specific structure for the reading of the keys. This structure is out of alignment with any default construction as of the latest near-cli 1.6.0 The key paths by default are ~/.near-credentials/default/nameofaccount.json

The issue is the assumptions made here: https://github.com/near/near-api-js/blob/master/src/key_stores/unencrypted_file_system_keystore.ts#L114

To Reproduce npm install near-api-js

const nearAPI = require('near-api-js')
const 

{ keyStores } 

= nearAPI;
const KEY_PATH = "~/.near-credentials/default/" 
const keyStore = new keyStores.UnencryptedFileSystemKeyStore(KEY_PATH);
const result = await keyStore.getKey("yournetwork","youracccountid")

copy pasta into an index.js

Expected behavior You expect to receive your key instead you’ll throw a can’t find key error

Desktop (please complete the following information):

  • OS: Mac
  • Version 10.15.7

Additional context Should be a simple fix, the network order assumption should perhaps be removed, or the cli should put the keys in the proper paths, alternatively the error message should throw a put structure keys in directory as follows.

I’m most inclined for removing the path assumption from the Keystore.

What do you all think?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
zcstarrcommented, Apr 29, 2021

I like this idea, I think it’ll make the keystore a little more flexible. How do you feel about Json with Json Schema?

My thought here is that if we just spec out a schema see below. Then we can do cool things like add type ahead support in all the environments for configuring the keystore. Like the monaco editor allows you to add support for json schema in configuration files.

This would mean that in online editing environments you can add typeahead and light error correcting. Additionally if we use json we can support in emory representations of the keystore that have a similar mapping if not 1-1 corresponding mapping to the JSON config representation of the keystore.

The interface would be:

UnencryptedJsonKeystore(keyConfig: jsonKeyStoreConfig) 
UnencryptedJsonFileKeystore(path: string)

UnencryptedJsonFileKeystore just uses JsonKeystore under the hood, it’s a convenience wrapper that reads files from disk.

here’s a spec via gist and inline here so it’s maybe easier to read it’s verbose 😁

{
    "$id": "near-keystore-config-schema.json",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "description": "Near JSON Keystore Configuration",
    "type": "object",
    "$ref": "#/definitions/jsonKeystoreConfig",
    "definitions": {
        "jsonKeystoreConfig": {
            "title": "jsonKeystoreConfig",
            "type": "object",
            "properties": {
                "mainnet": {
                    "$ref": "#/definitions/keyConfigs"
                },
                "testnet": {
                    "$ref": "#/definitions/keyConfigs"
                }
            },
            "additionalProperties": {
                "$ref": "#/definitions/keyConfigs"
            },
            "$comment": "keys are assumed to be the networkId"
        },
        "keyConfigs": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/keyConfig"
            }
        },
        "keyConfig" : {
            "title": "keyConfig",
            "type": "object",
            "properties": {
                "account_id": {
                    "$ref": "#/definitions/account_id"
                },
                "public_key": {
                    "$ref": "#/definitions/public_key"
                },
                "private_key": {
                    "$ref": "#/definitions/private_key"
                }
            }
        },
        "account_id": {
            "title": "account_id",
            "type": "string",
            "pattern": "^(([a-z0-9]+[\\-_])*[a-z0-9]+\\.)*([a-z0-9]+[\\-_])*[a-z0-9]+$",
            "minLength": 2,
            "maxLength": 64
        },
        "public_key": {
            "title": "public_key",
            "type": "string",
            "pattern": "^ed25519:[a-zA-Z0-9]+$"
        },
        "private_key": {
            "title": "private_key",
            "type": "string",
            "pattern": "^ed25519:[a-zA-Z0-9]+$"
        }
    }
}

example

{
    "mainnet": [
        {
            "account_id": "foobar.near",
            "private_key": "ed25519:asdfasdfXAwfeafafwaef",
            "public_key": "ed25519:asdfasdfXAwfeafafwaefbobbyB33FXawfaw33faf3f3a3"
        },
        {
            "account_id": "baz.near",
            "private_key": "ed25519:KxTsdfasdfXAwfeafafwaef",
            "public_key": "ed25519:ZsdfasdfXAwfeafafwaefbobbyB33FXawfaw33faf3f3a3"
        }
    ],
    "testnet": [
        {
            "account_id": "foobar.testnet",
            "private_key": "ed25519:asdfasdfXAwfeafafwaef",
            "public_key": "ed25519:asdfasdfXAwfeafafwaefbobbyB33FXawfaw33faf3f3a3"
        }
    ],
    "customlocal": [
        {
            "account_id": "foobar.customlocal",
            "private_key": "ed25519:asdfasdfXAwfeafafwaef",
            "public_key": "ed25519:asdfasdfXAwfeafafwaefbobbyB33FXawfaw33faf3f3a3"
        }
    ]
}
0reactions
mehtaphysicalcommented, May 2, 2021

This looks good to me!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues - GitHub
CLI is not able to use .near-credentials/testnet in case of absence ... Implement UnencryptedJsonKeystore & UnencryptedJsonFileKeystore ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found