Skip to content

Commit

Permalink
Backend compiler and Frontend Setup (keep-starknet-strange#177)
Browse files Browse the repository at this point in the history
<!-- enter the gh issue after hash -->

- [ ] issue #
- [ ] follows contribution
[guide](https://github.com/keep-starknet-strange/shinigami/blob/main/CONTRIBUTING.md)
- [ ] code change includes tests

<!-- PR description below -->

Created a simple nodejs script with Express.js to collect two arguments
via post requests

Also I setup the frontend to use ```swr``` to fetch mock data and
simulate how clicking run script will work

Made Pub key to be the default editor when editor is not split and sig
key to be the top editor when split

---------

Co-authored-by: Brandon Roberts <[email protected]>
  • Loading branch information
supreme2580 and b-j-roberts authored Aug 29, 2024
1 parent fe1c0d6 commit 1833214
Show file tree
Hide file tree
Showing 11 changed files with 2,145 additions and 2,414 deletions.
22 changes: 22 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM node:20.5.0

SHELL ["bash", "-c"]
RUN curl https://get.starkli.sh | sh && \
source $HOME/.starkli/env && \
starkliup && \
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.14.1 && \
. "$HOME/.asdf/asdf.sh" && \
. "$HOME/.asdf/completions/asdf.bash" && \
asdf plugin add scarb && \
asdf install scarb latest && \
asdf global scarb latest

WORKDIR /backend

COPY ./index.js ./package.json ./

RUN npm i

EXPOSE 3000

CMD ["node", "index.js"]
10 changes: 10 additions & 0 deletions backend/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
web:
build: .
ports:
- "8000:3000"
develop:
watch:
- action: sync
path: .
target: /backend
64 changes: 64 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const express = require('express');
const { exec } = require('child_process');
const app = express();
const cors = require('cors')

const MAX_SIZE = 350000; // Max script size is 10000 bytes, longest named opcode is ~25 chars, so 25 * 10000 = 250000 + extra allowance

function runShellCommand(command, callback) {
exec(command, (error, stdout, stderr) => {
if (error) {
callback(`Error: ${error.message}`);
return;
}
if (stderr) {
callback(stderr);
return;
}
callback(stdout);
});
}

function extractStack(output) {
const match = output.match(/\[.*\]/);
return match ? match[0] : 'No message found';
}

app.use(cors());
app.use(express.json());

app.post('/run-script', (req, res) => {
const { pub_key, sig } = req.body;
if (!pub_key) {
return res.status(400).send('Missing public key parameter');
}
if (pub_key.length > MAX_SIZE || sig.length > MAX_SIZE) {
//for a more detailed error message
if (pub_key.length > MAX_SIZE) {
return res.status(400).send('Script Public Key exceeds maximum allowed size');
}
if (sig.length > MAX_SIZE) {
return res.status(400).send('Script Signature exceeds maximum allowed size');
}
}
const scriptPath = '../tests/text_to_byte_array.sh';
const sigCommand = `bash ${scriptPath} "${sig}"`;
const pubCommand = `bash ${scriptPath} "${pub_key}"`;
runShellCommand(sigCommand, (sigOutput) => {
runShellCommand(pubCommand, (pubOutput) => {
const modifiedSigOutput = sigOutput.trim().slice(1, -1);
const modifiedPubOutput = pubOutput.trim().slice(1, -1);
const combinedOutput = `[${modifiedSigOutput},${modifiedPubOutput}]`;
const cairoCommand = `scarb cairo-run ${combinedOutput} --no-build`;
runShellCommand(cairoCommand, (finalOutput) => {
const message = extractStack(finalOutput);
res.json({ message });
});
});
});
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
19 changes: 19 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "backend",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"@types/node": "^22.5.0"
},
"dependencies": {
"cors": "^2.8.5",
"express": "^4.19.2"
}
}
Empty file removed frontend/global.d.ts
Empty file.
Loading

0 comments on commit 1833214

Please sign in to comment.