Skip to content

Commit

Permalink
Merge pull request #1 from FileOnchain/ft/addjsscr
Browse files Browse the repository at this point in the history
Add some js and fix Dockerfile
  • Loading branch information
marc-aurele-besner authored Sep 1, 2024
2 parents b49a942 + 689c0f8 commit ad5fa5e
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 16 deletions.
10 changes: 8 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
29 changes: 19 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions assets/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
14 changes: 14 additions & 0 deletions assets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
17 changes: 14 additions & 3 deletions lib/fileonchain_web/live/file_live/form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,31 @@ 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
notify_parent({:saved, file})

{: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

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 22 additions & 0 deletions src/sendRemark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { connectToPolkadot, sendRemark } from './sendTransaction';

async function main(): Promise<void> {
const [senderSeed, hash] = process.argv.slice(2);

if (!senderSeed || !hash) {
console.error('Usage: node dist/sendRemark.js <senderSeed> <hash>');
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();
17 changes: 17 additions & 0 deletions src/sendTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ApiPromise, WsProvider, Keyring } from '@polkadot/api';

export async function connectToPolkadot(): Promise<ApiPromise> {
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<string> {
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();
}

0 comments on commit ad5fa5e

Please sign in to comment.