From aa52d2ea54ec679889db9abdb8cdd6639824f50e Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Sun, 30 May 2021 13:51:21 -0600 Subject: [PATCH] feat: new access-token package for encapsulation from swing-store --- package.json | 1 + packages/access-token/.eslintignore | 0 packages/access-token/.esmrc.json | 4 + packages/access-token/.gitignore | 2 + packages/access-token/.prettierignore | 0 packages/access-token/CHANGELOG.md | 204 +++++++++++ packages/access-token/LICENSE | 201 +++++++++++ packages/access-token/package.json | 46 +++ .../src/access-token.js | 17 +- packages/access-token/src/json-store.js | 335 ++++++++++++++++++ packages/access-token/test/test-state.js | 57 +++ packages/agoric-cli/lib/deploy.js | 2 +- packages/agoric-cli/lib/open.js | 51 +-- packages/agoric-cli/package.json | 2 +- packages/solo/package.json | 1 + packages/solo/src/web.js | 2 +- packages/solo/test/captp-fixture.js | 2 +- 17 files changed, 868 insertions(+), 59 deletions(-) create mode 100644 packages/access-token/.eslintignore create mode 100644 packages/access-token/.esmrc.json create mode 100644 packages/access-token/.gitignore create mode 100644 packages/access-token/.prettierignore create mode 100644 packages/access-token/CHANGELOG.md create mode 100644 packages/access-token/LICENSE create mode 100644 packages/access-token/package.json rename packages/{solo => access-token}/src/access-token.js (73%) create mode 100644 packages/access-token/src/json-store.js create mode 100644 packages/access-token/test/test-state.js diff --git a/package.json b/package.json index 08b5c4634e3..bea10f3b88a 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "packages/sharing-service", "packages/zoe", "packages/vats", + "packages/access-token", "packages/solo", "packages/dapp-svelte-wallet/ui", "packages/dapp-svelte-wallet/api", diff --git a/packages/access-token/.eslintignore b/packages/access-token/.eslintignore new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/access-token/.esmrc.json b/packages/access-token/.esmrc.json new file mode 100644 index 00000000000..3ca5707ba40 --- /dev/null +++ b/packages/access-token/.esmrc.json @@ -0,0 +1,4 @@ +{ + "cache": true, + "sourceMap": true +} diff --git a/packages/access-token/.gitignore b/packages/access-token/.gitignore new file mode 100644 index 00000000000..b5d4d627d73 --- /dev/null +++ b/packages/access-token/.gitignore @@ -0,0 +1,2 @@ +# swingset persistence data +*.jsonlines diff --git a/packages/access-token/.prettierignore b/packages/access-token/.prettierignore new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/access-token/CHANGELOG.md b/packages/access-token/CHANGELOG.md new file mode 100644 index 00000000000..7a9c569ecc9 --- /dev/null +++ b/packages/access-token/CHANGELOG.md @@ -0,0 +1,204 @@ +# Change Log + +All notable changes to this project will be documented in this file. +See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. + +## [0.3.6](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.3.5...@agoric/swing-store-simple@0.3.6) (2021-04-07) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.3.5](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.3.4...@agoric/swing-store-simple@0.3.5) (2021-04-06) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.3.4](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.3.3...@agoric/swing-store-simple@0.3.4) (2021-03-24) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.3.3](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.3.2...@agoric/swing-store-simple@0.3.3) (2021-03-16) + + +### Bug Fixes + +* make separate 'test:xs' target, remove XS from 'test' target ([b9c1a69](https://github.com/Agoric/agoric-sdk/commit/b9c1a6987093fc8e09e8aba7acd2a1618413bac8)), closes [#2647](https://github.com/Agoric/agoric-sdk/issues/2647) + + + + + +## [0.3.2](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.3.1...@agoric/swing-store-simple@0.3.2) (2021-02-22) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.3.1](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.3.0...@agoric/swing-store-simple@0.3.1) (2021-02-16) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +# [0.3.0](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.2.7...@agoric/swing-store-simple@0.3.0) (2020-12-10) + + +### Features + +* **import-bundle:** Preliminary support Endo zip hex bundle format ([#1983](https://github.com/Agoric/agoric-sdk/issues/1983)) ([983681b](https://github.com/Agoric/agoric-sdk/commit/983681bfc4bf512b6bd90806ed9220cd4fefc13c)) + + + + + +## [0.2.7](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.2.7-dev.0...@agoric/swing-store-simple@0.2.7) (2020-11-07) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.2.7-dev.0](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.2.6...@agoric/swing-store-simple@0.2.7-dev.0) (2020-10-19) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.2.6](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.2.6-dev.2...@agoric/swing-store-simple@0.2.6) (2020-10-11) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.2.6-dev.2](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.2.6-dev.1...@agoric/swing-store-simple@0.2.6-dev.2) (2020-09-18) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.2.6-dev.1](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.2.6-dev.0...@agoric/swing-store-simple@0.2.6-dev.1) (2020-09-18) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.2.6-dev.0](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.2.5...@agoric/swing-store-simple@0.2.6-dev.0) (2020-09-18) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.2.5](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.2.4...@agoric/swing-store-simple@0.2.5) (2020-09-16) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.2.4](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.2.3...@agoric/swing-store-simple@0.2.4) (2020-08-31) + + +### Bug Fixes + +* reduce inconsistency among our linting rules ([#1492](https://github.com/Agoric/agoric-sdk/issues/1492)) ([b6b675e](https://github.com/Agoric/agoric-sdk/commit/b6b675e2de110e2af19cad784a66220cab21dacf)) + + + + + +## [0.2.3](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.2.2...@agoric/swing-store-simple@0.2.3) (2020-06-30) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.2.2](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.2.1...@agoric/swing-store-simple@0.2.2) (2020-05-17) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.2.1](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.2.0...@agoric/swing-store-simple@0.2.1) (2020-05-10) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +# [0.2.0](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.1.2...@agoric/swing-store-simple@0.2.0) (2020-05-04) + + +### Features + +* swing-store-simple: add isSwingStore() query ([c450459](https://github.com/Agoric/agoric-sdk/commit/c450459a92d3ecba4a106820d980683babdf8c29)), closes [#953](https://github.com/Agoric/agoric-sdk/issues/953) + + + + + +## [0.1.2](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.1.2-alpha.0...@agoric/swing-store-simple@0.1.2) (2020-04-13) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.1.2-alpha.0](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.1.1...@agoric/swing-store-simple@0.1.2-alpha.0) (2020-04-12) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## [0.1.1](https://github.com/Agoric/agoric-sdk/compare/@agoric/swing-store-simple@0.1.1-alpha.0...@agoric/swing-store-simple@0.1.1) (2020-04-02) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## 0.1.1-alpha.0 (2020-04-02) + +**Note:** Version bump only for package @agoric/swing-store-simple + + + + + +## 0.1.1-alpha.0 (2020-04-02) + +**Note:** Version bump only for package @agoric/swing-store-simple diff --git a/packages/access-token/LICENSE b/packages/access-token/LICENSE new file mode 100644 index 00000000000..261eeb9e9f8 --- /dev/null +++ b/packages/access-token/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/packages/access-token/package.json b/packages/access-token/package.json new file mode 100644 index 00000000000..c507f65895a --- /dev/null +++ b/packages/access-token/package.json @@ -0,0 +1,46 @@ +{ + "name": "@agoric/access-token", + "version": "0.4.0", + "description": "Persistent credentials for Agoric users, backed by a simple JSON file", + "main": "src/access-token.js", + "repository": "https://github.com/Agoric/agoric-sdk", + "author": "Agoric", + "license": "Apache-2.0", + "scripts": { + "build": "exit 0", + "test": "ava", + "test:xs": "exit 0", + "lint-fix": "eslint --fix '**/*.js'", + "lint-check": "eslint '**/*.js'" + }, + "dependencies": { + "@agoric/assert": "^0.2.12", + "n-readlines": "^1.0.0" + }, + "devDependencies": { + "@agoric/install-ses": "^0.5.13", + "ava": "^3.12.1", + "esm": "^3.2.25" + }, + "publishConfig": { + "access": "public" + }, + "ava": { + "files": [ + "test/**/test-*.js" + ], + "require": [ + "esm" + ], + "timeout": "2m" + }, + "eslintConfig": { + "extends": [ + "@agoric" + ] + }, + "prettier": { + "trailingComma": "all", + "singleQuote": true + } +} diff --git a/packages/solo/src/access-token.js b/packages/access-token/src/access-token.js similarity index 73% rename from packages/solo/src/access-token.js rename to packages/access-token/src/access-token.js index fa0b72e860c..142b38e7f33 100644 --- a/packages/solo/src/access-token.js +++ b/packages/access-token/src/access-token.js @@ -3,7 +3,7 @@ import crypto from 'crypto'; import os from 'os'; import path from 'path'; -import { openSwingStore } from '@agoric/swing-store-lmdb'; +import { openJSONStore } from './json-store.js'; // Adapted from https://stackoverflow.com/a/43866992/14073862 export function generateAccessToken({ @@ -27,18 +27,25 @@ export function generateAccessToken({ } export async function getAccessToken(port) { + if (typeof port === 'string') { + const match = port.match(/^(.*:)?(\d+)$/); + if (match) { + port = match[2]; + } + } + // Ensure we're protected with a unique accessToken for this basedir. const sharedStateDir = path.join(os.homedir(), '.agoric'); await fs.promises.mkdir(sharedStateDir, { mode: 0o700, recursive: true }); // Ensure an access token exists. - const { kvStore, commit, close } = openSwingStore(sharedStateDir); + const { storage, commit, close } = openJSONStore(sharedStateDir); const accessTokenKey = `accessToken/${port}`; - if (!kvStore.has(accessTokenKey)) { - kvStore.set(accessTokenKey, await generateAccessToken()); + if (!storage.has(accessTokenKey)) { + storage.set(accessTokenKey, await generateAccessToken()); commit(); } - const accessToken = kvStore.get(accessTokenKey); + const accessToken = storage.get(accessTokenKey); close(); return accessToken; } diff --git a/packages/access-token/src/json-store.js b/packages/access-token/src/json-store.js new file mode 100644 index 00000000000..b950c940808 --- /dev/null +++ b/packages/access-token/src/json-store.js @@ -0,0 +1,335 @@ +// @ts-check +import fs from 'fs'; +import path from 'path'; +import Readlines from 'n-readlines'; + +// TODO: Update this when we make a breaking change. +// const DATA_FILE = 'data.jsonlines'; +// +// For compatibility, because it's tricky to diagnose that the file location has +// changed when you're just trying to use the wallet from the browser. +const DATA_FILE = 'swingset-kernel-state.jsonlines'; + +/** + * @typedef {ReturnType['storage']} JSONStore + */ + +function safeUnlink(filePath) { + try { + fs.unlinkSync(filePath); + } catch (e) { + if (e.code !== 'ENOENT') { + throw e; + } + } +} + +/** + * Create a new instance of a RAM-based implementation of the Storage API. + * + * The "Storage API" is a set of functions { has, getKeys, get, set, delete } + * that work on string keys and accept string values. + * + * returns an object: { + * storage, // the storage API object itself + * state // the underlying map that holds the state in memory + * } + */ +function makeStorageInMemory() { + const state = new Map(); + + /** + * Test if the state contains a value for a given key. + * + * @param {string} key The key that is of interest. + * + * @returns {boolean} true if a value is stored for the key, false if not. + * + * @throws if key is not a string. + */ + function has(key) { + if (`${key}` !== key) { + throw new Error(`non-string key ${key}`); + } + return state.has(key); + } + + /** + * Generator function that returns an iterator over all the keys within a + * given range, in lexicographical order. + * + * Note that this can be slow as it's only intended for use in debugging and + * test result verification. + * + * @param {string} start Start of the key range of interest (inclusive). An empty + * string indicates a range from the beginning of the key set. + * @param {string} end End of the key range of interest (exclusive). An empty string + * indicates a range through the end of the key set. + * + * @yields an iterator for the keys from start <= key < end + * + * @throws if either parameter is not a string. + */ + function* getKeys(start, end) { + if (`${start}` !== start) { + throw new Error(`non-string start ${start}`); + } + if (`${end}` !== end) { + throw new Error(`non-string end ${end}`); + } + + const keys = Array.from(state.keys()).sort(); + for (const k of keys) { + if ((start === '' || start <= k) && (end === '' || k < end)) { + yield k; + } + } + } + + /** + * Obtain the value stored for a given key. + * + * @param {string} key The key whose value is sought. + * + * @returns {string|undefined} the (string) value for the given key, or undefined if there is no + * such value. + * + * @throws if key is not a string. + */ + function get(key) { + if (`${key}` !== key) { + throw new Error(`non-string key ${key}`); + } + return state.get(key); + } + + /** + * Store a value for a given key. The value will replace any prior value if + * there was one. + * + * @param {string} key The key whose value is being set. + * @param {string} value The value to set the key to. + * + * @throws if either parameter is not a string. + */ + function set(key, value) { + if (`${key}` !== key) { + throw new Error(`non-string key ${key}`); + } + if (`${value}` !== value) { + throw new Error(`non-string value ${value}`); + } + state.set(key, value); + } + + /** + * Remove any stored value for a given key. It is permissible for there to + * be no existing stored value for the key. + * + * @param {string} key The key whose value is to be deleted + * + * @throws if key is not a string. + */ + function del(key) { + if (`${key}` !== key) { + throw new Error(`non-string key ${key}`); + } + state.delete(key); + } + + const storage = { + has, + getKeys, + get, + set, + delete: del, + }; + + return { storage, state }; +} + +/** + * Do the work of `initJSONStore` and `openJSONStore`. + * + * @param {string} [dirPath] Path to a directory in which database files may be kept, or + * null. + * @param {boolean} [forceReset] If true, initialize the database to an empty state + * + * @returns {{ + * storage: JSONStore, // a storage API object to load and store data + * commit: () => void, // commit changes made since the last commit + * close: () => void, // shutdown the store, abandoning any uncommitted changes + * }} + */ +function makeJSONStore(dirPath, forceReset = false) { + const { storage, state } = makeStorageInMemory(); + + let storeFile; + if (dirPath) { + fs.mkdirSync(dirPath, { recursive: true }); + storeFile = path.resolve(dirPath, DATA_FILE); + if (forceReset) { + safeUnlink(storeFile); + } else { + let lines; + try { + lines = new Readlines(storeFile); + } catch (e) { + // storeFile will be missing the first time we try to use it. That's OK; + // commit will create it. + if (e.code !== 'ENOENT') { + throw e; + } + } + if (lines) { + let line = lines.next(); + while (line) { + // @ts-ignore JSON.parse can take a Buffer + const [key, value] = JSON.parse(line); + storage.set(key, value); + line = lines.next(); + } + } + } + } + + /** + * Commit unsaved changes. + */ + function commit() { + if (dirPath) { + const tempFile = `${storeFile}.tmp`; + const fd = fs.openSync(tempFile, 'w'); + + for (const [key, value] of state.entries()) { + const line = JSON.stringify([key, value]); + fs.writeSync(fd, line); + fs.writeSync(fd, '\n'); + } + fs.closeSync(fd); + fs.renameSync(tempFile, storeFile); + } + } + + /** + * Close the "database", abandoning any changes made since the last commit + * (if you want to save them, call commit() first). + */ + function close() { + // Nothing to do here. + } + + return { storage, commit, close }; +} + +/** + * Create a swingset store that is an in-memory map, normally backed by JSON + * serialized to a text file. If there is an existing store at the given + * `dirPath`, it will be reinitialized to an empty state. + * + * @param {string=} dirPath Path to a directory in which database files may be kept. + * This directory need not actually exist yet (if it doesn't it will be + * created) but it is reserved (by the caller) for the exclusive use of this + * JSON store instance. If this is nullish, the JSON store created will + * have no backing store and thus be non-persistent. + * + * returns an object: { + * storage, // a storage API object to load and store data + * commit, // a function to commit changes made since the last commit + * close // a function to shutdown the store, abandoning any uncommitted + * // changes + * } + */ +export function initJSONStore(dirPath) { + if (dirPath !== null && dirPath !== undefined && `${dirPath}` !== dirPath) { + throw new Error('dirPath must be a string or nullish'); + } + return makeJSONStore(dirPath, true); +} + +/** + * Open a swingset store that is an in-memory map, backed by JSON serialized to + * a text file. If there is no existing store at the given `dirPath`, a new, + * empty store will be created. + * + * @param {string} dirPath Path to a directory in which database files may be kept. + * This directory need not actually exist yet (if it doesn't it will be + * created) but it is reserved (by the caller) for the exclusive use of this + * JSON store instance. + * + * returns an object: { + * storage, // a storage API object to load and store data + * commit, // a function to commit changes made since the last commit + * close // a function to shutdown the store, abandoning any uncommitted + * // changes + * } + */ +export function openJSONStore(dirPath) { + if (`${dirPath}` !== dirPath) { + throw new Error('dirPath must be a string'); + } + return makeJSONStore(dirPath, false); +} + +/** + * Produce a representation of all the state found in a JSON store. + * + * WARNING: This is a helper function intended for use in testing and + * debugging. It extracts *everything*, and does so in the simplest and + * stupidest possible way, hence it is likely to be a performance and memory + * hog if you attempt to use it on anything real. + * + * @param {JSONStore} storage The swing storage whose state is to be extracted. + * + * @returns {Record} an array representing all the current state in `storage`, one + * element of the form [key, value] per key/value pair. + */ +export function getAllState(storage) { + /** @type { Record } */ + const stuff = {}; + for (const key of Array.from(storage.getKeys('', ''))) { + // @ts-ignore get(key) of key from getKeys() is not undefined + stuff[key] = storage.get(key); + } + return stuff; +} + +/** + * Stuff a bunch of state into a JSON store. + * + * WARNING: This is intended to support testing and should not be used as a + * general store initialization mechanism. In particular, note that it does + * not bother to remove any existing state in the store that it is given. + * + * @param {JSONStore} storage The storage whose state is to be set. + * @param {Array<[string, string]>} stuff An array of key/value pairs, each element of the form [key, value] + */ +export function setAllState(storage, stuff) { + for (const k of Object.getOwnPropertyNames(stuff)) { + storage.set(k, stuff[k]); + } +} + +/** + * Is this directory a compatible JSON store? + * + * @param {string} dirPath Path to a directory in which database files might be present. + * This directory need not actually exist + * + * @returns {boolean} + * If the directory is present and contains the files created by initJSONStore + * or openJSONStore, returns true. Else returns false. + * + */ +export function isJSONStore(dirPath) { + if (`${dirPath}` !== dirPath) { + throw new Error('dirPath must be a string'); + } + if (fs.existsSync(dirPath)) { + const storeFile = path.resolve(dirPath, DATA_FILE); + if (fs.existsSync(storeFile)) { + return true; + } + } + return false; +} diff --git a/packages/access-token/test/test-state.js b/packages/access-token/test/test-state.js new file mode 100644 index 00000000000..b4645243b21 --- /dev/null +++ b/packages/access-token/test/test-state.js @@ -0,0 +1,57 @@ +import fs from 'fs'; + +import test from 'ava'; +import { + initJSONStore, + openJSONStore, + getAllState, + isJSONStore, +} from '../src/json-store'; + +function testStorage(t, storage) { + t.falsy(storage.has('missing')); + t.is(storage.get('missing'), undefined); + + storage.set('foo', 'f'); + t.truthy(storage.has('foo')); + t.is(storage.get('foo'), 'f'); + + storage.set('foo2', 'f2'); + storage.set('foo1', 'f1'); + storage.set('foo3', 'f3'); + t.deepEqual(Array.from(storage.getKeys('foo1', 'foo3')), ['foo1', 'foo2']); + t.deepEqual(Array.from(storage.getKeys('foo1', 'foo4')), [ + 'foo1', + 'foo2', + 'foo3', + ]); + + storage.delete('foo2'); + t.falsy(storage.has('foo2')); + t.is(storage.get('foo2'), undefined); + t.deepEqual(Array.from(storage.getKeys('foo1', 'foo4')), ['foo1', 'foo3']); + + const reference = { + foo: 'f', + foo1: 'f1', + foo3: 'f3', + }; + t.deepEqual(getAllState(storage), reference, 'check state after changes'); +} + +test('storageInFile', t => { + const dbDir = 'testdb'; + t.teardown(() => fs.rmdirSync(dbDir, { recursive: true })); + fs.rmdirSync(dbDir, { recursive: true }); + t.is(isJSONStore(dbDir), false); + const { storage, commit, close } = initJSONStore(dbDir); + testStorage(t, storage); + commit(); + const before = getAllState(storage); + close(); + t.is(isJSONStore(dbDir), true); + + const { storage: after } = openJSONStore(dbDir); + t.deepEqual(getAllState(after), before, 'check state after reread'); + t.is(isJSONStore(dbDir), true); +}); diff --git a/packages/agoric-cli/lib/deploy.js b/packages/agoric-cli/lib/deploy.js index 34ca5cbfe55..f26ec313d79 100644 --- a/packages/agoric-cli/lib/deploy.js +++ b/packages/agoric-cli/lib/deploy.js @@ -6,7 +6,7 @@ import bundleSource from '@agoric/bundle-source'; import path from 'path'; import inquirer from 'inquirer'; -import { getAccessToken } from './open'; +import { getAccessToken } from '@agoric/access-token'; // note: CapTP has its own HandledPromise instantiation, and the contract // must use the same one that CapTP uses. We achieve this by not bundling diff --git a/packages/agoric-cli/lib/open.js b/packages/agoric-cli/lib/open.js index 3ce4a07b42b..d11ce74d811 100644 --- a/packages/agoric-cli/lib/open.js +++ b/packages/agoric-cli/lib/open.js @@ -1,59 +1,10 @@ /* global process setInterval clearInterval */ -import { promises as defaultFs } from 'fs'; import opener from 'opener'; -import crypto from 'crypto'; -import path from 'path'; -import os from 'os'; -import { openSwingStore } from '@agoric/swing-store-lmdb'; +import { getAccessToken } from '@agoric/access-token'; import { assert, details as X } from '@agoric/assert'; -// Adapted from https://stackoverflow.com/a/43866992/14073862 -export function generateAccessToken({ - stringBase = 'base64url', - byteLength = 48, -} = {}) { - return new Promise((resolve, reject) => - crypto.randomBytes(byteLength, (err, buffer) => { - if (err) { - reject(err); - } else if (stringBase === 'base64url') { - // Convert to url-safe base64. - const base64 = buffer.toString('base64'); - const base64url = base64.replace(/\+/g, '-').replace(/\//g, '_'); - resolve(base64url); - } else { - resolve(buffer.toString(stringBase)); - } - }), - ); -} - -export async function getAccessToken(port, powers = {}) { - const { fs = defaultFs } = powers; - - const match = port.match(/^(.*:)?(\d+)$/); - if (match) { - port = match[2]; - } - - // Ensure we're protected with a unique accessToken for this basedir. - const sharedStateDir = path.join(os.homedir(), '.agoric'); - await fs.mkdir(sharedStateDir, { mode: 0o700, recursive: true }); - - // Ensure an access token exists. - const { kvStore, commit, close } = openSwingStore(sharedStateDir); - const accessTokenKey = `accessToken/${port}`; - if (!kvStore.has(accessTokenKey)) { - kvStore.set(accessTokenKey, await generateAccessToken()); - commit(); - } - const accessToken = kvStore.get(accessTokenKey); - close(); - return accessToken; -} - export default async function walletMain(progname, rawArgs, powers, opts) { const { anylogger, fs } = powers; const console = anylogger('agoric:wallet'); diff --git a/packages/agoric-cli/package.json b/packages/agoric-cli/package.json index 69b09853f9e..b42c518cd65 100644 --- a/packages/agoric-cli/package.json +++ b/packages/agoric-cli/package.json @@ -25,13 +25,13 @@ "tmp": "^0.1.0" }, "dependencies": { + "@agoric/access-token": "^0.4.0", "@agoric/assert": "^0.2.12", "@agoric/bundle-source": "^1.3.7", "@agoric/captp": "^1.7.13", "@agoric/install-ses": "^0.5.13", "@agoric/nat": "^4.0.0", "@agoric/promise-kit": "^0.2.13", - "@agoric/swing-store-lmdb": "^0.4.11", "@iarna/toml": "^2.2.3", "anylogger": "^0.21.0", "chalk": "^2.4.2", diff --git a/packages/solo/package.json b/packages/solo/package.json index 611bac13f7f..a79f9530104 100644 --- a/packages/solo/package.json +++ b/packages/solo/package.json @@ -26,6 +26,7 @@ "author": "Agoric", "license": "Apache-2.0", "dependencies": { + "@agoric/access-token": "^0.4.0", "@agoric/assert": "^0.2.12", "@agoric/captp": "^1.7.13", "@agoric/cosmic-swingset": "^0.31.0", diff --git a/packages/solo/src/web.js b/packages/solo/src/web.js index 564f813e8a6..993156010ce 100644 --- a/packages/solo/src/web.js +++ b/packages/solo/src/web.js @@ -7,7 +7,7 @@ import express from 'express'; import WebSocket from 'ws'; import anylogger from 'anylogger'; -import { getAccessToken } from './access-token'; +import { getAccessToken } from '@agoric/access-token'; // We need to CommonJS require morgan or else it warns, until: // https://github.com/expressjs/morgan/issues/190 diff --git a/packages/solo/test/captp-fixture.js b/packages/solo/test/captp-fixture.js index 3c0f9ae7854..e0a243ad111 100644 --- a/packages/solo/test/captp-fixture.js +++ b/packages/solo/test/captp-fixture.js @@ -3,7 +3,7 @@ import { spawn } from 'child_process'; import WebSocket from 'ws'; import { makeCapTP, E } from '@agoric/captp'; -import { getAccessToken } from '../src/access-token'; +import { getAccessToken } from '@agoric/access-token'; // Ensure we're all using the same HandledPromise. export { E };