-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfig.js
60 lines (55 loc) · 1.77 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import * as ed25519 from '@ucanto/principal/ed25519'
import * as DID from '@ipld/dag-ucan/did'
import { CAR, HTTP } from '@ucanto/transport'
import { connect } from '@ucanto/client'
/**
* Given a config, return a ucanto Signer object representing the service
*
* @param {object} config
* @param {string} config.privateKey - multiformats private key of primary signing key
* @param {string} [config.did] - public DID for the upload service (did:key:... derived from PRIVATE_KEY if not set)
* @returns {import('@ucanto/principal/ed25519').Signer.Signer}
*/
export function getServiceSigner(config) {
const signer = ed25519.parse(config.privateKey)
if (config.did) {
const did = DID.parse(config.did).did()
return signer.withDID(did)
}
return signer
}
/**
* Given a string, parse into provider ServiceDIDs.
*
* @param {string} serviceDids a comma-separated string of ServiceDIDs
* @returns {import('@web3-storage/upload-api').ServiceDID[]}
*/
export function parseServiceDids(serviceDids) {
return /** @type {import('@web3-storage/upload-api').ServiceDID[]} */(
serviceDids.split(',').map(s => {
const did = DID.parse(s.trim()).did()
if (!did.startsWith('did:web:')) {
throw new Error(`Invalid ServiceDID - ServiceDID must be a did:web: ${did}`)
}
return did
})
)
}
/**
*
* @param {{ did: string, url: string }} config
* @returns
*/
export function getServiceConnection (config) {
const servicePrincipal = DID.parse(config.did) // 'did:web:web3.storage'
const serviceURL = new URL(config.url) // 'https://tracker.web3.storage'
const serviceConnection = connect({
id: servicePrincipal,
codec: CAR.outbound,
channel: HTTP.open({
url: serviceURL,
method: 'POST',
}),
})
return serviceConnection
}