bsv2 2.1.0 → 2.1.1
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/dist/bsv2.mjs +1 -1
- package/package.json +3 -2
- package/.babel.test +0 -10
- package/.editorconfig +0 -4
- package/.env.defaults +0 -1
- package/.env.schema +0 -1
- package/.husky/pre-commit +0 -1
- package/.mocharc.yml +0 -4
- package/.nvmrc +0 -1
- package/.travis.yml +0 -7
- package/babel.config.cjs +0 -44
- package/benchmark.js +0 -42
- package/bin/bsv.js +0 -11
- package/bin/network.js +0 -93
- package/build/index.html +0 -20
- package/dist/bsv2.umd.js +0 -28
- package/doc/assets/anchor.js +0 -350
- package/doc/assets/bass-addons.css +0 -12
- package/doc/assets/bass.css +0 -544
- package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
- package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
- package/doc/assets/fonts/LICENSE.txt +0 -93
- package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
- package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
- package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
- package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
- package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
- package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
- package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
- package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
- package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
- package/doc/assets/fonts/source-code-pro.css +0 -23
- package/doc/assets/github.css +0 -123
- package/doc/assets/site.js +0 -168
- package/doc/assets/split.css +0 -15
- package/doc/assets/split.js +0 -782
- package/doc/assets/style.css +0 -147
- package/doc/index.html +0 -9461
- package/entry.js +0 -81
- package/eslint.config.mjs +0 -8
- package/index.js +0 -98
- package/karma.conf.ts +0 -77
- package/lib/ach.js +0 -59
- package/lib/address.js +0 -266
- package/lib/aes.js +0 -57
- package/lib/aescbc.js +0 -38
- package/lib/base-58-check.js +0 -83
- package/lib/base-58.js +0 -59
- package/lib/bip-32.js +0 -472
- package/lib/bip-39-en-wordlist.js +0 -2054
- package/lib/bip-39-en.js +0 -10
- package/lib/bip-39-es-wordlist.js +0 -2055
- package/lib/bip-39-es.js +0 -10
- package/lib/bip-39-jp-wordlist.js +0 -2053
- package/lib/bip-39-jp.js +0 -10
- package/lib/bip-39-words.js +0 -5
- package/lib/bip-39.js +0 -286
- package/lib/block-header.js +0 -79
- package/lib/block.js +0 -132
- package/lib/bn.js +0 -462
- package/lib/br.js +0 -172
- package/lib/bsm.js +0 -127
- package/lib/bw.js +0 -188
- package/lib/cbc.js +0 -140
- package/lib/cmp.js +0 -37
- package/lib/config.js +0 -50
- package/lib/constants.js +0 -150
- package/lib/ecdsa.js +0 -496
- package/lib/ecies.js +0 -151
- package/lib/hash-cache.js +0 -41
- package/lib/hash.js +0 -191
- package/lib/interp.js +0 -2017
- package/lib/inv.js +0 -49
- package/lib/key-pair.js +0 -133
- package/lib/mainnet-address.js +0 -5
- package/lib/mainnet-bip-32.js +0 -5
- package/lib/mainnet-key-pair.js +0 -5
- package/lib/mainnet-priv-key.js +0 -5
- package/lib/merkle.js +0 -92
- package/lib/msg.js +0 -184
- package/lib/op-code.js +0 -400
- package/lib/point.js +0 -134
- package/lib/priv-key.js +0 -157
- package/lib/pub-key.js +0 -230
- package/lib/random.js +0 -28
- package/lib/script.js +0 -828
- package/lib/sig-operations.js +0 -120
- package/lib/sig.js +0 -399
- package/lib/struct.js +0 -392
- package/lib/testnet-address.js +0 -5
- package/lib/testnet-bip-32.js +0 -5
- package/lib/testnet-key-pair.js +0 -5
- package/lib/testnet-priv-key.js +0 -5
- package/lib/tx-builder.js +0 -593
- package/lib/tx-in.js +0 -172
- package/lib/tx-out-map.js +0 -57
- package/lib/tx-out.js +0 -73
- package/lib/tx-verifier.js +0 -209
- package/lib/tx.js +0 -430
- package/lib/var-int.js +0 -73
- package/lib/version.js +0 -92
- package/lib/workers-result.js +0 -65
- package/lib/workers.js +0 -77
- package/load-babel.js +0 -5
- package/mtest/test.html +0 -17
- package/mtest/test.js +0 -2
- package/mtest/test.mjs +0 -2
- package/mtest/webpack.config.js +0 -115
- package/out/Sig.html +0 -541
- package/out/bip-32.js.html +0 -523
- package/out/bip-39.js.html +0 -336
- package/out/block.js.html +0 -183
- package/out/bn.js.html +0 -512
- package/out/ecdsa.js.html +0 -546
- package/out/fonts/OpenSans-Bold-webfont.eot +0 -0
- package/out/fonts/OpenSans-Bold-webfont.svg +0 -1830
- package/out/fonts/OpenSans-Bold-webfont.woff +0 -0
- package/out/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
- package/out/fonts/OpenSans-BoldItalic-webfont.svg +0 -1830
- package/out/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
- package/out/fonts/OpenSans-Italic-webfont.eot +0 -0
- package/out/fonts/OpenSans-Italic-webfont.svg +0 -1830
- package/out/fonts/OpenSans-Italic-webfont.woff +0 -0
- package/out/fonts/OpenSans-Light-webfont.eot +0 -0
- package/out/fonts/OpenSans-Light-webfont.svg +0 -1831
- package/out/fonts/OpenSans-Light-webfont.woff +0 -0
- package/out/fonts/OpenSans-LightItalic-webfont.eot +0 -0
- package/out/fonts/OpenSans-LightItalic-webfont.svg +0 -1835
- package/out/fonts/OpenSans-LightItalic-webfont.woff +0 -0
- package/out/fonts/OpenSans-Regular-webfont.eot +0 -0
- package/out/fonts/OpenSans-Regular-webfont.svg +0 -1831
- package/out/fonts/OpenSans-Regular-webfont.woff +0 -0
- package/out/global.html +0 -220
- package/out/index.html +0 -128
- package/out/interp.js.html +0 -2064
- package/out/merkle.js.html +0 -143
- package/out/msg.js.html +0 -235
- package/out/priv-key.js.html +0 -208
- package/out/pub-key.js.html +0 -281
- package/out/script.js.html +0 -876
- package/out/scripts/linenumber.js +0 -25
- package/out/scripts/prettify/Apache-License-2.0.txt +0 -202
- package/out/scripts/prettify/lang-css.js +0 -2
- package/out/scripts/prettify/prettify.js +0 -28
- package/out/sig-operations.js.html +0 -171
- package/out/sig.js.html +0 -450
- package/out/struct.js.html +0 -440
- package/out/styles/jsdoc-default.css +0 -358
- package/out/styles/prettify-jsdoc.css +0 -111
- package/out/styles/prettify-tomorrow.css +0 -132
- package/out/tx-builder.js.html +0 -644
- package/out/tx-in.js.html +0 -223
- package/out/tx-verifier.js.html +0 -260
- package/out/tx.js.html +0 -481
- package/test/ach.js +0 -172
- package/test/address.js +0 -464
- package/test/aes.js +0 -96
- package/test/aescbc.js +0 -65
- package/test/base-58-check.js +0 -128
- package/test/base-58.js +0 -113
- package/test/bip-32.js +0 -721
- package/test/bip-39.js +0 -282
- package/test/block-header.js +0 -152
- package/test/block.js +0 -243
- package/test/bn.js +0 -643
- package/test/br.js +0 -353
- package/test/bsm.js +0 -141
- package/test/bw.js +0 -292
- package/test/cbc.js +0 -335
- package/test/cmp.js +0 -56
- package/test/data/bip69.json +0 -215
- package/test/data/bitcoind/base58_keys_invalid.json +0 -152
- package/test/data/bitcoind/base58_keys_valid.json +0 -452
- package/test/data/bitcoind/blocks.json +0 -27
- package/test/data/bitcoind/script_tests.json +0 -2244
- package/test/data/bitcoind/sig_canonical.json +0 -7
- package/test/data/bitcoind/sig_noncanonical.json +0 -22
- package/test/data/bitcoind/tx_invalid.json +0 -177
- package/test/data/bitcoind/tx_valid.json +0 -224
- package/test/data/blk86756-testnet.dat +0 -0
- package/test/data/blk86756-testnet.js +0 -12
- package/test/data/blk86756-testnet.json +0 -684
- package/test/data/ecdsa.json +0 -230
- package/test/data/messages.json +0 -22
- package/test/data/sighash.json +0 -1004
- package/test/data/tx_creation.json +0 -85
- package/test/ecdsa.js +0 -748
- package/test/ecies.js +0 -238
- package/test/global.js +0 -13
- package/test/hash-cache.js +0 -52
- package/test/hash.js +0 -354
- package/test/index.js +0 -10
- package/test/interp.js +0 -512
- package/test/inv.js +0 -65
- package/test/key-pair.js +0 -243
- package/test/merkle.js +0 -179
- package/test/msg.js +0 -295
- package/test/op-code.js +0 -79
- package/test/point.js +0 -375
- package/test/priv-key.js +0 -264
- package/test/pub-key.js +0 -549
- package/test/random.js +0 -31
- package/test/script.js +0 -1340
- package/test/sig-operations.js +0 -149
- package/test/sig.js +0 -662
- package/test/struct.js +0 -552
- package/test/tx-builder.js +0 -2104
- package/test/tx-in.js +0 -198
- package/test/tx-out-map.js +0 -104
- package/test/tx-out.js +0 -162
- package/test/tx-verifier.js +0 -246
- package/test/tx.js +0 -539
- package/test/var-int.js +0 -132
- package/test/vectors/aes.json +0 -10392
- package/test/vectors/aescbc.json +0 -620
- package/test/vectors/bip39.json +0 -502
- package/test/vectors/bip69.json +0 -215
- package/test/vectors/bitcoin-abc/script_tests.json +0 -48
- package/test/vectors/bitcoin-abc/sighash.json +0 -7004
- package/test/vectors/bitcoin-sv/script_tests.json +0 -248
- package/test/vectors/bitcoind/script_invalid.json +0 -1151
- package/test/vectors/bitcoind/script_valid.json +0 -1462
- package/test/vectors/bitcoind/sighash.json +0 -3503
- package/test/vectors/bitcoind/tx_invalid.json +0 -883
- package/test/vectors/bitcoind/tx_valid.json +0 -1150
- package/test/vectors/coolest-tx-ever-sent.json +0 -8
- package/test/vectors/ecdsa.json +0 -300
- package/test/vectors/hash.json +0 -6133
- package/test/vectors/kdf.json +0 -47
- package/test/vectors/largesttx.json +0 -5
- package/test/vectors/largesttxblock.json +0 -6
- package/test/vectors/sig.json +0 -79
- package/test/vectors/sighash-single-bug.json +0 -8
- package/test/version.js +0 -81
- package/test/workers-result.js +0 -89
- package/test/workers.js +0 -91
- package/test-index.js +0 -3
- package/vite.config.js +0 -36
package/test/tx-builder.js
DELETED
|
@@ -1,2104 +0,0 @@
|
|
|
1
|
-
/* global describe,it,before */
|
|
2
|
-
'use strict'
|
|
3
|
-
import { Address } from '../lib/address'
|
|
4
|
-
import { Bn } from '../lib/bn'
|
|
5
|
-
import { Interp } from '../lib/interp'
|
|
6
|
-
import { KeyPair } from '../lib/key-pair'
|
|
7
|
-
import { PrivKey } from '../lib/priv-key'
|
|
8
|
-
import { PubKey } from '../lib/pub-key'
|
|
9
|
-
import { Script } from '../lib/script'
|
|
10
|
-
import { Sig } from '../lib/sig'
|
|
11
|
-
import { Tx } from '../lib/tx'
|
|
12
|
-
import { TxBuilder } from '../lib/tx-builder'
|
|
13
|
-
import { TxOut } from '../lib/tx-out'
|
|
14
|
-
import { TxOutMap } from '../lib/tx-out-map'
|
|
15
|
-
import { TxVerifier } from '../lib/tx-verifier'
|
|
16
|
-
import should from 'should'
|
|
17
|
-
import sinon from 'sinon'
|
|
18
|
-
|
|
19
|
-
describe('TxBuilder', function () {
|
|
20
|
-
it('should make a new txbuilder', function () {
|
|
21
|
-
let txb = new TxBuilder()
|
|
22
|
-
;(txb instanceof TxBuilder).should.equal(true)
|
|
23
|
-
should.exist(txb.tx)
|
|
24
|
-
txb = new TxBuilder()
|
|
25
|
-
;(txb instanceof TxBuilder).should.equal(true)
|
|
26
|
-
should.exist(txb.tx)
|
|
27
|
-
txb = new TxBuilder({
|
|
28
|
-
tx: new Tx()
|
|
29
|
-
})
|
|
30
|
-
should.exist(txb.tx)
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
function prepareTxBuilder () {
|
|
34
|
-
const txb = new TxBuilder()
|
|
35
|
-
|
|
36
|
-
// make change address
|
|
37
|
-
const privKey = new PrivKey().fromBn(new Bn(1))
|
|
38
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
39
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
40
|
-
|
|
41
|
-
// make addresses to send from
|
|
42
|
-
const privKey1 = new PrivKey().fromBn(new Bn(2))
|
|
43
|
-
const keyPair1 = new KeyPair().fromPrivKey(privKey1)
|
|
44
|
-
const addr1 = new Address().fromPubKey(keyPair1.pubKey)
|
|
45
|
-
|
|
46
|
-
const privKey2 = new PrivKey().fromBn(new Bn(3))
|
|
47
|
-
const keyPair2 = new KeyPair().fromPrivKey(privKey2)
|
|
48
|
-
const addr2 = new Address().fromPubKey(keyPair2.pubKey)
|
|
49
|
-
|
|
50
|
-
// make addresses to send to
|
|
51
|
-
const saddr1 = addr1
|
|
52
|
-
|
|
53
|
-
// txOuts that we are spending
|
|
54
|
-
|
|
55
|
-
// pubKeyHash out
|
|
56
|
-
const scriptout1 = new Script().fromString(
|
|
57
|
-
'OP_DUP OP_HASH160 20 0x' +
|
|
58
|
-
addr1.hashBuf.toString('hex') +
|
|
59
|
-
' OP_EQUALVERIFY OP_CHECKSIG'
|
|
60
|
-
)
|
|
61
|
-
|
|
62
|
-
// pubKeyHash out
|
|
63
|
-
const scriptout2 = new Script().fromString(
|
|
64
|
-
'OP_DUP OP_HASH160 20 0x' +
|
|
65
|
-
addr2.hashBuf.toString('hex') +
|
|
66
|
-
' OP_EQUALVERIFY OP_CHECKSIG'
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
const txOut1 = TxOut.fromProperties(new Bn(1e8), scriptout1)
|
|
70
|
-
const txOut2 = TxOut.fromProperties(new Bn(1e8), scriptout2)
|
|
71
|
-
// total balance: 2e8
|
|
72
|
-
|
|
73
|
-
const txHashBuf = Buffer.alloc(32)
|
|
74
|
-
txHashBuf.fill(0)
|
|
75
|
-
const txOutNum1 = 0
|
|
76
|
-
const txOutNum2 = 1
|
|
77
|
-
|
|
78
|
-
txb.setFeePerKbNum(0.0001e8)
|
|
79
|
-
txb.setChangeAddress(changeaddr)
|
|
80
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum1, txOut1, keyPair1.pubKey)
|
|
81
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum2, txOut2, keyPair2.pubKey)
|
|
82
|
-
txb.outputToAddress(new Bn(1.5e8), saddr1) // pubKeyHash address
|
|
83
|
-
// total sending: 2e8 (plus fee)
|
|
84
|
-
// txb.randomizeInputs()
|
|
85
|
-
// txb.randomizeOutputs()
|
|
86
|
-
|
|
87
|
-
return {
|
|
88
|
-
txb,
|
|
89
|
-
keyPair1,
|
|
90
|
-
keyPair2,
|
|
91
|
-
addr1,
|
|
92
|
-
addr2,
|
|
93
|
-
saddr1,
|
|
94
|
-
changeaddr,
|
|
95
|
-
txOut1,
|
|
96
|
-
txOut2
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function prepareAndBuildTxBuilder () {
|
|
101
|
-
const obj = prepareTxBuilder()
|
|
102
|
-
obj.txb.build()
|
|
103
|
-
return obj
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
describe('#toJSON', function () {
|
|
107
|
-
it('should convert this txb to JSON', function () {
|
|
108
|
-
const obj = prepareAndBuildTxBuilder()
|
|
109
|
-
const txb = obj.txb
|
|
110
|
-
const json = txb.toJSON()
|
|
111
|
-
should.exist(json.tx)
|
|
112
|
-
should.exist(json.txIns)
|
|
113
|
-
should.exist(json.txIns[0])
|
|
114
|
-
should.exist(json.txOuts)
|
|
115
|
-
should.exist(json.uTxOutMap)
|
|
116
|
-
should.exist(json.txOuts[0])
|
|
117
|
-
should.exist(json.changeScript)
|
|
118
|
-
should.exist(json.feePerKbNum)
|
|
119
|
-
})
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
describe('#fromJSON', function () {
|
|
123
|
-
it('should convert to/from json isomorphically', function () {
|
|
124
|
-
const obj = prepareAndBuildTxBuilder()
|
|
125
|
-
const txb = obj.txb
|
|
126
|
-
const json = txb.toJSON()
|
|
127
|
-
const txb2 = new TxBuilder().fromJSON(json)
|
|
128
|
-
const json2 = txb2.toJSON()
|
|
129
|
-
json2.tx.should.equal(json.tx)
|
|
130
|
-
json2.txIns[0].should.equal(json.txIns[0])
|
|
131
|
-
json2.txOuts[0].should.equal(json.txOuts[0])
|
|
132
|
-
JSON.stringify(json2.uTxOutMap).should.equal(
|
|
133
|
-
JSON.stringify(json.uTxOutMap)
|
|
134
|
-
)
|
|
135
|
-
json2.changeScript.should.equal(json.changeScript)
|
|
136
|
-
json2.feePerKbNum.should.equal(json.feePerKbNum)
|
|
137
|
-
})
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
describe('#setDust', function () {
|
|
141
|
-
it('should set the dust', function () {
|
|
142
|
-
const obj = prepareTxBuilder()
|
|
143
|
-
const txb = obj.txb
|
|
144
|
-
txb.setDust(200)
|
|
145
|
-
txb.dust.should.equal(200)
|
|
146
|
-
txb.setDust(400)
|
|
147
|
-
txb.dust.should.equal(400)
|
|
148
|
-
})
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
describe('#dustChangeToFees', function () {
|
|
152
|
-
it('should set the dustChangeToFees', function () {
|
|
153
|
-
const obj = prepareTxBuilder()
|
|
154
|
-
const txb = obj.txb
|
|
155
|
-
txb.sendDustChangeToFees(true)
|
|
156
|
-
txb.dustChangeToFees.should.equal(true)
|
|
157
|
-
txb.build()
|
|
158
|
-
txb.sendDustChangeToFees(false)
|
|
159
|
-
txb.dustChangeToFees.should.equal(false)
|
|
160
|
-
txb.build()
|
|
161
|
-
})
|
|
162
|
-
|
|
163
|
-
it('should not be able to build a tx if dust is greater than all outputs', function () {
|
|
164
|
-
const obj = prepareTxBuilder()
|
|
165
|
-
const txb = obj.txb
|
|
166
|
-
txb.sendDustChangeToFees(true)
|
|
167
|
-
txb.setDust(4e8)
|
|
168
|
-
;(function () {
|
|
169
|
-
txb.build()
|
|
170
|
-
}.should.throw(
|
|
171
|
-
'cannot create output lesser than dust'
|
|
172
|
-
))
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
it('should not be able to build a tx if dust is greater than all outputs', function () {
|
|
176
|
-
const obj = prepareTxBuilder()
|
|
177
|
-
const txb = obj.txb
|
|
178
|
-
txb.sendDustChangeToFees(true)
|
|
179
|
-
txb.setDust(4e8 + 1)
|
|
180
|
-
;(function () {
|
|
181
|
-
txb.build()
|
|
182
|
-
console.log(txb.tx.toJSON())
|
|
183
|
-
}.should.throw(
|
|
184
|
-
'cannot create output lesser than dust'
|
|
185
|
-
))
|
|
186
|
-
})
|
|
187
|
-
|
|
188
|
-
it('should have two outputs if dust is zero', function () {
|
|
189
|
-
const obj = prepareTxBuilder()
|
|
190
|
-
const txb = obj.txb
|
|
191
|
-
txb.sendDustChangeToFees(true)
|
|
192
|
-
txb.setDust(0)
|
|
193
|
-
txb.build()
|
|
194
|
-
txb.tx.txOuts.length.should.equal(2)
|
|
195
|
-
})
|
|
196
|
-
})
|
|
197
|
-
|
|
198
|
-
describe('#setFeePerKbNum', function () {
|
|
199
|
-
it('should set the feePerKbNum', function () {
|
|
200
|
-
const obj = prepareTxBuilder()
|
|
201
|
-
const txb = obj.txb
|
|
202
|
-
txb.setFeePerKbNum(1000)
|
|
203
|
-
txb.feePerKbNum.should.equal(1000)
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
it('allows zero', function () {
|
|
207
|
-
const obj = prepareTxBuilder()
|
|
208
|
-
const txb = obj.txb
|
|
209
|
-
txb.setFeePerKbNum(0)
|
|
210
|
-
should(txb.feePerKbNum).be.eql(0)
|
|
211
|
-
})
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
describe('#setChangeAddress', function () {
|
|
215
|
-
it('should set the change address', function () {
|
|
216
|
-
const obj = prepareTxBuilder()
|
|
217
|
-
const txb = obj.txb
|
|
218
|
-
const privKey = new PrivKey().fromRandom()
|
|
219
|
-
const address = new Address().fromPrivKey(privKey)
|
|
220
|
-
txb.setChangeAddress(address)
|
|
221
|
-
txb.changeScript.toString().should.equal(address.toTxOutScript().toString())
|
|
222
|
-
})
|
|
223
|
-
})
|
|
224
|
-
|
|
225
|
-
describe('#setChangeScript', function () {
|
|
226
|
-
it('should set the changeScript', function () {
|
|
227
|
-
const obj = prepareTxBuilder()
|
|
228
|
-
const txb = obj.txb
|
|
229
|
-
const privKey = new PrivKey().fromRandom()
|
|
230
|
-
const address = new Address().fromPrivKey(privKey)
|
|
231
|
-
txb.setChangeScript(address.toTxOutScript())
|
|
232
|
-
txb.changeScript.toString().should.equal(address.toTxOutScript().toString())
|
|
233
|
-
})
|
|
234
|
-
})
|
|
235
|
-
|
|
236
|
-
describe('#setNLocktime', function () {
|
|
237
|
-
it('should set the nLockTime', function () {
|
|
238
|
-
const obj = prepareTxBuilder()
|
|
239
|
-
const txb = obj.txb
|
|
240
|
-
txb.setNLocktime(1)
|
|
241
|
-
txb.build()
|
|
242
|
-
txb.tx.nLockTime.should.equal(1)
|
|
243
|
-
})
|
|
244
|
-
})
|
|
245
|
-
|
|
246
|
-
describe('#setVersion', function () {
|
|
247
|
-
it('should set the versionBytesNum', function () {
|
|
248
|
-
const obj = prepareTxBuilder()
|
|
249
|
-
const txb = obj.txb
|
|
250
|
-
txb.setVersion(2)
|
|
251
|
-
txb.build()
|
|
252
|
-
txb.tx.versionBytesNum.should.equal(2)
|
|
253
|
-
})
|
|
254
|
-
})
|
|
255
|
-
|
|
256
|
-
describe('#importPartiallySignedTx', function () {
|
|
257
|
-
it('should set tx', function () {
|
|
258
|
-
const tx = new Tx()
|
|
259
|
-
const txb = new TxBuilder().importPartiallySignedTx(tx)
|
|
260
|
-
should.exist(txb.tx)
|
|
261
|
-
})
|
|
262
|
-
|
|
263
|
-
it('should set tx and uTxOutMap', function () {
|
|
264
|
-
const tx = new Tx()
|
|
265
|
-
const uTxOutMap = new TxOutMap()
|
|
266
|
-
const txb = new TxBuilder().importPartiallySignedTx(tx, uTxOutMap)
|
|
267
|
-
should.exist(txb.tx)
|
|
268
|
-
should.exist(txb.uTxOutMap)
|
|
269
|
-
})
|
|
270
|
-
})
|
|
271
|
-
|
|
272
|
-
describe('#outputToAddress', function () {
|
|
273
|
-
it('should add a pubKeyHash address', function () {
|
|
274
|
-
const pubKey = new PubKey().fromPrivKey(new PrivKey().fromRandom())
|
|
275
|
-
const address = new Address().fromPubKey(pubKey)
|
|
276
|
-
const txb = new TxBuilder()
|
|
277
|
-
txb.outputToAddress(new Bn(0), address)
|
|
278
|
-
txb.txOuts.length.should.equal(1)
|
|
279
|
-
})
|
|
280
|
-
})
|
|
281
|
-
|
|
282
|
-
describe('#outputToScript', function () {
|
|
283
|
-
it('should add an OP_RETURN output', function () {
|
|
284
|
-
const script = new Script().fromString('OP_RETURN')
|
|
285
|
-
const txb = new TxBuilder()
|
|
286
|
-
txb.outputToScript(new Bn(0), script)
|
|
287
|
-
txb.txOuts.length.should.equal(1)
|
|
288
|
-
})
|
|
289
|
-
})
|
|
290
|
-
|
|
291
|
-
describe('#build', function () {
|
|
292
|
-
function prepareTxBuilder (outAmountBn = new Bn(1e8)) {
|
|
293
|
-
const txb = new TxBuilder()
|
|
294
|
-
|
|
295
|
-
// make change address
|
|
296
|
-
const privKey = new PrivKey().fromBn(new Bn(1))
|
|
297
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
298
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
299
|
-
|
|
300
|
-
// make addresses to send from
|
|
301
|
-
const privKey1 = new PrivKey().fromBn(new Bn(2))
|
|
302
|
-
const keyPair1 = new KeyPair().fromPrivKey(privKey1)
|
|
303
|
-
const addr1 = new Address().fromPubKey(keyPair1.pubKey)
|
|
304
|
-
|
|
305
|
-
const privKey2 = new PrivKey().fromBn(new Bn(3))
|
|
306
|
-
const keyPair2 = new KeyPair().fromPrivKey(privKey2)
|
|
307
|
-
const addr2 = new Address().fromPubKey(keyPair2.pubKey)
|
|
308
|
-
|
|
309
|
-
const privKey3 = new PrivKey().fromBn(new Bn(4))
|
|
310
|
-
const keyPair3 = new KeyPair().fromPrivKey(privKey3)
|
|
311
|
-
const addr3 = new Address().fromPubKey(keyPair3.pubKey)
|
|
312
|
-
|
|
313
|
-
const txOut1 = TxOut.fromProperties(new Bn(1e8), addr1.toTxOutScript())
|
|
314
|
-
const txOut2 = TxOut.fromProperties(new Bn(1e8), addr2.toTxOutScript())
|
|
315
|
-
const txOut3 = TxOut.fromProperties(new Bn(1e8), addr3.toTxOutScript())
|
|
316
|
-
// total balance: 3e8
|
|
317
|
-
|
|
318
|
-
const txHashBuf = Buffer.alloc(32)
|
|
319
|
-
txHashBuf.fill(0)
|
|
320
|
-
const txOutNum1 = 0
|
|
321
|
-
const txOutNum2 = 1
|
|
322
|
-
const txOutNum3 = 2
|
|
323
|
-
|
|
324
|
-
txb.setFeePerKbNum(0.0001e8)
|
|
325
|
-
txb.setChangeAddress(changeaddr)
|
|
326
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum1, txOut1, keyPair1.pubKey)
|
|
327
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum2, txOut2, keyPair2.pubKey)
|
|
328
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum3, txOut3, keyPair3.pubKey)
|
|
329
|
-
txb.outputToAddress(outAmountBn, addr1)
|
|
330
|
-
|
|
331
|
-
return txb
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
it('should build a tx where all inputs are NOT required', function () {
|
|
335
|
-
const txb = prepareTxBuilder()
|
|
336
|
-
|
|
337
|
-
txb.build()
|
|
338
|
-
|
|
339
|
-
txb.tx.txIns.length.should.equal(2)
|
|
340
|
-
})
|
|
341
|
-
|
|
342
|
-
it('should build a tx where all inputs are required', function () {
|
|
343
|
-
const txb = prepareTxBuilder()
|
|
344
|
-
|
|
345
|
-
txb.build({ useAllInputs: true })
|
|
346
|
-
|
|
347
|
-
txb.tx.txIns.length.should.equal(3)
|
|
348
|
-
})
|
|
349
|
-
|
|
350
|
-
it('should buld a tx with zero fees', function () {
|
|
351
|
-
const txb = new TxBuilder()
|
|
352
|
-
|
|
353
|
-
const changePrivKey = new PrivKey().fromBn(new Bn(1))
|
|
354
|
-
const changeKeyPair = new KeyPair().fromPrivKey(changePrivKey)
|
|
355
|
-
const changeAddr = new Address().fromPubKey(changeKeyPair.pubKey)
|
|
356
|
-
|
|
357
|
-
const inputPrivKey = new PrivKey().fromBn(new Bn(2))
|
|
358
|
-
const inputKeyPair = new KeyPair().fromPrivKey(inputPrivKey)
|
|
359
|
-
const inputAddress = new Address().fromPubKey(inputKeyPair.pubKey)
|
|
360
|
-
|
|
361
|
-
const txHashBuf = Buffer.alloc(32).fill(1)
|
|
362
|
-
const txOutNum = 0
|
|
363
|
-
const inputAmount = Bn().fromNumber(1000)
|
|
364
|
-
const inputScript = inputAddress.toTxOutScript()
|
|
365
|
-
const txOut = TxOut.fromProperties(inputAmount, inputScript)
|
|
366
|
-
|
|
367
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum, txOut, inputKeyPair.pubKey)
|
|
368
|
-
txb.setChangeAddress(changeAddr)
|
|
369
|
-
txb.setFeePerKbNum(0)
|
|
370
|
-
|
|
371
|
-
should(() => txb.build()).not.throw()
|
|
372
|
-
|
|
373
|
-
const tx = txb.tx
|
|
374
|
-
should(tx.txOuts[0].valueBn.toString()).be.eql(inputAmount.toString())
|
|
375
|
-
})
|
|
376
|
-
|
|
377
|
-
it('builds a tx whith unspendable output containing OP_FALSE and output less than dust', () => {
|
|
378
|
-
const txb = new TxBuilder()
|
|
379
|
-
txb.setDust(145)
|
|
380
|
-
|
|
381
|
-
// input
|
|
382
|
-
const inputPrivKey = new PrivKey().fromBn(new Bn(2))
|
|
383
|
-
const inputKeyPair = new KeyPair().fromPrivKey(inputPrivKey)
|
|
384
|
-
const inputAddress = new Address().fromPubKey(inputKeyPair.pubKey)
|
|
385
|
-
const txHashBuf = Buffer.alloc(32).fill(1)
|
|
386
|
-
const inputAmount = Bn().fromNumber(1000)
|
|
387
|
-
const inputScript = inputAddress.toTxOutScript()
|
|
388
|
-
const txOut = TxOut.fromProperties(inputAmount, inputScript)
|
|
389
|
-
txb.inputFromPubKeyHash(txHashBuf, 0, txOut, inputKeyPair.pubKey)
|
|
390
|
-
|
|
391
|
-
// change
|
|
392
|
-
const changePrivKey = new PrivKey().fromBn(new Bn(1))
|
|
393
|
-
const changeKeyPair = new KeyPair().fromPrivKey(changePrivKey)
|
|
394
|
-
const changeAddr = new Address().fromPubKey(changeKeyPair.pubKey)
|
|
395
|
-
txb.setChangeAddress(changeAddr)
|
|
396
|
-
|
|
397
|
-
// build
|
|
398
|
-
txb.outputToScript(new Bn().fromNumber(0), Script.fromAsmString('OP_FALSE OP_RETURN 0'))
|
|
399
|
-
|
|
400
|
-
// assertions
|
|
401
|
-
should(() => txb.build()).not.throw()
|
|
402
|
-
const tx = txb.tx
|
|
403
|
-
should(tx.txOuts[0].valueBn).be.eql(new Bn().fromNumber(0))
|
|
404
|
-
should(tx.txOuts[0].script).be.eql(Script.fromAsmString('OP_FALSE OP_RETURN 0'))
|
|
405
|
-
})
|
|
406
|
-
})
|
|
407
|
-
|
|
408
|
-
describe('#sort', function () {
|
|
409
|
-
it('it should call tx sort', function () {
|
|
410
|
-
const txBuilder = new TxBuilder()
|
|
411
|
-
let called = 0
|
|
412
|
-
txBuilder.tx.sort = () => { called++ }
|
|
413
|
-
txBuilder.sort()
|
|
414
|
-
called.should.equal(1)
|
|
415
|
-
})
|
|
416
|
-
})
|
|
417
|
-
|
|
418
|
-
describe('@allSigsPresent', function () {
|
|
419
|
-
it('should know all sigs are or are not present these scripts', function () {
|
|
420
|
-
let script
|
|
421
|
-
script = new Script().fromString(
|
|
422
|
-
'OP_0 71 0x304402204c99f293ca4d84f01e8f319e93978866877c948628cb4d4ff5ccdf42ae8434cc02206516aa37dcd9f50ddb2f7484aeaef3c0fbab77db60eeafd5ad91b0ba54b715e901 72 0x3045022100ff53e3f8ee64eb0f816a85a244d5e3bc20e7ade814e4377be5279a12130c8414022068e00c79272539d03357d4d589bf4c0c7a517023aaa2abe3f341c26ca9077d0801 OP_PUSHDATA1 105 0x522102c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee52102f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f92102f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f953ae'
|
|
423
|
-
)
|
|
424
|
-
TxBuilder.allSigsPresent(2, script).should.equal(true)
|
|
425
|
-
script = new Script().fromString(
|
|
426
|
-
'OP_0 71 0x304402204c99f293ca4d84f01e8f319e93978866877c948628cb4d4ff5ccdf42ae8434cc02206516aa37dcd9f50ddb2f7484aeaef3c0fbab77db60eeafd5ad91b0ba54b715e901 71 0x304402204c99f293ca4d84f01e8f319e93978866877c948628cb4d4ff5ccdf42ae8434cc02206516aa37dcd9f50ddb2f7484aeaef3c0fbab77db60eeafd5ad91b0ba54b715e901 72 0x3045022100ff53e3f8ee64eb0f816a85a244d5e3bc20e7ade814e4377be5279a12130c8414022068e00c79272539d03357d4d589bf4c0c7a517023aaa2abe3f341c26ca9077d0801 OP_PUSHDATA1 105 0x522102c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee52102f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f92102f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f953ae'
|
|
427
|
-
)
|
|
428
|
-
TxBuilder.allSigsPresent(3, script).should.equal(true)
|
|
429
|
-
script = new Script().fromString(
|
|
430
|
-
'OP_0 OP_0 71 0x304402204c99f293ca4d84f01e8f319e93978866877c948628cb4d4ff5ccdf42ae8434cc02206516aa37dcd9f50ddb2f7484aeaef3c0fbab77db60eeafd5ad91b0ba54b715e901 72 0x3045022100ff53e3f8ee64eb0f816a85a244d5e3bc20e7ade814e4377be5279a12130c8414022068e00c79272539d03357d4d589bf4c0c7a517023aaa2abe3f341c26ca9077d0801 OP_PUSHDATA1 105 0x522102c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee52102f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f92102f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f953ae'
|
|
431
|
-
)
|
|
432
|
-
TxBuilder.allSigsPresent(3, script).should.equal(false)
|
|
433
|
-
})
|
|
434
|
-
})
|
|
435
|
-
|
|
436
|
-
describe('@removeBlankSigs', function () {
|
|
437
|
-
it('should know all sigs are or are not present these scripts', function () {
|
|
438
|
-
let script
|
|
439
|
-
script = new Script().fromString(
|
|
440
|
-
'OP_0 OP_0 71 0x304402204c99f293ca4d84f01e8f319e93978866877c948628cb4d4ff5ccdf42ae8434cc02206516aa37dcd9f50ddb2f7484aeaef3c0fbab77db60eeafd5ad91b0ba54b715e901 72 0x3045022100ff53e3f8ee64eb0f816a85a244d5e3bc20e7ade814e4377be5279a12130c8414022068e00c79272539d03357d4d589bf4c0c7a517023aaa2abe3f341c26ca9077d0801 OP_PUSHDATA1 105 0x522102c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee52102f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f92102f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f953ae'
|
|
441
|
-
)
|
|
442
|
-
TxBuilder.allSigsPresent(3, script).should.equal(false)
|
|
443
|
-
script = TxBuilder.removeBlankSigs(script)
|
|
444
|
-
TxBuilder.allSigsPresent(2, script).should.equal(true)
|
|
445
|
-
})
|
|
446
|
-
})
|
|
447
|
-
|
|
448
|
-
describe('#inputFromScript', function () {
|
|
449
|
-
it('should add an input from a script', function () {
|
|
450
|
-
const keyPair = new KeyPair().fromRandom()
|
|
451
|
-
const address = new Address().fromPubKey(keyPair.pubKey)
|
|
452
|
-
const txOut = TxOut.fromProperties(
|
|
453
|
-
new Bn(1000),
|
|
454
|
-
new Script().fromPubKeyHash(address.hashBuf)
|
|
455
|
-
)
|
|
456
|
-
const script = new Script().fromString('OP_RETURN')
|
|
457
|
-
const txHashBuf = Buffer.alloc(32)
|
|
458
|
-
txHashBuf.fill(0)
|
|
459
|
-
const txOutNum = 0
|
|
460
|
-
const txbuilder = new TxBuilder().inputFromScript(
|
|
461
|
-
txHashBuf,
|
|
462
|
-
txOutNum,
|
|
463
|
-
txOut,
|
|
464
|
-
script
|
|
465
|
-
)
|
|
466
|
-
txbuilder.txIns.length.should.equal(1)
|
|
467
|
-
})
|
|
468
|
-
|
|
469
|
-
it('should add an input from a script and set nSequence', function () {
|
|
470
|
-
const keyPair = new KeyPair().fromRandom()
|
|
471
|
-
const address = new Address().fromPubKey(keyPair.pubKey)
|
|
472
|
-
const txOut = TxOut.fromProperties(
|
|
473
|
-
new Bn(1000),
|
|
474
|
-
new Script().fromPubKeyHash(address.hashBuf)
|
|
475
|
-
)
|
|
476
|
-
const script = new Script().fromString('OP_RETURN')
|
|
477
|
-
const txHashBuf = Buffer.alloc(32)
|
|
478
|
-
txHashBuf.fill(0)
|
|
479
|
-
const txOutNum = 0
|
|
480
|
-
const txbuilder = new TxBuilder().inputFromScript(
|
|
481
|
-
txHashBuf,
|
|
482
|
-
txOutNum,
|
|
483
|
-
txOut,
|
|
484
|
-
script,
|
|
485
|
-
0xf0f0f0f0
|
|
486
|
-
)
|
|
487
|
-
txbuilder.txIns.length.should.equal(1)
|
|
488
|
-
txbuilder.txIns[0].nSequence.should.equal(0xf0f0f0f0)
|
|
489
|
-
})
|
|
490
|
-
})
|
|
491
|
-
|
|
492
|
-
describe('#inputFromPubKeyHash', function () {
|
|
493
|
-
it('should add an input from a pubKeyHash output', function () {
|
|
494
|
-
const keyPair = new KeyPair().fromRandom()
|
|
495
|
-
const address = new Address().fromPubKey(keyPair.pubKey)
|
|
496
|
-
const txOut = TxOut.fromProperties(
|
|
497
|
-
new Bn(1000),
|
|
498
|
-
new Script().fromPubKeyHash(address.hashBuf)
|
|
499
|
-
)
|
|
500
|
-
const txHashBuf = Buffer.alloc(32)
|
|
501
|
-
txHashBuf.fill(0)
|
|
502
|
-
const txOutNum = 0
|
|
503
|
-
const txbuilder = new TxBuilder().inputFromPubKeyHash(
|
|
504
|
-
txHashBuf,
|
|
505
|
-
txOutNum,
|
|
506
|
-
txOut,
|
|
507
|
-
keyPair.pubKey
|
|
508
|
-
)
|
|
509
|
-
Buffer.compare(
|
|
510
|
-
txbuilder.txIns[0].script.chunks[1].buf,
|
|
511
|
-
keyPair.pubKey.toBuffer()
|
|
512
|
-
).should.equal(0)
|
|
513
|
-
})
|
|
514
|
-
|
|
515
|
-
it('should add an input from a pubKeyHash output and set nSequence', function () {
|
|
516
|
-
const keyPair = new KeyPair().fromRandom()
|
|
517
|
-
const address = new Address().fromPubKey(keyPair.pubKey)
|
|
518
|
-
const txOut = TxOut.fromProperties(
|
|
519
|
-
new Bn(1000),
|
|
520
|
-
new Script().fromPubKeyHash(address.hashBuf)
|
|
521
|
-
)
|
|
522
|
-
const txHashBuf = Buffer.alloc(32)
|
|
523
|
-
txHashBuf.fill(0)
|
|
524
|
-
const txOutNum = 0
|
|
525
|
-
const txbuilder = new TxBuilder().inputFromPubKeyHash(
|
|
526
|
-
txHashBuf,
|
|
527
|
-
txOutNum,
|
|
528
|
-
txOut,
|
|
529
|
-
keyPair.pubKey,
|
|
530
|
-
0xf0f0f0f0
|
|
531
|
-
)
|
|
532
|
-
Buffer.compare(
|
|
533
|
-
txbuilder.txIns[0].script.chunks[1].buf,
|
|
534
|
-
keyPair.pubKey.toBuffer()
|
|
535
|
-
).should.equal(0)
|
|
536
|
-
txbuilder.txIns[0].nSequence.should.equal(0xf0f0f0f0)
|
|
537
|
-
})
|
|
538
|
-
})
|
|
539
|
-
|
|
540
|
-
describe('#getSig', function () {
|
|
541
|
-
let txb, keyPair1, txOut1
|
|
542
|
-
|
|
543
|
-
before(function () {
|
|
544
|
-
const obj = prepareAndBuildTxBuilder()
|
|
545
|
-
txb = obj.txb
|
|
546
|
-
keyPair1 = obj.keyPair1
|
|
547
|
-
txOut1 = obj.txOut1
|
|
548
|
-
})
|
|
549
|
-
|
|
550
|
-
it('should sign and verify synchronously', function () {
|
|
551
|
-
const sig = txb.getSig(keyPair1, Sig.SIGHASH_ALL, 0, keyPair1, txOut1)
|
|
552
|
-
;(sig instanceof Sig).should.equal(true)
|
|
553
|
-
})
|
|
554
|
-
})
|
|
555
|
-
|
|
556
|
-
describe('#signTxIn', function () {
|
|
557
|
-
it('should sign and verify no SIGHASH_FORKID synchronously', function () {
|
|
558
|
-
// prepare
|
|
559
|
-
const obj = prepareAndBuildTxBuilder()
|
|
560
|
-
const txb = obj.txb
|
|
561
|
-
const keyPair1 = obj.keyPair1
|
|
562
|
-
const keyPair2 = obj.keyPair2
|
|
563
|
-
const saddr1 = obj.saddr1
|
|
564
|
-
const changeaddr = obj.changeaddr
|
|
565
|
-
|
|
566
|
-
// begin signing
|
|
567
|
-
const nHashType = Sig.SIGHASH_ALL
|
|
568
|
-
const flags = Interp.SCRIPT_VERIFY_P2SH
|
|
569
|
-
txb.signTxIn(0, keyPair1, undefined, undefined, nHashType, flags)
|
|
570
|
-
|
|
571
|
-
// transaction not fully signed yet, so should be invalid
|
|
572
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap, flags).should.equal(false)
|
|
573
|
-
|
|
574
|
-
txb.signTxIn(1, keyPair2, undefined, undefined, nHashType, flags)
|
|
575
|
-
|
|
576
|
-
txb.tx.txOuts[0].script.chunks[2].buf
|
|
577
|
-
.toString('hex')
|
|
578
|
-
.should.equal(saddr1.hashBuf.toString('hex'))
|
|
579
|
-
txb.tx.txOuts[0].valueBn.eq(1.5e8).should.equal(true)
|
|
580
|
-
txb.tx.txOuts[1].valueBn.gt(546).should.equal(true)
|
|
581
|
-
txb.tx.txOuts[1].valueBn.toNumber().should.equal(49996250)
|
|
582
|
-
txb.changeAmountBn.toNumber().should.equal(49996250)
|
|
583
|
-
txb.feeAmountBn.toNumber().should.equal(3750)
|
|
584
|
-
txb.tx.txOuts[1].script.chunks[2].buf
|
|
585
|
-
.toString('hex')
|
|
586
|
-
.should.equal(changeaddr.hashBuf.toString('hex'))
|
|
587
|
-
|
|
588
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap, flags).should.equal(true)
|
|
589
|
-
})
|
|
590
|
-
|
|
591
|
-
it('should sign and verify SIGHASH_FORKID synchronously', function () {
|
|
592
|
-
// prepare
|
|
593
|
-
const obj = prepareAndBuildTxBuilder()
|
|
594
|
-
const txb = obj.txb
|
|
595
|
-
const keyPair1 = obj.keyPair1
|
|
596
|
-
const keyPair2 = obj.keyPair2
|
|
597
|
-
const saddr1 = obj.saddr1
|
|
598
|
-
const changeaddr = obj.changeaddr
|
|
599
|
-
|
|
600
|
-
// begin signing
|
|
601
|
-
const nHashType = Sig.SIGHASH_ALL | Sig.SIGHASH_FORKID
|
|
602
|
-
const flags =
|
|
603
|
-
Interp.SCRIPT_ENABLE_SIGHASH_FORKID | Interp.SCRIPT_VERIFY_P2SH
|
|
604
|
-
txb.signTxIn(0, keyPair1, undefined, undefined, nHashType, flags)
|
|
605
|
-
|
|
606
|
-
// transaction not fully signed yet, so should be invalid
|
|
607
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap, flags).should.equal(false)
|
|
608
|
-
|
|
609
|
-
txb.signTxIn(1, keyPair2, undefined, undefined, nHashType, flags)
|
|
610
|
-
|
|
611
|
-
txb.tx.txOuts[0].script.chunks[2].buf
|
|
612
|
-
.toString('hex')
|
|
613
|
-
.should.equal(saddr1.hashBuf.toString('hex'))
|
|
614
|
-
txb.tx.txOuts[0].valueBn.eq(1.5e8).should.equal(true)
|
|
615
|
-
txb.tx.txOuts[1].valueBn.gt(546).should.equal(true)
|
|
616
|
-
txb.tx.txOuts[1].valueBn.toNumber().should.equal(49996250)
|
|
617
|
-
txb.changeAmountBn.toNumber().should.equal(49996250)
|
|
618
|
-
txb.feeAmountBn.toNumber().should.equal(3750)
|
|
619
|
-
txb.tx.txOuts[1].script.chunks[2].buf
|
|
620
|
-
.toString('hex')
|
|
621
|
-
.should.equal(changeaddr.hashBuf.toString('hex'))
|
|
622
|
-
|
|
623
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap, flags).should.equal(true)
|
|
624
|
-
})
|
|
625
|
-
|
|
626
|
-
it('should pass in txOut', function () {
|
|
627
|
-
// prepare
|
|
628
|
-
const obj = prepareAndBuildTxBuilder()
|
|
629
|
-
const txb = obj.txb
|
|
630
|
-
const keyPair1 = obj.keyPair1
|
|
631
|
-
const txOut1 = obj.txOut1
|
|
632
|
-
|
|
633
|
-
txb.txOutMap = sinon.spy()
|
|
634
|
-
txb.uTxOutMap = {
|
|
635
|
-
get: sinon.spy()
|
|
636
|
-
}
|
|
637
|
-
txb.signTxIn(0, keyPair1, txOut1, undefined, Sig.SIGHASH_ALL, 0)
|
|
638
|
-
txb.uTxOutMap.get.calledOnce.should.equal(false)
|
|
639
|
-
})
|
|
640
|
-
})
|
|
641
|
-
|
|
642
|
-
describe('#asyncGetSig', function () {
|
|
643
|
-
let txb, keyPair1, txOut1
|
|
644
|
-
|
|
645
|
-
before(function () {
|
|
646
|
-
const obj = prepareAndBuildTxBuilder()
|
|
647
|
-
txb = obj.txb
|
|
648
|
-
keyPair1 = obj.keyPair1
|
|
649
|
-
txOut1 = obj.txOut1
|
|
650
|
-
})
|
|
651
|
-
|
|
652
|
-
it('should sign and verify synchronously', async function () {
|
|
653
|
-
const sig = await txb.asyncGetSig(
|
|
654
|
-
keyPair1,
|
|
655
|
-
Sig.SIGHASH_ALL,
|
|
656
|
-
0,
|
|
657
|
-
keyPair1,
|
|
658
|
-
txOut1
|
|
659
|
-
)
|
|
660
|
-
;(sig instanceof Sig).should.equal(true)
|
|
661
|
-
})
|
|
662
|
-
})
|
|
663
|
-
|
|
664
|
-
describe('#asyncSignTxIn', function () {
|
|
665
|
-
it('should sign and verify no SIGHASH_FORKID asynchronously', async function () {
|
|
666
|
-
// prepare
|
|
667
|
-
const obj = prepareAndBuildTxBuilder()
|
|
668
|
-
const txb = obj.txb
|
|
669
|
-
const keyPair1 = obj.keyPair1
|
|
670
|
-
const keyPair2 = obj.keyPair2
|
|
671
|
-
const saddr1 = obj.saddr1
|
|
672
|
-
const changeaddr = obj.changeaddr
|
|
673
|
-
|
|
674
|
-
// begin signing
|
|
675
|
-
const nHashType = Sig.SIGHASH_ALL
|
|
676
|
-
const flags = Interp.SCRIPT_VERIFY_P2SH
|
|
677
|
-
await txb.asyncSignTxIn(0, keyPair1, undefined, undefined, nHashType, flags)
|
|
678
|
-
|
|
679
|
-
// transaction not fully signed yet, so should be invalid
|
|
680
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap, flags).should.equal(false)
|
|
681
|
-
|
|
682
|
-
await txb.asyncSignTxIn(1, keyPair2, undefined, undefined, nHashType, flags)
|
|
683
|
-
|
|
684
|
-
txb.tx.txOuts[0].script.chunks[2].buf
|
|
685
|
-
.toString('hex')
|
|
686
|
-
.should.equal(saddr1.hashBuf.toString('hex'))
|
|
687
|
-
txb.tx.txOuts[0].valueBn.eq(1.5e8).should.equal(true)
|
|
688
|
-
txb.tx.txOuts[1].valueBn.gt(546).should.equal(true)
|
|
689
|
-
txb.tx.txOuts[1].valueBn.toNumber().should.equal(49996250)
|
|
690
|
-
txb.changeAmountBn.toNumber().should.equal(49996250)
|
|
691
|
-
txb.feeAmountBn.toNumber().should.equal(3750)
|
|
692
|
-
txb.tx.txOuts[1].script.chunks[2].buf
|
|
693
|
-
.toString('hex')
|
|
694
|
-
.should.equal(changeaddr.hashBuf.toString('hex'))
|
|
695
|
-
|
|
696
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap, flags).should.equal(true)
|
|
697
|
-
})
|
|
698
|
-
|
|
699
|
-
it('should sign and verify SIGHASH_FORKID asynchronously', async function () {
|
|
700
|
-
// prepare
|
|
701
|
-
const obj = prepareAndBuildTxBuilder()
|
|
702
|
-
const txb = obj.txb
|
|
703
|
-
const keyPair1 = obj.keyPair1
|
|
704
|
-
const keyPair2 = obj.keyPair2
|
|
705
|
-
const saddr1 = obj.saddr1
|
|
706
|
-
const changeaddr = obj.changeaddr
|
|
707
|
-
|
|
708
|
-
// begin signing
|
|
709
|
-
const nHashType = Sig.SIGHASH_ALL | Sig.SIGHASH_FORKID
|
|
710
|
-
const flags =
|
|
711
|
-
Interp.SCRIPT_ENABLE_SIGHASH_FORKID | Interp.SCRIPT_VERIFY_P2SH
|
|
712
|
-
await txb.asyncSignTxIn(0, keyPair1, undefined, undefined, nHashType, flags)
|
|
713
|
-
|
|
714
|
-
// transaction not fully signed yet, so should be invalid
|
|
715
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap, flags).should.equal(false)
|
|
716
|
-
|
|
717
|
-
await txb.asyncSignTxIn(1, keyPair2, undefined, undefined, nHashType, flags)
|
|
718
|
-
|
|
719
|
-
txb.tx.txOuts[0].script.chunks[2].buf
|
|
720
|
-
.toString('hex')
|
|
721
|
-
.should.equal(saddr1.hashBuf.toString('hex'))
|
|
722
|
-
txb.tx.txOuts[0].valueBn.eq(1.5e8).should.equal(true)
|
|
723
|
-
txb.tx.txOuts[1].valueBn.gt(546).should.equal(true)
|
|
724
|
-
txb.tx.txOuts[1].valueBn.toNumber().should.equal(49996250)
|
|
725
|
-
txb.changeAmountBn.toNumber().should.equal(49996250)
|
|
726
|
-
txb.feeAmountBn.toNumber().should.equal(3750)
|
|
727
|
-
txb.tx.txOuts[1].script.chunks[2].buf
|
|
728
|
-
.toString('hex')
|
|
729
|
-
.should.equal(changeaddr.hashBuf.toString('hex'))
|
|
730
|
-
|
|
731
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap, flags).should.equal(true)
|
|
732
|
-
})
|
|
733
|
-
|
|
734
|
-
it('should pass in txOut', async function () {
|
|
735
|
-
const obj = prepareAndBuildTxBuilder()
|
|
736
|
-
const txb = obj.txb
|
|
737
|
-
const keyPair1 = obj.keyPair1
|
|
738
|
-
const txOut1 = obj.txOut1
|
|
739
|
-
txb.txOutMap = sinon.spy()
|
|
740
|
-
txb.uTxOutMap = {
|
|
741
|
-
get: sinon.spy()
|
|
742
|
-
}
|
|
743
|
-
await txb.asyncSignTxIn(0, keyPair1, txOut1, undefined, Sig.SIGHASH_ALL, 0)
|
|
744
|
-
txb.uTxOutMap.get.calledOnce.should.equal(false)
|
|
745
|
-
})
|
|
746
|
-
})
|
|
747
|
-
|
|
748
|
-
describe('#signWithKeyPairs', function () {
|
|
749
|
-
it('should sign and verify synchronously', function () {
|
|
750
|
-
// prepare
|
|
751
|
-
const obj = prepareAndBuildTxBuilder()
|
|
752
|
-
const txb = obj.txb
|
|
753
|
-
const keyPair1 = obj.keyPair1
|
|
754
|
-
const keyPair2 = obj.keyPair2
|
|
755
|
-
const saddr1 = obj.saddr1
|
|
756
|
-
const changeaddr = obj.changeaddr
|
|
757
|
-
|
|
758
|
-
// begin signing
|
|
759
|
-
txb.signWithKeyPairs([keyPair1])
|
|
760
|
-
|
|
761
|
-
txb.sigOperations.map.get('0000000000000000000000000000000000000000000000000000000000000000:0')[0].log.should.equal('successfully inserted signature')
|
|
762
|
-
txb.sigOperations.map.get('0000000000000000000000000000000000000000000000000000000000000000:0')[1].log.should.equal('successfully inserted public key')
|
|
763
|
-
txb.sigOperations.map.get('0000000000000000000000000000000000000000000000000000000000000000:1')[0].log.should.equal('cannot find keyPair for addressStr 1CUNEBjYrCn2y1SdiUMohaKUi4wpP326Lb')
|
|
764
|
-
txb.sigOperations.map.get('0000000000000000000000000000000000000000000000000000000000000000:1')[1].log.should.equal('cannot find keyPair for addressStr 1CUNEBjYrCn2y1SdiUMohaKUi4wpP326Lb')
|
|
765
|
-
|
|
766
|
-
// transaction not fully signed yet, so should be invalid
|
|
767
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(false)
|
|
768
|
-
|
|
769
|
-
// this should effectively add
|
|
770
|
-
txb.signWithKeyPairs([keyPair2])
|
|
771
|
-
|
|
772
|
-
txb.tx.txOuts[0].script.chunks[2].buf
|
|
773
|
-
.toString('hex')
|
|
774
|
-
.should.equal(saddr1.hashBuf.toString('hex'))
|
|
775
|
-
txb.tx.txOuts[0].valueBn.eq(1.5e8).should.equal(true)
|
|
776
|
-
txb.tx.txOuts[1].valueBn.gt(546).should.equal(true)
|
|
777
|
-
txb.tx.txOuts[1].valueBn.toNumber().should.equal(49996250)
|
|
778
|
-
txb.changeAmountBn.toNumber().should.equal(49996250)
|
|
779
|
-
txb.feeAmountBn.toNumber().should.equal(3750)
|
|
780
|
-
txb.tx.txOuts[1].script.chunks[2].buf
|
|
781
|
-
.toString('hex')
|
|
782
|
-
.should.equal(changeaddr.hashBuf.toString('hex'))
|
|
783
|
-
|
|
784
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
785
|
-
|
|
786
|
-
// re-signing just puts the same signatures back into the same place and
|
|
787
|
-
// thus should still be valid
|
|
788
|
-
txb.signWithKeyPairs([keyPair1, keyPair2])
|
|
789
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
790
|
-
})
|
|
791
|
-
|
|
792
|
-
it('should sign and verify synchronously with no public key inserted at input', function () {
|
|
793
|
-
function prepareTxBuilder () {
|
|
794
|
-
const txb = new TxBuilder()
|
|
795
|
-
|
|
796
|
-
// make change address
|
|
797
|
-
const privKey = new PrivKey().fromBn(new Bn(1))
|
|
798
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
799
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
800
|
-
|
|
801
|
-
// make addresses to send from
|
|
802
|
-
const privKey1 = new PrivKey().fromBn(new Bn(2))
|
|
803
|
-
const keyPair1 = new KeyPair().fromPrivKey(privKey1)
|
|
804
|
-
const addr1 = new Address().fromPubKey(keyPair1.pubKey)
|
|
805
|
-
|
|
806
|
-
const privKey2 = new PrivKey().fromBn(new Bn(3))
|
|
807
|
-
const keyPair2 = new KeyPair().fromPrivKey(privKey2)
|
|
808
|
-
const addr2 = new Address().fromPubKey(keyPair2.pubKey)
|
|
809
|
-
|
|
810
|
-
// make addresses to send to
|
|
811
|
-
const saddr1 = addr1
|
|
812
|
-
|
|
813
|
-
// txOuts that we are spending
|
|
814
|
-
|
|
815
|
-
// pubKeyHash out
|
|
816
|
-
const scriptout1 = new Script().fromString(
|
|
817
|
-
'OP_DUP OP_HASH160 20 0x' +
|
|
818
|
-
addr1.hashBuf.toString('hex') +
|
|
819
|
-
' OP_EQUALVERIFY OP_CHECKSIG'
|
|
820
|
-
)
|
|
821
|
-
|
|
822
|
-
// pubKeyHash out
|
|
823
|
-
const scriptout2 = new Script().fromString(
|
|
824
|
-
'OP_DUP OP_HASH160 20 0x' +
|
|
825
|
-
addr2.hashBuf.toString('hex') +
|
|
826
|
-
' OP_EQUALVERIFY OP_CHECKSIG'
|
|
827
|
-
)
|
|
828
|
-
|
|
829
|
-
const txOut1 = TxOut.fromProperties(new Bn(1e8), scriptout1)
|
|
830
|
-
const txOut2 = TxOut.fromProperties(new Bn(1e8), scriptout2)
|
|
831
|
-
// total balance: 2e8
|
|
832
|
-
|
|
833
|
-
const txHashBuf = Buffer.alloc(32)
|
|
834
|
-
txHashBuf.fill(0)
|
|
835
|
-
const txOutNum1 = 0
|
|
836
|
-
const txOutNum2 = 1
|
|
837
|
-
|
|
838
|
-
txb.setFeePerKbNum(0.0001e8)
|
|
839
|
-
txb.setChangeAddress(changeaddr)
|
|
840
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum1, txOut1)
|
|
841
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum2, txOut2)
|
|
842
|
-
txb.outputToAddress(new Bn(1.5e8), saddr1) // pubKeyHash address
|
|
843
|
-
// total sending: 2e8 (plus fee)
|
|
844
|
-
// txb.randomizeInputs()
|
|
845
|
-
// txb.randomizeOutputs()
|
|
846
|
-
|
|
847
|
-
return {
|
|
848
|
-
txb,
|
|
849
|
-
keyPair1,
|
|
850
|
-
keyPair2,
|
|
851
|
-
addr1,
|
|
852
|
-
addr2,
|
|
853
|
-
saddr1,
|
|
854
|
-
changeaddr,
|
|
855
|
-
txOut1,
|
|
856
|
-
txOut2
|
|
857
|
-
}
|
|
858
|
-
}
|
|
859
|
-
|
|
860
|
-
function prepareAndBuildTxBuilder () {
|
|
861
|
-
const obj = prepareTxBuilder()
|
|
862
|
-
obj.txb.build()
|
|
863
|
-
return obj
|
|
864
|
-
}
|
|
865
|
-
|
|
866
|
-
// prepare
|
|
867
|
-
const obj = prepareAndBuildTxBuilder()
|
|
868
|
-
const txb = obj.txb
|
|
869
|
-
const keyPair1 = obj.keyPair1
|
|
870
|
-
const keyPair2 = obj.keyPair2
|
|
871
|
-
const saddr1 = obj.saddr1
|
|
872
|
-
const changeaddr = obj.changeaddr
|
|
873
|
-
|
|
874
|
-
// begin signing
|
|
875
|
-
txb.signWithKeyPairs([keyPair1])
|
|
876
|
-
|
|
877
|
-
txb.sigOperations.map.get('0000000000000000000000000000000000000000000000000000000000000000:0')[0].log.should.equal('successfully inserted signature')
|
|
878
|
-
txb.sigOperations.map.get('0000000000000000000000000000000000000000000000000000000000000000:0')[1].log.should.equal('successfully inserted public key')
|
|
879
|
-
txb.sigOperations.map.get('0000000000000000000000000000000000000000000000000000000000000000:1')[0].log.should.equal('cannot find keyPair for addressStr 1CUNEBjYrCn2y1SdiUMohaKUi4wpP326Lb')
|
|
880
|
-
txb.sigOperations.map.get('0000000000000000000000000000000000000000000000000000000000000000:1')[1].log.should.equal('cannot find keyPair for addressStr 1CUNEBjYrCn2y1SdiUMohaKUi4wpP326Lb')
|
|
881
|
-
|
|
882
|
-
// transaction not fully signed yet, so should be invalid
|
|
883
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(false)
|
|
884
|
-
|
|
885
|
-
// this should effectively add
|
|
886
|
-
txb.signWithKeyPairs([keyPair2])
|
|
887
|
-
|
|
888
|
-
txb.tx.txOuts[0].script.chunks[2].buf
|
|
889
|
-
.toString('hex')
|
|
890
|
-
.should.equal(saddr1.hashBuf.toString('hex'))
|
|
891
|
-
txb.tx.txOuts[0].valueBn.eq(1.5e8).should.equal(true)
|
|
892
|
-
txb.tx.txOuts[1].valueBn.gt(546).should.equal(true)
|
|
893
|
-
txb.tx.txOuts[1].valueBn.toNumber().should.equal(49996250)
|
|
894
|
-
txb.changeAmountBn.toNumber().should.equal(49996250)
|
|
895
|
-
txb.feeAmountBn.toNumber().should.equal(3750)
|
|
896
|
-
txb.tx.txOuts[1].script.chunks[2].buf
|
|
897
|
-
.toString('hex')
|
|
898
|
-
.should.equal(changeaddr.hashBuf.toString('hex'))
|
|
899
|
-
|
|
900
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
901
|
-
|
|
902
|
-
// re-signing just puts the same signatures back into the same place and
|
|
903
|
-
// thus should still be valid
|
|
904
|
-
txb.signWithKeyPairs([keyPair1, keyPair2])
|
|
905
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
906
|
-
})
|
|
907
|
-
|
|
908
|
-
it('should work with custom scripts', () => {
|
|
909
|
-
// make change address
|
|
910
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
911
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
912
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
913
|
-
|
|
914
|
-
// make addresses to send from (and to)
|
|
915
|
-
const privKey1 = new PrivKey().fromBn(new Bn(1))
|
|
916
|
-
const keyPair1 = new KeyPair().fromPrivKey(privKey1)
|
|
917
|
-
const addr1 = new Address().fromPubKey(keyPair1.pubKey)
|
|
918
|
-
|
|
919
|
-
const customScript = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr1.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
920
|
-
|
|
921
|
-
const txOut1 = TxOut.fromProperties(new Bn(1e8), customScript)
|
|
922
|
-
|
|
923
|
-
const txHashBuf = Buffer.alloc(32)
|
|
924
|
-
txHashBuf.fill(1)
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
const txb = new TxBuilder()
|
|
928
|
-
txb.setFeePerKbNum(0.0001e8)
|
|
929
|
-
txb.setChangeAddress(changeaddr)
|
|
930
|
-
txb.inputFromScript(
|
|
931
|
-
txHashBuf,
|
|
932
|
-
0,
|
|
933
|
-
txOut1,
|
|
934
|
-
customScript
|
|
935
|
-
)
|
|
936
|
-
txb.build()
|
|
937
|
-
should(() => txb.signWithKeyPairs([keyPair1])).not.throw()
|
|
938
|
-
})
|
|
939
|
-
|
|
940
|
-
it('should sign and verify a lot of inputs and outputs', function () {
|
|
941
|
-
// make change address
|
|
942
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
943
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
944
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
945
|
-
|
|
946
|
-
// make addresses to send from (and to)
|
|
947
|
-
const privKey1 = new PrivKey().fromBn(new Bn(1))
|
|
948
|
-
const keyPair1 = new KeyPair().fromPrivKey(privKey1)
|
|
949
|
-
const addr1 = new Address().fromPubKey(keyPair1.pubKey)
|
|
950
|
-
|
|
951
|
-
const privKey2 = new PrivKey().fromBn(new Bn(2))
|
|
952
|
-
const keyPair2 = new KeyPair().fromPrivKey(privKey2)
|
|
953
|
-
const addr2 = new Address().fromPubKey(keyPair2.pubKey)
|
|
954
|
-
|
|
955
|
-
const privKey3 = new PrivKey().fromBn(new Bn(3))
|
|
956
|
-
const keyPair3 = new KeyPair().fromPrivKey(privKey3)
|
|
957
|
-
const addr3 = new Address().fromPubKey(keyPair3.pubKey)
|
|
958
|
-
|
|
959
|
-
const privKey4 = new PrivKey().fromBn(new Bn(4))
|
|
960
|
-
const keyPair4 = new KeyPair().fromPrivKey(privKey4)
|
|
961
|
-
const addr4 = new Address().fromPubKey(keyPair4.pubKey)
|
|
962
|
-
|
|
963
|
-
const privKey5 = new PrivKey().fromBn(new Bn(5))
|
|
964
|
-
const keyPair5 = new KeyPair().fromPrivKey(privKey5)
|
|
965
|
-
const addr5 = new Address().fromPubKey(keyPair5.pubKey)
|
|
966
|
-
|
|
967
|
-
const privKey6 = new PrivKey().fromBn(new Bn(6))
|
|
968
|
-
const keyPair6 = new KeyPair().fromPrivKey(privKey6)
|
|
969
|
-
const addr6 = new Address().fromPubKey(keyPair6.pubKey)
|
|
970
|
-
|
|
971
|
-
const privKey7 = new PrivKey().fromBn(new Bn(7))
|
|
972
|
-
const keyPair7 = new KeyPair().fromPrivKey(privKey7)
|
|
973
|
-
const addr7 = new Address().fromPubKey(keyPair7.pubKey)
|
|
974
|
-
|
|
975
|
-
const privKey8 = new PrivKey().fromBn(new Bn(8))
|
|
976
|
-
const keyPair8 = new KeyPair().fromPrivKey(privKey8)
|
|
977
|
-
const addr8 = new Address().fromPubKey(keyPair8.pubKey)
|
|
978
|
-
|
|
979
|
-
const privKey9 = new PrivKey().fromBn(new Bn(9))
|
|
980
|
-
const keyPair9 = new KeyPair().fromPrivKey(privKey9)
|
|
981
|
-
const addr9 = new Address().fromPubKey(keyPair9.pubKey)
|
|
982
|
-
|
|
983
|
-
const privKey10 = new PrivKey().fromBn(new Bn(10))
|
|
984
|
-
const keyPair10 = new KeyPair().fromPrivKey(privKey10)
|
|
985
|
-
const addr10 = new Address().fromPubKey(keyPair10.pubKey)
|
|
986
|
-
|
|
987
|
-
const privKey11 = new PrivKey().fromBn(new Bn(11))
|
|
988
|
-
const keyPair11 = new KeyPair().fromPrivKey(privKey11)
|
|
989
|
-
const addr11 = new Address().fromPubKey(keyPair11.pubKey)
|
|
990
|
-
|
|
991
|
-
// txOuts that we are spending
|
|
992
|
-
const scriptout1 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr1.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
993
|
-
const scriptout2 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr2.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
994
|
-
const scriptout3 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr3.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
995
|
-
const scriptout4 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr4.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
996
|
-
const scriptout5 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr5.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
997
|
-
const scriptout6 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr6.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
998
|
-
const scriptout7 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr7.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
999
|
-
const scriptout8 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr8.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
1000
|
-
const scriptout9 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr9.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
1001
|
-
const scriptout10 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr10.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
1002
|
-
const scriptout11 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr11.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
1003
|
-
|
|
1004
|
-
const txOut1 = TxOut.fromProperties(new Bn(1e8), scriptout1)
|
|
1005
|
-
const txOut2 = TxOut.fromProperties(new Bn(1e8), scriptout2)
|
|
1006
|
-
const txOut3 = TxOut.fromProperties(new Bn(1e8), scriptout3)
|
|
1007
|
-
const txOut4 = TxOut.fromProperties(new Bn(1e8), scriptout4)
|
|
1008
|
-
const txOut5 = TxOut.fromProperties(new Bn(1e8), scriptout5)
|
|
1009
|
-
const txOut6 = TxOut.fromProperties(new Bn(1e8), scriptout6)
|
|
1010
|
-
const txOut7 = TxOut.fromProperties(new Bn(1e8), scriptout7)
|
|
1011
|
-
const txOut8 = TxOut.fromProperties(new Bn(1e8), scriptout8)
|
|
1012
|
-
const txOut9 = TxOut.fromProperties(new Bn(1e8), scriptout9)
|
|
1013
|
-
const txOut10 = TxOut.fromProperties(new Bn(1e8), scriptout10)
|
|
1014
|
-
const txOut11 = TxOut.fromProperties(new Bn(1e8), scriptout11)
|
|
1015
|
-
// total balance: 11e8
|
|
1016
|
-
|
|
1017
|
-
const txHashBuf = Buffer.alloc(32)
|
|
1018
|
-
txHashBuf.fill(1)
|
|
1019
|
-
const txOutNum1 = 0
|
|
1020
|
-
const txOutNum2 = 1
|
|
1021
|
-
const txOutNum3 = 2
|
|
1022
|
-
const txOutNum4 = 3
|
|
1023
|
-
const txOutNum5 = 4
|
|
1024
|
-
const txOutNum6 = 5
|
|
1025
|
-
const txOutNum7 = 6
|
|
1026
|
-
const txOutNum8 = 7
|
|
1027
|
-
const txOutNum9 = 8
|
|
1028
|
-
const txOutNum10 = 9
|
|
1029
|
-
const txOutNum11 = 10
|
|
1030
|
-
|
|
1031
|
-
const txb = new TxBuilder()
|
|
1032
|
-
txb.setFeePerKbNum(0.0001e8)
|
|
1033
|
-
txb.setChangeAddress(changeaddr)
|
|
1034
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum1, txOut1)
|
|
1035
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum2, txOut2)
|
|
1036
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum3, txOut3)
|
|
1037
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum4, txOut4)
|
|
1038
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum5, txOut5)
|
|
1039
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum6, txOut6)
|
|
1040
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum7, txOut7)
|
|
1041
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum8, txOut8)
|
|
1042
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum9, txOut9)
|
|
1043
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum10, txOut10)
|
|
1044
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum11, txOut11)
|
|
1045
|
-
txb.outputToAddress(new Bn(0.999e8), addr1)
|
|
1046
|
-
txb.outputToAddress(new Bn(0.999e8), addr2)
|
|
1047
|
-
txb.outputToAddress(new Bn(0.999e8), addr3)
|
|
1048
|
-
txb.outputToAddress(new Bn(0.999e8), addr4)
|
|
1049
|
-
txb.outputToAddress(new Bn(0.999e8), addr5)
|
|
1050
|
-
txb.outputToAddress(new Bn(0.999e8), addr6)
|
|
1051
|
-
txb.outputToAddress(new Bn(0.999e8), addr7)
|
|
1052
|
-
txb.outputToAddress(new Bn(0.999e8), addr8)
|
|
1053
|
-
txb.outputToAddress(new Bn(0.999e8), addr9)
|
|
1054
|
-
txb.outputToAddress(new Bn(0.999e8), addr10)
|
|
1055
|
-
txb.outputToAddress(new Bn(0.999e8), addr11)
|
|
1056
|
-
// total sending: 10.989e8 (plus fee)
|
|
1057
|
-
|
|
1058
|
-
txb.build()
|
|
1059
|
-
|
|
1060
|
-
// partially sign - deliberately resulting in invalid tx
|
|
1061
|
-
txb.signWithKeyPairs([
|
|
1062
|
-
keyPair1,
|
|
1063
|
-
keyPair2,
|
|
1064
|
-
keyPair3
|
|
1065
|
-
])
|
|
1066
|
-
|
|
1067
|
-
// transaction not fully signed yet, so should be invalid
|
|
1068
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(false)
|
|
1069
|
-
|
|
1070
|
-
// fully sign
|
|
1071
|
-
txb.signWithKeyPairs([
|
|
1072
|
-
keyPair1,
|
|
1073
|
-
keyPair2,
|
|
1074
|
-
keyPair3,
|
|
1075
|
-
keyPair4,
|
|
1076
|
-
keyPair5,
|
|
1077
|
-
keyPair6,
|
|
1078
|
-
keyPair7,
|
|
1079
|
-
keyPair8,
|
|
1080
|
-
keyPair9,
|
|
1081
|
-
keyPair10,
|
|
1082
|
-
keyPair11,
|
|
1083
|
-
])
|
|
1084
|
-
|
|
1085
|
-
// txb.changeAmountBn.toNumber().should.equal(49996250)
|
|
1086
|
-
// txb.feeAmountBn.toNumber().should.equal(3750)
|
|
1087
|
-
|
|
1088
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1089
|
-
|
|
1090
|
-
// re-signing just puts the same signatures back into the same place and
|
|
1091
|
-
// thus should still be valid
|
|
1092
|
-
txb.signWithKeyPairs([
|
|
1093
|
-
keyPair1,
|
|
1094
|
-
keyPair2,
|
|
1095
|
-
keyPair3,
|
|
1096
|
-
keyPair4,
|
|
1097
|
-
keyPair5,
|
|
1098
|
-
keyPair6,
|
|
1099
|
-
keyPair7,
|
|
1100
|
-
keyPair8,
|
|
1101
|
-
keyPair9,
|
|
1102
|
-
keyPair10,
|
|
1103
|
-
keyPair11,
|
|
1104
|
-
])
|
|
1105
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1106
|
-
})
|
|
1107
|
-
|
|
1108
|
-
it('should be able to add more inputs to pay the fee', function () {
|
|
1109
|
-
// make change address
|
|
1110
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
1111
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
1112
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
1113
|
-
|
|
1114
|
-
// make addresses to send from (and to)
|
|
1115
|
-
const privKey1 = new PrivKey().fromBn(new Bn(1))
|
|
1116
|
-
const keyPair1 = new KeyPair().fromPrivKey(privKey1)
|
|
1117
|
-
const addr1 = new Address().fromPubKey(keyPair1.pubKey)
|
|
1118
|
-
|
|
1119
|
-
const privKey2 = new PrivKey().fromBn(new Bn(2))
|
|
1120
|
-
const keyPair2 = new KeyPair().fromPrivKey(privKey2)
|
|
1121
|
-
const addr2 = new Address().fromPubKey(keyPair2.pubKey)
|
|
1122
|
-
|
|
1123
|
-
const privKey3 = new PrivKey().fromBn(new Bn(3))
|
|
1124
|
-
const keyPair3 = new KeyPair().fromPrivKey(privKey3)
|
|
1125
|
-
const addr3 = new Address().fromPubKey(keyPair3.pubKey)
|
|
1126
|
-
|
|
1127
|
-
const privKey4 = new PrivKey().fromBn(new Bn(4))
|
|
1128
|
-
const keyPair4 = new KeyPair().fromPrivKey(privKey4)
|
|
1129
|
-
const addr4 = new Address().fromPubKey(keyPair4.pubKey)
|
|
1130
|
-
|
|
1131
|
-
// txOuts that we are spending
|
|
1132
|
-
const scriptout1 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr1.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
1133
|
-
const scriptout2 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr2.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
1134
|
-
const scriptout3 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr3.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
1135
|
-
const scriptout4 = new Script().fromString('OP_DUP OP_HASH160 20 0x' + addr4.hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG')
|
|
1136
|
-
|
|
1137
|
-
const txOut1 = TxOut.fromProperties(new Bn(1e8), scriptout1)
|
|
1138
|
-
const txOut2 = TxOut.fromProperties(new Bn(1e8), scriptout2)
|
|
1139
|
-
const txOut3 = TxOut.fromProperties(new Bn(1e8), scriptout3)
|
|
1140
|
-
const txOut4 = TxOut.fromProperties(new Bn(1e8), scriptout4)
|
|
1141
|
-
// total balance: 4e8
|
|
1142
|
-
|
|
1143
|
-
const txHashBuf = Buffer.alloc(32)
|
|
1144
|
-
txHashBuf.fill(1)
|
|
1145
|
-
const txOutNum1 = 0
|
|
1146
|
-
const txOutNum2 = 1
|
|
1147
|
-
const txOutNum3 = 2
|
|
1148
|
-
const txOutNum4 = 3
|
|
1149
|
-
|
|
1150
|
-
const txb = new TxBuilder()
|
|
1151
|
-
txb.setFeePerKbNum(0.0001e8)
|
|
1152
|
-
txb.setChangeAddress(changeaddr)
|
|
1153
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum1, txOut1)
|
|
1154
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum2, txOut2)
|
|
1155
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum3, txOut3)
|
|
1156
|
-
// don't add fourth input yet
|
|
1157
|
-
// txb.inputFromPubKeyHash(txHashBuf, txOutNum4, txOut4)
|
|
1158
|
-
|
|
1159
|
-
// amount is sum of first three, but requires the fourth input to pay the fees
|
|
1160
|
-
txb.outputToAddress(new Bn(3e8), addr1)
|
|
1161
|
-
|
|
1162
|
-
// first try failure
|
|
1163
|
-
let errors = 0
|
|
1164
|
-
try {
|
|
1165
|
-
txb.build()
|
|
1166
|
-
} catch (err) {
|
|
1167
|
-
errors++
|
|
1168
|
-
}
|
|
1169
|
-
errors.should.equal(1)
|
|
1170
|
-
|
|
1171
|
-
// add fourth input. this should succeed.
|
|
1172
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum4, txOut4)
|
|
1173
|
-
txb.build()
|
|
1174
|
-
// fully sign
|
|
1175
|
-
txb.signWithKeyPairs([
|
|
1176
|
-
keyPair1,
|
|
1177
|
-
keyPair2,
|
|
1178
|
-
keyPair3,
|
|
1179
|
-
keyPair4
|
|
1180
|
-
])
|
|
1181
|
-
|
|
1182
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1183
|
-
})
|
|
1184
|
-
|
|
1185
|
-
it('should sign and verify synchronously with no public key inserted at input', function () {
|
|
1186
|
-
function prepareTxBuilder () {
|
|
1187
|
-
const txb = new TxBuilder()
|
|
1188
|
-
|
|
1189
|
-
// make change address
|
|
1190
|
-
const privKey = new PrivKey().fromBn(new Bn(1))
|
|
1191
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
1192
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
1193
|
-
|
|
1194
|
-
// make addresses to send from
|
|
1195
|
-
const privKey1 = new PrivKey().fromBn(new Bn(2))
|
|
1196
|
-
const keyPair1 = new KeyPair().fromPrivKey(privKey1)
|
|
1197
|
-
const addr1 = new Address().fromPubKey(keyPair1.pubKey)
|
|
1198
|
-
|
|
1199
|
-
const privKey2 = new PrivKey().fromBn(new Bn(3))
|
|
1200
|
-
const keyPair2 = new KeyPair().fromPrivKey(privKey2)
|
|
1201
|
-
const addr2 = new Address().fromPubKey(keyPair2.pubKey)
|
|
1202
|
-
|
|
1203
|
-
// make addresses to send to
|
|
1204
|
-
const saddr1 = addr1
|
|
1205
|
-
|
|
1206
|
-
// txOuts that we are spending
|
|
1207
|
-
|
|
1208
|
-
// pubKeyHash out
|
|
1209
|
-
const scriptout1 = new Script().fromString(
|
|
1210
|
-
'OP_DUP OP_HASH160 20 0x' +
|
|
1211
|
-
addr1.hashBuf.toString('hex') +
|
|
1212
|
-
' OP_EQUALVERIFY OP_CHECKSIG'
|
|
1213
|
-
)
|
|
1214
|
-
|
|
1215
|
-
// pubKeyHash out
|
|
1216
|
-
const scriptout2 = new Script().fromString(
|
|
1217
|
-
'OP_DUP OP_HASH160 20 0x' +
|
|
1218
|
-
addr2.hashBuf.toString('hex') +
|
|
1219
|
-
' OP_EQUALVERIFY OP_CHECKSIG'
|
|
1220
|
-
)
|
|
1221
|
-
|
|
1222
|
-
const txOut1 = TxOut.fromProperties(new Bn(1e8), scriptout1)
|
|
1223
|
-
const txOut2 = TxOut.fromProperties(new Bn(1e8), scriptout2)
|
|
1224
|
-
// total balance: 2e8
|
|
1225
|
-
|
|
1226
|
-
const txHashBuf = Buffer.alloc(32)
|
|
1227
|
-
txHashBuf.fill(0)
|
|
1228
|
-
const txOutNum1 = 0
|
|
1229
|
-
const txOutNum2 = 1
|
|
1230
|
-
|
|
1231
|
-
txb.setFeePerKbNum(0.0001e8)
|
|
1232
|
-
txb.setChangeAddress(changeaddr)
|
|
1233
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum1, txOut1)
|
|
1234
|
-
txb.inputFromPubKeyHash(txHashBuf, txOutNum2, txOut2)
|
|
1235
|
-
txb.outputToAddress(new Bn(1.5e8), saddr1) // pubKeyHash address
|
|
1236
|
-
// total sending: 2e8 (plus fee)
|
|
1237
|
-
// txb.randomizeInputs()
|
|
1238
|
-
// txb.randomizeOutputs()
|
|
1239
|
-
|
|
1240
|
-
return {
|
|
1241
|
-
txb,
|
|
1242
|
-
keyPair1,
|
|
1243
|
-
keyPair2,
|
|
1244
|
-
addr1,
|
|
1245
|
-
addr2,
|
|
1246
|
-
saddr1,
|
|
1247
|
-
changeaddr,
|
|
1248
|
-
txOut1,
|
|
1249
|
-
txOut2
|
|
1250
|
-
}
|
|
1251
|
-
}
|
|
1252
|
-
|
|
1253
|
-
function prepareAndBuildTxBuilder () {
|
|
1254
|
-
const obj = prepareTxBuilder()
|
|
1255
|
-
obj.txb.build()
|
|
1256
|
-
return obj
|
|
1257
|
-
}
|
|
1258
|
-
|
|
1259
|
-
// prepare
|
|
1260
|
-
const obj = prepareAndBuildTxBuilder()
|
|
1261
|
-
const txb = obj.txb
|
|
1262
|
-
const keyPair1 = obj.keyPair1
|
|
1263
|
-
const keyPair2 = obj.keyPair2
|
|
1264
|
-
const saddr1 = obj.saddr1
|
|
1265
|
-
const changeaddr = obj.changeaddr
|
|
1266
|
-
|
|
1267
|
-
// begin signing
|
|
1268
|
-
txb.signWithKeyPairs([keyPair1])
|
|
1269
|
-
|
|
1270
|
-
txb.sigOperations.map.get('0000000000000000000000000000000000000000000000000000000000000000:0')[0].log.should.equal('successfully inserted signature')
|
|
1271
|
-
txb.sigOperations.map.get('0000000000000000000000000000000000000000000000000000000000000000:0')[1].log.should.equal('successfully inserted public key')
|
|
1272
|
-
txb.sigOperations.map.get('0000000000000000000000000000000000000000000000000000000000000000:1')[0].log.should.equal('cannot find keyPair for addressStr 1CUNEBjYrCn2y1SdiUMohaKUi4wpP326Lb')
|
|
1273
|
-
txb.sigOperations.map.get('0000000000000000000000000000000000000000000000000000000000000000:1')[1].log.should.equal('cannot find keyPair for addressStr 1CUNEBjYrCn2y1SdiUMohaKUi4wpP326Lb')
|
|
1274
|
-
|
|
1275
|
-
// transaction not fully signed yet, so should be invalid
|
|
1276
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(false)
|
|
1277
|
-
|
|
1278
|
-
// this should effectively add
|
|
1279
|
-
txb.signWithKeyPairs([keyPair2])
|
|
1280
|
-
|
|
1281
|
-
txb.tx.txOuts[0].script.chunks[2].buf
|
|
1282
|
-
.toString('hex')
|
|
1283
|
-
.should.equal(saddr1.hashBuf.toString('hex'))
|
|
1284
|
-
txb.tx.txOuts[0].valueBn.eq(1.5e8).should.equal(true)
|
|
1285
|
-
txb.tx.txOuts[1].valueBn.gt(546).should.equal(true)
|
|
1286
|
-
txb.tx.txOuts[1].valueBn.toNumber().should.equal(49996250)
|
|
1287
|
-
txb.changeAmountBn.toNumber().should.equal(49996250)
|
|
1288
|
-
txb.feeAmountBn.toNumber().should.equal(3750)
|
|
1289
|
-
txb.tx.txOuts[1].script.chunks[2].buf
|
|
1290
|
-
.toString('hex')
|
|
1291
|
-
.should.equal(changeaddr.hashBuf.toString('hex'))
|
|
1292
|
-
|
|
1293
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1294
|
-
|
|
1295
|
-
// re-signing just puts the same signatures back into the same place and
|
|
1296
|
-
// thus should still be valid
|
|
1297
|
-
txb.signWithKeyPairs([keyPair1, keyPair2])
|
|
1298
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1299
|
-
})
|
|
1300
|
-
|
|
1301
|
-
it('should sign and verify a really large number of inputs and outputs', function () {
|
|
1302
|
-
this.timeout(10000)
|
|
1303
|
-
const nIns = 100
|
|
1304
|
-
const nOuts = 100
|
|
1305
|
-
|
|
1306
|
-
// make change address
|
|
1307
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
1308
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
1309
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
1310
|
-
|
|
1311
|
-
// make addresses to send from (and to)
|
|
1312
|
-
const privKeys = []
|
|
1313
|
-
const keyPairs = []
|
|
1314
|
-
const addrs = []
|
|
1315
|
-
for (let i = 0; i < nIns; i++) {
|
|
1316
|
-
privKeys.push(new PrivKey().fromBn(new Bn(i + 1)))
|
|
1317
|
-
keyPairs.push(new KeyPair().fromPrivKey(privKeys[i]))
|
|
1318
|
-
addrs.push(new Address().fromPubKey(keyPairs[i].pubKey))
|
|
1319
|
-
}
|
|
1320
|
-
|
|
1321
|
-
// txOuts that we are spending
|
|
1322
|
-
const scriptouts = []
|
|
1323
|
-
const txOuts = []
|
|
1324
|
-
for (let i = 0; i < nIns; i++) {
|
|
1325
|
-
scriptouts.push(new Script().fromString('OP_DUP OP_HASH160 20 0x' + addrs[i].hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG'))
|
|
1326
|
-
txOuts.push(TxOut.fromProperties(new Bn(1e8), scriptouts[i]))
|
|
1327
|
-
}
|
|
1328
|
-
// total input amount: nIns * 1e8
|
|
1329
|
-
|
|
1330
|
-
const txb = new TxBuilder()
|
|
1331
|
-
txb.setFeePerKbNum(0.0001e8)
|
|
1332
|
-
txb.setChangeAddress(changeaddr)
|
|
1333
|
-
|
|
1334
|
-
// put inputs into tx
|
|
1335
|
-
for (let i = 0; i < nIns; i++) {
|
|
1336
|
-
const txHashBuf = Buffer.alloc(32)
|
|
1337
|
-
txHashBuf.fill(i + 1)
|
|
1338
|
-
txb.inputFromPubKeyHash(txHashBuf, i, txOuts[i])
|
|
1339
|
-
}
|
|
1340
|
-
|
|
1341
|
-
// put outputs into tx
|
|
1342
|
-
for (let i = 0; i < nOuts; i++) {
|
|
1343
|
-
txb.outputToAddress(new Bn(0.999e8), addrs[i])
|
|
1344
|
-
}
|
|
1345
|
-
// total sending: nOuts * 0.999e8
|
|
1346
|
-
|
|
1347
|
-
txb.build({ useAllInputs: true })
|
|
1348
|
-
|
|
1349
|
-
txb.tx.txIns.length.should.equal(nIns)
|
|
1350
|
-
txb.tx.txOuts.length.should.equal(nOuts + 1)
|
|
1351
|
-
|
|
1352
|
-
// partially sign - deliberately resulting in invalid tx
|
|
1353
|
-
txb.signWithKeyPairs([
|
|
1354
|
-
keyPairs[0],
|
|
1355
|
-
keyPairs[1],
|
|
1356
|
-
keyPairs[2]
|
|
1357
|
-
])
|
|
1358
|
-
|
|
1359
|
-
// transaction not fully signed yet, so should be invalid
|
|
1360
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(false)
|
|
1361
|
-
|
|
1362
|
-
// fully sign
|
|
1363
|
-
txb.signWithKeyPairs(keyPairs)
|
|
1364
|
-
|
|
1365
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1366
|
-
})
|
|
1367
|
-
|
|
1368
|
-
it('should sign and verify a large number of inputs and outputs with converting to/from JSON', function () {
|
|
1369
|
-
this.timeout(10000)
|
|
1370
|
-
const nIns = 50
|
|
1371
|
-
const nOuts = 50
|
|
1372
|
-
|
|
1373
|
-
// make change address
|
|
1374
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
1375
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
1376
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
1377
|
-
|
|
1378
|
-
// make addresses to send from (and to)
|
|
1379
|
-
const privKeys = []
|
|
1380
|
-
const keyPairs = []
|
|
1381
|
-
const addrs = []
|
|
1382
|
-
for (let i = 0; i < nIns; i++) {
|
|
1383
|
-
privKeys.push(new PrivKey().fromBn(new Bn(i + 1)))
|
|
1384
|
-
keyPairs.push(new KeyPair().fromPrivKey(privKeys[i]))
|
|
1385
|
-
addrs.push(new Address().fromPubKey(keyPairs[i].pubKey))
|
|
1386
|
-
}
|
|
1387
|
-
|
|
1388
|
-
// txOuts that we are spending
|
|
1389
|
-
const scriptouts = []
|
|
1390
|
-
const txOuts = []
|
|
1391
|
-
for (let i = 0; i < nIns; i++) {
|
|
1392
|
-
scriptouts.push(new Script().fromString('OP_DUP OP_HASH160 20 0x' + addrs[i].hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG'))
|
|
1393
|
-
txOuts.push(TxOut.fromProperties(new Bn(1e8), scriptouts[i]))
|
|
1394
|
-
}
|
|
1395
|
-
// total input amount: nIns * 1e8
|
|
1396
|
-
|
|
1397
|
-
let txb = new TxBuilder()
|
|
1398
|
-
txb.setFeePerKbNum(0.0001e8)
|
|
1399
|
-
txb.setChangeAddress(changeaddr)
|
|
1400
|
-
|
|
1401
|
-
// put inputs into tx
|
|
1402
|
-
for (let i = 0; i < nIns; i++) {
|
|
1403
|
-
const txHashBuf = Buffer.alloc(32)
|
|
1404
|
-
txHashBuf.fill(i + 1)
|
|
1405
|
-
txb.inputFromPubKeyHash(txHashBuf, i, txOuts[i])
|
|
1406
|
-
}
|
|
1407
|
-
|
|
1408
|
-
// put outputs into tx
|
|
1409
|
-
for (let i = 0; i < nOuts; i++) {
|
|
1410
|
-
txb.outputToAddress(new Bn(0.999e8), addrs[i])
|
|
1411
|
-
}
|
|
1412
|
-
// total sending: nOuts * 0.999e8
|
|
1413
|
-
|
|
1414
|
-
txb.build({ useAllInputs: true })
|
|
1415
|
-
|
|
1416
|
-
txb.tx.txIns.length.should.equal(nIns)
|
|
1417
|
-
txb.tx.txOuts.length.should.equal(nOuts + 1)
|
|
1418
|
-
|
|
1419
|
-
// before signing, convert to/from JSON, simulating real-world wallet use-case
|
|
1420
|
-
txb = TxBuilder.fromJSON(txb.toJSON())
|
|
1421
|
-
|
|
1422
|
-
// partially sign - deliberately resulting in invalid tx
|
|
1423
|
-
txb.signWithKeyPairs([
|
|
1424
|
-
keyPairs[0],
|
|
1425
|
-
keyPairs[1],
|
|
1426
|
-
keyPairs[2]
|
|
1427
|
-
])
|
|
1428
|
-
|
|
1429
|
-
// transaction not fully signed yet, so should be invalid
|
|
1430
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(false)
|
|
1431
|
-
|
|
1432
|
-
// fully sign
|
|
1433
|
-
txb.signWithKeyPairs(keyPairs)
|
|
1434
|
-
|
|
1435
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1436
|
-
})
|
|
1437
|
-
|
|
1438
|
-
it('should sign and verify a large number of inputs and outputs, with incrementing amounts, with converting to/from JSON', function () {
|
|
1439
|
-
this.timeout(10000)
|
|
1440
|
-
const nIns = 50
|
|
1441
|
-
const nOuts = 30
|
|
1442
|
-
|
|
1443
|
-
// make change address
|
|
1444
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
1445
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
1446
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
1447
|
-
|
|
1448
|
-
// make addresses to send from (and to)
|
|
1449
|
-
const privKeys = []
|
|
1450
|
-
const keyPairs = []
|
|
1451
|
-
const addrs = []
|
|
1452
|
-
for (let i = 0; i < nIns; i++) {
|
|
1453
|
-
privKeys.push(new PrivKey().fromBn(new Bn(i + 1)))
|
|
1454
|
-
keyPairs.push(new KeyPair().fromPrivKey(privKeys[i]))
|
|
1455
|
-
addrs.push(new Address().fromPubKey(keyPairs[i].pubKey))
|
|
1456
|
-
}
|
|
1457
|
-
|
|
1458
|
-
// txOuts that we are spending
|
|
1459
|
-
const scriptouts = []
|
|
1460
|
-
const txOuts = []
|
|
1461
|
-
for (let i = 0; i < nIns; i++) {
|
|
1462
|
-
scriptouts.push(new Script().fromString('OP_DUP OP_HASH160 20 0x' + addrs[i].hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG'))
|
|
1463
|
-
txOuts.push(TxOut.fromProperties(new Bn(1e8 * (i + 1)), scriptouts[i]))
|
|
1464
|
-
}
|
|
1465
|
-
// total input amount: nIns * 1e8
|
|
1466
|
-
|
|
1467
|
-
let txb = new TxBuilder()
|
|
1468
|
-
txb.setFeePerKbNum(0.0001e8)
|
|
1469
|
-
txb.setChangeAddress(changeaddr)
|
|
1470
|
-
|
|
1471
|
-
// put inputs into tx
|
|
1472
|
-
for (let i = 0; i < nIns; i++) {
|
|
1473
|
-
const txHashBuf = Buffer.alloc(32)
|
|
1474
|
-
txHashBuf.fill(i + 1)
|
|
1475
|
-
txb.inputFromPubKeyHash(txHashBuf, i, txOuts[i])
|
|
1476
|
-
}
|
|
1477
|
-
|
|
1478
|
-
// put outputs into tx
|
|
1479
|
-
for (let i = 0; i < nOuts; i++) {
|
|
1480
|
-
txb.outputToAddress(new Bn(0.999e8 * (i + 1)), addrs[i])
|
|
1481
|
-
}
|
|
1482
|
-
// total sending: nOuts * 0.999e8
|
|
1483
|
-
|
|
1484
|
-
txb.build({ useAllInputs: true })
|
|
1485
|
-
|
|
1486
|
-
txb.tx.txIns.length.should.equal(nIns)
|
|
1487
|
-
txb.tx.txOuts.length.should.equal(nOuts + 1)
|
|
1488
|
-
|
|
1489
|
-
// before signing, convert to/from JSON, simulating real-world wallet use-case
|
|
1490
|
-
txb = TxBuilder.fromJSON(txb.toJSON())
|
|
1491
|
-
|
|
1492
|
-
// partially sign - deliberately resulting in invalid tx
|
|
1493
|
-
txb.signWithKeyPairs([
|
|
1494
|
-
keyPairs[0],
|
|
1495
|
-
keyPairs[1],
|
|
1496
|
-
keyPairs[2]
|
|
1497
|
-
])
|
|
1498
|
-
|
|
1499
|
-
// transaction not fully signed yet, so should be invalid
|
|
1500
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(false)
|
|
1501
|
-
|
|
1502
|
-
// fully sign
|
|
1503
|
-
txb.signWithKeyPairs(keyPairs)
|
|
1504
|
-
|
|
1505
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1506
|
-
})
|
|
1507
|
-
|
|
1508
|
-
it('should sign and verify a large number of inputs and outputs, with the same key, with converting to/from JSON', function () {
|
|
1509
|
-
this.timeout(10000)
|
|
1510
|
-
const nIns = 50
|
|
1511
|
-
const nOuts = 30
|
|
1512
|
-
|
|
1513
|
-
// make change address
|
|
1514
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
1515
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
1516
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
1517
|
-
|
|
1518
|
-
// make addresses to send from (and to)
|
|
1519
|
-
const privKeys = []
|
|
1520
|
-
const keyPairs = []
|
|
1521
|
-
const addrs = []
|
|
1522
|
-
for (let i = 0; i < nIns; i++) {
|
|
1523
|
-
privKeys.push(new PrivKey().fromBn(new Bn(1)))
|
|
1524
|
-
keyPairs.push(new KeyPair().fromPrivKey(privKeys[i]))
|
|
1525
|
-
addrs.push(new Address().fromPubKey(keyPairs[i].pubKey))
|
|
1526
|
-
}
|
|
1527
|
-
|
|
1528
|
-
// txOuts that we are spending
|
|
1529
|
-
const scriptouts = []
|
|
1530
|
-
const txOuts = []
|
|
1531
|
-
for (let i = 0; i < nIns; i++) {
|
|
1532
|
-
scriptouts.push(new Script().fromString('OP_DUP OP_HASH160 20 0x' + addrs[i].hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG'))
|
|
1533
|
-
txOuts.push(TxOut.fromProperties(new Bn(1e8), scriptouts[i]))
|
|
1534
|
-
}
|
|
1535
|
-
// total input amount: nIns * 1e8
|
|
1536
|
-
|
|
1537
|
-
let txb = new TxBuilder()
|
|
1538
|
-
txb.setFeePerKbNum(0.0001e8)
|
|
1539
|
-
txb.setChangeAddress(changeaddr)
|
|
1540
|
-
|
|
1541
|
-
// put inputs into tx
|
|
1542
|
-
for (let i = 0; i < nIns; i++) {
|
|
1543
|
-
const txHashBuf = Buffer.alloc(32)
|
|
1544
|
-
txHashBuf.fill(i + 1)
|
|
1545
|
-
txb.inputFromPubKeyHash(txHashBuf, i, txOuts[i])
|
|
1546
|
-
}
|
|
1547
|
-
|
|
1548
|
-
// put outputs into tx
|
|
1549
|
-
for (let i = 0; i < nOuts; i++) {
|
|
1550
|
-
txb.outputToAddress(new Bn(0.999e8), addrs[i])
|
|
1551
|
-
}
|
|
1552
|
-
// total sending: nOuts * 0.999e8
|
|
1553
|
-
|
|
1554
|
-
txb.build({ useAllInputs: true })
|
|
1555
|
-
|
|
1556
|
-
txb.tx.txIns.length.should.equal(nIns)
|
|
1557
|
-
txb.tx.txOuts.length.should.equal(nOuts + 1)
|
|
1558
|
-
|
|
1559
|
-
// before signing, convert to/from JSON, simulating real-world wallet use-case
|
|
1560
|
-
txb = TxBuilder.fromJSON(txb.toJSON())
|
|
1561
|
-
|
|
1562
|
-
// fully sign
|
|
1563
|
-
txb.signWithKeyPairs(keyPairs)
|
|
1564
|
-
|
|
1565
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1566
|
-
})
|
|
1567
|
-
|
|
1568
|
-
it('should sign and verify a large number of inputs and outputs, with BIP 69 sorting, with converting to/from JSON', function () {
|
|
1569
|
-
this.timeout(10000)
|
|
1570
|
-
const nIns = 50
|
|
1571
|
-
const nOuts = 30
|
|
1572
|
-
|
|
1573
|
-
// make change address
|
|
1574
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
1575
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
1576
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
1577
|
-
|
|
1578
|
-
// make addresses to send from (and to)
|
|
1579
|
-
const privKeys = []
|
|
1580
|
-
const keyPairs = []
|
|
1581
|
-
const addrs = []
|
|
1582
|
-
for (let i = 0; i < nIns; i++) {
|
|
1583
|
-
privKeys.push(new PrivKey().fromBn(new Bn(1)))
|
|
1584
|
-
keyPairs.push(new KeyPair().fromPrivKey(privKeys[i]))
|
|
1585
|
-
addrs.push(new Address().fromPubKey(keyPairs[i].pubKey))
|
|
1586
|
-
}
|
|
1587
|
-
|
|
1588
|
-
// txOuts that we are spending
|
|
1589
|
-
const scriptouts = []
|
|
1590
|
-
const txOuts = []
|
|
1591
|
-
for (let i = 0; i < nIns; i++) {
|
|
1592
|
-
scriptouts.push(new Script().fromString('OP_DUP OP_HASH160 20 0x' + addrs[i].hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG'))
|
|
1593
|
-
txOuts.push(TxOut.fromProperties(new Bn(1e8), scriptouts[i]))
|
|
1594
|
-
}
|
|
1595
|
-
// total input amount: nIns * 1e8
|
|
1596
|
-
|
|
1597
|
-
let txb = new TxBuilder()
|
|
1598
|
-
txb.setFeePerKbNum(0.0001e8)
|
|
1599
|
-
txb.setChangeAddress(changeaddr)
|
|
1600
|
-
|
|
1601
|
-
// put inputs into tx
|
|
1602
|
-
for (let i = 0; i < nIns; i++) {
|
|
1603
|
-
const txHashBuf = Buffer.alloc(32)
|
|
1604
|
-
txHashBuf.fill(i + 1)
|
|
1605
|
-
txb.inputFromPubKeyHash(txHashBuf, i, txOuts[i])
|
|
1606
|
-
}
|
|
1607
|
-
|
|
1608
|
-
// put outputs into tx
|
|
1609
|
-
for (let i = 0; i < nOuts; i++) {
|
|
1610
|
-
txb.outputToAddress(new Bn(0.999e8), addrs[i])
|
|
1611
|
-
}
|
|
1612
|
-
// total sending: nOuts * 0.999e8
|
|
1613
|
-
|
|
1614
|
-
txb.build({ useAllInputs: true })
|
|
1615
|
-
txb.sort()
|
|
1616
|
-
|
|
1617
|
-
txb.tx.txIns.length.should.equal(nIns)
|
|
1618
|
-
txb.tx.txOuts.length.should.equal(nOuts + 1)
|
|
1619
|
-
|
|
1620
|
-
// before signing, convert to/from JSON, simulating real-world wallet use-case
|
|
1621
|
-
txb = TxBuilder.fromJSON(txb.toJSON())
|
|
1622
|
-
|
|
1623
|
-
// fully sign
|
|
1624
|
-
txb.signWithKeyPairs(keyPairs)
|
|
1625
|
-
|
|
1626
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1627
|
-
})
|
|
1628
|
-
|
|
1629
|
-
it('should sign and verify a large number of inputs and outputs, with BIP 69 sorting with incrementing amounts, with converting to/from JSON', function () {
|
|
1630
|
-
this.timeout(10000)
|
|
1631
|
-
const nIns = 50
|
|
1632
|
-
const nOuts = 30
|
|
1633
|
-
|
|
1634
|
-
// make change address
|
|
1635
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
1636
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
1637
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
1638
|
-
|
|
1639
|
-
// make addresses to send from (and to)
|
|
1640
|
-
const privKeys = []
|
|
1641
|
-
const keyPairs = []
|
|
1642
|
-
const addrs = []
|
|
1643
|
-
for (let i = 0; i < nIns; i++) {
|
|
1644
|
-
privKeys.push(new PrivKey().fromBn(new Bn(1)))
|
|
1645
|
-
keyPairs.push(new KeyPair().fromPrivKey(privKeys[i]))
|
|
1646
|
-
addrs.push(new Address().fromPubKey(keyPairs[i].pubKey))
|
|
1647
|
-
}
|
|
1648
|
-
|
|
1649
|
-
// txOuts that we are spending
|
|
1650
|
-
const scriptouts = []
|
|
1651
|
-
const txOuts = []
|
|
1652
|
-
for (let i = 0; i < nIns; i++) {
|
|
1653
|
-
scriptouts.push(new Script().fromString('OP_DUP OP_HASH160 20 0x' + addrs[i].hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG'))
|
|
1654
|
-
txOuts.push(TxOut.fromProperties(new Bn(1e8 + i), scriptouts[i]))
|
|
1655
|
-
}
|
|
1656
|
-
// total input amount: nIns * 1e8...plus a bit due to incrementing amounts
|
|
1657
|
-
|
|
1658
|
-
let txb = new TxBuilder()
|
|
1659
|
-
txb.setFeePerKbNum(0.0001e8)
|
|
1660
|
-
txb.setChangeAddress(changeaddr)
|
|
1661
|
-
|
|
1662
|
-
// put inputs into tx
|
|
1663
|
-
for (let i = 0; i < nIns; i++) {
|
|
1664
|
-
const txHashBuf = Buffer.alloc(32)
|
|
1665
|
-
txHashBuf.fill(i + 1)
|
|
1666
|
-
txb.inputFromPubKeyHash(txHashBuf, i, txOuts[i])
|
|
1667
|
-
}
|
|
1668
|
-
|
|
1669
|
-
// put outputs into tx
|
|
1670
|
-
for (let i = 0; i < nOuts; i++) {
|
|
1671
|
-
txb.outputToAddress(new Bn(0.999e8), addrs[i])
|
|
1672
|
-
}
|
|
1673
|
-
// total sending: nOuts * 0.999e8
|
|
1674
|
-
|
|
1675
|
-
txb.build({ useAllInputs: true })
|
|
1676
|
-
txb.sort()
|
|
1677
|
-
|
|
1678
|
-
txb.tx.txIns.length.should.equal(nIns)
|
|
1679
|
-
txb.tx.txOuts.length.should.equal(nOuts + 1)
|
|
1680
|
-
|
|
1681
|
-
// before signing, convert to/from JSON, simulating real-world wallet use-case
|
|
1682
|
-
txb = TxBuilder.fromJSON(txb.toJSON())
|
|
1683
|
-
|
|
1684
|
-
// fully sign
|
|
1685
|
-
txb.signWithKeyPairs(keyPairs)
|
|
1686
|
-
|
|
1687
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1688
|
-
})
|
|
1689
|
-
|
|
1690
|
-
it('should sign and verify a large number of inputs and outputs, with BIP 69 sorting with decrementing amounts, with converting to/from JSON', function () {
|
|
1691
|
-
this.timeout(10000)
|
|
1692
|
-
const nIns = 50
|
|
1693
|
-
const nOuts = 30
|
|
1694
|
-
|
|
1695
|
-
// make change address
|
|
1696
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
1697
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
1698
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
1699
|
-
|
|
1700
|
-
// make addresses to send from (and to)
|
|
1701
|
-
const privKeys = []
|
|
1702
|
-
const keyPairs = []
|
|
1703
|
-
const addrs = []
|
|
1704
|
-
for (let i = 0; i < nIns; i++) {
|
|
1705
|
-
privKeys.push(new PrivKey().fromBn(new Bn(1)))
|
|
1706
|
-
keyPairs.push(new KeyPair().fromPrivKey(privKeys[i]))
|
|
1707
|
-
addrs.push(new Address().fromPubKey(keyPairs[i].pubKey))
|
|
1708
|
-
}
|
|
1709
|
-
|
|
1710
|
-
// txOuts that we are spending
|
|
1711
|
-
const scriptouts = []
|
|
1712
|
-
const txOuts = []
|
|
1713
|
-
for (let i = 0; i < nIns; i++) {
|
|
1714
|
-
scriptouts.push(new Script().fromString('OP_DUP OP_HASH160 20 0x' + addrs[i].hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG'))
|
|
1715
|
-
txOuts.push(TxOut.fromProperties(new Bn(1e8 + 10000 - i), scriptouts[i]))
|
|
1716
|
-
}
|
|
1717
|
-
// total input amount: nIns * 1e8...plus a bit due to incrementing amounts
|
|
1718
|
-
|
|
1719
|
-
let txb = new TxBuilder()
|
|
1720
|
-
txb.setFeePerKbNum(0.0001e8)
|
|
1721
|
-
txb.setChangeAddress(changeaddr)
|
|
1722
|
-
|
|
1723
|
-
// put inputs into tx
|
|
1724
|
-
for (let i = 0; i < nIns; i++) {
|
|
1725
|
-
const txHashBuf = Buffer.alloc(32)
|
|
1726
|
-
txHashBuf.fill(i + 1)
|
|
1727
|
-
txb.inputFromPubKeyHash(txHashBuf, i, txOuts[i])
|
|
1728
|
-
}
|
|
1729
|
-
|
|
1730
|
-
// put outputs into tx
|
|
1731
|
-
for (let i = 0; i < nOuts; i++) {
|
|
1732
|
-
txb.outputToAddress(new Bn(0.999e8), addrs[i])
|
|
1733
|
-
}
|
|
1734
|
-
// total sending: nOuts * 0.999e8
|
|
1735
|
-
|
|
1736
|
-
txb.build({ useAllInputs: true })
|
|
1737
|
-
txb.sort()
|
|
1738
|
-
|
|
1739
|
-
txb.tx.txIns.length.should.equal(nIns)
|
|
1740
|
-
txb.tx.txOuts.length.should.equal(nOuts + 1)
|
|
1741
|
-
|
|
1742
|
-
// before signing, convert to/from JSON, simulating real-world wallet use-case
|
|
1743
|
-
txb = TxBuilder.fromJSON(txb.toJSON())
|
|
1744
|
-
|
|
1745
|
-
// fully sign
|
|
1746
|
-
txb.signWithKeyPairs(keyPairs)
|
|
1747
|
-
|
|
1748
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1749
|
-
})
|
|
1750
|
-
/*
|
|
1751
|
-
it('should sign and verify with one output if change is less than dust with dropping change with converting to/from JSON', function () {
|
|
1752
|
-
this.timeout(10000)
|
|
1753
|
-
const nIns = 3
|
|
1754
|
-
const nOuts = 1
|
|
1755
|
-
|
|
1756
|
-
// make change address
|
|
1757
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
1758
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
1759
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
1760
|
-
|
|
1761
|
-
// make addresses to send from (and to)
|
|
1762
|
-
const privKeys = []
|
|
1763
|
-
const keyPairs = []
|
|
1764
|
-
const addrs = []
|
|
1765
|
-
for (let i = 0; i < nIns; i++) {
|
|
1766
|
-
privKeys.push(new PrivKey().fromBn(new Bn(1)))
|
|
1767
|
-
keyPairs.push(new KeyPair().fromPrivKey(privKeys[i]))
|
|
1768
|
-
addrs.push(new Address().fromPubKey(keyPairs[i].pubKey))
|
|
1769
|
-
}
|
|
1770
|
-
|
|
1771
|
-
// txOuts that we are spending
|
|
1772
|
-
const scriptouts = []
|
|
1773
|
-
const txOuts = []
|
|
1774
|
-
for (let i = 0; i < nIns; i++) {
|
|
1775
|
-
scriptouts.push(new Script().fromString('OP_DUP OP_HASH160 20 0x' + addrs[i].hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG'))
|
|
1776
|
-
txOuts.push(TxOut.fromProperties(new Bn(200), scriptouts[i]))
|
|
1777
|
-
}
|
|
1778
|
-
// total input amount: nIns * 1e8 = 600
|
|
1779
|
-
|
|
1780
|
-
let txb = new TxBuilder()
|
|
1781
|
-
txb.setFeePerKbNum(1)
|
|
1782
|
-
txb.sendDustChangeToFees(true)
|
|
1783
|
-
txb.setChangeAddress(changeaddr)
|
|
1784
|
-
|
|
1785
|
-
// put inputs into tx
|
|
1786
|
-
for (let i = 0; i < nIns; i++) {
|
|
1787
|
-
const txHashBuf = Buffer.alloc(32)
|
|
1788
|
-
txHashBuf.fill(i + 1)
|
|
1789
|
-
txb.inputFromPubKeyHash(txHashBuf, i, txOuts[i])
|
|
1790
|
-
}
|
|
1791
|
-
|
|
1792
|
-
// put outputs into tx
|
|
1793
|
-
for (let i = 0; i < nOuts; i++) {
|
|
1794
|
-
txb.outputToAddress(new Bn(590), addrs[i])
|
|
1795
|
-
}
|
|
1796
|
-
// total sending: nOuts * 0.999e8
|
|
1797
|
-
|
|
1798
|
-
txb.build({ useAllInputs: true })
|
|
1799
|
-
console.log(tx.toJSON())
|
|
1800
|
-
txb.tx.txIns.length.should.equal(nIns)
|
|
1801
|
-
txb.tx.txOuts.length.should.equal(nOuts)
|
|
1802
|
-
|
|
1803
|
-
// before signing, convert to/from JSON, simulating real-world wallet use-case
|
|
1804
|
-
txb = TxBuilder.fromJSON(txb.toJSON())
|
|
1805
|
-
|
|
1806
|
-
// fully sign
|
|
1807
|
-
txb.signWithKeyPairs(keyPairs)
|
|
1808
|
-
|
|
1809
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1810
|
-
})
|
|
1811
|
-
|
|
1812
|
-
it('should sign and verify a large number of inputs and outputs, with very small amounts for inputs (1000 satoshis), with converting to/from JSON', function () {
|
|
1813
|
-
this.timeout(10000)
|
|
1814
|
-
const nIns = 100
|
|
1815
|
-
const nOuts = 1
|
|
1816
|
-
|
|
1817
|
-
// make change address
|
|
1818
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
1819
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
1820
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
1821
|
-
|
|
1822
|
-
// make addresses to send from (and to)
|
|
1823
|
-
const privKeys = []
|
|
1824
|
-
const keyPairs = []
|
|
1825
|
-
const addrs = []
|
|
1826
|
-
for (let i = 0; i < nIns; i++) {
|
|
1827
|
-
privKeys.push(new PrivKey().fromBn(new Bn(i + 1)))
|
|
1828
|
-
keyPairs.push(new KeyPair().fromPrivKey(privKeys[i]))
|
|
1829
|
-
addrs.push(new Address().fromPubKey(keyPairs[i].pubKey))
|
|
1830
|
-
}
|
|
1831
|
-
|
|
1832
|
-
// txOuts that we are spending
|
|
1833
|
-
const scriptouts = []
|
|
1834
|
-
const txOuts = []
|
|
1835
|
-
for (let i = 0; i < nIns; i++) {
|
|
1836
|
-
scriptouts.push(new Script().fromString('OP_DUP OP_HASH160 20 0x' + addrs[i].hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG'))
|
|
1837
|
-
txOuts.push(TxOut.fromProperties(new Bn(1000), scriptouts[i]))
|
|
1838
|
-
}
|
|
1839
|
-
// total input amount: nIns * 1e8
|
|
1840
|
-
|
|
1841
|
-
let txb = new TxBuilder()
|
|
1842
|
-
txb.setFeePerKbNum(0.0000500e8)
|
|
1843
|
-
txb.setChangeAddress(changeaddr)
|
|
1844
|
-
|
|
1845
|
-
// put inputs into tx
|
|
1846
|
-
for (let i = 0; i < nIns; i++) {
|
|
1847
|
-
const txHashBuf = Buffer.alloc(32)
|
|
1848
|
-
txHashBuf.fill(i + 1)
|
|
1849
|
-
txb.inputFromPubKeyHash(txHashBuf, i, txOuts[i])
|
|
1850
|
-
}
|
|
1851
|
-
|
|
1852
|
-
// put outputs into tx
|
|
1853
|
-
for (let i = 0; i < nOuts; i++) {
|
|
1854
|
-
txb.outputToAddress(new Bn(1000), addrs[i])
|
|
1855
|
-
}
|
|
1856
|
-
// total sending: nOuts * 0.999e8
|
|
1857
|
-
|
|
1858
|
-
txb.build({ useAllInputs: true })
|
|
1859
|
-
|
|
1860
|
-
txb.tx.txIns.length.should.equal(nIns)
|
|
1861
|
-
txb.tx.txOuts.length.should.equal(nOuts + 1)
|
|
1862
|
-
|
|
1863
|
-
// before signing, convert to/from JSON, simulating real-world wallet use-case
|
|
1864
|
-
txb = TxBuilder.fromJSON(txb.toJSON())
|
|
1865
|
-
|
|
1866
|
-
// fully sign
|
|
1867
|
-
txb.signWithKeyPairs(keyPairs)
|
|
1868
|
-
|
|
1869
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1870
|
-
})
|
|
1871
|
-
*/
|
|
1872
|
-
it('should sign and verify a large number of inputs and outputs, with very small amounts for inputs (1000 satoshis, 0.01 sat/byte fee), with converting to/from JSON', function () {
|
|
1873
|
-
this.timeout(10000)
|
|
1874
|
-
const nIns = 100
|
|
1875
|
-
const nOuts = 1
|
|
1876
|
-
|
|
1877
|
-
// make change address
|
|
1878
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
1879
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
1880
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
1881
|
-
|
|
1882
|
-
// make addresses to send from (and to)
|
|
1883
|
-
const privKeys = []
|
|
1884
|
-
const keyPairs = []
|
|
1885
|
-
const addrs = []
|
|
1886
|
-
for (let i = 0; i < nIns; i++) {
|
|
1887
|
-
privKeys.push(new PrivKey().fromBn(new Bn(i + 1)))
|
|
1888
|
-
keyPairs.push(new KeyPair().fromPrivKey(privKeys[i]))
|
|
1889
|
-
addrs.push(new Address().fromPubKey(keyPairs[i].pubKey))
|
|
1890
|
-
}
|
|
1891
|
-
|
|
1892
|
-
// txOuts that we are spending
|
|
1893
|
-
const scriptouts = []
|
|
1894
|
-
const txOuts = []
|
|
1895
|
-
for (let i = 0; i < nIns; i++) {
|
|
1896
|
-
scriptouts.push(new Script().fromString('OP_DUP OP_HASH160 20 0x' + addrs[i].hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG'))
|
|
1897
|
-
txOuts.push(TxOut.fromProperties(new Bn(1000), scriptouts[i]))
|
|
1898
|
-
}
|
|
1899
|
-
// total input amount: nIns * 1e8
|
|
1900
|
-
|
|
1901
|
-
let txb = new TxBuilder()
|
|
1902
|
-
txb.setFeePerKbNum(0.0000010e8)
|
|
1903
|
-
txb.setChangeAddress(changeaddr)
|
|
1904
|
-
|
|
1905
|
-
// put inputs into tx
|
|
1906
|
-
for (let i = 0; i < nIns; i++) {
|
|
1907
|
-
const txHashBuf = Buffer.alloc(32)
|
|
1908
|
-
txHashBuf.fill(i + 1)
|
|
1909
|
-
txb.inputFromPubKeyHash(txHashBuf, i, txOuts[i])
|
|
1910
|
-
}
|
|
1911
|
-
|
|
1912
|
-
// put outputs into tx
|
|
1913
|
-
for (let i = 0; i < nOuts; i++) {
|
|
1914
|
-
txb.outputToAddress(new Bn(1000), addrs[i])
|
|
1915
|
-
}
|
|
1916
|
-
// total sending: nOuts * 0.999e8
|
|
1917
|
-
|
|
1918
|
-
txb.build({ useAllInputs: true })
|
|
1919
|
-
|
|
1920
|
-
txb.tx.txIns.length.should.equal(nIns)
|
|
1921
|
-
txb.tx.txOuts.length.should.equal(nOuts + 1)
|
|
1922
|
-
|
|
1923
|
-
// before signing, convert to/from JSON, simulating real-world wallet use-case
|
|
1924
|
-
txb = TxBuilder.fromJSON(txb.toJSON())
|
|
1925
|
-
|
|
1926
|
-
// fully sign
|
|
1927
|
-
txb.signWithKeyPairs(keyPairs)
|
|
1928
|
-
|
|
1929
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1930
|
-
})
|
|
1931
|
-
|
|
1932
|
-
it('should sign and verify a large number of inputs and outputs, with very small amounts for inputs (1499 satoshis, 0.01 sat/byte fee), with converting to/from JSON', function () {
|
|
1933
|
-
this.timeout(10000)
|
|
1934
|
-
const nIns = 100
|
|
1935
|
-
const nOuts = 1
|
|
1936
|
-
|
|
1937
|
-
// make change address
|
|
1938
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
1939
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
1940
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
1941
|
-
|
|
1942
|
-
// make addresses to send from (and to)
|
|
1943
|
-
const privKeys = []
|
|
1944
|
-
const keyPairs = []
|
|
1945
|
-
const addrs = []
|
|
1946
|
-
for (let i = 0; i < nIns; i++) {
|
|
1947
|
-
privKeys.push(new PrivKey().fromBn(new Bn(i + 1)))
|
|
1948
|
-
keyPairs.push(new KeyPair().fromPrivKey(privKeys[i]))
|
|
1949
|
-
addrs.push(new Address().fromPubKey(keyPairs[i].pubKey))
|
|
1950
|
-
}
|
|
1951
|
-
|
|
1952
|
-
// txOuts that we are spending
|
|
1953
|
-
const scriptouts = []
|
|
1954
|
-
const txOuts = []
|
|
1955
|
-
for (let i = 0; i < nIns; i++) {
|
|
1956
|
-
scriptouts.push(new Script().fromString('OP_DUP OP_HASH160 20 0x' + addrs[i].hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG'))
|
|
1957
|
-
txOuts.push(TxOut.fromProperties(new Bn(1499), scriptouts[i]))
|
|
1958
|
-
}
|
|
1959
|
-
// total input amount: nIns * 1e8
|
|
1960
|
-
|
|
1961
|
-
let txb = new TxBuilder()
|
|
1962
|
-
txb.setFeePerKbNum(0.0000010e8)
|
|
1963
|
-
txb.setChangeAddress(changeaddr)
|
|
1964
|
-
|
|
1965
|
-
// put inputs into tx
|
|
1966
|
-
for (let i = 0; i < nIns; i++) {
|
|
1967
|
-
const txHashBuf = Buffer.alloc(32)
|
|
1968
|
-
txHashBuf.fill(i + 1)
|
|
1969
|
-
txb.inputFromPubKeyHash(txHashBuf, i, txOuts[i])
|
|
1970
|
-
}
|
|
1971
|
-
|
|
1972
|
-
// put outputs into tx
|
|
1973
|
-
for (let i = 0; i < nOuts; i++) {
|
|
1974
|
-
txb.outputToAddress(new Bn(1000), addrs[i])
|
|
1975
|
-
}
|
|
1976
|
-
// total sending: nOuts * 0.999e8
|
|
1977
|
-
|
|
1978
|
-
txb.build({ useAllInputs: true })
|
|
1979
|
-
|
|
1980
|
-
txb.tx.txIns.length.should.equal(nIns)
|
|
1981
|
-
txb.tx.txOuts.length.should.equal(nOuts + 1)
|
|
1982
|
-
|
|
1983
|
-
// before signing, convert to/from JSON, simulating real-world wallet use-case
|
|
1984
|
-
txb = TxBuilder.fromJSON(txb.toJSON())
|
|
1985
|
-
|
|
1986
|
-
// fully sign
|
|
1987
|
-
txb.signWithKeyPairs(keyPairs)
|
|
1988
|
-
|
|
1989
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
1990
|
-
})
|
|
1991
|
-
|
|
1992
|
-
it('should sign and verify a large number of inputs and outputs, with very small amounts for inputs (1499 satoshis, 0.5 sat/byte fee), with converting to/from JSON', function () {
|
|
1993
|
-
this.timeout(10000)
|
|
1994
|
-
const nIns = 100
|
|
1995
|
-
const nOuts = 1
|
|
1996
|
-
|
|
1997
|
-
// make change address
|
|
1998
|
-
const privKey = new PrivKey().fromBn(new Bn(100))
|
|
1999
|
-
const keyPair = new KeyPair().fromPrivKey(privKey)
|
|
2000
|
-
const changeaddr = new Address().fromPubKey(keyPair.pubKey)
|
|
2001
|
-
|
|
2002
|
-
// make addresses to send from (and to)
|
|
2003
|
-
const privKeys = []
|
|
2004
|
-
const keyPairs = []
|
|
2005
|
-
const addrs = []
|
|
2006
|
-
for (let i = 0; i < nIns; i++) {
|
|
2007
|
-
privKeys.push(new PrivKey().fromBn(new Bn(i + 1)))
|
|
2008
|
-
keyPairs.push(new KeyPair().fromPrivKey(privKeys[i]))
|
|
2009
|
-
addrs.push(new Address().fromPubKey(keyPairs[i].pubKey))
|
|
2010
|
-
}
|
|
2011
|
-
|
|
2012
|
-
// txOuts that we are spending
|
|
2013
|
-
const scriptouts = []
|
|
2014
|
-
const txOuts = []
|
|
2015
|
-
for (let i = 0; i < nIns; i++) {
|
|
2016
|
-
scriptouts.push(new Script().fromString('OP_DUP OP_HASH160 20 0x' + addrs[i].hashBuf.toString('hex') + ' OP_EQUALVERIFY OP_CHECKSIG'))
|
|
2017
|
-
txOuts.push(TxOut.fromProperties(new Bn(1499), scriptouts[i]))
|
|
2018
|
-
}
|
|
2019
|
-
// total input amount: nIns * 1e8
|
|
2020
|
-
|
|
2021
|
-
let txb = new TxBuilder()
|
|
2022
|
-
txb.setFeePerKbNum(0.0000500e8)
|
|
2023
|
-
txb.setChangeAddress(changeaddr)
|
|
2024
|
-
|
|
2025
|
-
// put inputs into tx
|
|
2026
|
-
for (let i = 0; i < nIns; i++) {
|
|
2027
|
-
const txHashBuf = Buffer.alloc(32)
|
|
2028
|
-
txHashBuf.fill(i + 1)
|
|
2029
|
-
txb.inputFromPubKeyHash(txHashBuf, i, txOuts[i])
|
|
2030
|
-
}
|
|
2031
|
-
|
|
2032
|
-
// put outputs into tx
|
|
2033
|
-
for (let i = 0; i < nOuts; i++) {
|
|
2034
|
-
txb.outputToAddress(new Bn(1000), addrs[i])
|
|
2035
|
-
}
|
|
2036
|
-
// total sending: nOuts * 0.999e8
|
|
2037
|
-
|
|
2038
|
-
txb.build({ useAllInputs: true })
|
|
2039
|
-
|
|
2040
|
-
txb.tx.txIns.length.should.equal(nIns)
|
|
2041
|
-
txb.tx.txOuts.length.should.equal(nOuts + 1)
|
|
2042
|
-
|
|
2043
|
-
// before signing, convert to/from JSON, simulating real-world wallet use-case
|
|
2044
|
-
txb = TxBuilder.fromJSON(txb.toJSON())
|
|
2045
|
-
|
|
2046
|
-
// fully sign
|
|
2047
|
-
txb.signWithKeyPairs(keyPairs)
|
|
2048
|
-
|
|
2049
|
-
TxVerifier.verify(txb.tx, txb.uTxOutMap).should.equal(true)
|
|
2050
|
-
})
|
|
2051
|
-
|
|
2052
|
-
it('should not mess up with bip 69 sorting with this known txbuilder object', function () {
|
|
2053
|
-
this.timeout(10000)
|
|
2054
|
-
const txbJSON = '{"tx":"0100000005b27d77a351245f3ed2c38e40114d432ffecee69d8c64dc06fe427a1ac09f1b40000000006b483045022100f7831904c76f49e23bdd5d2813cd708074ec77d345a0311ee7150fcfcd494cf3022058cbce7ea22b8d759c35d37a375ce0c73b9e1e87d047cb7748ade1fd082370d1412102bf0925d874daedbc1e0abdffa399b2b2042fddbba1cca850cdfdfc3ec9f218ffffffffff98f1664e4ad769225b39033b5a7269afdff54cc676908724d74d4efdebc2ee59000000006b483045022100f2e3195d6c104aacbf04589916d3e529414abbc8379b7d92fa6d32c32c297f05022024775901b5d71cb639297cf9b757d97738b16715023320ef9a51311bb1a97e94412102cd925e193fe4e0848e1f99e0975904a73eea7f779ff0e870115ad16b784803ceffffffff011ac299dce0415097d1fd67287e83cb92fbd2b9b7efe2c91751eb454e05c394000000006a4730440220739794fc726b205a34569c590d32e13459f5127fcf71d2169fc5944a727de8be0220505d2cbedc63514a6147fe756d45f7faf9bd5852df446fa35b62b554fb2d32514121034828cbbf04f9ae5608fdcf47598e191846b4970c1b6eda66519661ca75035a9dffffffffe85c87e34ed16ab8988fc398c16b9738f6032e9a4377b6636e2ec6a0ee752fcb000000006a473044022030e01c5bda4da0d4aadec98c5ece7b3529509b0aef6f437a14417abaf833956002207d8ece580800807132bca535ce06398ac5c68c68acd20566ecd3db0ecd58d7114121033fb03af6776d59cdb6ae78dff502d36b5a51429f243116f827e3d7438343b213ffffffff19cc6850ec0e9b8a01ef6ea0ef9888fbdc0e6794b9a6b7c7c36621d80665a0ec000000006b4830450221009f25a7e50bc857eb75f6dd06b55ec495a3f11c73f1f0151190bbb417f051978b02207169bb7aaa05d58dcc50f091d9b0a584157420cc2e4c3d841cafddf891356a95412103582a67dd32baefe4063e0408809bc740df4233c44dc2e5e3737de5c2249f0863ffffffff028d160000000000001976a914821972770205b3466023acd90790f95aee37914488accc580300000000001976a9145ff56ab8054c9e038254ee92f9ba364f84c2736188ac00000000","txIns":["98f1664e4ad769225b39033b5a7269afdff54cc676908724d74d4efdebc2ee5900000000020000ffffffff","011ac299dce0415097d1fd67287e83cb92fbd2b9b7efe2c91751eb454e05c39400000000020000ffffffff","b27d77a351245f3ed2c38e40114d432ffecee69d8c64dc06fe427a1ac09f1b4000000000020000ffffffff","e85c87e34ed16ab8988fc398c16b9738f6032e9a4377b6636e2ec6a0ee752fcb00000000020000ffffffff","19cc6850ec0e9b8a01ef6ea0ef9888fbdc0e6794b9a6b7c7c36621d80665a0ec00000000020000ffffffff"],"txOuts":["cc580300000000001976a9145ff56ab8054c9e038254ee92f9ba364f84c2736188ac"],"uTxOutMap":{"98f1664e4ad769225b39033b5a7269afdff54cc676908724d74d4efdebc2ee59:0":"f9140000000000001976a9145629fb19a3c46420707f61785c1b63cc69b7da3288ac","011ac299dce0415097d1fd67287e83cb92fbd2b9b7efe2c91751eb454e05c394:0":"1ad70000000000001976a91462698da42f87021fc8cdb4c945d09d93e82480a088ac","b27d77a351245f3ed2c38e40114d432ffecee69d8c64dc06fe427a1ac09f1b40:0":"acd60000000000001976a9144c6daa9f0afa439dc98075c531e1fb614139818e88ac","e85c87e34ed16ab8988fc398c16b9738f6032e9a4377b6636e2ec6a0ee752fcb:0":"1ad70000000000001976a914841067d1f0abdd78ef8feb3aa42b52c0d7ee83a188ac","19cc6850ec0e9b8a01ef6ea0ef9888fbdc0e6794b9a6b7c7c36621d80665a0ec:0":"1ad70000000000001976a914d3647137576bc3561444a2c84018c942796ab5bb88ac"},"sigOperations":{"98f1664e4ad769225b39033b5a7269afdff54cc676908724d74d4efdebc2ee59:0":[{"nScriptChunk":0,"type":"sig","addressStr":"18rbQNxxgYdpMnsvNjvbXJP1TBZ9zQM3WJ","nHashType":65,"log":"successfully inserted signature"},{"nScriptChunk":1,"type":"pubKey","addressStr":"18rbQNxxgYdpMnsvNjvbXJP1TBZ9zQM3WJ","nHashType":65,"log":"successfully inserted public key"}],"011ac299dce0415097d1fd67287e83cb92fbd2b9b7efe2c91751eb454e05c394:0":[{"nScriptChunk":0,"type":"sig","addressStr":"19yMfmgJAEsGHUS4J6ZXqtv3Z4hG3NT4pA","nHashType":65,"log":"successfully inserted signature"},{"nScriptChunk":1,"type":"pubKey","addressStr":"19yMfmgJAEsGHUS4J6ZXqtv3Z4hG3NT4pA","nHashType":65,"log":"successfully inserted public key"}],"b27d77a351245f3ed2c38e40114d432ffecee69d8c64dc06fe427a1ac09f1b40:0":[{"nScriptChunk":0,"type":"sig","addressStr":"17y7jLk1f1oQcV78pRsnv7dUNBLnFUzakZ","nHashType":65,"log":"successfully inserted signature"},{"nScriptChunk":1,"type":"pubKey","addressStr":"17y7jLk1f1oQcV78pRsnv7dUNBLnFUzakZ","nHashType":65,"log":"successfully inserted public key"}],"e85c87e34ed16ab8988fc398c16b9738f6032e9a4377b6636e2ec6a0ee752fcb:0":[{"nScriptChunk":0,"type":"sig","addressStr":"1D3HrCYVc1fybgtY4e8Yhg1k1KF7X9C725","nHashType":65,"log":"successfully inserted signature"},{"nScriptChunk":1,"type":"pubKey","addressStr":"1D3HrCYVc1fybgtY4e8Yhg1k1KF7X9C725","nHashType":65,"log":"successfully inserted public key"}],"19cc6850ec0e9b8a01ef6ea0ef9888fbdc0e6794b9a6b7c7c36621d80665a0ec:0":[{"nScriptChunk":0,"type":"sig","addressStr":"1LGjuKv9eqPUzg2YsMdfzJWckuAwfcnmZo","nHashType":65,"log":"successfully inserted signature"},{"nScriptChunk":1,"type":"pubKey","addressStr":"1LGjuKv9eqPUzg2YsMdfzJWckuAwfcnmZo","nHashType":65,"log":"successfully inserted public key"}]},"changeScript":"76a914821972770205b3466023acd90790f95aee37914488ac","changeAmountBn":5773,"feeAmountBn":410,"feePerKbNum":500,"sigsPerInput":1,"dust":546,"dustChangeToFees":true,"hashCache":{"prevoutsHashBuf":"1d67bdef4b743591ada4922b5af092d7cce85efcf8f630e94afcde068f6fa2d0","sequenceHashBuf":"99399659a6e129b6497faa061be7d8f6558a8594b5dd80474859a12d6ccf0b20","outputsHashBuf":"bcda35b250afaa69c8ffe37021ebece36afbe749c1935f69de3eab93e450fd3d"}}'
|
|
2055
|
-
const txb = TxBuilder.fromJSON(JSON.parse(txbJSON))
|
|
2056
|
-
|
|
2057
|
-
const keyPairs = []
|
|
2058
|
-
keyPairs.push(KeyPair.fromJSON(JSON.parse('{"privKey":"8032334c28c99a38fce22a6d4224e766d894e727a0a9cccfd2038fe4d4ca23ec9901","pubKey":"0104cd925e193fe4e0848e1f99e0975904a73eea7f779ff0e870115ad16b784803ce70874550b3e86a8d670422d4e138c9f7700e4903f1d1e4af35eac9fba6377ef4"}')))
|
|
2059
|
-
keyPairs.push(KeyPair.fromJSON(JSON.parse('{"privKey":"803f50e990b3518c654c022f6bc8ee9464db857aee821133bbe8090c035f81851201","pubKey":"01044828cbbf04f9ae5608fdcf47598e191846b4970c1b6eda66519661ca75035a9dd6228621b4462f6a6791897b6d60b2ec1492509ba871a707b34bf94eade868e5"}')))
|
|
2060
|
-
keyPairs.push(KeyPair.fromJSON(JSON.parse('{"privKey":"80f7744e00816dc306b7c452fef77883317efbd59863f7adcac0a0d95b489b196701","pubKey":"0104bf0925d874daedbc1e0abdffa399b2b2042fddbba1cca850cdfdfc3ec9f218ff0bdb932b2ced35cefc0988116b426cbb68f563c401329e77e9eaae29256aad5c"}')))
|
|
2061
|
-
keyPairs.push(KeyPair.fromJSON(JSON.parse('{"privKey":"805d3c50621207fc5783c82bf7e7f090161154818467258c49368dfbf95a6cb5a301","pubKey":"01043fb03af6776d59cdb6ae78dff502d36b5a51429f243116f827e3d7438343b213ab07f6bc08e1a0afba025e0cd34395afedce01bf8e9b6b7c6d385eaaee57403d"}')))
|
|
2062
|
-
keyPairs.push(KeyPair.fromJSON(JSON.parse('{"privKey":"80aaf1703f957b80a023088c5a04c9fb4d4950705dcd8b7aaf6cd9eea66e807f8801","pubKey":"0104582a67dd32baefe4063e0408809bc740df4233c44dc2e5e3737de5c2249f08630bb6640376b7ed60ed12ec161a458c8bbb3417a7e3474a3e9436cc32d9296627"}')))
|
|
2063
|
-
|
|
2064
|
-
let txVerifier = new TxVerifier(txb.tx, txb.uTxOutMap)
|
|
2065
|
-
|
|
2066
|
-
// txb.uTxOutMap.map.forEach((txOut, label) => {
|
|
2067
|
-
// console.log(label, Address.fromTxOutScript(txOut.script).toString(), txOut.valueBn.toNumber())
|
|
2068
|
-
// })
|
|
2069
|
-
|
|
2070
|
-
// txb.tx.txIns.forEach((txIn, nIn) => {
|
|
2071
|
-
// const verified = txVerifier.verifyNIn(nIn, Interp.SCRIPT_ENABLE_SIGHASH_FORKID)
|
|
2072
|
-
// console.log(txIn.txHashBuf.toString('hex'), txIn.txOutNum, Address.fromTxInScript(txIn.script).toString(), verified)
|
|
2073
|
-
// })
|
|
2074
|
-
|
|
2075
|
-
txVerifier.verify().should.equal(false) // this was computed with signatures in the wrong place; it should be invalid
|
|
2076
|
-
|
|
2077
|
-
const txb2 = new TxBuilder()
|
|
2078
|
-
txb2.sendDustChangeToFees(true)
|
|
2079
|
-
txb2.setChangeAddress(Address.fromTxOutScript(txb.changeScript))
|
|
2080
|
-
txb2.inputFromPubKeyHash(txb.txIns[0].txHashBuf, txb.txIns[0].txOutNum, txb.uTxOutMap.get(txb.txIns[0].txHashBuf, txb.txIns[0].txOutNum))
|
|
2081
|
-
txb2.inputFromPubKeyHash(txb.txIns[1].txHashBuf, txb.txIns[1].txOutNum, txb.uTxOutMap.get(txb.txIns[1].txHashBuf, txb.txIns[1].txOutNum))
|
|
2082
|
-
txb2.inputFromPubKeyHash(txb.txIns[2].txHashBuf, txb.txIns[2].txOutNum, txb.uTxOutMap.get(txb.txIns[2].txHashBuf, txb.txIns[2].txOutNum))
|
|
2083
|
-
txb2.inputFromPubKeyHash(txb.txIns[3].txHashBuf, txb.txIns[3].txOutNum, txb.uTxOutMap.get(txb.txIns[3].txHashBuf, txb.txIns[3].txOutNum))
|
|
2084
|
-
txb2.inputFromPubKeyHash(txb.txIns[4].txHashBuf, txb.txIns[4].txOutNum, txb.uTxOutMap.get(txb.txIns[4].txHashBuf, txb.txIns[4].txOutNum))
|
|
2085
|
-
|
|
2086
|
-
txb2.build({ useAllInputs: true })
|
|
2087
|
-
txb2.sort() // NOT sorting should lead to valid tx
|
|
2088
|
-
txb2.signWithKeyPairs(keyPairs)
|
|
2089
|
-
|
|
2090
|
-
let txVerifier2 = new TxVerifier(txb2.tx, txb2.uTxOutMap)
|
|
2091
|
-
|
|
2092
|
-
// txb2.uTxOutMap.map.forEach((txOut, label) => {
|
|
2093
|
-
// console.log(label, Address.fromTxOutScript(txOut.script).toString(), txOut.valueBn.toNumber())
|
|
2094
|
-
// })
|
|
2095
|
-
|
|
2096
|
-
// txb2.tx.txIns.forEach((txIn, nIn) => {
|
|
2097
|
-
// const verified = txVerifier2.verifyNIn(nIn, Interp.SCRIPT_ENABLE_SIGHASH_FORKID)
|
|
2098
|
-
// console.log(txIn.txHashBuf.toString('hex'), txIn.txOutNum, Address.fromTxInScript(txIn.script).toString(), verified)
|
|
2099
|
-
// })
|
|
2100
|
-
|
|
2101
|
-
txVerifier2.verify().should.equal(true)
|
|
2102
|
-
})
|
|
2103
|
-
})
|
|
2104
|
-
})
|