Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use UCAN tokens for NFT marketplace #779

Merged
merged 11 commits into from
Aug 23, 2022
22 changes: 22 additions & 0 deletions scripts/ucan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## UCAN token generation script

The script is intended to emit UCAN token which will be valid for 10 days. The output file (token) should be hosted at public route without any risk for the end-users. It will establish secure communication channel between NFT.Storage and Marketplace, and Marketplace and its end-users.

### Setup

Before UCAN token generation it is neccessary to set API_KEY environment variable for this directory. (API_KEY could be get at `nft.storage/manage` website per account).
After that, installing dependencies is required by running:

```
npm install
```

Once the dependencies are installed it is possible to generate UCAN token. The output file (_ucan.json_) will be located in the same directory where scripts resides.

### UCAN generation

Run the command below to emit this file:

```
node generateNftServiceKeypair.js
```
99 changes: 99 additions & 0 deletions scripts/ucan/generateNftServiceKeypair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { writeFile } from 'fs';
import fetch from 'node-fetch';
import { KeyPair } from 'ucan-storage/keypair';
import { build } from 'ucan-storage/ucan-storage';

const SERVICE_ENDPOINT = 'https://api.nft.storage'; // default
const API_TOKEN = process.env.API_KEY;

/**
* Obtaining the service DID
*
*/
async function getServiceDid() {
const didRes = await fetch(new URL('/did', SERVICE_ENDPOINT));
const { ok, value: serviceDid } = await didRes.json();

if (ok) {
return serviceDid;
} else {
throw new Error('Could not get Service DID');
}
}

/**
* Obtaining a root UCAN token.
* It will be valid for a duration of two weeks
*
*/
async function getRootToken(token) {
const ucanReq = await fetch(new URL('/ucan/token', SERVICE_ENDPOINT), {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
},
});

if (!ucanReq.ok) {
throw new Error('Failed to get root UCAN token');
}

const { value: rootUCAN } = await ucanReq.json();

return rootUCAN;
}

/**
* Obtaining UCAN token with specified expiration date (10 days)
*
*/
async function getUCAN(kp, serviceDid, rootUCAN) {
// convert timestamp to seconds
const nowInSeconds = Math.floor(Date.now() / 1000);
const expiration = nowInSeconds + 864000; // 10 days from now

try {
return await build({
issuer: kp,
audience: serviceDid,
expiration: expiration,
capabilities: [
{
with: `storage://${kp.did()}`,
can: 'upload/*',
},
],
proofs: [rootUCAN],
});
} catch (error) {
throw new Error('Could not create UCAN token:', error);
}
}

(async function main() {
try {
const pair = await KeyPair.create();
const privateKey = pair.export();

const kp = await KeyPair.fromExportedKey(privateKey);

const serviceDid = await getServiceDid();

const rootUCAN = await getRootToken(API_TOKEN);

const ucan = await getUCAN(kp, serviceDid, rootUCAN);

const credentials = `{
"marketplaceDid": "${kp.did()}",
"ucan": "${ucan}"
}\n`;

writeFile('ucan.json', credentials, (err) => {
if (err) throw new Error(err);

process.stdout.write('The ucan file has been saved!\n');
});
} catch (error) {
console.error(error);
}
})();
83 changes: 83 additions & 0 deletions scripts/ucan/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions scripts/ucan/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "ucan",
"version": "1.0.0",
"description": "This script generates new ucan token for NFT marketplace each time it runs.",
"main": "generateNftServiceKeypair.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"ucan"
],
"author": "",
"license": "ISC",
"dependencies": {
"node-fetch": "^3.2.10",
"ucan-storage": "^1.3.0"
},
"type": "module"
}