Jonathan Underwood
7 years ago
committed by
GitHub
8 changed files with 139 additions and 43 deletions
@ -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 |
@ -0,0 +1,63 @@ |
|||
{ |
|||
"valid": [ |
|||
{ |
|||
"description": "output from output", |
|||
"arguments": { |
|||
"output": "OP_RETURN a3b147dbe4a85579fc4b5a1811e76620560e07267e62b9a0d6858f9127735cadd82f67e06c24dbc4" |
|||
}, |
|||
"expected": { |
|||
"data": [ |
|||
"a3b147dbe4a85579fc4b5a1811e76620560e07267e62b9a0d6858f9127735cadd82f67e06c24dbc4" |
|||
], |
|||
"output": "OP_RETURN a3b147dbe4a85579fc4b5a1811e76620560e07267e62b9a0d6858f9127735cadd82f67e06c24dbc4", |
|||
"input": null, |
|||
"witness": null |
|||
} |
|||
}, |
|||
{ |
|||
"description": "output from data", |
|||
"arguments": { |
|||
"data": [ |
|||
"a3b147dbe4a85579fc4b5a1811e76620560e07267e62b9a0d6858f9127735cadd82f67e06c24dbc4" |
|||
] |
|||
}, |
|||
"expected": { |
|||
"data": [ |
|||
"a3b147dbe4a85579fc4b5a1811e76620560e07267e62b9a0d6858f9127735cadd82f67e06c24dbc4" |
|||
], |
|||
"output": "OP_RETURN a3b147dbe4a85579fc4b5a1811e76620560e07267e62b9a0d6858f9127735cadd82f67e06c24dbc4", |
|||
"input": null, |
|||
"witness": null |
|||
} |
|||
} |
|||
], |
|||
"invalid": [ |
|||
{ |
|||
"exception": "Not enough data", |
|||
"arguments": {} |
|||
} |
|||
], |
|||
"dynamic": { |
|||
"depends": { |
|||
"data": [ "data", "output" ], |
|||
"output": [ "output", "data" ] |
|||
}, |
|||
"details": [ |
|||
{ |
|||
"description": "embed", |
|||
"data": [ |
|||
"a3b147dbe4a85579fc4b5a1811e76620560e07267e62b9a0d6858f9127735cadd82f67e06c24dbc4" |
|||
], |
|||
"output": "OP_RETURN a3b147dbe4a85579fc4b5a1811e76620560e07267e62b9a0d6858f9127735cadd82f67e06c24dbc4" |
|||
}, |
|||
{ |
|||
"description": "embed", |
|||
"data": [ |
|||
"a3b147dbe4a85579fc4b5a1811e76620560e0726", |
|||
"7e62b9a0d6858f9127735cadd82f67e06c24dbc4" |
|||
], |
|||
"output": "OP_RETURN a3b147dbe4a85579fc4b5a1811e76620560e0726 7e62b9a0d6858f9127735cadd82f67e06c24dbc4" |
|||
} |
|||
] |
|||
} |
|||
} |
Loading…
Reference in new issue