Skip to content

Commit fa2313c

Browse files
author
gege251
committed
Add test for build without sign
1 parent 23a2ff9 commit fa2313c

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

src/BotPlutusInterface/Files.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module BotPlutusInterface.Files (
1616
writeValidatorScriptFile,
1717
datumJsonFilePath,
1818
skeyToDummyPrivKey,
19+
vkeyToDummyPrivKey,
1920
writeDatumJsonFile,
2021
) where
2122

test/Spec/BotPlutusInterface/Contract.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ import Spec.MockContract (
5757
pkhAddr1,
5858
runContractPure,
5959
tip,
60+
toVerificationKeyFile,
6061
utxos,
62+
verificationKey1,
6163
)
6264
import Test.Tasty (TestTree, testGroup)
6365
import Test.Tasty.HUnit (Assertion, assertBool, assertFailure, testCase, (@?=))
@@ -74,6 +76,7 @@ tests =
7476
[ testCase "Send ada to address" sendAda
7577
, testCase "Send ada to address with staking key" sendAdaStaking
7678
, testCase "Support multiple signatories" multisigSupport
79+
, testCase "Create a tx without signing" withoutSigning
7780
, testCase "Send native tokens" sendTokens
7881
, testCase "Send native tokens (without token name)" sendTokensWithoutName
7982
, testCase "Mint native tokens" mintTokens
@@ -301,6 +304,39 @@ multisigSupport = do
301304
)
302305
]
303306

