1
0
mirror of https://github.com/bigchaindb/js-bigchaindb-driver.git synced 2024-12-27 23:27:50 +01:00

Basic usage examples added, not completed

This commit is contained in:
michielmulders 2017-08-28 16:56:12 +02:00 committed by Tim Daubenschütz
parent 6f6c21dacf
commit dfaf97e5ce
55 changed files with 217 additions and 3 deletions

0
.babelrc Normal file → Executable file
View File

0
.eslintignore Normal file → Executable file
View File

0
.eslintrc.json Normal file → Executable file
View File

0
.gitignore vendored Normal file → Executable file
View File

0
.npmignore Normal file → Executable file
View File

0
.travis.yml Normal file → Executable file
View File

0
API.md Normal file → Executable file
View File

0
CODE_OF_CONDUCT.md Normal file → Executable file
View File

0
CONTRIBUTING.rst Normal file → Executable file
View File

0
LICENSE Normal file → Executable file
View File

0
README.md Normal file → Executable file
View File

0
docs/Makefile Normal file → Executable file
View File

0
docs/README.md Normal file → Executable file
View File

0
docs/make.bat Normal file → Executable file
View File

0
docs/requirements.txt Normal file → Executable file
View File

0
docs/source/.conf.py.swp Normal file → Executable file
View File

0
docs/source/conf.py Normal file → Executable file
View File

1
docs/source/index.rst Normal file → Executable file
View File

@ -7,6 +7,7 @@ BigchainDB Javascript Driver Documentation
← Back to All BigchainDB Docs <https://bigchaindb.readthedocs.io/en/latest/index.html>
readme
quickstart
usage
Another header

11
docs/source/quickstart.rst Normal file → Executable file
View File

@ -1,4 +1,9 @@
Quickstart
==========
=========================
Quickstart / Installation
=========================
Quickstart comes here
Installation with package manager npm:
.. code-block:: bash
$ npm install bigchaindb-driver

0
docs/source/readme.rst Normal file → Executable file
View File

208
docs/source/usage.rst Executable file
View File

@ -0,0 +1,208 @@
====================
Basic Usage Examples
====================
For the examples on this page, we assume you've :doc:`installed the bigchaindb_driver JavaScript package <quickstart>`,
and you have determined the BigchainDB Root URL (issue: move this to general docs)
of the node or cluster you want to connect to.
This example guides you through creating and transferring an asset.
We walk through the code explaining its use, some pieces are left out
because they have no real use (e.g. defenition of global variable)
*Full working code* can be found at the bottom of this document.
Getting Started
---------------
We begin by creating an object of BigchainDB driver:
.. code-block:: js
// ES6 Browser
import * as driver from 'js-bigchaindb-driver';
// ES<<6 Browser
let driver = require('js-bigchaindb-driver');
// ES<<6 CommonJS / node
let driver = require('js-bigchaindb-driver/dist/node');
Next, we define a constant containing the API path.
.. code-block:: js
// http(s)://<bigchaindb-API-url>/ (e.g. http://localhost:9984/api/v1/)
const API_PATH = 'http://localhost:9984/api/v1/';
Cryptographic Identities Generation
-----------------------------------
Alice and Bob are represented by public/private key pairs. The private key is
used to sign transactions, meanwhile the public key is used to verify that a
signed transaction was indeed signed by the one who claims to be the signee.
.. code-block:: js
const alice = new driver.Ed25519Keypair();
const bob = new driver.Ed25519Keypair();
Digital Asset Definition
------------------------
As an example, lets consider the creation and transfer of a digital asset
that represents a bicycle:
.. code-block:: js
bicycle = {
'data': {
'bicycle': {
'serial_number': 'abcd1234',
'manufacturer': 'bkfab',
},
},
}
We'll suppose that the bike belongs to Alice, and that it eventually will be
transferred to Bob.
In general, you are free to define any JSON object you which to store for the
``'data'`` property
Metadata Definition (*optional*)
--------------------------------
You can `optionally` add metadata to a transaction. Any JSON object is accepted.
For example, the bicycle will be transferred on earth which is metadata:
.. code-block:: js
metadata = {'planet': 'earth'}
Asset Creation
--------------
We're now ready to create the digital asset. First, let's make a 'CREATE'
transaction:
.. code-block:: js
const txCreateAliceSimple = driver.Transaction.makeCreateTransaction(
bicycle,
metadata,
[ driver.Transaction.makeOutput(
driver.Transaction.makeEd25519Condition(alice.publicKey)
)],
alice.publicKey
);
The transaction now needs to be fulfilled by signing it with Alices
private key:
.. code-block:: js
const txCreateAliceSimpleSigned = driver.Transaction.signTransaction(txCreateAliceSimple, alice.privateKey);
And sent over to a BigchainDB node:
.. code-block:: js
driver.Connection.postTransaction(txCreateAliceSimpleSigned, API_PATH)
Notice the transaction ``id``:
.. code-block:: js
txid = txCreateAliceSimpleSigned.id
To check the status of the transaction:
.. code-block:: js
driver.Connection.getStatus(txCreateAliceSimpleSigned.id, API_PATH)
It is also possible to check the status every 0.5 seconds
with use of the transaction ``id``:
.. code-block:: js
return driver.Connection.pollStatusAndFetchTransaction(txCreateAliceSimpleSigned.id, API_PATH)
.. note:: It may take a small amount of time before a BigchainDB cluster
confirms a transaction as being valid.
Asset Transfer
--------------
Imagine some time goes by, during which Alice is happy with her bicycle, and
one day, she meets Bob, who is interested in acquiring her bicycle. The timing
is good for Alice as she had been wanting to get a new bicycle.
To transfer the bicycle (asset) to Bob, Alice must consume the transaction in
which the Bicycle asset was created.
Alice could retrieve the transaction:
.. code-block:: js
driver.Connection.getTransaction(txCreateAliceSimpleSigned.id)
First, let's prepare the transaction to be transferred.
.. code-block:: js
const txTransferBob = driver.Transaction.makeTransferTransaction(
txCreateAliceSimpleSigned,
{price: '100 euro'},
[ driver.Transaction.makeOutput(
driver.Transaction.makeEd25519Condition(bob.publicKey)
)],
0
);
The function ``makeTransferTransaction()`` needs following parameters:
- Unspent transaction: Previous transaction you have control over (i.e. can fulfill its Output Condition)
- Metadata for transaction (e.g. price of sold bike)
- Array of output objects to add to the transaction: Think of these as the recipients of the asset after the transaction. For `TRANSFER` transactions, this should usually just be a list of outputs wrapping Ed25519 conditions generated from the public keys of the recipients.
- Indices of the outputs in `unspent transaction` that this transaction fulfills.
Fulfill transaction by signing it with Alice's private key.
.. code-block:: js
txTransferBobSigned = driver.Transaction.signTransaction(txTransferBob, alice.privateKey);
And sent over to a BigchainDB node:
.. code-block:: js
return driver.Connection.postTransaction(txTransferBobSigned, API_PATH)
Bob is the new owner:
.. code-block:: js
Alice is the former owner:
.. code-block:: js
code
Recap: Asset Creation & Transfer
--------------------------------
.. code-block:: js
Other examples
--------------
TODO:
- Add lexer: https://stackoverflow.com/questions/4259105/which-sphinx-code-block-language-to-use-for-json

