Skip to content

Commit

Permalink
Merge pull request #26 from Plutonomicon/bhart/extend-tx-type
Browse files Browse the repository at this point in the history
extend the transaction type to handle plutus scripts, datum, etc
  • Loading branch information
ngua committed Jan 26, 2022
2 parents c7762c2 + 352ffdd commit 1c116c5
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .gitignore
@@ -1,5 +1,4 @@
/bower_components/
/node_modules/
/.pulp-cache/
/output/
/generated-docs/
Expand All @@ -13,6 +12,7 @@ result
result-*
.envrc
.direnv
node_modules
/node_modules
/node_modules/
.node
.node-cfg
48 changes: 48 additions & 0 deletions spago-packages.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion spago.dhall
Expand Up @@ -31,11 +31,15 @@ You can edit this file as you like.
, "prelude"
, "psci-support"
, "quickcheck"
, "rationals"
, "refs"
, "spec"
, "strings"
, "transformers"
, "transformers"
, "tuples"
, "uint"
, "undefined"
, "unordered-collections"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
Expand Down
10 changes: 6 additions & 4 deletions src/Lib.purs
Expand Up @@ -13,9 +13,10 @@ tx
:: Array Types.TransactionInput
-> Array Types.TransactionOutput
-> Array Types.TransactionInput
-> Types.NetworkId
-> Types.Transaction
tx i o c = Types.Transaction
{ body: txBody i o c
tx i o c id = Types.Transaction
{ body: txBody i o c id
, witness_set: txWitness
, is_valid: true -- why is this?
, auxiliary_data: Nothing
Expand All @@ -25,8 +26,9 @@ txBody
:: Array Types.TransactionInput
-> Array Types.TransactionOutput
-> Array Types.TransactionInput
-> Types.NetworkId
-> Types.TxBody
txBody inputs outputs collateral = Types.TxBody
txBody inputs outputs collateral id = Types.TxBody
{ inputs: inputs
, outputs: outputs
, fee: Types.Coin $ BigInt.fromInt 1000000
Expand All @@ -40,7 +42,7 @@ txBody inputs outputs collateral = Types.TxBody
, script_data_hash: Nothing
, collateral: Just $ collateral
, required_signers: Nothing
, network_id: Just $ Types.NetworkId 1
, network_id: Just $ id
}

txWitness :: Types.TransactionWitnessSet
Expand Down
4 changes: 4 additions & 0 deletions src/Types/RedeemerTag.purs
@@ -0,0 +1,4 @@
module Types.RedeemerTag where

-- lives in it's own module due to a name conflict with the `Mint` Type
data RedeemerTag = Spend | Mint | Cert | Reward
170 changes: 151 additions & 19 deletions src/Types/Transaction.purs
@@ -1,13 +1,21 @@
module Types.Transaction where

import Prelude
import Data.ArrayBuffer.Types (Uint8Array)
import Data.ArrayBuffer.Types(Uint8Array)
import Data.BigInt as BigInt
import Data.Generic.Rep (class Generic)
import Data.HashMap (HashMap)
import Data.Maybe (Maybe)
import Data.Tuple.Nested (type (/\))
import Data.Map (Map)
import Data.Generic.Rep (class Generic)
import Data.Show.Generic (genericShow)
import Data.Rational (Rational)
import Data.Tuple.Nested (type (/\))
import Data.UInt (UInt)
import Types.RedeemerTag (RedeemerTag)

-- note: these types are derived from the cardano-serialization-lib Sundae fork
-- the source of truth for these types should be that library and the
-- corresponding Rust types

newtype Transaction = Transaction {
body :: TxBody,
Expand All @@ -21,28 +29,132 @@ newtype TxBody = TxBody
outputs :: Array TransactionOutput,
fee :: Coin,
ttl :: Maybe Slot,
certs :: Maybe Unit, -- Certificates,
withdrawals :: Maybe Unit, -- Withdrawals,
update :: Maybe Unit, -- Update,
auxiliary_data_hash :: Maybe String, -- AuxiliaryDataHash, - script hashes
certs :: Maybe (Array Certificate),
withdrawals :: Maybe (Map RewardAddress Coin),
update :: Maybe Update,
auxiliary_data_hash :: Maybe AuxiliaryDataHash,
validity_start_interval :: Maybe Slot,
mint :: Maybe Value, -- Mint
script_data_hash :: Maybe String, -- ScriptDataHash,
mint :: Maybe Mint,
script_data_hash :: Maybe ScriptDataHash,
collateral :: Maybe (Array TransactionInput),
required_signers :: Maybe (Array RequiredSigner),
network_id :: Maybe NetworkId
}

newtype ScriptDataHash = ScriptDataHash String

newtype Mint = Mint Value

newtype AuxiliaryDataHash = AuxiliaryDataHash String

type Update =
{ proposed_protocol_parameter_updates :: ProposedProtocolParameterUpdates
, epoch :: Epoch
}

newtype ProposedProtocolParameterUpdates
= ProposedProtocolParameterUpdates (Map GenesisHash ProtocolParamUpdate)

newtype GenesisHash = GenesisHash String

type ProtocolParamUpdate =
{ minfee_a :: Maybe Coin,
minfee_b :: Maybe Coin,
max_block_body_size :: Maybe UInt,
max_tx_size :: Maybe UInt,
max_block_header_size :: Maybe UInt,
key_deposit :: Maybe Coin,
pool_deposit :: Maybe Coin,
max_epoch :: Maybe Epoch,
n_opt :: Maybe UInt,
pool_pledge_influence :: Maybe Rational,
expansion_rate :: Maybe UnitInterval,
treasury_growth_rate :: Maybe UnitInterval,
d :: Maybe UnitInterval,
extra_entropy :: Maybe Nonce,
protocol_version :: Maybe (Array ProtocolVersion),
min_pool_cost :: Maybe Coin,
ada_per_utxo_byte :: Maybe Coin,
cost_models :: Maybe Costmdls,
execution_costs :: Maybe ExUnitPrices,
max_tx_ex_units :: Maybe ExUnits,
max_block_ex_units :: Maybe ExUnits,
max_value_size :: Maybe UInt
}

type ExUnitPrices =
{ mem_price :: SubCoin
, step_price :: SubCoin
}

type ExUnits =
{ mem :: BigInt.BigInt
, steps :: BigInt.BigInt
}

type SubCoin = UnitInterval

type RewardAddress =
{ network :: UInt
, payment :: StakeCredential
}

data StakeCredential
= Key Ed25519KeyHash
| Script ScriptHash

newtype Ed25519KeyHash = Ed25519KeyHash String

newtype ScriptHash = ScriptHash String

newtype Costmdls = Costmdls (Map Language CostModel)

data Language = PlutusV1

newtype CostModel = CostModel (Array UInt)

type ProtocolVersion =
{ major :: UInt
, minor :: UInt
}

newtype Nonce = Nonce String

type UnitInterval =
{ numerator :: BigInt.BigInt
, denominator :: BigInt.BigInt
}

newtype Epoch = Epoch UInt

data Certificate
= StakeRegistration
| StakeDeregistration
| StakeDelegation
| PoolRegistration
| PoolRetirement
| GenesisKeyDelegation
| MoveInstantaneousRewardsCert

newtype TransactionWitnessSet = TransactionWitnessSet
{ vkeys :: Maybe (Array Vkeywitness),
native_scripts :: Maybe Unit, -- NativeScripts,
bootstraps :: Maybe Unit, -- BootstrapWitnesses,
native_scripts :: Maybe (Array NativeScript),
bootstraps :: Maybe (Array BootstrapWitness),
plutus_scripts :: Maybe (Array PlutusScript),
plutus_data :: Maybe (Array PlutusData),
redeemers :: Maybe (Array Redeemer)
}

newtype NetworkId = NetworkId Int
type BootstrapWitness =
{ vkey :: Vkey
, signature :: Ed25519Signature
, chain_code :: Uint8Array
, attributes :: Uint8Array
}

data NetworkId
= Mainnet
| Testnet

newtype RequiredSigner = RequiredSigner String

Expand Down Expand Up @@ -78,11 +190,9 @@ newtype Ed25519Signature = Ed25519Signature String -- (bech32)
newtype PlutusScript = PlutusScript String

newtype PlutusData = PlutusData String
-- TODO - we need a capability to encode/decode Datum from/to serialized format
-- see `makeIsDataIndexed`

newtype Redeemer = Redeemer
{ tag :: RedeemerTag, -- ScriptPurpose: 'spending' 'minting' etc
{ tag :: RedeemerTag,
index :: BigInt.BigInt,
data :: PlutusData,
ex_units :: (MemExUnits /\ CpuExUnits)
Expand All @@ -92,14 +202,36 @@ newtype MemExUnits = MemExUnits BigInt.BigInt

newtype CpuExUnits = CpuExUnits BigInt.BigInt

data RedeemerTag = Spend | Mint | Cert | Reward

type AuxiliaryData = Unit -- this is big and weird in serialization-lib
type AuxiliaryData =
{ metadata :: Maybe GeneralTransactionMetadata
, native_scripts :: Maybe (Array NativeScript)
, plutus_scripts :: Maybe (Array PlutusScript)
}

newtype GeneralTransactionMetadata =
GeneralTransactionMetadata (HashMap TransactionMetadatumLabel TransactionMetadatum)

newtype TransactionMetadatumLabel = TransactionMetadatumLabel BigInt.BigInt

data TransactionMetadatum
= MetadataMap (HashMap TransactionMetadatum TransactionMetadatum)
| MetadataList (Array TransactionMetadatum)
| Int Int
| Bytes Uint8Array
| Text String

data NativeScript
= ScriptPubkey
| ScriptAll
| ScriptAny
| ScriptNOfK
| TimelockStart
| TimelockExpiry

newtype TransactionInput = TransactionInput
{ transaction_id :: TransactionHash
, index :: BigInt.BigInt -- u32 TransactionIndex
, index :: UInt
}

newtype TransactionOutput = TransactionOutput
Expand Down Expand Up @@ -127,7 +259,7 @@ newtype Address = Address
}

newtype BaseAddress = BaseAddress
{ network :: Int, -- u8,
{ network :: UInt, -- UInt8
stake :: Credential,
payment :: Credential
}
Expand Down

0 comments on commit 1c116c5

Please sign in to comment.