• Jump To … +
    app.js bitcoin.js blockchain.js chain.js cli.js collection.js fabric.js game.js heartbeat.js http.js index.js network.js oracle.js p2pkh.js relay.js service.js store.js swarm.js witness.js
  • oracle.js

  • ¶

    An example of an Oracle built with @fabric/core, a framework for building high-throughput distributed systems with Bitcoin.

  • ¶

    An Oracle offers a simple, self- contained Service for establishing and verifying claims made against an underlying trust anchor.

  • ¶

    When combined with an HTML browser, an Oracle can be used to manage long-running State for offline-first applications.

  • ¶

    Quick Start

    Run locally with node examples/oracle.js — use Node 12, a la nvm use 12 if you’re using NVM, or from nodejs.org if not!

  • ¶

    TODO: use bottom panel for inline execution (a la “Run this Code »”)

    Source

  • ¶

    First, let’s ensure strict mode is used to parse our code.

    'use strict';
  • ¶

    Import types from @fabric/core:

    const Oracle = require('@fabric/core/types/oracle');
  • ¶

    An example configuration object, encoded as JSON:

    {
      "name": "@examples/oracle",
      "description": "a simple Oracle example",
      "version": "0.1.0"
    }
    

    Configuration files are most commonly stored in config.json, but you can also use an existing package.json to pre-load an Oracle with some state.

    const config = require('./config');
    
    /**
     * An {@link Oracle} offers a simple, self-contained {@link Service} for Fabric-
     * capable agents.  The `main()` function allocates necessary resources, then
     * starts the service.
     */
    async function main () {
  • ¶

    Our primary objective is to create an Oracle, so we do that next by passing the config constant from earlier into the Fabric.Oracle constructor.

      const oracle = new Oracle(config);
  • ¶

    The oracle variable contains our Oracle, so now let’s define a Resource for it to manage.

  • ¶

    Resources

    An Oracle’s purpose is to provide a canonical reference for a list of Resources, which require both a name and a definition. Resources can be thought of as “typed collections”, with atomic operations such as POP and PUSH for managing their contents.

  • ¶

    Here, we define a Request as a resource with one field, input, which is both required and restricted to a maximum size of 2048 bytes.

      oracle.define('Request', {
        attributes: {
          input: { type: 'String', required: true, max: 2048 }
        }
      });
  • ¶

    Now that a Resource has been defined, start the Oracle.

      await oracle.start();
  • ¶

    Log some output.

      console.log('oracle started!');
      console.log('oracle:', oracle);
    }
  • ¶

    We’ve defined our program. Start the main process!

    module.exports = main();
  • ¶

    Fabric exposes a powerful API through @fabric/core, a standard library for building decentralized applications. You can install it now by using the npm install --save @fabric/core command, or use npm install FabricLabs/fabric#develop for bleeding-edge #beta testing.