@exodus/bitcoinjs-lib-forks 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/README.md +0 -2
- package/package.json +21 -31
- package/src/address.js +4 -4
- package/src/ecsignature.js +23 -90
- package/src/index.js +4 -20
- package/src/script.js +2 -2
- package/src/templates/multisig/input.js +1 -1
- package/src/templates/witnesscommitment/output.js +1 -1
- package/src/transaction.js +8 -8
- package/src/transaction_builder.js +0 -92
- package/src/types.js +0 -8
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
## [0.0.7](https://github.com/ExodusMovement/assets/compare/@exodus/bitcoinjs-lib-forks@0.0.6...@exodus/bitcoinjs-lib-forks@0.0.7) (2026-11-14)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* clean up deps ([30d1c23](https://github.com/ExodusMovement/assets/commit/30d1c230e67653a7d0320cb84560d04befa503a1))
|
|
12
|
+
* declare bs58check as a runtime dependency ([5028356](https://github.com/ExodusMovement/assets/commit/50283562ca3ed194fa940802729fad68ffb618df))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# 0.0.3 / 2019-03-19
|
|
17
|
+
|
|
18
|
+
- Use `ripemd160` instead of `rmd160` for Electron v4
|
|
19
|
+
|
|
20
|
+
# 0.0.2 / 2018-12-12
|
|
21
|
+
|
|
22
|
+
- Remove unneeded BCH fork changes
|
|
23
|
+
|
|
24
|
+
# 0.0.1 / 2018-11-14
|
|
25
|
+
|
|
26
|
+
Initial release, with 2018-11-15 BCH (BitcoinABC) HF support.
|
package/README.md
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# BitcoinJS Forks (`@exodus/bitcoinjs-lib-forks`)
|
|
2
2
|
|
|
3
|
-
[](https://travis-ci.com/ExodusMovement/bitcoinjs-lib-forks)
|
|
4
|
-
|
|
5
3
|
`bitcoinjs-lib` forked to support Bitcoin Cash & Bitcoin Gold. Based off of https://github.com/bitcoinjs/bitcoinjs-lib/pull/831. Original README below.
|
|
6
4
|
|
|
7
5
|
# BitcoinJS (bitcoinjs-lib)
|
package/package.json
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/bitcoinjs-lib-forks",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "Client-side Bitcoin Forks JavaScript library",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
5
|
+
"author": "Exodus Movement, Inc.",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/ExodusMovement/assets.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/ExodusMovement/assets/tree/master/bitcoin/bitcoinjs-lib-forks",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/ExodusMovement/assets/issues?q=is%3Aissue+is%3Aopen+label%3Abitcoinjs-lib-forks"
|
|
8
14
|
},
|
|
15
|
+
"main": "./src/index.js",
|
|
9
16
|
"keywords": [
|
|
10
17
|
"bitcoinjs",
|
|
11
18
|
"bitcoin",
|
|
@@ -16,49 +23,32 @@
|
|
|
16
23
|
"cash",
|
|
17
24
|
"gold"
|
|
18
25
|
],
|
|
26
|
+
"files": [
|
|
27
|
+
"src",
|
|
28
|
+
"CHANGELOG.md"
|
|
29
|
+
],
|
|
19
30
|
"scripts": {
|
|
20
|
-
"coverage-report": "nyc report --reporter=lcov",
|
|
21
|
-
"coverage-html": "nyc report --reporter=html",
|
|
22
|
-
"coverage": "nyc --check-coverage --branches 90 --functions 90 mocha",
|
|
23
|
-
"integration": "mocha test/integration/",
|
|
24
|
-
"standard": "standard",
|
|
25
|
-
"test": "npm run standard && npm run coverage",
|
|
26
31
|
"unit": "mocha"
|
|
27
32
|
},
|
|
28
|
-
"repository": {
|
|
29
|
-
"type": "git",
|
|
30
|
-
"url": "https://github.com/bitcoinjs/bitcoinjs-lib.git"
|
|
31
|
-
},
|
|
32
|
-
"files": [
|
|
33
|
-
"src"
|
|
34
|
-
],
|
|
35
33
|
"dependencies": {
|
|
34
|
+
"@exodus/bytes": "^1.16.0",
|
|
36
35
|
"@exodus/crypto": "^1.0.0-rc.27",
|
|
37
36
|
"bech32": "0.0.3",
|
|
38
|
-
"bigi": "^1.4.0",
|
|
39
37
|
"bip66": "^1.1.0",
|
|
40
38
|
"bitcoin-ops": "^1.3.0",
|
|
41
39
|
"pushdata-bitcoin": "^1.0.1",
|
|
42
|
-
"safe-buffer": "^5.0.1",
|
|
43
40
|
"typeforce": "^1.11.3",
|
|
44
41
|
"varuint-bitcoin": "^1.0.4"
|
|
45
42
|
},
|
|
46
43
|
"devDependencies": {
|
|
47
|
-
"async": "^2.0.1",
|
|
48
|
-
"bip39": "^2.3.0",
|
|
49
44
|
"bs58": "^4.0.0",
|
|
50
|
-
"cb-http-client": "^0.2.0",
|
|
51
|
-
"coinselect": "^3.1.1",
|
|
52
|
-
"dhttp": "^2.3.5",
|
|
53
|
-
"ecurve": "^1.0.0",
|
|
54
45
|
"minimaldata": "^1.0.2",
|
|
55
46
|
"mocha": "^3.1.0",
|
|
56
|
-
"nyc": "^10.2.0",
|
|
57
|
-
"proxyquire": "^1.4.0",
|
|
58
|
-
"randombytes": "^2.0.1",
|
|
59
|
-
"sinon": "^1.12.2",
|
|
60
|
-
"standard": "^9.0.2",
|
|
61
47
|
"wif": "^2.0.1"
|
|
62
48
|
},
|
|
63
|
-
"
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public",
|
|
51
|
+
"provenance": false
|
|
52
|
+
},
|
|
53
|
+
"gitHead": "96212d9be9fa026ff8b683785a01c9892b610dd8"
|
|
64
54
|
}
|
package/src/address.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
const { fromBase58checkSync, toBase58checkSync } = require('@exodus/bytes/base58check.js')
|
|
1
2
|
var bech32 = require('bech32')
|
|
2
|
-
var bs58check = require('bs58check')
|
|
3
3
|
var bscript = require('./script')
|
|
4
4
|
var btemplates = require('./templates')
|
|
5
5
|
var networks = require('./networks')
|
|
@@ -7,7 +7,7 @@ var typeforce = require('typeforce')
|
|
|
7
7
|
var types = require('./types')
|
|
8
8
|
|
|
9
9
|
function fromBase58Check (address) {
|
|
10
|
-
|
|
10
|
+
const payload = fromBase58checkSync(address, 'buffer')
|
|
11
11
|
|
|
12
12
|
// TODO: 4.0.0, move to "toOutputScript"
|
|
13
13
|
if (payload.length < 21) throw new TypeError(address + ' is too short')
|
|
@@ -33,11 +33,11 @@ function fromBech32 (address) {
|
|
|
33
33
|
function toBase58Check (hash, version) {
|
|
34
34
|
typeforce(types.tuple(types.Hash160bit, types.UInt8), arguments)
|
|
35
35
|
|
|
36
|
-
var payload = Buffer.
|
|
36
|
+
var payload = Buffer.alloc(21)
|
|
37
37
|
payload.writeUInt8(version, 0)
|
|
38
38
|
hash.copy(payload, 1)
|
|
39
39
|
|
|
40
|
-
return
|
|
40
|
+
return toBase58checkSync(payload)
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
function toBech32 (data, version, prefix) {
|
package/src/ecsignature.js
CHANGED
|
@@ -2,99 +2,32 @@ var bip66 = require('bip66')
|
|
|
2
2
|
var typeforce = require('typeforce')
|
|
3
3
|
var types = require('./types')
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
var flagByte = buffer.readUInt8(0) - 27
|
|
18
|
-
if (flagByte !== (flagByte & 7)) throw new Error('Invalid signature parameter')
|
|
19
|
-
|
|
20
|
-
var compressed = !!(flagByte & 4)
|
|
21
|
-
var recoveryParam = flagByte & 3
|
|
22
|
-
var signature = ECSignature.fromRSBuffer(buffer.slice(1))
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
compressed: compressed,
|
|
26
|
-
i: recoveryParam,
|
|
27
|
-
signature: signature
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
ECSignature.fromRSBuffer = function (buffer) {
|
|
5
|
+
// Parses buffer as an unsigned integer and converts to DER
|
|
6
|
+
// minimum width but highest bit is 0 for unsigned
|
|
7
|
+
function toDer (buf) {
|
|
8
|
+
let start = 0
|
|
9
|
+
while (start < buf.length && buf[start] === 0) start++
|
|
10
|
+
if (start > 0) buf = buf.slice(start)
|
|
11
|
+
if (buf.length === 0) return Buffer.alloc(1)
|
|
12
|
+
const prefix = Buffer.alloc(buf[0] >= 0x80 ? 1 : 0)
|
|
13
|
+
return Buffer.concat([prefix, buf]) // copies
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
exports.fromRSBuffer = function (buffer) {
|
|
32
17
|
typeforce(types.BufferN(64), buffer)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
var s = BigInteger.fromBuffer(buffer.slice(32, 64))
|
|
36
|
-
return new ECSignature(r, s)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
ECSignature.fromDER = function (buffer) {
|
|
40
|
-
var decode = bip66.decode(buffer)
|
|
41
|
-
var r = BigInteger.fromDERInteger(decode.r)
|
|
42
|
-
var s = BigInteger.fromDERInteger(decode.s)
|
|
43
|
-
|
|
44
|
-
return new ECSignature(r, s)
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// BIP62: 1 byte hashType flag (only 0x01, 0x02, 0x03, 0x81, 0x82 and 0x83 are allowed)
|
|
48
|
-
ECSignature.parseScriptSignature = function (buffer) {
|
|
49
|
-
var hashType = buffer.readUInt8(buffer.length - 1)
|
|
50
|
-
var hashTypeMod = hashType & ~0xc0
|
|
51
|
-
|
|
52
|
-
if (hashTypeMod <= 0x00 || hashTypeMod >= 0x04) throw new Error('Invalid hashType ' + hashType)
|
|
18
|
+
const r = toDer(buffer.slice(0, 32))
|
|
19
|
+
const s = toDer(buffer.slice(32, 64))
|
|
53
20
|
|
|
54
21
|
return {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
ECSignature.prototype.toCompact = function (i, compressed) {
|
|
61
|
-
if (compressed) {
|
|
62
|
-
i += 4
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
i += 27
|
|
66
|
-
|
|
67
|
-
var buffer = Buffer.alloc(65)
|
|
68
|
-
buffer.writeUInt8(i, 0)
|
|
69
|
-
this.toRSBuffer(buffer, 1)
|
|
70
|
-
return buffer
|
|
71
|
-
}
|
|
22
|
+
toScriptSignature (hashType) {
|
|
23
|
+
var hashTypeMod = hashType & ~0xc0
|
|
24
|
+
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
|
|
72
25
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
var s = Buffer.from(this.s.toDERInteger())
|
|
26
|
+
var hashTypeBuffer = Buffer.alloc(1)
|
|
27
|
+
hashTypeBuffer.writeUInt8(hashType, 0)
|
|
76
28
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
ECSignature.prototype.toRSBuffer = function (buffer, offset, { canonical } = {}) {
|
|
82
|
-
buffer = buffer || Buffer.alloc(64)
|
|
83
|
-
this.r.toBuffer(32).copy(buffer, offset)
|
|
84
|
-
let s = this.s
|
|
85
|
-
if (canonical && s.compareTo(N) >= 0) s = N.subtract(s)
|
|
86
|
-
s.toBuffer(32).copy(buffer, offset + 32)
|
|
87
|
-
return buffer
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
ECSignature.prototype.toScriptSignature = function (hashType) {
|
|
91
|
-
var hashTypeMod = hashType & ~0xc0
|
|
92
|
-
if (hashTypeMod <= 0 || hashTypeMod >= 4) throw new Error('Invalid hashType ' + hashType)
|
|
93
|
-
|
|
94
|
-
var hashTypeBuffer = Buffer.alloc(1)
|
|
95
|
-
hashTypeBuffer.writeUInt8(hashType, 0)
|
|
96
|
-
|
|
97
|
-
return Buffer.concat([this.toDER(), hashTypeBuffer])
|
|
29
|
+
const der = bip66.encode(r, s)
|
|
30
|
+
return Buffer.concat([der, hashTypeBuffer])
|
|
31
|
+
}
|
|
32
|
+
}
|
|
98
33
|
}
|
|
99
|
-
|
|
100
|
-
module.exports = ECSignature
|
package/src/index.js
CHANGED
|
@@ -1,20 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
script[key] = templates[key]
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
module.exports = {
|
|
9
|
-
bufferutils: require('./bufferutils'), // TODO: remove in 4.0.0
|
|
10
|
-
|
|
11
|
-
ECSignature: require('./ecsignature'),
|
|
12
|
-
Transaction: require('./transaction'),
|
|
13
|
-
TransactionBuilder: require('./transaction_builder'),
|
|
14
|
-
|
|
15
|
-
address: require('./address'),
|
|
16
|
-
crypto: require('./crypto'),
|
|
17
|
-
networks: require('./networks'),
|
|
18
|
-
opcodes: require('bitcoin-ops'),
|
|
19
|
-
script: script
|
|
20
|
-
}
|
|
1
|
+
exports.Transaction = require('./transaction')
|
|
2
|
+
exports.TransactionBuilder = require('./transaction_builder')
|
|
3
|
+
exports.address = require('./address')
|
|
4
|
+
exports.networks = require('./networks')
|
package/src/script.js
CHANGED
|
@@ -51,7 +51,7 @@ function compile (chunks) {
|
|
|
51
51
|
return accum + 1
|
|
52
52
|
}, 0.0)
|
|
53
53
|
|
|
54
|
-
var buffer = Buffer.
|
|
54
|
+
var buffer = Buffer.alloc(bufferSize)
|
|
55
55
|
var offset = 0
|
|
56
56
|
|
|
57
57
|
chunks.forEach(function (chunk) {
|
|
@@ -162,7 +162,7 @@ function toStack (chunks) {
|
|
|
162
162
|
|
|
163
163
|
return chunks.map(function (op) {
|
|
164
164
|
if (Buffer.isBuffer(op)) return op
|
|
165
|
-
if (op === OPS.OP_0) return Buffer.
|
|
165
|
+
if (op === OPS.OP_0) return Buffer.alloc(0)
|
|
166
166
|
|
|
167
167
|
return scriptNumber.encode(op - OP_INT_BASE)
|
|
168
168
|
})
|
|
@@ -22,7 +22,7 @@ function check (script, allowIncomplete) {
|
|
|
22
22
|
}
|
|
23
23
|
check.toJSON = function () { return 'multisig input' }
|
|
24
24
|
|
|
25
|
-
var EMPTY_BUFFER = Buffer.
|
|
25
|
+
var EMPTY_BUFFER = Buffer.alloc(0)
|
|
26
26
|
|
|
27
27
|
function encodeStack (signatures, scriptPubKey) {
|
|
28
28
|
typeforce([partialSignature], signatures)
|
|
@@ -21,7 +21,7 @@ check.toJSON = function () { return 'Witness commitment output' }
|
|
|
21
21
|
function encode (commitment) {
|
|
22
22
|
typeforce(types.Hash256bit, commitment)
|
|
23
23
|
|
|
24
|
-
var buffer = Buffer.
|
|
24
|
+
var buffer = Buffer.alloc(36)
|
|
25
25
|
HEADER.copy(buffer, 0)
|
|
26
26
|
commitment.copy(buffer, 4)
|
|
27
27
|
|
package/src/transaction.js
CHANGED
|
@@ -38,7 +38,7 @@ Transaction.ADVANCED_TRANSACTION_FLAG = 0x01
|
|
|
38
38
|
Transaction.FORKID_BTG = 0x4F // 79
|
|
39
39
|
Transaction.FORKID_BCH = 0x00
|
|
40
40
|
|
|
41
|
-
var EMPTY_SCRIPT = Buffer.
|
|
41
|
+
var EMPTY_SCRIPT = Buffer.alloc(0)
|
|
42
42
|
var EMPTY_WITNESS = []
|
|
43
43
|
var ZERO = Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
|
|
44
44
|
var ONE = Buffer.from('0000000000000000000000000000000000000000000000000000000000000001', 'hex')
|
|
@@ -311,7 +311,7 @@ Transaction.prototype.hashForSignature = function (inIndex, prevOutScript, hashT
|
|
|
311
311
|
}
|
|
312
312
|
|
|
313
313
|
// serialize and hash
|
|
314
|
-
var buffer = Buffer.
|
|
314
|
+
var buffer = Buffer.alloc(txTmp.__byteLength(false) + 4)
|
|
315
315
|
buffer.writeInt32LE(hashType, buffer.length - 4)
|
|
316
316
|
txTmp.__toBuffer(buffer, 0, false)
|
|
317
317
|
|
|
@@ -336,7 +336,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
|
|
|
336
336
|
var hashSequence = ZERO
|
|
337
337
|
|
|
338
338
|
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY)) {
|
|
339
|
-
tbuffer = Buffer.
|
|
339
|
+
tbuffer = Buffer.alloc(36 * this.ins.length)
|
|
340
340
|
toffset = 0
|
|
341
341
|
|
|
342
342
|
this.ins.forEach(function (txIn) {
|
|
@@ -350,7 +350,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
|
|
|
350
350
|
if (!(hashType & Transaction.SIGHASH_ANYONECANPAY) &&
|
|
351
351
|
(hashType & 0x1f) !== Transaction.SIGHASH_SINGLE &&
|
|
352
352
|
(hashType & 0x1f) !== Transaction.SIGHASH_NONE) {
|
|
353
|
-
tbuffer = Buffer.
|
|
353
|
+
tbuffer = Buffer.alloc(4 * this.ins.length)
|
|
354
354
|
toffset = 0
|
|
355
355
|
|
|
356
356
|
this.ins.forEach(function (txIn) {
|
|
@@ -366,7 +366,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
|
|
|
366
366
|
return sum + 8 + varSliceSize(output.script)
|
|
367
367
|
}, 0)
|
|
368
368
|
|
|
369
|
-
tbuffer = Buffer.
|
|
369
|
+
tbuffer = Buffer.alloc(txOutsSize)
|
|
370
370
|
toffset = 0
|
|
371
371
|
|
|
372
372
|
this.outs.forEach(function (out) {
|
|
@@ -378,7 +378,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
|
|
|
378
378
|
} else if ((hashType & 0x1f) === Transaction.SIGHASH_SINGLE && inIndex < this.outs.length) {
|
|
379
379
|
var output = this.outs[inIndex]
|
|
380
380
|
|
|
381
|
-
tbuffer = Buffer.
|
|
381
|
+
tbuffer = Buffer.alloc(8 + varSliceSize(output.script))
|
|
382
382
|
toffset = 0
|
|
383
383
|
writeUInt64(output.value)
|
|
384
384
|
writeVarSlice(output.script)
|
|
@@ -386,7 +386,7 @@ Transaction.prototype.hashForWitnessV0 = function (inIndex, prevOutScript, value
|
|
|
386
386
|
hashOutputs = bcrypto.hash256(tbuffer)
|
|
387
387
|
}
|
|
388
388
|
|
|
389
|
-
tbuffer = Buffer.
|
|
389
|
+
tbuffer = Buffer.alloc(156 + varSliceSize(prevOutScript))
|
|
390
390
|
toffset = 0
|
|
391
391
|
|
|
392
392
|
var input = this.ins[inIndex]
|
|
@@ -470,7 +470,7 @@ Transaction.prototype.toBuffer = function (buffer, initialOffset) {
|
|
|
470
470
|
}
|
|
471
471
|
|
|
472
472
|
Transaction.prototype.__toBuffer = function (buffer, initialOffset, __allowWitness) {
|
|
473
|
-
if (!buffer) buffer = Buffer.
|
|
473
|
+
if (!buffer) buffer = Buffer.alloc(this.__byteLength(__allowWitness))
|
|
474
474
|
|
|
475
475
|
var offset = initialOffset || 0
|
|
476
476
|
function writeSlice (slice) { offset += slice.copy(buffer, offset) }
|
|
@@ -12,7 +12,6 @@ var P2SH = SIGNABLE.concat([btemplates.types.P2WPKH, btemplates.types.P2WSH])
|
|
|
12
12
|
|
|
13
13
|
var ECSignature = require('./ecsignature')
|
|
14
14
|
var Transaction = require('./transaction')
|
|
15
|
-
const { ecdsaVerifyHashSync } = require('@exodus/crypto/secp256k1')
|
|
16
15
|
|
|
17
16
|
function supportedType (type) {
|
|
18
17
|
return SIGNABLE.indexOf(type) !== -1
|
|
@@ -172,59 +171,6 @@ function expandInput (scriptSig, witnessStack) {
|
|
|
172
171
|
return result
|
|
173
172
|
}
|
|
174
173
|
|
|
175
|
-
// could be done in expandInput, but requires the original Transaction for hashForSignature
|
|
176
|
-
function fixMultisigOrder (input, transaction, vin, value, forkId) {
|
|
177
|
-
if (input.redeemScriptType !== scriptTypes.MULTISIG || !input.redeemScript) return
|
|
178
|
-
if (input.pubKeys.length === input.signatures.length) return
|
|
179
|
-
|
|
180
|
-
var unmatched = input.signatures.concat()
|
|
181
|
-
|
|
182
|
-
input.signatures = input.pubKeys.map(function (pubKey) {
|
|
183
|
-
var match
|
|
184
|
-
|
|
185
|
-
// check for a signature
|
|
186
|
-
unmatched.some(function (signature, i) {
|
|
187
|
-
// skip if undefined || OP_0
|
|
188
|
-
if (!signature) return false
|
|
189
|
-
|
|
190
|
-
// TODO: avoid O(n) hashForSignature
|
|
191
|
-
var parsed = ECSignature.parseScriptSignature(signature)
|
|
192
|
-
var hash
|
|
193
|
-
switch (forkId) {
|
|
194
|
-
case Transaction.FORKID_BCH:
|
|
195
|
-
hash = transaction.hashForCashSignature(vin, input.signScript, value, parsed.hashType)
|
|
196
|
-
break
|
|
197
|
-
case Transaction.FORKID_BTG:
|
|
198
|
-
hash = transaction.hashForGoldSignature(vin, input.signScript, value, parsed.hashType)
|
|
199
|
-
break
|
|
200
|
-
default:
|
|
201
|
-
if (input.witness) {
|
|
202
|
-
hash = transaction.hashForWitnessV0(vin, input.signScript, value, parsed.hashType)
|
|
203
|
-
} else {
|
|
204
|
-
hash = transaction.hashForSignature(vin, input.signScript, parsed.hashType)
|
|
205
|
-
}
|
|
206
|
-
break
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// skip if signature does not match pubKey
|
|
210
|
-
// Allow non-normalized though
|
|
211
|
-
if (!ecdsaVerifyHashSync({
|
|
212
|
-
signature: parsed.signature.toRSBuffer(null, 0, { canonical: true }),
|
|
213
|
-
hash,
|
|
214
|
-
publicKey: pubKey
|
|
215
|
-
})) return false
|
|
216
|
-
|
|
217
|
-
// remove matched signature from unmatched
|
|
218
|
-
unmatched[i] = undefined
|
|
219
|
-
match = signature
|
|
220
|
-
|
|
221
|
-
return true
|
|
222
|
-
})
|
|
223
|
-
|
|
224
|
-
return match
|
|
225
|
-
})
|
|
226
|
-
}
|
|
227
|
-
|
|
228
174
|
function expandOutput (script, scriptType, ourPubKey) {
|
|
229
175
|
typeforce(types.Buffer, script)
|
|
230
176
|
|
|
@@ -533,44 +479,6 @@ TransactionBuilder.prototype.setVersion = function (version) {
|
|
|
533
479
|
this.tx.version = version
|
|
534
480
|
}
|
|
535
481
|
|
|
536
|
-
TransactionBuilder.fromTransaction = function (transaction, network, forkId) {
|
|
537
|
-
var txb = new TransactionBuilder(network)
|
|
538
|
-
|
|
539
|
-
if (typeof forkId === 'number') {
|
|
540
|
-
if (forkId === Transaction.FORKID_BTG) {
|
|
541
|
-
txb.enableBitcoinGold(true)
|
|
542
|
-
} else if (forkId === Transaction.FORKID_BCH) {
|
|
543
|
-
txb.enableBitcoinCash(true)
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
// Copy transaction fields
|
|
548
|
-
txb.setVersion(transaction.version)
|
|
549
|
-
txb.setLockTime(transaction.locktime)
|
|
550
|
-
|
|
551
|
-
// Copy outputs (done first to avoid signature invalidation)
|
|
552
|
-
transaction.outs.forEach(function (txOut) {
|
|
553
|
-
txb.addOutput(txOut.script, txOut.value)
|
|
554
|
-
})
|
|
555
|
-
|
|
556
|
-
// Copy inputs
|
|
557
|
-
transaction.ins.forEach(function (txIn) {
|
|
558
|
-
txb.__addInputUnsafe(txIn.hash, txIn.index, {
|
|
559
|
-
sequence: txIn.sequence,
|
|
560
|
-
script: txIn.script,
|
|
561
|
-
witness: txIn.witness,
|
|
562
|
-
value: txIn.value
|
|
563
|
-
})
|
|
564
|
-
})
|
|
565
|
-
|
|
566
|
-
// fix some things not possible through the public API
|
|
567
|
-
txb.inputs.forEach(function (input, i) {
|
|
568
|
-
fixMultisigOrder(input, transaction, i, input.value, forkId)
|
|
569
|
-
})
|
|
570
|
-
|
|
571
|
-
return txb
|
|
572
|
-
}
|
|
573
|
-
|
|
574
482
|
TransactionBuilder.prototype.addInput = function (txHash, vout, sequence, prevOutScript) {
|
|
575
483
|
if (!this.__canModifyInputs()) {
|
|
576
484
|
throw new Error('No, this would invalidate signatures')
|
package/src/types.js
CHANGED
|
@@ -15,12 +15,7 @@ function Satoshi (value) {
|
|
|
15
15
|
return typeforce.UInt53(value) && value <= SATOSHI_MAX
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
// external dependent types
|
|
19
|
-
var BigInt = typeforce.quacksLike('BigInteger')
|
|
20
|
-
var ECPoint = typeforce.quacksLike('Point')
|
|
21
|
-
|
|
22
18
|
// exposed, external API
|
|
23
|
-
var ECSignature = typeforce.compile({ r: BigInt, s: BigInt })
|
|
24
19
|
var Network = typeforce.compile({
|
|
25
20
|
messagePrefix: typeforce.oneOf(typeforce.Buffer, typeforce.String),
|
|
26
21
|
bip32: {
|
|
@@ -34,11 +29,8 @@ var Network = typeforce.compile({
|
|
|
34
29
|
|
|
35
30
|
// extend typeforce types with ours
|
|
36
31
|
var types = {
|
|
37
|
-
BigInt: BigInt,
|
|
38
32
|
BIP32Path: BIP32Path,
|
|
39
33
|
Buffer256bit: typeforce.BufferN(32),
|
|
40
|
-
ECPoint: ECPoint,
|
|
41
|
-
ECSignature: ECSignature,
|
|
42
34
|
Hash160bit: typeforce.BufferN(20),
|
|
43
35
|
Hash256bit: typeforce.BufferN(32),
|
|
44
36
|
Network: Network,
|