Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
That's just the start. Currently only the "util" part of the IPLD format
spec is supported ((de)serialization and getting the CID).
  • Loading branch information
vmx committed Jan 5, 2018
0 parents commit 525d003
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
docs
package-lock.json
yarn.lock
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

dist
29 changes: 29 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

test
32 changes: 32 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
sudo: false
language: node_js

matrix:
include:
- node_js: 6
env: CXX=g++-4.8
- node_js: 8
env: CXX=g++-4.8
# - node_js: stable
# env: CXX=g++-4.8

script:
- npm run lint
- npm run test
- npm run coverage

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

after_success:
- npm run coverage-publish

addons:
firefox: 'latest'
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 IPFS

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# IPLD for Bitcoin

[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/project-IPLD-blue.svg?style=flat-square)](http://github.com/ipld/ipld)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Coverage](https://coveralls.io/repos/github/ipld/js-ipld-bitcoin/badge.svg?branch=master)](https://coveralls.io/github/ipld/js-ipld-bitcoin?branch=master)
[![Travis](https://travis-ci.org/ipld/js-ipld-bitcoin.svg?branch=master)](https://travis-ci.org/ipld/js-ipld-bitcoin)
[![Circle](https://circleci.com/gh/ipld/js-ipld-bitcoin.svg?style=svg)](https://circleci.com/gh/ipld/js-ipld-bitcoin)
[![](https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
[![](https://david-dm.org/ipld/js-ipld-bitcoin.svg?style=flat-square)](https://david-dm.org/ipld/js-ipld-bitcoin)
[![](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/Node.js-%3E%3D6.0.0-orange.svg?style=flat-square)

> JavaScript implementation of the [IPLD spec](https://github.com/ipfs/specs/tree/master/ipld).
## Table of Contents

- [Install](#install)
- [npm](#npm)
- [Use in Node.js](#use-in-nodejs)
- [Use in a browser with browserify, webpack or any other bundler](#use-in-a-browser-with-browserify-webpack-or-any-other-bundler)
- [Use in a browser Using a script tag](#use-in-a-browser-using-a-script-tag)
- [Usage](#usage)
- [Maintainers](#maintainers)
- [Contribute](#contribute)
- [License](#license)

## Install

### npm

```sh
> npm install ipld-bitcoin
```

### Use in Node.js

```JavaScript
const IpldBitcoin = require('ipld-bitcoin')
```

### Use in a browser with browserify, webpack or any other bundler

The code published to npm that gets loaded on require is in fact a ES5 transpiled version with the right shims added. This means that you can require it and use with your favourite bundler without having to adjust asset management process.

```JavaScript
var IpldBitcoin = require('ipld-bitcoin')
```

### Use in a browser Using a script tag

Loading this module through a script tag will make the `IpldBitcoint` obj available in the global namespace.

```html
<script src="https://unpkg.com/ipld-bitcoin/dist/index.min.js"></script>
<!-- OR -->
<script src="https://unpkg.com/ipld-bitcoin/dist/index.js"></script>
```

## Usage

```JavaScript
const IpldBitcoin = require('ipld-bitcoin')

// `bitcoinBlock` is some binary Bitcoin block
IpldBitcoin.util.deserialize(bitcoinBlock, (err, dagNode) => {
if (err) {
throw err
}
console.log(dagNode)
})
```

## Maintainers

[@vmx](https://github.com/vmx)

## Contribute

Feel free to join in. All welcome. Open an [issue](https://github.com/ipld/js-ipld-bitcoin/issues)!

Check out our [contributing document](https://github.com/ipld/ipld/blob/master/contributing.md) for more information on how we work, and about contributing in general. Please be aware that all interactions related to IPLD are subject to the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).

Small note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.

## License

[MIT](LICENSE) © 2018 IPFS
29 changes: 29 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
version: "{build}"

environment:
matrix:
- nodejs_version: "6"
- nodejs_version: "8"

matrix:
fast_finish: true

install:
# Install Node.js
- ps: Install-Product node $env:nodejs_version

# Upgrade npm
- npm install -g npm

# Output our current versions for debugging
- node --version
- npm --version

# Install our package dependencies
- npm install

test_script:
- npm run test:node

build: off
2 changes: 2 additions & 0 deletions ci/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
javascript()
15 changes: 15 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Warning: This file is automatically synced from https://github.com/ipfs/ci-sync so if you want to change it, please change it there and ask someone to sync all repositories.
machine:
node:
version: stable

dependencies:
pre:
- google-chrome --version
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
- sudo dpkg -i google-chrome.deb || true
- sudo apt-get update
- sudo apt-get install -f
- sudo apt-get install --only-upgrade lsb-base
- sudo dpkg -i google-chrome.deb
- google-chrome --version
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "ipld-bitcoin",
"version": "0.1.0",
"description": "JavaScript implementation of Bitcoin IPLD formats",
"main": "src/index.js",
"scripts": {
"test": "aegir test",
"test:browser": "aegir test --target browser",
"test:node": "aegir test --target node",
"lint": "aegir lint",
"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
"build": "aegir build",
"coverage": "aegir coverage",
"coverage-publish": "aegir coverage publish"
},
"pre-commit": [
"lint",
"test"
],
"repository": {
"type": "git",
"url": "git+https://github.com/ipld/js-ipld-btc.git"
},
"keywords": [
"IPFS"
],
"author": "vmx",
"license": "MIT",
"bugs": {
"url": "https://github.com/ipld/js-ipld-btc/issues"
},
"homepage": "https://github.com/ipld/js-ipld-btc#readme",
"dependencies": {
"bitcoinjs-lib": "^3.3.2",
"cids": "~0.5.2",
"hash.js": "^1.1.3",
"multihashes": "~0.4.12"
},
"devDependencies": {
"aegir": "^12.1.3",
"chai": "^4.1.2"
},
"contributors": []
}
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

exports.util = require('./util.js')
36 changes: 36 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

const BitcoinjsBlock = require('bitcoinjs-lib').Block
const CID = require('cids')
const multihashes = require('multihashes')
const sha256 = require('hash.js/lib/hash/sha/256');

const serialize = (dagNode, callback) => {
const binaryBlob = dagNode.toBuffer()
const err = null
callback(err, binaryBlob)
}

const deserialize = (binaryBlob, callback) => {
const dagNode = BitcoinjsBlock.fromBuffer(binaryBlob)
const err = null
callback(err, dagNode)
}

const cid = (dagNode, callback) => {
// Bitcoin double hashes
const firstHash = sha256().update(dagNode.toBuffer(true)).digest()
const headerHash = sha256().update(Buffer.from(firstHash)).digest()

const multihash = multihashes.encode(Buffer.from(headerHash), 'dbl-sha2-256')
const cidVersion = 1
const cid = new CID(cidVersion, 'bitcoin-block', multihash)
const err = null;
callback(err, cid)
}

module.exports = {
cid: cid,
deserialize: deserialize,
serialize: serialize
}
1 change: 1 addition & 0 deletions test/fixtures/block.hex

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-env mocha */
'use strict'

const loadFixture = require('aegir/fixtures')
const expect = require('chai').expect
const IpldBitcoin = require('../src/index')

describe('IPLD format util API', () => {
const fixtureBlockHex = loadFixture(__dirname, 'fixtures/block.hex')
const fixtureBlock = Buffer.from(fixtureBlockHex.toString(), 'hex')

it('should deserialize correctly', (done) => {
IpldBitcoin.util.deserialize(fixtureBlock, (err, dagNode) => {
expect(err).to.not.exist
expect(dagNode.version).to.equal(2);
expect(dagNode.prevHash.toString('hex')).to.equal(
'87d6242b27d248a9e145fe764a0bcef03a403883a2e4c8590200000000000000')
expect(dagNode.merkleRoot.toString('hex')).to.equal(
'11a5b9a70acebedbbf71ef8ca341e8a98cf279c49eee8f92e10a2227743b6aeb')
expect(dagNode.timestamp).to.equal(1386981279);
expect(dagNode.bits).to.equal(419740270);
expect(dagNode.nonce).to.equal(3159344128);
done()
})
})

it('should round-trip (de)serialization correctly', (done) => {
IpldBitcoin.util.deserialize(fixtureBlock, (err, dagNode) => {
IpldBitcoin.util.serialize(dagNode, (err, binaryBlob) => {
expect(err).to.not.exist
expect(binaryBlob.equals(fixtureBlock)).to.be.true
done()
})
})
})

it('should encode the CID correctly', (done) => {
IpldBitcoin.util.deserialize(fixtureBlock, (err, dagNode) => {
IpldBitcoin.util.cid(dagNode, (err, cid) => {
expect(err).to.not.exist
expect(cid.multihash.toString('hex')).to.equal(
'56203ec2c691d447b2fd0d6a94742345af1f351037dab1ab9e900200000000000000')
done()
})
})
})
})

0 comments on commit 525d003

Please sign in to comment.