Skip to content

Commit

Permalink
Update PK to Address
Browse files Browse the repository at this point in the history
  • Loading branch information
hrajchert committed Nov 16, 2022
1 parent a846257 commit 5a87c13
Show file tree
Hide file tree
Showing 24 changed files with 262 additions and 280 deletions.
343 changes: 189 additions & 154 deletions cabal.project

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions doc/marlowe/tutorials/javascript-embedding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Marlowe embedded in JavaScript
==============================

Marlowe is written as a Haskell data type, and thus it is
straightforward to describe Marlowe smart contracts using Haskell. But since Marlowe contracts are “just”
a form of data, we can equally well represent them in other languages.
straightforward to describe Marlowe smart contracts using Haskell. But since Marlowe contracts are “just”
a form of data, we can equally well represent them in other languages that can be serialized as JSON or CBOR.

Here we describe a library written in TypeScript that can
be used to generate Marlowe smart contracts from TypeScript or
Expand All @@ -14,7 +14,7 @@ are not familiar with TypeScript, you can also use the API as if it was
written in JavaScript since TypeScript is a superset of JavaScript.

You can try the library online in the
Marlowe Playground by selecting **Start in JavaScript** on the home page, or by opening one of the
Marlowe Playground by selecting **Start in JavaScript** on the home page, or by opening one of the
JavaScript examples.

We begin this section by explaining the embedding, then explain a couple of particular points about
Expand All @@ -32,7 +32,7 @@ corresponding to each *constructor* there is a constant definition.
.. code:: typescript
import {
PK, Role, Account, Party, ada, AvailableMoney, Constant, ConstantParam,
Address, Role, Account, Party, ada, AvailableMoney, Constant, ConstantParam,
NegValue, AddValue, SubValue, MulValue, DivValue, ChoiceValue, TimeIntervalStart,
TimeIntervalEnd, UseValue, Cond, AndObs, OrObs, NotObs, ChoseSomething,
ValueGE, ValueGT, ValueLT, ValueLE, ValueEQ, TrueObs, FalseObs, Deposit,
Expand Down
6 changes: 3 additions & 3 deletions flake.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ explicitRefunds = False
seller, buyer, burnAddress :: Party
buyer = Role "Buyer"
seller = Role "Seller"
burnAddress = PK "0000000000000000000000000000000000000000000000000000000000000000"
burnAddress = Address "0000000000000000000000000000000000000000000000000000000000000000"
price, collateral :: Value
price = ConstantParam "Price"
Expand Down Expand Up @@ -603,4 +603,4 @@ contract = initialDeposit party partyDeposit (TimeParam "Party deposit deadline"
$ refundAfterDifference party partyDeposit counterparty counterpartyDeposit (UseValue increaseInPrice)
)
refundBoth
"""
"""
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ escrowWithCollateral =
(Pay
(Role "Seller")
(Party
(PK "0000000000000000000000000000000000000000000000000000000000000000"))
(Address "0000000000000000000000000000000000000000000000000000000000000000"))
(Token "" "")
(ConstantParam "Collateral amount")
(Pay
(Role "Buyer")
(Party
(PK "0000000000000000000000000000000000000000000000000000000000000000"))
(Address "0000000000000000000000000000000000000000000000000000000000000000"))
(Token "" "")
(ConstantParam "Collateral amount") Close)))] (TimeParam "Complaint deadline") Close)))] (TimeParam "Dispute by buyer timeout") Close))] (TimeParam "Deposit of price by buyer timeout") Close))] (TimeParam "Deposit of collateral by buyer timeout") Close))] (TimeParam "Collateral deposit by seller timeout") Close"""

Expand Down Expand Up @@ -457,4 +457,4 @@ contractForDifferencesWithOracle =
(UseValue "Increase in price")
(ConstantParam "Amount paid by party"))
(UseValue "Increase in price")
(ConstantParam "Amount paid by party")) Close)) Close))))] (TimeParam "Second window deadline") Close)))] (TimeParam "First window deadline") Close)))] (TimeParam "Counterparty deposit deadline") Close))] (TimeParam "Party deposit deadline") Close"""
(ConstantParam "Amount paid by party")) Close)) Close))))] (TimeParam "Second window deadline") Close)))] (TimeParam "First window deadline") Close)))] (TimeParam "Counterparty deposit deadline") Close))] (TimeParam "Party deposit deadline") Close"""
4 changes: 2 additions & 2 deletions marlowe-playground-client/grammar.ne
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const lexer = moo.compile({
],
TOKEN: ['Token'],
PAYEE: ['Account', 'Party'],
PARTY: ['PK', 'Role'],
PARTY: ['Address', 'Role'],
BOUND: ['Bound'],
TIMEOUT: ['TimeParam'],
VALUE_ID: ['ValueId'],
Expand Down Expand Up @@ -160,7 +160,7 @@ token

party
-> hole {% ([hole]) => hole %}
| lparen "PK" someWS base16 rparen {% ([start,{line,col},,k,end]) => opts.mkTerm(opts.mkPK(k))({startLineNumber: start.line, startColumn: start.col, endLineNumber: end.line, endColumn: end.col}) %}
| lparen "Address" someWS string rparen {% ([start,{line,col},,k,end]) => opts.mkTerm(opts.mkAddress(k))({startLineNumber: start.line, startColumn: start.col, endLineNumber: end.line, endColumn: end.col}) %}
| lparen "Role" someWS string rparen {% ([start,{line,col},,k,end]) => opts.mkTerm(opts.mkRole(k))({startLineNumber: start.line, startColumn: start.col, endLineNumber: end.line, endColumn: end.col}) %}

payee
Expand Down
2 changes: 1 addition & 1 deletion marlowe-playground-client/src/Examples/JS/Contracts.purs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ escrowWithCollateral =
const buyer: Party = Role("Buyer");
const seller: Party = Role("Seller");
const burnAddress: Party = PK("0000000000000000000000000000000000000000000000000000000000000000");
const burnAddress: Party = Address("0000000000000000000000000000000000000000000000000000000000000000");
const price: Value = ConstantParam("Price");
const collateral: Value = ConstantParam("Collateral amount");
Expand Down
4 changes: 2 additions & 2 deletions marlowe-playground-client/src/Help.purs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ A Marlowe Account holds amounts of multiple currencies and/or fungible and non-f

marloweTypeMarkerText PartyType =
"""
A Party is represented as either a public key hash or a role name.
In order to progress a Marlowe contract, a party must provide an evidence. For PK party that would be a valid signature of a transaction signed by a private key of a public key that hashes to party’s PubKeyHash, similarly to Bitcoin’s Pay to Public Key Hash mechanism. For a Role party the evidence is spending a role token within the same transaction, usually to the same owner.
A Party is represented as either an address or a role name.
In order to progress a Marlowe contract, a party must provide an evidence. For Address party that would be a valid signature of a transaction signed by the corresponding private key. For a Role party the evidence is spending a role token within the same transaction, usually to the same owner.
So, Role parties will look like (Role "alice"), (Role "bob") and so on.
"""
Expand Down
11 changes: 3 additions & 8 deletions marlowe-playground-client/src/Language/Javascript/MarloweJS.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import bignumber = require("bignumber.js");

type Party = { pk_hash: string } | { role_token: string };
type Party = { address: string } | { role_token: string };

type SomeNumber = number | string | bigint;

Expand All @@ -22,13 +22,8 @@ function coerceNumber(n: SomeNumber): bignumber.BigNumber {
}
}

export const PK = function (pubKey: string): Party {
var regexp = /^([0-9a-f][0-9a-f])*$/g;
if (pubKey.match(regexp)) {
return { pk_hash: pubKey };
} else {
throw new Error("Public key must be base16");
}
export const Address = function (address: string): Party {
return { address: address };
};

export const Role = function (roleToken: string): Party {
Expand Down
2 changes: 1 addition & 1 deletion marlowe-playground-client/src/Language/Marlowe/ToTerm.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ instance ToTerm TermWrapper S.ValueId T.ValueId where
toTerm (S.ValueId s) = TermWrapper (T.ValueId s) NoLocation

instance ToTerm Term S.Party T.Party where
toTerm (S.PK pubKey) = Term (T.PK pubKey) NoLocation
toTerm (S.Address address) = Term (T.Address address) NoLocation
toTerm (S.Role tokenName) = Term (T.Role tokenName) NoLocation

instance ToTerm Term E.Payee T.Payee where
Expand Down
25 changes: 12 additions & 13 deletions marlowe-playground-client/src/Marlowe/Blockly.purs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ payeeTypes :: Array PayeeType
payeeTypes = upFromIncluding bottom

data PartyType
= PKPartyType
= AddressPartyType
| RolePartyType

derive instance genericPartyType :: Generic PartyType _
Expand Down Expand Up @@ -615,23 +615,22 @@ toDefinition blockType@(PayeeType PartyPayeeType) =
defaultBlockDefinition

-- Party
toDefinition blockType@(PartyType PKPartyType) =
toDefinition blockType@(PartyType AddressPartyType) =
BlockDefinition
$ merge
{ type: show PKPartyType
, message0: "Public Key %1"
{ type: show AddressPartyType
, message0: "Address %1"
, args0:
[ Input
{ name: "pubkey"
{ name: "address"
, text:
"0000000000000000000000000000000000000000000000000000000000000000"
""
, spellcheck: false
}
]
, colour: blockColour blockType
, output: Just "party"
, inputsInline: Just true
, extensions: [ "hash_validator" ]
}
defaultBlockDefinition

Expand Down Expand Up @@ -1660,9 +1659,9 @@ instance blockToTermCase :: BlockToTerm Case where
blockToTerm block = throwError $ InvalidBlock block "Action"

instance blockToTermParty :: BlockToTerm Party where
blockToTerm b@({ type: "PKPartyType", id }) = do
pubkey <- fieldAsString "pubkey" b
pure $ Term (PK pubkey) (BlockId id)
blockToTerm b@({ type: "AddressPartyType", id }) = do
address <- fieldAsString "address" b
pure $ Term (Address address) (BlockId id)
blockToTerm b@({ type: "RolePartyType", id }) = do
role <- fieldAsString "role" b
pure $ Term (Role role) (BlockId id)
Expand Down Expand Up @@ -1761,10 +1760,10 @@ instance toBlocklyPayee :: ToBlockly Payee where
inputToBlockly newBlock workspace block "party" party

instance toBlocklyParty :: ToBlockly Party where
toBlockly newBlock workspace input (PK pk) = do
block <- newBlock workspace (show PKPartyType)
toBlockly newBlock workspace input (Address address) = do
block <- newBlock workspace (show AddressPartyType)
connectToOutput block input
setField block "pubkey" pk
setField block "pubkey" address
toBlockly newBlock workspace input (Role role) = do
block <- newBlock workspace (show RolePartyType)
connectToOutput block input
Expand Down
14 changes: 7 additions & 7 deletions marlowe-playground-client/src/Marlowe/Gen.purs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import Data.Semigroup.Foldable (foldl1)
import Data.String.CodeUnits (fromCharArray)
import Data.Time.Duration (Milliseconds(..))
import Language.Marlowe.Core.V1.Semantics.Types
( CurrencySymbol
( Address
, CurrencySymbol
, Input(..)
, PubKey
, Rational(..)
, TimeInterval(..)
, TokenName
Expand Down Expand Up @@ -138,7 +138,7 @@ genAlphaNum = oneOf $ genAlpha :| [ genDigitChar ]
genString :: forall m. MonadGen m => MonadRec m => m String
genString = fromCharArray <$> resize (_ - 1) (unfoldable genAlphaNum)

genPubKey :: forall m. MonadGen m => MonadRec m => m PubKey
genPubKey :: forall m. MonadGen m => MonadRec m => m Address
genPubKey = genBase16

genTokenName :: forall m. MonadGen m => MonadRec m => m TokenName
Expand All @@ -150,9 +150,9 @@ genParty
=> MonadRec m
=> MonadReader GenerationOptions m
=> m Party
genParty = oneOf $ pk :| [ role ]
genParty = oneOf $ addr :| [ role ]
where
pk = PK <$> genPubKey
addr = Address <$> genPubKey

role = Role <$> genTokenName

Expand Down Expand Up @@ -492,9 +492,9 @@ genTokenValue = do
pure $ S.Token currencySymbol tokenName

genPartyValue :: forall m. MonadGen m => MonadRec m => m S.Party
genPartyValue = oneOf $ pk :| [ role ]
genPartyValue = oneOf $ addr :| [ role ]
where
pk = S.PK <$> genPubKey
addr = S.Address <$> genPubKey

role = S.Role <$> genTokenNameValue

Expand Down
12 changes: 6 additions & 6 deletions marlowe-playground-client/src/Marlowe/Holes.purs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ import Language.Marlowe.Core.V1.Semantics
)
import Language.Marlowe.Core.V1.Semantics.Types
( class HasTimeout
, Address
, CurrencySymbol
, IntervalResult(..)
, PubKey
, Timeouts(..)
, TokenName
, _timeInterval
Expand Down Expand Up @@ -322,9 +322,9 @@ getMarloweConstructors TokenType = Map.singleton "Token" $ ArgumentArray

getMarloweConstructors PartyType =
Map.fromFoldable
[ ( Tuple "PK" $ ArgumentArray
[ ( Tuple "Address" $ ArgumentArray
[ DefaultString
"0000000000000000000000000000000000000000000000000000000000000000"
""
]
)
, (Tuple "Role" $ ArgumentArray [ DefaultString "token" ])
Expand Down Expand Up @@ -751,7 +751,7 @@ instance timeoutHasMarloweHoles :: HasMarloweHoles Timeout where
getHoles (TimeParam _) m = m

data Party
= PK PubKey
= Address Address
| Role TokenName

derive instance genericParty :: Generic Party _
Expand All @@ -771,14 +771,14 @@ instance hasArgsParty :: Args Party where
hasNestedArgs = genericHasNestedArgs

instance partyFromTerm :: FromTerm Party S.Party where
fromTerm (PK b) = pure $ S.PK b
fromTerm (Address b) = pure $ S.Address b
fromTerm (Role b) = pure $ S.Role b

instance partyIsMarloweType :: IsMarloweType Party where
marloweType _ = PartyType

instance partyHasMarloweHoles :: HasMarloweHoles Party where
getHoles (PK _) m = m
getHoles (Address _) m = m
getHoles (Role _) m = m

instance partyHasContractData :: HasContractData Party where
Expand Down
2 changes: 1 addition & 1 deletion marlowe-playground-client/src/Marlowe/Monaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ const marloweLexer = moo.compile({
],
TOKEN: ["Token"],
PAYEE: ["Account", "Party"],
PARTY: ["PK", "Role"],
PARTY: ["Address", "Role"],
BOUND: ["Bound"],
TIMEOUT: ["TimeParam"],
VALUE_ID: ["ValueId"],
Expand Down
4 changes: 2 additions & 2 deletions marlowe-playground-client/src/Marlowe/Parser.purs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type HelperFunctions a =
, mkChoiceId :: String -> Term Party -> ChoiceId
, mkValueId :: String -> ValueId
, mkToken :: String -> String -> Token
, mkPK :: String -> Party
, mkAddress :: String -> Party
, mkRole :: String -> Party
, mkAccount :: AccountId -> Payee
, mkParty :: Term Party -> Payee
Expand Down Expand Up @@ -136,7 +136,7 @@ helperFunctions =
, mkChoiceId: ChoiceId
, mkValueId: ValueId
, mkToken: Token
, mkPK: PK
, mkAddress: Address
, mkRole: Role
, mkAccount: Account
, mkParty: Party
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ analyze doAnalyze = do
decorationHeader :: String
decorationHeader =
"""import {
PK, Role, Account, Party, ada, AvailableMoney, Constant, ConstantParam,
Address, Role, Account, Party, ada, AvailableMoney, Constant, ConstantParam,
NegValue, AddValue, SubValue, MulValue, DivValue, ChoiceValue, TimeIntervalStart,
TimeIntervalEnd, UseValue, Cond, AndObs, OrObs, NotObs, ChoseSomething,
ValueGE, ValueGT, ValueLT, ValueLE, ValueEQ, TrueObs, FalseObs, Deposit,
Expand Down
Loading

0 comments on commit 5a87c13

Please sign in to comment.