From 7999dc92b94d1b7285375cfaad4a0ae6644b71b6 Mon Sep 17 00:00:00 2001 From: James Brock Date: Wed, 18 May 2022 15:42:51 +0900 Subject: [PATCH 1/3] Upgrade to PureScript 0.15 --- packages.dhall | 5 +-- spago.dhall | 10 ++++- src/Crypto/Subtle/Encrypt.js | 6 +-- src/Crypto/Subtle/Encrypt.purs | 29 ++++++------- src/Crypto/Subtle/Hash.js | 4 +- src/Crypto/Subtle/Hash.purs | 15 +++---- src/Crypto/Subtle/Key/Derive.js | 6 +-- src/Crypto/Subtle/Key/Derive.purs | 28 +++++++------ src/Crypto/Subtle/Key/Generate.js | 6 +-- src/Crypto/Subtle/Key/Generate.purs | 31 +++++++------- src/Crypto/Subtle/Key/Import.js | 4 +- src/Crypto/Subtle/Key/Import.purs | 23 ++++------- src/Crypto/Subtle/Key/Types.js | 4 +- src/Crypto/Subtle/Key/Types.purs | 64 +++++++++++++++++++++-------- src/Crypto/Subtle/Key/Wrap.js | 6 +-- src/Crypto/Subtle/Key/Wrap.purs | 25 +++++------ src/Crypto/Subtle/Sign.js | 10 ++--- src/Crypto/Subtle/Sign.purs | 21 +++++----- 18 files changed, 154 insertions(+), 143 deletions(-) diff --git a/packages.dhall b/packages.dhall index 62f06fd..06c6e1e 100644 --- a/packages.dhall +++ b/packages.dhall @@ -116,10 +116,9 @@ let additions = } ------------------------------- -} - - let upstream = - https://github.com/purescript/package-sets/releases/download/psc-0.13.6-20200423/packages.dhall sha256:c180a06bb5444fd950f8cbdd6605c644fd246deb397e62572b8f4a6b9dbcaf22 + https://github.com/purescript/package-sets/releases/download/psc-0.15.0-20220516/packages.dhall + sha256:b0bf932de16a10b7d69c6bbbb31ec9ca575237c43a999fa32e59e35eb8c024a1 let overrides = {=} diff --git a/spago.dhall b/spago.dhall index dd7ab78..c495f0e 100644 --- a/spago.dhall +++ b/spago.dhall @@ -5,13 +5,19 @@ You can edit this file as you like. { name = "subtlecrypto" , dependencies = [ "aff" + , "aff-promise" , "arraybuffer-types" , "console" , "effect" + , "either" + , "exceptions" , "foreign" + , "functions" + , "maybe" , "prelude" - , "promises" - , "psci-support" + , "transformers" + , "tuples" + , "unsafe-coerce" ] , packages = ./packages.dhall , sources = [ "src/**/*.purs", "test/**/*.purs" ] diff --git a/src/Crypto/Subtle/Encrypt.js b/src/Crypto/Subtle/Encrypt.js index 7fc15e8..ba91c7f 100644 --- a/src/Crypto/Subtle/Encrypt.js +++ b/src/Crypto/Subtle/Encrypt.js @@ -1,8 +1,6 @@ -"use strict"; - -exports.encryptImpl = function encryptImpl (a,k,x) { +export function encryptImpl (a,k,x) { return crypto.subtle.encrypt(a,k,x); }; -exports.decryptImpl = function decryptImpl (a,k,x) { +export function decryptImpl (a,k,x) { return crypto.subtle.decrypt(a,k,x); }; diff --git a/src/Crypto/Subtle/Encrypt.purs b/src/Crypto/Subtle/Encrypt.purs index 72ff2fb..76e666a 100644 --- a/src/Crypto/Subtle/Encrypt.purs +++ b/src/Crypto/Subtle/Encrypt.purs @@ -3,38 +3,39 @@ module Crypto.Subtle.Encrypt , EncryptAlgorithm, rsaOAEP, aesCTR, aesCBC, aesGCM, aesKW ) where -import Crypto.Subtle.Key.Types (CryptoKey) +import Control.Promise (Promise, toAff') import Crypto.Subtle.Constants.AES (AESTagLength) - -import Prelude ((<<<), (<$)) -import Data.Function.Uncurried (Fn3, runFn3) -import Data.Tuple (Tuple (..)) -import Data.Maybe (Maybe (..)) -import Data.Either (Either (..)) +import Crypto.Subtle.Key.Types (CryptoKey, errorFromDOMException) import Data.ArrayBuffer.Types (ArrayBuffer) -import Effect.Promise (Promise, runPromise) -import Effect.Aff (Aff, makeAff, nonCanceler) +import Data.Function.Uncurried (Fn3, runFn3) +import Data.Maybe (Maybe(..)) +import Data.Tuple (Tuple(..)) +import Effect.Aff (Aff) import Unsafe.Coerce (unsafeCoerce) foreign import data EncryptAlgorithm :: Type +-- | https://developer.mozilla.org/en-US/docs/Web/API/RsaOaepParams rsaOAEP :: Maybe ArrayBuffer -- ^ Label -> EncryptAlgorithm rsaOAEP mL = case mL of Nothing -> unsafeCoerce {name: "RSA_OAEP"} Just l -> unsafeCoerce {name: "RSA_OAEP", label: l} +-- | https://developer.mozilla.org/en-US/docs/Web/API/AesCtrParams aesCTR :: ArrayBuffer -- ^ Counter -> Int -- ^ Counter length -> EncryptAlgorithm aesCTR c l = unsafeCoerce {name: "AES-CTR", counter: c, length: l} +-- | https://developer.mozilla.org/en-US/docs/Web/API/AesCbcParams aesCBC :: ArrayBuffer -- ^ Initialization vector -> EncryptAlgorithm aesCBC i = unsafeCoerce {name: "AES-CBC", iv: i} +-- | https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams aesGCM :: ArrayBuffer -- ^ Initialization vector -> Maybe ArrayBuffer -- ^ Additional data -> Maybe AESTagLength -- ^ Tag length @@ -53,20 +54,20 @@ aesKW = unsafeCoerce {name: "AES-KW"} foreign import encryptImpl :: Fn3 EncryptAlgorithm CryptoKey ArrayBuffer (Promise ArrayBuffer) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/encrypt encrypt :: EncryptAlgorithm -> CryptoKey -> ArrayBuffer -> Aff ArrayBuffer -encrypt a k x = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 encryptImpl a k x) +encrypt a k x = toAff' errorFromDOMException (runFn3 encryptImpl a k x) foreign import decryptImpl :: Fn3 EncryptAlgorithm CryptoKey ArrayBuffer (Promise ArrayBuffer) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/decrypt decrypt :: EncryptAlgorithm -> CryptoKey -> ArrayBuffer - -> Aff (Maybe ArrayBuffer) -decrypt a k x = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right <<< Just) (\_ -> resolve (Right Nothing)) (runFn3 decryptImpl a k x) + -> Aff ArrayBuffer +decrypt a k x = toAff' errorFromDOMException (runFn3 decryptImpl a k x) diff --git a/src/Crypto/Subtle/Hash.js b/src/Crypto/Subtle/Hash.js index 3f0b9f4..168acc9 100644 --- a/src/Crypto/Subtle/Hash.js +++ b/src/Crypto/Subtle/Hash.js @@ -1,5 +1,3 @@ -"use strict"; - -exports.digestImpl = function digestImpl (h,x) { +export function digestImpl (h,x) { return crypto.subtle.digest(h,x); }; diff --git a/src/Crypto/Subtle/Hash.purs b/src/Crypto/Subtle/Hash.purs index a0398d1..0fe4529 100644 --- a/src/Crypto/Subtle/Hash.purs +++ b/src/Crypto/Subtle/Hash.purs @@ -1,11 +1,11 @@ module Crypto.Subtle.Hash (HashingFunction, sha1, sha256, sha384, sha512, digest) where -import Prelude ((<<<), (<$), class Eq) +import Control.Promise (Promise, toAff') +import Crypto.Subtle.Key.Types (errorFromDOMException) import Data.ArrayBuffer.Types (ArrayBuffer) import Data.Function.Uncurried (Fn2, runFn2) -import Data.Either (Either (..)) -import Effect.Promise (Promise, runPromise) -import Effect.Aff (Aff, makeAff, nonCanceler) +import Effect.Aff (Aff) +import Prelude (class Eq) @@ -25,11 +25,8 @@ sha384 = HashingFunction "SHA-384" sha512 :: HashingFunction sha512 = HashingFunction "SHA-512" - foreign import digestImpl :: Fn2 HashingFunction ArrayBuffer (Promise ArrayBuffer) - +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest digest :: HashingFunction -> ArrayBuffer -> Aff ArrayBuffer -digest h x = - let p = runFn2 digestImpl h x - in makeAff \resolve -> nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) p +digest h x = toAff' errorFromDOMException (runFn2 digestImpl h x) diff --git a/src/Crypto/Subtle/Key/Derive.js b/src/Crypto/Subtle/Key/Derive.js index fea90dd..9a7b9af 100644 --- a/src/Crypto/Subtle/Key/Derive.js +++ b/src/Crypto/Subtle/Key/Derive.js @@ -1,8 +1,6 @@ -"use strict"; - -exports.deriveKeyImpl = function deriveKeyImpl (a,k,t,e,u) { +export function deriveKeyImpl (a,k,t,e,u) { return crypto.subtle.deriveKey(a,k,t,e,u); }; -exports.deriveBitsImpl = function deriveBitsImpl (a,k,l) { +export function deriveBitsImpl (a,k,l) { return crypto.subtle.deriveBits(a,k,l); }; diff --git a/src/Crypto/Subtle/Key/Derive.purs b/src/Crypto/Subtle/Key/Derive.purs index c479da0..fcb0bda 100644 --- a/src/Crypto/Subtle/Key/Derive.purs +++ b/src/Crypto/Subtle/Key/Derive.purs @@ -5,17 +5,14 @@ module Crypto.Subtle.Key.Derive , DeriveTargetAlgorithm, hmac, aes ) where -import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyUsage) -import Crypto.Subtle.Hash (HashingFunction) -import Crypto.Subtle.Constants.EC (ECAlgorithm) +import Control.Promise (Promise, toAff') import Crypto.Subtle.Constants.AES (AESAlgorithm, AESBitLength) - -import Prelude ((<<<), (<$)) -import Data.Function.Uncurried (Fn3, Fn5, runFn3, runFn5) +import Crypto.Subtle.Constants.EC (ECAlgorithm) +import Crypto.Subtle.Hash (HashingFunction) +import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyUsage, errorFromDOMException) import Data.ArrayBuffer.Types (ArrayBuffer) -import Data.Either (Either (..)) -import Effect.Promise (Promise, runPromise) -import Effect.Aff (Aff, makeAff, nonCanceler) +import Data.Function.Uncurried (Fn3, Fn5, runFn3, runFn5) +import Effect.Aff (Aff) import Unsafe.Coerce (unsafeCoerce) @@ -24,38 +21,41 @@ foreign import deriveKeyImpl :: Fn5 DeriveAlgorithm CryptoKey DeriveTargetAlgori foreign import deriveBitsImpl :: Fn3 DeriveAlgorithm CryptoKey Int (Promise ArrayBuffer) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey deriveKey :: DeriveAlgorithm -> CryptoKey -- ^ Base key -> DeriveTargetAlgorithm -> Boolean -- ^ Extractable -> Array CryptoKeyUsage -> Aff CryptoKey -deriveKey a k t e u = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn5 deriveKeyImpl a k t e u) +deriveKey a k t e u = toAff' errorFromDOMException (runFn5 deriveKeyImpl a k t e u) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveBits deriveBits :: DeriveAlgorithm -> CryptoKey -- ^ Base key -> Int -- ^ Length in bits -> Aff ArrayBuffer -deriveBits a k l = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 deriveBitsImpl a k l) +deriveBits a k l = toAff' errorFromDOMException (runFn3 deriveBitsImpl a k l) foreign import data DeriveAlgorithm :: Type foreign import data DeriveTargetAlgorithm :: Type +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#ecdh ec :: ECAlgorithm -> CryptoKey -- ^ Public key of the other entity -> DeriveAlgorithm ec e k = unsafeCoerce {name: e, public: k} +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#hkdf hkdf :: HashingFunction -> ArrayBuffer -- ^ Salt -> ArrayBuffer -- ^ Info -> DeriveAlgorithm hkdf h s i = unsafeCoerce {name: "HKDF", hash: h, salt: s, info: i} +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/deriveKey#pbkdf2 pbkdf2 :: HashingFunction -> ArrayBuffer -- ^ Salt -> Int -- ^ Iterations @@ -65,8 +65,10 @@ pbkdf2 h s i = unsafeCoerce {name: "PBKDF2", hash: h, salt: s, iterations: i} +-- | https://developer.mozilla.org/en-US/docs/Web/API/HmacKeyGenParams hmac :: HashingFunction -> DeriveTargetAlgorithm hmac h = unsafeCoerce {name: "HMAC", hash: h} +-- | https://developer.mozilla.org/en-US/docs/Web/API/AesKeyGenParams aes :: AESAlgorithm -> AESBitLength -> DeriveTargetAlgorithm aes a l = unsafeCoerce {name: a, length: l} diff --git a/src/Crypto/Subtle/Key/Generate.js b/src/Crypto/Subtle/Key/Generate.js index d28a8d2..5f55c45 100644 --- a/src/Crypto/Subtle/Key/Generate.js +++ b/src/Crypto/Subtle/Key/Generate.js @@ -1,6 +1,4 @@ -"use strict"; - -exports.generateKeyImpl = function generateKeyImpl (a,e,u) { +export function generateKeyImpl (a,e,u) { return crypto.subtle.generateKey(a,e,u); }; -exports.exp65537 = new Uint8Array([0x01,0x00,0x01]); +export const exp65537 = new Uint8Array([0x01,0x00,0x01]); diff --git a/src/Crypto/Subtle/Key/Generate.purs b/src/Crypto/Subtle/Key/Generate.purs index 9ea6275..ca142bc 100644 --- a/src/Crypto/Subtle/Key/Generate.purs +++ b/src/Crypto/Subtle/Key/Generate.purs @@ -5,18 +5,15 @@ module Crypto.Subtle.Key.Generate , exp65537 ) where -import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyPair, CryptoKeyUsage) -import Crypto.Subtle.Hash (HashingFunction) -import Crypto.Subtle.Constants.RSA (RSAAlgorithm) -import Crypto.Subtle.Constants.EC (ECAlgorithm, ECCurve) +import Control.Promise (Promise, toAff') import Crypto.Subtle.Constants.AES (AESAlgorithm, AESBitLength) - -import Prelude ((<<<), (<$)) -import Data.Function.Uncurried (Fn3, runFn3) +import Crypto.Subtle.Constants.EC (ECAlgorithm, ECCurve) +import Crypto.Subtle.Constants.RSA (RSAAlgorithm) +import Crypto.Subtle.Hash (HashingFunction) +import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyPair, CryptoKeyUsage, errorFromDOMException) import Data.ArrayBuffer.Types (Uint8Array) -import Data.Either (Either (..)) -import Effect.Promise (Promise, runPromise) -import Effect.Aff (Aff, makeAff, nonCanceler) +import Data.Function.Uncurried (Fn3, runFn3) +import Effect.Aff (Aff) import Unsafe.Coerce (unsafeCoerce) @@ -25,20 +22,22 @@ foreign import generateKeyImpl :: forall a b. Fn3 a Boolean (Array CryptoKeyUsag -- | Generate a symmetric key +-- | +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey generateKey :: SymmetricAlgorithm -> Boolean -- ^ Extractable -> Array CryptoKeyUsage -> Aff CryptoKey -generateKey a e u = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 generateKeyImpl a e u) +generateKey a e u = toAff' errorFromDOMException (runFn3 generateKeyImpl a e u) -- | Generate an asymmetric keypair +-- | +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey generateKeyPair :: AsymmetricAlgorithm -> Boolean -- ^ Extractable -> Array CryptoKeyUsage -> Aff CryptoKeyPair -generateKeyPair a e u = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 generateKeyImpl a e u) +generateKeyPair a e u = toAff' errorFromDOMException (runFn3 generateKeyImpl a e u) foreign import data SymmetricAlgorithm :: Type @@ -48,6 +47,7 @@ foreign import data AsymmetricAlgorithm :: Type foreign import exp65537 :: Uint8Array +-- | https://developer.mozilla.org/en-US/docs/Web/API/RsaHashedKeyGenParams rsa :: RSAAlgorithm -> Int -- ^ Modulus length. Should be at least 2048, or 4096 according to some bozo -> Uint8Array -- ^ Public exponent. Just use `exp65537`. @@ -55,12 +55,15 @@ rsa :: RSAAlgorithm -> AsymmetricAlgorithm rsa r l e h = unsafeCoerce {name: r, modulusLength: l, publicExponent: e, hash: h} +-- | https://developer.mozilla.org/en-US/docs/Web/API/EcKeyGenParams ec :: ECAlgorithm -> ECCurve -> AsymmetricAlgorithm ec e c = unsafeCoerce {name: e, namedCurve: c} +-- | https://developer.mozilla.org/en-US/docs/Web/API/HmacKeyGenParams hmac :: HashingFunction -> SymmetricAlgorithm hmac h = unsafeCoerce {name: "HMAC", hash: h} +-- | https://developer.mozilla.org/en-US/docs/Web/API/AesKeyGenParams aes :: AESAlgorithm -> AESBitLength -> SymmetricAlgorithm diff --git a/src/Crypto/Subtle/Key/Import.js b/src/Crypto/Subtle/Key/Import.js index fb73401..2b82c8a 100644 --- a/src/Crypto/Subtle/Key/Import.js +++ b/src/Crypto/Subtle/Key/Import.js @@ -1,5 +1,3 @@ -"use strict"; - -exports.importKeyImpl = function importKeyImpl (f,x,a,e,u) { +export function importKeyImpl (f,x,a,e,u) { return crypto.subtle.importKey(f,x,a,e,u); }; diff --git a/src/Crypto/Subtle/Key/Import.purs b/src/Crypto/Subtle/Key/Import.purs index 84e54f6..462d0a6 100644 --- a/src/Crypto/Subtle/Key/Import.purs +++ b/src/Crypto/Subtle/Key/Import.purs @@ -3,18 +3,15 @@ module Crypto.Subtle.Key.Import , ImportAlgorithm, rsa, ec, hmac, aes ) where -import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyUsage, ExternalFormat) -import Crypto.Subtle.Hash (HashingFunction) -import Crypto.Subtle.Constants.RSA (RSAAlgorithm) -import Crypto.Subtle.Constants.EC (ECAlgorithm, ECCurve) +import Control.Promise (Promise, toAff') import Crypto.Subtle.Constants.AES (AESAlgorithm) - -import Prelude ((<<<), (<$)) -import Data.Function.Uncurried (Fn5, runFn5) +import Crypto.Subtle.Constants.EC (ECAlgorithm, ECCurve) +import Crypto.Subtle.Constants.RSA (RSAAlgorithm) +import Crypto.Subtle.Hash (HashingFunction) +import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyUsage, ExternalFormat, errorFromDOMException) import Data.ArrayBuffer.Types (ArrayBuffer) -import Data.Either (Either (..)) -import Effect.Promise (Promise, runPromise) -import Effect.Aff (Aff, makeAff, nonCanceler) +import Data.Function.Uncurried (Fn5, runFn5) +import Effect.Aff (Aff) import Unsafe.Coerce (unsafeCoerce) @@ -22,16 +19,14 @@ import Unsafe.Coerce (unsafeCoerce) foreign import importKeyImpl :: Fn5 ExternalFormat ArrayBuffer ImportAlgorithm Boolean (Array CryptoKeyUsage) (Promise CryptoKey) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey importKey :: ExternalFormat -> ArrayBuffer -- ^ Key data -> ImportAlgorithm -> Boolean -- ^ Extractable -> Array CryptoKeyUsage -> Aff CryptoKey -importKey f x a e u = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn5 importKeyImpl f x a e u) - - +importKey f x a e u = toAff' errorFromDOMException (runFn5 importKeyImpl f x a e u) foreign import data ImportAlgorithm :: Type diff --git a/src/Crypto/Subtle/Key/Types.js b/src/Crypto/Subtle/Key/Types.js index c7e4d4b..28c54f4 100644 --- a/src/Crypto/Subtle/Key/Types.js +++ b/src/Crypto/Subtle/Key/Types.js @@ -1,5 +1,3 @@ -"use strict"; - -exports.exportKeyImpl = function exportKeyImpl (f,x) { +export function exportKeyImpl (f,x) { return crypto.subtle.exportKey(f,x); }; diff --git a/src/Crypto/Subtle/Key/Types.purs b/src/Crypto/Subtle/Key/Types.purs index 232489a..6a4bf29 100644 --- a/src/Crypto/Subtle/Key/Types.purs +++ b/src/Crypto/Subtle/Key/Types.purs @@ -1,24 +1,43 @@ module Crypto.Subtle.Key.Types - ( CryptoKeyType - , secret, public, private + ( CryptoKey + , CryptoKeyPair(..) + , CryptoKeyType , CryptoKeyUsage - , encrypt, decrypt, sign, verify, deriveKey, deriveBits, unwrapKey, wrapKey + , ExternalFormat , allUsages - , CryptoKey - , getType, getExtractable, getAlgorithm, getUsages + , decrypt + , deriveBits + , deriveKey + , encrypt + , errorFromDOMException , exportKey - , CryptoKeyPair (..) - , ExternalFormat, raw, pkcs8, spki, jwk - ) where - - -import Prelude ((<<<), (<$), class Eq) -import Data.Either (Either (..)) -import Data.Function.Uncurried (Fn2, runFn2) + , getAlgorithm + , getExtractable + , getType + , getUsages + , jwk + , pkcs8 + , private + , public + , raw + , secret + , sign + , spki + , unwrapKey + , verify + , wrapKey + ) + where + +import Prelude ((>>=), class Eq) +import Control.Monad.Except (runExcept) +import Control.Promise (Promise, toAff') import Data.ArrayBuffer.Types (ArrayBuffer) -import Foreign (Foreign) -import Effect.Aff (Aff, makeAff, nonCanceler) -import Effect.Promise (Promise, runPromise) +import Data.Either (Either(..)) +import Data.Function.Uncurried (Fn2, runFn2) +import Effect.Aff (Aff, Error, error) +import Foreign (Foreign, readString) +import Foreign.Index (readProp) import Unsafe.Coerce (unsafeCoerce) @@ -103,8 +122,17 @@ jwk = ExternalFormat "jwk" foreign import exportKeyImpl :: Fn2 ExternalFormat CryptoKey (Promise ArrayBuffer) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/exportKey exportKey :: ExternalFormat -> CryptoKey -> Aff ArrayBuffer -exportKey f x = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn2 exportKeyImpl f x) +exportKey f x = toAff' errorFromDOMException (runFn2 exportKeyImpl f x) + +-- Most of the Promises throw DOMException, so read the message from the DOMException. +-- https://developer.mozilla.org/en-US/docs/Web/API/DOMException +-- Some of the Promises throw TypeError, which also has a message property. +-- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError +errorFromDOMException :: Foreign -> Error +errorFromDOMException x = case runExcept (readProp "message" x >>= readString) of + Left _ -> error "Not a DOMException" + Right m -> error m diff --git a/src/Crypto/Subtle/Key/Wrap.js b/src/Crypto/Subtle/Key/Wrap.js index 193b762..5d2e0ef 100644 --- a/src/Crypto/Subtle/Key/Wrap.js +++ b/src/Crypto/Subtle/Key/Wrap.js @@ -1,8 +1,6 @@ -"use strict"; - -exports.wrapKeyImpl = function wrapKeyImpl (f,x,k,a) { +export function wrapKeyImpl (f,x,k,a) { return crypto.subtle.wrapKey(f,x,k,a); }; -exports.unwrapKeyImpl = function unwrapKeyImpl (f,x,k,a,i,e,u) { +export function unwrapKeyImpl (f,x,k,a,i,e,u) { return crypto.subtle.unwrapKey(f,x,k,a,i,e,u); }; diff --git a/src/Crypto/Subtle/Key/Wrap.purs b/src/Crypto/Subtle/Key/Wrap.purs index a570005..6812f77 100644 --- a/src/Crypto/Subtle/Key/Wrap.purs +++ b/src/Crypto/Subtle/Key/Wrap.purs @@ -1,33 +1,30 @@ module Crypto.Subtle.Key.Wrap where -import Crypto.Subtle.Key.Types (CryptoKey, ExternalFormat, CryptoKeyUsage) -import Crypto.Subtle.Key.Import (ImportAlgorithm) +import Control.Promise (Promise, toAff') import Crypto.Subtle.Encrypt (EncryptAlgorithm) - -import Prelude ((<<<), (<$)) -import Data.Function.Uncurried (Fn4, Fn7, runFn4, runFn7) -import Data.Maybe (Maybe (..)) -import Data.Either (Either (..)) +import Crypto.Subtle.Key.Import (ImportAlgorithm) +import Crypto.Subtle.Key.Types (CryptoKey, CryptoKeyUsage, ExternalFormat, errorFromDOMException) import Data.ArrayBuffer.Types (ArrayBuffer) -import Effect.Promise (Promise, runPromise) -import Effect.Aff (Aff, makeAff, nonCanceler) +import Data.Function.Uncurried (Fn4, Fn7, runFn4, runFn7) +import Effect.Aff (Aff) foreign import wrapKeyImpl :: Fn4 ExternalFormat CryptoKey CryptoKey EncryptAlgorithm (Promise ArrayBuffer) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/wrapKey wrapKey :: ExternalFormat -> CryptoKey -- ^ Subject key -> CryptoKey -- ^ Wrapping key -> EncryptAlgorithm -> Aff ArrayBuffer -wrapKey f x k a = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn4 wrapKeyImpl f x k a) +wrapKey f x k a = toAff' errorFromDOMException (runFn4 wrapKeyImpl f x k a) foreign import unwrapKeyImpl :: Fn7 ExternalFormat ArrayBuffer CryptoKey EncryptAlgorithm ImportAlgorithm Boolean (Array CryptoKeyUsage) (Promise CryptoKey) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/unwrapKey unwrapKey :: ExternalFormat -> ArrayBuffer -- ^ Wrapped key -> CryptoKey -- ^ Unwrapping key @@ -35,6 +32,6 @@ unwrapKey :: ExternalFormat -> ImportAlgorithm -> Boolean -- ^ Extractable -> Array CryptoKeyUsage - -> Aff (Maybe CryptoKey) -unwrapKey f x k a i e u = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right <<< Just) (\_ -> resolve (Right Nothing)) (runFn7 unwrapKeyImpl f x k a i e u) + -- -> Aff (Maybe CryptoKey) + -> Aff CryptoKey +unwrapKey f x k a i e u = toAff' errorFromDOMException (runFn7 unwrapKeyImpl f x k a i e u) diff --git a/src/Crypto/Subtle/Sign.js b/src/Crypto/Subtle/Sign.js index 21114b0..ddeee99 100644 --- a/src/Crypto/Subtle/Sign.js +++ b/src/Crypto/Subtle/Sign.js @@ -1,8 +1,6 @@ -"use strict"; - -exports.signImpl = function signImpl (a,k,x) { +export function signImpl (a,k,x) { return crypto.subtle.sign(a,k,x); }; -exports.verifyImpl = function verifyImpl (a,k,s,x) { - return crypto.subtle.verify(a,k,s,x); -}; +export function verifyImpl(a, k, s, x) { + return crypto.subtle.verify(a, k, s, x); +} diff --git a/src/Crypto/Subtle/Sign.purs b/src/Crypto/Subtle/Sign.purs index 478deb4..695a0b3 100644 --- a/src/Crypto/Subtle/Sign.purs +++ b/src/Crypto/Subtle/Sign.purs @@ -3,15 +3,12 @@ module Crypto.Subtle.Sign , SignAlgorithm, rsaPKCS1, rsaPSS, ecdsa, hmac ) where -import Crypto.Subtle.Key.Types (CryptoKey) +import Control.Promise (Promise, toAff') import Crypto.Subtle.Hash (HashingFunction) - -import Prelude ((<<<), (<$)) -import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4) -import Data.Either (Either (..)) +import Crypto.Subtle.Key.Types (CryptoKey, errorFromDOMException) import Data.ArrayBuffer.Types (ArrayBuffer) -import Effect.Promise (Promise, runPromise) -import Effect.Aff (Aff, makeAff, nonCanceler) +import Data.Function.Uncurried (Fn3, Fn4, runFn3, runFn4) +import Effect.Aff (Aff) import Unsafe.Coerce (unsafeCoerce) @@ -21,10 +18,12 @@ foreign import data SignAlgorithm :: Type rsaPKCS1 :: SignAlgorithm rsaPKCS1 = unsafeCoerce {name: "RSASSA-PKCS1-v1_5"} +-- | https://developer.mozilla.org/en-US/docs/Web/API/RsaPssParams rsaPSS :: Int -- ^ Salt length -> SignAlgorithm rsaPSS l = unsafeCoerce {name: "RSA-PSS", saltLength: l} +-- | https://developer.mozilla.org/en-US/docs/Web/API/EcdsaParams ecdsa :: HashingFunction -> SignAlgorithm ecdsa h = unsafeCoerce {name: "ECDSA", hash: h} @@ -35,20 +34,20 @@ hmac = unsafeCoerce {name: "HMAC"} foreign import signImpl :: Fn3 SignAlgorithm CryptoKey ArrayBuffer (Promise ArrayBuffer) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/sign sign :: SignAlgorithm -> CryptoKey -> ArrayBuffer -> Aff ArrayBuffer -sign a k x = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn3 signImpl a k x) +sign a k x = toAff' errorFromDOMException (runFn3 signImpl a k x) foreign import verifyImpl :: Fn4 SignAlgorithm CryptoKey ArrayBuffer ArrayBuffer (Promise Boolean) +-- | https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/verify verify :: SignAlgorithm -> CryptoKey -> ArrayBuffer -- ^ Signature -> ArrayBuffer -- ^ Subject data -> Aff Boolean -verify a k s x = makeAff \resolve -> - nonCanceler <$ runPromise (resolve <<< Right) (resolve <<< Left) (runFn4 verifyImpl a k s x) +verify a k s x = toAff' errorFromDOMException (runFn4 verifyImpl a k s x) From da06d4a8c3b6d5e1e16b8f9e1106c8d1cecd9c9f Mon Sep 17 00:00:00 2001 From: James Brock Date: Tue, 29 Oct 2024 15:08:28 +0900 Subject: [PATCH 2/3] Publish yoga-subtlecrypto --- README.md | 6 +++++- bower.json | 51 ++++++++++++++++++++++++++++++--------------------- spago.dhall | 2 ++ 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index c3e4c32..b9c7c1f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ -# purescript-subtlecrypto +# purescript-yoga-subtlecrypto Simple bindings to the Browser's `window.crypto.subtle` API thing. *__Warning__*: This only works on specific browsers - not even node.js! + +## Provenance + +Forked from https://github.com/athanclark/purescript-subtlecrypto diff --git a/bower.json b/bower.json index f6d5285..ac9906d 100644 --- a/bower.json +++ b/bower.json @@ -1,23 +1,32 @@ { - "name": "purescript-subtlecrypto", - "ignore": [ - "**/.*", - "node_modules", - "bower_components", - "output" - ], - "license": "BSD-3-Clause", - "repository": { - "type": "git", - "url": "https://github.com/athanclark/purescript-subtlecrypto.git" - }, - "dependencies": { - "purescript-arraybuffer-types": "^2.0.0", - "purescript-aff": "^5.1.0", - "purescript-promises": "^3.0.0", - "purescript-foreign": "^5.0.0" - }, - "devDependencies": { - "purescript-psci-support": "^4.0.0" - } + "name": "purescript-subtlecrypto", + "license": [ + "BSD-3-Clause" + ], + "repository": { + "type": "git", + "url": "https://github.com/rowtype-yoga/purescript-yoga-subtlecrypto.git" + }, + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "output" + ], + "dependencies": { + "purescript-aff": "^v7.0.0", + "purescript-aff-promise": "^v4.0.0", + "purescript-arraybuffer-types": "^v3.0.2", + "purescript-console": "^v6.0.0", + "purescript-effect": "^v4.0.0", + "purescript-either": "^v6.0.0", + "purescript-exceptions": "^v6.0.0", + "purescript-foreign": "^v7.0.0", + "purescript-functions": "^v6.0.0", + "purescript-maybe": "^v6.0.0", + "purescript-prelude": "^v6.0.0", + "purescript-transformers": "^v6.0.0", + "purescript-tuples": "^v7.0.0", + "purescript-unsafe-coerce": "^v6.0.0" + } } diff --git a/spago.dhall b/spago.dhall index c495f0e..73d095f 100644 --- a/spago.dhall +++ b/spago.dhall @@ -21,4 +21,6 @@ You can edit this file as you like. ] , packages = ./packages.dhall , sources = [ "src/**/*.purs", "test/**/*.purs" ] +, license = "BSD-3-Clause" +, repository = "https://github.com/rowtype-yoga/purescript-yoga-subtlecrypto.git" } From fdd900e514eeb074d15d11f40679fb786381b059 Mon Sep 17 00:00:00 2001 From: James Brock Date: Tue, 29 Oct 2024 15:24:05 +0900 Subject: [PATCH 3/3] =?UTF-8?q?v0.0.1=20=E2=86=92=20v0.1.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit