63 lines
1.8 KiB
Haskell
63 lines
1.8 KiB
Haskell
-- Copyright 2022-2024 Vergara Technologies LLC
|
|
--
|
|
-- This file is part of Zcash-Haskell.
|
|
--
|
|
-- |
|
|
-- Module : ZcashHaskell.Transparent
|
|
-- Copyright : 2022-2024 Vergara Technologies
|
|
-- License : MIT
|
|
--
|
|
-- Maintainer : pitmutt@vergara.tech
|
|
-- Stability : experimental
|
|
-- Portability : unknown
|
|
--
|
|
-- Functions to interact with the transparent addresses in the Zcash blockchain
|
|
--
|
|
module ZcashHaskell.Transparent where
|
|
|
|
import Crypto.Hash
|
|
import qualified Data.ByteArray as BA
|
|
import qualified Data.ByteString as BS
|
|
import Data.ByteString.Base58 (bitcoinAlphabet, encodeBase58)
|
|
import qualified Data.Text as T
|
|
import qualified Data.Text.Encoding as E
|
|
import Data.Word
|
|
import ZcashHaskell.Types
|
|
( TransparentAddress(..)
|
|
, TransparentType(..)
|
|
, ZcashNet(..)
|
|
, getTransparentPrefix
|
|
)
|
|
|
|
import Haskoin.Crypto.Keys.Extended
|
|
import Data.Word
|
|
import Crypto.Secp256k1
|
|
|
|
encodeTransparent :: TransparentAddress -> T.Text
|
|
encodeTransparent t =
|
|
encodeTransparent' (getTransparentPrefix (ta_net t) (ta_type t)) $ ta_bytes t
|
|
where
|
|
encodeTransparent' :: (Word8, Word8) -> BS.ByteString -> T.Text
|
|
encodeTransparent' (a, b) h =
|
|
E.decodeUtf8 $ encodeBase58 bitcoinAlphabet $ digest <> BS.take 4 checksum
|
|
where
|
|
sha256 :: BS.ByteString -> BS.ByteString
|
|
sha256 bs = BA.convert (hash bs :: Digest SHA256)
|
|
digest = BS.pack [a, b] <> h
|
|
checksum = sha256 $ sha256 digest
|
|
|
|
-- | Attempts to generate an Extended Private Key from a known HDSeed.
|
|
genTransparentPrvKey ::
|
|
BS.ByteString -> XPrvKey
|
|
genTransparentPrvKey hdseed = do
|
|
makeXPrvKey hdseed
|
|
|
|
-- | Attempts to obtain an Extended Public Key from a known Extended Private Key
|
|
genTransparentPubKey ::
|
|
XPrvKey -> IO XPubKey
|
|
genTransparentPubKey xpvk = do
|
|
ioCtx <- createContext
|
|
let xpubk = deriveXPubKey ioCtx xpvk
|
|
return xpubk
|
|
|
|
|