Skip to content

Commit ad5fa5e

Browse files
Merge pull request #1 from FileOnchain/ft/addjsscr
Add some js and fix Dockerfile
2 parents b49a942 + 689c0f8 commit ad5fa5e

File tree

8 files changed

+122
-16
lines changed

8 files changed

+122
-16
lines changed

.gitignore

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ fileonchain-*.tar
3535
npm-debug.log
3636
/assets/node_modules/
3737

38+
# Root node_modules
39+
node_modules
40+
41+
# Ignore dist
42+
dist
43+
3844
# Db hasura setup
39-
/db/node_modules/
45+
/db/node_modules
4046

41-
# SSH keys
47+
# SSH Keys
4248
.ssh/

Dockerfile

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,18 @@ RUN mkdir config
4343
# to ensure any relevant config change will trigger the dependencies
4444
# to be re-compiled.
4545
COPY config/config.exs config/${MIX_ENV}.exs config/
46-
RUN mix deps.compile
4746

48-
COPY priv priv
47+
# Install rustup and set the toolchain
48+
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
49+
. $HOME/.cargo/env
4950

50-
COPY lib lib
51+
# Add .cargo/bin to PATH
52+
ENV PATH="/root/.cargo/bin:${PATH}"
5153

52-
COPY assets assets
54+
RUN rustup install stable
55+
RUN rustup default stable
56+
RUN rustup target add wasm32-unknown-unknown
57+
RUN rustup target add x86_64-unknown-linux-gnu
5358

5459
# Install nvm
5560
ENV NVM_DIR /usr/local/nvm
@@ -69,12 +74,16 @@ ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
6974
RUN node --version
7075
RUN npm --version
7176

72-
# Install rustup and set the toolchain
73-
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
74-
source $HOME/.cargo/env && \
75-
rustup install nightly-2022-07-24 && \
76-
rustup default nightly-2022-07-24 && \
77-
rustup target add wasm32-unknown-unknown
77+
# Compile deps
78+
# RUN mix deps.compile blake3 --force
79+
# RUN mix deps.compile
80+
81+
COPY priv priv
82+
83+
COPY lib lib
84+
85+
COPY assets assets
86+
7887

7988
# compile assets
8089
RUN mix assets.deploy

assets/package.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "fileonchain",
3+
"version": "1.0.0",
4+
"description": "To start the server:",
5+
"main": "index.js",
6+
"directories": {
7+
"lib": "lib",
8+
"test": "test"
9+
},
10+
"scripts": {
11+
"test": "echo \"Error: no test specified\" && exit 1",
12+
"build": "tsc",
13+
"watch": "tsc -w"
14+
},
15+
"keywords": [],
16+
"author": "",
17+
"license": "ISC",
18+
"dependencies": {
19+
"@autonomys/auto-consensus": "^0.4.0",
20+
"@autonomys/auto-utils": "^0.4.0",
21+
"@polkadot/api": "^10.9.1"
22+
},
23+
"devDependencies": {
24+
"@types/node": "^20.3.1",
25+
"typescript": "^5.1.3"
26+
}
27+
}

assets/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "commonjs",
5+
"strict": true,
6+
"esModuleInterop": true,
7+
"skipLibCheck": true,
8+
"forceConsistentCasingInFileNames": true,
9+
"outDir": "./dist",
10+
"rootDir": "."
11+
},
12+
"include": ["src/**/*"],
13+
"exclude": ["node_modules"]
14+
}

lib/fileonchain_web/live/file_live/form_component.ex

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,31 @@ defmodule FileonchainWeb.FileLive.FormComponent do
101101
chunks_results =
102102
Enum.map(chunks, fn chunk ->
103103
hash = Blake3.hash(chunk) |> Base.encode16(case: :lower)
104-
Fileonchain.Chunks.create_chunk(%{hash: hash, cid: "dummy_cid", data: chunk})
104+
case Fileonchain.Chunks.create_chunk(%{hash: hash, cid: "dummy_cid", data: chunk}) do
105+
{:ok, _chunk} ->
106+
# Send remark transaction
107+
sender_seed = System.get_env("POLKADOT_SENDER_SEED") || "//Alice"
108+
{tx_hash, exit_code} = System.cmd("node", ["dist/sendRemark.js", sender_seed, hash])
109+
if exit_code == 0 do
110+
{:ok, String.trim(tx_hash)}
111+
else
112+
{:error, "Failed to send remark: #{String.trim(tx_hash)}"}
113+
end
114+
error -> error
115+
end
105116
end)
106117

107118
if Enum.all?(chunks_results, fn {:ok, _} -> true; _ -> false end) do
108119
notify_parent({:saved, file})
109120

110121
{:noreply,
111122
socket
112-
|> put_flash(:info, "File and Chunks created successfully")
123+
|> put_flash(:info, "File and Chunks created successfully, remarks sent to Polkadot")
113124
|> push_patch(to: socket.assigns.patch)}
114125
else
115126
{:noreply,
116127
socket
117-
|> put_flash(:error, "File created but failed to create some Chunks")
128+
|> put_flash(:error, "File created but failed to create some Chunks or send remarks")
118129
|> push_patch(to: socket.assigns.patch)}
119130
end
120131

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ defmodule Fileonchain.MixProject do
6161
{:jason, "~> 1.4.4"},
6262
{:dns_cluster, "~> 0.1.3"},
6363
{:bandit, "~> 1.5.7"},
64-
{:blake3, "~> 1.0"}
64+
{:blake3, "~> 1.0.0"}
6565
]
6666
end
6767

src/sendRemark.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { connectToPolkadot, sendRemark } from './sendTransaction';
2+
3+
async function main(): Promise<void> {
4+
const [senderSeed, hash] = process.argv.slice(2);
5+
6+
if (!senderSeed || !hash) {
7+
console.error('Usage: node dist/sendRemark.js <senderSeed> <hash>');
8+
process.exit(1);
9+
}
10+
11+
try {
12+
const api = await connectToPolkadot();
13+
const txHash = await sendRemark(api, senderSeed, hash);
14+
console.log(txHash);
15+
process.exit(0);
16+
} catch (error) {
17+
console.error('Error:', error);
18+
process.exit(1);
19+
}
20+
}
21+
22+
main();

src/sendTransaction.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { ApiPromise, WsProvider, Keyring } from '@polkadot/api';
2+
3+
export async function connectToPolkadot(): Promise<ApiPromise> {
4+
const wsProvider = new WsProvider('wss://rpc.polkadot.io');
5+
const api = await ApiPromise.create({ provider: wsProvider });
6+
return api;
7+
}
8+
9+
export async function sendRemark(api: ApiPromise, senderSeed: string, hash: string): Promise<string> {
10+
const keyring = new Keyring({ type: 'sr25519' });
11+
const sender = keyring.addFromUri(senderSeed);
12+
13+
const remark = api.tx.system.remark(hash);
14+
const txHash = await remark.signAndSend(sender);
15+
16+
return txHash.toHex();
17+
}

0 commit comments

Comments
 (0)