Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ node_js:
- iojs
- '0.10'
- '0.12'
- '4.4.6'
before_script:
- npm install -g istanbul
- npm install -g mocha
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Transfer-Encoder
[![Build Status](https://travis-ci.org/Colored-Coins/Transfer-Encoder.svg?branch=master)](https://travis-ci.org/Colored-Coins/Transfer-Encoder) [![Coverage Status](https://coveralls.io/repos/Colored-Coins/Transfer-Encoder/badge.svg?branch=master)](https://coveralls.io/r/Colored-Coins/Transfer-Encoder?branch=master) [![npm version](https://badge.fury.io/js/cc-transfer-encoder.svg)](http://badge.fury.io/js/cc-transfer-encoder) [![npm version](http://slack.coloredcoins.org/badge.svg)](http://slack.coloredcoins.org)
[![Build Status](https://travis-ci.org/Colored-Coins/Transfer-Encoder.svg?branch=master)](https://travis-ci.org/Colored-Coins/Transfer-Encoder) [![Coverage Status](https://coveralls.io/repos/Colored-Coins/Transfer-Encoder/badge.svg?branch=master)](https://coveralls.io/r/Colored-Coins/Transfer-Encoder?branch=master) [![npm version](https://badge.fury.io/js/cc-transfer-encoder.svg)](http://badge.fury.io/js/cc-transfer-encoder) [![Slack Status](http://slack.coloredcoins.org/badge.svg)](http://slack.coloredcoins.org)

[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "The encoding/decoding functions for the colored-coins scheme transfer set of OP_CODES",
"main": "transferEncoder.js",
"dependencies": {
"cc-payment-encoder": "^1.0.0"
"cc-payment-encoder": "^1.0.0",
"cc-burn-payment-encoder": "^1.0.0"
},
"directories": {
"test": "test"
Expand Down
15 changes: 12 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,28 @@ describe('Colored-Coins transfer Decoding', function () {
console.log(result.codeBuffer.toString('hex'), result.leftover)
console.log(ccEncoding.decode(result.codeBuffer))

// check throws when pushing burn to a default transfer transaction
assert.throws(function () {
data.payments.push({skip: false, percent: false, amount: 7, burn: true})
ccEncoding.encode(data, 40)
}, /Needs output value/,
'Should Throw Error')

// now no error
data.type = 'burn'
result = ccEncoding.encode(data, 40)

delete data.allowMeta
data.payments = []
data.payments.push({skip: false, range: false, percent: true, output: 12, amount: 3213213})
result = ccEncoding.encode(data, 80)
result = ccEncoding.encode(data, 40)
console.log(result.codeBuffer.toString('hex'), result.leftover)
console.log(ccEncoding.decode(result.codeBuffer))
done()
})

})

describe('80 byte OP_RETURN', function () {

var code
var decoded
var torrentHash = new Buffer('46b7e0d000d69330ac1caa48c6559763828762e1', 'hex')
Expand Down
45 changes: 34 additions & 11 deletions transferEncoder.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
var OP_CODES = [
new Buffer([0x10]), // All Hashes in OP_RETURN - Pay-to-PubkeyHash
var TYPE_MASK = 0xf0
var TRANSFER_MASK = 0x10
var BURN_MASK = 0x20
var TRANSFER_OP_CODES = [
new Buffer([0x10]), // All Hashes in OP_RETURN
new Buffer([0x11]), // SHA2 in Pay-to-Script-Hash multi-sig output (1 out of 2)
new Buffer([0x12]), // All Hashes in Pay-to-Script-Hash multi-sig outputs (1 out of 3)
new Buffer([0x13]), // Low security transaction no SHA2 for torrent data. SHA1 is always inside OP_RETURN in this case.
new Buffer([0x14]), // Low security transaction no SHA2 for torrent data. SHA1 is always inside OP_RETURN in this case. also no rules inside the metadata (if there are any they will be in ignored)
new Buffer([0x15]) // No metadata or rules (no SHA1 or SHA2)
]
var BURN_OP_CODES = [
new Buffer([0x20]), // All Hashes in OP_RETURN
new Buffer([0x21]), // SHA2 in Pay-to-Script-Hash multi-sig output (1 out of 2)
new Buffer([0x22]), // All Hashes in Pay-to-Script-Hash multi-sig outputs (1 out of 3)
new Buffer([0x23]), // Low security transaction no SHA2 for torrent data. SHA1 is always inside OP_RETURN in this case.
new Buffer([0x24]), // Low security transaction no SHA2 for torrent data. SHA1 is always inside OP_RETURN in this case. also no rules inside the metadata (if there are any they will be in ignored)
new Buffer([0x25]) // No metadata or rules (no SHA1 or SHA2)
]

var paymentCodex = require('cc-payment-encoder')
var transferPaymentEncoder = require('cc-payment-encoder')
var burnPaymentEncoder = require('cc-burn-payment-encoder')

var consumer = function (buff) {
var curr = 0
Expand All @@ -28,11 +40,13 @@ module.exports = {
throw new Error('Missing Data')
}
var opcode
var OP_CODES = data.type === 'burn' ? BURN_OP_CODES : TRANSFER_OP_CODES
var paymentEncoder = data.type === 'burn' ? burnPaymentEncoder : transferPaymentEncoder
var hash = new Buffer(0)
var protocol = new Buffer(padLeadingZeros(data.protocol.toString(16), 2), 'hex')
var version = new Buffer([data.version])
var transferHeader = Buffer.concat([protocol, version])
var payments = paymentCodex.encodeBulk(data.payments)
var payments = paymentEncoder.encodeBulk(data.payments)
var issueByteSize = transferHeader.length + payments.length + 1

if (issueByteSize > byteSize) throw new Error('Data code is bigger then the allowed byte size')
Expand Down Expand Up @@ -70,26 +84,35 @@ module.exports = {
data.version = parseInt(consume(1).toString('hex'), 16)
data.multiSig = []
var opcode = consume(1)
if (opcode[0] === OP_CODES[0][0]) {
var paymentEncoder
if ((opcode[0] & TYPE_MASK) === TRANSFER_MASK) {
paymentEncoder = transferPaymentEncoder
} else if ((opcode[0] & TYPE_MASK) === BURN_MASK) {
paymentEncoder = burnPaymentEncoder
} else {
throw new Error('Unrecognized Code')
}

if (opcode[0] === TRANSFER_OP_CODES[0][0] || opcode[0] === BURN_OP_CODES[0][0]) {
data.torrentHash = consume(20)
data.sha2 = consume(32)
} else if (opcode[0] === OP_CODES[1][0]) {
} else if (opcode[0] === TRANSFER_OP_CODES[1][0] || opcode[0] === BURN_OP_CODES[1][0]) {
data.torrentHash = consume(20)
data.multiSig.push({'index': 1, 'hashType': 'sha2'})
} else if (opcode[0] === OP_CODES[2][0]) {
} else if (opcode[0] === TRANSFER_OP_CODES[2][0] || opcode[0] === BURN_OP_CODES[2][0]) {
data.multiSig.push({'index': 1, 'hashType': 'sha2'})
data.multiSig.push({'index': 2, 'hashType': 'torrentHash'})
} else if (opcode[0] === OP_CODES[3][0]) {
} else if (opcode[0] === TRANSFER_OP_CODES[3][0] || opcode[0] === BURN_OP_CODES[3][0]) {
data.torrentHash = consume(20)
data.noRules = false
} else if (opcode[0] === OP_CODES[4][0]) {
} else if (opcode[0] === TRANSFER_OP_CODES[4][0] || opcode[0] === BURN_OP_CODES[4][0]) {
data.torrentHash = consume(20)
data.noRules = true
} else if (opcode[0] === OP_CODES[5][0]) {
} else if (opcode[0] === TRANSFER_OP_CODES[5][0] || opcode[0] === BURN_OP_CODES[5][0]) {
} else {
throw new Error('Unrecognized Code')
}
data.payments = paymentCodex.decodeBulk(consume)
data.payments = paymentEncoder.decodeBulk(consume)

return data
}
Expand Down