Created
March 18, 2018 22:58
-
-
Save argenisleon/46ce70b16cbe9b6ebe4613bb1a2148ed to your computer and use it in GitHub Desktop.
Prueba de concepto de Bigchain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var orm = require('bigchaindb-orm'); | |
| const driver = require('bigchaindb-driver') | |
| const bip39 = require('bip39') | |
| var express = require('express'); | |
| var router = express.Router(); | |
| /* GET home page. */ | |
| router.get('/', function(req, res, next) { | |
| res.render('index', { title: 'Express'}); | |
| }); | |
| module.exports = router; | |
| // connect to BigchainDB | |
| // testnet using argenisleon@gmail.com account | |
| const bdbOrm = new orm( | |
| "https://test.bigchaindb.com/api/v1/", | |
| { | |
| app_id: "85473aa3", | |
| app_key: "d9020f1367123b65ce60e37db97a1806" | |
| } | |
| ) | |
| // define(<model name>,<additional information>) | |
| // <model name>: represents the name of model you want to store | |
| // <additional inf.>: any information you want to pass about the model (can be string or object) | |
| // note: cannot be changed once set! | |
| bdbOrm.define("crabModel", "https://schema.org/v1/crab") | |
| // create a public and private key for Alice | |
| //const aliceKeypair = new driver.Ed25519Keypair() | |
| const aliceKeypair = new driver.Ed25519Keypair(bip39.mnemonicToSeed('seedPhrase').slice(0,32)) | |
| // Create | |
| // Quizas debe guardarse el id de la trasaccion en la base de datos? | |
| bdbOrm.crabModel | |
| .create({ | |
| keypair: aliceKeypair, | |
| data: { | |
| // user_id : esto tenemos que explorarlo un poco mas ya que si borramos el | |
| // id del usuario de la base de datos se va a perder la referencia | |
| user_id : "argenisleon", | |
| authot: 'Argenis Leon', | |
| hash_image: 'Este debe ser el hash de la imagen', | |
| lat: '1.2131', | |
| lng:'3.1426', | |
| place:'', | |
| date:'' | |
| url_image: 'aqui debe ir el hash de IPFS, de entrada al server' | |
| } | |
| }) | |
| .then(crab => { | |
| // crab is an object with all data & functions | |
| // crab.id equals the id of the asset | |
| // crab.data is latest version | |
| // crab.transactionHistory gives the full history | |
| console.log(crab) | |
| }) | |
| //retrieve : id : 59adb0ac637bfd4e72638f91f551df19b9b20ca2cd61713d209e61ce8e0897bb | |
| console.log('Retrieve') | |
| bdbOrm.crabModel | |
| .retrieve('59adb0ac637bfd4e72638f91f551df19b9b20ca2cd61713d209e61ce8e0897bb') | |
| .then(crabs => { | |
| // crabs is an array of crabModel | |
| console.log(crabs.map(crab => crab.data)) | |
| }) | |
| // update our retrieved crab | |
| /*crab.append( | |
| { | |
| toPublicKey: aliceKeypair.publicKey, | |
| keypair: aliceKeypair, | |
| data: { color: 'red' } | |
| }) | |
| .then(updatedCrab => { | |
| // updatedCrab contains the last (unspent) state | |
| // of our crab so any actions | |
| // need to be done to updatedCrab | |
| console.log(updatedCrab.data) | |
| }) | |
| // burn our retrieved crab | |
| crab.burn( | |
| { | |
| keypair: aliceKeypair | |
| }) | |
| .then(burnedCrab => { | |
| // crab is now tagged as "burned", | |
| // the new publicKey is randomized | |
| // and the corresponding privateKey "lost" | |
| console.log(burnedCrab.transactionHistory.reverse() | |
| .map(tx => tx.data)) | |
| })*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment