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

fix: access-api ctx.signer no longer uses env.DID. instead env.DID is only used for ucanto server id #303

Merged
merged 3 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion packages/access-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"@ucanto/principal": "^4.0.2",
"@ucanto/server": "^4.0.2",
"@ucanto/transport": "^4.0.2",
"@ucanto/validator": "^4.0.2",
"@web3-storage/access": "workspace:^",
"@web3-storage/capabilities": "workspace:^",
"@web3-storage/worker-utils": "0.4.3-dev",
Expand Down
27 changes: 20 additions & 7 deletions packages/access-api/src/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DID } from '@ucanto/validator'
import { Signer } from '@ucanto/principal/ed25519'
import * as DID from '@ipld/dag-ucan/did'

/**
* Loads configuration variables from the global environment and returns a JS object
Expand Down Expand Up @@ -34,7 +34,8 @@ export function loadConfig(env) {

const DID = env.DID
const PRIVATE_KEY = vars.PRIVATE_KEY
const signer = configureSigner({ DID, PRIVATE_KEY })
const signer = configureSigner({ PRIVATE_KEY })
const ucantoServerId = configureUcantoServerId({ DID, PRIVATE_KEY })
return {
DEBUG: boolValue(vars.DEBUG),
ENV: parseRuntimeEnv(vars.ENV),
Expand All @@ -55,6 +56,7 @@ export function loadConfig(env) {
COMMITHASH: ACCOUNT_COMMITHASH,

signer,
ucantoServerId,

// bindings
METRICS:
Expand Down Expand Up @@ -117,14 +119,25 @@ export function createAnalyticsEngine() {
* Given a config, return a ucanto Signer object representing the service
*
* @param {object} config
* @param {string} [config.DID] - public identifier of the running service. e.g. a did:key or a did:web
* @param {string} config.PRIVATE_KEY - multiformats private key of primary signing key
* @returns {Signer.EdSigner}
*/
export function configureSigner(config) {
const signer = Signer.parse(config.PRIVATE_KEY)
const did = config.DID
if (!did) {
return signer
return signer
}

/**
* Given a config, return a ucanto principal
*
* @param {object} config
* @param {string} [config.DID] - public identifier of the running service. e.g. a did:key or a did:web
* @param {string} config.PRIVATE_KEY - multiformats private key of primary signing key
* @returns {import('@ucanto/interface').Principal}
*/
export function configureUcantoServerId(config) {
if (config.DID) {
return DID.parse(config.DID)
}
return signer.withDID(DID.match({}).from(did))
return configureSigner(config)
}
2 changes: 1 addition & 1 deletion packages/access-api/src/routes/raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { service } from '../service/index.js'
*/
export async function postRaw(request, env) {
const server = Server.create({
id: env.signer,
id: env.config.ucantoServerId,
encoder: serverCodec,
decoder: serverCodec,
service: service(env),
Expand Down
25 changes: 18 additions & 7 deletions packages/access-api/test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,32 @@ const testKeypair = {
}

describe('@web3-storage/access-api/src/config configureSigner', () => {
it('creates a signer using config.{DID,PRIVATE_KEY}', async () => {
it('creates a signer using config.PRIVATE_KEY', async () => {
const config = {
PRIVATE_KEY: testKeypair.private.multiformats,
DID: 'did:web:exampe.com',
}
const signer = configModule.configureSigner(config)
assert.ok(signer)
assert.equal(signer.did().toString(), config.DID)
assert.equal(signer.did().toString(), testKeypair.public.did)
const { keys } = signer.toArchive()
const didKeys = Object.keys(keys)
assert.deepEqual(didKeys, [testKeypair.public.did])
})
})

describe('@web3-storage/access-api/src/config configureUcantoServerId', () => {
it('creates a signer using config.{DID,PRIVATE_KEY}', async () => {
const config = {
PRIVATE_KEY: testKeypair.private.multiformats,
DID: 'did:web:exampe.com',
}
const serverId = configModule.configureUcantoServerId(config)
assert.ok(serverId)
assert.equal(serverId.did().toString(), config.DID)
})
it('errors if config.DID is provided but not a did', () => {
assert.throws(() => {
configModule.configureSigner({
configModule.configureUcantoServerId({
DID: 'not a did',
PRIVATE_KEY: testKeypair.private.multiformats,
})
Expand All @@ -43,8 +54,8 @@ describe('@web3-storage/access-api/src/config configureSigner', () => {
const config = {
PRIVATE_KEY: testKeypair.private.multiformats,
}
const signer = configModule.configureSigner(config)
assert.ok(signer)
assert.equal(signer.did().toString(), testKeypair.public.did)
const serverId = configModule.configureUcantoServerId(config)
assert.ok(serverId)
assert.equal(serverId.did().toString(), testKeypair.public.did)
})
})
2 changes: 1 addition & 1 deletion packages/access-api/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ database_id = "7c676e0c-b9e7-4711-97c8-7b1c8eb229ae"
[vars]
ENV = "dev"
DEBUG = "true"

DID = "did:web:local.web3.storage"

[build]
command = "scripts/cli.js build"
Expand Down
2 changes: 0 additions & 2 deletions pnpm-lock.yaml

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