Quantcast
Channel: Openchain – Coinprism Blog
Viewing all articles
Browse latest Browse all 4

Announcing the JavaScript SDK for Openchain

$
0
0

nodejs-2560x1440We are happy to announce the release of openchain-js, the JavaScript SDK for Openchain. It works both on Node.js and in the browser.

The library is available as an NPM and Bower package, and encapsulates all the logic necessary to work with Openchain, including transaction serialization in protocol buffers format and signing. The library relies on Bitcore for signing and key management given that Openchain uses the same cryptography as Bitcoin.

To install it through NPM (Node.js):

$ npm install openchain

If you want to install it through Bower:

$ bower install openchain

If you decide to use it in the browser, make sure to reference the scripts in your page:

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

It is easy to build and submit a transaction to openchain using the TransactionBuilder class. Here is a code sample issuing an asset on Openchain programatically:

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); });

For more code samples, check the GitHub repository. In particular, the last code sample will show you how to store arbitrary data securely in the chain in just a few lines of code.

Finally, we have “rewritten” the Openchain wallet to make use of that new library.


Viewing all articles
Browse latest Browse all 4

Trending Articles