Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions documentation/schemas/dahr.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<mxfile host="Electron" modified="2024-03-16T18:31:06.762Z" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/22.1.11 Chrome/114.0.5735.289 Electron/25.9.8 Safari/537.36" etag="PiScJdNKES-3hd9LoLwj" version="22.1.11" type="device">
<diagram name="Pagina-1" id="KozjIDIOYP8Ty1yJAhfX">
<mxGraphModel dx="1137" dy="646" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-6" value="Ask for an HTTPS&amp;nbsp;&lt;br&gt;attestation" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="nxRVkFhHDUuOQ-_nWOQr-1" target="nxRVkFhHDUuOQ-_nWOQr-5">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="80" y="140" />
<mxPoint x="80" y="140" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-1" value="User" style="shape=umlActor;verticalLabelPosition=bottom;verticalAlign=top;html=1;outlineConnect=0;" vertex="1" parent="1">
<mxGeometry x="20" y="130" width="30" height="60" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-8" value="Asks for a Data Agnostic HTTPS Relay" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="nxRVkFhHDUuOQ-_nWOQr-5" target="nxRVkFhHDUuOQ-_nWOQr-7">
<mxGeometry relative="1" as="geometry">
<Array as="points">
<mxPoint x="510" y="100" />
<mxPoint x="510" y="100" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-5" value="dApp" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="240" y="70" width="90" height="180" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-7" value="DEMOS&lt;br&gt;SDK" style="shape=card;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="600" y="60" width="120" height="190" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-9" value="Generates a DAHR and send back the details" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=1.033;entryY=0.467;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="600" y="140.94" as="sourcePoint" />
<mxPoint x="332.97" y="139.99999999999994" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-10" value="Uses the DHAR instance to relay HTTPS&lt;br&gt;encrypted data privately and securely" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;entryX=0.013;entryY=0.526;entryDx=0;entryDy=0;entryPerimeter=0;" edge="1" parent="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="330" y="180.1" as="sourcePoint" />
<mxPoint x="601.04" y="180.04000000000005" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-11" value="Receives the HTTPS&lt;br&gt;attestation" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;startArrow=classic;startFill=1;endArrow=none;endFill=0;" edge="1" parent="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="50" y="180" as="sourcePoint" />
<mxPoint x="240" y="180" as="targetPoint" />
<Array as="points">
<mxPoint x="80" y="180" />
<mxPoint x="80" y="180" />
</Array>
</mxGeometry>
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-13" value="Data is exchanged and attested without&lt;br&gt;needs for decryption" style="edgeStyle=orthogonalEdgeStyle;rounded=0;orthogonalLoop=1;jettySize=auto;html=1;exitX=1;exitY=0.75;exitDx=0;exitDy=0;entryX=-0.025;entryY=0.763;entryDx=0;entryDy=0;entryPerimeter=0;startArrow=classic;startFill=1;" edge="1" parent="1">
<mxGeometry relative="1" as="geometry">
<mxPoint x="332" y="220.03" as="sourcePoint" />
<mxPoint x="600" y="220.00000000000003" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-31" value="" style="group" vertex="1" connectable="0" parent="1">
<mxGeometry x="360" y="320" width="200" height="280" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-18" value="" style="swimlane;startSize=0;rounded=1;" vertex="1" parent="nxRVkFhHDUuOQ-_nWOQr-31">
<mxGeometry width="200" height="80" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-16" value="Data Agnostic HTTPS Relay" style="image;aspect=fixed;html=1;points=[];align=center;fontSize=12;image=img/lib/azure2/other/API_Proxy.svg;" vertex="1" parent="nxRVkFhHDUuOQ-_nWOQr-18">
<mxGeometry x="66" y="10" width="68" height="38" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-20" value="" style="swimlane;startSize=0;" vertex="1" parent="nxRVkFhHDUuOQ-_nWOQr-31">
<mxGeometry y="80" width="100" height="200" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-23" value="Proxy Instance" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="nxRVkFhHDUuOQ-_nWOQr-20">
<mxGeometry width="100" height="40" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-28" value="&lt;font style=&quot;font-size: 10px;&quot;&gt;P2P Communication&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="nxRVkFhHDUuOQ-_nWOQr-20">
<mxGeometry x="7" y="50" width="86" height="40" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-29" value="&lt;font style=&quot;font-size: 10px;&quot;&gt;Direct Client-Server Relay&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="nxRVkFhHDUuOQ-_nWOQr-20">
<mxGeometry x="7" y="100" width="86" height="40" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-30" value="&lt;font style=&quot;font-size: 10px;&quot;&gt;Forward Responses&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="nxRVkFhHDUuOQ-_nWOQr-20">
<mxGeometry x="7" y="150" width="86" height="40" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-21" value="" style="swimlane;startSize=0;" vertex="1" parent="nxRVkFhHDUuOQ-_nWOQr-31">
<mxGeometry x="100" y="80" width="100" height="200" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-24" value="Manager" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="nxRVkFhHDUuOQ-_nWOQr-21">
<mxGeometry width="100" height="40" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-25" value="&lt;font style=&quot;font-size: 10px;&quot;&gt;Exposes Proxies&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="nxRVkFhHDUuOQ-_nWOQr-21">
<mxGeometry x="7" y="50" width="86" height="40" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-26" value="&lt;font style=&quot;font-size: 10px;&quot;&gt;Manages Auth&lt;/font&gt;" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="nxRVkFhHDUuOQ-_nWOQr-21">
<mxGeometry x="7" y="100" width="86" height="40" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-27" value="&lt;span style=&quot;font-size: 10px;&quot;&gt;Manages Memory&lt;/span&gt;" style="rounded=1;whiteSpace=wrap;html=1;" vertex="1" parent="nxRVkFhHDUuOQ-_nWOQr-21">
<mxGeometry x="7" y="150" width="86" height="40" as="geometry" />
</mxCell>
<mxCell id="nxRVkFhHDUuOQ-_nWOQr-33" value="DHAR works by creating on-demand HTTPS passthrough proxies that can be used by the client (being it a dApp or anything using DEMOS SDK) to communicate HTTPS requests and having them attested on DEMOS without the network ever decrypting your data." style="text;html=1;strokeColor=none;fillColor=none;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="20" y="320" width="200" height="165" as="geometry" />
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>
Binary file added documentation/schemas/dahr.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@multiversx/sdk-wallet": "^4.3.0",
"@solana/web3.js": "^1.78.0",
"@types/express": "^4.17.17",
"@types/http-proxy": "^1.17.14",
"@types/node-forge": "^1.3.6",
"ace-builds": "^1.31.0",
"argon2": "^0.31.2",
Expand All @@ -69,6 +70,7 @@
"express": "^4.18.2",
"file-saver": "^2.0.5",
"fuse.js": "^6.6.2",
"http-proxy": "^1.18.1",
"https-browserify": "^1.0.0",
"javascript-time-ago": "^2.5.9",
"js-sha256": "^0.9.0",
Expand Down
30 changes: 30 additions & 0 deletions src/features/web2/routines/httpsRelayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* INFO Data agnostic HTTPS relay
* NOTE Workflow:
* A client uses the SDK to request a secure HTTPS relay
* The client must specify the target URL and if the relay is secure (aka if the target is using HTTPS)
* httpsRelayer uses prepareHTTPSRelay to spawn a proxy for the client
* The proxy is returned to the client that uses it to send data to the target through the SDK
* TODO & REVIEW
* The SDK must be able to handle the same format as axios for the request
* The request must be given to the client using the same format as axios
* The client must be able to use the proxy to send data to the target using the same format as axios
* The client must be able to receive the response from the target using the same format as axios
*/

import proxyManager from "./types/proxyManager"

export default class httpsRelayer {
// Preparing a relay to be used by the client
// TODO Add authentication/authorization
static prepareHTTPSRelay(target: string, is_secure: boolean) {
// Random port number between 8500 and 9400
let rnd_port = Math.floor(Math.random() * (9400 - 8500) + 8500)
// We spawn a proxy for this client (NOTE: Is not yet running)
let spawned_proxy = new proxyManager(rnd_port, target, is_secure)
let proxy_id: string = spawned_proxy.proxid
// Run the proxy
spawned_proxy.run()
// Return the port and the id
return [rnd_port, proxy_id]
}
}
28 changes: 28 additions & 0 deletions src/features/web2/routines/operations/httpsPlainRelay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import https from "https"

export interface IHTTPSOptions {
hostname: string
port: number
path: string
method: string
headers: {
"Content-Type": string
"Content-Length": number
}
}

// Relay a https request to the server taking care of handling the options passed
// NOTE Data is received either through HTTP or HTTPS, anyway using this method the server
// is able to read the HTTPS data from the client
export function httpsPlainRelay(options: IHTTPSOptions, data: any) {
var postdata = JSON.stringify(data)
options.headers["Content-Length"] = Buffer.byteLength(postdata)
var req = https.request(options, function (res) {
res.setEncoding("utf8")
res.on("data", function (chunk) {
console.log("Response: " + chunk)
})
})
req.write(postdata)
req.end()
}
117 changes: 117 additions & 0 deletions src/features/web2/routines/types/proxyManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// LINK https://stackoverflow.com/questions/46412934/forward-https-traffic-thru-nginx-without-ssl-certificate
// LINK https://github.com/http-party/node-http-proxy
// LINK https://github.com/http-party/node-http-proxy?tab=readme-ov-file#https---https
import httpProxy from "http-proxy"
import fs from "fs"
import Cryptography from "src/libs/crypto/cryptography"
import Hashing from "src/libs/crypto/hashing"
import sharedState from "src/utilities/sharedState"
import required from "src/utilities/required"

