mirror of
https://github.com/bigchaindb/site.git
synced 2024-11-22 01:36:55 +01:00
changes due new js driver
This commit is contained in:
parent
b61d5416f9
commit
20a7bc298a
@ -14,9 +14,9 @@ learn: >
|
||||
- How to make a `CREATE` transaction to digitally register an asset on BigchainDB
|
||||
|
||||
- How to create `TRANSFER` transactions to change the ownership of an asset in BigchainDB
|
||||
|
||||
|
||||
- How BigchainDB can be used to record dynamic parameters of an asset
|
||||
|
||||
|
||||
---
|
||||
|
||||
Hi there! Welcome to our first tutorial! For this tutorial, we assume that you are familiar with the BigchainDB primitives (assets, inputs, outputs, transactions etc.). If you are not, familiarize yourself with [Key concepts of BigchainDB](../key-concepts-of-bigchaindb/).
|
||||
@ -25,7 +25,7 @@ Hi there! Welcome to our first tutorial! For this tutorial, we assume that you a
|
||||
|
||||
We are moving towards an era where every physical object has a digital representation in a database. This can be in the form of a certificate, a simple entry in a database or another form of digital footprint. In the past, this used to be the paper trail associated with the purchase of a car, a painting or any other type of asset. Today, digital is slowly replacing analog in most aspects of our life. Thanks to advances in cryptography, we are reaching a point where even ownership claims of a specific object don't need to be a signed paper certificate anymore. This allows digitization to move to a new level. BigchainDB as a solution is suited perfectly to act as a digital asset registration and tracking tool.
|
||||
|
||||
Using the example of the digital registration of a famous painting you own, in this tutorial you will learn how to register an asset on BigchainDB and how to digitally transfer the ownership of this asset to someone else. The example is for illustrative purposes. For a real life application, there would be additional components that would need to be included.
|
||||
Using the example of the digital registration of a famous painting you own, in this tutorial you will learn how to register an asset on BigchainDB and how to digitally transfer the ownership of this asset to someone else. The example is for illustrative purposes. For a real life application, there would be additional components that would need to be included.
|
||||
|
||||
Let's get started!
|
||||
|
||||
@ -43,7 +43,7 @@ const alice = new BigchainDB.Ed25519Keypair(bip39.mnemonicToSeed('seedPhrase').s
|
||||
|
||||
# Digital registration of an asset on BigchainDB
|
||||
|
||||
Now, let's assume that Alice is extremely lucky and gets to acquire the famous painting "Las Meninas" by the Spanish painter Diego Velázquez at a fantastic price during an auction held by the Spanish museum "museo nacional del prado". Now, she wants to ensure that she can digitally certify that she is the owner of this painting. For this reason, she wants to register the painting as an asset on BigchainDB to have an immutable claim. This corresponds to a CREATE transaction in BigchainDB. Using her key pair, Alice can create an asset on BigchainDB. In our case, the asset will represent an object in real life, namely the painting "Las Meninas". This asset will live in BigchainDB forever and there is no possibility to delete it.
|
||||
Now, let's assume that Alice is extremely lucky and gets to acquire the famous painting "Las Meninas" by the Spanish painter Diego Velázquez at a fantastic price during an auction held by the Spanish museum "museo nacional del prado". Now, she wants to ensure that she can digitally certify that she is the owner of this painting. For this reason, she wants to register the painting as an asset on BigchainDB to have an immutable claim. This corresponds to a CREATE transaction in BigchainDB. Using her key pair, Alice can create an asset on BigchainDB. In our case, the asset will represent an object in real life, namely the painting "Las Meninas". This asset will live in BigchainDB forever and there is no possibility to delete it.
|
||||
|
||||
The first step required is the definition of the asset field that represents the painting. This field contains the data about the asset that is immutable. It has a JSON format:
|
||||
|
||||
@ -61,7 +61,7 @@ As a next step, you need to generate a `CREATE` transaction to link the defined
|
||||
```js
|
||||
function createPaint() {
|
||||
// Construct a transaction payload
|
||||
const txCreateCar = BigchainDB.Transaction.makeCreateTransaction(
|
||||
const txCreatePaint = BigchainDB.Transaction.makeCreateTransaction(
|
||||
// Asset field
|
||||
{
|
||||
...painting,
|
||||
@ -83,7 +83,7 @@ function createPaint() {
|
||||
alice.publicKey
|
||||
)
|
||||
// The owner of the painting signs the transaction
|
||||
const txSigned = BigchainDB.Transaction.signTransaction(txCreateCar,
|
||||
const txSigned = BigchainDB.Transaction.signTransaction(txCreatePaint,
|
||||
alice.privateKey)
|
||||
|
||||
// Send the transaction off to BigchainDB
|
||||
@ -105,7 +105,7 @@ Once a transaction ends up in a decided-valid block, it's "etched into stone". T
|
||||
# Transfer of an asset on BigchainDB
|
||||
|
||||
Now, let's assume Alice has sold her painting in a good deal to someone else and she wants to digitally reflect that transfer. In BigchainDB, this would correspond to a TRANSFER transaction. For this, you first need to create a new key pair for a new owner (
|
||||
(newOwner). For simplicity, this step is left out in the code below.
|
||||
(newOwner). For simplicity, this step is left out in the code below.
|
||||
|
||||
Now, before creating the transfer transaction, furthermore you need to search for the last transaction with the asset id of your painting, as you will transfer this specific last transaction:
|
||||
|
||||
@ -114,23 +114,28 @@ The `listTransactions` command of BigchainDB retrieves all of the create and tra
|
||||
Based on that, we can now create the transfer transaction:
|
||||
|
||||
```js
|
||||
function transferOnwership(txCreated, newOwner) {
|
||||
// Update the painting with a new owner
|
||||
// First, we query for the asset that we created
|
||||
function transferOwnership(txCreated, newOwner) {
|
||||
|
||||
const createTranfer = BigchainDB.Transaction.
|
||||
makeTransferTransaction(
|
||||
txCreated, {
|
||||
mileage: txCreated.metadata.mileage + mileageValue,
|
||||
units: 'km'
|
||||
}, [BigchainDB.Transaction.makeOutput(
|
||||
// The output index 0 is the one that is being spent
|
||||
[{
|
||||
tx: txCreated,
|
||||
output_index: 0
|
||||
}], [BigchainDB.Transaction.makeOutput(
|
||||
BigchainDB.Transaction.makeEd25519Condition(
|
||||
newOwner.publicKey))],
|
||||
0
|
||||
newOwner.publicKey))] {
|
||||
datetime: new Date().toString(),
|
||||
value: {
|
||||
value_eur: '30000000€',
|
||||
value_btc: '2100',
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
// Sign with the owner of the car as she was the creator of the car
|
||||
// Sign with the owner of the paint (Alice)
|
||||
const signedTransfer = BigchainDB.Transaction
|
||||
.signTransaction(createTranfer, carOwner.privateKey)
|
||||
.signTransaction(createTranfer, alice.privateKey)
|
||||
conn.postTransaction(signedTransfer)
|
||||
.then((signedTransfer) => conn
|
||||
.pollStatusAndFetchTransaction(signedTransfer.id))
|
||||
@ -141,9 +146,9 @@ function transferOnwership(txCreated, newOwner) {
|
||||
}
|
||||
```
|
||||
|
||||
Note again that in the output of this transfer transaction we have `newOwner.publicKey`. This shows that Alice has transferred the ownership of the Meninas to anybody else (newOwner). Furthermore, the input being spent is 0, as there is just one input. Additionally, note that the metadata field was used to update information about the painting (the price of the transaction, which increased to 30 mn. EUR etc.).
|
||||
Note again that in the output of this transfer transaction we have `newOwner.publicKey`. This shows that Alice has transferred the ownership of the Meninas to anybody else (newOwner). Furthermore, the input being spent is 0, as there is just one input. Additionally, note that the metadata field was used to update information about the painting (the price of the transaction, which increased to 30 mn. EUR etc.).
|
||||
|
||||
You have now updated your asset and it is now not anymore you who will be able to transfer the painting, because someone else is the owner.
|
||||
You have now updated your asset and it is now not anymore you who will be able to transfer the painting, because someone else is now the owner.
|
||||
|
||||
That's it, we have created a digital representation of a painting and transferred the ownership to another user.
|
||||
|
||||
|
@ -97,8 +97,8 @@ function transferTokens() {
|
||||
tx,
|
||||
// Metadata (optional)
|
||||
{
|
||||
tranferTo: 'john',
|
||||
tokensLeft: tokensLeft
|
||||
tranfe_to: 'john',
|
||||
tokens_left: tokensLeft
|
||||
},
|
||||
// Transaction output: Two outputs, because the whole input must be spent
|
||||
[BigchainDB.Transaction.makeOutput(
|
||||
@ -133,6 +133,45 @@ function transferTokens() {
|
||||
You have now transferred 200 tokens to the user John. You could repeat the same with multiple other users.
|
||||
With `listOutputs` using `false` as the second argument you retrieved all the outputs belonging to the user `tokenCreator`, that were not spent yet. There will just be one output that fulfills these characteristics, because when you transfer tokens to another user, you are spending this output and giving the ownership to the other user. Then, you queried for that transaction and made a transfer to John with it. Note however, that there is also a transaction back to `tokenCreator.publicKey`, as you need to 'give back change' due to BigchainDB's transaction model. It is designed in a way that all of the inputs have to be spent in a transaction. That means that if you send part of the `tokensLeft` (200 tokens) to John, you have to send the rest (9800 tokens) back to the `tokenCreator` to preserve that amount.
|
||||
|
||||
|
||||
Imagine you have received several transactions of tokens and you want to combine all of the quantities and transfer to you best friend. That is possible as well
|
||||
|
||||
```js
|
||||
const amountToSend = 200
|
||||
const bestFriend = new driver.Ed25519Keypair()
|
||||
|
||||
function combineTokens(transaction1, outputIndex1, transaction2, outputIndex2,
|
||||
totalTokens) {
|
||||
const combineTranfer = BigchainDB.Transaction.makeTransferTransaction(
|
||||
[{
|
||||
tx: transaction1,
|
||||
output_index: outputIndex1
|
||||
}, {
|
||||
tx: transaction2,
|
||||
output_index: outputIndex2
|
||||
}],
|
||||
// Output. Two outputs. The hole input must be spent
|
||||
[BigchainDB.Transaction.makeOutput(
|
||||
BigchainDB.Transaction.makeEd25519Condition(
|
||||
bestFriend.publicKey),
|
||||
(totalTokens).toString())],
|
||||
{
|
||||
transfer_to: 'my best friend'
|
||||
}
|
||||
)
|
||||
|
||||
// Sign the transaction with the newUser key
|
||||
const signedTransfer = BigchainDB.Transaction
|
||||
.signTransaction(combineTranfer, newUser.privateKey)
|
||||
|
||||
return conn.postTransaction(signedTransfer)
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
You just made a transfer transaction combining two different transactions into one output. The `totalToken` quantity is needed which is the sum of the tokens of the two outputs being spent. As you have seen before if this quantity is not correct, the transaction will fail, as you literally need to spend all of the outputs in a transaction.
|
||||
`transaction1` and `transaction2` can look like the transaction `createTranfer` that you did before, then the `outputIndex1` and `outputIndex2` would be `0`.
|
||||
|
||||
Note that in our example, the supply of your tokens was fixed and cannot be changed anymore after creation. So, you would need to clearly define for yourself, how many tokens you will need. However, BigchainDB does offer the option of refillable, divisible assets that allow for a more dynamic token supply. You can learn more about that [here](https://github.com/bigchaindb/bigchaindb/issues/1741).
|
||||
|
||||
That's it! Now you know, how divisible assets in BigchainDB can be used as a potential building block for token launches. Of course, in practice a token distribution event is much more complex and requires other important building blocks like smart contracts etc. But, this tutorial showed you how divisible assets can play a part of that.
|
||||
|
Loading…
Reference in New Issue
Block a user