From 79f1b30750edb8f8c52de374c00a97636c81a994 Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Wed, 2 Nov 2016 15:56:23 +0200 Subject: [PATCH 1/5] burn --- .travis.yml | 1 + test/test.js | 2 +- transferEncoder.js | 35 +++++++++++++++++++++++------------ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index b2ede09..7076239 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ node_js: - iojs - '0.10' - '0.12' +- '4.4.6' before_script: - npm install -g istanbul - npm install -g mocha diff --git a/test/test.js b/test/test.js index 9f9f6e0..a71bbd5 100644 --- a/test/test.js +++ b/test/test.js @@ -70,7 +70,7 @@ describe('Colored-Coins transfer Decoding', function () { data.payments.push({skip: true, range: false, percent: false, output: 2, amount: 3}) data.payments.push({skip: false, range: false, percent: false, output: 3, amount: 4}) data.payments.push({skip: true, range: false, percent: false, output: 4, amount: 5}) - data.payments.push({skip: false, range: false, percent: false, output: 5, amount: 6}) + data.payments.push({skip: false, percent: false, amount: 6, burn: true}) result = ccEncoding.encode(data, 40) console.log(result.codeBuffer.toString('hex'), result.leftover) diff --git a/transferEncoder.js b/transferEncoder.js index cc76e26..2dc48ae 100644 --- a/transferEncoder.js +++ b/transferEncoder.js @@ -1,11 +1,20 @@ -var OP_CODES = [ - new Buffer([0x10]), // All Hashes in OP_RETURN - Pay-to-PubkeyHash +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) + 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') @@ -28,6 +37,7 @@ module.exports = { throw new Error('Missing Data') } var opcode + var OP_CODES = data.type === 'burn' ? BURN_OP_CODES : TRANSFER_OP_CODES var hash = new Buffer(0) var protocol = new Buffer(padLeadingZeros(data.protocol.toString(16), 2), 'hex') var version = new Buffer([data.version]) @@ -70,22 +80,23 @@ module.exports = { data.version = parseInt(consume(1).toString('hex'), 16) data.multiSig = [] var opcode = consume(1) - if (opcode[0] === OP_CODES[0][0]) { + + 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') } From a6b8ed69b55d8c723d97922cab8cc43407c87673 Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Thu, 3 Nov 2016 14:50:54 +0200 Subject: [PATCH 2/5] cc-burn-payment-encoder@1.0.0 --- package.json | 3 ++- test/test.js | 17 +++++++++++++---- transferEncoder.js | 42 ++++++++++++++++++++++++++++-------------- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 00eb083..077bc79 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/test.js b/test/test.js index a71bbd5..3946c8c 100644 --- a/test/test.js +++ b/test/test.js @@ -70,25 +70,34 @@ describe('Colored-Coins transfer Decoding', function () { data.payments.push({skip: true, range: false, percent: false, output: 2, amount: 3}) data.payments.push({skip: false, range: false, percent: false, output: 3, amount: 4}) data.payments.push({skip: true, range: false, percent: false, output: 4, amount: 5}) - data.payments.push({skip: false, percent: false, amount: 6, burn: true}) + data.payments.push({skip: false, range: false, percent: false, output: 5, amount: 6}) result = ccEncoding.encode(data, 40) 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') diff --git a/transferEncoder.js b/transferEncoder.js index 2dc48ae..a732c3a 100644 --- a/transferEncoder.js +++ b/transferEncoder.js @@ -1,22 +1,25 @@ +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([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) + 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([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) + 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 @@ -38,11 +41,12 @@ module.exports = { } 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') @@ -80,6 +84,16 @@ module.exports = { data.version = parseInt(consume(1).toString('hex'), 16) data.multiSig = [] var opcode = consume(1) + var paymentEncoder + console.log('opcode[0] = 0x', opcode[0].toString(16)) + console.log('(opcode[0] & TYPE_MASK) = 0x', (opcode[0] & TYPE_MASK).toString(16)) + 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) @@ -100,7 +114,7 @@ module.exports = { } else { throw new Error('Unrecognized Code') } - data.payments = paymentCodex.decodeBulk(consume) + data.payments = paymentEncoder.decodeBulk(consume) return data } From 6b8f253e0ffec4f12a65a3097193dfebbea423e1 Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Thu, 3 Nov 2016 14:52:58 +0200 Subject: [PATCH 3/5] clean logs --- transferEncoder.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/transferEncoder.js b/transferEncoder.js index a732c3a..169206e 100644 --- a/transferEncoder.js +++ b/transferEncoder.js @@ -85,8 +85,6 @@ module.exports = { data.multiSig = [] var opcode = consume(1) var paymentEncoder - console.log('opcode[0] = 0x', opcode[0].toString(16)) - console.log('(opcode[0] & TYPE_MASK) = 0x', (opcode[0] & TYPE_MASK).toString(16)) if ((opcode[0] & TYPE_MASK) === TRANSFER_MASK) { paymentEncoder = transferPaymentEncoder } else if ((opcode[0] & TYPE_MASK) === BURN_MASK) { From 089fcb39a3e8a9d6a02ce3ef2d9d94a54e3b00bd Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Sun, 6 Nov 2016 18:08:45 +0200 Subject: [PATCH 4/5] cc-burn-payment-encoder#COLU-948_burn --- README.md | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a844d75..008d4a1 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/package.json b/package.json index 077bc79..32efff7 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "transferEncoder.js", "dependencies": { "cc-payment-encoder": "^1.0.0", - "cc-burn-payment-encoder": "^1.0.0" + "cc-burn-payment-encoder": "git+https://github.com/Colored-Coins/Burn-Payment-Encoder.git#COLU-948_burn" }, "directories": { "test": "test" From c4b247702fbf5c6d1c3e780133aeb9bdc95bc12e Mon Sep 17 00:00:00 2001 From: Oded Leiba Date: Tue, 8 Nov 2016 17:25:54 +0200 Subject: [PATCH 5/5] COLU-948_burn -> master --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 32efff7..077bc79 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "transferEncoder.js", "dependencies": { "cc-payment-encoder": "^1.0.0", - "cc-burn-payment-encoder": "git+https://github.com/Colored-Coins/Burn-Payment-Encoder.git#COLU-948_burn" + "cc-burn-payment-encoder": "^1.0.0" }, "directories": { "test": "test"