commit a142587677bc0e08bcf395ed88643fd3521fa3c3
parent 3501a14be9ff7023034c4a66cee54307bde3212c
Author: Jared Tobin <jared@jtobin.io>
Date: Sat, 18 Jan 2025 22:22:41 +0400
lib: add pinned check
Originally I had thought to hide the Script constructor, but this is a better
solution.
Diffstat:
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/lib/Bitcoin/Prim/Script.hs b/lib/Bitcoin/Prim/Script.hs
@@ -29,6 +29,7 @@ module Bitcoin.Prim.Script (
, bs_to_ba
) where
+import Control.Monad (guard)
import qualified Crypto.Hash.RIPEMD160 as RIPEMD160
import qualified Crypto.Hash.SHA256 as SHA256
import qualified Data.Bits as B
@@ -62,6 +63,7 @@ fi = fromIntegral
-- convert a pinned ByteArray to a ByteString
ba_to_bs :: BA.ByteArray -> BS.ByteString
ba_to_bs ba = unsafeDupablePerformIO $ do
+ guard (BA.isByteArrayPinned ba)
let l = BA.sizeofByteArray ba
buf <- mallocPlainForeignPtrBytes l
withForeignPtr buf $ \p ->
@@ -89,9 +91,6 @@ hilo b =
-- types ----------------------------------------------------------------------
--- XX since we have to export the constructor, add checks that
--- bytearrays are pinned as required
-
-- | A Script program, represented as a 'ByteArray'.
newtype Script = Script BA.ByteArray
deriving (Eq, Show)