Skip to content

Commit

Permalink
server: add createDpid route and controller
Browse files Browse the repository at this point in the history
  • Loading branch information
m0ar committed May 31, 2024
1 parent 67c1664 commit 30ebea7
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion desci-server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ server.log

repo-tmp

queries.sql
queries.sql
1 change: 1 addition & 0 deletions desci-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@automerge/automerge-repo": "^1.0.19",
"@automerge/automerge-repo-network-websocket": "^1.0.19",
"@aws-sdk/client-s3": "^3.537.0",
"@desci-labs/desci-contracts": "0.2.5-rc3",
"@desci-labs/desci-models": "link:./desci-models",
"@honeycombio/opentelemetry-node": "^0.3.2",
"@ipld/dag-pb": "^4.0.0",
Expand Down
99 changes: 99 additions & 0 deletions desci-server/src/controllers/nodes/createDpid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { Response } from "express";
import { ethers } from "ethers";
import { logger as parentLogger } from '../../logger.js';
import { RequestWithNode } from "../../middleware/authorisation.js";
import { contracts, typechain as tc } from "@desci-labs/desci-contracts";
import { DpidMintedEvent } from "@desci-labs/desci-contracts/dist/typechain-types/DpidAliasRegistry.js";

type DpidResponse = DpidSuccessResponse | DpidErrorResponse;
export type DpidSuccessResponse = {
dpid: number;
};

export type DpidErrorResponse = {
error: string;
};

/** Not secret: pre-seeded ganache account for local dev */
const GANACHE_PKEY = "59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d";

let aliasRegistryAddress: string;
const url = process.env.SERVER_URL;

if (url.includes("localhost")) {
aliasRegistryAddress = contracts.localDpidAliasInfo.proxies.at(0).address;
} else if (url.includes("dev") || url.includes("staging")) {
aliasRegistryAddress = contracts.devDpidAliasInfo.proxies.at(0).address;
} else if (process.env.NODE_ENV === "production") {
aliasRegistryAddress = contracts.prodDpidAliasInfo.proxies.at(0).address;
};

export const createDpid = async (req: RequestWithNode, res: Response<DpidResponse>) => {
const owner = req.user;
const node = req.node;
const { uuid } = req.body;

const logger = parentLogger.child({
module: "NODE::createDpidController",
body: req.body,
uuid,
user: owner,
ceramicStream: node.ceramicStream,
});

if (!uuid) {
return res.status(400).json({ error: "UUID is required" });
};

if (!process.env.HOT_WALLET_KEY) {
logger.error("hot wallet not configured");
return res.status(500).json({ error: "dpid registration not available" });
};

if (!process.env.ETHEREUM_RPC_URL) {
logger.error("ethereum RPC endpoint not configured");
return res.status(500).json({ error: "dpid registration not available" });
};

try {
debugger;
const provider = new ethers.providers.JsonRpcProvider(
process.env.ETHEREUM_RPC_URL
);

await provider.ready;
const wallet = new ethers.Wallet(
url.includes("localhost") ? GANACHE_PKEY : process.env.HOT_WALLET_KEY,
provider,
);

const dpidAliasRegistry = tc.DpidAliasRegistry__factory.connect(
aliasRegistryAddress,
wallet,
);

const derp = await dpidAliasRegistry.owner();
console.log("owner:", derp);
const hasDpid = await dpidAliasRegistry.find(node.ceramicStream);

if (ethers.BigNumber.from(hasDpid).toNumber() !== 0) {
return res.status(400).json({
error: `stream already has dPID: ${node.ceramicStream}`,
});
};

const tx = await dpidAliasRegistry.mintDpid(node.ceramicStream);
const receipt = await tx.wait();
const { args: [dpidBn, streamID] }= receipt.events[0] as DpidMintedEvent;
const dpid = ethers.BigNumber.from(dpidBn).toNumber();

logger.info(
`Created dPID alias ${dpid} for stream ${streamID}`,
);

return res.status(200).send({ dpid });
} catch (err) {
logger.error({ err }, "node-create-dpid-err");
return res.status(400).send({ error: err.message });
};
};
2 changes: 2 additions & 0 deletions desci-server/src/routes/v1/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ import { versionDetails } from '../../controllers/nodes/versionDetails.js';
import { asyncHander, attachUser, validate } from '../../internal.js';
import { ensureNodeAccess, ensureWriteNodeAccess } from '../../middleware/authorisation.js';
import { ensureUser } from '../../middleware/permissions.js';
import { createDpid } from '../../controllers/nodes/createDpid.js';

const router = Router();

router.post('/prepublish', [ensureUser, ensureNodeAccess], prepublish);
router.post('/publish', [ensureUser], publish);
router.post('/createDpid', [ensureUser, ensureWriteNodeAccess], createDpid);
router.post('/createDraft', [ensureUser], draftCreate);
// is this api deprecated?
router.post('/addComponentToDraft', [ensureUser], draftAddComponent);
Expand Down
5 changes: 5 additions & 0 deletions desci-server/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,11 @@
dependencies:
"@jridgewell/trace-mapping" "0.3.9"

"@desci-labs/desci-contracts@0.2.5-rc3":
version "0.2.5-rc3"
resolved "https://registry.yarnpkg.com/@desci-labs/desci-contracts/-/desci-contracts-0.2.5-rc3.tgz#bd9f0b221bf4df295855a50853f35161ce6d066e"
integrity sha512-+b0TTZCZjceCw5DuVqCxu9LlkHBMvF5PHw2Q/5N+cuveb2YMtPcj+TlVAEfsi+HcgAiagqaUKWI9903Kf8X1IA==

"@desci-labs/desci-models@link:./desci-models":
version "0.0.0"
uid ""
Expand Down

0 comments on commit 30ebea7

Please sign in to comment.