Skip to content

Commit

Permalink
use ShelleyKey, all withMultisigLayer, align code, add some instances
Browse files Browse the repository at this point in the history
hlint
  • Loading branch information
paweljakubas committed May 5, 2021
1 parent eda2677 commit 5f40367
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 23 deletions.
3 changes: 2 additions & 1 deletion lib/core-integration/src/Test/Integration/Framework/DSL.hs
Expand Up @@ -413,6 +413,7 @@ import Web.HttpApiData
import qualified Cardano.Wallet.Api.Link as Link
import qualified Cardano.Wallet.Primitive.AddressDerivation.Byron as Byron
import qualified Cardano.Wallet.Primitive.AddressDerivation.Icarus as Icarus
import qualified Cardano.Wallet.Primitive.AddressDerivation.Shared as Shared
import qualified Cardano.Wallet.Primitive.AddressDerivation.Shelley as Shelley
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Cardano.Wallet.Primitive.Types.TokenBundle as TokenBundle
Expand Down Expand Up @@ -834,7 +835,7 @@ accPubKeyFromMnemonics mnemonic1 mnemonic2 ix passphrase =
T.decodeUtf8 $ serializeXPub $ publicKey $
deriveAccountPrivateKey passphrase rootXPrv (Index $ 2147483648 + ix)
where
rootXPrv = Shelley.generateKeyFromSeed (mnemonic1, mnemonic2) passphrase
rootXPrv = Shared.generateKeyFromSeed (mnemonic1, mnemonic2) passphrase

genXPubs :: Int -> IO [(XPub,Text)]
genXPubs num =
Expand Down
Expand Up @@ -31,8 +31,8 @@ import Cardano.Wallet.Api.Types
)
import Cardano.Wallet.Primitive.AddressDerivation
( DerivationIndex (..), Passphrase (..), PaymentAddress, Role (..), hex )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey )
import Cardano.Wallet.Primitive.AddressDerivation.Shared
( SharedKey )
import Cardano.Wallet.Primitive.AddressDiscovery.Script
( CredentialType (..) )
import Cardano.Wallet.Primitive.SyncProgress
Expand Down Expand Up @@ -99,7 +99,7 @@ spec :: forall n.
( DecodeAddress n
, DecodeStakeAddress n
, EncodeAddress n
, PaymentAddress n ShelleyKey
, PaymentAddress n SharedKey
) => SpecWith Context
spec = describe "SHARED_WALLETS" $ do
it "SHARED_WALLETS_CREATE_01 - Create an active shared wallet from root xprv" $ \ctx -> runResourceT $ do
Expand Down Expand Up @@ -159,7 +159,7 @@ spec = describe "SHARED_WALLETS" $ do
, expectField #extended (`shouldBe` True)
]
let (ApiAccountKeyShared bytes _) = getFromResponse id rKey
(T.decodeUtf8 $ hex bytes) `Expectations.shouldBe` accXPubDerived
T.decodeUtf8 (hex bytes) `Expectations.shouldBe` accXPubDerived

it "SHARED_WALLETS_CREATE_02 - Create a pending shared wallet from root xprv" $ \ctx -> runResourceT $ do
m15txt <- liftIO $ genMnemonics M15
Expand Down Expand Up @@ -212,7 +212,7 @@ spec = describe "SHARED_WALLETS" $ do
, expectField #extended (`shouldBe` True)
]
let (ApiAccountKeyShared bytes _) = getFromResponse id rKey
(T.decodeUtf8 $ hex bytes) `Expectations.shouldBe` accXPubDerived
T.decodeUtf8 (hex bytes) `Expectations.shouldBe` accXPubDerived


it "SHARED_WALLETS_CREATE_03 - Create an active shared wallet from account xpub" $ \ctx -> runResourceT $ do
Expand Down
8 changes: 4 additions & 4 deletions lib/core/src/Cardano/Wallet.hs
Expand Up @@ -2124,8 +2124,8 @@ derivePublicKeyShelley
-> Role
-> DerivationIndex
-> ExceptT ErrDerivePublicKey IO (k 'AddressK XPub)
derivePublicKeyShelley ctx wid role_ ix =
derivePublicKey @ctx @s @k ctx toAccXPubShelley wid role_ ix
derivePublicKeyShelley ctx =
derivePublicKey @ctx @s @k ctx toAccXPubShelley

derivePublicKeyShared
:: forall ctx s k n.
Expand All @@ -2138,8 +2138,8 @@ derivePublicKeyShared
-> Role
-> DerivationIndex
-> ExceptT ErrDerivePublicKey IO (k 'AddressK XPub)
derivePublicKeyShared ctx wid role_ ix =
derivePublicKey @ctx @s @k ctx toAccXPubShared wid role_ ix
derivePublicKeyShared ctx =
derivePublicKey @ctx @s @k ctx toAccXPubShared

toAccXPubShared
:: SharedState n k
Expand Down
32 changes: 32 additions & 0 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Expand Up @@ -98,6 +98,7 @@ module Cardano.Wallet.Api.Server
, mkLegacyWallet
, withLegacyLayer
, withLegacyLayer'
, withMultisigLayer
, rndStateChange
, withWorkerCtx
, getCurrentEpoch
Expand Down Expand Up @@ -291,6 +292,8 @@ import Cardano.Wallet.Primitive.AddressDerivation.Byron
( ByronKey, mkByronKeyFromMasterKey )
import Cardano.Wallet.Primitive.AddressDerivation.Icarus
( IcarusKey )
import Cardano.Wallet.Primitive.AddressDerivation.Shared
( SharedKey )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey )
import Cardano.Wallet.Primitive.AddressDiscovery
Expand Down Expand Up @@ -1276,6 +1279,35 @@ withLegacyLayer' (ApiT wid)
onNotResponding
(const withByron)

withMultisigLayer
:: forall shared n a.
( shared ~ ApiLayer (SharedState n SharedKey) SharedKey
)
=> ApiT WalletId
-> (shared, Handler a)
-> Handler a
withMultisigLayer (ApiT wid) (shared, withShared) =
withMultisigLayer' (ApiT wid) (shared, withShared, liftE)

withMultisigLayer'
:: forall shared n a.
( shared ~ ApiLayer (SharedState n SharedKey) SharedKey
)
=> ApiT WalletId
-> (shared, Handler a, ErrWalletNotResponding -> Handler a)
-> Handler a
withMultisigLayer' (ApiT wid) (shared, withShared, deadShared) =
tryShared liftE deadShared
where
tryShared onMissing onNotResponding = withWorkerCtx @_
@(SharedState n SharedKey)
@SharedKey
shared
wid
onMissing
onNotResponding
(const withShared)

{-------------------------------------------------------------------------------
Wallets
-------------------------------------------------------------------------------}
Expand Down
74 changes: 72 additions & 2 deletions lib/core/src/Cardano/Wallet/Primitive/AddressDerivation/Shared.hs
Expand Up @@ -5,7 +5,6 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
Expand All @@ -30,15 +29,21 @@ module Cardano.Wallet.Primitive.AddressDerivation.Shared

import Prelude

import Cardano.Address.Derivation
( xpubPublicKey )
import Cardano.Crypto.Wallet
( XPrv, toXPub, unXPrv, unXPub, xPrvChangePass, xprv, xpub )
( XPrv, XPub, toXPub, unXPrv, unXPub, xPrvChangePass, xprv, xpub )
import Cardano.Mnemonic
( SomeMnemonic )
import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..)
, DerivationType (..)
, HardDerivation (..)
, KeyFingerprint (..)
, MkKeyFingerprint (..)
, NetworkDiscriminant (..)
, Passphrase (..)
, PaymentAddress (..)
, PersistPrivateKey (..)
, PersistPublicKey (..)
, SoftDerivation (..)
Expand All @@ -56,6 +61,8 @@ import Cardano.Wallet.Primitive.AddressDiscovery.Sequential
( GetPurpose (..) )
import Cardano.Wallet.Primitive.AddressDiscovery.SharedState
( purposeCIP1854 )
import Cardano.Wallet.Primitive.Types.Address
( Address (..) )
import Cardano.Wallet.Primitive.Types.Hash
( Hash (..) )
import Control.DeepSeq
Expand All @@ -64,11 +71,24 @@ import Control.Monad
( (<=<) )
import Crypto.Hash
( hash )
import Crypto.Hash.Algorithms
( Blake2b_224 (..) )
import Crypto.Hash.IO
( HashAlgorithm (hashDigestSize) )
import Crypto.Hash.Utils
( blake2b224 )
import Data.Binary.Put
( putByteString, putWord8, runPut )
import Data.ByteString
( ByteString )
import Data.Proxy
( Proxy (..) )
import GHC.Generics
( Generic )

