Skip to content

Commit

Permalink
add integration test showing correctness of adding cosigner key to de…
Browse files Browse the repository at this point in the history
…legation script template
  • Loading branch information
paweljakubas committed Apr 15, 2021
1 parent d59b40d commit 7e8d5fb
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 24 deletions.
Expand Up @@ -103,7 +103,7 @@ spec = describe "SHARED_WALLETS" $ do
"account_index": "30H",
"payment_script_template":
{ "cosigners":
{ "cosigner#0": "1423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db11423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db1" },
{ "cosigner#0": #{accXPubTxt0} },
"template":
{ "all":
[ "cosigner#0",
Expand Down Expand Up @@ -141,7 +141,7 @@ spec = describe "SHARED_WALLETS" $ do
"account_index": "30H",
"payment_script_template":
{ "cosigners":
{ "cosigner#0": "1423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db11423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db1" },
{ "cosigner#0": #{accXPubTxt0} },
"template":
{ "all":
[ "cosigner#0",
Expand All @@ -165,11 +165,11 @@ spec = describe "SHARED_WALLETS" $ do
it "SHARED_WALLETS_CREATE_03 - Create an active shared wallet from account xpub" $ \ctx -> runResourceT $ do
let payload = Json [json| {
"name": "Shared Wallet",
"account_public_key": "1423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db11423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db1",
"account_public_key": #{accXPubTxt0},
"account_index": "30H",
"payment_script_template":
{ "cosigners":
{ "cosigner#0": "1423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db11423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db1" },
{ "cosigner#0": #{accXPubTxt0} },
"template":
{ "all":
[ "cosigner#0",
Expand Down Expand Up @@ -199,11 +199,11 @@ spec = describe "SHARED_WALLETS" $ do
it "SHARED_WALLETS_CREATE_04 - Create a pending shared wallet from account xpub" $ \ctx -> runResourceT $ do
let payload = Json [json| {
"name": "Shared Wallet",
"account_public_key": "1423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db11423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db2",
"account_public_key": #{accXPubTxt0},
"account_index": "30H",
"payment_script_template":
{ "cosigners":
{ "cosigner#0": "1423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db11423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db1" },
{ "cosigner#0": #{accXPubTxt0} },
"template":
{ "all":
[ "cosigner#0",
Expand All @@ -227,11 +227,11 @@ spec = describe "SHARED_WALLETS" $ do
it "SHARED_WALLETS_DELETE_01 - Delete of a shared wallet" $ \ctx -> runResourceT $ do
let payload = Json [json| {
"name": "Shared Wallet",
"account_public_key": "1423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db11423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db1",
"account_public_key": #{accXPubTxt0},
"account_index": "30H",
"payment_script_template":
{ "cosigners":
{ "cosigner#0": "1423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db11423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db1" },
{ "cosigner#0": #{accXPubTxt0} },
"template":
{ "all":
[ "cosigner#0",
Expand All @@ -253,8 +253,7 @@ spec = describe "SHARED_WALLETS" $ do
rDel <- deleteSharedWallet ctx wal
expectResponseCode HTTP.status204 rDel

it "SHARED_WALLETS_PATCH_01 - Patch a pending shared wallet to active shared wallet" $ \ctx -> runResourceT $ do
let accXPubTxt0 = "1423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db11423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db2"
it "SHARED_WALLETS_PATCH_01 - Add cosigner key in a pending shared wallet and transit it to the active shared wallet" $ \ctx -> runResourceT $ do
let payloadCreate = Json [json| {
"name": "Shared Wallet",
"account_public_key": #{accXPubTxt0},
Expand All @@ -275,12 +274,8 @@ spec = describe "SHARED_WALLETS" $ do
expectResponseCode HTTP.status201 rPost
let wal@(ApiSharedWallet (Left pendingWal)) = getFromResponse id rPost
let cosignerKeysPost = pendingWal ^. #paymentScriptTemplate
let (Just accXPub0) = xpubFromText accXPubTxt0
liftIO $ cosigners cosignerKeysPost `shouldBe` Map.fromList [(Cosigner 0,accXPub0)]

let accXPubTxt1 = "1423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db11423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db8"
let (Just accXPub1) = xpubFromText accXPubTxt1

let payloadPatch = Json [json| {
"account_public_key": #{accXPub1},
"cosigner": "cosigner#1"
Expand All @@ -291,12 +286,65 @@ spec = describe "SHARED_WALLETS" $ do
let (ApiSharedWallet (Right activeWal)) = getFromResponse id rPatch
let cosignerKeysPatch = activeWal ^. #paymentScriptTemplate
liftIO $ cosigners cosignerKeysPatch `shouldBe` Map.fromList [(Cosigner 0,accXPub0), (Cosigner 1,accXPub1)]

it "SHARED_WALLETS_PATCH_02 - Add cosigner for delegation script template" $ \ctx -> runResourceT $ do
let payloadCreate = Json [json| {
"name": "Shared Wallet",
"account_public_key": #{accXPubTxt0},
"account_index": "30H",
"payment_script_template":
{ "cosigners":
{ "cosigner#0": #{accXPubTxt0} },
"template":
{ "all":
[ "cosigner#0",
{ "active_from": 120 }
]
}
},
"delegation_script_template":
{ "cosigners":
{ "cosigner#0": #{accXPubTxt0} },
"template":
{ "all":
[ "cosigner#0",
"cosigner#1",
{ "active_from": 120 },
{ "active_until": 100 }
]
}
}
} |]
rPost <- postSharedWallet ctx Default payloadCreate
expectResponseCode HTTP.status201 rPost
let wal@(ApiSharedWallet (Left pendingWal)) = getFromResponse id rPost
let cosignerKeysPostInPayment = pendingWal ^. #paymentScriptTemplate
liftIO $ cosigners cosignerKeysPostInPayment `shouldBe` Map.fromList [(Cosigner 0,accXPub0)]
let (Just cosignerKeysPostInDelegation) = pendingWal ^. #delegationScriptTemplate
liftIO $ cosigners cosignerKeysPostInDelegation `shouldBe` Map.fromList [(Cosigner 0,accXPub0)]

let payloadPatch = Json [json| {
"account_public_key": #{accXPub1},
"cosigner": "cosigner#1"
} |]

rPatch <- patchSharedWallet ctx wal Delegation payloadPatch
expectResponseCode HTTP.status200 rPatch
let (ApiSharedWallet (Right activeWal)) = getFromResponse id rPatch
let (Just cosignerKeysPatch) = activeWal ^. #delegationScriptTemplate
liftIO $ cosigners cosignerKeysPatch `shouldBe` Map.fromList [(Cosigner 0,accXPub0), (Cosigner 1,accXPub1)]
where
xpubFromText :: Text -> Maybe XPub
xpubFromText = fmap eitherToMaybe fromHexText >=> xpubFromBytes

fromHexText :: Text -> Either String ByteString
fromHexText = fromHex . T.encodeUtf8

accXPubTxt0 = "1423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db11423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db2"
(Just accXPub0) = xpubFromText accXPubTxt0

accXPubTxt1 = "1423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db11423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db8"
(Just accXPub1) = xpubFromText accXPubTxt1

instance ToJSON XPub where
toJSON = toJSON . T.decodeLatin1 . hex . xpubToBytes
Expand Up @@ -8,7 +8,6 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
Expand Down
Expand Up @@ -45,7 +45,7 @@ import Cardano.Wallet.Primitive.AddressDiscovery.Script
import Cardano.Wallet.Primitive.AddressDiscovery.Sequential
( AddressPoolGap (..), addresses, mkUnboundedAddressPoolGap )
import Cardano.Wallet.Primitive.AddressDiscovery.SharedState
( SharedState (..), isShared, newSharedState )
( SharedState (..), isShared, mkSharedStateFromAccountXPub )
import Cardano.Wallet.Primitive.Types.Address
( AddressState (..) )
import Cardano.Wallet.Unsafe
Expand Down Expand Up @@ -110,7 +110,7 @@ prop_addressWithScriptFromOurVerKeyIxIn (CatalystSharedState accXPub' accIx' pTe
where
addr = constructAddressFromIx @n pTemplate' dTemplate' keyIx
keyHash = keyHashFromAccXPubIx accXPub' keyIx
sharedState = newSharedState @n accXPub' accIx' g pTemplate' dTemplate'
sharedState = mkSharedStateFromAccountXPub @n accXPub' accIx' g pTemplate' dTemplate'
((Just (keyIx', keyHash')), _) = isShared @n addr sharedState

prop_addressWithScriptFromOurVerKeyIxBeyond
Expand All @@ -124,7 +124,7 @@ prop_addressWithScriptFromOurVerKeyIxBeyond (CatalystSharedState accXPub' accIx'
snd (isShared @n addr sharedState) === sharedState
where
addr = constructAddressFromIx @n pTemplate' dTemplate' keyIx
sharedState = newSharedState @n accXPub' accIx' g pTemplate' dTemplate'
sharedState = mkSharedStateFromAccountXPub @n accXPub' accIx' g pTemplate' dTemplate'

prop_addressDiscoveryMakesAddressUsed
:: forall (n :: NetworkDiscriminant). Typeable n
Expand All @@ -137,7 +137,7 @@ prop_addressDiscoveryMakesAddressUsed (CatalystSharedState accXPub' accIx' pTemp
fromIntegral (L.length ourAddrs) === (fromIntegral (fromEnum ix + 1) + getAddressPoolGap g)
where
addr = constructAddressFromIx @n pTemplate' dTemplate' keyIx
sharedState = newSharedState @n accXPub' accIx' g pTemplate' dTemplate'
sharedState = mkSharedStateFromAccountXPub @n accXPub' accIx' g pTemplate' dTemplate'
((Just (ix,_)), sharedState') = isShared @n addr sharedState
pair' (a,s,_) = (a,s)
ourAddrs = case dTemplate' of
Expand All @@ -159,7 +159,7 @@ prop_addressDoubleDiscovery (CatalystSharedState accXPub' accIx' pTemplate' dTem
snd sharedState' === snd sharedState''
where
addr = constructAddressFromIx @n pTemplate' dTemplate' keyIx
sharedState = newSharedState @n accXPub' accIx' g pTemplate' dTemplate'
sharedState = mkSharedStateFromAccountXPub @n accXPub' accIx' g pTemplate' dTemplate'
sharedState' = isShared @n addr sharedState
sharedState'' = isShared @n addr (snd sharedState')

Expand All @@ -177,7 +177,7 @@ prop_addressDiscoveryImpossibleFromOtherAccXPub (CatalystSharedState _ accIx' pT
addr = constructAddressFromIx @n pTemplate' dTemplate' keyIx
(ScriptTemplate _ script') = pTemplate'
pTemplate'' = ScriptTemplate (Map.fromList [(Cosigner 0, getRawKey accXPub')]) script'
sharedState = newSharedState @n accXPub' accIx' g pTemplate'' dTemplate'
sharedState = mkSharedStateFromAccountXPub @n accXPub' accIx' g pTemplate'' dTemplate'

prop_addressDiscoveryImpossibleFromOtherAccountOfTheSameRootXPrv
:: forall (n :: NetworkDiscriminant). Typeable n
Expand All @@ -195,7 +195,7 @@ prop_addressDiscoveryImpossibleFromOtherAccountOfTheSameRootXPrv (CatalystShared
(ScriptTemplate _ script') = pTemplate'
pTemplate'' = ScriptTemplate (Map.fromList [(Cosigner 0, getRawKey accXPub')]) script'
pTemplate''' = ScriptTemplate (Map.fromList [(Cosigner 0, getRawKey accXPub'')]) script'
sharedState = newSharedState @n accXPub'' accIx'' g pTemplate'' dTemplate'
sharedState = mkSharedStateFromAccountXPub @n accXPub'' accIx'' g pTemplate'' dTemplate'
addr = constructAddressFromIx @n pTemplate''' dTemplate' keyIx

prop_addressDiscoveryImpossibleWithinAccountButDifferentScript
Expand All @@ -211,7 +211,7 @@ prop_addressDiscoveryImpossibleWithinAccountButDifferentScript (CatalystSharedSt
where
(ScriptTemplate cosignerXpubs _) = pTemplate'
pTemplate'' = ScriptTemplate cosignerXpubs script'
sharedState = newSharedState @n accXPub' accIx' g pTemplate' dTemplate'
sharedState = mkSharedStateFromAccountXPub @n accXPub' accIx' g pTemplate' dTemplate'
addr = constructAddressFromIx @n pTemplate'' dTemplate' keyIx

prop_addressDiscoveryDoesNotChangeGapInvariance
Expand All @@ -224,7 +224,7 @@ prop_addressDiscoveryDoesNotChangeGapInvariance (CatalystSharedState accXPub' ac
fromIntegral (L.length mapOfConsecutiveUnused) === getAddressPoolGap g
where
addr = constructAddressFromIx @n pTemplate' dTemplate' keyIx
sharedState = newSharedState @n accXPub' accIx' g pTemplate' dTemplate'
sharedState = mkSharedStateFromAccountXPub @n accXPub' accIx' g pTemplate' dTemplate'
(_, sharedState') = isShared @n addr sharedState
mapOfConsecutiveUnused =
L.tail $
Expand Down

0 comments on commit 7e8d5fb

Please sign in to comment.