Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Signing module with mkStdTx #166

Merged
merged 3 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cardano-wallet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ library
, http-types
, memory
, servant
, servant-server
, servant-client
, servant-client-core
, servant-server
, text
, time
, transformers
, vector
hs-source-dirs:
src
exposed-modules:
Expand All @@ -81,6 +82,7 @@ library
Cardano.Wallet.Primitive.AddressDiscovery
Cardano.Wallet.Primitive.Mnemonic
Cardano.Wallet.Primitive.Model
Cardano.Wallet.Primitive.Signing
Cardano.Wallet.Primitive.Types
Data.Quantity
Data.Text.Class
Expand Down
37 changes: 28 additions & 9 deletions src/Cardano/Wallet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ import Prelude
import Cardano.Wallet.Binary
( encodeSignedTx, toByteString )
import Cardano.Wallet.CoinSelection
( CoinSelection (..), CoinSelectionError (..), CoinSelectionOptions )
( CoinSelection (..)
, CoinSelectionError (..)
, CoinSelectionOptions
, shuffle
)
import Cardano.Wallet.DB
( DBLayer
, ErrNoSuchWallet (..)
Expand All @@ -58,9 +62,16 @@ import Cardano.Wallet.Primitive.AddressDerivation
, publicKey
)
import Cardano.Wallet.Primitive.AddressDiscovery
( AddressPoolGap, SeqState (..), mkAddressPool )
( AddressPoolGap
, SeqState (..)
, emptyPendingIxs
, generateChangeOutput
, mkAddressPool
)
import Cardano.Wallet.Primitive.Model
( Wallet, applyBlocks, availableUTxO, currentTip, getState, initWallet )
import Cardano.Wallet.Primitive.Signing
( SignTxError, mkStdTx )
import Cardano.Wallet.Primitive.Types
( Block (..)
, BlockHeader (..)
Expand Down Expand Up @@ -91,6 +102,8 @@ import Control.Monad.Trans.Except
( ExceptT, runExceptT, throwE, withExceptT )
import Control.Monad.Trans.Maybe
( MaybeT (..), maybeToExceptT )
import Control.Monad.Trans.State
( runState, state )
import Data.Functor
( ($>) )
import Data.List.NonEmpty
Expand Down Expand Up @@ -180,7 +193,7 @@ data ErrCreateUnsignedTx
-- | Errors occuring when signing a transaction
data ErrSignTx
= ErrSignTxNoSuchWallet ErrNoSuchWallet
| ErrSignTx
| ErrSignTx SignTxError

-- | Errors occuring when submitting a signed transaction to the network
data ErrSubmitTx = forall a. NetworkError a
Expand Down Expand Up @@ -214,6 +227,7 @@ mkWalletLayer db network = WalletLayer
let checkpoint = initWallet $ SeqState
{ externalPool = extPool
, internalPool = intPool
, pendingChangeIxs = emptyPendingIxs
}
now <- liftIO getCurrentTime
let metadata = WalletMetadata
Expand Down Expand Up @@ -252,11 +266,18 @@ mkWalletLayer db network = WalletLayer
withExceptT NetworkError $ postTx network signed

, signTx = \wid rootXPrv password (CoinSelection ins outs chgs) -> do
-- TODO: This is untested
(w, _) <- withExceptT ErrSignTxNoSuchWallet $ _readWallet wid
maybe
(throwE ErrSignTx)
return
(mkStdTx (getState w) rootXPrv password ins outs chgs)

let (changeOuts, _newState) = runState
(mapM (state . generateChangeOutput) chgs)
(getState w)

allShuffledOuts <- liftIO $ shuffle (outs ++ changeOuts)

case mkStdTx (getState w) (rootXPrv, password) ins allShuffledOuts of
Right a -> return a
Left e -> throwE $ ErrSignTx e
}
where
_readWallet
Expand Down Expand Up @@ -337,8 +358,6 @@ mkWalletLayer db network = WalletLayer
DB.putTxHistory db (PrimaryKey wid) txs
DB.putWalletMeta db (PrimaryKey wid) meta'

mkStdTx = error "TODO: mkStdTx not implemented yet"

{-------------------------------------------------------------------------------
Helpers
-------------------------------------------------------------------------------}
Expand Down
41 changes: 41 additions & 0 deletions src/Cardano/Wallet/CoinSelection.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,31 @@ module Cardano.Wallet.CoinSelection
CoinSelectionOptions (..)
, CoinSelectionError(..)
, CoinSelection(..)

-- * Helpers
, shuffle
) where

import Prelude

import Cardano.Wallet.Primitive.Types
( Coin (..), TxIn, TxOut (..) )
import Control.Monad
( forM_ )
import Crypto.Number.Generate
( generateBetween )
import Data.Vector.Mutable
( IOVector )
import Data.Word
( Word64 )
import Fmt
( Buildable (..), blockListF, blockListF', listF, nameF )
import GHC.Generics
( Generic )

import qualified Data.Vector as V
import qualified Data.Vector.Mutable as MV

{-------------------------------------------------------------------------------
Coin Selection
-------------------------------------------------------------------------------}
Expand Down Expand Up @@ -84,3 +96,32 @@ instance Buildable CoinSelection where
<> nameF "change" (listF chngs)
where
inpsF (txin, txout) = build txin <> " (~ " <> build txout <> ")"

{-------------------------------------------------------------------------------
Helpers
-------------------------------------------------------------------------------}

-- | Shuffles a list of elements.
--
-- >>> shuffle (outputs coinSel)
-- [...]
--
shuffle :: [a] -> IO [a]
Anviking marked this conversation as resolved.
Show resolved Hide resolved
shuffle = modifyInPlace $ \v -> do
let (lo, hi) = (0, MV.length v - 1)
forM_ [lo .. hi] $ \i -> do
j <- fromInteger <$> generateBetween (fromIntegral lo) (fromIntegral hi)
swapElems v i j
where
swapElems :: IOVector a -> Int -> Int -> IO ()
swapElems v i j = do
x <- MV.read v i
y <- MV.read v j
MV.write v i y
MV.write v j x

modifyInPlace :: forall a. (IOVector a -> IO ()) -> [a] -> IO [a]
modifyInPlace f xs = do
v' <- V.thaw $ V.fromList xs
f v'
V.toList <$> V.freeze v'
3 changes: 2 additions & 1 deletion src/Cardano/Wallet/Primitive/AddressDerivation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ module Cardano.Wallet.Primitive.AddressDerivation
-- * Polymorphic / General Purpose Types
-- $use
Key
, getKey
, Depth (..)
, Index
, getIndex
Expand Down Expand Up @@ -132,7 +133,7 @@ import qualified Data.Text.Encoding as T
-- let accountPubKey = Key 'AccountK XPub
-- let addressPubKey = Key 'AddressK XPub
-- @
newtype Key (level :: Depth) key = Key key
newtype Key (level :: Depth) key = Key { getKey :: key }
deriving stock (Generic, Show, Eq)

instance (NFData key) => NFData (Key level key)
Expand Down
Loading