-
Notifications
You must be signed in to change notification settings - Fork 211
/
Transaction.hs
558 lines (512 loc) · 20.1 KB
/
Transaction.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
-- |
-- Copyright: © 2020 IOHK
-- License: Apache-2.0
--
-- Working with Shelley transactions.
module Cardano.Wallet.Shelley.Transaction
( newTransactionLayer
-- * Internals
, _minimumFee
, _decodeSignedTx
, _estimateMaxNumberOfInputs
, mkUnsignedShelleyTx
, mkShelleyWitness
, mkByronWitness
, realFee
) where
import Prelude
import Cardano.Address.Derivation
( XPrv, XPub, toXPub, xpubPublicKey )
import Cardano.Binary
( serialize' )
import Cardano.Crypto.DSIGN
( DSIGNAlgorithm (..), SignedDSIGN (..) )
import Cardano.Crypto.DSIGN.Ed25519
( VerKeyDSIGN (..) )
import Cardano.Wallet.Primitive.AddressDerivation
( Depth (..), NetworkDiscriminant (..), Passphrase (..), WalletKey (..) )
import Cardano.Wallet.Primitive.AddressDerivation.Byron
( ByronKey )
import Cardano.Wallet.Primitive.AddressDerivation.Icarus
( IcarusKey )
import Cardano.Wallet.Primitive.AddressDerivation.Shelley
( ShelleyKey )
import Cardano.Wallet.Primitive.CoinSelection
( CoinSelection (..) )
import Cardano.Wallet.Primitive.Fee
( Fee (..), FeePolicy (..) )
import Cardano.Wallet.Primitive.Types
( Address (..)
, Coin (..)
, EpochLength (..)
, Hash (..)
, PoolId (..)
, ProtocolMagic (..)
, SealedTx (..)
, SlotId (..)
, Tx (..)
, TxIn (..)
, TxOut (..)
, WalletDelegation (..)
, WalletDelegationStatus (..)
)
import Cardano.Wallet.Shelley.Compatibility
( Shelley
, TPraosStandardCrypto
, toCardanoLovelace
, toCardanoNetwork
, toCardanoTxIn
, toCardanoTxOut
, toSealed
, toSlotNo
, toStakeKeyDeregCert
, toStakeKeyRegCert
, toStakePoolDlgCert
)
import Cardano.Wallet.Transaction
( Certificate (..)
, ErrDecodeSignedTx (..)
, ErrMkTx (..)
, ErrValidateSelection
, TransactionLayer (..)
)
import Control.Arrow
( second )
import Control.Monad
( forM )
import Crypto.Error
( throwCryptoError )
import Crypto.Hash.Utils
( blake2b256 )
import Data.ByteString
( ByteString )
import Data.List
( find )
import Data.Maybe
( fromMaybe, isJust )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
( Quantity (..) )
import Data.Word
( Word16, Word8 )
import Ouroboros.Consensus.Shelley.Protocol.Crypto
( Crypto (..) )
import Ouroboros.Network.Block
( SlotNo )
import qualified Cardano.Api as Cardano
import qualified Cardano.Byron.Codec.Cbor as CBOR
import qualified Cardano.Crypto as Crypto
import qualified Cardano.Crypto.Hash.Class as Hash
import qualified Cardano.Crypto.Wallet as CC
import qualified Cardano.Wallet.Primitive.Types as W
import qualified Codec.CBOR.Encoding as CBOR
import qualified Codec.CBOR.Read as CBOR
import qualified Codec.CBOR.Write as CBOR
import qualified Crypto.PubKey.Ed25519 as Ed25519
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Char8 as L8
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Shelley.Spec.Ledger.Address.Bootstrap as SL
import qualified Shelley.Spec.Ledger.BaseTypes as SL
import qualified Shelley.Spec.Ledger.Keys as SL
import qualified Shelley.Spec.Ledger.LedgerState as SL
import qualified Shelley.Spec.Ledger.Tx as SL
import qualified Shelley.Spec.Ledger.TxData as SL
import qualified Shelley.Spec.Ledger.UTxO as SL
data TxWitnessTag
= TxWitnessByronUTxO
| TxWitnessShelleyUTxO
deriving (Show, Eq)
-- | Provide a transaction witness for a given private key. The type of witness
-- is different between types of keys and, with backward-compatible support, we
-- need to support many types for one backend target.
class TxWitnessTagFor (k :: Depth -> * -> *) where
txWitnessTagFor :: TxWitnessTag
instance TxWitnessTagFor ShelleyKey where txWitnessTagFor = TxWitnessShelleyUTxO
instance TxWitnessTagFor IcarusKey where txWitnessTagFor = TxWitnessByronUTxO
instance TxWitnessTagFor ByronKey where txWitnessTagFor = TxWitnessByronUTxO
newTransactionLayer
:: forall (n :: NetworkDiscriminant) k t.
( t ~ IO Shelley
, TxWitnessTagFor k
, WalletKey k
)
=> Proxy n
-> ProtocolMagic
-> EpochLength
-> TransactionLayer t k
newTransactionLayer _proxy protocolMagic epochLength = TransactionLayer
{ mkStdTx = _mkStdTx
, mkDelegationJoinTx = _mkDelegationJoinTx
, mkDelegationQuitTx = _mkDelegationQuitTx
, decodeSignedTx = _decodeSignedTx
, minimumFee = _minimumFee
, estimateMaxNumberOfInputs = _estimateMaxNumberOfInputs
, validateSelection = const $ return ()
, allowUnbalancedTx = True
}
where
_mkStdTx
:: (Address -> Maybe (k 'AddressK XPrv, Passphrase "encryption"))
-> SlotId -- ^ The current slot
-> [(TxIn, TxOut)]
-> [TxOut]
-> Either ErrMkTx (Tx, SealedTx)
_mkStdTx keyFrom slot ownedIns outs = do
let timeToLive = defaultTTL epochLength slot
let fee = realFee ownedIns outs
let unsigned = mkUnsignedShelleyTx timeToLive ownedIns outs [] fee
let metadata = SL.SNothing
wits <- case (txWitnessTagFor @k) of
TxWitnessShelleyUTxO -> do
addrWits <- fmap Set.fromList $ forM ownedIns $ \(_, TxOut addr _) -> do
(k, pwd) <- lookupPrivateKey keyFrom addr
pure $ mkShelleyWitness unsigned (getRawKey k, pwd)
pure $ SL.WitnessSet addrWits mempty mempty
TxWitnessByronUTxO -> do
let toSigningKey (k,_) = Crypto.SigningKey $ getRawKey k
bootstrapWits <- fmap Set.fromList $ forM ownedIns $ \(_, TxOut addr _) -> do
pair <- lookupPrivateKey keyFrom addr
let signingKeys = toSigningKey pair
pure $ SL.makeBootstrapWitness _ signingKeys _
pure $ SL.WitnessSet mempty mempty bootstrapWits
pure $ toSealed $ SL.Tx unsigned wits metadata
_mkDelegationJoinTx
:: FeePolicy
-- Latest fee policy
-> WalletDelegation
-- Wallet current delegation status
-> PoolId
-- Pool Id to which we're planning to delegate
-> (k 'AddressK XPrv, Passphrase "encryption")
-- Reward account
-> (Address -> Maybe (k 'AddressK XPrv, Passphrase "encryption"))
-- Key store
-> SlotId
-- Tip of the chain, for TTL
-> [(TxIn, TxOut)]
-- Resolved inputs
-> [TxOut]
-- Outputs
-> [TxOut]
-- Change, with assigned address
-> Either ErrMkTx (Tx, SealedTx)
_mkDelegationJoinTx policy dlg poolId (accXPrv, pwd') keyFrom slot inps outs chgs = do
let timeToLive = defaultTTL epochLength slot
let accXPub = toXPub $ getRawKey accXPrv
let (certs, certsInfo) = case dlg of
(WalletDelegation NotDelegating []) ->
( [ toStakeKeyRegCert accXPub
, toStakePoolDlgCert accXPub poolId
]
, [ PoolDelegationCertificate
, KeyRegistrationCertificate
]
)
_ ->
( [ toStakePoolDlgCert accXPub poolId ]
, [ PoolDelegationCertificate ]
)
-- NOTE
-- We treat key deposit as a _fee_ when constucting the coin selection,
-- so that we are sure that there are enough inputs selected to cover
-- for the deposit. However, the deposit is "implicit" when constructing
-- a transaction and needs to be removed from the actual fee.
--
-- This is why here, we recalculate the fee without the "certificate
-- fee". The missing amount is the actual deposit.
let LinearFee a b _ = policy
let fee = _minimumFee (LinearFee a b (Quantity 0))
certsInfo
(CoinSelection inps (outs ++ chgs) [])
let unsigned = mkUnsignedShelleyTx timeToLive inps (outs ++ chgs) certs fee
let metadata = SL.SNothing
addrWits <- fmap Set.fromList $ forM inps $ \(_, TxOut addr _) -> do
(k, pwd) <- lookupPrivateKey keyFrom addr
pure $ mkShelleyWitness unsigned (getRawKey k, pwd)
let certWits =
Set.singleton (mkShelleyWitness unsigned (getRawKey accXPrv, pwd'))
let wits = SL.WitnessSet (Set.union addrWits certWits) mempty mempty
pure $ toSealed $ SL.Tx unsigned wits metadata
_mkDelegationQuitTx
:: FeePolicy
-- Latest fee policy
-> (k 'AddressK XPrv, Passphrase "encryption")
-- reward account
-> (Address -> Maybe (k 'AddressK XPrv, Passphrase "encryption"))
-- Key store
-> SlotId
-- Tip of the chain, for TTL
-> [(TxIn, TxOut)]
-- ^ Resolved inputs
-> [TxOut]
-- ^ Outputs
-> [TxOut]
-- ^ Change, with assigned address
-> Either ErrMkTx (Tx, SealedTx)
_mkDelegationQuitTx policy (accXPrv, pwd') keyFrom slot inps outs chgs = do
let timeToLive = defaultTTL epochLength slot
let accXPub = toXPub $ getRawKey accXPrv
let certs = [toStakeKeyDeregCert accXPub]
-- NOTE / FIXME
-- When registering a stake key, users gave a deposit fixed by the fee
-- policy. When de-registing a key, the deposit should be given back.
--
-- The deposit doesn't come from an explicit input, but rather, from an
-- implicit balance available from the input side. So, output are
-- allowed to create more than what's consumed as inputs. We therefore
-- add the deposit to the first change output available, which doesn't
-- change the fee in Shelley. Still, the real fee are computed from the
-- differences between inputs and outputs BEFORE the deposit is added.
--
-- There's on "gotcha" with this method: it isn't resilient to protocol
-- updates. So, if a key is registered at a given epoch, with a deposit
-- X and later on, the deposit amount is changed to Y, Y /= X, then,
-- when de-registering the stake key, there'll be a mismatch between the
-- amount expected by the ledger, and the amount given by the wallet.
-- Ideally, the deposit amount should be deduced from the transaction
-- that was used to register the key! We don't have any ways at the
-- moment to lookup such a transaction from the database and making it
-- so would require some extensive changes that are quite risky to
-- undergo _now_. So long as the deposit key isn't updated via protocol
-- updates, the present solution will work fine.
chgs' <- mapFirst (withDeposit policy) chgs
let fee = realFee inps (outs ++ chgs)
let unsigned = mkUnsignedShelleyTx timeToLive inps (outs ++ chgs') certs fee
let metadata = SL.SNothing
addrWits <- fmap Set.fromList $ forM inps $ \(_, TxOut addr _) -> do
(k, pwd) <- lookupPrivateKey keyFrom addr
pure $ mkShelleyWitness unsigned (getRawKey k, pwd)
let certWits =
Set.singleton (mkShelleyWitness unsigned (getRawKey accXPrv, pwd'))
let wits = SL.WitnessSet (Set.union addrWits certWits) mempty mempty
pure $ toSealed $ SL.Tx unsigned wits metadata
where
withDeposit :: FeePolicy -> TxOut -> TxOut
withDeposit (LinearFee _ _ (Quantity deposit)) (TxOut addr (Coin c)) =
TxOut addr (Coin (c + round deposit))
mapFirst :: (a -> a) -> [a] -> Either ErrMkTx [a]
mapFirst _ [] = Left ErrChangeIsEmptyForRetirement
mapFirst fn (h:q) = Right (fn h:q)
_estimateMaxNumberOfInputs
:: Quantity "byte" Word16
-- ^ Transaction max size in bytes
-> Word8
-- ^ Number of outputs in transaction
-> Word8
_estimateMaxNumberOfInputs (Quantity maxSize) nOuts =
fromIntegral $ bisect (lowerBound, upperBound)
where
bisect (!inf, !sup)
| middle == inf && isTooBig sup = inf
| middle == inf = sup
| isTooBig middle = bisect (inf, middle)
| otherwise = bisect (middle, sup)
where
middle = inf + ((sup - inf) `div` 2)
growingFactor = 2
lowerBound = upperBound `div` growingFactor
upperBound = upperBound_ 1
where
upperBound_ !n | isTooBig n = n
| otherwise = upperBound_ (n*growingFactor)
isTooBig nInps = size > fromIntegral maxSize
where
size = computeTxSize [] sel
sel = dummyCoinSel nInps (fromIntegral nOuts)
dummyCoinSel :: Int -> Int -> CoinSelection
dummyCoinSel nInps nOuts = CoinSelection
(map (\ix -> (dummyTxIn ix, dummyTxOut)) [0..nInps-1])
(replicate nOuts dummyTxOut)
(replicate nOuts (Coin 1))
where
dummyTxIn = TxIn (Hash $ BS.pack (1:replicate 64 0)) . fromIntegral
dummyTxOut = TxOut dummyAddr (Coin 1)
dummyAddr = Address $ BS.pack (1:replicate 64 0)
_decodeSignedTx
:: ByteString
-> Either ErrDecodeSignedTx (Tx, SealedTx)
_decodeSignedTx bytes = do
case Cardano.txSignedFromCBOR bytes of
Right (Cardano.TxSignedShelley txValid) ->
pure $ toSealed txValid
Right (Cardano.TxSignedByron{}) ->
case CBOR.deserialiseFromBytes CBOR.decodeSignedTx (BL.fromStrict bytes) of
Left e ->
Left $ ErrDecodeSignedTxWrongPayload $ T.pack $ show e
Right (_, ((inps, outs), _)) -> Right
( W.Tx
{ W.txId = Hash
$ blake2b256
$ CBOR.toStrictByteString
$ CBOR.encodeTx (inps, outs)
, W.resolvedInputs = (,Coin 0) <$> inps
, W.outputs = outs
}
, SealedTx bytes
)
Left apiErr ->
Left $ ErrDecodeSignedTxWrongPayload (Cardano.renderApiError apiErr)
_minimumFee
:: FeePolicy
-> [Certificate]
-> CoinSelection
-> Fee
_minimumFee policy certs coinSel =
computeFee $ computeTxSize certs coinSel
where
computeFee :: Integer -> Fee
computeFee size =
Fee $ ceiling (a + b*fromIntegral size + if needsDeposit then c else 0)
where
LinearFee (Quantity a) (Quantity b) (Quantity c) = policy
needsDeposit = isJust $ find (== KeyRegistrationCertificate) certs
realFee :: [(TxIn, TxOut)] -> [TxOut] -> Fee
realFee inps outs = Fee
$ sum (map (getCoin . coin . snd) inps)
- sum (map (getCoin . coin) outs)
computeTxSize
:: [Certificate]
-> CoinSelection
-> Integer
computeTxSize certs (CoinSelection inps outs chngs) =
SL.txsize $ SL.Tx unsigned wits metadata
where
metadata = SL.SNothing
unsigned = mkUnsignedShelleyTx maxBound inps outs' certs' (realFee inps outs')
where
outs' :: [TxOut]
outs' = outs <> (dummyOutput <$> chngs)
dummyOutput :: Coin -> TxOut
dummyOutput = TxOut $ Address $ BS.pack (1:replicate 56 0)
dummyKeyHash = SL.KeyHash . Hash.UnsafeHash $ BS.pack (replicate 28 0)
certs' = flip map certs $ \case
PoolDelegationCertificate ->
Cardano.shelleyDelegateStake dummyKeyHash dummyKeyHash
KeyRegistrationCertificate ->
Cardano.shelleyRegisterStakingAddress dummyKeyHash
KeyDeRegistrationCertificate ->
Cardano.shelleyDeregisterStakingAddress dummyKeyHash
(addrWits, certWits) =
( Set.map dummyWitnessUniq $ Set.fromList (fst <$> inps)
, if null certs
then Set.empty
else Set.singleton (dummyWitness "a")
)
where
dummyWitness :: BL.ByteString -> SL.WitVKey TPraosStandardCrypto 'SL.Witness
dummyWitness chaff = SL.WitVKey key sig
where
sig = SignedDSIGN
$ fromMaybe (error "error creating dummy witness sig")
$ rawDeserialiseSigDSIGN
$ bloatChaff sigLen
key = SL.VKey
$ fromMaybe (error "error creating dummy witness ver key")
$ rawDeserialiseVerKeyDSIGN
$ bloatChaff keyLen
sigLen = sizeSigDSIGN $ Proxy @(DSIGN TPraosStandardCrypto)
keyLen = sizeVerKeyDSIGN $ Proxy @(DSIGN TPraosStandardCrypto)
bloatChaff n = BL.toStrict $ BL.take (fromIntegral n) $ BL.cycle chaff
dummyWitnessUniq :: TxIn -> SL.WitVKey TPraosStandardCrypto 'SL.Witness
dummyWitnessUniq (TxIn (Hash txid) ix) = dummyWitness chaff
where
chaff = L8.pack (show ix) <> BL.fromStrict txid
wits = SL.WitnessSet (Set.union addrWits certWits) mempty mempty
lookupPrivateKey
:: (Address -> Maybe (k 'AddressK XPrv, Passphrase "encryption"))
-> Address
-> Either ErrMkTx (k 'AddressK XPrv, Passphrase "encryption")
lookupPrivateKey keyFrom addr =
maybe (Left $ ErrKeyNotFoundForAddress addr) Right (keyFrom addr)
mkUnsignedShelleyTx
:: Cardano.SlotNo
-> [(TxIn, TxOut)]
-> [TxOut]
-> [Cardano.Certificate]
-- ^ TODO: This should be not be a Cardano type, but a wallet type.
-> Fee
-> Cardano.ShelleyTxBody
mkUnsignedShelleyTx ttl ownedIns outs certs fee =
let
Cardano.TxUnsignedShelley unsigned = Cardano.buildShelleyTransaction
(toCardanoTxIn . fst <$> ownedIns)
(map toCardanoTxOut outs)
ttl
(toCardanoLovelace $ Coin $ getFee fee)
certs
(Cardano.WithdrawalsShelley $ SL.Wdrl mempty) -- Withdrawals
Nothing -- Update
Nothing -- Metadata hash
in
unsigned
-- TODO: The SlotId-SlotNo conversion based on epoch length would not
-- work if the epoch length changed in a hard fork.
-- NOTE: The (+7200) was selected arbitrarily when we were trying to get
-- this working on the FF testnet. Perhaps a better motivated and/or
-- configurable value would be better.
defaultTTL :: EpochLength -> SlotId -> SlotNo
defaultTTL epochLength slot =
(toSlotNo epochLength slot) + 7200
mkShelleyWitness
:: SL.TxBody TPraosStandardCrypto
-> (XPrv, Passphrase "encryption")
-> SL.WitVKey TPraosStandardCrypto 'SL.Witness
mkShelleyWitness body (prv, pwd) =
SL.WitVKey key sig
where
sig = SignedDSIGN
$ fromMaybe (error "error converting signatures")
$ rawDeserialiseSigDSIGN
$ serialize' (SL.hashTxBody body) `signWith` (prv, pwd)
key = SL.VKey
$ VerKeyEd25519DSIGN
$ unsafeMkEd25519
$ toXPub prv
signWith
:: ByteString
-> (XPrv, Passphrase "encryption")
-> ByteString
signWith msg (prv, pass) =
CC.unXSignature . CC.sign pass prv $ msg
unsafeMkEd25519 :: XPub -> Ed25519.PublicKey
unsafeMkEd25519 =
throwCryptoError . Ed25519.publicKey . xpubPublicKey
mkByronWitness
:: WalletKey k
=> ProtocolMagic
-> ByteString
-> (k 'AddressK XPrv, Passphrase "encryption")
-> ByteString
mkByronWitness (ProtocolMagic pm) sigData (xPrv, Passphrase pwd) =
CBOR.toStrictByteString
$ CBOR.encodePublicKeyWitness (getRawKey $ publicKey xPrv)
$ CC.unXSignature (CC.sign pwd (getRawKey xPrv) message)
where
message = mconcat
[ "\x01"
, CBOR.toStrictByteString (CBOR.encodeInt32 pm)
, CBOR.toStrictByteString (CBOR.encodeBytes sigData)
]
--------------------------------------------------------------------------------
-- Extra validations on coin selection
--
type instance ErrValidateSelection (IO Shelley) = ()