Server-Hosted Skynet Usage

This is a draft version documenting ideas for using Skynet from outside of a browser context. We’re working to improve this tooling, so this information is subject to change. This is a work-in-progess, so please reach out if you run into any issues.

Overview

As of 08/19/2021, the tooling for Skynet outside of the browser is somewhat disjointed. Hopefully, this guide will walk you through some of the main considerations of interacting with Skynet from a server-side context.

If you are working with us as a partner, please reach out to [email protected] (Discord dghelm#8125) for a promo code for a free elevated tier account.

File Uploads

skynet-js currently only supports browser uploads. You will want to upload files using one of our other SDK or CLI tools (ie @skynetlabs/skynet-nodejs), even though they don’t support the full feature-set of Skynet.

The node SDK has been updated to support large file uploads in version 2.1.0. The tus protocol will automatically be used for any file >40MB.

File Persistence

In a browser context, cookies containing encrypted JWTs are used for linking file uploads to your portal account. This is needed for pinning files >90 days.

If your upload method does not support cookies and your file needs persisted longer than 90 days, you will want to upload your file then use your encrypted JSON Web Token (located in the skynet-jwt cookie) to “pin” files to your account.

Using your JWT with skynet-js

When initializing SkynetClient you will want to pass along your JWT as follows:

import { SkynetClient } from 'skynet-js';
const client = new SkynetClient('https://siasky.net', {customCookie: SKYNET_JWT});

Where the value of SKYNET_JWT is not committed to the repo and is in the format where the value begins with skynet-jwt=.

Using your JWT with skynet-nodejs

The process looks similar to the above example, but replacing the import.

const { SkynetClient } = require('@skynetlabs/skynet-nodejs');
const client = new SkynetClient('https://siasky.net', {customCookie: SKYNET_JWT});

Obtaining your JWT

  1. First, login to your account at https://account.siasky.net/

  2. Press F12 to open your developer tools, select the “Application” tab

  3. In the bar at left, under “Storage”, find “account.siasky.net” under “Cookies”

  4. Locate the item named “skynet-jwt” and copy-paste its value, but make sure the string is prefixed with skynet-jwt=.

Once the file is uploaded, you need to the Siasky.net portal that you wish to associate your account with the given file and commit to “pinning” the file. Your account will then treat the skylink as being one you uploaded. (This method does not work with resolver skylinks.)

After initializing the client with a custom cookie: client.pinSkylink(skylink);

Cookie / JWT Expiration

JWTs expire after 720 hours and we have no tooling to detect if your JWT is expired. We don’t currently support API Keys, so please reach out if you want a longer expiration time on your JWT.

Resolver Skylink Creation

One common use-case for server-side applications is to upload files that are pointed to using a resolver skylink, so that the data can be consumed using a consistent URL.

Here is a code example:

import { SkynetClient, genKeyPairFromSeed } from 'skynet-js';
import { SKYNET_JWT, SECRET_SEED } from './consts';
const portal = 'https://siasky.net';
const client = new SkynetClient(portal, { customCookie: SKYNET_JWT })
// Setup Keys for Read/Write of Mutable Data
const { privateKey, publicKey } = genKeyPairFromSeed( SECRET_SEED );
const dataKey = 'nameForRegistryEntry';
// skylink to point to with resolver skylink
const skylink = "sia://MABdWWku6YETM2zooGCjQi26Rs4a6Hb74q26i-vMMcximQ";
// Set Registry Entry to point at our Skylink
await client.db.setDataLink(privateKey, dataKey, skylink);
// Get the resolver skylink that represents the registry entry
const skylinkV2 = await client.registry.getEntryLink(publicKey, dataKey);
// Get the URL for the skylink, at `siasky.net`
const publicUrl = await client.getSkylinkUrl(skylinkV2);