export interface IHTTPSCerts {
key: string
cert: string
}


export default class proxyManager {
// TODO Persist this registry
private static _proxies: Map<string, proxyManager> = new Map()

// Properties
public proxid: string
public port: number
public is_secure: boolean
public target: string
public isOn: boolean = false

// State
private internal_proxy: httpProxy = null

// Constructor for instances
constructor(
port: number,
target: string,
is_secure: boolean = true,
proxid: string = null,
) {
if (proxid) {
this.proxid = proxid
} else {
this.proxid = Math.random().toString(36).substring(7)
}
proxyManager._proxies.set(this.proxid, this)
}

// Singleton logic
static getProxy(id: string): proxyManager {
if (this._proxies.has(id)) {
return this._proxies.get(id)
} else {
throw new Error("Proxy not found")
}
}

// Methods

// NOTE Get the proxy instance and connection status
public state(): { proxy: httpProxy; isOn: boolean } {
return { proxy: this.internal_proxy, isOn: this.isOn }
}

// NOTE Run the proxy with the specified properties
public run(
certs: IHTTPSCerts = {
key: "src/features/web2/routines/certs/key.pem",
cert: "src/features/web2/routines/certs/cert.pem",
},
): httpProxy {
this.internal_proxy = httpProxy
.createServer({
ssl: {
key: fs.readFileSync(certs.key, "utf8"),
cert: fs.readFileSync(certs.cert, "utf8"),
},
target: this.target,
secure: this.is_secure, // Depends on the target
})
.listen(this.port)
console.log(
"Proxy server listening on port " +
this.port.toString() +
" with id " +
this.proxid,
)
// Informing everyone that we are listening
this.isOn = true
// Activate listeners
this.listenersForProxy()
return this.internal_proxy
}

private listenersForProxy() {
required(this.isOn, "Proxy is not running")
// TODO Add listeners for the proxy based on this blurbprint
this.internal_proxy.on("error", (err) => {
console.log("Proxy server error: " + err)
})
this.internal_proxy.on("proxyReq", (proxyReq, req, res, options) => {
console.log("Proxy request")
})
}

// NOTE Stop the proxy
public stop() {
if (this.isOn) {
this.isOn = false
console.log("Stopping proxy server with id " + this.proxid)
this.internal_proxy.close()
proxyManager._proxies.delete(this.proxid)
}
}

// TODO Add methods to manage a chain of communication as comlink

// SECTION Static security methods
// TODO Signing, hashing, etc.
}
Loading