Skip to content

Commit

Permalink
make format
Browse files Browse the repository at this point in the history
  • Loading branch information
Geometer1729 committed Aug 8, 2022
1 parent 5dc3e08 commit 6991d5b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
35 changes: 19 additions & 16 deletions src/Wallet/KeyFile.purs
Expand Up @@ -9,7 +9,7 @@ module Wallet.KeyFile

import Prelude

import Aeson(encodeAeson)
import Aeson (encodeAeson)
import Cardano.TextEnvelope
( TextEnvelopeType
( PaymentSigningKeyShelleyed25519
Expand All @@ -18,18 +18,18 @@ import Cardano.TextEnvelope
, textEnvelopeBytes
)
import Data.Newtype (wrap)
import Data.Maybe(Maybe)
import Data.Maybe (Maybe)
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import Effect.Exception (error)
import Helpers (liftM)
import Node.Encoding as Encoding
import Node.FS.Sync (readTextFile,writeTextFile)
import Node.FS.Sync (readTextFile, writeTextFile)
import Node.Path (FilePath)
import Serialization (privateKeyFromBytes,bytesFromPrivateKey)
import Serialization (privateKeyFromBytes, bytesFromPrivateKey)
import Serialization.Types (PrivateKey)
import Types.ByteArray (ByteArray)
import Types.RawBytes(rawBytesToHex)
import Types.RawBytes (rawBytesToHex)
import Wallet.Key
( PrivatePaymentKey(PrivatePaymentKey)
, PrivateStakeKey(PrivateStakeKey)
Expand All @@ -53,29 +53,32 @@ privateStakeKeyFromFile filePath = do
PrivateStakeKey <$> privateKeyFromBytes (wrap bytes)

privatePaymentKeyToFile :: FilePath -> PrivatePaymentKey -> Aff Unit
privatePaymentKeyToFile filePath key
= liftEffect $ writeTextFile Encoding.UTF8 filePath (formatPaymentKey key)
privatePaymentKeyToFile filePath key = liftEffect $ writeTextFile Encoding.UTF8
filePath
(formatPaymentKey key)

privateStakeKeyToFile :: FilePath -> PrivateStakeKey -> Aff Unit
privateStakeKeyToFile filePath key
= liftEffect $ writeTextFile Encoding.UTF8 filePath (formatStakeKey key)
privateStakeKeyToFile filePath key = liftEffect $ writeTextFile Encoding.UTF8
filePath
(formatStakeKey key)

formatPaymentKey :: PrivatePaymentKey -> String
formatPaymentKey (PrivatePaymentKey key) = encodeAeson >>> show $
{ "type" : "PaymentSigningKeyShelley_ed25519",
description : "Payment Signing Key",
cborHex : keyToCbor key
{ "type": "PaymentSigningKeyShelley_ed25519"
, description: "Payment Signing Key"
, cborHex: keyToCbor key
}

formatStakeKey :: PrivateStakeKey -> String
formatStakeKey (PrivateStakeKey key) = encodeAeson >>> show $
{ "type": "StakeSigningKeyShelley_ed25519",
description : "Stake Signing Key",
cborHex : keyToCbor key
{ "type": "StakeSigningKeyShelley_ed25519"
, description: "Stake Signing Key"
, cborHex: keyToCbor key
}

keyToCbor :: PrivateKey -> Maybe String
keyToCbor = ((rawBytesToHex >>> (magicPrefix <> _)) <$> _) <<< bytesFromPrivateKey
keyToCbor = ((rawBytesToHex >>> (magicPrefix <> _)) <$> _) <<<
bytesFromPrivateKey

magicPrefix :: String
magicPrefix = "5820"
52 changes: 32 additions & 20 deletions test/PrivateKey.purs
Expand Up @@ -3,7 +3,7 @@ module Test.PrivateKey where
import Contract.Config (testnetConfig)
import Prelude

import Aeson(fromString)
import Aeson (fromString)
import Cardano.Types.Transaction
( Ed25519Signature(Ed25519Signature)
, Transaction(Transaction)
Expand All @@ -17,14 +17,14 @@ import Data.Lens.Index (ix)
import Data.Lens.Iso.Newtype (unto)
import Data.Lens.Record (prop)
import Data.Maybe (Maybe(Just))
import Data.Newtype(unwrap)
import Effect.Class(liftEffect)
import Data.Newtype (unwrap)
import Effect.Class (liftEffect)
import Mote (group, test)
import Node.Encoding as Encoding
import Node.FS.Sync (readTextFile,writeTextFile,unlink)
import Serialization(publicKeyFromPrivateKey,publicKeyHash)
import Node.FS.Sync (readTextFile, writeTextFile, unlink)
import Serialization (publicKeyFromPrivateKey, publicKeyHash)
import Test.Fixtures (txFixture1)
import Test.Spec.Assertions (shouldEqual,fail)
import Test.Spec.Assertions (shouldEqual, fail)
import TestM (TestPlanM)
import Type.Proxy (Proxy(Proxy))
import Wallet.Spec
Expand All @@ -33,10 +33,10 @@ import Wallet.Spec
, WalletSpec(UseKeys)
)
import Wallet.KeyFile
(privatePaymentKeyToFile
,privatePaymentKeyFromFile
,privateStakeKeyFromFile
,privateStakeKeyToFile
( privatePaymentKeyToFile
, privatePaymentKeyFromFile
, privateStakeKeyFromFile
, privateStakeKeyToFile
)

suite :: TestPlanM Unit
Expand Down Expand Up @@ -73,20 +73,32 @@ suite = do
"ed25519_sig1w7nkmvk57r6094j9u85r4pddve0hg3985ywl9yzwecx03aa9fnfspl9zmtngmqmczd284lnusjdwkysgukxeq05a548dyepr6vn62qs744wxz"
)
test "privateKeyToFile round-trips" do
key <- privatePaymentKeyFromFile "fixtures/test/parsing/PrivateKey/payment.skey"
privatePaymentKeyToFile "fixtures/test/parsing/PrivateKey/payment_round_trip.skey" key
key2 <- privatePaymentKeyFromFile "fixtures/test/parsing/PrivateKey/payment_round_trip.skey"
liftEffect $ unlink "fixtures/test/parsing/PrivateKey/payment_round_trip.skey"
key <- privatePaymentKeyFromFile
"fixtures/test/parsing/PrivateKey/payment.skey"
privatePaymentKeyToFile
"fixtures/test/parsing/PrivateKey/payment_round_trip.skey"
key
key2 <- privatePaymentKeyFromFile
"fixtures/test/parsing/PrivateKey/payment_round_trip.skey"
liftEffect $ unlink
"fixtures/test/parsing/PrivateKey/payment_round_trip.skey"
-- converting to pub key hashes to check equality isn't great
-- but there aren't Eq instances for PrivateKeys or PublicKeys
pkh <- liftEffect $ publicKeyHash <$> publicKeyFromPrivateKey (unwrap key)
pkh2 <- liftEffect $ publicKeyHash <$> publicKeyFromPrivateKey (unwrap key2)
pkh2 <- liftEffect $ publicKeyHash <$> publicKeyFromPrivateKey
(unwrap key2)
pkh `shouldEqual` pkh2
test "stakeKeyToFile round-trips" do
key <- privateStakeKeyFromFile "fixtures/test/parsing/PrivateKey/stake.skey"
privateStakeKeyToFile "fixtures/test/parsing/PrivateKey/stake_round_trip.skey" key
key2 <- privateStakeKeyFromFile "fixtures/test/parsing/PrivateKey/stake_round_trip.skey"
liftEffect $ unlink "fixtures/test/parsing/PrivateKey/stake_round_trip.skey"
key <- privateStakeKeyFromFile
"fixtures/test/parsing/PrivateKey/stake.skey"
privateStakeKeyToFile
"fixtures/test/parsing/PrivateKey/stake_round_trip.skey"
key
key2 <- privateStakeKeyFromFile
"fixtures/test/parsing/PrivateKey/stake_round_trip.skey"
liftEffect $ unlink
"fixtures/test/parsing/PrivateKey/stake_round_trip.skey"
pkh <- liftEffect $ publicKeyHash <$> publicKeyFromPrivateKey (unwrap key)
pkh2 <- liftEffect $ publicKeyHash <$> publicKeyFromPrivateKey (unwrap key2)
pkh2 <- liftEffect $ publicKeyHash <$> publicKeyFromPrivateKey
(unwrap key2)
pkh `shouldEqual` pkh2

0 comments on commit 6991d5b

Please sign in to comment.