Browse Source

payments: rename p2data to embed

addLowRGrinding
Daniel Cousens 7 years ago
parent
commit
de90fea0ac
  1. 56
      src/payments/embed.js
  2. 4
      src/payments/index.js
  3. 56
      src/payments/p2data.js
  4. 4
      test/fixtures/embed.json
  5. 4
      test/integration/transactions.js
  6. 2
      test/payments.js

56
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

4
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

56
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

4
test/fixtures/p2data.json → 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"

4
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)

2
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)

Loading…
Cancel
Save