From de90fea0ac0e21c9a00b8bbfff2c334cc3521649 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 11 Jul 2018 17:27:44 +1000 Subject: [PATCH] payments: rename p2data to embed --- src/payments/embed.js | 56 +++++++++++++++++++++++ src/payments/index.js | 4 +- src/payments/p2data.js | 56 ----------------------- test/fixtures/{p2data.json => embed.json} | 4 +- test/integration/transactions.js | 4 +- test/payments.js | 2 +- 6 files changed, 63 insertions(+), 63 deletions(-) create mode 100644 src/payments/embed.js rename test/fixtures/{p2data.json => embed.json} (96%) diff --git a/src/payments/embed.js b/src/payments/embed.js new file mode 100644 index 0000000..c636c80 --- /dev/null +++ b/src/payments/embed.js @@ -0,0 +1,56 @@ +const lazy = require('./lazy') +const typef = require('typeforce') +const OPS = require('bitcoin-ops') + +const bscript = require('../script') +const BITCOIN_NETWORK = require('../networks').bitcoin + +function stacksEqual (a, b) { + if (a.length !== b.length) return false + + return a.every(function (x, i) { + return x.equals(b[i]) + }) +} + +// output: OP_RETURN ... +function p2data (a, opts) { + if ( + !a.data && + !a.output + ) throw new TypeError('Not enough data') + opts = opts || { validate: true } + + typef({ + network: typef.maybe(typef.Object), + output: typef.maybe(typef.Buffer), + data: typef.maybe(typef.arrayOf(typef.Buffer)) + }, a) + + const network = a.network || BITCOIN_NETWORK + const o = { network } + + lazy.prop(o, 'output', function () { + if (!a.data) return + return bscript.compile([OPS.OP_RETURN].concat(a.data)) + }) + lazy.prop(o, 'data', function () { + if (!a.output) return + return bscript.decompile(a.output).slice(1) + }) + + // extended validation + if (opts.validate) { + if (a.output) { + const chunks = bscript.decompile(a.output) + if (chunks[0] !== OPS.OP_RETURN) throw new TypeError('Output is invalid') + if (!chunks.slice(1).every(typef.Buffer)) throw new TypeError('Output is invalid') + + if (a.data && !stacksEqual(a.data, o.data)) throw new TypeError('Data mismatch') + } + } + + return Object.assign(o, a) +} + +module.exports = p2data diff --git a/src/payments/index.js b/src/payments/index.js index de3bec1..d445466 100644 --- a/src/payments/index.js +++ b/src/payments/index.js @@ -1,4 +1,4 @@ -const p2data = require('./p2data') +const embed = require('./embed') const p2ms = require('./p2ms') const p2pk = require('./p2pk') const p2pkh = require('./p2pkh') @@ -6,7 +6,7 @@ const p2sh = require('./p2sh') const p2wpkh = require('./p2wpkh') const p2wsh = require('./p2wsh') -module.exports = { p2data, p2ms, p2pk, p2pkh, p2sh, p2wpkh, p2wsh } +module.exports = { embed, p2ms, p2pk, p2pkh, p2sh, p2wpkh, p2wsh } // TODO // witness commitment diff --git a/src/payments/p2data.js b/src/payments/p2data.js index c636c80..e69de29 100644 --- a/src/payments/p2data.js +++ b/src/payments/p2data.js @@ -1,56 +0,0 @@ -const lazy = require('./lazy') -const typef = require('typeforce') -const OPS = require('bitcoin-ops') - -const bscript = require('../script') -const BITCOIN_NETWORK = require('../networks').bitcoin - -function stacksEqual (a, b) { - if (a.length !== b.length) return false - - return a.every(function (x, i) { - return x.equals(b[i]) - }) -} - -// output: OP_RETURN ... -function p2data (a, opts) { - if ( - !a.data && - !a.output - ) throw new TypeError('Not enough data') - opts = opts || { validate: true } - - typef({ - network: typef.maybe(typef.Object), - output: typef.maybe(typef.Buffer), - data: typef.maybe(typef.arrayOf(typef.Buffer)) - }, a) - - const network = a.network || BITCOIN_NETWORK - const o = { network } - - lazy.prop(o, 'output', function () { - if (!a.data) return - return bscript.compile([OPS.OP_RETURN].concat(a.data)) - }) - lazy.prop(o, 'data', function () { - if (!a.output) return - return bscript.decompile(a.output).slice(1) - }) - - // extended validation - if (opts.validate) { - if (a.output) { - const chunks = bscript.decompile(a.output) - if (chunks[0] !== OPS.OP_RETURN) throw new TypeError('Output is invalid') - if (!chunks.slice(1).every(typef.Buffer)) throw new TypeError('Output is invalid') - - if (a.data && !stacksEqual(a.data, o.data)) throw new TypeError('Data mismatch') - } - } - - return Object.assign(o, a) -} - -module.exports = p2data diff --git a/test/fixtures/p2data.json b/test/fixtures/embed.json similarity index 96% rename from test/fixtures/p2data.json rename to test/fixtures/embed.json index b0486cd..ccc0e70 100644 --- a/test/fixtures/p2data.json +++ b/test/fixtures/embed.json @@ -44,14 +44,14 @@ }, "details": [ { - "description": "p2data", + "description": "embed", "data": [ "a3b147dbe4a85579fc4b5a1811e76620560e07267e62b9a0d6858f9127735cadd82f67e06c24dbc4" ], "output": "OP_RETURN a3b147dbe4a85579fc4b5a1811e76620560e07267e62b9a0d6858f9127735cadd82f67e06c24dbc4" }, { - "description": "p2data", + "description": "embed", "data": [ "a3b147dbe4a85579fc4b5a1811e76620560e0726", "7e62b9a0d6858f9127735cadd82f67e06c24dbc4" diff --git a/test/integration/transactions.js b/test/integration/transactions.js index cec0839..5358461 100644 --- a/test/integration/transactions.js +++ b/test/integration/transactions.js @@ -91,9 +91,9 @@ describe('bitcoinjs-lib (transactions)', function () { const txb = new bitcoin.TransactionBuilder(regtest) const data = Buffer.from('bitcoinjs-lib', 'utf8') - const p2data = bitcoin.payments.p2data({ data: [data] }) + const embed = bitcoin.payments.embed({ data: [data] }) txb.addInput(unspent.txId, unspent.vout) - txb.addOutput(p2data.output, 1000) + txb.addOutput(embed.output, 1000) txb.addOutput(regtestUtils.RANDOM_ADDRESS, 1e5) txb.sign(0, keyPair) diff --git a/test/payments.js b/test/payments.js index 3645ea7..3af6699 100644 --- a/test/payments.js +++ b/test/payments.js @@ -3,7 +3,7 @@ const assert = require('assert') const u = require('./payments.utils') -;['p2data', 'p2ms', 'p2pk', 'p2pkh', 'p2sh', 'p2wpkh', 'p2wsh'].forEach(function (p) { +;['embed', 'p2ms', 'p2pk', 'p2pkh', 'p2sh', 'p2wpkh', 'p2wsh'].forEach(function (p) { describe(p, function () { const fn = require('../src/payments/' + p) const fixtures = require('./fixtures/' + p)