Browse Source

package/*: use bitcoin-ops, pushdata-bitcoin packages

hk-custom-address
Daniel Cousens 8 years ago
committed by Thomas Kerin
parent
commit
33c72e49c4
  1. 2
      package.json
  2. 78
      src/bufferutils.js
  3. 2
      src/index.js
  4. 130
      src/opcodes.json
  5. 20
      src/script.js
  6. 2
      src/templates/multisig/input.js
  7. 2
      src/templates/multisig/output.js
  8. 2
      src/templates/nulldata.js
  9. 2
      src/templates/pubkey/output.js
  10. 2
      src/templates/pubkeyhash/output.js
  11. 2
      src/templates/scripthash/output.js
  12. 2
      src/templates/witnesspubkeyhash/output.js
  13. 2
      src/templates/witnessscripthash/output.js
  14. 2
      src/transaction.js
  15. 2
      src/transaction_builder.js
  16. 2
      test/templates.js
  17. 2
      test/transaction_builder.js

2
package.json

@ -53,12 +53,14 @@
"dependencies": {
"bigi": "^1.4.0",
"bip66": "^1.1.0",
"bitcoin-ops": "^1.3.0",
"bs58check": "^1.0.5",
"buffer-reverse": "^1.0.0",
"create-hash": "^1.1.0",
"create-hmac": "^1.1.3",
"ecurve": "^1.0.0",
"merkle-lib": "^1.0.0",
"pushdata-bitcoin": "^1.0.1",
"randombytes": "^2.0.1",
"typeforce": "^1.8.7",
"varuint-bitcoin": "^1.0.4",

78
src/bufferutils.js

@ -1,50 +1,6 @@
var opcodes = require('./opcodes.json')
var pushdata = require('pushdata-bitcoin')
var varuint = require('varuint-bitcoin')
function pushDataSize (i) {
return i < opcodes.OP_PUSHDATA1 ? 1
: i <= 0xff ? 2
: i <= 0xffff ? 3
: 5
}
function readPushDataInt (buffer, offset) {
var opcode = buffer.readUInt8(offset)
var number, size
// ~6 bit
if (opcode < opcodes.OP_PUSHDATA1) {
number = opcode
size = 1
// 8 bit
} else if (opcode === opcodes.OP_PUSHDATA1) {
if (offset + 2 > buffer.length) return null
number = buffer.readUInt8(offset + 1)
size = 2
// 16 bit
} else if (opcode === opcodes.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 !== opcodes.OP_PUSHDATA4) throw new Error('Unexpected opcode')
number = buffer.readUInt32LE(offset + 1)
size = 5
}
return {
opcode: opcode,
number: number,
size: size
}
}
// https://github.com/feross/buffer/blob/master/index.js#L1127
function verifuint (value, max) {
if (typeof value !== 'number') throw new Error('cannot write a non-number as a number')
@ -63,32 +19,6 @@ function readUInt64LE (buffer, offset) {
return b + a
}
function writePushDataInt (buffer, number, offset) {
var size = pushDataSize(number)
// ~6 bit
if (size === 1) {
buffer.writeUInt8(number, offset)
// 8 bit
} else if (size === 2) {
buffer.writeUInt8(opcodes.OP_PUSHDATA1, offset)
buffer.writeUInt8(number, offset + 1)
// 16 bit
} else if (size === 3) {
buffer.writeUInt8(opcodes.OP_PUSHDATA2, offset)
buffer.writeUInt16LE(number, offset + 1)
// 32 bit
} else {
buffer.writeUInt8(opcodes.OP_PUSHDATA4, offset)
buffer.writeUInt32LE(number, offset + 1)
}
return size
}
function writeUInt64LE (buffer, value, offset) {
verifuint(value, 0x001fffffffffffff)
@ -114,13 +44,13 @@ function writeVarInt (buffer, number, offset) {
}
module.exports = {
pushDataSize: pushDataSize,
readPushDataInt: readPushDataInt,
pushDataSize: pushdata.encodingLength,
readPushDataInt: pushdata.decode,
readUInt64LE: readUInt64LE,
readVarInt: readVarInt,
varIntBuffer: varuint.encode,
varIntSize: varuint.encodingLength,
writePushDataInt: writePushDataInt,
writePushDataInt: pushdata.encode,
writeUInt64LE: writeUInt64LE,
writeVarInt: writeVarInt
}

2
src/index.js

@ -10,6 +10,6 @@ module.exports = {
bufferutils: require('./bufferutils'),
crypto: require('./crypto'),
networks: require('./networks'),
opcodes: require('./opcodes.json'),
opcodes: require('bitcoin-ops'),
script: require('./script')
}

130
src/opcodes.json

@ -1,130 +0,0 @@
{
"OP_FALSE": 0,
"OP_0": 0,
"OP_PUSHDATA1": 76,
"OP_PUSHDATA2": 77,
"OP_PUSHDATA4": 78,
"OP_1NEGATE": 79,
"OP_RESERVED": 80,
"OP_1": 81,
"OP_TRUE": 81,
"OP_2": 82,
"OP_3": 83,
"OP_4": 84,
"OP_5": 85,
"OP_6": 86,
"OP_7": 87,
"OP_8": 88,
"OP_9": 89,
"OP_10": 90,
"OP_11": 91,
"OP_12": 92,
"OP_13": 93,
"OP_14": 94,
"OP_15": 95,
"OP_16": 96,
"OP_NOP": 97,
"OP_VER": 98,
"OP_IF": 99,
"OP_NOTIF": 100,
"OP_VERIF": 101,
"OP_VERNOTIF": 102,
"OP_ELSE": 103,
"OP_ENDIF": 104,
"OP_VERIFY": 105,
"OP_RETURN": 106,
"OP_TOALTSTACK": 107,
"OP_FROMALTSTACK": 108,
"OP_2DROP": 109,
"OP_2DUP": 110,
"OP_3DUP": 111,
"OP_2OVER": 112,
"OP_2ROT": 113,
"OP_2SWAP": 114,
"OP_IFDUP": 115,
"OP_DEPTH": 116,
"OP_DROP": 117,
"OP_DUP": 118,
"OP_NIP": 119,
"OP_OVER": 120,
"OP_PICK": 121,
"OP_ROLL": 122,
"OP_ROT": 123,
"OP_SWAP": 124,
"OP_TUCK": 125,
"OP_CAT": 126,
"OP_SUBSTR": 127,
"OP_LEFT": 128,
"OP_RIGHT": 129,
"OP_SIZE": 130,
"OP_INVERT": 131,
"OP_AND": 132,
"OP_OR": 133,
"OP_XOR": 134,
"OP_EQUAL": 135,
"OP_EQUALVERIFY": 136,
"OP_RESERVED1": 137,
"OP_RESERVED2": 138,
"OP_1ADD": 139,
"OP_1SUB": 140,
"OP_2MUL": 141,
"OP_2DIV": 142,
"OP_NEGATE": 143,
"OP_ABS": 144,
"OP_NOT": 145,
"OP_0NOTEQUAL": 146,
"OP_ADD": 147,
"OP_SUB": 148,
"OP_MUL": 149,
"OP_DIV": 150,
"OP_MOD": 151,
"OP_LSHIFT": 152,
"OP_RSHIFT": 153,
"OP_BOOLAND": 154,
"OP_BOOLOR": 155,
"OP_NUMEQUAL": 156,
"OP_NUMEQUALVERIFY": 157,
"OP_NUMNOTEQUAL": 158,
"OP_LESSTHAN": 159,
"OP_GREATERTHAN": 160,
"OP_LESSTHANOREQUAL": 161,
"OP_GREATERTHANOREQUAL": 162,
"OP_MIN": 163,
"OP_MAX": 164,
"OP_WITHIN": 165,
"OP_RIPEMD160": 166,
"OP_SHA1": 167,
"OP_SHA256": 168,
"OP_HASH160": 169,
"OP_HASH256": 170,
"OP_CODESEPARATOR": 171,
"OP_CHECKSIG": 172,
"OP_CHECKSIGVERIFY": 173,
"OP_CHECKMULTISIG": 174,
"OP_CHECKMULTISIGVERIFY": 175,
"OP_NOP1": 176,
"OP_NOP2": 177,
"OP_CHECKLOCKTIMEVERIFY": 177,
"OP_NOP3": 178,
"OP_NOP4": 179,
"OP_NOP5": 180,
"OP_NOP6": 181,
"OP_NOP7": 182,
"OP_NOP8": 183,
"OP_NOP9": 184,
"OP_NOP10": 185,
"OP_PUBKEYHASH": 253,
"OP_PUBKEY": 254,
"OP_INVALIDOPCODE": 255
}

20
src/script.js

@ -1,17 +1,11 @@
var bip66 = require('bip66')
var bufferutils = require('./bufferutils')
var pushdata = require('pushdata-bitcoin')
var typeforce = require('typeforce')
var types = require('./types')
var scriptNumber = require('./script_number')
var OPS = require('./opcodes.json')
var REVERSE_OPS = (function () {
var result = {}
for (var op in OPS) {
var code = OPS[op]
result[code] = op
}
return result
})()
var OPS = require('bitcoin-ops')
var REVERSE_OPS = require('bitcoin-ops/map')
var OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
function isOPInt (value) {
@ -43,7 +37,7 @@ function compile (chunks) {
return accum + 1
}
return accum + bufferutils.pushDataSize(chunk.length) + chunk.length
return accum + pushdata.encodingLength(chunk.length) + chunk.length
}
// opcode
@ -70,7 +64,7 @@ function compile (chunks) {
return
}
offset += bufferutils.writePushDataInt(buffer, chunk.length, offset)
offset += pushdata.encode(buffer, chunk.length, offset)
chunk.copy(buffer, offset)
offset += chunk.length
@ -100,7 +94,7 @@ function decompile (buffer) {
// data chunk
if ((opcode > OPS.OP_0) && (opcode <= OPS.OP_PUSHDATA4)) {
var d = bufferutils.readPushDataInt(buffer, i)
var d = pushdata.decode(buffer, i)
// did reading a pushDataInt fail? empty script
if (d === null) return []

2
src/templates/multisig/input.js

@ -2,7 +2,7 @@
var bscript = require('../../script')
var typeforce = require('typeforce')
var OPS = require('../../opcodes.json')
var OPS = require('bitcoin-ops')
function partialSignature (value) {
return value === OPS.OP_0 || bscript.isCanonicalSignature(value)

2
src/templates/multisig/output.js

@ -3,7 +3,7 @@
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
var OPS = require('../../opcodes.json')
var OPS = require('bitcoin-ops')
var OP_INT_BASE = OPS.OP_RESERVED // OP_1 - 1
function check (script, allowIncomplete) {

2
src/templates/nulldata.js

@ -3,7 +3,7 @@
var bscript = require('../script')
var types = require('../types')
var typeforce = require('typeforce')
var OPS = require('../opcodes.json')
var OPS = require('bitcoin-ops')
function check (script) {
var buffer = bscript.compile(script)

2
src/templates/pubkey/output.js

@ -2,7 +2,7 @@
var bscript = require('../../script')
var typeforce = require('typeforce')
var OPS = require('../../opcodes.json')
var OPS = require('bitcoin-ops')
function check (script) {
var chunks = bscript.decompile(script)

2
src/templates/pubkeyhash/output.js

@ -3,7 +3,7 @@
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
var OPS = require('../../opcodes.json')
var OPS = require('bitcoin-ops')
function check (script) {
var buffer = bscript.compile(script)

2
src/templates/scripthash/output.js

@ -3,7 +3,7 @@
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
var OPS = require('../../opcodes.json')
var OPS = require('bitcoin-ops')
function check (script) {
var buffer = bscript.compile(script)

2
src/templates/witnesspubkeyhash/output.js

@ -3,7 +3,7 @@
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
var OPS = require('../../opcodes.json')
var OPS = require('bitcoin-ops')
function check (script) {
var buffer = bscript.compile(script)

2
src/templates/witnessscripthash/output.js

@ -3,7 +3,7 @@
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
var OPS = require('../../opcodes.json')
var OPS = require('bitcoin-ops')
function check (script) {
var buffer = bscript.compile(script)

2
src/transaction.js

@ -2,7 +2,7 @@ var bcrypto = require('./crypto')
var bscript = require('./script')
var bufferutils = require('./bufferutils')
var bufferReverse = require('buffer-reverse')
var opcodes = require('./opcodes.json')
var opcodes = require('bitcoin-ops')
var typeforce = require('typeforce')
var types = require('./types')

2
src/transaction_builder.js

@ -3,7 +3,7 @@ var bcrypto = require('./crypto')
var bscript = require('./script')
var bufferReverse = require('buffer-reverse')
var networks = require('./networks')
var ops = require('./opcodes.json')
var ops = require('bitcoin-ops')
var typeforce = require('typeforce')
var types = require('./types')
var scriptTypes = bscript.types

2
test/templates.js

@ -3,7 +3,7 @@
var assert = require('assert')
var bcrypto = require('../src/crypto')
var bscript = require('../src/script')
var ops = require('../src/opcodes')
var ops = require('bitcoin-ops')
var fixtures = require('./fixtures/templates.json')

2
test/transaction_builder.js

@ -3,7 +3,7 @@
var assert = require('assert')
var baddress = require('../src/address')
var bscript = require('../src/script')
var ops = require('../src/opcodes')
var ops = require('bitcoin-ops')
var BigInteger = require('bigi')
var ECPair = require('../src/ecpair')

Loading…
Cancel
Save