Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scaffold simple Cardano client interface
* Computes a payment address from a verification key * Simplifies submit_test.sh script remove call to compute address
- Loading branch information
1 parent
7ed9c79
commit 511af95
Showing
6 changed files
with
70 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-- | A basic cardano-node client that can talk to a local cardano-node. | ||
-- | ||
-- The idea of this module is to provide a Haskell interface on top of cardano-cli's API. | ||
module CardanoClient where | ||
|
||
import Hydra.Prelude | ||
|
||
import Cardano.Api (Address, NetworkId, ShelleyAddr) | ||
import Cardano.Api.Shelley (VerificationKey (PaymentVerificationKey)) | ||
import Cardano.CLI.Shelley.Run.Address (buildShelleyAddress) | ||
import qualified Cardano.Ledger.Keys as Keys | ||
import qualified Hydra.Chain.Direct.Wallet as Hydra | ||
|
||
type NodeSocket = FilePath | ||
|
||
-- | Build an address give a key. | ||
-- From <runAddressBuild https://github.com/input-output-hk/cardano-node/blob/master/cardano-cli/src/Cardano/CLI/Shelley/Run/Address.hs#L106> | ||
buildAddress :: Hydra.VerificationKey -> NetworkId -> IO (Address ShelleyAddr) | ||
buildAddress vKey networkId = do | ||
let shelleyKey = PaymentVerificationKey $ Keys.VKey vKey | ||
runExceptT (buildShelleyAddress shelleyKey Nothing networkId) >>= \case | ||
Left err -> throwIO $ BuildAddressException (show err) | ||
Right addr -> pure addr | ||
|
||
newtype BuildAddressException = BuildAddressException Text | ||
deriving (Show) | ||
|
||
instance Exception BuildAddressException |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module Test.CardanoClientSpec where | ||
|
||
import Cardano.Api (NetworkId (Testnet), NetworkMagic (NetworkMagic)) | ||
import qualified Cardano.Ledger.Keys as Ledger | ||
import CardanoClient (buildAddress) | ||
import Hydra.Ledger.Cardano (genKeyPair) | ||
import Hydra.Prelude | ||
import Test.Hydra.Prelude | ||
import Test.QuickCheck.Monadic (PropertyM, forAllM, monadicIO, run) | ||
|
||
spec :: Spec | ||
spec = do | ||
prop "can generate address from verification key" $ monadicIO generateAddressFromKey | ||
|
||
generateAddressFromKey :: PropertyM IO () | ||
generateAddressFromKey = | ||
forAllM genKeyPair $ \(Ledger.KeyPair (Ledger.VKey key) _) -> do | ||
void $ run $ buildAddress key (Testnet $ NetworkMagic 42) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters