-
Notifications
You must be signed in to change notification settings - Fork 214
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 a cli option for creating a new transaction #225
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
eabe431
Add a cli option for creating a new transaction
akegalj 4a908a8
Remove signed from FromText Natural instance
akegalj a083eb4
Rename targets to payments in API and CLI
akegalj d685b35
Comply to 80chars line width
akegalj 863c2c3
Comply to hlint
akegalj dace9ee
Comply to 80chars line width
akegalj a14624e
Fix small review points, mostly cosmetic
akegalj e575e62
Fix CLI example comment
akegalj 60177c5
Don't confirm password when creating tx in CLI
akegalj 076bea2
Reformat CLI example to max 80chars width
akegalj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,13 @@ import Prelude hiding | |
( getLine ) | ||
|
||
import Cardano.CLI | ||
( Port (..), getLine, getSensitiveLine, parseArgWith, putErrLn ) | ||
( Port (..) | ||
, getLine | ||
, getSensitiveLine | ||
, parseAllArgsWith | ||
, parseArgWith | ||
, putErrLn | ||
) | ||
import Cardano.Environment | ||
( network ) | ||
import Cardano.Wallet | ||
|
@@ -35,7 +41,12 @@ import Cardano.Wallet.Api | |
import Cardano.Wallet.Api.Server | ||
( server ) | ||
import Cardano.Wallet.Api.Types | ||
( ApiMnemonicT (..), ApiT (..), WalletPostData (..), WalletPutData (..) ) | ||
( ApiMnemonicT (..) | ||
, ApiT (..) | ||
, PostTransactionData (..) | ||
, WalletPostData (..) | ||
, WalletPutData (..) | ||
) | ||
import Cardano.Wallet.Compatibility.HttpBridge | ||
( HttpBridge ) | ||
import Cardano.Wallet.Primitive.AddressDerivation | ||
|
@@ -54,6 +65,7 @@ import Data.Function | |
( (&) ) | ||
import Data.Functor | ||
( (<&>) ) | ||
import qualified Data.List.NonEmpty as NE | ||
import Data.Proxy | ||
( Proxy (..) ) | ||
import Data.Text.Class | ||
|
@@ -122,6 +134,7 @@ Usage: | |
cardano-wallet wallet get [--port=INT] <wallet-id> | ||
cardano-wallet wallet update [--port=INT] <wallet-id> --name=STRING | ||
cardano-wallet wallet delete [--port=INT] <wallet-id> | ||
cardano-wallet transaction create [--port=INT] --wallet-id=STRING --payment=PAYMENT... | ||
cardano-wallet -h | --help | ||
cardano-wallet --version | ||
|
||
|
@@ -130,6 +143,11 @@ Options: | |
--bridge-port <INT> port used for communicating with the http-bridge [default: 8080] | ||
--address-pool-gap <INT> number of unused consecutive addresses to keep track of [default: 20] | ||
--size <INT> number of mnemonic words to generate [default: 15] | ||
--payment <PAYMENT> address to send to and amount to send separated by @: '<amount>@<address>' | ||
|
||
Examples: | ||
cardano-wallet transaction create --wallet-id 2512a00e9653fe49a44a5886202e24d77eeb998f --payment 22@2cWKMJemoBam7gg1y5K2aFDhAm5L8fVc96NfxgcGhdLMFTsToNAU9t5HVdBBQKy4iDswL # Create a transaction and send 22 lovelace from wallet-id to specified addres | ||
|
||
|] | ||
|
||
main :: IO () | ||
|
@@ -153,18 +171,22 @@ exec manager args | |
bridgePort <- args `parseArg` longOption "bridge-port" | ||
execServer walletPort bridgePort | ||
|
||
| args `isPresent` command "generate" && args `isPresent` command "mnemonic" = do | ||
| args `isPresent` command "generate" && | ||
args `isPresent` command "mnemonic" = do | ||
KtorZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
n <- args `parseArg` longOption "size" | ||
execGenerateMnemonic n | ||
|
||
| args `isPresent` command "wallet" && args `isPresent` command "list" = do | ||
| args `isPresent` command "wallet" && | ||
args `isPresent` command "list" = do | ||
runClient @Wallet Aeson.encodePretty listWallets | ||
|
||
| args `isPresent` command "wallet" && args `isPresent` command "get" = do | ||
| args `isPresent` command "wallet" && | ||
args `isPresent` command "get" = do | ||
wId <- args `parseArg` argument "wallet-id" | ||
runClient @Wallet Aeson.encodePretty $ getWallet $ ApiT wId | ||
|
||
| args `isPresent` command "wallet" && args `isPresent` command "create" = do | ||
| args `isPresent` command "wallet" && | ||
args `isPresent` command "create" = do | ||
wName <- args `parseArg` longOption "name" | ||
wGap <- args `parseArg` longOption "address-pool-gap" | ||
wSeed <- do | ||
|
@@ -179,31 +201,23 @@ exec manager args | |
getLine prompt parser <&> \case | ||
(Nothing, _) -> Nothing | ||
(Just a, t) -> Just (a, t) | ||
(wPwd, _) <- do | ||
let prompt = "Please enter a passphrase: " | ||
let parser = fromText @(Passphrase "encryption") | ||
getSensitiveLine prompt parser | ||
(wPwd', _) <- do | ||
let prompt = "Enter the passphrase a second time: " | ||
let parser = fromText @(Passphrase "encryption") | ||
getSensitiveLine prompt parser | ||
when (wPwd /= wPwd') $ do | ||
putErrLn "Passphrases don't match." | ||
exitFailure | ||
wPwd <- getPassphrase | ||
runClient @Wallet Aeson.encodePretty $ postWallet $ WalletPostData | ||
(Just $ ApiT wGap) | ||
(ApiMnemonicT . second T.words $ wSeed) | ||
(ApiMnemonicT . second T.words <$> wSndFactor) | ||
(ApiT wName) | ||
(ApiT wPwd) | ||
|
||
| args `isPresent` command "wallet" && args `isPresent` command "update" = do | ||
| args `isPresent` command "wallet" && | ||
args `isPresent` command "update" = do | ||
wId <- args `parseArg` argument "wallet-id" | ||
wName <- args `parseArg` longOption "name" | ||
runClient @Wallet Aeson.encodePretty $ putWallet (ApiT wId) $ WalletPutData | ||
(Just $ ApiT wName) | ||
|
||
| args `isPresent` command "wallet" && args `isPresent` command "delete" = do | ||
| args `isPresent` command "wallet" && | ||
args `isPresent` command "delete" = do | ||
wId <- args `parseArg` argument "wallet-id" | ||
runClient @Wallet (const "") $ deleteWallet (ApiT wId) | ||
|
||
|
@@ -219,11 +233,38 @@ exec manager args | |
Just version -> do | ||
TIO.putStrLn $ T.pack version | ||
|
||
| args `isPresent` command "transaction" && | ||
args `isPresent` command "create" = do | ||
wId <- args `parseArg` longOption "wallet-id" | ||
ts <- args `parseAllArgs` longOption "payment" | ||
wPwd <- getPassphrase | ||
runClient @Transaction Aeson.encodePretty $ createTransaction (ApiT wId) $ | ||
PostTransactionData | ||
-- NOTE: we are sure this will be non-empty | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side-note: for this kind of comment, the reason why we are sure is really what we're interested in 😉 |
||
(NE.fromList ts) | ||
(ApiT wPwd) | ||
|
||
| otherwise = | ||
exitWithUsage cli | ||
where | ||
parseArg :: FromText a => Arguments -> Option -> IO a | ||
parseArg = parseArgWith cli | ||
parseAllArgs :: FromText a => Arguments -> Option -> IO [a] | ||
parseAllArgs = parseAllArgsWith cli | ||
getPassphrase :: IO (Passphrase "encryption") | ||
getPassphrase = do | ||
(wPwd, _) <- do | ||
let prompt = "Please enter a passphrase: " | ||
let parser = fromText @(Passphrase "encryption") | ||
getSensitiveLine prompt parser | ||
(wPwd', _) <- do | ||
let prompt = "Enter the passphrase a second time: " | ||
let parser = fromText @(Passphrase "encryption") | ||
getSensitiveLine prompt parser | ||
when (wPwd /= wPwd') $ do | ||
putErrLn "Passphrases don't match." | ||
exitFailure | ||
pure wPwd | ||
|
||
_ :<|> -- List Address | ||
( deleteWallet | ||
|
@@ -233,9 +274,7 @@ exec manager args | |
:<|> putWallet | ||
:<|> _ -- Put Wallet Passphrase | ||
) | ||
:<|> | ||
( _ -- Create Transaction | ||
) | ||
:<|> createTransaction | ||
= client (Proxy @("v2" :> Api)) | ||
|
||
-- | 'runClient' requires a type-application to carry a particular | ||
|
@@ -272,9 +311,9 @@ exec manager args | |
TIO.hPutStrLn stderr "Ok." | ||
BL8.putStrLn (encode a) | ||
|
||
-- | Namespaces for commands. Only 'Wallet' for now, 'Address' & 'Transaction' | ||
-- later. | ||
-- | Namespaces for commands. | ||
data Wallet deriving (Typeable) | ||
data Transaction deriving (Typeable) | ||
|
||
-- | Start a web-server to serve the wallet backend API on the given port. | ||
execServer :: Port "wallet" -> Port "bridge" -> IO () | ||
|
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about:
--payment
on a new line with a\
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense - didn't notice
👍
👍