GithubHelp home page GithubHelp logo

hymersm / openchain-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from openchain/openchain-js

0.0 1.0 0.0 226 KB

JavaScript Openchain client library for Node.js and the browser

Home Page: https://www.openchain.org/

License: Apache License 2.0

JavaScript 100.00%

openchain-js's Introduction

Openchain is an open source distributed ledger technology. It is suited for organizations wishing to issue and manage digital assets in a robust, secure and scalable way. Visit openchain.org for more information.

The full documentation for Openchain is available at docs.openchain.org.

This module is a NodeJS client library to use in conjunction with the Openchain server.

Getting started

Node.js

Install the module through NPM:

$ npm install openchain

Import the module:

var openchain = require("openchain");

In the browser

Install the module through Bower:

$ bower install openchain

Reference the scripts:

<script src="bower_components/bitcore-lib/bitcore-lib.min.js"></script>
<script src="bower_components/openchain/dist/openchain.min.js"></script>

Import the module:

var openchain = require("openchain");

Modules

The openchain module exports the following objects:

  • ApiClient: A class wrapping the Openchain API calls.
  • Schema: A set of classes (Schema.Record, Schema.Mutation and Schema.Transaction) that can be used for serialization and deserialization of transactions and mutations.
  • TransactionBuilder: A class facilitating the construction, signature and submission of transactions to an Openchain instance.
  • LedgerPath: A class representing a path within the Openchain structure.
  • RecordKey: A class representing a record key.
  • encoding: A submodule that contains methods that can be used for encoding and decoding integers and string to/from a ByteBuffer object.
  • MutationSigner: A class that can be used to sign a mutation.
  • ByteBuffer: A buffer of raw bytes.
  • Long: A class for representing a 64 bit integer value.

Code samples

Query the balance of an account

This code queries an Openchain server for the balance of the account represented by the path /p2pkh/Xat6UaXpQE9Dxv6rLtxY1peBkzC1SQDiEX/ for the asset represented by the path /asset/p2pkh/XcDCGPMtdrKxodQ4soFyYfDmr78gTvJ9jN/.

var openchain = require("openchain");
        
var client = new openchain.ApiClient("http://localhost:8080/");

client.getAccountRecord(
    // Account path
    "/p2pkh/Xat6UaXpQE9Dxv6rLtxY1peBkzC1SQDiEX/",
    // Asset path
    "/asset/p2pkh/XcDCGPMtdrKxodQ4soFyYfDmr78gTvJ9jN/")
.then(function (result) {
    console.log("Balance: " + result.balance.toString());
});

Submit a transaction

This code submits a transaction that transfers 100 units of an asset from an issuance account (e.g. /asset/p2pkh/Xat6UaXpQE9Dxv6rLtxY1peBkzC1SQDiEX/) to a normal wallet account (e.g. /p2pkh/Xat6UaXpQE9Dxv6rLtxY1peBkzC1SQDiEX/).

var openchain = require("openchain");
var bitcore = require("bitcore-lib");

var seed = "0123456789abcdef0123456789abcdef";

// Load a private key from a seed
var privateKey = bitcore.HDPrivateKey.fromSeed(seed, "openchain");
var address = privateKey.publicKey.toAddress();

// Calculate the accounts corresponding to the private key
var issuancePath = "/asset/p2pkh/" + address + "/";
var assetPath = issuancePath;
var walletPath = "/p2pkh/" + address + "/";

console.log("Issuance path: " + issuancePath);
console.log("Wallet path: " + walletPath);

// Create an Openchain client and signer
var client = new openchain.ApiClient("http://localhost:8080/");
var signer = new openchain.MutationSigner(privateKey);

// Initialize the client
client.initialize()
.then(function () {
    // Create a new transaction builder
    return new openchain.TransactionBuilder(client)
        // Add the key to the transaction builder
        .addSigningKey(signer)
        // Add some metadata to the transaction
        .setMetadata({ "memo": "Issued through NodeJS" })
        // Take 100 units of the asset from the issuance path
        .updateAccountRecord(issuancePath, assetPath, -100);
})
.then(function (transactionBuilder) {
    // Add 100 units of the asset to the target wallet path
    return transactionBuilder.updateAccountRecord(walletPath, assetPath, 100);
})
.then(function (transactionBuilder) {
    // Submit the transaction
    return transactionBuilder.submit();
})
.then(function (result) { console.log(result); });

Store data on the chain

This code submits a transaction recording a piece of arbitrary data (the storedData variable) into the chain. The data may be anything: arbitrary text, JSON data, XML data or even binary data.

Asset definition records, ACL records and goto records use this approach.

var openchain = require("openchain");
var bitcore = require("bitcore-lib");

var seed = "0123456789abcdef0123456789abcdef";

// Load a private key from a seed
var privateKey = bitcore.HDPrivateKey.fromSeed(seed, "openchain");
var address = privateKey.publicKey.toAddress();

// Calculate the accounts corresponding to the private key
var dataPath = "/asset/p2pkh/" + address + "/metadata/";
var recordName = "datarecord";
var storedData = "This is the data to store in the chain";

console.log("Account path: " + dataPath);
console.log("Record name: " + recordName);

// Create an Openchain client and signer
var client = new openchain.ApiClient("http://localhost:8080/");
var signer = new openchain.MutationSigner(privateKey);

// Initialize the client
client.initialize()
.then(function () {
    // Retrieve the record being modified
    return client.getDataRecord(dataPath, recordName)
})
.then(function (dataRecord) {
    // Encode the data into a ByteBuffer
    var newValue = openchain.encoding.encodeString(storedData);

    // Create a new transaction builder
    return new openchain.TransactionBuilder(client)
        // Add the key to the transaction builder
        .addSigningKey(signer)
        // Modify the record
        .addRecord(dataRecord.key, newValue, dataRecord.version)
        // Submit the transaction
        .submit();
})
.then(function (result) { console.log(result); });

Other use

The Openchain JavaScript client library is also used by the Openchain web-wallet available on GitHub.

License

Copyright 2015 Coinprism, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

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.