Socket
Socket
Sign inDemoInstall

bitcoinjs-lib

Package Overview
Dependencies
Maintainers
1
Versions
88
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bitcoinjs-lib - npm Package Compare versions

Comparing version 3.2.0 to 3.2.1

31

package.json
{
"name": "bitcoinjs-lib",
"version": "3.2.0",
"version": "3.2.1",
"description": "Client-side Bitcoin JavaScript library",

@@ -10,29 +10,8 @@ "main": "./src/index.js",

"keywords": [
"bitcoinjs",
"bitcoin",
"browser",
"client",
"library"
"browserify",
"javascript",
"bitcoinjs"
],
"contributors": [
{
"name": "Daniel Cousens",
"email": "bitcoin@dcousens.com",
"url": "http://dcousens.com"
},
{
"name": "Kyle Drake",
"email": "kyle@kyledrake.net",
"url": "http://kyledrake.net/"
},
{
"name": "Wei Lu",
"email": "luwei.here@gmail.com",
"url": "http://weilu.github.io/"
},
{
"name": "Stefan Thomas",
"email": "justmoon@members.fsf.org",
"url": "http://www.justmoon.net"
}
],
"scripts": {

@@ -39,0 +18,0 @@ "coverage-report": "nyc report --reporter=lcov",

40

README.md

@@ -9,3 +9,3 @@ # BitcoinJS (bitcoinjs-lib)

The pure JavaScript Bitcoin library for node.js and browsers.
Used by over a million wallet users and the backbone for almost all Bitcoin web wallets in production today.
Estimated to be in use by over 15 million wallet users and is the backbone for almost all Bitcoin web wallets in production today.

@@ -45,37 +45,37 @@

If you're familiar with how to use browserify, ignore this and proceed normally.
These steps are advisory only, and may not be necessary for your application.
These steps are advisory only, and may not be suitable for your application.
[Browserify](https://github.com/substack/node-browserify) is assumed to be installed for these steps.
From your repository, create an `index.js` file
For your project, create an `index.js` file
``` javascript
let bitcoin = require('bitcoinjs-lib')
// your code here
function myFunction () {
return bitcoin.ECPair.makeRandom().toWIF()
}
module.exports = {
base58: require('bs58'),
bitcoin: require('bitcoinjs-lib'),
ecurve: require('ecurve'),
BigInteger: require('bigi')
myFunction
}
```
Install each of the above packages locally
Now, to compile for the browser:
``` bash
npm install bs58 bitcoinjs-lib ecurve bigi
browserify index.js --standalone foo > app.js
```
After installation, use browserify to compile `index.js` for use in the browser:
``` bash
$ browserify index.js --standalone foo > app.js
```
You can now put `<script src="app.js" />` in your web page, using `foo.myFunction` to create a new Bitcoin private key.
You will now be able to use `<script src="app.js" />` in your browser, with each of the above exports accessible via the global `foo` object (or whatever you chose for the `--standalone` parameter above).
**NOTE**: If you uglify the javascript, you must exclude the following variable names from being mangled: `BigInteger`, `ECPair`, `Point`.
This is because of the function-name-duck-typing used in [typeforce](https://github.com/dcousens/typeforce).
**NOTE**: See our package.json for the currently supported version of browserify used by this repository.
**NOTE**: When uglifying the javascript, you must exclude the following variable names from being mangled: `Array`, `BigInteger`, `Boolean`, `ECPair`, `Function`, `Number`, `Point` and `Script`.
This is because of the function-name-duck-typing used in [typeforce](https://github.com/dcousens/typeforce).
Example:
``` bash
uglifyjs ... --mangle --reserved 'Array,BigInteger,Boolean,ECPair,Function,Number,Point'
uglifyjs ... --mangle --reserved 'BigInteger,ECPair,Point'
```
**NOTE**: This library tracks Node LTS features, if you need strict ES5, use [`--transform babelify`](https://github.com/babel/babelify) in conjunction with your `browserify` step (using an [`es2015`](http://babeljs.io/docs/plugins/preset-es2015/) preset).
**NOTE**: If you expect this library to run on an iOS 10 device, ensure that you are using [buffer@5.0.5](https://github.com/feross/buffer/pull/155) or greater.

@@ -90,3 +90,3 @@

You can now use `bitcoinjs-lib` as a typescript compliant library.
You can now use `bitcoinjs-lib` as a typescript compliant library.
``` javascript

@@ -93,0 +93,0 @@ import { HDNode, Transaction } from 'bitcoinjs-lib'

@@ -5,2 +5,3 @@ var Buffer = require('safe-buffer').Buffer

var bscript = require('./script')
var btemplates = require('./templates')
var networks = require('./networks')

@@ -54,6 +55,6 @@ var typeforce = require('typeforce')

if (bscript.pubKeyHash.output.check(outputScript)) return toBase58Check(bscript.compile(outputScript).slice(3, 23), network.pubKeyHash)
if (bscript.scriptHash.output.check(outputScript)) return toBase58Check(bscript.compile(outputScript).slice(2, 22), network.scriptHash)
if (bscript.witnessPubKeyHash.output.check(outputScript)) return toBech32(bscript.compile(outputScript).slice(2, 22), 0, network.bech32)
if (bscript.witnessScriptHash.output.check(outputScript)) return toBech32(bscript.compile(outputScript).slice(2, 34), 0, network.bech32)
if (btemplates.pubKeyHash.output.check(outputScript)) return toBase58Check(bscript.compile(outputScript).slice(3, 23), network.pubKeyHash)
if (btemplates.scriptHash.output.check(outputScript)) return toBase58Check(bscript.compile(outputScript).slice(2, 22), network.scriptHash)
if (btemplates.witnessPubKeyHash.output.check(outputScript)) return toBech32(bscript.compile(outputScript).slice(2, 22), 0, network.bech32)
if (btemplates.witnessScriptHash.output.check(outputScript)) return toBech32(bscript.compile(outputScript).slice(2, 34), 0, network.bech32)

@@ -72,4 +73,4 @@ throw new Error(bscript.toASM(outputScript) + ' has no matching Address')

if (decode) {
if (decode.version === network.pubKeyHash) return bscript.pubKeyHash.output.encode(decode.hash)
if (decode.version === network.scriptHash) return bscript.scriptHash.output.encode(decode.hash)
if (decode.version === network.pubKeyHash) return btemplates.pubKeyHash.output.encode(decode.hash)
if (decode.version === network.scriptHash) return btemplates.scriptHash.output.encode(decode.hash)
} else {

@@ -83,4 +84,4 @@ try {

if (decode.version === 0) {
if (decode.data.length === 20) return bscript.witnessPubKeyHash.output.encode(decode.data)
if (decode.data.length === 32) return bscript.witnessScriptHash.output.encode(decode.data)
if (decode.data.length === 20) return btemplates.witnessPubKeyHash.output.encode(decode.data)
if (decode.data.length === 32) return btemplates.witnessScriptHash.output.encode(decode.data)
}

@@ -87,0 +88,0 @@ }

@@ -0,2 +1,11 @@

var script = require('./script')
var templates = require('./templates')
for (var key in templates) {
script[key] = templates[key]
}
module.exports = {
bufferutils: require('./bufferutils'), // TODO: remove in 4.0.0
Block: require('./block'),

@@ -10,7 +19,6 @@ ECPair: require('./ecpair'),

address: require('./address'),
bufferutils: require('./bufferutils'), // TODO: remove in 4.0.0
crypto: require('./crypto'),
networks: require('./networks'),
opcodes: require('bitcoin-ops'),
script: require('./script')
script: script
}

@@ -215,6 +215,1 @@ var Buffer = require('safe-buffer').Buffer

}
var templates = require('./templates')
for (var key in templates) {
module.exports[key] = templates[key]
}

@@ -5,2 +5,3 @@ // OP_0 [signatures ...]

var bscript = require('../../script')
var p2mso = require('./output')
var typeforce = require('typeforce')

@@ -32,3 +33,3 @@ var OPS = require('bitcoin-ops')

if (scriptPubKey) {
var scriptData = bscript.multisig.output.decode(scriptPubKey)
var scriptData = p2mso.decode(scriptPubKey)

@@ -35,0 +36,0 @@ if (signatures.length < scriptData.m) {

@@ -7,2 +7,8 @@ // <scriptSig> {serialized scriptPubKey script}

var p2ms = require('../multisig/')
var p2pk = require('../pubkey/')
var p2pkh = require('../pubkeyhash/')
var p2wpkho = require('../witnesspubkeyhash/output')
var p2wsho = require('../witnessscripthash/output')
function check (script, allowIncomplete) {

@@ -24,8 +30,19 @@ var chunks = bscript.decompile(script)

var inputType = bscript.classifyInput(scriptSigChunks, allowIncomplete)
var outputType = bscript.classifyOutput(redeemScriptChunks)
// is witness?
if (chunks.length === 1) {
return outputType === bscript.types.P2WSH || outputType === bscript.types.P2WPKH
return p2wsho.check(redeemScriptChunks) ||
p2wpkho.check(redeemScriptChunks)
}
return inputType === outputType
// match types
if (p2pkh.input.check(scriptSigChunks) &&
p2pkh.output.check(redeemScriptChunks)) return true
if (p2ms.input.check(scriptSigChunks, allowIncomplete) &&
p2ms.output.check(redeemScriptChunks)) return true
if (p2pk.input.check(scriptSigChunks) &&
p2pk.output.check(redeemScriptChunks)) return true
return false
}

@@ -32,0 +49,0 @@ check.toJSON = function () { return 'scriptHash input' }

@@ -1,9 +0,63 @@

// {signature} {pubKey}
// <scriptSig> {serialized scriptPubKey script}
var p2sh = require('../scripthash/input')
var bscript = require('../../script')
var types = require('../../types')
var typeforce = require('typeforce')
var p2ms = require('../multisig/')
var p2pk = require('../pubkey/')
var p2pkh = require('../pubkeyhash/')
function check (chunks, allowIncomplete) {
typeforce(types.Array, chunks)
if (chunks.length < 1) return false
var witnessScript = chunks[chunks.length - 1]
if (!Buffer.isBuffer(witnessScript)) return false
var witnessScriptChunks = bscript.decompile(witnessScript)
// is witnessScript a valid script?
if (witnessScriptChunks.length === 0) return false
var witnessRawScriptSig = bscript.compile(chunks.slice(0, -1))
// match types
if (p2pkh.input.check(witnessRawScriptSig) &&
p2pkh.output.check(witnessScriptChunks)) return true
if (p2ms.input.check(witnessRawScriptSig, allowIncomplete) &&
p2ms.output.check(witnessScriptChunks)) return true
if (p2pk.input.check(witnessRawScriptSig) &&
p2pk.output.check(witnessScriptChunks)) return true
return false
}
check.toJSON = function () { return 'witnessScriptHash input' }
function encodeStack (witnessData, witnessScript) {
typeforce({
witnessData: [types.Buffer],
witnessScript: types.Buffer
}, {
witnessData: witnessData,
witnessScript: witnessScript
})
return [].concat(witnessData, witnessScript)
}
function decodeStack (chunks) {
typeforce(check, chunks)
return {
witnessData: chunks.slice(0, -1),
witnessScript: chunks[chunks.length - 1]
}
}
module.exports = {
check: p2sh.check,
decodeStack: p2sh.decodeStack,
encodeStack: p2sh.encodeStack
check: check,
decodeStack: decodeStack,
encodeStack: encodeStack
}

@@ -5,2 +5,3 @@ var Buffer = require('safe-buffer').Buffer

var bscript = require('./script')
var btemplates = require('./templates')
var networks = require('./networks')

@@ -10,5 +11,5 @@ var ops = require('bitcoin-ops')

var types = require('./types')
var scriptTypes = bscript.types
var SIGNABLE = [bscript.types.P2PKH, bscript.types.P2PK, bscript.types.MULTISIG]
var P2SH = SIGNABLE.concat([bscript.types.P2WPKH, bscript.types.P2WSH])
var scriptTypes = btemplates.types
var SIGNABLE = [btemplates.types.P2PKH, btemplates.types.P2PK, btemplates.types.MULTISIG]
var P2SH = SIGNABLE.concat([btemplates.types.P2WPKH, btemplates.types.P2WSH])

@@ -38,3 +39,3 @@ var ECPair = require('./ecpair')

case scriptTypes.P2PK:
pubKeys[0] = script ? bscript.pubKey.output.decode(script) : undefined
pubKeys[0] = script ? btemplates.pubKey.output.decode(script) : undefined
signatures = chunks.slice(0, 1)

@@ -45,3 +46,3 @@ break

if (script) {
var multisig = bscript.multisig.output.decode(script)
var multisig = btemplates.multisig.output.decode(script)
pubKeys = multisig.pubKeys

@@ -79,8 +80,8 @@ }

var scriptSigChunks = bscript.decompile(scriptSig)
var sigType = bscript.classifyInput(scriptSigChunks, true)
var sigType = btemplates.classifyInput(scriptSigChunks, true)
if (sigType === scriptTypes.P2SH) {
p2sh = true
redeemScript = scriptSigChunks[scriptSigChunks.length - 1]
redeemScriptType = bscript.classifyOutput(redeemScript)
prevOutScript = bscript.scriptHash.output.encode(bcrypto.hash160(redeemScript))
redeemScriptType = btemplates.classifyOutput(redeemScript)
prevOutScript = btemplates.scriptHash.output.encode(bcrypto.hash160(redeemScript))
prevOutType = scriptTypes.P2SH

@@ -90,10 +91,10 @@ script = redeemScript

var classifyWitness = bscript.classifyWitness(witnessStack, true)
var classifyWitness = btemplates.classifyWitness(witnessStack, true)
if (classifyWitness === scriptTypes.P2WSH) {
witnessScript = witnessStack[witnessStack.length - 1]
witnessScriptType = bscript.classifyOutput(witnessScript)
witnessScriptType = btemplates.classifyOutput(witnessScript)
p2wsh = true
witness = true
if (scriptSig.length === 0) {
prevOutScript = bscript.witnessScriptHash.output.encode(bcrypto.sha256(witnessScript))
prevOutScript = btemplates.witnessScriptHash.output.encode(bcrypto.sha256(witnessScript))
prevOutType = scriptTypes.P2WSH

@@ -108,3 +109,3 @@ if (redeemScript !== undefined) {

}
witnessProgram = bscript.witnessScriptHash.output.encode(bcrypto.sha256(witnessScript))
witnessProgram = btemplates.witnessScriptHash.output.encode(bcrypto.sha256(witnessScript))
if (!redeemScript.equals(witnessProgram)) {

@@ -115,3 +116,3 @@ throw new Error('Redeem script didn\'t match witnessScript')

if (!supportedType(bscript.classifyOutput(witnessScript))) {
if (!supportedType(btemplates.classifyOutput(witnessScript))) {
throw new Error('unsupported witness script')

@@ -128,3 +129,3 @@ }

if (scriptSig.length === 0) {
prevOutScript = bscript.witnessPubKeyHash.output.encode(keyHash)
prevOutScript = btemplates.witnessPubKeyHash.output.encode(keyHash)
prevOutType = scriptTypes.P2WPKH

@@ -138,3 +139,3 @@ if (typeof redeemScript !== 'undefined') {

}
witnessProgram = bscript.witnessPubKeyHash.output.encode(keyHash)
witnessProgram = btemplates.witnessPubKeyHash.output.encode(keyHash)
if (!redeemScript.equals(witnessProgram)) {

@@ -156,3 +157,3 @@ throw new Error('Redeem script did not have the right witness program')

} else {
prevOutType = scriptType = bscript.classifyInput(scriptSig)
prevOutType = scriptType = btemplates.classifyInput(scriptSig)
chunks = scriptSigChunks

@@ -225,3 +226,3 @@ }

if (!scriptType) {
scriptType = bscript.classifyOutput(script)
scriptType = btemplates.classifyOutput(script)
}

@@ -268,3 +269,3 @@

function checkP2shInput (input, redeemScriptHash) {
function checkP2SHInput (input, redeemScriptHash) {
if (input.prevOutType) {

@@ -307,12 +308,13 @@ if (input.prevOutType !== scriptTypes.P2SH) throw new Error('PrevOutScript must be P2SH')

witnessScriptHash = bcrypto.sha256(witnessScript)
checkP2shInput(input, redeemScriptHash)
checkP2SHInput(input, redeemScriptHash)
if (!redeemScript.equals(bscript.witnessScriptHash.output.encode(witnessScriptHash))) throw new Error('Witness script inconsistent with redeem script')
if (!redeemScript.equals(btemplates.witnessScriptHash.output.encode(witnessScriptHash))) throw new Error('Witness script inconsistent with redeem script')
expanded = expandOutput(witnessScript, undefined, kpPubKey)
if (!expanded.pubKeys) throw new Error('WitnessScript not supported "' + bscript.toASM(redeemScript) + '"')
prevOutType = bscript.types.P2SH
prevOutScript = bscript.scriptHash.output.encode(redeemScriptHash)
prevOutType = btemplates.types.P2SH
prevOutScript = btemplates.scriptHash.output.encode(redeemScriptHash)
p2sh = witness = p2wsh = true
p2shType = bscript.types.P2WSH
p2shType = btemplates.types.P2WSH
signType = witnessType = expanded.scriptType

@@ -322,3 +324,3 @@ signScript = witnessScript

redeemScriptHash = bcrypto.hash160(redeemScript)
checkP2shInput(input, redeemScriptHash)
checkP2SHInput(input, redeemScriptHash)

@@ -328,8 +330,8 @@ expanded = expandOutput(redeemScript, undefined, kpPubKey)

prevOutType = bscript.types.P2SH
prevOutScript = bscript.scriptHash.output.encode(redeemScriptHash)
prevOutType = btemplates.types.P2SH
prevOutScript = btemplates.scriptHash.output.encode(redeemScriptHash)
p2sh = true
signType = p2shType = expanded.scriptType
signScript = redeemScript
witness = signType === bscript.types.P2WPKH
witness = signType === btemplates.types.P2WPKH
} else if (witnessScript) {

@@ -342,4 +344,4 @@ witnessScriptHash = bcrypto.sha256(witnessScript)

prevOutType = bscript.types.P2WSH
prevOutScript = bscript.witnessScriptHash.output.encode(witnessScriptHash)
prevOutType = btemplates.types.P2WSH
prevOutScript = btemplates.witnessScriptHash.output.encode(witnessScriptHash)
witness = p2wsh = true

@@ -364,4 +366,5 @@ signType = witnessType = expanded.scriptType

} else {
prevOutScript = bscript.pubKeyHash.output.encode(bcrypto.hash160(kpPubKey))
prevOutScript = btemplates.pubKeyHash.output.encode(bcrypto.hash160(kpPubKey))
expanded = expandOutput(prevOutScript, scriptTypes.P2PKH, kpPubKey)
prevOutType = scriptTypes.P2PKH

@@ -373,10 +376,4 @@ witness = false

if (witnessValue !== undefined || witness) {
typeforce(types.Satoshi, witnessValue)
if (input.value !== undefined && input.value !== witnessValue) throw new Error('Input didn\'t match witnessValue')
input.value = witnessValue
}
if (signType === scriptTypes.P2WPKH) {
signScript = bscript.pubKeyHash.output.encode(bscript.witnessPubKeyHash.output.decode(signScript))
signScript = btemplates.pubKeyHash.output.encode(btemplates.witnessPubKeyHash.output.decode(signScript))
}

@@ -405,5 +402,5 @@

if (type === scriptTypes.P2PKH) {
if (signatures.length === 1 && Buffer.isBuffer(signatures[0]) && pubKeys.length === 1) return bscript.pubKeyHash.input.encodeStack(signatures[0], pubKeys[0])
if (signatures.length === 1 && Buffer.isBuffer(signatures[0]) && pubKeys.length === 1) return btemplates.pubKeyHash.input.encodeStack(signatures[0], pubKeys[0])
} else if (type === scriptTypes.P2PK) {
if (signatures.length === 1 && Buffer.isBuffer(signatures[0])) return bscript.pubKey.input.encodeStack(signatures[0])
if (signatures.length === 1 && Buffer.isBuffer(signatures[0])) return btemplates.pubKey.input.encodeStack(signatures[0])
} else if (type === scriptTypes.MULTISIG) {

@@ -419,3 +416,3 @@ if (signatures.length > 0) {

return bscript.multisig.input.encodeStack(signatures)
return btemplates.multisig.input.encodeStack(signatures)
}

@@ -440,3 +437,3 @@ } else {

var p2sh = false
if (scriptType === bscript.types.P2SH) {
if (scriptType === btemplates.types.P2SH) {
// We can remove this error later when we have a guarantee prepareInput

@@ -461,7 +458,7 @@ // rejects unsignable scripts - it MUST be signable at this point.

// P2WPKH is a special case of P2PKH
case bscript.types.P2WPKH:
witness = buildStack(bscript.types.P2PKH, input.signatures, input.pubKeys, allowIncomplete)
case btemplates.types.P2WPKH:
witness = buildStack(btemplates.types.P2PKH, input.signatures, input.pubKeys, allowIncomplete)
break
case bscript.types.P2WSH:
case btemplates.types.P2WSH:
// We can remove this check later

@@ -619,3 +616,3 @@ if (!allowIncomplete && !supportedType(input.witnessScriptType)) {

input.prevOutScript = options.prevOutScript
input.prevOutType = prevOutType || bscript.classifyOutput(options.prevOutScript)
input.prevOutType = prevOutType || btemplates.classifyOutput(options.prevOutScript)
}

@@ -664,3 +661,3 @@

if (!allowIncomplete) {
if (!supportedType(result.type) && result.type !== bscript.types.P2WPKH) {
if (!supportedType(result.type) && result.type !== btemplates.types.P2WPKH) {
throw new Error(result.type + ' not supported')

@@ -691,3 +688,6 @@ }

input.pubKeys.length > 0 &&
input.witness !== undefined
(
input.witness === false ||
(input.witness === true && input.value !== undefined)
)
}

@@ -711,3 +711,9 @@

if (!canSign(input)) {
prepareInput(input, kpPubKey, redeemScript, witnessValue, witnessScript)
if (witnessValue !== undefined) {
if (input.value !== undefined && input.value !== witnessValue) throw new Error('Input didn\'t match witnessValue')
typeforce(types.Satoshi, witnessValue)
input.value = witnessValue
}
if (!canSign(input)) prepareInput(input, kpPubKey, redeemScript, witnessValue, witnessScript)
if (!canSign(input)) throw Error(input.prevOutType + ' not supported')

@@ -714,0 +720,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc