MCPcopy Create free account
hub / github.com/Effect-TS/effect / cryptoKeyFromFormat

Function cryptoKeyFromFormat

packages/effect/src/Schema.ts:14517–14542  ·  view source on GitHub ↗
(
  name: string,
  to: To,
  format: KeyFormat_.Format<R, "decode", Data>,
  decode: (data: Data) => Effect.Effect<To["Type"], CryptoError_.CryptoError>,
  canEncode: boolean
)

Source from the content-addressed store, hash-verified

14515 (format._tag === "jwk" ? CryptoJsonWebKey : format.pem ? String : Uint8Array) as Codec<any>
14516
14517const cryptoKeyFromFormat = <To extends Codec<any>, R extends KeyFormat_.Role, Data>(
14518 name: string,
14519 to: To,
14520 format: KeyFormat_.Format<R, "decode", Data>,
14521 decode: (data: Data) => Effect.Effect<To["Type"], CryptoError_.CryptoError>,
14522 canEncode: boolean
14523): decodeTo<To, Codec<Data>> => {
14524 // PKCS#1 and SEC1 are read only.
14525 const writable = canEncode && format._tag !== "pkcs1" && format._tag !== "sec1"
14526 return cryptoFormatData(format).pipe(decodeTo(
14527 to,
14528 SchemaTransformation.transformEffect<To["Type"], Data>({
14529 decode: (data, options) =>
14530 // Secret JWK decoding is synchronous, but all format schemas are async-only.
14531 Effect.flatMap(Effect.promise(() => Promise.resolve()), () =>
14532 Effect.mapError(decode(data), (error) => cryptoIssue(error, data, options))),
14533 encode: (key, options) =>
14534 writable
14535 ? Effect.mapError(
14536 KeyFormat_.encode(format as any, key as KeyFormat_.Key) as Effect.Effect<Data, CryptoError_.CryptoError>,
14537 (error) => cryptoIssue(error, key, options)
14538 )
14539 : cryptoForbiddenEncode(name, key, options)
14540 })
14541 ))
14542}
14543
14544/**
14545 * Type-level representation of {@link MacKey}.

Callers 10

MacKeyFromFormatFunction · 0.85
AeadKeyFromFormatFunction · 0.85
CipherKeyFromFormatFunction · 0.85
KeyWrapKeyFromFormatFunction · 0.85

Calls 8

cryptoFormatDataFunction · 0.85
cryptoIssueFunction · 0.85
cryptoForbiddenEncodeFunction · 0.85
resolveMethod · 0.80
encodeMethod · 0.80
decodeToFunction · 0.70
decodeFunction · 0.70
pipeMethod · 0.65

Tested by

no test coverage detected