Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sevanspowell committed May 11, 2021
1 parent c268b85 commit ec3fd6d
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 47 deletions.
Expand Up @@ -1021,6 +1021,7 @@ spec = describe "SHELLEY_TRANSACTIONS" $ do
addrs <- listAddresses @n ctx w
let destination = (addrs !! 1) ^. #id
let payload = Json [json|{
"address": #{destination},
"mint_amount": {
"quantity": 5,
"unit": "assets"
Expand Down
5 changes: 3 additions & 2 deletions lib/core/src/Cardano/Wallet.hs
Expand Up @@ -1460,7 +1460,7 @@ signTransaction
-> Passphrase "raw"
-> TransactionCtx
-> SelectionResult TokenBundle
-> Maybe (k 'AddressK XPrv, Passphrase "encryption")
-> Maybe (k 'ScriptK XPrv, Passphrase "encryption")
-> ExceptT ErrSignPayment IO (Tx, TxMeta, UTCTime, SealedTx)
signTransaction ctx wid argChange mkRwdAcct pwd txCtx sel extraWit = db & \DBLayer{..} -> do
era <- liftIO $ currentNodeEra nl
Expand Down Expand Up @@ -2151,6 +2151,7 @@ derivePrivateKey
, HardDerivation k
, AddressIndexDerivationType k ~ 'Soft
, s ~ SeqState n k
, WalletKey k
)
=> ctx
-> WalletId
Expand All @@ -2171,7 +2172,7 @@ derivePrivateKey ctx wid pwd (role_, ix) = db & \DBLayer{..} -> do
let DerivationPrefix (_, _, acctIx) = derivationPrefix (getState cp)
let acctK = deriveAccountPrivateKey encPwd rootK acctIx
let addrK = deriveAddressPrivateKey encPwd acctK role_ addrIx
pure (addrK, encPwd)
pure (liftRawKey . getRawKey $ addrK, encPwd)
where
db = ctx ^. dbLayer @IO @s @k

Expand Down
15 changes: 7 additions & 8 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Expand Up @@ -113,7 +113,8 @@ import Prelude
import Cardano.Address.Derivation
( XPrv, XPub, xpubPublicKey, xpubToBytes )
import Cardano.Address.Script
( Cosigner (..), KeyHash, KeyRole (..), Script (RequireSignatureOf) )
( Cosigner (..), KeyHash, KeyRole, Script (RequireSignatureOf) )
import qualified Cardano.Address.Script as CA
import Cardano.Api
( AnyCardanoEra (..)
, AssetName (AssetName)
Expand Down Expand Up @@ -3361,7 +3362,7 @@ forgeToken ctx genChange (ApiT wid) body = do
let derivationIndex = fromMaybe (DerivationIndex 0) $ fmap getApiT $ body ^. #monetaryPolicyIndex
let md = body ^? #metadata . traverse . #getApiT
let mTTL = body ^? #timeToLive . traverse . #getQuantity
let addr = body ^? #address
let (ApiT addr, _) = body ^. #address

(wdrl, mkRwdAcct) <-
mkRewardAccountBuilder @_ @s @_ @n ctx wid Nothing
Expand All @@ -3384,7 +3385,7 @@ forgeToken ctx genChange (ApiT wid) body = do
scriptXPub = publicKey $ fst policyKey

vkeyHash :: KeyHash
vkeyHash = hashVerificationKey @k Payment $ liftRawKey $ getRawKey scriptXPub
vkeyHash = hashVerificationKey @k MultisigScript $ liftRawKey $ getRawKey scriptXPub

script :: Script KeyHash
script = RequireSignatureOf vkeyHash
Expand All @@ -3395,19 +3396,17 @@ forgeToken ctx genChange (ApiT wid) body = do
assetId :: AssetId
assetId = AssetId policyId assetName

liftIO $ putStrLn $ T.unpack $ toText vkeyHash

-- Transfer the minted assets to the payment address
-- associated with the monetary policy
let assets = TokenMap.singleton assetId assetQty
let txout = [TxOut addr (TokenBundle.TokenBundle (Coin 0) assets)]
let txout = (TxOut addr (TokenBundle.TokenBundle (Coin 0) assets)) NE.:| []
-- let outs = fmap (\(TxOut addr (TokenBundle.TokenBundle coin tokens)) -> TxOut addr (TokenBundle.TokenBundle coin mempty)) (pure txout)

let txCtx = defaultTransactionCtx
{ txWithdrawal = wdrl
, txMetadata = md
, txTimeToLive = ttl
, txMintBurnInfo = Just (pure (payAddrXPub, assets) :: NonEmpty (Address, TokenMap.TokenMap))
, txMintBurnInfo = Just (pure (addr, assets) :: NonEmpty (Address, TokenMap.TokenMap))
}

w <- liftHandler $ W.readWalletUTxOIndex @_ @s @k wrk wid
Expand All @@ -3423,7 +3422,7 @@ forgeToken ctx genChange (ApiT wid) body = do
-- ) (outputsCovered sel)

(tx, txMeta, txTime, sealedTx) <- liftHandler
$ W.signTransaction @_ @s @k wrk wid genChange mkRwdAcct pwd txCtx sel (Just addrXPrv)
$ W.signTransaction @_ @s @k wrk wid genChange mkRwdAcct pwd txCtx sel (Just policyKey)
-- liftIO $ putStrLn $ "Finished SIGN"
-- liftIO $ putStrLn $ show tx
liftHandler
Expand Down
3 changes: 2 additions & 1 deletion lib/core/src/Cardano/Wallet/Api/Types.hs
Expand Up @@ -2775,7 +2775,7 @@ instance ToJSON (ApiT SmashServer) where
-------------------------------------------------------------------------------}

data ForgeTokenData (n :: NetworkDiscriminant) = ForgeTokenData
{ address :: !(ApiT W.Address)
{ address :: !(ApiT Address, Proxy n)
, assetName :: !(ApiT W.TokenName)
, mintAmount :: !(Quantity "assets" Natural)
, monetaryPolicyIndex :: !(Maybe (ApiT DerivationIndex))
Expand All @@ -2786,5 +2786,6 @@ data ForgeTokenData (n :: NetworkDiscriminant) = ForgeTokenData

instance DecodeAddress n => FromJSON (ForgeTokenData n) where
parseJSON = genericParseJSON defaultRecordTypeOptions

instance EncodeAddress n => ToJSON (ForgeTokenData n) where
toJSON = genericToJSON defaultRecordTypeOptions
21 changes: 0 additions & 21 deletions lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs
Expand Up @@ -493,27 +493,6 @@ instance PersistField ScriptHash where
instance PersistFieldSql ScriptHash where
sqlType _ = sqlType (Proxy @Text)

----------------------------------------------------------------------------
-- KeyHash

instance ToText KeyHash where
toText (KeyHash sh) =
T.decodeUtf8 $ convertToBase Base16 sh

instance FromText KeyHash where
fromText = bimap textDecodingError KeyHash
. convertFromBase Base16
. T.encodeUtf8
where
textDecodingError = TextDecodingError . show

instance PersistField KeyHash where
toPersistValue = toPersistValue . toText
fromPersistValue = fromPersistValueFromText

instance PersistFieldSql KeyHash where
sqlType _ = sqlType (Proxy @Text)

----------------------------------------------------------------------------
-- Script Cosigner

Expand Down
17 changes: 12 additions & 5 deletions lib/core/src/Cardano/Wallet/Primitive/AddressDerivation.hs
Expand Up @@ -154,6 +154,7 @@ import GHC.TypeLits
import Safe
( readMay, toEnumMay )

import qualified Cardano.Address.Script as CA
import qualified Codec.CBOR.Encoding as CBOR
import qualified Codec.CBOR.Write as CBOR
import qualified Crypto.Scrypt as Scrypt
Expand Down Expand Up @@ -500,18 +501,24 @@ deriveRewardAccount pwd rootPrv =
deriveVerificationKey
:: (SoftDerivation k, WalletKey k)
=> k 'AccountK XPub
-> Role
-> Index 'Soft 'ScriptK
-> k 'ScriptK XPub
deriveVerificationKey accXPub =
liftRawKey . getRawKey . deriveAddressPublicKey accXPub MultisigScript . coerce
deriveVerificationKey accXPub role' =
liftRawKey . getRawKey . deriveAddressPublicKey accXPub role' . coerce

hashVerificationKey
:: WalletKey k
=> KeyRole
=> Role
-> k 'ScriptK XPub
-> KeyHash
hashVerificationKey r =
KeyHash r . blake2b224 . xpubPublicKey . getRawKey
hashVerificationKey role' =
KeyHash keyRole . blake2b224 . xpubPublicKey . getRawKey
where
keyRole = case role' of
UtxoExternal -> CA.Payment
MutableAccount -> CA.Delegation
_ -> error "verification keys make sense only for payment (role=0) and delegation (role=2)"

{-------------------------------------------------------------------------------
Passphrases
Expand Down
Expand Up @@ -44,11 +44,10 @@ import Cardano.Address.Script
, toScriptHash
)
import Cardano.Address.Style.Shared
( deriveAddressPublicKey, deriveDelegationPublicKey, hashKey )
( deriveAddressPublicKey, deriveDelegationPublicKey, hashKey, liftXPub )
import Cardano.Address.Style.Shelley
( Credential (..)
, delegationAddress
, liftXPub
, mkNetworkDiscriminant
, paymentAddress
)
Expand Down Expand Up @@ -116,8 +115,8 @@ keyHashFromAccXPubIx
-> Role
-> Index 'Soft 'ScriptK
-> KeyHash
keyHashFromAccXPubIx accXPub r =
hashVerificationKey r . deriveVerificationKey accXPub
keyHashFromAccXPubIx accXPub r ix =
hashVerificationKey r $ deriveVerificationKey accXPub r ix

replaceCosignersWithVerKeys
:: CA.Role
Expand Down Expand Up @@ -153,6 +152,7 @@ replaceCosignersWithVerKeys role' (ScriptTemplate xpubs scriptTemplate) ix =
CA.UTxOExternal -> deriveAddressPublicKey
CA.Stake -> deriveDelegationPublicKey
_ -> error "replaceCosignersWithVerKeys is supported only for role=0 and role=2"

toNetworkTag
:: forall (n :: NetworkDiscriminant). Typeable n => CA.NetworkTag
toNetworkTag =
Expand Down
Expand Up @@ -404,20 +404,18 @@ retrieveAllCosigners = foldScript (:) []
isShared
:: forall (n :: NetworkDiscriminant) k.
( SoftDerivation k
, WalletKey k
, Typeable n
, MkKeyFingerprint k Address
, MkKeyFingerprint k (Proxy n, k 'AddressK XPub) )
=> Address
-> SharedState n k
-> (Maybe (Index 'Soft 'ScriptK, KeyHash), SharedState n k)
-> (Maybe (Index 'Soft 'ScriptK), SharedState n k)
isShared addr st = case fields st of
ReadyFields pool ->
let (ixM, pool') = lookupAddress @n (const Used) addr pool
(ParentContextMultisigScript accXPub _ _) = context pool
in case ixM of
Just ix ->
(Just (coerce ix, keyHashFromAccXPubIx accXPub (coerce ix))
( Just $ coerce ix
, st { fields = ReadyFields pool' })
Nothing ->
(Nothing, st)
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/Cardano/Wallet/Transaction.hs
Expand Up @@ -94,7 +94,7 @@ data TransactionLayer k = TransactionLayer
-> SelectionResult TxOut
-- A balanced coin selection where all change addresses have been
-- assigned.
-> Maybe (k 'AddressK XPrv, Passphrase "encryption")
-> Maybe (k 'ScriptK XPrv, Passphrase "encryption")
-- Extra witness
-> Either ErrMkTx (Tx, SealedTx)
-- ^ Construct a standard transaction
Expand Down
2 changes: 1 addition & 1 deletion lib/shelley/src/Cardano/Wallet/Shelley/Transaction.hs
Expand Up @@ -281,7 +281,7 @@ mkTx
-> Coin
-- ^ Explicit fee amount
-> Maybe (NE.NonEmpty (Address, TokenMap))
-> Maybe (k 'AddressK XPrv, Passphrase "encryption")
-> Maybe (k 'ScriptK XPrv, Passphrase "encryption")
-> ShelleyBasedEra era
-> Either ErrMkTx (Tx, SealedTx)
mkTx networkId payload ttl (rewardAcnt, pwdAcnt) keyFrom wdrl cs fees mForgeOuts extraWit era = do
Expand Down

0 comments on commit ec3fd6d

Please sign in to comment.