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

feat!: fix types for exported entrypoints and simplify others #50

Merged
merged 10 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
44 changes: 14 additions & 30 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,26 @@
},
"type": "module",
"main": "src/lib.js",
"module": "src/lib.js",
"types": "./dist/src/lib.d.ts",
"typesVersions": {
"*": {
"*": [
"dist/*"
],
"dist/src/lib.d.ts": [
".": [
"dist/src/lib.d.ts"
],
"did": [
"dist/src/did"
],
"codec/*": [
"dist/src/codec/*"
]
}
},
"exports": {
".": {
"types": "./dist/src/lib.d.ts",
"import": "./src/lib.js"
},
"./did": {
"types": "./dist/src/did.d.ts",
"import": "./src/did.js"
},
"./src/lib.js": {
"types": "./dist/src/lib.d.ts",
"import": "./src/lib.js"
},
"./src/did.js": {
"types": "./dist/src/did.d.ts",
"import": "./src/did.js"
},
"./src/codec/cbor.js": {
"types": "./dist/src/codec/cbor.d.ts",
"import": "./src/codec/cbor.js"
},
"./src/codec/raw.js": {
"types": "./dist/src/codec/raw.d.ts",
"import": "./src/codec/raw.js"
}
".": "./src/lib.js",
"./did": "./src/did.js",
"./codec/cbor": "./src/codec/cbor.js",
"./codec/raw": "./src/codec/raw.js"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS is moving to export maps and I think it would be a good idea to follow

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

give me links please, i would love to but what was there before didnt work.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried to do this but we cant change module in tsconfig yet it breaks other dep imports ...

},
"c8": {
"exclude": [
Expand All @@ -82,13 +66,13 @@
"@types/mocha": "^9.1.0",
"@web-std/fetch": "^4.0.0",
"@web-std/file": "^3.0.2",
"ucans": "^0.9.0",
"ucans": "0.9.0",
"c8": "^7.11.0",
"chai": "^4.3.6",
"mocha": "^9.2.0",
"nyc": "^15.1.0",
"playwright-test": "^7.2.2",
"typescript": "^4.6.3",
"typescript": "4.8.2",
"@noble/ed25519": "^1.6.0"
},
"license": "(Apache-2.0 AND MIT)"
Expand Down
2 changes: 1 addition & 1 deletion src/codec/cbor.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const match = data => ({
* @param {string} context
*/
const parseProof = (cid, context) =>
/** @type {UCAN.Proof<C>} */ (CID.asCID(cid)) ||
/** @type {UCAN.Link<C>} */ (CID.asCID(cid)) ||
Parser.ParseError.throw(
`Expected ${context} to be CID, instead got ${JSON.stringify(cid)}`
)
Expand Down
20 changes: 14 additions & 6 deletions src/did.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { varint } from "multiformats"
const DID_KEY_PREFIX = `did:key:`
export const ED25519 = 0xed
export const RSA = 0x1205
/**
* @typedef {typeof ED25519|typeof RSA} Code
*/

/**
* @param {Uint8Array} key
Expand All @@ -23,9 +26,6 @@ export const algorithm = key => {
}
}

/**
* @typedef {typeof ED25519|typeof RSA} Code
*/

/**
* @param {UCAN.DID} did
Expand All @@ -39,11 +39,11 @@ export const parse = did => {
}

/**
* @param {UCAN.ByteView<UCAN.DID>} key
* @param {UCAN.DIDView | Uint8Array} key
* @returns {UCAN.DID}
*/
export const format = key =>
/** @type {UCAN.DID} */ (`${DID_KEY_PREFIX}${base58btc.encode(encode(key))}`)
export const format = (key) =>
`${DID_KEY_PREFIX}${base58btc.encode(encode(key))}`

/**
* @param {Uint8Array} bytes
Expand Down Expand Up @@ -77,7 +77,15 @@ export const from = input => {
}
}

/**
* @implements {UCAN.DIDView}
* @extends {Uint8Array}
*/
class DID extends Uint8Array {
/**
*
* @returns {import('./ucan.js').DID}
*/
did() {
return format(this)
}
Expand Down
13 changes: 3 additions & 10 deletions src/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,23 @@ export const format = model =>
/**
* @template {UCAN.Capability} C
* @param {UCAN.Data<C>} model
* @returns {UCAN.SignPayload<C>}
*/
export const formatSignPayload = model =>
`${formatHeader(model)}.${formatPayload(model)}`

/**
* @param {UCAN.Data} data
* @returns {UCAN.ToString<UCAN.ByteView<UCAN.Header>>}
*/
export const formatHeader = data => base64url.baseEncode(encodeHeader(data))

/**
* @template {UCAN.Capability} C
* @param {UCAN.Data<C>} data
* @returns {UCAN.ToString<UCAN.ByteView<UCAN.Payload<C>>>}
*/
export const formatPayload = data => base64url.baseEncode(encodePayload(data))

/**
* @template {UCAN.Capability} C
* @param {UCAN.Signature<UCAN.SignPayload<C>>} signature
* @returns {UCAN.ToString<UCAN.Signature<C>>}
* @param {UCAN.Signature<string>} signature
*/
export const formatSignature = signature => base64url.baseEncode(signature)

Expand Down Expand Up @@ -71,14 +66,12 @@ export const encodePayload = data =>
})

/**
* @param {UCAN.Proof} proof
* @returns {string}
* @param {UCAN.Link} proof
*/
export const encodeProof = proof => proof.toString()

/**
* @param {object} data
* @param {UCAN.ByteView<UCAN.DID>} data.issuer
* @param {UCAN.Data} data
*/
export const encodeAgorithm = data => {
switch (algorithm(data.issuer)) {
Expand Down
21 changes: 9 additions & 12 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,19 @@ export const link = async (ucan, options) => {

/**
* @template {UCAN.Capability} C
* @template {number} [A=number]
* @template {number} [A=number] - Multihash code
* @param {UCAN.UCAN<C>} data
* @param {{hasher?: UCAN.MultihashHasher<A>}} [options]
* @returns {Promise<{cid:UCAN.Proof<C> & CID, bytes: UCAN.ByteView<UCAN.UCAN<C>>, data: UCAN.UCAN<C> }>}
* @returns {Promise<UCAN.Block<C,A>>}
*/
export const write = async (
data,
{ hasher = /** @type {UCAN.MultihashHasher<any> } */ (sha256) } = {}
) => {
export const write = async (data, options) => {
const hasher = options?.hasher || sha256
const [code, bytes] =
data instanceof Uint8Array
? [RAW.code, RAW.encode(data)]
: [CBOR.code, CBOR.encode(data)]

const cid = /** @type {CID & UCAN.Proof<C, A>} */ (
const cid = /** @type {UCAN.Link<C, A>} */ (
CID.createV1(code, await hasher.digest(bytes))
)
return { cid, bytes, data }
Expand All @@ -100,7 +98,6 @@ export const write = async (
* @template {UCAN.Capability} C
* @param {UCAN.JWT<C>} jwt
* @returns {UCAN.View<C>}

*/
export const parse = jwt => {
const model = Parser.parse(jwt)
Expand Down Expand Up @@ -171,11 +168,11 @@ export const issue = async ({
*
* @template {UCAN.Capability} C
* @param {UCAN.Model<C>} ucan
* @param {UCAN.Authority} authority
* @param {UCAN.Verifier} verifier
*/
export const verifySignature = (ucan, authority) =>
formatDID(ucan.issuer) === authority.did() &&
authority.verify(
export const verifySignature = (ucan, verifier) =>
formatDID(ucan.issuer) === verifier.did() &&
verifier.verify(
UTF8.encode(Formatter.formatSignPayload(ucan)),
ucan.signature
)
Expand Down
5 changes: 2 additions & 3 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ const parseProofs = (input, context) =>
/**
* @param {unknown} input
* @param {string} context
* @returns {UCAN.Proof}
*/
const parseProof = (input, context) => {
const proof =
Expand All @@ -230,9 +229,9 @@ const parseProof = (input, context) => {
)}, must be a string`
)
try {
return /** @type {UCAN.Proof} */ (CID.parse(proof))
return /** @type {UCAN.Link} */ (CID.parse(proof))
} catch (error) {
return /** @type {UCAN.Proof} */ (
return /** @type {UCAN.Link} */ (
CID.create(1, raw.code, identity.digest(UTF8.encode(proof)))
)
}
Expand Down
Loading