├── .eslintignore ├── .eslintrc.json ├── .gitignore ├── .mocharc.json ├── .prettierrc.json ├── LICENSE ├── README.md ├── bin ├── dev.cmd ├── dev.js ├── run.cmd └── run.js ├── package-lock.json ├── package.json ├── src ├── commands │ ├── all-data │ │ └── index.ts │ ├── create-did │ │ └── index.ts │ ├── retrieve-data │ │ └── index.ts │ └── store-data │ │ └── index.ts ├── config │ └── web5-config.ts └── index.ts ├── tsconfig.json └── yarn.lock /.eslintignore: -------------------------------------------------------------------------------- 1 | /dist 2 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["oclif", "oclif-typescript", "prettier"] 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *-debug.log 2 | *-error.log 3 | **/.DS_Store 4 | /.idea 5 | /dist 6 | /tmp 7 | /node_modules 8 | oclif.manifest.json 9 | /.vscode 10 | 11 | /DATA 12 | dist 13 | pnpm-lock.yaml 14 | .pnp.* 15 | .yarn/* 16 | !.yarn/patches 17 | !.yarn/plugins 18 | !.yarn/releases 19 | !.yarn/sdks 20 | !.yarn/versions 21 | -------------------------------------------------------------------------------- /.mocharc.json: -------------------------------------------------------------------------------- 1 | { 2 | "require": [ 3 | "ts-node/register" 4 | ], 5 | "watch-extensions": [ 6 | "ts" 7 | ], 8 | "recursive": true, 9 | "reporter": "spec", 10 | "timeout": 60000, 11 | "node-option": [ 12 | "loader=ts-node/esm", 13 | "experimental-specifier-resolution=node" 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | "@oclif/prettier-config" 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2024 Decentralized Identity Foundation contributors 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DWN Community Node User Guide 2 | 3 | This will get you up to speed with the basics of working with Decentralized Web Nodes (DWNs) and DIF's DWN Community Node, powered by TBD and Google Cloud. This includes a minimal CLI project and additional guidance to get you started. 4 | ![dwn_community_node](https://github.com/user-attachments/assets/0f1617d2-5dec-4a74-9f70-a9f5634ebec0) 5 | 6 | ## Introduction 7 | 8 | A DWN is a decentralized personal and application data storage and message relay node. Users may have multiple nodes that replicate their data between them. 9 | 10 | DWNs provide the following benefits: 11 | 12 | - Reduced Reliance on Third Parties: Users are less dependent on centralized platforms that often collect and monetize their data. 13 | - Increased User Control: Users can choose what data to share and with whom, fostering a more secure and empowering online experience via the use of [protocols](https://developer.tbd.website/docs/web5/learn/protocols). 14 | 15 | DWNs follow an open standard; see the DIF [Decentralized Web Node specification](https://identity.foundation/decentralized-web-node/spec/). DIF plays a vital role in addressing these challenges through standardization and education, paving the way for a future Web where privacy and data sovereignty are paramount. 16 | 17 | ## About the DIF DWN Community Node 18 | 19 | TBD and Google Cloud have partnered to enable DIF’s DWN Community Node. The goal is to make it easy for builders to get started with next generation web infrastructure. 20 | 21 | The Community Node is a reliable sandbox for staging, pre-production, or testing -- not production use. 22 | 23 | You can connect to the Community Node easily by passing the endpoint when calling `Web5.connect()`, as shown below. 24 | 25 | ## Basic CLI Tutorial 26 | 27 | ### About 28 | 29 | For simplicity, this sample uses [TBD's `@web/api` package](https://github.com/TBD54566975/web5-js), which handles the details of interacting with DWNs. Because this conforms to the DWN spec, you may use a different library or write your own to interact with DWNs. 30 | 31 | By default, the `@web5/api` package connects to TBD's DWN instance. You can configure this code to connect to other instances, such as your own self-hosted instance or DIF's Community Node. 32 | 33 | We'll start with a basic DWN command line example, which will use the default (TBD's) DWN node, and then update it to use DIF's Community Node. 34 | 35 | ### Install 36 | 37 | ```bash 38 | git clone https://github.com/decentralized-identity/dwn-cli-sample.git 39 | cd dwn-cli-sample 40 | npm install 41 | npm run build 42 | ``` 43 | 44 | ### Commands 45 | 46 | - all-data: List all the DWN records 47 | - create-did: Create a DID 48 | - help: Display help for dwn-cli. 49 | - plugins: List installed plugins. 50 | - retrieve-data: Retrieve records from DWN 51 | - store-data: Store data to DWN 52 | 53 | ### Examples 54 | 55 | #### Create a DID 56 | 57 | ```sh 58 | ./bin/run.js create-did 59 | ``` 60 | 61 | Output will look like: 62 | 63 | ```sh 64 | Created DID: did:dht:43ffbao7fxkhofcxpody87dz74cp5ynx8bsed4iudtzqk7gimewy 65 | ``` 66 | 67 | #### Write to the DWN 68 | 69 | ```sh 70 | ./bin/run.js store-data "hello world" 71 | ``` 72 | 73 | Output will look like: 74 | 75 | ```sh 76 | Record ID: bafyreih4rqmu4jztwoews26onwda3y7p4gfguevke2cxhvtvmul73bl4v4 77 | ``` 78 | 79 | #### List the records in the DWN 80 | 81 | ```sh 82 | ./bin/run.js all-data 83 | ``` 84 | 85 | Output will look like: 86 | 87 | ```sh 88 | ==============Document list========== 89 | 1. bafyreih4rqmu4jztwoews26onwda3y7p4gfguevke2cxhvtvmul73bl4v4 90 | 91 | ``` 92 | 93 | #### Retrieve a DWN record by record ID 94 | 95 | ```sh 96 | ./bin/run.js retrieve-data bafyreih4rqmu4jztwoews26onwda3y7p4gfguevke2cxhvtvmul73bl4v4 97 | ``` 98 | 99 | Output will look like: 100 | 101 | ```sh 102 | ============Record Data================= 103 | Author: did:dht:43ffbao7fxkhofcxpody87dz74cp5ynx8bsed4iudtzqk7gimewy 104 | Content: hello world 105 | ``` 106 | 107 | ## The DIF Community Node 108 | 109 | ### Connecting 110 | 111 | To connect to the community node, simply pass https://dwn.gcda.xyz to the `dwnEndpoints` array as shown below: 112 | 113 | ```js 114 | const {did, web5} = await Web5.connect({ 115 | didCreateOptions: { 116 | dwnEndpoints: ['https://dwn.gcda.xyz'], // User provides google's community DWN instance 117 | }, 118 | registration: { 119 | onSuccess: () => { 120 | // Registration succeeded, set a local storage value to indicate the user is registered and registration does not need to occur again. 121 | }, 122 | onFailure: (error) => { 123 | // Registration failed, display an error message to the user, and pass in the registration object again to retry next time the user connects. 124 | }, 125 | }, 126 | }) 127 | ``` 128 | 129 | ### Limits & Terms of Use 130 | 131 | - Reminder: not production use 132 | - While the Community Node is a stable testbed for experimenting with DWNs, it's not intended for long-term or production use 133 | - Data can be deleted at any time! 134 | - Limits 135 | - Each DID gets 1 gig of storage 136 | - Records for storage unless the payload exceeds 30k 137 | - Terms of use: To use the community node, you agree to the terms of service: https://dwn.gcda.xyz/registration/terms-of-service 138 | 139 | ## Running your own DWN server 140 | 141 | By default, when you call `Web5.connect()` it will use the bootstrap DWN nodes included which allow people to reach you via your DID. 142 | 143 | You may want to run a DWN server just for you, or as a public service for you and your friends and family. DWNs can be as simple as a docker image or a node process running somewhere. 144 | 145 | DWN-servers can run anywhere you can run node.js or docker. http and websocket need to be available to the DWN server. See below for some suggestions. 146 | 147 | For additional details about configuration settings for privacy, data storage, and network connectivity, see [TBD's recommendations](https://github.com/TBD54566975/dwn-server/blob/main/README.md). 148 | 149 | ### Running on render.com 150 | 151 | You can run an instance on the render.com service: 152 | 153 | - Create a render.com account 154 | - Fork this repo 155 | - Upgrade your render.com account to a paid account 156 | - Create a new "Web service" type application 157 | - Choose the forked repo to run (or you can point to the main repo) 158 | - Choose the "starter" size instance 159 | - Create a 1GB (or larger) disk, and mount it on /dwn-server/data 160 | 161 | ### Running with ngrok 162 | 163 | You can run a DWN-server on your local machine or home server and expose it to the internet using ngrok. 164 | 165 | First, install [ngrok](https://ngrok.com/download) 166 | 167 | Then run: 168 | 169 | ```bash 170 | docker run -p 3000:3000 -v myvolume:/dwn-server/data ghcr.io/tbd54566975/dwn-server:main 171 | 172 | ## in another terminal: 173 | ngrok http 3000 174 | ``` 175 | 176 | - Note the resulting publicly addressable https url for your DWN instance. 177 | 178 | ### Running with cloudflared 179 | 180 | Cloudflare has a tunnel service that you can use to expose your DWN server to the internet, if you run it on a server at home. With cloudflared installed, run the following commands: 181 | 182 | ```sh 183 | git clone https://github.com/TBD54566975/dwn-server.git 184 | cd dwn-server 185 | npm install 186 | npm run server 187 | 188 | ## in another terminal: 189 | 190 | cloudflared tunnel --url http://localhost:3000 191 | ``` 192 | 193 | ## Community and Support 194 | 195 | - Support & usage (sponsored by TBD): [Discord](https://discord.gg/tbd) 196 | - Issues with this documentation or DIF samples: create a github issue in the appropriate repo 197 | - Get involved in the DWN specification: [join DIF](https://identity.foundation/join/) 198 | 199 | ## Additional Resources 200 | 201 | - [TBD's documentation](https://developer.tbd.website/docs/web5/build/decentralized-web-nodes/web5-connect) 202 | -------------------------------------------------------------------------------- /bin/dev.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | node --loader ts-node/esm --no-warnings=ExperimentalWarning "%~dp0\dev" %* 4 | -------------------------------------------------------------------------------- /bin/dev.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env -S node --loader ts-node/esm --no-warnings=ExperimentalWarning 2 | 3 | // eslint-disable-next-line n/shebang 4 | import { execute } from '@oclif/core' 5 | 6 | await execute({ development: true, dir: import.meta.url }) 7 | -------------------------------------------------------------------------------- /bin/run.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | node "%~dp0\run" %* 4 | -------------------------------------------------------------------------------- /bin/run.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | import {execute} from '@oclif/core' 4 | 5 | await execute({dir: import.meta.url}) 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dwn-cli", 3 | "description": "A simple CLI to interact with the Decentralized Web Nodes (DWN) APIs.", 4 | "version": "0.0.0", 5 | "author": "Decentralized Identity Foundation", 6 | "license": "Apache-2.0", 7 | "repository": "https://github.com/decentralized-identity/dwn-cli-sample.git", 8 | "homepage": "https://github.com/decentralized-identity/dwn-cli-app", 9 | "bugs": "https://github.com/decentralized-identity/dwn-cli-app/issues", 10 | "keywords": [ 11 | "DWN" 12 | ], 13 | "type": "module", 14 | "main": "dist/index.js", 15 | "files": [ 16 | "/bin", 17 | "/dist", 18 | "/oclif.manifest.json" 19 | ], 20 | "types": "dist/index.d.ts", 21 | "pkg": { 22 | "scripts": "./dist/**/*.js" 23 | }, 24 | "bin": { 25 | "dwn-cli": "./bin/run.js" 26 | }, 27 | "oclif": { 28 | "bin": "dwn-cli", 29 | "dirname": "dwn-cli", 30 | "commands": "./dist/commands", 31 | "plugins": [ 32 | "@oclif/plugin-help", 33 | "@oclif/plugin-plugins" 34 | ], 35 | "topicSeparator": " " 36 | }, 37 | "scripts": { 38 | "build": "shx rm -rf dist && tsc -b", 39 | "lint": "eslint . --ext .ts", 40 | "postpack": "shx rm -f oclif.manifest.json", 41 | "posttest": "yarn lint", 42 | "prepack": "oclif manifest && oclif readme", 43 | "test": "mocha --forbid-only \"test/**/*.test.ts\"", 44 | "version": "oclif readme && git add README.md" 45 | }, 46 | "dependencies": { 47 | "@oclif/core": "^3", 48 | "@oclif/plugin-help": "^6", 49 | "@oclif/plugin-plugins": "^5", 50 | "@types/readable-stream": "^4.0.14", 51 | "@web5/api": "^0.10.0", 52 | "pkg": "^5.8.1" 53 | }, 54 | "devDependencies": { 55 | "@oclif/prettier-config": "^0.2.1", 56 | "@oclif/test": "^3", 57 | "@types/chai": "^4", 58 | "@types/mocha": "^10", 59 | "@types/node": "^18", 60 | "chai": "^4", 61 | "eslint": "^8", 62 | "eslint-config-oclif": "^5", 63 | "eslint-config-oclif-typescript": "^3", 64 | "eslint-config-prettier": "^9", 65 | "mocha": "^10", 66 | "oclif": "^4", 67 | "shx": "^0.3.3", 68 | "ts-node": "^10", 69 | "typescript": "^5" 70 | }, 71 | "engines": { 72 | "node": ">=20.0.0" 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/commands/all-data/index.ts: -------------------------------------------------------------------------------- 1 | import {Command, Flags} from '@oclif/core' 2 | import {Web5} from '@web5/api' 3 | 4 | import { web5DefaultConfig } from '../../config/web5-config.js' 5 | 6 | export default class AllData extends Command { 7 | static override description = 'List all the DWN documents' 8 | 9 | static override examples = ['<%= config.bin %> <%= command.id %>'] 10 | 11 | static override flags = { 12 | password: Flags.string({char: 'p', description: 'Password'}), 13 | } 14 | 15 | public async run(): Promise { 16 | const {flags} = await this.parse(AllData) 17 | 18 | // move to a reusable config location 19 | 20 | const { did, web5 } = await Web5.connect({ password: flags.password, ...web5DefaultConfig }); 21 | 22 | const response = await web5.dwn.records.query({ 23 | message: { 24 | filter: { 25 | dataFormat: 'text/plain', 26 | }, 27 | // published:true 28 | }, 29 | }) 30 | 31 | const records = response?.records || []; 32 | 33 | this.log('==============Document list==========') 34 | for (const [index, record] of records.entries()) { 35 | this.log(`${index + 1}. ${record?.id}`) 36 | } 37 | 38 | // eslint-disable-next-line n/no-process-exit 39 | process.exit(0) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/commands/create-did/index.ts: -------------------------------------------------------------------------------- 1 | import {Command, Flags} from '@oclif/core' 2 | import {Web5} from '@web5/api' 3 | import {webcrypto} from 'node:crypto' 4 | import fs from 'node:fs' 5 | 6 | import { web5DefaultConfig } from '../../config/web5-config.js' 7 | 8 | // @ts-ignore 9 | if (!globalThis.crypto) globalThis.crypto = webcrypto 10 | 11 | export default class CreateDid extends Command { 12 | static description = 'Create a DID' 13 | 14 | static override examples = ['<%= config.bin %> <%= command.id %>'] 15 | 16 | static override flags = { 17 | password: Flags.string({char: 'p', description: 'Password'}), 18 | } 19 | 20 | async run(): Promise { 21 | const {flags} = await this.parse(CreateDid) 22 | 23 | const {did: userDid, web5} = await Web5.connect({password:flags.password, ...web5DefaultConfig}) 24 | 25 | this.log('Created DID:', userDid) 26 | fs.writeFileSync('DATA/did.json', JSON.stringify({did: userDid})) 27 | 28 | // eslint-disable-next-line n/no-process-exit 29 | process.exit(0) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/commands/retrieve-data/index.ts: -------------------------------------------------------------------------------- 1 | import {Args, Command, Flags} from '@oclif/core' 2 | import {Web5} from '@web5/api' 3 | 4 | import { web5DefaultConfig } from '../../config/web5-config.js' 5 | 6 | export default class RetrieveData extends Command { 7 | static override args = { 8 | data: Args.string({description: 'Dwn document Id'}), 9 | } 10 | 11 | static override description = 'Retrieve data from DWN' 12 | 13 | static override examples = ['<%= config.bin %> <%= command.id %>'] 14 | 15 | static override flags = { 16 | password: Flags.string({char: 'p', description: 'Password'}), 17 | } 18 | 19 | public async run(): Promise { 20 | const {args, flags} = await this.parse(RetrieveData) 21 | 22 | if (args.data) { 23 | const {web5} = await Web5.connect({password: flags.password, ...web5DefaultConfig}) 24 | const {record} = await web5.dwn.records.read({ 25 | message: { 26 | filter: { 27 | recordId: args.data, 28 | }, 29 | // published:true 30 | }, 31 | }) 32 | this.log('============Record Data=================') 33 | this.log('Author:', record?.author) 34 | this.log('Content:', await record.data.text()) 35 | 36 | // eslint-disable-next-line n/no-process-exit 37 | process.exit(0) 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/commands/store-data/index.ts: -------------------------------------------------------------------------------- 1 | import {Args, Command, Flags} from '@oclif/core' 2 | import {Web5} from '@web5/api' 3 | 4 | import { web5DefaultConfig } from '../../config/web5-config.js' 5 | 6 | export default class StoreData extends Command { 7 | static override args = { 8 | data: Args.string({description: 'String data to store'}), 9 | } 10 | 11 | static override description = 'Store data to DWN' 12 | 13 | static override examples = ['<%= config.bin %> <%= command.id %>'] 14 | 15 | static override flags = { 16 | password: Flags.string({char: 'p', description: 'Password'}), 17 | } 18 | 19 | public async run(): Promise { 20 | const {args, flags} = await this.parse(StoreData) 21 | 22 | if (args.data) { 23 | 24 | const {web5} = await Web5.connect({password: flags.password, ...web5DefaultConfig}) 25 | const {record} = await web5.dwn.records.create({ 26 | data: args.data, 27 | message: { 28 | dataFormat: 'text/plain', 29 | // published:true 30 | }, 31 | }) 32 | 33 | this.log('Record ID:', record?.id) 34 | 35 | 36 | process.exit(0) 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/config/web5-config.ts: -------------------------------------------------------------------------------- 1 | export const web5CommunityConfig = { 2 | didCreateOptions: { 3 | dwnEndpoints: ['https://dwn.gcda.xyz'] // User provides google's community DWN instance 4 | }, 5 | registration: { 6 | onFailure(error: unknown) { 7 | console.log('Registration failed', error) 8 | // Registration failed, display an error message to the user, and pass in the registration object again to retry next time the user connects. 9 | }, 10 | onSuccess() { 11 | console.log('Registration succeeded') 12 | } 13 | } 14 | } 15 | 16 | export const web5DefaultConfig = {} -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export {run} from '@oclif/core' 2 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "declaration": true, 4 | "module": "Node16", 5 | "outDir": "dist", 6 | "rootDir": "src", 7 | "strict": true, 8 | "target": "es2022", 9 | "moduleResolution": "node16" 10 | }, 11 | "include": ["./src/**/*"], 12 | "ts-node": { 13 | "esm": true 14 | } 15 | } 16 | --------------------------------------------------------------------------------