import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL

{-------------------------------------------------------------------------------
Sequential Derivation
-------------------------------------------------------------------------------}
Expand Down Expand Up @@ -176,3 +196,53 @@ instance PersistPublicKey (SharedKey depth) where
either err SharedKey . (xpub <=< fromHex @ByteString)
where
err _ = error "unsafeDeserializeXPub: unable to deserialize SharedKey"

instance MkKeyFingerprint SharedKey Address where
paymentKeyFingerprint (Address bytes) =
Right $ KeyFingerprint $ BS.take hashSize $ BS.drop 1 bytes

instance MkKeyFingerprint SharedKey (Proxy (n :: NetworkDiscriminant), SharedKey 'AddressK XPub) where
paymentKeyFingerprint (_, paymentK) =
Right $ KeyFingerprint $ blake2b224 $ xpubPublicKey $ getKey paymentK

instance PaymentAddress 'Mainnet SharedKey where
paymentAddress paymentK = do
Address $ BL.toStrict $ runPut $ do
putWord8 (enterprise + networkId)
putByteString . blake2b224 . xpubPublicKey . getKey $ paymentK
where
enterprise = 96
networkId = 1

liftPaymentAddress (KeyFingerprint fingerprint) =
Address $ BL.toStrict $ runPut $ do
putWord8 (enterprise + networkId)
putByteString fingerprint
where
enterprise = 96
networkId = 1

instance PaymentAddress ('Testnet pm) SharedKey where
paymentAddress paymentK =
Address $ BL.toStrict $ runPut $ do
putWord8 (enterprise + networkId)
putByteString . blake2b224 . xpubPublicKey . getKey $ paymentK
where
enterprise = 96
networkId = 0

liftPaymentAddress (KeyFingerprint fingerprint) =
Address $ BL.toStrict $ runPut $ do
putWord8 (enterprise + networkId)
putByteString fingerprint
where
enterprise = 96
networkId = 0

{-------------------------------------------------------------------------------
Internals
-------------------------------------------------------------------------------}

hashSize :: Int
hashSize =
hashDigestSize Blake2b_224
5 changes: 4 additions & 1 deletion lib/shelley/src/Cardano/Wallet/Shelley.hs
Expand Up @@ -92,6 +92,8 @@ import Cardano.Wallet.Primitive.AddressDerivation.Byron
( ByronKey )
import Cardano.Wallet.Primitive.AddressDerivation.Icarus
( IcarusKey )
import Cardano.Wallet.Primitive.AddressDerivation.Shared
( SharedKey )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey )
import Cardano.Wallet.Primitive.AddressDiscovery
Expand Down Expand Up @@ -203,6 +205,7 @@ data SomeNetworkDiscriminant where
, PaymentAddress n IcarusKey
, PaymentAddress n ByronKey
, PaymentAddress n ShelleyKey
, PaymentAddress n SharedKey
, DelegationAddress n ShelleyKey
, HasNetworkId n
, DecodeAddress n
Expand Down Expand Up @@ -326,7 +329,7 @@ serveWallet
-> ApiLayer (RndState n) ByronKey
-> ApiLayer (SeqState n IcarusKey) IcarusKey
-> ApiLayer (SeqState n ShelleyKey) ShelleyKey
-> ApiLayer (SharedState n ShelleyKey) ShelleyKey
-> ApiLayer (SharedState n SharedKey) SharedKey
-> StakePoolLayer
-> NtpClient
-> IO ()
Expand Down
38 changes: 28 additions & 10 deletions lib/shelley/src/Cardano/Wallet/Shelley/Api/Server.hs
Expand Up @@ -113,6 +113,7 @@ import Cardano.Wallet.Api.Server
, signMetadata
, withLegacyLayer
, withLegacyLayer'
, withMultisigLayer
)
import Cardano.Wallet.Api.Types
( AnyAddress (..)
Expand Down Expand Up @@ -142,8 +143,10 @@ import Cardano.Wallet.Primitive.AddressDerivation.Byron
( ByronKey )
import Cardano.Wallet.Primitive.AddressDerivation.Icarus
( IcarusKey (..) )
import Cardano.Wallet.Primitive.AddressDerivation.Shared
( SharedKey (..) )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey (..), generateKeyFromSeed )
( ShelleyKey (..) )
import Cardano.Wallet.Primitive.AddressDiscovery.Random
( RndState )
import Cardano.Wallet.Primitive.AddressDiscovery.Script
Expand Down Expand Up @@ -193,6 +196,8 @@ import qualified Cardano.Address.Derivation as CA
import qualified Cardano.Address.Script as CA
import qualified Cardano.Address.Style.Shelley as CA
import qualified Cardano.Api as Cardano
import qualified Cardano.Wallet.Primitive.AddressDerivation.Shared as Shared
import qualified Cardano.Wallet.Primitive.AddressDerivation.Shelley as Shelley
import qualified Data.ByteString as BS
import qualified Data.Text as T

