Skip to content

Instantly share code, notes, and snippets.

@tracy-codes
Last active July 1, 2022 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tracy-codes/608d8a6e5d917cfff86167250812ebe4 to your computer and use it in GitHub Desktop.
Save tracy-codes/608d8a6e5d917cfff86167250812ebe4 to your computer and use it in GitHub Desktop.
Snippet of the javascript representation of how files should be signed and uploaded to Shadow Drive
import bs58 from 'bs58'
import nacl from 'tweetnacl'
import crypto from 'crypto'
// `files` is an array of each file passed in.
const allFileNames = files.map(file => file.fileName)
const hashSum = crypto.createHash("sha256")
// `allFileNames.toString()` creates a comma-separated list of all the file names.
const hashedFileNames = hashSum.update(allFileNames.toString())
const fileNamesHashed = hashSum.digest("hex")
// `storageAccount` is the string representation of a storage account pubkey
let msg = `Shadow Drive Signed Message:\nStorage Account: ${storageAccount}\nUpload files with hash: ${fileNamesHashed}`;
const fd = new FormData();
// `files` is an array of each file passed in
for (let j = 0; j < files.length; j++) {
fd.append("file", files[j].data, {
contentType: files[j].contentType as string,
filename: files[j].fileName,
});
}
// Expect the final message string to look something like this if you were to output it
// Shadow Drive Signed Message:
// Storage Acount: ABC123
// Upload files with hash: hash1
// If the message is not formatted like above exactly, it will fail message signature verification
// on the Shadow Drive Network side.
const encodedMessage = new TextEncoder().encode(message);
// Uses https://github.com/dchest/tweetnacl-js to sign the message. If it's not signed in the same manor,
// the message will fail signature verification on the Shadow Network side.
// This will return a base58 byte array of the signature.
const signedMessage = nacl.sign.detached(encodedMessage, keypair.secretKey);
// Convert the byte array to a bs58-encoded string
const signature = bs58.encode(signedMessage)
fd.append("message", signature);
fd.append("signer", keypair.publicKey.toString());
fd.append("storage_account", storageAccount.toString());
fd.append("fileNames", allFileNames.toString());
const request = await fetch(`${SHDW_DRIVE_ENDPOINT}/upload`, {
method: "POST",
body: fd,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment