Browse Source

storage-node-v2: Add account URI flag.

- add volta config
- change some flags
Shamil Gadelshin 3 years ago
parent
commit
f64777262d

+ 5 - 0
storage-node-v2/package.json

@@ -28,6 +28,7 @@
     "express-openapi-validator": "^4.12.4",
     "express-winston": "^4.1.0",
     "file-type": "^16.5.0",
+    "lodash": "^4.17.21",
     "multihashes": "^4.0.2",
     "node-cache": "^5.1.2",
     "openapi-editor": "^0.3.0",
@@ -63,6 +64,10 @@
   "engines": {
     "node": ">=8.0.0"
   },
+  "volta": {
+    "node": "12.18.2",
+    "yarn": "1.22.4"
+  },
   "files": [
     "/bin",
     "/lib",

+ 22 - 15
storage-node-v2/src/command-base/ApiCommandBase.ts

@@ -1,12 +1,13 @@
 import { Command, flags } from '@oclif/command'
 import { createApi } from '../services/runtime/api'
-import { getAccountFromJsonFile, getAlicePair } from '../services/runtime/accounts'
+import { getAccountFromJsonFile, getAlicePair, getAccountFromUri } from '../services/runtime/accounts'
 import { KeyringPair } from '@polkadot/keyring/types'
 import { ApiPromise } from '@polkadot/api'
 import logger from '../services/logger'
 import ExitCodes from './ExitCodes'
 import { CLIError } from '@oclif/errors'
 import { Input } from '@oclif/parser'
+import _ from 'lodash'
 
 /**
  * Parent class for all runtime-based commands. Defines common functions.
@@ -19,7 +20,8 @@ export default abstract class ApiCommandBase extends Command {
     dev: flags.boolean({ char: 'm', description: 'Use development mode' }),
     apiUrl: flags.string({
       char: 'u',
-      description: 'Runtime API URL. Mandatory in non-dev environment. Default is ws://localhost:9944',
+      description: 'Runtime API URL. Mandatory in non-dev environment.',
+      default: 'ws://localhost:9944',
     }),
     keyfile: flags.string({
       char: 'k',
@@ -29,6 +31,10 @@ export default abstract class ApiCommandBase extends Command {
       char: 'p',
       description: 'Key file password (optional).',
     }),
+    accountURI: flags.string({
+      char: 'y',
+      description: 'Account URI (optional). Overrides keyfile and password flags.',
+    }),
   }
 
   /**
@@ -65,9 +71,9 @@ export default abstract class ApiCommandBase extends Command {
     /* eslint-disable @typescript-eslint/no-explicit-any */
     const { flags } = this.parse(<Input<any>>this.constructor)
 
-    const apiUrl = flags.apiUrl ?? 'ws://localhost:9944'
+    this.api = await createApi(flags.apiUrl)
 
-    this.api = await createApi(apiUrl)
+    logger.info(`Initialized runtime connection: ${flags.apiUrl}`)
 
     await this.getApi()
   }
