|
1 | | -# psg-cardano-wallet-api |
2 | | -Scala client to the Cardano wallet REST API |
| 1 | +# PSG Cardano Wallet API |
| 2 | + |
| 3 | +_For consultancy services email [enterprise.solutions@iohk.io](mailto:enterprise.solutions@iohk.io)_ |
| 4 | +### Scala and Java client for the Cardano Wallet API |
| 5 | + |
| 6 | +The Cardano node exposes a [REST like API](https://github.com/input-output-hk/cardano-wallet) |
| 7 | +allowing clients to perform a variety of tasks including |
| 8 | + - creating or restoring a wallet |
| 9 | + - submitting a transaction with or without [metadata](https://github.com/input-output-hk/cardano-wallet/wiki/TxMetadata) |
| 10 | + - checking on the status of the node |
| 11 | + - listing transactions |
| 12 | + - listing wallets |
| 13 | + |
| 14 | +The full list of capabilities can be found [here](https://input-output-hk.github.io/cardano-wallet/api/edge/). |
| 15 | + |
| 16 | +This artefact wraps calls to that API to make them easily accessible to Java or Scala developers. |
| 17 | + |
| 18 | +It also provides an executable jar to provide rudimentary command line access. |
| 19 | + |
| 20 | + |
| 21 | +- [Building](#building) |
| 22 | +- [Usage](#usage) |
| 23 | + - [scala](#usagescala) |
| 24 | + - [java](#usagejava) |
| 25 | +- [Command line executable jar](#cmdline) |
| 26 | +- [Examples](#examples) |
| 27 | +- [Issues](#issues) |
| 28 | + |
| 29 | + |
| 30 | +### <a name="building"></a> Building |
| 31 | + |
| 32 | +This is an `sbt` project, so the usual `sbt` commands apply. |
| 33 | + |
| 34 | +Clone the [repository](https://github.com/input-output-hk/psg-cardano-wallet-api) |
| 35 | + |
| 36 | +To build and publish the project to your local repository use |
| 37 | + |
| 38 | +`sbt publish` |
| 39 | + |
| 40 | +To build the command line executable jar use |
| 41 | + |
| 42 | +`sbt assembly` |
| 43 | + |
| 44 | +To build the command line executable jar skipping tests, use |
| 45 | + |
| 46 | +`sbt 'set test in assembly := {}' assembly` |
| 47 | + |
| 48 | +This will create a jar in the `target/scala-2.13` folder. |
| 49 | + |
| 50 | +#### Implementation |
| 51 | + |
| 52 | +The jar is part of an Akka streaming ecosystem and unsurprisingly uses [Akka Http](https://doc.akka.io/docs/akka-http/current/introduction.html) to make the http requests, |
| 53 | +it also uses [circe](https://circe.github.io/circe/) to marshal and unmarshal the json. |
| 54 | + |
| 55 | +### <a name="usage"></a>Usage |
| 56 | + |
| 57 | +The jar is published in Maven Central, the command line executable jar can be downloaded from the releases section |
| 58 | +of the [github repository](https://github.com/input-output-hk/psg-cardano-wallet-api) |
| 59 | + |
| 60 | + |
| 61 | +Before you can use this API you need a cardano wallet backend to contact, you can set one up following the instructions |
| 62 | +[here](https://github.com/input-output-hk/cardano-wallet). The docker setup is recommended. |
| 63 | + |
| 64 | +Alternatively, for 'tire kicking' purposes you may try `http://cardano-wallet-testnet.iog.solutions:8090/v2/` |
| 65 | + |
| 66 | +#### <a name="usagescala"></a>Scala |
| 67 | + |
| 68 | +Add the library to your dependencies |
| 69 | + |
| 70 | +`libraryDependencies += "iog.psg" %% "psg-cardano-wallet-api" % "0.2.0"` |
| 71 | + |
| 72 | +The api calls return a HttpRequest set up to the correct url and a mapper to take the entity result and |
| 73 | +map it from Json to the corresponding case classes. Using `networkInfo` as an example... |
| 74 | + |
| 75 | +``` |
| 76 | +import iog.psg.cardano.CardanoApi.CardanoApiOps._ |
| 77 | +import iog.psg.cardano.CardanoApi._ |
| 78 | +
|
| 79 | +implicit val as = ActorSystem("MyActorSystem") |
| 80 | +val baseUri = "http://localhost:8090/v2/" |
| 81 | +import as.dispatcher |
| 82 | +
|
| 83 | +val api = new CardanoApi(baseUri) |
| 84 | +
|
| 85 | +val networkInfoF: Future[CardanoApiResponse[NetworkInfo]] = |
| 86 | + api.networkInfo.toFuture.execute |
| 87 | +
|
| 88 | +val networkInfo: CardanoApiResponse[NetworkInfo] = |
| 89 | + api.networkInfo.toFuture.executeBlocking |
| 90 | +
|
| 91 | +networkInfo match { |
| 92 | + case Left(ErrorMessage(message, code)) => //do something |
| 93 | + case Right(netInfo: NetworkInfo) => // good! |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +#### <a name="usagejava"></a>Java |
| 98 | + |
| 99 | +First, add the library to your dependencies, |
| 100 | +``` |
| 101 | +<dependency> |
| 102 | + <groupId>iog.psg</groupId> |
| 103 | + <artifactId>psg-cardano-wallet-api_2.13</artifactId> |
| 104 | + <version>0.2.0</version> |
| 105 | +</dependency> |
| 106 | +``` |
| 107 | + |
| 108 | +Then, using `getWallet` as an example... |
| 109 | + |
| 110 | +``` |
| 111 | +import iog.psg.cardano.jpi.*; |
| 112 | +
|
| 113 | +ActorSystem as = ActorSystem.create(); |
| 114 | +ExecutorService es = Executors.newFixedThreadPool(10); |
| 115 | +CardanoApiBuilder builder = |
| 116 | + CardanoApiBuilder.create("http://localhost:8090/v2/") |
| 117 | + .withActorSystem(as) |
| 118 | + .withExecutorService(es); |
| 119 | +
|
| 120 | +CardanoApi api = builder.build(); |
| 121 | +
|
| 122 | +String walletId = "<PUT WALLET ID HERE>"; |
| 123 | +CardanoApiCodec.Wallet wallet = |
| 124 | + api.getWallet(walletId).toCompletableFuture().get(); |
| 125 | +
|
| 126 | +``` |
| 127 | + |
| 128 | +#### <a name="cmdline"></a>Command Line |
| 129 | + |
| 130 | +To see the usage instructions, use |
| 131 | + |
| 132 | +`java -jar psg-cardano-wallet-api-assembly-x.x.x-SNAPSHOT.jar` |
| 133 | + |
| 134 | +For example, to see the [network information](https://input-output-hk.github.io/cardano-wallet/api/edge/#tag/Network) use |
| 135 | + |
| 136 | +`java -jar psg-cardano-wallet-api-assembly-x.x.x-SNAPSHOT.jar -baseUrl http://localhost:8090/v2/ -netInfo` |
| 137 | + |
| 138 | +#### <a name="examples"></a> Examples |
| 139 | + |
| 140 | +The best place to find working examples is in the [test](https://github.com/input-output-hk/psg-cardano-wallet-api/tree/develop/src/test) folder |
| 141 | + |
| 142 | +#### <a name="issues"></a> Issues |
| 143 | + |
| 144 | +This release does *not* cover the entire cardano-wallet API, it focuses on getting the shelley core functionality into the hands of developers, if you need another call covered please log |
| 145 | +an [issue (or make a PR!)](https://github.com/input-output-hk/psg-cardano-wallet-api/issues) |
0 commit comments