0
docs/upgrade-guides/0.3.0.md Normal file → Executable file
View File

0
media/repo-banner.sketch Normal file → Executable file
View File

0
media/repo-banner@2x.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 54 KiB

After

Width:  |  Height:  |  Size: 54 KiB

0
package.json Normal file → Executable file
View File

0
src/Ed25519Keypair.js Normal file → Executable file
View File

0
src/baseRequest.js Normal file → Executable file
View File

0
src/connection.js Normal file → Executable file
View File

0
src/format_text.js Normal file → Executable file
View File

0
src/index.js Normal file → Executable file
View File

0
src/request.js Normal file → Executable file
View File

0
src/sanitize.js Normal file → Executable file
View File

0
src/sha256Hash.js Normal file → Executable file
View File

0
src/stringify_as_query_param.js Normal file → Executable file
View File

0
src/transaction/hashTransaction.js Normal file → Executable file
View File

0
src/transaction/index.js Normal file → Executable file
View File

0
src/transaction/makeCreateTransaction.js Normal file → Executable file
View File

0
src/transaction/makeEd25519Condition.js Normal file → Executable file
View File

0
src/transaction/makeInputTemplate.js Normal file → Executable file
View File

0
src/transaction/makeOutput.js Normal file → Executable file
View File

0
src/transaction/makeSha256Condition.js Normal file → Executable file
View File

0
src/transaction/makeThresholdCondition.js Normal file → Executable file
View File

0
src/transaction/makeTransaction.js Normal file → Executable file
View File

0
src/transaction/makeTransferTransaction.js Normal file → Executable file
View File

View File

0
src/transaction/signTransaction.js Normal file → Executable file
View File

0
src/transaction/utils/ccJsonLoad.js Normal file → Executable file
View File

0
src/transaction/utils/ccJsonify.js Normal file → Executable file
View File

0
test/connection/test_connection.js Normal file → Executable file
View File

0
test/constants.js Normal file → Executable file
View File

0
test/integration/test_integration.js Normal file → Executable file
View File

0
test/transaction/test_cryptoconditions.js Normal file → Executable file
View File

0
test/transaction/test_transaction.js Normal file → Executable file
View File

0
webpack.config.js Normal file → Executable file
View File

0
yarn.lock Normal file → Executable file
View File