@@ -97,25 +103,26 @@ export default abstract class ApiCommandBase extends Command {
    * @param dev - indicates the development mode (optional).
    * @param keyfile - key file path (optional).
    * @param password - password for the key file (optional).
+   * @param accountURI - accountURI (optional). Overrides keyfile and password flags.
    * @returns KeyringPair instance.
    */
-  getAccount(flags: { dev?: boolean; keyfile?: string; password?: string }): KeyringPair {
+  getAccount(flags: { dev?: boolean; keyfile?: string; password?: string; accountURI?: string }): KeyringPair {
     const keyfile = flags.keyfile ?? ''
     const password = flags.password
+    const accountURI = flags.accountURI
 
-    let account: KeyringPair
     if (flags.dev) {
-      account = getAlicePair()
-    } else {
-      if (keyfile === '') {
-        this.error('Keyfile must be set.')
-      }
-
-      account = getAccountFromJsonFile(keyfile)
+      return getAlicePair()
+    } else if (!_.isEmpty(accountURI) && accountURI !== undefined) {
+      return getAccountFromUri(accountURI)
+    } else if (!_.isEmpty(keyfile)) {
+      const account = getAccountFromJsonFile(keyfile)
       account.unlock(password)
-    }
 
-    return account
+      return account
+    } else {
+      this.error('Keyfile or account URI must be set.')
+    }
   }
 
   /**

+ 22 - 2
storage-node-v2/src/services/runtime/accounts.ts

@@ -35,7 +35,7 @@ export function getAccountFromJsonFile(jsonBackupFilePath: string): KeyringPair
     })
   }
 
-  const keyring = new Keyring()
+  const keyring = configureKeyring()
   let account: KeyringPair
   try {
     // Try adding and retrieving the keys in order to validate that the backup file is correct
@@ -59,6 +59,26 @@ export function getAccountFromJsonFile(jsonBackupFilePath: string): KeyringPair
  * @returns 'Alice' KeyPair instance.
  */
 export function getAlicePair(): KeyringPair {
-  const keyring = new Keyring({ type: 'sr25519' })
+  const keyring = configureKeyring()
   return keyring.addFromUri('//Alice')
 }
+
+/**
+ * Create KeyPair instance from the account URI.
+ *
+ * @param accountURI - account URI (//Alice)
+ * @returns KeyPair instance.
+ */
+export function getAccountFromUri(accountURI: string): KeyringPair {
+  const keyring = configureKeyring()
+  return keyring.addFromUri(accountURI)
+}
+
+/**
+ * Configures the Keyring with the proper account type.
+ *
+ * @returns configured Keyring.
+ */
+function configureKeyring(): Keyring {
+  return new Keyring({ type: 'sr25519' })
+}

+ 19 - 6
yarn.lock

@@ -9421,7 +9421,7 @@ cli-cursor@^3.1.0:
   dependencies:
     restore-cursor "^3.1.0"
 
-cli-highlight@^2.1.10:
+cli-highlight@^2.1.11:
   version "2.1.11"
   resolved "https://registry.yarnpkg.com/cli-highlight/-/cli-highlight-2.1.11.tgz#49736fa452f0aaf4fae580e30acb26828d2dc1bf"
   integrity sha512-9KDcoEVwyUXrjcJNvHD0NFc/hiwe/WPVYIleQh2O1N2Zro5gWJZ/K+3DGn8w8P/F6FxOgzyC5bxDyHIgCSPhGg==
@@ -28220,15 +28220,15 @@ typeorm-typedi-extensions@^0.2.3:
   integrity sha512-T9i1NvRZNjPn9Jb8oT772ihfn6PwdqDVpzPCtKSqjkZGOgXrCkdyD3dDrzfMaoWJ1afU58bVx2CMb95FzT42Ow==
 
 typeorm@^0.2.25, typeorm@^0.2.31:
-  version "0.2.34"
-  resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.34.tgz#637b3cec2de54ee7f423012b813a2022c0aacc8b"
-  integrity sha512-FZAeEGGdSGq7uTH3FWRQq67JjKu0mgANsSZ04j3kvDYNgy9KwBl/6RFgMVgiSgjf7Rqd7NrhC2KxVT7I80qf7w==
+  version "0.2.37"
+  resolved "https://registry.yarnpkg.com/typeorm/-/typeorm-0.2.37.tgz#1a5e59216077640694d27c04c99ed3f968d15dc8"
+  integrity sha512-7rkW0yCgFC24I5T0f3S/twmLSuccPh1SQmxET/oDWn2sSDVzbyWdnItSdKy27CdJGTlKHYtUVeOcMYw5LRsXVw==
   dependencies:
     "@sqltools/formatter" "^1.2.2"
     app-root-path "^3.0.0"
     buffer "^6.0.3"
     chalk "^4.1.0"
-    cli-highlight "^2.1.10"
+    cli-highlight "^2.1.11"
     debug "^4.3.1"
     dotenv "^8.2.0"
     glob "^7.1.6"
@@ -28239,7 +28239,7 @@ typeorm@^0.2.25, typeorm@^0.2.31:
     tslib "^2.1.0"
     xml2js "^0.4.23"
     yargonaut "^1.1.4"
-    yargs "^16.2.0"
+    yargs "^17.0.1"
     zen-observable-ts "^1.0.0"
 
 typescript-formatter@^7.2.2:
@@ -30248,6 +30248,19 @@ yargs@^16.0.0, yargs@^16.2.0:
     y18n "^5.0.5"
     yargs-parser "^20.2.2"
 
+yargs@^17.0.1:
+  version "17.1.1"
+  resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.1.1.tgz#c2a8091564bdb196f7c0a67c1d12e5b85b8067ba"
+  integrity sha512-c2k48R0PwKIqKhPMWjeiF6y2xY/gPMUlro0sgxqXpbOIohWiLNXWslsootttv7E1e73QPAMQSg5FeySbVcpsPQ==
+  dependencies:
+    cliui "^7.0.2"
+    escalade "^3.1.1"
+    get-caller-file "^2.0.5"
+    require-directory "^2.1.1"
+    string-width "^4.2.0"
+    y18n "^5.0.5"
+    yargs-parser "^20.2.2"
+
 yargs@^2.1.1:
   version "2.3.0"
   resolved "https://registry.yarnpkg.com/yargs/-/yargs-2.3.0.tgz#e900c87250ec5cd080db6009fe3dd63156f1d7fb"