Skip to content
Open
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
36 changes: 34 additions & 2 deletions lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { connect } from '@ucanto/client'
import * as CAR from '@ucanto/transport/car'
import * as CBOR from '@ucanto/transport/cbor'
import * as HTTP from '@ucanto/transport/http'
import { derive } from "@ucanto/principal/ed25519";
import { parse } from '@ipld/dag-ucan/did'
import { create } from '@web3-storage/w3up-client'
import { Client, create } from '@web3-storage/w3up-client'
import { StoreConf } from '@web3-storage/access/stores/store-conf'
import { AgentData } from "@web3-storage/access";
import { CarReader } from '@ipld/car'

/**
Expand Down Expand Up @@ -42,8 +44,14 @@ export function filesize (bytes) {
* Get a new API client configured from env vars.
*/
export function getClient () {
if (process.env.W3_PRINCIPAL_KEY && process.env.W3_DELEGATION_PROOF) {
// use credentials from env variables
return createFromStatic(process.env.W3_PRINCIPAL_KEY, process.env.W3_DELEGATION_PROOF)
}

// use stored credentials
const store = new StoreConf({ profile: process.env.W3_STORE_NAME ?? 'w3cli' })

let serviceConf
if (
process.env.W3_ACCESS_SERVICE_DID &&
Expand Down Expand Up @@ -193,3 +201,27 @@ async function * filesFromDir (dir, filter) {
}
}
}

async function createFromStatic(principalKey, delegationProof) {
// create a client with the UCAN private key
const principal = await derive(
Buffer.from(principalKey, "base64")
);
const data = await AgentData.create({ principal });
const client = new Client(data);

// create a space with the delegation proof
const blocks = [];
const reader = await CarReader.fromBytes(
Buffer.from(delegationProof, "base64")
);
for await (const block of reader.blocks()) {
blocks.push(block);
}
const proof = importDAG(blocks);

const space = await client.addSpace(proof);
await client.setCurrentSpace(space.did());

return client;
}