Skip to content

Commit

Permalink
fix: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Oct 13, 2022
1 parent 1cdec2e commit a3752e6
Show file tree
Hide file tree
Showing 10 changed files with 429 additions and 368 deletions.
51 changes: 45 additions & 6 deletions packages/interface/src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,22 +389,61 @@ export interface PrincipalParser {
parse(did: UCAN.DID): Verifier
}

export interface IntoSigner {
decode: (bytes: Uint8Array) => Signer
/**
* Represents component that can create a signer from it's archive. Usually
* signer module would provide `from` function and therefor be implementation
* of this interface.
* Library also provides utility functions for combining multiple
* SignerImporters into one.
*/
export interface SignerImporter<Self extends Signer = Signer> {
from(archive: SignerArchive<Self>): Self
}

export interface Signer<M extends string = string, A extends number = number>
extends UCANSigner<M, A> {
export?: () => Await<ByteView<Signer<M, A>>>
toCryptoKey?: () => Await<CryptoKey>
/**
* Returns archive of this signer which is byte encoded form when signer key
* is extractable and is {@link SignerInfo} form otherwise. This allows a user
* to store non extractable archives in indexedDB and store extractable
* archives on disk, which matches general expectation that in browsers
* unextratable keys should be used and extractable keys in node.
*
* @example
* ```ts
* const save = async (signer: Signer) => {
* const archive = signer.toAchive()
* if (archive instanceof Uint8Array) {
* await fs.writeFile(KEY_PATH, archive)
* } else (signer.exportKey) {
* await IDB_OBJECT_STORE.add(archive)
* }
* }
* ```
*/
toArchive(): SignerArchive<Signer<M, A>>
}

export interface SignerInfo<Self extends Signer = Signer> {
readonly did: ReturnType<Self['did']>
readonly key: CryptoKey
}

export type SignerArchive<Self extends Signer = Signer> =
| ByteView<Self>
| SignerInfo<Self>

export interface Verifier<M extends string = string, A extends number = number>
extends UCANVerifier<M, A> {
export?: () => Await<ByteView<Verifier<M, A>>>
toCryptoKey?: () => Await<CryptoKey>
format(): DID<M>
}

export type InferInvokedCapability<
C extends CapabilityParser<Match<ParsedCapability>>
> = C extends CapabilityParser<Match<infer T>> ? T : never

export type Intersection<T> = (T extends any ? (i: T) => void : never) extends (
i: infer I
) => void
? I
: never
23 changes: 16 additions & 7 deletions packages/principal/src/ed25519/signer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as ED25519 from '@noble/ed25519'
import { varint } from 'multiformats'
import * as API from './type.js'
import * as Verifier from './verifier.js'
import { base64pad, base64url } from 'multiformats/bases/base64'
import { base64pad } from 'multiformats/bases/base64'
import * as Signature from '@ipld/dag-ucan/signature'

export const code = 0x1300
Expand Down Expand Up @@ -47,6 +47,18 @@ export const derive = async secret => {
return signer
}

/**
* @param {API.SignerArchive<API.Signer<"key", typeof signatureCode>>} archive
* @returns {API.EdSigner}
*/
export const from = archive => {
if (archive instanceof Uint8Array) {
return decode(archive)
} else {
throw new Error(`Unsupported archive format`)
}
}

/**
* @param {Uint8Array} bytes
* @returns {API.EdSigner}
Expand Down Expand Up @@ -81,15 +93,15 @@ export const decode = bytes => {
* @param {API.EdSigner} signer
* @return {API.ByteView<API.EdSigner>}
*/
export const encode = signer => signer.encode()
export const encode = signer => signer.toArchive()

/**
* @template {string} Prefix
* @param {API.EdSigner} signer
* @param {API.MultibaseEncoder<Prefix>} [encoder]
*/
export const format = (signer, encoder) =>
(encoder || base64pad).encode(signer.encode())
(encoder || base64pad).encode(encode(signer))

/**
* @template {string} Prefix
Expand Down Expand Up @@ -173,10 +185,7 @@ class Ed25519Signer extends Uint8Array {
return Signature.EdDSA
}

export() {
return this
}
encode() {
toArchive() {
return this
}
}
2 changes: 1 addition & 1 deletion packages/principal/src/ed25519/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface EdSigner<M extends string = 'key'>
readonly verifier: EdVerifier<M>

readonly code: 0x1300
encode(): ByteView<EdSigner<M>>
toArchive(): ByteView<EdSigner<M>>
}

export interface EdVerifier<M extends string = 'key'>
Expand Down
4 changes: 2 additions & 2 deletions packages/principal/src/ed25519/verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ class Ed25519Verifier extends Uint8Array {
)
}

export() {
return this
format() {
return this.did()
}
encode() {
return this
Expand Down
Loading

0 comments on commit a3752e6

Please sign in to comment.