307+
withoutSigning :: Assertion
308+
withoutSigning = do
309+
let txOutRef = TxOutRef "e406b0cf676fc2b1a9edb0617f259ad025c20ea6f0333820aa7cef1bfe7302e5" 0
310+
txOut = TxOut pkhAddr1 (Ada.lovelaceValueOf 1250) Nothing
311+
initState =
312+
def
313+
& utxos .~ [(txOutRef, txOut)]
314+
& files .~ uncurry Map.singleton (toVerificationKeyFile "./signing-keys" verificationKey1)
315+
inTxId = encodeByteString $ fromBuiltin $ TxId.getTxId $ Tx.txOutRefId txOutRef
316+
317+
contract :: Contract Text (Endpoint "SendAda" ()) Text CardanoTx
318+
contract = do
319+
let constraints = Constraints.mustPayToPubKey paymentPkh2 (Ada.lovelaceValueOf 1000)
320+
submitTx constraints
321+
322+
-- Building and siging the tx should include both signing keys
323+
assertContractWithTxId contract initState $ \state outTxId ->
324+
assertCommandHistory
325+
state
326+
[
327+
( 6
328+
, [text|
329+
cardano-cli transaction build --alonzo-era
330+
--tx-in ${inTxId}#0
331+
--tx-in-collateral ${inTxId}#0
332+
--tx-out ${addr2}+1000
333+
--required-signer-hash ${pkh1'}
334+
--change-address ${addr1}
335+
--mainnet --protocol-params-file ./protocol.json --out-file ./txs/tx-${outTxId}.raw
336+
|]
337+
)
338+
]
339+
304340
sendTokens :: Assertion
305341
sendTokens = do
306342
let txOutRef1 = TxOutRef "08b27dbdcff9ab3b432638536ec7eab36c8a2e457703fb1b559dd754032ef431" 0

test/Spec/MockContract.hs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
{-# OPTIONS_GHC -Wno-orphans #-}
55

66
module Spec.MockContract (
7+
-- Mock private and public keys etc.
78
signingKey1,
89
signingKey2,
9-
runContractPure,
10+
signingKey3,
11+
verificationKey1,
12+
verificationKey2,
13+
verificationKey3,
1014
toSigningKeyFile,
11-
runContractPure',
12-
MockContractState (..),
15+
toVerificationKeyFile,
1316
pubKey1,
1417
pubKey2,
1518
pubKey3,
@@ -28,6 +31,10 @@ module Spec.MockContract (
2831
pkhAddr1,
2932
pkhAddr2,
3033
pkhAddr3,
34+
-- Test interpreter
35+
runContractPure,
36+
runContractPure',
37+
MockContractState (..),
3138
commandHistory,
3239
instanceUpdateHistory,
3340
logHistory,
@@ -52,13 +59,15 @@ import Cardano.Api (
5259
AsType,
5360
FileError (FileError, FileIOError),
5461
HasTextEnvelope,
62+
Key (VerificationKey, getVerificationKey),
5563
NetworkId (Mainnet),
5664
PaymentKey,
5765
SigningKey (PaymentSigningKey),
5866
TextEnvelope,
5967
TextEnvelopeDescr,
6068
TextEnvelopeError (TextEnvelopeAesonDecodeError),
6169
deserialiseFromTextEnvelope,
70+
getVerificationKey,
6271
serialiseToTextEnvelope,
6372
)
6473
import Cardano.Crypto.DSIGN (genKeyDSIGN)
@@ -116,10 +125,15 @@ signingKey1 = PaymentSigningKey $ genKeyDSIGN $ mkSeedFromBytes $ ByteString.rep
116125
signingKey2 = PaymentSigningKey $ genKeyDSIGN $ mkSeedFromBytes $ ByteString.replicate 32 1
117126
signingKey3 = PaymentSigningKey $ genKeyDSIGN $ mkSeedFromBytes $ ByteString.replicate 32 2
118127

128+
verificationKey1, verificationKey2, verificationKey3 :: VerificationKey PaymentKey
129+
verificationKey1 = getVerificationKey signingKey1
130+
verificationKey2 = getVerificationKey signingKey2
131+
verificationKey3 = getVerificationKey signingKey3
132+
119133
pubKey1, pubKey2, pubKey3 :: PubKey
120-
pubKey1 = toPubKey signingKey1
121-
pubKey2 = toPubKey signingKey2
122-
pubKey3 = toPubKey signingKey3
134+
pubKey1 = skeyToPubKey signingKey1
135+
pubKey2 = skeyToPubKey signingKey2
136+
pubKey3 = skeyToPubKey signingKey3
123137

124138
pkh1, pkh2, pkh3 :: PubKeyHash
125139
pkh1 = Ledger.pubKeyHash pubKey1
@@ -146,19 +160,32 @@ addr1 = unsafeSerialiseAddress Mainnet (Ledger.pubKeyHashAddress paymentPkh1 Not
146160
addr2 = unsafeSerialiseAddress Mainnet (Ledger.pubKeyHashAddress paymentPkh2 Nothing)
147161
addr3 = unsafeSerialiseAddress Mainnet (Ledger.pubKeyHashAddress paymentPkh3 Nothing)
148162

149-
toPubKey :: SigningKey PaymentKey -> PubKey
150-
toPubKey =
163+
skeyToPubKey :: SigningKey PaymentKey -> PubKey
164+
skeyToPubKey =
151165
Ledger.toPublicKey
152166
. Files.unDummyPrivateKey
153167
. fromRight (error "Impossible happened")
154168
. Files.skeyToDummyPrivKey
155169

170+
vkeyToPubKey :: VerificationKey PaymentKey -> PubKey
171+
vkeyToPubKey =
172+
Ledger.toPublicKey
173+
. Files.unDummyPrivateKey
174+
. fromRight (error "Impossible happened")
175+
. Files.vkeyToDummyPrivKey
176+
156177
toSigningKeyFile :: FilePath -> SigningKey PaymentKey -> (FilePath, MockFile)
157178
toSigningKeyFile signingKeyFileDir sKey =
158-
( signingKeyFileDir ++ "/signing-key-" ++ show (Ledger.pubKeyHash (toPubKey sKey)) ++ ".skey"
179+
( signingKeyFileDir ++ "/signing-key-" ++ show (Ledger.pubKeyHash (skeyToPubKey sKey)) ++ ".skey"
159180
, TextEnvelopeFile $ serialiseToTextEnvelope Nothing sKey
160181
)
161182

183+
toVerificationKeyFile :: FilePath -> VerificationKey PaymentKey -> (FilePath, MockFile)
184+
toVerificationKeyFile signingKeyFileDir vKey =
185+
( signingKeyFileDir ++ "/signing-key-" ++ show (Ledger.pubKeyHash (vkeyToPubKey vKey)) ++ ".vkey"
186+
, TextEnvelopeFile $ serialiseToTextEnvelope Nothing vKey
187+
)
188+
162189
data MockFile
163190
= TextEnvelopeFile TextEnvelope
164191
| JsonFile JSON.Value

0 commit comments

Comments
 (0)