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 = ;
In the browser
Install the module through Bower:
$ bower install openchain
Reference the scripts:
Import the module:
var 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
andSchema.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 aByteBuffer
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 = ;var client = "http://localhost:8080/";client;
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 = ;var bitcore = ;var seed = "0123456789abcdef0123456789abcdef";// Load a private key from a seedvar privateKey = bitcoreHDPrivateKey;var address = privateKeypublicKey;// Calculate the accounts corresponding to the private keyvar issuancePath = "/asset/p2pkh/" + address + "/";var assetPath = issuancePath;var walletPath = "/p2pkh/" + address + "/";console;console;// Create an Openchain client and signervar client = "http://localhost:8080/";var signer = privateKey;// Initialize the clientclient;
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 = ;var bitcore = ;var seed = "0123456789abcdef0123456789abcdef";// Load a private key from a seedvar privateKey = bitcoreHDPrivateKey;var address = privateKeypublicKey;// Calculate the accounts corresponding to the private keyvar dataPath = "/asset/p2pkh/" + address + "/metadata/";var recordName = "datarecord";var storedData = "This is the data to store in the chain";console;console;// Create an Openchain client and signervar client = "http://localhost:8080/";var signer = privateKey;// Initialize the clientclient;
Connect to the WebSocket endpoint
Openchain exposes the transaction stream via a WebSocket. The following code snippet will connect to an endpoint and pull the transactions from the Openchain server.
var WebSocket = ;var openchain = ;var ByteBuffer = ByteBuffer;var bitcore = ;// The URL of the WebSocket endpointvar ws = "ws://localhost:5000/stream";// Format the raw transaction into readable text{var buffer = ByteBuffer;var transaction = openchainSchemaTransaction;var mutation = openchainSchemaMutation;var transactionBuffer = buffer;var transactionHash = bitcorecryptoHash;var mutationBuffer = transactionmutation;var mutationHash = bitcorecryptoHash;return JSON;}ws;// Process every incoming transactionws;
Use the following command to install the required dependencies:
$ npm install ws openchain bitcore-lib
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.