Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colu-948 burn #1

Merged
merged 7 commits into from Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions .travis.yml
@@ -0,0 +1,13 @@
language: node_js
node_js:
- iojs
- '0.10'
- '0.12'
- '4.4.6'
before_script:
- npm install -g istanbul
- npm install -g mocha
- npm install -g coveralls
after_script:
- npm run coverage
- npm run coveralls
126 changes: 124 additions & 2 deletions README.md
@@ -1,2 +1,124 @@
# Burn-Payment-Encoder
Provides the encode/decode functions between a Colored Coins burn-transaction payment object to buffer
# Burn-Payment-Encoder
[![Build Status](https://travis-ci.org/Colored-Coins/Burn-Payment-Encoder.svg?branch=COLU-948_burn)](https://travis-ci.org/Colored-Coins/Burn-Payment-Encoder)
[![Coverage Status](https://coveralls.io/repos/github/Colored-Coins/Burn-Payment-Encoder/badge.svg?branch=COLU-948_burn)](https://coveralls.io/github/Colored-Coins/Burn-Payment-Encoder?branch=COLU-948_burn)
[![npm version](https://badge.fury.io/js/cc-burn-payment-encoder.svg)](http://badge.fury.io/js/cc-burn-payment-encoder)
[![Slack Status](http://slack.coloredcoins.org/badge.svg)](http://slack.coloredcoins.org)

[![Standard - JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

Payment-Encoder provides the encode/decode functions between a Colored Coins burn-transaction payment Object to buffer

### Installation

```sh
$ npm install cc-burn-payment-encoder
```


### Encode

Params:

`paymentObject` - A standard Colored Coins payment object with the following properties:

```js
{
// Skip input after reading asset
skip: "Boolean"

// Range or fixed value output (valid only if burn is false or undefined)
range: "Boolean"

// percent or fixed amount
percent: "Boolean"

// Output to send asset to - max value is 30 if range is false and 8191 if true (valid only if burn is false or undefined)
output: "Number"

// Total amount of units to send
amount: "Number"

// Should this payment be interpreted as an execution of "burn" (valid only if output value and range are undefined)
burn: "Boolean"
}

```

Returns a new Buffer holding the encoded payment.

##### Example:

```js
var paymentEncode = require('cc-burn-payment-encoder')
var paymentObject = {
skip: false,
range: false,
percent: true,
output: 1,
amount: 321321321
}

var code = paymentEncode.encode(paymentObject)

console.log(code) // Will print: <Buffer 21 80 99 37 cb 48>
```
Alternatively, you can encode a "burn" payment:
```js
var paymentObject = {
skip: false,
percent: false,
amount: 13,
burn: true
}

var code = paymentEncode.encode(paymentObject)

console.log(code) // Will print: <Buffer 1f 0d>
```

### Decode

Params:

- consume - takes a consumable buffer (You can use [buffer-consumer] like in the example to create one)

Returns a Colored Coins payment Object

##### Example:

```js
var paymentEncode = require('cc-burn-payment-encoder')
var consumer = require('buffer-consumer')

var decode = paymentEncode.decode(consumer(code))
var codeBuffer = new Buffer([0x82,0x76,0x0e,0x1b,0x48])

console.log(paymentEncode.decode(consumer(codeBuffer)))
// Will print:
// {
// skip: false,
// range: false,
// percent: true,
// output: 1,
// amount: 321321321
// }
```

### Testing

In order to test you need to install [mocha] globaly on your machine

```sh
$ cd /"module-path"/cc-burn-payment-encoder
$ mocha
```


License
----

MIT


[mocha]:https://www.npmjs.com/package/mocha
[buffer-consumer]:https://www.npmjs.com/package/buffer-consumer
22 changes: 22 additions & 0 deletions package.json
@@ -0,0 +1,22 @@
{
"name": "cc-burn-payment-encoder",
"version": "1.0.0",
"description": "The encoding/decoding functions for the colored-coins burn payment object scheme",
"main": "paymentEncoder.js",
"scripts": {
"test": "mocha",
"coverage": "istanbul cover _mocha -- -R spec",
"coveralls": "cat ./coverage/lcov.info | coveralls"
},
"keywords": [
"colored coins",
"bitcoin",
"blockchain"
],
"author": "oleiba",
"license": "MIT",
"dependencies": {
"cc-payment-encoder": "^1.0.0",
"clone": "^2.0.0"
}
}
66 changes: 66 additions & 0 deletions paymentEncoder.js
@@ -0,0 +1,66 @@
var basePaymentEncoder = require('cc-payment-encoder')
var clone = require('clone')

var BURN_OUTPUT = 0x1f

module.exports = {
// isBurn - is this payment as part of a burn transaction
encode: function (paymentObject) {
if (typeof paymentObject.output === 'undefined' && !paymentObject.burn) {
throw new Error('Needs output value or burn flag')
}
if (typeof paymentObject.output !== 'undefined' && paymentObject.burn) {
throw new Error('Received both burn and output')
}
if (typeof paymentObject.range !== 'undefined' && paymentObject.burn) {
throw new Error('Received both burn and range')
}
if (!paymentObject.range && paymentObject.output === BURN_OUTPUT) {
throw new Error('Received range and output values reserved to represent burn (to indicate burn use burn flag)')
}

if (paymentObject.burn) {
paymentObject = clone(paymentObject)
paymentObject.output = BURN_OUTPUT
paymentObject.range = false
delete paymentObject.burn
}

return basePaymentEncoder.encode(paymentObject)
},

// isBurn - is this payment as part of a burn transaction
decode: function (consume) {
var ans = basePaymentEncoder.decode(consume)
var burn = !ans.range && (ans.output === BURN_OUTPUT)
if (burn) {
ans.burn = true
delete ans.output
delete ans.range
}
return ans
},

encodeBulk: function (paymentsArray, isBurn) {
var payments = new Buffer(0)
var amountOfPayments = paymentsArray.length
for (var i = 0; i < amountOfPayments; i++) {
var payment = paymentsArray[i]
var paymentCode = this.encode(payment, isBurn)
payments = Buffer.concat([payments, paymentCode])
}
return payments
},

decodeBulk: function (consume, paymentsArray, isBurn) {
paymentsArray = paymentsArray || []
while (true) {
try {
paymentsArray.push(this.decode(consume, isBurn))
this.decodeBulk(consume, paymentsArray)
} catch (e) {
return paymentsArray
}
}
}
}