Git Product home page Git Product logo

planter's Introduction

planter

Create Metanet Nodes on Bitcoin SV

planter is a simple library for fetching and creating Metanet nodes on the Bitcoin SV blockchain.

code

Setup

npm i planter

Include planter in your project

import { Planter } from "planter";
<script src="https://unpkg.com/bsv/bsv.min.js"></script>
<script src="https://unpkg.com/planter/dist/planter.min.js"></script>

Be sure to include the bsv library as well when using the web version.

Usage

const planter = new Planter();

This will generate a wallet for you which will be used to derive node addresses and sign transactions. You can use an existing wallet by passing an extended private Key inside of the config object.

const planter = new Planter({
  xprivKey:
    "xprv9s21ZrQH143K3eQCpBqZiuLgNSFPAfkqimfqyDxJ6HAaVUqWWJ4vz7eZdhgkR66jD1a2BtQEXbYjjbfVXWhxz7g4sNujBt6cnAoJrdfLkHh"
});

Accepted config options are

  • xprivKey: string - An extended private Key from which the wallet and nodes are generated.
  • feeb: number - Fee per byte. Default is 1.4
  • nodeInterface - The instance of an implementation of a NodeInterface. Implementations exist in src/node-interfaces/.

By default, the Bitindex API is used for pushing transactions and fetching UTXOs. An Implementation for the newer MatterCloud API exists as well. New MatterCloud instances can be passed an API key, alternatively a new one is automatically requested.

import { Mattercloud } from "planter/lib/node-interfaces/mattercloud";

const planter = new Planter({
  nodeInterface: new Mattercloud({
    apiKey: "198t2pusaKhaqHSRsfQBtfyE2XR8Xe7Wsd"
  })
});

Funding can be provided by depositing BSV to the associated address.

planter.fundingAddress;

Creating nodes

await planter.createNode(options);

These additional options can be passed:

  • data: string[] - Array of data to include in OP_RETURN
  • parentTxID: string - For creating child nodes.
  • parentKeyPath: string - Can be passed when parentTxID is also passed to override keyPath of parent node.
  • keyPath: string - For setting the keypath manually. Default is m/0.
  • safe: boolean - Use OP_FALSE for scripts. Default is true.
  • includeKeyPath: boolean - Write keyPath information to OP_RETURN. Defaults to true. Can be deactivated to manage keyPaths locally.

Successfully creating nodes returns an object that contains the new nodes address, id, txid and used keyPath.

Traversing the Metanet

await planter.findAllNodes();

This will query all nodes owned by the Planter instance.

planter is built on top of TreeHugger and exposes its API for querying and traversing metanet nodes. See TreeHuggers Github page for details.

planter also exposes TreeHugger directly for general node querying.

import { TreeHugger } from "planter";

const node = await TreeHugger.findNodeByTxid(txid);

Queries

await planter.findSingleNode(query);
await planter.findAllNodes(query);

await planter.findNodeById(id);
await planter.findNodeByTxid(txid);
await planter.findNodesByAddress(address);
await planter.findNodesByParentId(id);
await planter.findNodeAndDescendants(id);

Relative traversal

await node.root();
await node.parent();
await node.ancestors();
await node.siblings();
await node.children();
await node.descendants();
await node.selfAndAncestors();
await node.selfAndSiblings();
await node.selfAndChildren();
await node.selfAndDescendants();

Creating child nodes and updates

await node.createChild(planter, options);
await node.createUpdate(planter, options);

The same options as before are accepted. Additionally, the Planter instance that should be used has to be passed.

Node properties

node.keyPath; // extracts keyPath out of OP_RETURN if it exists. Rerturns undefined otherwise

// Properties inherited from Treehugger
node.id; // Metanet node id
node.txid; // Transaction id
node.address; // Metanet node address

node.isRoot;
node.isChild;
node.isLeaf;

node.tx; // Planaria tx object

node.inputs; // Shortcut to node.tx.in
node.outputs; // Shortcut to node.tx.out
node.opReturn; // Shortcut to the OP_RETURN output object

Under the hood

planter randomly generates the keypaths used to derive node addresses to avoid accidental reuse and writes them onto the OP_RETURN data right after the metanet protocol keywords.

planter's People

Contributors

merlinb avatar torusjkl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

planter's Issues

safe: boolean should default to true

With the Genesis upgrade the default of the boolean should be set to true.
Otherwise the OP_RETURN script will be interpreted as a hash puzzle.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.