2018-10-03 23:31:28 +03:00
# Grin Wallet + Library Design
2018-07-30 19:10:43 +03:00
![wallet design ](wallet-arch.png )
2018-10-03 23:31:28 +03:00
## High Level Wallet Design Overview
2018-07-30 19:10:43 +03:00
The current Grin `wallet` crate provides several layers of libraries, services, and traits that can be mixed, matched and reimplemented to support
2018-10-03 23:31:28 +03:00
various needs within the default Grin wallet as well as provide a set of useful library functions for 3rd-party implementors. At a very high level,
2018-07-30 19:10:43 +03:00
the code is organized into the following components (from highest-level to lowest):
2018-10-03 23:31:28 +03:00
* **Command Line Client** - The command line client invoked by `grin wallet [command]` , simply instantiates the other components below
2018-07-30 19:10:43 +03:00
and parses command line arguments as needed.
2018-10-03 23:31:28 +03:00
* **Web Wallet Client** - [Work In Progress] A web wallet client accessible from the local machine only. Current code can be viewed here:
https://github.com/mimblewimble/grin-web-wallet
2018-07-30 19:10:43 +03:00
* **Static File Server** - [TBD] A means of serving up the web wallet client above to the user (still under consideration)
* **libWallet** - A high level wallet library that provides functions for the default grin wallet. The functions in here can be somewhat
2018-10-03 23:31:28 +03:00
specific to how the grin wallet does things, but could still be reused by 3rd party implementors following the same basic principles as grin
does. Major functionality is split into:
2018-07-30 19:10:43 +03:00
* **Owner API** - An API that provides information that should only be viewable by the wallet owner
* **Foreign API** - An API to communicate with other wallets and external grin nodes
* **Service Controller** - A Controller that instantiates the above APIs (either locally or via web services)
* **Internal Functions** Helper functions to perform needed wallet tasks, such as selecting coins, updating wallet outputs with
results from a Grin node, etc.
* **libTx** - Library that provides lower-level transaction building, rangeproof and signing functions, highly-reusable by wallet implementors.
* **Wallet Traits** - A set of generic traits defined within libWallet and the `keychain` crate . A wallet implementation such as Grin's current
2018-10-03 23:31:28 +03:00
default only needs to implement these traits in order to provide a wallet:
2018-11-13 15:18:33 +03:00
* **WalletToNodeClient** - Defines communication between the wallet, a running grin node and/or other wallets
2018-07-30 19:10:43 +03:00
* **WalletBackend** - Defines the storage implementation of the wallet
* **KeyChain** - Defines key derivation operations
2018-10-03 23:31:28 +03:00
## Module-Specific Notes
2018-07-30 19:10:43 +03:00
A full API-Description for each of these parts is still TBD (and should be generated by rustdoc rather than repeated here). However a few design
notes on each module are worth mentioning here.
2018-10-03 23:31:28 +03:00
### Web Wallet Client / Static File Server
2018-07-30 19:10:43 +03:00
This component is not a 3rd-party hosted 'Web Wallet' , but a client meant to be run on the local machine only by the wallet owner. It should provide
a usable browser interface into the wallet, that should be functionally equivalent to using the command line but (hopefully) far easier to use.
It is currently not being included by a default grin build, although the required listener is currently being run by default. To build and test this
component, see instructions on the [project page ](https://github.com/mimblewimble/grin-web-wallet ). The 'Static File Server' is still under
discussion, and concerns how to provide the web-wallet to the user in a default Grin build.
2018-10-03 23:31:28 +03:00
### Owner API / Foreign API
2018-07-30 19:10:43 +03:00
The high-level wallet API has been split into two, to allow for different requirements on each. For instance, the Foreign API would listen on
an external-facing port, and therefore potentially has different security requirements from the Owner API, which can simply be bound to localhost
only.
2018-10-03 23:31:28 +03:00
### libTX
2018-07-30 19:10:43 +03:00
Transactions are built using the concept of a 'Slate', which is a data structure that gets passed around to all participants in a transaction,
with each appending their Inputs, Outputs or Signatures to it to build a completed wallet transaction. Although the current mode of operation in
the default client only supports single-user - single recipient, an arbitrary number of participants to a transaction is supported within libTX.
2018-10-03 23:31:28 +03:00
### Wallet Traits
2018-07-30 19:10:43 +03:00
In the current code, a Wallet implementation is just a combination of these three traits. The vast majority of functions within libwallet
and libTX have a signature similar to the following:
2018-10-03 23:31:28 +03:00
```rust
2018-07-30 19:10:43 +03:00
pub fn retrieve_outputs< T: ? Sized , C , K > (
2018-10-03 23:31:28 +03:00
!·wallet: & mut T,
!·show_spent: bool,
!·tx_id: Option< u32 > ,
2018-07-30 19:10:43 +03:00
) -> Result< Vec < OutputData > , Error>
2018-10-03 23:31:28 +03:00
where
!·T: WalletBackend< C , K > ,
2018-11-13 15:18:33 +03:00
!·C: WalletToNodeClient,
2018-07-30 19:10:43 +03:00
!·K: Keychain,
{
```
With `T` in this instance being a class that implements the `WalletBackend` trait, which is further parameterized with implementations of
2018-11-13 15:18:33 +03:00
`WalletToNodeClient` and `Keychain` .
2018-07-30 19:10:43 +03:00
2018-10-03 23:31:28 +03:00
There is currently only a single implementation of the Keychain trait within the Grin code, in the `keychain` crate exported as `ExtKeyChain` .
The `Keychain` trait makes several assumptions about the underlying implementation, particularly that it will adhere to a
2018-07-30 19:10:43 +03:00
[BIP-38 style ](https://github.com/bitcoin/bips/blob/master/bip-0038.mediawiki ) 'master key -> child key' model.
2018-11-13 15:18:33 +03:00
There are two implementations of `WalletToNodeClient` within the code, the main version being the `HTTPWalletToNodeClient` found within `wallet/src/client.rs` and
the seconds a test client that communicates with an in-process instance of a chain. The WalletToNodeClient isolates all network calls, so upgrading wallet
2018-07-30 19:10:43 +03:00
communication from the current simple http interaction to a more secure protocol (or allowing for many options) should be a simple
2018-11-13 15:18:33 +03:00
matter of dropping in different `WalletToNodeClient` implementations.
2018-07-30 19:10:43 +03:00
2018-10-03 23:31:28 +03:00
There are also two implementations of `WalletBackend` within the code at the base of the `wallet` crate. `LMDBBackend` found within
2018-07-30 19:10:43 +03:00
`wallet/src/lmdb_wallet.rs` is the main implementation, and is now used by all grin wallet commands. The earlier `FileWallet` still exists
within the code, however it is not invoked, and given there are no real advantages to running it over a DB implementation, development on it
2018-10-03 23:31:28 +03:00
has been dropped in favour of the LMDB implementation.