Page MenuHomePhabricator

D12864.id37227.diff
No OneTemporary

D12864.id37227.diff

diff --git a/web/pushdata-bitcoin/.gitignore b/web/pushdata-bitcoin/.gitignore
new file mode 100644
--- /dev/null
+++ b/web/pushdata-bitcoin/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/web/pushdata-bitcoin/LICENSE b/web/pushdata-bitcoin/LICENSE
new file mode 100644
--- /dev/null
+++ b/web/pushdata-bitcoin/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2016 Daniel Cousens
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/web/pushdata-bitcoin/README.md b/web/pushdata-bitcoin/README.md
new file mode 100644
--- /dev/null
+++ b/web/pushdata-bitcoin/README.md
@@ -0,0 +1,20 @@
+# pushdata-bitcoin
+
+[![NPM Package](https://img.shields.io/npm/v/pushdata-bitcoin.svg?style=flat-square)](https://www.npmjs.org/package/pushdata-bitcoin)
+[![Build Status](https://img.shields.io/travis/bitcoinjs/pushdata-bitcoin.svg?branch=master&style=flat-square)](https://travis-ci.org/bitcoinjs/pushdata-bitcoin)
+[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
+
+Encode/decode value as bitcoin `OP_PUSHDATA` integer
+
+## Example
+
+```javascript
+var pushdata = require('pushdata-bitcoin');
+
+var i = 120;
+var buffer = new Buffer(pushdata.encodingLength(i));
+
+pushdata.encode(buffer, i);
+```
+
+## LICENSE [MIT](LICENSE)
diff --git a/web/pushdata-bitcoin/index.js b/web/pushdata-bitcoin/index.js
new file mode 100644
--- /dev/null
+++ b/web/pushdata-bitcoin/index.js
@@ -0,0 +1,74 @@
+var OPS = require('bitcoin-ops');
+
+function encodingLength(i) {
+ return i < OPS.OP_PUSHDATA1 ? 1 : i <= 0xff ? 2 : i <= 0xffff ? 3 : 5;
+}
+
+function encode(buffer, number, offset) {
+ var size = encodingLength(number);
+
+ // ~6 bit
+ if (size === 1) {
+ buffer.writeUInt8(number, offset);
+
+ // 8 bit
+ } else if (size === 2) {
+ buffer.writeUInt8(OPS.OP_PUSHDATA1, offset);
+ buffer.writeUInt8(number, offset + 1);
+
+ // 16 bit
+ } else if (size === 3) {
+ buffer.writeUInt8(OPS.OP_PUSHDATA2, offset);
+ buffer.writeUInt16LE(number, offset + 1);
+
+ // 32 bit
+ } else {
+ buffer.writeUInt8(OPS.OP_PUSHDATA4, offset);
+ buffer.writeUInt32LE(number, offset + 1);
+ }
+
+ return size;
+}
+
+function decode(buffer, offset) {
+ var opcode = buffer.readUInt8(offset);
+ var number, size;
+
+ // ~6 bit
+ if (opcode < OPS.OP_PUSHDATA1) {
+ number = opcode;
+ size = 1;
+
+ // 8 bit
+ } else if (opcode === OPS.OP_PUSHDATA1) {
+ if (offset + 2 > buffer.length) return null;
+ number = buffer.readUInt8(offset + 1);
+ size = 2;
+
+ // 16 bit
+ } else if (opcode === OPS.OP_PUSHDATA2) {
+ if (offset + 3 > buffer.length) return null;
+ number = buffer.readUInt16LE(offset + 1);
+ size = 3;
+
+ // 32 bit
+ } else {
+ if (offset + 5 > buffer.length) return null;
+ if (opcode !== OPS.OP_PUSHDATA4) throw new Error('Unexpected opcode');
+
+ number = buffer.readUInt32LE(offset + 1);
+ size = 5;
+ }
+
+ return {
+ opcode: opcode,
+ number: number,
+ size: size,
+ };
+}
+
+module.exports = {
+ encodingLength: encodingLength,
+ encode: encode,
+ decode: decode,
+};
diff --git a/web/pushdata-bitcoin/package.json b/web/pushdata-bitcoin/package.json
new file mode 100644
--- /dev/null
+++ b/web/pushdata-bitcoin/package.json
@@ -0,0 +1,48 @@
+{
+ "name": "pushdata-bitcoin",
+ "version": "1.0.1",
+ "description": "encode/decode value as bitcoin OP_PUSHDATA integer",
+ "keywords": [
+ "push",
+ "encode",
+ "decode",
+ "abstract-encoding",
+ "OP_0",
+ "OP_1",
+ "OP_PUSHDATA1",
+ "OP_PUSHDATA2",
+ "OP_PUSHDATA4",
+ "pushdata-bitcoin",
+ "bitcoinjs-lib",
+ "bitcoin"
+ ],
+ "homepage": "https://github.com/bitcoinjs/pushdata-bitcoin",
+ "bugs": {
+ "url": "https://github.com/bitcoinjs/pushdata-bitcoin/issues"
+ },
+ "license": "MIT",
+ "author": "Daniel Cousens",
+ "files": [
+ "index.js"
+ ],
+ "main": "index.js",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/bitcoinjs/pushdata-bitcoin.git"
+ },
+ "scripts": {
+ "prepublish": "npm run test",
+ "coverage": "nyc --check-coverage --branches 100 --functions 100 tape test/*.js",
+ "lint": "standard",
+ "test": "npm run lint && npm run unit",
+ "unit": "tape test/*.js"
+ },
+ "devDependencies": {
+ "nyc": "^6.4.0",
+ "standard": "*",
+ "tape": "^4.5.1"
+ },
+ "dependencies": {
+ "bitcoin-ops": "^1.3.0"
+ }
+}
diff --git a/web/pushdata-bitcoin/test/fixtures.json b/web/pushdata-bitcoin/test/fixtures.json
new file mode 100644
--- /dev/null
+++ b/web/pushdata-bitcoin/test/fixtures.json
@@ -0,0 +1,62 @@
+{
+ "valid": [
+ {
+ "dec": 0,
+ "hex": "00"
+ },
+ {
+ "dec": 1,
+ "hex": "01"
+ },
+ {
+ "dec": 252,
+ "hex": "4cfc"
+ },
+ {
+ "dec": 253,
+ "hex": "4cfd"
+ },
+ {
+ "dec": 254,
+ "hex": "4cfe"
+ },
+ {
+ "dec": 255,
+ "hex": "4cff"
+ },
+ {
+ "dec": 65534,
+ "hex": "4dfeff"
+ },
+ {
+ "dec": 65535,
+ "hex": "4dffff"
+ },
+ {
+ "dec": 65536,
+ "hex": "4e00000100"
+ },
+ {
+ "dec": 65537,
+ "hex": "4e01000100"
+ },
+ {
+ "dec": 4294967295,
+ "hex": "4effffffff"
+ }
+ ],
+ "invalid": [
+ {
+ "description": "OP_PUSHDATA1, no size",
+ "hex": "4c"
+ },
+ {
+ "description": "OP_PUSHDATA2, no size",
+ "hex": "4d"
+ },
+ {
+ "description": "OP_PUSHDATA4, no size",
+ "hex": "4e"
+ }
+ ]
+}
diff --git a/web/pushdata-bitcoin/test/index.js b/web/pushdata-bitcoin/test/index.js
new file mode 100644
--- /dev/null
+++ b/web/pushdata-bitcoin/test/index.js
@@ -0,0 +1,33 @@
+var pushdata = require('../');
+var fixtures = require('./fixtures');
+var tape = require('tape');
+
+fixtures.valid.forEach(function (f) {
+ tape('Valid for ' + f.hex, function (t) {
+ t.plan(5);
+ var fopcode = parseInt(f.hex.substr(0, 2), 16);
+ var size = pushdata.encodingLength(f.dec);
+ t.strictEqual(size, f.hex.length / 2);
+
+ var buffer = new Buffer(f.hex, 'hex');
+ var d = pushdata.decode(buffer, 0);
+
+ t.strictEqual(d.opcode, fopcode);
+ t.strictEqual(d.number, f.dec);
+ t.strictEqual(d.size, buffer.length);
+
+ buffer.fill(0);
+ var n = pushdata.encode(buffer, f.dec, 0);
+ t.strictEqual(buffer.slice(0, n).toString('hex'), f.hex);
+ });
+});
+
+fixtures.invalid.forEach(function (f) {
+ tape('Invalid for ' + f.description, function (t) {
+ t.plan(1);
+
+ var buffer = new Buffer(f.hex, 'hex');
+ var n = pushdata.decode(buffer, 0);
+ t.strictEqual(n, null);
+ });
+});

File Metadata

Mime Type
text/plain
Expires
Sat, Apr 26, 10:02 (1 h, 23 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5569955
Default Alt Text
D12864.id37227.diff (8 KB)

Event Timeline