|
1 | 1 | # Offchain |
2 | 2 |
|
3 | | -Like crosschain, but offchain. |
| 3 | +Offchain is a server that consolidates API keys in a central, secure location, and presents |
| 4 | +a universal API that can be used to manage funds. This is intended for making withdrawals, |
| 5 | +sub-account transfers, and deposits. Trading is out of scope. |
4 | 6 |
|
5 | | -## Addresses |
| 7 | +Being able to withdraw funds directly from an exchange is a very sensitive operation. If you |
| 8 | +withdraw to the wrong address, those funds could be lost forever. This necessitates a separate |
| 9 | +application to take 'custody' of withdrawal-permissioned API keys. |
6 | 10 |
|
7 | | -For our testing, we want addresses to add the following "crosschain" addresses |
8 | | -to exchange allowlists. If there is a universal address option, we should use that. |
| 11 | +### Features |
9 | 12 |
|
10 | | -API keys should either be read only, or have full read + trade + withdraw permissions. |
| 13 | +- Simple configuration |
| 14 | +- Single binary |
| 15 | +- Rich exchange support and incredibly lightweight framework to easily add more |
| 16 | +- Strong authentication using ed25519 based [http-signatures](https://datatracker.ietf.org/doc/html/rfc9421). |
| 17 | +- Stateless |
| 18 | +- Universal asset symbology (based on Cordial Systems asset registry) |
| 19 | +- Can store exchange API keys in popular secret managers (vault, gcp, aws, etc). |
| 20 | +- Ability exchange exchange operations on CLI. |
| 21 | +- [Stable OpenAPI API](https://cordialapis.stoplight.io/docs/Exchange/2gnp0107q21eh-exchange) |
11 | 22 |
|
| 23 | +# Usage |
| 24 | + |
| 25 | +First, configure API keys for supported exchanges. |
| 26 | + |
| 27 | +```yaml |
| 28 | +# config.yaml |
| 29 | +offchain: |
| 30 | + # setup API keys for the exchanges |
| 31 | + exchanges: |
| 32 | + binance: |
| 33 | + # load from your favorite secret manager |
| 34 | + api_key: "gcp:your_gcp_project,API_KEY_NAME" |
| 35 | + secret_key: "gcp:your_gcp_project,API_SECRET_NAME" |
| 36 | + # include any sub-accounts |
| 37 | + subaccounts: |
| 38 | + - id: "offchain1@example.com" |
| 39 | + api_key: "gcp:your_gcp_project,SUB1_API_KEY_NAME" |
| 40 | + secret_key: "gcp:your_gcp_project,SUB1_API_KEY_SECRET_NAME" |
| 41 | + # ... |
| 42 | +``` |
| 43 | + |
| 44 | +API keys can be loaded from env, file, or from you favorite secret manager (see `oc secret --help`). |
| 45 | + |
| 46 | +Second, generate a ed25519 key to authenticate to the `offchain` server. [HTTP Signatures](https://datatracker.ietf.org/doc/html/rfc9421) |
| 47 | +are used. |
| 48 | + |
| 49 | +You can generate a client-side key like so (private key will be written to disk). |
| 50 | + |
| 51 | +```bash |
| 52 | +oc keys generate mykey |
| 53 | +# client-keys/mykey |
| 54 | +# e7a205bbe21184f4f6cd72e7ba659566d96b8aea00e49b9967328b0109f9c706 |
| 55 | +``` |
| 56 | + |
| 57 | +Now update your server configuration again with the public key. |
| 58 | + |
| 59 | +```yaml |
| 60 | +offchain: |
| 61 | + server: |
| 62 | + public_keys: |
| 63 | + - id: "mykey" |
| 64 | + key: "abf9649d7a0a7534cde49f12de47effd601e60a2258e51b5a257af9ef78e901f" |
12 | 65 | ``` |
13 | | -SOL 8Rub84DA2L6BH2FVKzVBxD7jp1i6o1BtrCunm51hQcg2 |
14 | | -EVM 0x3fa26f0dc74cfa8a56cb9d5f94245524d0888277 |
15 | | -APTOS 0x5249a0f1ccb427e6595343ef001ec18765fd325beb70fbea0a9c25807167e60d |
| 66 | +
|
| 67 | +Start the server. |
| 68 | +
|
| 69 | +```bash |
| 70 | +oc start --config ./config.yaml -v |
16 | 71 | ``` |
17 | 72 |
|
18 | | -Ideally tokens like USDC and USDT should be able to be used on the addresses above. |
| 73 | +Now make requests against it. |
| 74 | + |
| 75 | +```bash |
| 76 | +# Lookup main account balances |
| 77 | +oc api balances --exchange binance --sign-with mykey |
| 78 | + |
| 79 | +# Lookup balance of a sub-account |
| 80 | +oc api balances --subaccount offchain1@example.com --exchange binance --sign-with mykey |
| 81 | + |
| 82 | +# Make sub-account transfer from main to sub-account |
| 83 | +oc api --exchange binance transfer --to offchain1@example.com --symbol USDC --amount 3 --sign-with mykey |
| 84 | +# From sub-account to main |
| 85 | +oc api --exchange binance transfer --from offchain1@example.com --symbol USDC --amount 2 --sign-with mykey |
| 86 | + |
| 87 | +# Make a withdrawal from main account |
| 88 | +oc api --exchange binance withdraw --to "<your-solana-address>" --network SOL --symbol USDC --sign-with mykey |
| 89 | + |
| 90 | +# Look at withdrawal history |
| 91 | +oc api --exchange binance history --sign-with mykey |
| 92 | +``` |
| 93 | + |
| 94 | +# Policy |
| 95 | + |
| 96 | +To further enhance the security, policies should be built on top of `offchain`. For example, you should check that |
| 97 | +a withdrawal address is approved before signing a request to `offchain`. |
| 98 | + |
| 99 | +[Cordial Treasury](https://cordialsystems.com/) integrates with `offchain` and includes rich Transfer policies. It is default deny, |
| 100 | +and allows you to easily build allow-lists, notional transfer limits, and approval/quorom conditions. |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +Cordial Treasury will enforce policies, then sign requests to offchain using an MPC key. Both Cordial Treasury and offchain are non-custodial, self-hosted products. |
| 105 | + |
| 106 | +# Supported Exchanges |
| 107 | + |
| 108 | +Exchange clients are lightweight, pure go implementations. It's very easy to add support for new exchanges. |
| 109 | + |
| 110 | +- Backpack |
| 111 | +- Binance |
| 112 | +- Binance US |
| 113 | +- Bybit |
| 114 | +- Okx |
| 115 | + |
| 116 | +# API Reference |
| 117 | + |
| 118 | +See the [API reference](https://cordialapis.stoplight.io/docs/Exchange/2gnp0107q21eh-exchange). |
| 119 | + |
| 120 | +For http-authorization, you can look at or use the simple [http-signature package](./pkg/httpsignature/). |
| 121 | + |
| 122 | +# Crosschain |
| 123 | + |
| 124 | +We maintain a similar library, [crosschain](https://github.com/cordialsys/crosschain). It's like offchain, |
| 125 | +but solely for blockchains. You can locally generate addresses and make transfers on many different blockchains (as well as manage staking). |
| 126 | + |
| 127 | +You could use both `oc` and `xc` to fully rebalance and/or take custody of your portfolio across all different exchanges |
| 128 | +and blockchain networks. |
| 129 | + |
| 130 | +# Roadmap |
| 131 | + |
| 132 | +### Universal asset symbols |
| 133 | + |
| 134 | +We are still building out the universal asset integration. If you have interest in this, reach out to us at https://cordialsystems.com/contact |
| 135 | +and we can escalate. |
0 commit comments