github.com/niluplatform/go-nilu@v1.7.4-0.20200912082737-a0cb0776d52c/cmd/clef/tutorial.md (about)

     1  ## Initializing the signer
     2  
     3  First, initialize the master seed.
     4  
     5  ```text
     6  #./signer init
     7  
     8  WARNING!
     9  
    10  The signer is alpha software, and not yet publically released. This software has _not_ been audited, and there
    11  are no guarantees about the workings of this software. It may contain severe flaws. You should not use this software
    12  unless you agree to take full responsibility for doing so, and know what you are doing.
    13  
    14  TLDR; THIS IS NOT PRODUCTION-READY SOFTWARE!
    15  
    16  
    17  Enter 'ok' to proceed:
    18  >ok
    19  A master seed has been generated into /home/martin/.signer/secrets.dat
    20  
    21  This is required to be able to store credentials, such as :
    22  * Passwords for keystores (used by rule engine)
    23  * Storage for javascript rules
    24  * Hash of rule-file
    25  
    26  You should treat that file with utmost secrecy, and make a backup of it.
    27  NOTE: This file does not contain your accounts. Those need to be backed up separately!
    28  ```
    29  
    30  (for readability purposes, we'll remove the WARNING printout in the rest of this document)
    31  
    32  ## Creating rules
    33  
    34  Now, you can create a rule-file.
    35  
    36  ```javascript
    37  function ApproveListing(){
    38      return "Approve"
    39  }
    40  ```
    41  Get the `sha256` hash....
    42  ```text
    43  #sha256sum rules.js
    44  6c21d1737429d6d4f2e55146da0797782f3c0a0355227f19d702df377c165d72  rules.js
    45  ```
    46  ...And then `attest` the file:
    47  ```text
    48  #./signer attest 6c21d1737429d6d4f2e55146da0797782f3c0a0355227f19d702df377c165d72
    49  
    50  INFO [02-21|12:14:38] Ruleset attestation updated              sha256=6c21d1737429d6d4f2e55146da0797782f3c0a0355227f19d702df377c165d72
    51  ```
    52  At this point, we then start the signer with the rule-file:
    53  
    54  ```text
    55  #./signer --rules rules.json
    56  
    57  INFO [02-21|12:15:18] Using CLI as UI-channel
    58  INFO [02-21|12:15:18] Loaded 4byte db                          signatures=5509 file=./4byte.json
    59  INFO [02-21|12:15:18] Could not load rulefile, rules not enabled file=rulefile
    60  DEBUG[02-21|12:15:18] FS scan times                            list=35.335µs set=5.536µs diff=5.073µs
    61  DEBUG[02-21|12:15:18] Ledger support enabled
    62  DEBUG[02-21|12:15:18] Trezor support enabled
    63  INFO [02-21|12:15:18] Audit logs configured                    file=audit.log
    64  INFO [02-21|12:15:18] HTTP endpoint opened                     url=http://localhost:8550
    65  ------- Signer info -------
    66  * extapi_http : http://localhost:8550
    67  * extapi_ipc : <nil>
    68  * extapi_version : 2.0.0
    69  * intapi_version : 1.2.0
    70  
    71  ```
    72  
    73  Any list-requests will now be auto-approved by our rule-file.
    74  
    75  ## Under the hood
    76  
    77  While doing the operations above, these files have been created:
    78  
    79  ```text
    80  #ls -laR ~/.signer/
    81  /home/martin/.signer/:
    82  total 16
    83  drwx------  3 martin martin 4096 feb 21 12:14 .
    84  drwxr-xr-x 71 martin martin 4096 feb 21 12:12 ..
    85  drwx------  2 martin martin 4096 feb 21 12:14 43f73718397aa54d1b22
    86  -rwx------  1 martin martin  256 feb 21 12:12 secrets.dat
    87  
    88  /home/martin/.signer/43f73718397aa54d1b22:
    89  total 12
    90  drwx------ 2 martin martin 4096 feb 21 12:14 .
    91  drwx------ 3 martin martin 4096 feb 21 12:14 ..
    92  -rw------- 1 martin martin  159 feb 21 12:14 config.json
    93  
    94  #cat /home/martin/.signer/43f73718397aa54d1b22/config.json
    95  {"ruleset_sha256":{"iv":"6v4W4tfJxj3zZFbl","c":"6dt5RTDiTq93yh1qDEjpsat/tsKG7cb+vr3sza26IPL2fvsQ6ZoqFx++CPUa8yy6fD9Bbq41L01ehkKHTG3pOAeqTW6zc/+t0wv3AB6xPmU="}}
    96  
    97  ```
    98  
    99  In `~/.signer`, the `secrets.dat` file was created, containing the `master_seed`.
   100  The `master_seed` was then used to derive a few other things:
   101  
   102  - `vault_location` : in this case `43f73718397aa54d1b22` .
   103     - Thus, if you use a different `master_seed`, another `vault_location` will be used that does not conflict with each other.
   104     - Example: `signer --signersecret /path/to/afile ...`
   105  - `config.json` which is the encrypted key/value storage for configuration data, containing the key `ruleset_sha256`.
   106  
   107  
   108  ## Adding credentials
   109  
   110  In order to make more useful rules; sign transactions, the signer needs access to the passwords needed to unlock keystores.
   111  
   112  ```text
   113  #./signer addpw 0x694267f14675d7e1b9494fd8d72fefe1755710fa test
   114  
   115  INFO [02-21|13:43:21] Credential store updated                 key=0x694267f14675d7e1b9494fd8d72fefe1755710fa
   116  ```
   117  ## More advanced rules
   118  
   119  Now let's update the rules to make use of credentials
   120  
   121  ```javascript
   122  function ApproveListing(){
   123      return "Approve"
   124  }
   125  function ApproveSignData(r){
   126      if( r.address.toLowerCase() == "0x694267f14675d7e1b9494fd8d72fefe1755710fa")
   127      {
   128          if(r.message.indexOf("bazonk") >= 0){
   129              return "Approve"
   130          }
   131          return "Reject"
   132      }
   133      // Otherwise goes to manual processing
   134  }
   135  
   136  ```
   137  In this example,
   138  * any requests to sign data with the account `0x694...` will be
   139      * auto-approved if the message contains with `bazonk`,
   140      * and auto-rejected if it does not.
   141      * Any other signing-requests will be passed along for manual approve/reject.
   142  
   143  ..attest the new file
   144  ```text
   145  #sha256sum rules.js
   146  2a0cb661dacfc804b6e95d935d813fd17c0997a7170e4092ffbc34ca976acd9f  rules.js
   147  
   148  #./signer attest 2a0cb661dacfc804b6e95d935d813fd17c0997a7170e4092ffbc34ca976acd9f
   149  
   150  INFO [02-21|14:36:30] Ruleset attestation updated              sha256=2a0cb661dacfc804b6e95d935d813fd17c0997a7170e4092ffbc34ca976acd9f
   151  ```
   152  
   153  And start the signer:
   154  
   155  ```
   156  #./signer --rules rules.js
   157  
   158  INFO [02-21|14:41:56] Using CLI as UI-channel
   159  INFO [02-21|14:41:56] Loaded 4byte db                          signatures=5509 file=./4byte.json
   160  INFO [02-21|14:41:56] Rule engine configured                   file=rules.js
   161  DEBUG[02-21|14:41:56] FS scan times                            list=34.607µs set=4.509µs diff=4.87µs
   162  DEBUG[02-21|14:41:56] Ledger support enabled
   163  DEBUG[02-21|14:41:56] Trezor support enabled
   164  INFO [02-21|14:41:56] Audit logs configured                    file=audit.log
   165  INFO [02-21|14:41:56] HTTP endpoint opened                     url=http://localhost:8550
   166  ------- Signer info -------
   167  * extapi_version : 2.0.0
   168  * intapi_version : 1.2.0
   169  * extapi_http : http://localhost:8550
   170  * extapi_ipc : <nil>
   171  INFO [02-21|14:41:56] error occurred during execution          error="ReferenceError: 'OnSignerStartup' is not defined"
   172  ```
   173  And then test signing, once with `bazonk` and once without:
   174  
   175  ```
   176  #curl -H "Content-Type: application/json" -X POST --data "{\"jsonrpc\":\"2.0\",\"method\":\"account_sign\",\"params\":[\"0x694267f14675d7e1b9494fd8d72fefe1755710fa\",\"0x$(xxd -pu <<< '  bazonk baz gaz')\"],\"id\":67}" http://localhost:8550/
   177  {"jsonrpc":"2.0","id":67,"result":"0x93e6161840c3ae1efc26dc68dedab6e8fc233bb3fefa1b4645dbf6609b93dace160572ea4ab33240256bb6d3dadb60dcd9c515d6374d3cf614ee897408d41d541c"}
   178  
   179  #curl -H "Content-Type: application/json" -X POST --data "{\"jsonrpc\":\"2.0\",\"method\":\"account_sign\",\"params\":[\"0x694267f14675d7e1b9494fd8d72fefe1755710fa\",\"0x$(xxd -pu <<< '  bonk baz gaz')\"],\"id\":67}" http://localhost:8550/
   180  {"jsonrpc":"2.0","id":67,"error":{"code":-32000,"message":"Request denied"}}
   181  
   182  ```
   183  
   184  Meanwhile, in the signer output:
   185  ```text
   186  INFO [02-21|14:42:41] Op approved
   187  INFO [02-21|14:42:56] Op rejected
   188  ```
   189  
   190  The signer also stores all traffic over the external API in a log file. The last 4 lines shows the two requests and their responses:
   191  
   192  ```text
   193  #tail audit.log -n 4
   194  t=2018-02-21T14:42:41+0100 lvl=info msg=Sign       api=signer type=request  metadata="{\"remote\":\"127.0.0.1:49706\",\"local\":\"localhost:8550\",\"scheme\":\"HTTP/1.1\"}" addr="0x694267f14675d7e1b9494fd8d72fefe1755710fa [chksum INVALID]" data=202062617a6f6e6b2062617a2067617a0a
   195  t=2018-02-21T14:42:42+0100 lvl=info msg=Sign       api=signer type=response data=93e6161840c3ae1efc26dc68dedab6e8fc233bb3fefa1b4645dbf6609b93dace160572ea4ab33240256bb6d3dadb60dcd9c515d6374d3cf614ee897408d41d541c error=nil
   196  t=2018-02-21T14:42:56+0100 lvl=info msg=Sign       api=signer type=request  metadata="{\"remote\":\"127.0.0.1:49708\",\"local\":\"localhost:8550\",\"scheme\":\"HTTP/1.1\"}" addr="0x694267f14675d7e1b9494fd8d72fefe1755710fa [chksum INVALID]" data=2020626f6e6b2062617a2067617a0a
   197  t=2018-02-21T14:42:56+0100 lvl=info msg=Sign       api=signer type=response data=                                                                                                                                   error="Request denied"
   198  ```