-
-
Save 0foxes/c1c6669b7c35416993e2238930e849cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Merkletree } from "@jackallabs/dogwood-tree"; | |
// Builds dogwood tree from file | |
// react ex: | |
// const [file, setFile] = useState<File>(new File([""], "")); | |
const tree = await Merkletree.grow({ | |
seed: await file.arrayBuffer(), | |
chunkSize: 10240, | |
preserve: false, | |
}); | |
const root = tree.getRootAsHex(); | |
// Create a new WebSocket connection | |
const socket = new WebSocket("wss://rpc.jackalprotocol.com/websocket"); | |
// Open websocket connection to RPC node | |
socket.addEventListener("open", (event) => { | |
const subscriptionMessage = JSON.stringify({ | |
jsonrpc: "2.0", | |
method: "subscribe", | |
id: "1", | |
params: { | |
query: `tm.event='Tx' AND post_file.file='${root}'`, | |
}, | |
}); | |
socket.send(subscriptionMessage); | |
}); | |
// Listen for the EVM chain -> Jackal tx | |
socket.addEventListener("message", async (event) => { | |
const data = JSON.parse(event.data); | |
if (Object.keys(data.result).length == 0) { | |
return; | |
} | |
// URL of provider we upload to | |
const url = "https://mprov01.jackallabs.io/v2/upload"; | |
// view all of them (https://api.jackalprotocol.com/jackal/canine-chain/storage/active_providers) | |
// Build our FormData object | |
const formData = new FormData(); | |
formData.append("file", file); | |
formData.append("sender", data.result.events["post_file.signer"][0]); | |
formData.append("merkle", root); | |
formData.append("start", data.result.events["post_file.start"][0]); | |
// Upload file to provider | |
const request = new Request(url, { | |
method: "POST", | |
body: formData, | |
}); | |
// Handle the response | |
const response = await fetch(request); | |
if (!response.ok) { | |
throw new Error(`Upload failed with status: ${response.status}`); | |
} | |
const jobResponseData = await response.json(); | |
const job = jobResponseData["job_id"] | |
const joburl = `https://mprov01.jackallabs.io/v2/status/${job}` | |
let complete = false | |
while (!complete) { | |
const response = await fetch(joburl); | |
const data = await response.json(); | |
const progress = data["progress"]; | |
if (progress == 100) { | |
complete = true | |
const cid = data["cid"]; | |
console.log("cid:", cid) | |
} else { | |
await new Promise(r => setTimeout(r, 250)); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment