Skip to content

Commit 8a07708

Browse files
author
Arik Sosman
committed
update state to the latest from bitcoinjs-lib
1 parent 415689c commit 8a07708

33 files changed

+1128
-692
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# 3.2.0
2+
__added__
3+
- Added `address.fromBech32/toBech32` (#846)
4+
5+
# 3.1.0
6+
__added__
7+
- Added `Transaction.prototype.virtualSize` (#811)
8+
- Added `Transaction.prototype.weight` (#811)
9+
110
# 3.0.0
211
From this release users can expect out-of-the-box Segregated Witness support.
312
The majority of breaking changes have been in how `script` encoding/decoding occurs, with the introduction of witness stacks.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2011-2016 bitcoinjs-lib contributors
3+
Copyright (c) 2011-2017 bitcoinjs-lib contributors
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 87 additions & 76 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,17 @@
11
{
22
"name": "bitcoinjs-lib",
3-
"version": "3.1.2",
3+
"version": "3.3.0",
44
"description": "Client-side Bitcoin JavaScript library",
55
"main": "./src/index.js",
66
"engines": {
77
"node": ">=4.0.0"
88
},
99
"keywords": [
10+
"bitcoinjs",
1011
"bitcoin",
11-
"browser",
12-
"client",
13-
"library"
14-
],
15-
"contributors": [
16-
{
17-
"name": "Daniel Cousens",
18-
"email": "[email protected]",
19-
"url": "http://dcousens.com"
20-
},
21-
{
22-
"name": "Kyle Drake",
23-
"email": "[email protected]",
24-
"url": "http://kyledrake.net/"
25-
},
26-
{
27-
"name": "Wei Lu",
28-
"email": "[email protected]",
29-
"url": "http://weilu.github.io/"
30-
},
31-
{
32-
"name": "Stefan Thomas",
33-
"email": "[email protected]",
34-
"url": "http://www.justmoon.net"
35-
}
12+
"browserify",
13+
"javascript",
14+
"bitcoinjs"
3615
],
3716
"scripts": {
3817
"coverage-report": "nyc report --reporter=lcov",
@@ -51,6 +30,7 @@
5130
"src"
5231
],
5332
"dependencies": {
33+
"bech32": "0.0.3",
5434
"bigi": "^1.4.0",
5535
"bip66": "^1.1.0",
5636
"bitcoin-ops": "^1.3.0",
@@ -62,7 +42,7 @@
6242
"pushdata-bitcoin": "^1.0.1",
6343
"randombytes": "^2.0.1",
6444
"safe-buffer": "^5.0.1",
65-
"typeforce": "^1.8.7",
45+
"typeforce": "^1.11.3",
6646
"varuint-bitcoin": "^1.0.4",
6747
"wif": "^2.0.1"
6848
},
@@ -72,6 +52,7 @@
7252
"bs58": "^4.0.0",
7353
"cb-http-client": "^0.2.0",
7454
"coinselect": "^3.1.1",
55+
"dhttp": "^2.3.5",
7556
"minimaldata": "^1.0.2",
7657
"mocha": "^3.1.0",
7758
"nyc": "^10.2.0",

src/address.js

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
var Buffer = require('safe-buffer').Buffer
2+
var bech32 = require('bech32')
23
var bs58check = require('bs58check')
34
var bscript = require('./script')
5+
var btemplates = require('./templates')
46
var networks = require('./networks')
57
var typeforce = require('typeforce')
68
var types = require('./types')
79

810
function fromBase58Check (address) {
911
var payload = bs58check.decode(address)
12+
13+
// TODO: 4.0.0, move to "toOutputScript"
1014
if (payload.length < 21) throw new TypeError(address + ' is too short')
1115
if (payload.length > 21) throw new TypeError(address + ' is too long')
1216

1317
var version = payload.readUInt8(0)
1418
var hash = payload.slice(1)
1519

16-
return { hash: hash, version: version }
20+
return { version: version, hash: hash }
21+
}
22+
23+
function fromBech32 (address) {
24+
var result = bech32.decode(address)
25+
var data = bech32.fromWords(result.words.slice(1))
26+
27+
return {
28+
version: result.words[0],
29+
prefix: result.prefix,
30+
data: Buffer.from(data)
31+
}
1732
}
1833

1934
function toBase58Check (hash, version) {
@@ -26,28 +41,57 @@ function toBase58Check (hash, version) {
2641
return bs58check.encode(payload)
2742
}
2843

44+
function toBech32 (data, version, prefix) {
45+
var words = bech32.toWords(data)
46+
words.unshift(version)
47+
48+
return bech32.encode(prefix, words)
49+
}
50+
2951
function fromOutputScript (outputScript, network) {
3052
network = network || networks.bitcoin
3153

32-
if (bscript.pubKeyHash.output.check(outputScript)) return toBase58Check(bscript.compile(outputScript).slice(3, 23), network.pubKeyHash)
33-
if (bscript.scriptHash.output.check(outputScript)) return toBase58Check(bscript.compile(outputScript).slice(2, 22), network.scriptHash)
54+
if (btemplates.pubKeyHash.output.check(outputScript)) return toBase58Check(bscript.compile(outputScript).slice(3, 23), network.pubKeyHash)
55+
if (btemplates.scriptHash.output.check(outputScript)) return toBase58Check(bscript.compile(outputScript).slice(2, 22), network.scriptHash)
56+
if (btemplates.witnessPubKeyHash.output.check(outputScript)) return toBech32(bscript.compile(outputScript).slice(2, 22), 0, network.bech32)
57+
if (btemplates.witnessScriptHash.output.check(outputScript)) return toBech32(bscript.compile(outputScript).slice(2, 34), 0, network.bech32)
3458

3559
throw new Error(bscript.toASM(outputScript) + ' has no matching Address')
3660
}
3761

3862
function toOutputScript (address, network) {
3963
network = network || networks.bitcoin
4064

41-
var decode = fromBase58Check(address)
42-
if (decode.version === network.pubKeyHash) return bscript.pubKeyHash.output.encode(decode.hash)
43-
if (decode.version === network.scriptHash) return bscript.scriptHash.output.encode(decode.hash)
65+
var decode
66+
try {
67+
decode = fromBase58Check(address)
68+
} catch (e) {}
69+
70+
if (decode) {
71+
if (decode.version === network.pubKeyHash) return btemplates.pubKeyHash.output.encode(decode.hash)
72+
if (decode.version === network.scriptHash) return btemplates.scriptHash.output.encode(decode.hash)
73+
} else {
74+
try {
75+
decode = fromBech32(address)
76+
} catch (e) {}
77+
78+
if (decode) {
79+
if (decode.prefix !== network.bech32) throw new Error(address + ' has an invalid prefix')
80+
if (decode.version === 0) {
81+
if (decode.data.length === 20) return btemplates.witnessPubKeyHash.output.encode(decode.data)
82+
if (decode.data.length === 32) return btemplates.witnessScriptHash.output.encode(decode.data)
83+
}
84+
}
85+
}
4486

4587
throw new Error(address + ' has no matching Script')
4688
}
4789

4890
module.exports = {
4991
fromBase58Check: fromBase58Check,
92+
fromBech32: fromBech32,
5093
fromOutputScript: fromOutputScript,
5194
toBase58Check: toBase58Check,
95+
toBech32: toBech32,
5296
toOutputScript: toOutputScript
5397
}

src/ecsignature.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,30 @@ function ECSignature (r, s) {
1212
}
1313

1414
ECSignature.parseCompact = function (buffer) {
15-
if (buffer.length !== 65) throw new Error('Invalid signature length')
15+
typeforce(types.BufferN(65), buffer)
1616

1717
var flagByte = buffer.readUInt8(0) - 27
1818
if (flagByte !== (flagByte & 7)) throw new Error('Invalid signature parameter')
1919

2020
var compressed = !!(flagByte & 4)
2121
var recoveryParam = flagByte & 3
22-
23-
var r = BigInteger.fromBuffer(buffer.slice(1, 33))
24-
var s = BigInteger.fromBuffer(buffer.slice(33))
22+
var signature = ECSignature.fromRSBuffer(buffer.slice(1))
2523

2624
return {
2725
compressed: compressed,
2826
i: recoveryParam,
29-
signature: new ECSignature(r, s)
27+
signature: signature
3028
}
3129
}
3230

31+
ECSignature.fromRSBuffer = function (buffer) {
32+
typeforce(types.BufferN(64), buffer)
33+
34+
var r = BigInteger.fromBuffer(buffer.slice(0, 32))
35+
var s = BigInteger.fromBuffer(buffer.slice(32, 64))
36+
return new ECSignature(r, s)
37+
}
38+
3339
ECSignature.fromDER = function (buffer) {
3440
var decode = bip66.decode(buffer)
3541
var r = BigInteger.fromDERInteger(decode.r)
@@ -60,9 +66,7 @@ ECSignature.prototype.toCompact = function (i, compressed) {
6066

6167
var buffer = Buffer.alloc(65)
6268
buffer.writeUInt8(i, 0)
63-
this.r.toBuffer(32).copy(buffer, 1)
64-
this.s.toBuffer(32).copy(buffer, 33)
65-
69+
this.toRSBuffer(buffer, 1)
6670
return buffer
6771
}
6872

@@ -73,6 +77,13 @@ ECSignature.prototype.toDER = function () {
7377
return bip66.encode(r, s)
7478
}
7579

80+
ECSignature.prototype.toRSBuffer = function (buffer, offset) {
81+
buffer = buffer || Buffer.alloc(64)
82+
this.r.toBuffer(32).copy(buffer, offset)
83+
this.s.toBuffer(32).copy(buffer, offset + 32)
84+
return buffer
85+
}
86+
7687
ECSignature.prototype.toScriptSignature = function (hashType) {
7788
var hashTypeMod = hashType & ~0x80
7889
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)

src/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1+
var script = require('./script')
2+
3+
var templates = require('./templates')
4+
for (var key in templates) {
5+
script[key] = templates[key]
6+
}
7+
18
module.exports = {
9+
bufferutils: require('./bufferutils'), // TODO: remove in 4.0.0
10+
211
Block: require('./block'),
312
ECPair: require('./ecpair'),
413
ECSignature: require('./ecsignature'),
@@ -7,9 +16,8 @@ module.exports = {
716
TransactionBuilder: require('./transaction_builder'),
817

918
address: require('./address'),
10-
bufferutils: require('./bufferutils'), // TODO: remove in 4.0.0
1119
crypto: require('./crypto'),
1220
networks: require('./networks'),
1321
opcodes: require('bitcoin-ops'),
14-
script: require('./script')
22+
script: script
1523
}

src/networks.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module.exports = {
55
bitcoin: {
66
messagePrefix: '\x18Bitcoin Signed Message:\n',
7+
bech32: 'bc',
78
bip32: {
89
public: 0x0488b21e,
910
private: 0x0488ade4
@@ -14,6 +15,7 @@ module.exports = {
1415
},
1516
testnet: {
1617
messagePrefix: '\x18Bitcoin Signed Message:\n',
18+
bech32: 'tb',
1719
bip32: {
1820
public: 0x043587cf,
1921
private: 0x04358394

src/script.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ function isPushOnly (value) {
2424
return types.Array(value) && value.every(isPushOnlyChunk)
2525
}
2626

27+
function asMinimalOP (buffer) {
28+
if (buffer.length === 0) return OPS.OP_0
29+
if (buffer.length !== 1) return
30+
if (buffer[0] >= 1 && buffer[0] <= 16) return OP_INT_BASE + buffer[0]
31+
if (buffer[0] === 0x81) return OPS.OP_1NEGATE
32+
}
33+
2734
function compile (chunks) {
2835
// TODO: remove me
2936
if (Buffer.isBuffer(chunks)) return chunks
@@ -34,7 +41,7 @@ function compile (chunks) {
3441
// data chunk
3542
if (Buffer.isBuffer(chunk)) {
3643
// adhere to BIP62.3, minimal push policy
37-
if (chunk.length === 1 && (chunk[0] === 0x81 || (chunk[0] >= 1 && chunk[0] <= 16))) {
44+
if (chunk.length === 1 && asMinimalOP(chunk) !== undefined) {
3845
return accum + 1
3946
}
4047

@@ -52,21 +59,14 @@ function compile (chunks) {
5259
// data chunk
5360
if (Buffer.isBuffer(chunk)) {
5461
// adhere to BIP62.3, minimal push policy
55-
if (chunk.length === 1 && chunk[0] >= 1 && chunk[0] <= 16) {
56-
var opcode = OP_INT_BASE + chunk[0]
62+
var opcode = asMinimalOP(chunk)
63+
if (opcode !== undefined) {
5764
buffer.writeUInt8(opcode, offset)
5865
offset += 1
5966
return
6067
}
6168

62-
if (chunk.length === 1 && chunk[0] === 0x81) {
63-
buffer.writeUInt8(OPS.OP_1NEGATE, offset)
64-
offset += 1
65-
return
66-
}
67-
6869
offset += pushdata.encode(buffer, chunk.length, offset)
69-
7070
chunk.copy(buffer, offset)
7171
offset += chunk.length
7272

@@ -107,7 +107,13 @@ function decompile (buffer) {
107107
var data = buffer.slice(i, i + d.number)
108108
i += d.number
109109

110-
chunks.push(data)
110+
// decompile minimally
111+
var op = asMinimalOP(data)
112+
if (op !== undefined) {
113+
chunks.push(op)
114+
} else {
115+
chunks.push(data)
116+
}
111117

112118
// opcode
113119
} else {
@@ -127,7 +133,11 @@ function toASM (chunks) {
127133

128134
return chunks.map(function (chunk) {
129135
// data?
130-
if (Buffer.isBuffer(chunk)) return chunk.toString('hex')
136+
if (Buffer.isBuffer(chunk)) {
137+
var op = asMinimalOP(chunk)
138+
if (op === undefined) return chunk.toString('hex')
139+
chunk = op
140+
}
131141

132142
// opcode!
133143
return REVERSE_OPS[chunk]
@@ -202,8 +212,3 @@ module.exports = {
202212
isPushOnly: isPushOnly,
203213
isDefinedHashType: isDefinedHashType
204214
}
205-
206-
var templates = require('./templates')
207-
for (var key in templates) {
208-
module.exports[key] = templates[key]
209-
}

src/templates/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ function classifyInput (script, allowIncomplete) {
4141
var chunks = decompile(script)
4242

4343
if (pubKeyHash.input.check(chunks)) return types.P2PKH
44-
if (multisig.input.check(chunks, allowIncomplete)) return types.MULTISIG
4544
if (scriptHash.input.check(chunks, allowIncomplete)) return types.P2SH
45+
if (multisig.input.check(chunks, allowIncomplete)) return types.MULTISIG
4646
if (pubKey.input.check(chunks)) return types.P2PK
4747

4848
return types.NONSTANDARD

0 commit comments

Comments
 (0)