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 accept #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
68 changes: 43 additions & 25 deletions src/MultiSig/Impl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,36 @@ recvInternal = drop

recvExternal :: '[Slice] :-> '[]
recvExternal = do
decodeFromSlice @SignMsg
endS
recvSignMsg

dup
preloadFromSlice @Nonce
pushInt 0
if IsEq
then do
comment "Handling init message"
drop
pushRoot
decodeFromCell @Storage
moveOnTop @4
pushInt 0
if IsEq
then do
comment "State wasn't yet initialized"
accept
stacktype @'[TimestampDict, OrderDict, DSet PublicKey, Word32]
reversePrefix @4
pushInt 1
encodeToCell @Storage
popRoot
else do
comment "State was already initialized, rejecting"
drop
drop
drop
drop
else do
decodeFromSlice @SignMsg
endS
recvSignMsg

recvSignMsg :: DecodeSliceFields SignMsg :-> '[]
recvSignMsg = viaSubroutine @(DecodeSliceFields SignMsg) @'[] "recvSignMsg" $ do
Expand Down Expand Up @@ -64,10 +90,6 @@ recvSignMsg = viaSubroutine @(DecodeSliceFields SignMsg) @'[] "recvSignMsg" $ do
moveOnTop @8
filterValidSignatures
stacktype @[AccumPkDict, Hash SignMsgBody, Cell SignMsgBody, OrderDict, DSet PublicKey, Word32, Nonce, TimestampDict]
dup
cast @AccumPkDict @(DSet PublicKey)
dictEmpty
throwIf NoValidSignatures

-- Add valid signatures to the storage's OrderDict
push @5 -- push K on the top
Expand Down Expand Up @@ -166,16 +188,16 @@ filterValidSignatures =
rollRev @2
push @5
rollRev @2
stacktype' @[PublicKey, Signature, Hash SignMsgBody]
chkSignU
stacktype' @[Bool, PublicKey, SignDict, AccumPkDict, Hash SignMsgBody, DSet PublicKey]
if Holds then do
moveOnTop @2
cast @AccumPkDict @(DSet PublicKey)
dsetSet
cast @(DSet PublicKey) @AccumPkDict
swap
else
drop
throwIfNot InvalidSignature
accept
moveOnTop @2
cast @AccumPkDict @(DSet PublicKey)
dsetSet
cast @(DSet PublicKey) @AccumPkDict
swap
pop @1
pop @1

Expand All @@ -196,8 +218,8 @@ extendOrder =
stacktype' @[Order, Word32]
cast @Order @Slice
decodeFromSliceFull @Order
swap
drop -- drop MsgBody from storage because there is one from msg
pop @1
-- ^ drop MsgBody from storage because there is one from msg
moveOnTop @2
swap
false -- not new one
Expand Down Expand Up @@ -316,15 +338,11 @@ getOrdersByKey = do
swap
)
(drop >> drop >> drop)
swap
drop
swap
drop
pop @1
pop @1

checkSignMsgBodyBelongsToPk
:: DSet PublicKey & PublicKey & Bool & s :-> Bool & s
checkSignMsgBodyBelongsToPk = do
dsetGet
if IsEq
then true
else false
equalInt
8 changes: 4 additions & 4 deletions src/MultiSig/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Prelude
import FiftAsm

newtype Nonce = Nonce Word32
deriving (Integral, Num, Real, Ord, Eq, Enum)

type instance ToTVM Nonce = 'IntT

Expand All @@ -36,7 +37,6 @@ instance DecodeSlice Nonce where
instance EncodeBuilder Nonce where
encodeToBuilder = st32Unsigned


-- Msg part
type SignDict = Dict PublicKey Signature
data SignMsg = SignMsg
Expand Down Expand Up @@ -140,7 +140,7 @@ type instance ToTVM Order = 'SliceT
data MultiSigError
= NonceMismatch
| MsgExpired
| NoValidSignatures
| InvalidSignature
| ErrorParsingMsg
deriving (Eq, Ord, Show, Generic)

Expand All @@ -149,11 +149,11 @@ instance Exception MultiSigError
instance Enum MultiSigError where
toEnum 32 = NonceMismatch
toEnum 33 = MsgExpired
toEnum 34 = NoValidSignatures
toEnum 34 = InvalidSignature
toEnum 35 = ErrorParsingMsg
toEnum _ = error "Uknown MultiSigError id"

fromEnum NonceMismatch = 32
fromEnum MsgExpired = 33
fromEnum NoValidSignatures = 34
fromEnum InvalidSignature = 34
fromEnum ErrorParsingMsg = 35