2024-01-18 18:55:23 +00:00
|
|
|
-- Copyright 2022-2024 Vergara Technologies LLC
|
|
|
|
--
|
|
|
|
-- This file is part of Zcash-Haskell.
|
|
|
|
--
|
2024-01-12 15:46:26 +00:00
|
|
|
-- |
|
|
|
|
-- Module : ZcashHaskell.Transparent
|
|
|
|
-- Copyright : 2022-2024 Vergara Technologies
|
2024-01-18 18:55:23 +00:00
|
|
|
-- License : MIT
|
2024-01-12 15:46:26 +00:00
|
|
|
--
|
|
|
|
-- Maintainer : pitmutt@vergara.tech
|
|
|
|
-- Stability : experimental
|
|
|
|
-- Portability : unknown
|
|
|
|
--
|
|
|
|
-- Functions to interact with the transparent addresses in the Zcash blockchain
|
|
|
|
--
|
|
|
|
module ZcashHaskell.Transparent where
|
|
|
|
|
2024-03-12 21:03:35 +00:00
|
|
|
import Control.Exception (throwIO)
|
2024-01-16 22:15:05 +00:00
|
|
|
import Crypto.Hash
|
2024-03-15 15:11:27 +00:00
|
|
|
import Crypto.Secp256k1
|
2024-01-16 22:15:05 +00:00
|
|
|
import qualified Data.ByteArray as BA
|
2024-01-12 15:46:26 +00:00
|
|
|
import qualified Data.ByteString as BS
|
2024-01-16 22:15:05 +00:00
|
|
|
import Data.ByteString.Base58 (bitcoinAlphabet, encodeBase58)
|
2024-03-15 15:11:27 +00:00
|
|
|
import Data.HexString
|
2024-01-16 22:15:05 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Text.Encoding as E
|
2024-03-15 15:11:27 +00:00
|
|
|
import Data.Word
|
|
|
|
import Haskoin.Address (Address(..))
|
|
|
|
import qualified Haskoin.Crypto.Hash as H
|
|
|
|
import Haskoin.Crypto.Keys.Extended
|
2024-01-16 22:15:05 +00:00
|
|
|
import ZcashHaskell.Types
|
2024-03-13 19:12:28 +00:00
|
|
|
( AccountId
|
2024-03-15 15:11:27 +00:00
|
|
|
, CoinType(..)
|
|
|
|
, Scope(..)
|
2024-03-14 16:13:10 +00:00
|
|
|
, Seed(..)
|
|
|
|
, ToBytes(..)
|
2024-03-13 19:12:28 +00:00
|
|
|
, TransparentAddress(..)
|
2024-01-16 22:15:05 +00:00
|
|
|
, TransparentType(..)
|
|
|
|
, ZcashNet(..)
|
2024-03-06 19:05:00 +00:00
|
|
|
, getTransparentPrefix
|
2024-03-14 21:44:18 +00:00
|
|
|
, getValue
|
2024-01-16 22:15:05 +00:00
|
|
|
)
|
|
|
|
|
2024-03-12 21:03:35 +00:00
|
|
|
encodeTransparent :: ZcashNet -> TransparentAddress -> T.Text
|
|
|
|
encodeTransparent zNet t =
|
|
|
|
encodeTransparent' (getTransparentPrefix zNet (ta_type t)) $
|
|
|
|
toBytes $ ta_bytes t
|
2024-01-16 22:15:05 +00:00
|
|
|
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
|
2024-03-03 21:19:06 +00:00
|
|
|
|
2024-03-15 15:11:27 +00:00
|
|
|
-- | Generate an Extended Private Key from a known HDSeed.
|
2024-03-14 21:44:18 +00:00
|
|
|
genTransparentPrvKey :: Seed -> CoinType -> AccountId -> IO XPrvKey
|
2024-03-15 15:11:27 +00:00
|
|
|
genTransparentPrvKey hdseed ctype accid = do
|
2024-03-14 21:44:18 +00:00
|
|
|
let coin = getValue ctype
|
|
|
|
ioCtx <- createContext
|
2024-03-15 15:11:27 +00:00
|
|
|
let path = Deriv :| 44 :| coin :| fromIntegral accid :: DerivPath
|
2024-03-14 16:13:10 +00:00
|
|
|
let prvKey = makeXPrvKey $ getBytes hdseed
|
2024-03-15 15:11:27 +00:00
|
|
|
return $ derivePath ioCtx path prvKey
|
2024-03-14 21:44:18 +00:00
|
|
|
|
|
|
|
genTransparentPubKey :: XPrvKey -> IO XPubKey
|
2024-03-15 15:11:27 +00:00
|
|
|
genTransparentPubKey xPrvKey = do
|
2024-03-14 21:44:18 +00:00
|
|
|
ioCtx <- createContext
|
|
|
|
return $ deriveXPubKey ioCtx xPrvKey
|
|
|
|
|
|
|
|
genTransparentPubAddress :: XPubKey -> IO Address
|
2024-03-15 15:11:27 +00:00
|
|
|
genTransparentPubAddress xPubKey = do
|
2024-03-12 21:03:35 +00:00
|
|
|
ioCtx <- createContext
|
2024-03-14 21:44:18 +00:00
|
|
|
return $ xPubAddr ioCtx xPubKey
|
2024-03-03 21:19:06 +00:00
|
|
|
|
2024-03-13 19:12:28 +00:00
|
|
|
-- | Generate a transparent receiver
|
2024-03-15 15:11:27 +00:00
|
|
|
genTransparentReceiver :: Int -> Scope -> XPrvKey -> IO TransparentAddress
|
|
|
|
genTransparentReceiver i scope xprvk = do
|
2024-03-12 21:03:35 +00:00
|
|
|
ioCtx <- createContext
|
2024-03-15 15:11:27 +00:00
|
|
|
let s =
|
|
|
|
case scope of
|
|
|
|
External -> 0
|
|
|
|
Internal -> 1
|
|
|
|
let path = Deriv :/ s :/ fromIntegral i :: DerivPath
|
|
|
|
let childPrvKey = derivePath ioCtx path xprvk
|
|
|
|
let childPubKey = deriveXPubKey ioCtx childPrvKey
|
2024-03-13 19:12:28 +00:00
|
|
|
let x = xPubAddr ioCtx childPubKey
|
2024-03-12 21:03:35 +00:00
|
|
|
case x of
|
|
|
|
PubKeyAddress k -> return $ TransparentAddress P2PKH $ fromBinary k
|
|
|
|
ScriptAddress j -> return $ TransparentAddress P2SH $ fromBinary j
|
|
|
|
_anyOtherKind -> throwIO $ userError "Unsupported transparent address type"
|