|
| 1 | +# JavaScript quickstart for BigchainDB |
| 2 | + |
| 3 | +> :bangbang: High chance of :fire: and :rage: ahead if you expect this to be production-ready |
| 4 | +
|
| 5 | +Some naive helpers to get you on your way to making some transactions :boom:, if you'd like to use |
| 6 | +JS with [BigchainDB](https://github.com/bigchaindb/bigchaindb). |
| 7 | + |
| 8 | +Aimed to support usage in browsers or node, but like every piece of :poop:, it might not. Use at |
| 9 | +your own risk :rocket:. At least I can tell you it's ES6, so you'll probably need a babel here and a |
| 10 | +bundler there, of which I expect you'll know quite well ([otherwise, go check out js-reactor :wink:](https://github.com/bigchaindb/js-reactor)). |
| 11 | + |
| 12 | +## Getting started |
| 13 | + |
| 14 | +Srs, just read through [index.js](./index.js) and see if you can make any sense of it. |
| 15 | + |
| 16 | +> :warning: You might want to check out the [current problems](#current-problems-that-block-usage) |
| 17 | + before commiting too far. |
| 18 | + |
| 19 | +The expected flow for making transactions: |
| 20 | + |
| 21 | +1. Go get yourself some keypairs! Just make a `new Keypair()` (or a whole bunch of them, nobody's |
| 22 | + counting :sunglasses:). |
| 23 | +1. Go get yourself a condition! `makeEd25519Condition()` should do the trick :sparkles:. |
| 24 | +1. Go get a fulfillment (don't worry about the *why*)! `makeEd25519Fulfillment()` no sweat :muscle:. |
| 25 | +1. (**Optional**) You've got everyting you need, except for an asset. Maybe define one (any |
| 26 | + JSON-serializable object will do). |
| 27 | +1. Time to get on the rocket ship, baby. `makeCreateTransaction()` your way to lifelong glory and |
| 28 | + fame :clap:! |
| 29 | +1. Ok, now you've got a transaction, but we need you to *sign* (`signTransaction()`) it cause, you |
| 30 | + know... cryptography and `¯\_(ツ)_/¯`. |
| 31 | +1. Alright, sick dude, you've *finally* got everything you need to `POST` to a server. Phew |
| 32 | + :sweat_drops:. Go `fetch()` your way to business, start:point_up:life4evar! |
| 33 | + |
| 34 | +... |
| 35 | + |
| 36 | +Alright, alright, so you've made a couple transactions. Now what? Do I hear you saying |
| 37 | +"<sub>Transfer them??</sub>" No problem, brotha, I gotcha covered :neckbeard:. |
| 38 | + |
| 39 | +1. Go get some more conditions and fulfillments, making sure you create fulfillments to *fulfill* a |
| 40 | + previous transaction's condition (maybe you wanna go check out [this](https://docs.bigchaindb.com/projects/server/en/latest/data-models/crypto-conditions.html) |
| 41 | + and [this](https://docs.bigchaindb.com/projects/py-driver/en/latest/usage.html#asset-transfer) |
| 42 | + and [this](https://tools.ietf.org/html/draft-thomas-crypto-conditions-01) if you're as confused |
| 43 | + as I think you are). |
| 44 | +1. Go make a transfer transaction, using the transaction you want to *spend* in |
| 45 | + `makeTransferTransaction()` :v:. |
| 46 | +1. Sign that transaction with `signTransaction()`! |
| 47 | +1. `POST` to the server, and watch the :dollar:s drop, man. |
| 48 | + |
| 49 | +## Current Problems That Block Usage |
| 50 | + |
| 51 | +A number of things I have no idea how to solve :fearful:: |
| 52 | + |
| 53 | +### SHA256 in JS doesn't match up with Python's |
| 54 | + |
| 55 | +JavaScript: |
| 56 | + |
| 57 | +```js |
| 58 | +tx = '{"operation":"CREATE","transaction":{"asset":{"data":{},"divisible":false,"id":"abe4cedb-bc8f-4115-a5e8-e3d225115770","refillable":false,"updatable":false},"conditions":[{"amount":1,"cid":0,"condition":{"cid":0,"details":{"bitmask":32,"public_key":"CGofQAC6xdhATnwbQU1Nj3QALTve2nErHT2RCxjqa6Yk","signature":null,"type":"fulfillment","type_id":4},"owners_after":["CGofQAC6xdhATnwbQU1Nj3QALTve2nErHT2RCxjqa6Yk"],"uri":"cc:4:20:p30HfwAxgDwv2iIGrQWU7y48OqxZgzq8dHH0kru9_I0:96"}}],"fulfillments":[{"fid":0,"fulfillment":null,"input":null,"owners_before":["CGofQAC6xdhATnwbQU1Nj3QALTve2nErHT2RCxjqa6Yk"]}],"metadata":null,"operation":null},"version":1}', 'utf8'; |
| 59 | + |
| 60 | +sha256 = crypto.createHash('sha256').update(tx).digest('hex'); |
| 61 | + |
| 62 | +// 6d7315a5185bd5a5e1ce027132ea0eb597fe2da61bff091cb94902bc8e997d03 |
| 63 | +``` |
| 64 | + |
| 65 | +Python: |
| 66 | + |
| 67 | +```py |
| 68 | +tx = '{"operation":"CREATE","transaction":{"asset":{"data":{},"divisible":false,"id":"abe4cedb-bc8f-4115-a5e8-e3d225115770","refillable":false,"updatable":false},"conditions":[{"amount":1,"cid":0,"condition":{"cid":0,"details":{"bitmask":32,"public_key":"CGofQAC6xdhATnwbQU1Nj3QALTve2nErHT2RCxjqa6Yk","signature":null,"type":"fulfillment","type_id":4},"owners_after":["CGofQAC6xdhATnwbQU1Nj3QALTve2nErHT2RCxjqa6Yk"],"uri":"cc:4:20:p30HfwAxgDwv2iIGrQWU7y48OqxZgzq8dHH0kru9_I0:96"}}],"fulfillments":[{"fid":0,"fulfillment":null,"input":null,"owners_before":["CGofQAC6xdhATnwbQU1Nj3QALTve2nErHT2RCxjqa6Yk"]}],"metadata":null,"operation":null},"version":1}', 'utf8'; |
| 69 | + |
| 70 | +sha256 = sha3.sha3_256(tx.encode()).hexdigest() |
| 71 | + |
| 72 | +# e6221b8ce703df98cd8385cfb3ebed7bc481f002725b24b7ac3aa4a47b6483a4 |
| 73 | +``` |
| 74 | + |
| 75 | +????? |
| 76 | + |
| 77 | +### tweetnacl-js doesn't provide 32-bit Ed25519 private keys |
| 78 | + |
| 79 | +For some reason, tweetnacl-js only generates [64-byte private keys](https://github.com/dchest/tweetnacl-js/blob/master/nacl.js#L876) |
| 80 | +for Ed25519 signature schemes while [five-bells-ledger only accepts 32-byte private keys](https://github.com/interledgerjs/five-bells-condition/blob/master/src/types/ed25519.js#L89). |
| 81 | +I guess you'll have to use another Ed25519 package????? |
0 commit comments