Skip to content

Commit b637550

Browse files
committed
add motoko/filevault
1 parent 850d304 commit b637550

File tree

20 files changed

+3952
-0
lines changed

20 files changed

+3952
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: motoko-filevault
2+
on:
3+
push:
4+
branches:
5+
- master
6+
pull_request:
7+
paths:
8+
- motoko/filevault/**
9+
- .github/workflows/provision-darwin.sh
10+
- .github/workflows/provision-linux.sh
11+
- .github/workflows/motoko-filevault-example.yml
12+
- .ic-commit
13+
concurrency:
14+
group: ${{ github.workflow }}-${{ github.ref }}
15+
cancel-in-progress: true
16+
jobs:
17+
motoko-filevault-darwin:
18+
runs-on: macos-15
19+
steps:
20+
- uses: actions/checkout@v1
21+
- name: Provision Darwin
22+
run: bash .github/workflows/provision-darwin.sh
23+
- name: Motoko Filevault Darwin
24+
run: |
25+
dfx start --background
26+
pushd motoko/filevault
27+
make install
28+
popd
29+
motoko-filevault-linux:
30+
runs-on: ubuntu-22.04
31+
steps:
32+
- uses: actions/checkout@v1
33+
- name: Provision Linux
34+
run: bash .github/workflows/provision-linux.sh
35+
- name: Motoko Filevault Linux
36+
run: |
37+
dfx start --background
38+
pushd motoko/filevault
39+
make install
40+
popd

motoko/filevault/BUILD.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Continue building locally
2+
3+
Projects deployed through ICP Ninja are temporary; they will only be live for 20 minutes before they are removed. The command-line tool `dfx` can be used to continue building your ICP Ninja project locally and deploy it to the mainnet.
4+
5+
To migrate your ICP Ninja project off of the web browser and develop it locally, follow these steps.
6+
7+
### 1. Install developer tools.
8+
9+
You can install the developer tools natively or use Dev Containers.
10+
11+
#### Option 1: Natively install developer tools
12+
13+
> Installing `dfx` natively is currently only supported on macOS and Linux systems. On Windows, it is recommended to use the Dev Containers option.
14+
15+
1. Install `dfx` with the following command:
16+
17+
```
18+
19+
sh -ci "$(curl -fsSL https://internetcomputer.org/install.sh)"
20+
21+
```
22+
23+
> On Apple Silicon (e.g., Apple M1 chip), make sure you have Rosetta installed (`softwareupdate --install-rosetta`).
24+
25+
2. [Install NodeJS](https://nodejs.org/en/download/package-manager).
26+
27+
3. For Rust projects, you will also need to:
28+
29+
- Install [Rust](https://doc.rust-lang.org/cargo/getting-started/installation.html#install-rust-and-cargo): `curl https://sh.rustup.rs -sSf | sh`
30+
31+
- Install [candid-extractor](https://crates.io/crates/candid-extractor): `cargo install candid-extractor`
32+
33+
4. For Motoko projects, you will also need to:
34+
35+
- Install the Motoko package manager [Mops](https://docs.mops.one/quick-start#2-install-mops-cli): `npm i -g ic-mops`
36+
37+
Lastly, navigate into your project's directory that you downloaded from ICP Ninja.
38+
39+
#### Option 2: Dev Containers
40+
41+
Continue building your projects locally by installing the [Dev Container extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers) for VS Code and [Docker](https://docs.docker.com/engine/install/).
42+
43+
Make sure Docker is running, then navigate into your project's directory that you downloaded from ICP Ninja and start the Dev Container by selecting `Dev-Containers: Reopen in Container` in VS Code's command palette (F1 or Ctrl/Cmd+Shift+P).
44+
45+
> Note that local development ports (e.g. the ports used by `dfx` or `vite`) are forwarded from the Dev Container to your local machine. In the VS code terminal, use Ctrl/Cmd+Click on the displayed local URLs to open them in your browser. To view the current port mappings, click the "Ports" tab in the VS Code terminal window.
46+
47+
### 2. Create a local developer identity.
48+
49+
To manage your project's canisters, it is recommended that you create a local [developer identity](https://internetcomputer.org/docs/building-apps/getting-started/identities) rather than use the `dfx` default identity that is not stored securely.
50+
51+
To create a new identity, run the commands:
52+
53+
```
54+
55+
dfx start --background
56+
57+
dfx identity new IDENTITY_NAME
58+
59+
dfx identity use IDENTITY_NAME
60+
61+
```
62+
63+
Replace `IDENTITY_NAME` with your preferred identity name. The first command `dfx start --background` starts the local `dfx` processes, then `dfx identity new` will create a new identity and return your identity's seed phase. Be sure to save this in a safe, secure location.
64+
65+
The third command `dfx identity use` will tell `dfx` to use your new identity as the active identity. Any canister smart contracts created after running `dfx identity use` will be owned and controlled by the active identity.
66+
67+
Your identity will have a principal ID associated with it. Principal IDs are used to identify different entities on ICP, such as users and canisters.
68+
69+
[Learn more about ICP developer identities](https://internetcomputer.org/docs/building-apps/getting-started/identities).
70+
71+
### 3. Deploy the project locally.
72+
73+
Deploy your project to your local developer environment with:
74+
75+
```
76+
npm install
77+
dfx deploy
78+
79+
```
80+
81+
Your project will be hosted on your local machine. The local canister URLs for your project will be shown in the terminal window as output of the `dfx deploy` command. You can open these URLs in your web browser to view the local instance of your project.
82+
83+
### 4. Obtain cycles.
84+
85+
To deploy your project to the mainnet for long-term public accessibility, first you will need [cycles](https://internetcomputer.org/docs/building-apps/getting-started/tokens-and-cycles). Cycles are used to pay for the resources your project uses on the mainnet, such as storage and compute.
86+
87+
> This cost model is known as ICP's [reverse gas model](https://internetcomputer.org/docs/building-apps/essentials/gas-cost), where developers pay for their project's gas fees rather than users pay for their own gas fees. This model provides an enhanced end user experience since they do not need to hold tokens or sign transactions when using a dapp deployed on ICP.
88+
89+
> Learn how much a project may cost by using the [pricing calculator](https://internetcomputer.org/docs/building-apps/essentials/cost-estimations-and-examples).
90+
91+
Cycles can be obtained through [converting ICP tokens into cycles using `dfx`](https://internetcomputer.org/docs/building-apps/developer-tools/dfx/dfx-cycles#dfx-cycles-convert).
92+
93+
### 5. Deploy to the mainnet.
94+
95+
Once you have cycles, run the command:
96+
97+
```
98+
99+
dfx deploy --network ic
100+
101+
```
102+
103+
After your project has been deployed to the mainnet, it will continuously require cycles to pay for the resources it uses. You will need to [top up](https://internetcomputer.org/docs/building-apps/canister-management/topping-up) your project's canisters or set up automatic cycles management through a service such as [CycleOps](https://cycleops.dev/).
104+
105+
> If your project's canisters run out of cycles, they will be removed from the network.
106+
107+
## Additional examples
108+
109+
Additional code examples and sample applications can be found in the [DFINITY examples repo](https://github.com/dfinity/examples).

motoko/filevault/Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.PHONY: all
2+
all: build
3+
4+
.PHONY: node_modules
5+
.SILENT: node_modules
6+
node_modules:
7+
npm install
8+
9+
.PHONY: build
10+
.SILENT: build
11+
build: node_modules
12+
dfx canister create --all
13+
dfx build
14+
15+
.PHONY: install
16+
.SILENT: install
17+
install: build
18+
dfx canister install --all
19+
20+
.PHONY: upgrade
21+
.SILENT: upgrade
22+
upgrade: build
23+
dfx canister install --all --mode=upgrade
24+
25+
.PHONY: clean
26+
.SILENT: clean
27+
clean:
28+
rm -fr .dfx

motoko/filevault/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# FileVault
2+
3+
![FileVault](filevault.png)
4+
5+
FileVault is a file storage application that allows you to upload files from your local computer and store them onchain. FileVault uses Internet Identity (II) for user login and authentication. Once files are uploaded, they can be downloaded at a later time, or they can be deleted.
6+
7+
This application's logic is written in [Motoko](https://internetcomputer.org/docs/motoko/main/getting-started/motoko-introduction), a programming language designed specifically for developing canisters on ICP.
8+
9+
## Project structure
10+
11+
The `/backend` folder contains the Motoko canister, `app.mo`. The `/frontend` folder contains web assets for the application's user interface. The user interface is written using the React framework. Edit the `mops.toml` file to add [Motoko dependencies](https://mops.one/) to the project.
12+
13+
## Deploying from ICP Ninja
14+
15+
When viewing this project in ICP Ninja, you can deploy it directly to the mainnet for free by clicking "Deploy" in the upper right corner.
16+
To open this project in ICP Ninja, click [here](https://icp.ninja/i?url=https://github.com/dfinity/examples/tree/master/motoko/filevault).
17+
18+
To **download** or **reset** the project files, click the menu option next to the deploy button.
19+
20+
## Build and deploy from the command-line
21+
22+
To migrate your ICP Ninja project off of the web browser and develop it locally, follow these steps. These steps are necessary if you want to deploy this project for long-term, production use on the mainnet.
23+
24+
### 1. Download your project from ICP Ninja using the 'Download files' button on the upper left corner under the pink ninja star icon.
25+
26+
### 2. Open the `BUILD.md` file for further instructions.

motoko/filevault/backend/app.mo

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import Bool "mo:base/Bool";
2+
import Array "mo:base/Array";
3+
import Blob "mo:base/Blob";
4+
import HashMap "mo:map/Map";
5+
import { phash; thash } "mo:map/Map";
6+
import Iter "mo:base/Iter";
7+
import Nat "mo:base/Nat";
8+
import Principal "mo:base/Principal";
9+
import Text "mo:base/Text";
10+
import Option "mo:base/Option";
11+
12+
persistent actor Filevault {
13+
14+
// Define a data type for a file's chunks.
15+
type FileChunk = {
16+
chunk : Blob;
17+
index : Nat;
18+
};
19+
20+
// Define a data type for a file's data.
21+
type File = {
22+
name : Text;
23+
chunks : [FileChunk];
24+
totalSize : Nat;
25+
fileType : Text;
26+
};
27+
28+
// Define a data type for storing files associated with a user principal.
29+
type UserFiles = HashMap.Map<Text, File>;
30+
31+
// HashMap to store the user data
32+
private var files = HashMap.new<Principal, UserFiles>();
33+
34+
// Return files associated with a user's principal.
35+
private func getUserFiles(user : Principal) : UserFiles {
36+
switch (HashMap.get(files, phash, user)) {
37+
case null {
38+
let newFileMap = HashMap.new<Text, File>();
39+
let _ = HashMap.put(files, phash, user, newFileMap);
40+
newFileMap;
41+
};
42+
case (?existingFiles) existingFiles;
43+
};
44+
};
45+
46+
// Check if a file name already exists for the user.
47+
public shared (msg) func checkFileExists(name : Text) : async Bool {
48+
Option.isSome(HashMap.get(getUserFiles(msg.caller), thash, name));
49+
};
50+
51+
// Upload a file in chunks.
52+
public shared (msg) func uploadFileChunk(name : Text, chunk : Blob, index : Nat, fileType : Text) : async () {
53+
let userFiles = getUserFiles(msg.caller);
54+
let fileChunk = { chunk = chunk; index = index };
55+
56+
switch (HashMap.get(userFiles, thash, name)) {
57+
case null {
58+
let _ = HashMap.put(userFiles, thash, name, { name = name; chunks = [fileChunk]; totalSize = chunk.size(); fileType = fileType });
59+
};
60+
case (?existingFile) {
61+
let updatedChunks = Array.append(existingFile.chunks, [fileChunk]);
62+
let _ = HashMap.put(
63+
userFiles,
64+
thash,
65+
name,
66+
{
67+
name = name;
68+
chunks = updatedChunks;
69+
totalSize = existingFile.totalSize + chunk.size();
70+
fileType = fileType;
71+
}
72+
);
73+
};
74+
};
75+
};
76+
77+
// Return list of files for a user.
78+
public shared (msg) func getFiles() : async [{ name : Text; size : Nat; fileType : Text }] {
79+
Iter.toArray(
80+
Iter.map(
81+
HashMap.vals(getUserFiles(msg.caller)),
82+
func(file : File) : { name : Text; size : Nat; fileType : Text } {
83+
{
84+
name = file.name;
85+
size = file.totalSize;
86+
fileType = file.fileType;
87+
};
88+
}
89+
)
90+
);
91+
};
92+
93+
// Return total chunks for a file
94+
public shared (msg) func getTotalChunks(name : Text) : async Nat {
95+
switch (HashMap.get(getUserFiles(msg.caller), thash, name)) {
96+
case null 0;
97+
case (?file) file.chunks.size();
98+
};
99+
};
100+
101+
// Return specific chunk for a file.
102+
public shared (msg) func getFileChunk(name : Text, index : Nat) : async ?Blob {
103+
switch (HashMap.get(getUserFiles(msg.caller), thash, name)) {
104+
case null null;
105+
case (?file) {
106+
switch (Array.find(file.chunks, func(chunk : FileChunk) : Bool { chunk.index == index })) {
107+
case null null;
108+
case (?foundChunk) ?foundChunk.chunk;
109+
};
110+
};
111+
};
112+
};
113+
114+
// Get file's type.
115+
public shared (msg) func getFileType(name : Text) : async ?Text {
116+
switch (HashMap.get(getUserFiles(msg.caller), thash, name)) {
117+
case null null;
118+
case (?file) ?file.fileType;
119+
};
120+
};
121+
122+
// Delete a file.
123+
public shared (msg) func deleteFile(name : Text) : async Bool {
124+
Option.isSome(HashMap.remove(getUserFiles(msg.caller), thash, name));
125+
};
126+
};

motoko/filevault/devcontainer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "ICP Dev Environment",
3+
"image": "ghcr.io/dfinity/icp-dev-env-slim:17",
4+
"forwardPorts": [4943, 5173],
5+
"portsAttributes": {
6+
"4943": {
7+
"label": "dfx",
8+
"onAutoForward": "ignore"
9+
},
10+
"5173": {
11+
"label": "vite",
12+
"onAutoForward": "openBrowser"
13+
}
14+
},
15+
"customizations": {
16+
"vscode": {
17+
"extensions": ["dfinity-foundation.vscode-motoko"]
18+
}
19+
}
20+
}

motoko/filevault/dfx.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"canisters": {
3+
"backend": {
4+
"main": "backend/app.mo",
5+
"type": "motoko",
6+
"args": "--enhanced-orthogonal-persistence"
7+
},
8+
"frontend": {
9+
"dependencies": ["backend"],
10+
"frontend": {
11+
"entrypoint": "frontend/index.html"
12+
},
13+
"source": ["frontend/dist"],
14+
"type": "assets"
15+
},
16+
"internet_identity": {
17+
"candid": "https://github.com/dfinity/internet-identity/releases/latest/download/internet_identity.did",
18+
"type": "custom",
19+
"specified_id": "rdmx6-jaaaa-aaaaa-aaadq-cai",
20+
"remote": {
21+
"id": {
22+
"ic": "rdmx6-jaaaa-aaaaa-aaadq-cai"
23+
}
24+
},
25+
"wasm": "https://github.com/dfinity/internet-identity/releases/latest/download/internet_identity_dev.wasm.gz"
26+
}
27+
},
28+
"output_env_file": ".env",
29+
"defaults": {
30+
"build": {
31+
"packtool": "mops sources"
32+
}
33+
}
34+
}

motoko/filevault/filevault.png

464 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;

0 commit comments

Comments
 (0)