Expand All @@ -207,7 +212,7 @@ server
=> ApiLayer (RndState n) ByronKey
-> ApiLayer (SeqState n IcarusKey) IcarusKey
-> ApiLayer (SeqState n ShelleyKey) ShelleyKey
-> ApiLayer (SharedState n ShelleyKey) ShelleyKey
-> ApiLayer (SharedState n SharedKey) SharedKey
-> StakePoolLayer
-> NtpClient
-> Server (Api n ApiStakePool)
Expand Down Expand Up @@ -237,7 +242,7 @@ server byron icarus shelley multisig spl ntp =
wallets = deleteWallet shelley
:<|> (fmap fst . getWallet shelley mkShelleyWallet)
:<|> (fmap fst <$> listWallets shelley mkShelleyWallet)
:<|> postWallet shelley generateKeyFromSeed ShelleyKey
:<|> postWallet shelley Shelley.generateKeyFromSeed ShelleyKey
:<|> putWallet shelley mkShelleyWallet
:<|> putWalletPassphrase shelley
:<|> getUTxOsStatistics shelley
Expand Down Expand Up @@ -493,15 +498,28 @@ server byron icarus shelley multisig spl ntp =

sharedWallets :: Server SharedWallets
sharedWallets =
postSharedWallet multisig generateKeyFromSeed ShelleyKey
:<|> (fmap fst . getWallet multisig mkSharedWallet)
:<|> patchSharedWallet multisig ShelleyKey Payment
:<|> patchSharedWallet multisig ShelleyKey Delegation
:<|> deleteWallet multisig
postSharedWallet @_ @_ @SharedKey multisig Shared.generateKeyFromSeed SharedKey
:<|> (\wid -> withMultisigLayer wid
(multisig, fst <$> getWallet multisig mkSharedWallet wid)
)
:<|> (\wid p -> withMultisigLayer wid
(multisig, patchSharedWallet @_ @_ @SharedKey multisig SharedKey Payment wid p)
)
:<|> (\wid p -> withMultisigLayer wid
(multisig, patchSharedWallet @_ @_ @SharedKey multisig SharedKey Delegation wid p)
)
:<|> (\wid -> withMultisigLayer wid
(multisig, deleteWallet multisig wid)
)

sharedWalletKeys :: Server SharedWalletKeys
sharedWalletKeys = derivePublicKeyShared multisig
:<|> postAccountPublicKeyShared multisig
sharedWalletKeys =
(\wid r ix h -> withMultisigLayer wid
(multisig, derivePublicKeyShared multisig wid r ix h)
)
:<|> (\wid ix acc -> withMultisigLayer wid
(multisig, postAccountPublicKeyShared multisig wid ix acc)
)

postAnyAddress
:: NetworkId
Expand Down
5 changes: 5 additions & 0 deletions lib/shelley/src/Cardano/Wallet/Shelley/Transaction.hs
Expand Up @@ -70,6 +70,8 @@ import Cardano.Wallet.Primitive.AddressDerivation.Byron
( ByronKey )
import Cardano.Wallet.Primitive.AddressDerivation.Icarus
( IcarusKey )
import Cardano.Wallet.Primitive.AddressDerivation.Shared
( SharedKey )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey, toRewardAccountRaw )
import Cardano.Wallet.Primitive.CoinSelection.MA.RoundRobin
Expand Down Expand Up @@ -247,6 +249,9 @@ instance TxWitnessTagFor IcarusKey where
instance TxWitnessTagFor ByronKey where
txWitnessTagFor = TxWitnessByronUTxO Byron

instance TxWitnessTagFor SharedKey where
txWitnessTagFor = TxWitnessShelleyUTxO

mkTx
:: forall k era.
( TxWitnessTagFor k
Expand Down

0 comments on commit 5f40367

Please sign in to comment.