diff --git a/.gitignore b/.gitignore index 97e06f7..df61304 100644 --- a/.gitignore +++ b/.gitignore @@ -35,8 +35,14 @@ fileonchain-*.tar npm-debug.log /assets/node_modules/ +# Root node_modules +node_modules + +# Ignore dist +dist + # Db hasura setup -/db/node_modules/ +/db/node_modules -# SSH keys +# SSH Keys .ssh/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 056c547..0bef547 100644 --- a/Dockerfile +++ b/Dockerfile @@ -43,13 +43,18 @@ RUN mkdir config # to ensure any relevant config change will trigger the dependencies # to be re-compiled. COPY config/config.exs config/${MIX_ENV}.exs config/ -RUN mix deps.compile -COPY priv priv +# Install rustup and set the toolchain +RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \ + . $HOME/.cargo/env -COPY lib lib +# Add .cargo/bin to PATH +ENV PATH="/root/.cargo/bin:${PATH}" -COPY assets assets +RUN rustup install stable +RUN rustup default stable +RUN rustup target add wasm32-unknown-unknown +RUN rustup target add x86_64-unknown-linux-gnu # Install nvm ENV NVM_DIR /usr/local/nvm @@ -69,12 +74,16 @@ ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH RUN node --version RUN npm --version -# Install rustup and set the toolchain -RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \ - source $HOME/.cargo/env && \ - rustup install nightly-2022-07-24 && \ - rustup default nightly-2022-07-24 && \ - rustup target add wasm32-unknown-unknown +# Compile deps +# RUN mix deps.compile blake3 --force +# RUN mix deps.compile + +COPY priv priv + +COPY lib lib + +COPY assets assets + # compile assets RUN mix assets.deploy diff --git a/assets/package.json b/assets/package.json new file mode 100644 index 0000000..315f51d --- /dev/null +++ b/assets/package.json @@ -0,0 +1,27 @@ +{ + "name": "fileonchain", + "version": "1.0.0", + "description": "To start the server:", + "main": "index.js", + "directories": { + "lib": "lib", + "test": "test" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "build": "tsc", + "watch": "tsc -w" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "@autonomys/auto-consensus": "^0.4.0", + "@autonomys/auto-utils": "^0.4.0", + "@polkadot/api": "^10.9.1" + }, + "devDependencies": { + "@types/node": "^20.3.1", + "typescript": "^5.1.3" + } +} diff --git a/assets/tsconfig.json b/assets/tsconfig.json new file mode 100644 index 0000000..14659de --- /dev/null +++ b/assets/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "ES2020", + "module": "commonjs", + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + "outDir": "./dist", + "rootDir": "." + }, + "include": ["src/**/*"], + "exclude": ["node_modules"] +} \ No newline at end of file diff --git a/lib/fileonchain_web/live/file_live/form_component.ex b/lib/fileonchain_web/live/file_live/form_component.ex index ce31a90..673f26d 100644 --- a/lib/fileonchain_web/live/file_live/form_component.ex +++ b/lib/fileonchain_web/live/file_live/form_component.ex @@ -101,7 +101,18 @@ defmodule FileonchainWeb.FileLive.FormComponent do chunks_results = Enum.map(chunks, fn chunk -> hash = Blake3.hash(chunk) |> Base.encode16(case: :lower) - Fileonchain.Chunks.create_chunk(%{hash: hash, cid: "dummy_cid", data: chunk}) + case Fileonchain.Chunks.create_chunk(%{hash: hash, cid: "dummy_cid", data: chunk}) do + {:ok, _chunk} -> + # Send remark transaction + sender_seed = System.get_env("POLKADOT_SENDER_SEED") || "//Alice" + {tx_hash, exit_code} = System.cmd("node", ["dist/sendRemark.js", sender_seed, hash]) + if exit_code == 0 do + {:ok, String.trim(tx_hash)} + else + {:error, "Failed to send remark: #{String.trim(tx_hash)}"} + end + error -> error + end end) if Enum.all?(chunks_results, fn {:ok, _} -> true; _ -> false end) do @@ -109,12 +120,12 @@ defmodule FileonchainWeb.FileLive.FormComponent do {:noreply, socket - |> put_flash(:info, "File and Chunks created successfully") + |> put_flash(:info, "File and Chunks created successfully, remarks sent to Polkadot") |> push_patch(to: socket.assigns.patch)} else {:noreply, socket - |> put_flash(:error, "File created but failed to create some Chunks") + |> put_flash(:error, "File created but failed to create some Chunks or send remarks") |> push_patch(to: socket.assigns.patch)} end diff --git a/mix.exs b/mix.exs index 4b33301..b40d568 100644 --- a/mix.exs +++ b/mix.exs @@ -61,7 +61,7 @@ defmodule Fileonchain.MixProject do {:jason, "~> 1.4.4"}, {:dns_cluster, "~> 0.1.3"}, {:bandit, "~> 1.5.7"}, - {:blake3, "~> 1.0"} + {:blake3, "~> 1.0.0"} ] end diff --git a/src/sendRemark.ts b/src/sendRemark.ts new file mode 100644 index 0000000..86ddd87 --- /dev/null +++ b/src/sendRemark.ts @@ -0,0 +1,22 @@ +import { connectToPolkadot, sendRemark } from './sendTransaction'; + +async function main(): Promise { + const [senderSeed, hash] = process.argv.slice(2); + + if (!senderSeed || !hash) { + console.error('Usage: node dist/sendRemark.js '); + process.exit(1); + } + + try { + const api = await connectToPolkadot(); + const txHash = await sendRemark(api, senderSeed, hash); + console.log(txHash); + process.exit(0); + } catch (error) { + console.error('Error:', error); + process.exit(1); + } +} + +main(); \ No newline at end of file diff --git a/src/sendTransaction.ts b/src/sendTransaction.ts new file mode 100644 index 0000000..9974f68 --- /dev/null +++ b/src/sendTransaction.ts @@ -0,0 +1,17 @@ +import { ApiPromise, WsProvider, Keyring } from '@polkadot/api'; + +export async function connectToPolkadot(): Promise { + const wsProvider = new WsProvider('wss://rpc.polkadot.io'); + const api = await ApiPromise.create({ provider: wsProvider }); + return api; +} + +export async function sendRemark(api: ApiPromise, senderSeed: string, hash: string): Promise { + const keyring = new Keyring({ type: 'sr25519' }); + const sender = keyring.addFromUri(senderSeed); + + const remark = api.tx.system.remark(hash); + const txHash = await remark.signAndSend(sender); + + return txHash.toHex(); +} \ No newline at end of file