@smartledger/bsv 5.5.0 → 5.5.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.
Files changed (87) hide show
  1. package/.mocharc.json +5 -0
  2. package/CHANGELOG.md +20 -0
  3. package/bsv-covenant.min.js +2 -2
  4. package/bsv-gdaf.min.js +2 -2
  5. package/bsv-ltp.min.js +2 -2
  6. package/bsv-smartcontract.min.js +2 -2
  7. package/bsv.bundle.js +2 -2
  8. package/bsv.min.js +2 -2
  9. package/lib/smart_contract/covenant_helpers.js +3 -3
  10. package/lib/smart_contract/index.js +4 -5
  11. package/package.json +3 -1
  12. package/test/address.js +629 -0
  13. package/test/block/block.js +239 -0
  14. package/test/block/blockheader.js +270 -0
  15. package/test/block/merkleblock.js +207 -0
  16. package/test/cli/smoke.js +261 -0
  17. package/test/credentials-test.js +329 -0
  18. package/test/crypto/bn.js +168 -0
  19. package/test/crypto/ecdsa.js +328 -0
  20. package/test/crypto/elliptic-fixed.js +61 -0
  21. package/test/crypto/hash.browser.js +121 -0
  22. package/test/crypto/hash.js +122 -0
  23. package/test/crypto/point.js +193 -0
  24. package/test/crypto/random.js +32 -0
  25. package/test/crypto/security.js +127 -0
  26. package/test/crypto/shamir.js +169 -0
  27. package/test/crypto/signature.js +318 -0
  28. package/test/data/bip69.json +215 -0
  29. package/test/data/bitcoind/base58_keys_invalid.json +152 -0
  30. package/test/data/bitcoind/base58_keys_valid.json +452 -0
  31. package/test/data/bitcoind/blocks.json +27 -0
  32. package/test/data/bitcoind/script_tests.json +2244 -0
  33. package/test/data/bitcoind/sig_canonical.json +7 -0
  34. package/test/data/bitcoind/sig_noncanonical.json +22 -0
  35. package/test/data/bitcoind/tx_invalid.json +177 -0
  36. package/test/data/bitcoind/tx_valid.json +224 -0
  37. package/test/data/blk86756-testnet.dat +0 -0
  38. package/test/data/blk86756-testnet.js +12 -0
  39. package/test/data/blk86756-testnet.json +684 -0
  40. package/test/data/ecdsa.json +230 -0
  41. package/test/data/merkleblocks.js +486 -0
  42. package/test/data/messages.json +22 -0
  43. package/test/data/sighash.json +1004 -0
  44. package/test/data/tx_creation.json +85 -0
  45. package/test/ecies/bitcore-ecies.js +170 -0
  46. package/test/ecies/electrum-ecies.js +206 -0
  47. package/test/encoding/base58.js +131 -0
  48. package/test/encoding/base58check.js +136 -0
  49. package/test/encoding/bufferreader.js +328 -0
  50. package/test/encoding/bufferwriter.js +160 -0
  51. package/test/encoding/varint.js +104 -0
  52. package/test/gdaf/canonicalize.js +140 -0
  53. package/test/hdkeys.js +365 -0
  54. package/test/hdprivatekey.js +330 -0
  55. package/test/hdpublickey.js +293 -0
  56. package/test/index.js +16 -0
  57. package/test/ltp/right.js +198 -0
  58. package/test/message/message.js +190 -0
  59. package/test/mnemonic/data/fixtures.json +300 -0
  60. package/test/mnemonic/mnemonic.js +245 -0
  61. package/test/mnemonic/mocha.opts +1 -0
  62. package/test/mnemonic/pbkdf2.test.js +43 -0
  63. package/test/networks.js +162 -0
  64. package/test/opcode.js +161 -0
  65. package/test/privatekey.js +430 -0
  66. package/test/publickey.js +411 -0
  67. package/test/script/genesis_limits.js +80 -0
  68. package/test/script/interpreter.js +723 -0
  69. package/test/script/script.js +1256 -0
  70. package/test/script/string_ops.js +72 -0
  71. package/test/smart_contract/covenants.js +247 -0
  72. package/test/smart_contract/dsl_debugger.js +103 -0
  73. package/test/smart_contract/token_generalized.js +187 -0
  74. package/test/transaction/deserialize.js +33 -0
  75. package/test/transaction/input/input.js +92 -0
  76. package/test/transaction/input/multisig.js +174 -0
  77. package/test/transaction/input/multisigscripthash.js +111 -0
  78. package/test/transaction/input/publickey.js +68 -0
  79. package/test/transaction/input/publickeyhash.js +59 -0
  80. package/test/transaction/output.js +185 -0
  81. package/test/transaction/sighash.js +71 -0
  82. package/test/transaction/signature.js +127 -0
  83. package/test/transaction/transaction.js +1299 -0
  84. package/test/transaction/unspentoutput.js +92 -0
  85. package/test/util/js.js +76 -0
  86. package/test/util/preconditions.js +79 -0
  87. package/test/vcjwt/interop.js +126 -0
@@ -0,0 +1,92 @@
1
+ 'use strict'
2
+
3
+ var should = require('chai').should()
4
+ var expect = require('chai').expect
5
+ var _ = require('../../../lib/util/_')
6
+
7
+ var bsv = require('../../..')
8
+ var errors = bsv.errors
9
+ var PrivateKey = bsv.PrivateKey
10
+ var Address = bsv.Address
11
+ var Script = bsv.Script
12
+ var Networks = bsv.Networks
13
+ var Input = bsv.Transaction.Input
14
+
15
+ describe('Transaction.Input', function () {
16
+ var privateKey = new PrivateKey('KwF9LjRraetZuEjR8VqEq539z137LW5anYDUnVK11vM3mNMHTWb4')
17
+ var publicKey = privateKey.publicKey
18
+ var address = new Address(publicKey, Networks.livenet)
19
+ var output = {
20
+ address: '33zbk2aSZYdNbRsMPPt6jgy6Kq1kQreqeb',
21
+ prevTxId: '66e64ef8a3b384164b78453fa8c8194de9a473ba14f89485a0e433699daec140',
22
+ outputIndex: 0,
23
+ script: new Script(address),
24
+ satoshis: 1000000
25
+ }
26
+ var coinbase = {
27
+ prevTxId: '0000000000000000000000000000000000000000000000000000000000000000',
28
+ outputIndex: 0xFFFFFFFF,
29
+ script: new Script(),
30
+ satoshis: 1000000
31
+ }
32
+
33
+ var coinbaseJSON = JSON.stringify({
34
+ prevTxId: '0000000000000000000000000000000000000000000000000000000000000000',
35
+ outputIndex: 4294967295,
36
+ script: ''
37
+ })
38
+
39
+ it('has abstract methods: "getSignatures", "isFullySigned", "addSignature", "clearSignatures"', function () {
40
+ var input = new Input(output)
41
+ _.each(['getSignatures', 'isFullySigned', 'addSignature', 'clearSignatures'], function (method) {
42
+ expect(function () {
43
+ return input[method]()
44
+ }).to.throw(errors.AbstractMethodInvoked)
45
+ })
46
+ })
47
+ it('detects coinbase transactions', function () {
48
+ new Input(output).isNull().should.equal(false)
49
+ var ci = new Input(coinbase)
50
+ ci.isNull().should.equal(true)
51
+ })
52
+
53
+ describe('instantiation', function () {
54
+ it('works without new', function () {
55
+ var input = Input()
56
+ should.exist(input)
57
+ })
58
+ it('fails with no script info', function () {
59
+ expect(function () {
60
+ var input = new Input({})
61
+ input.toString()
62
+ }).to.throw('Need a script to create an input')
63
+ })
64
+ it('fromObject should work', function () {
65
+ var jsonData = JSON.parse(coinbaseJSON)
66
+ var input = Input.fromObject(jsonData)
67
+ should.exist(input)
68
+ input.prevTxId.toString('hex').should.equal(jsonData.prevTxId)
69
+ input.outputIndex.should.equal(jsonData.outputIndex)
70
+ })
71
+ it('fromObject should work', function () {
72
+ var input = Input.fromObject(JSON.parse(coinbaseJSON))
73
+ var obj = input.toObject()
74
+ Input.fromObject(obj).should.deep.equal(input)
75
+ obj.script = 42
76
+ Input.fromObject.bind(null, obj).should.throw('Invalid argument type: script')
77
+ })
78
+ })
79
+
80
+ it('_estimateSize returns correct size', function () {
81
+ var input = new Input(output)
82
+ input._estimateSize().should.equal(66)
83
+ })
84
+
85
+ it('defaults isFinal correctly', function () {
86
+ new Input(output).isFinal().should.equal(true)
87
+ })
88
+
89
+ it('handles isFinal when sequence number is set to non default value', function () {
90
+ new Input({ ...output, sequenceNumber: 0 }).isFinal().should.equal(false)
91
+ })
92
+ })
@@ -0,0 +1,174 @@
1
+ 'use strict'
2
+
3
+ var should = require('chai').should()
4
+ var _ = require('../../../lib/util/_')
5
+
6
+ var bsv = require('../../..')
7
+ var Transaction = bsv.Transaction
8
+ var PrivateKey = bsv.PrivateKey
9
+ var Address = bsv.Address
10
+ var Script = bsv.Script
11
+ var Signature = bsv.crypto.Signature
12
+ var MultiSigInput = bsv.Transaction.Input.MultiSig
13
+
14
+ describe('MultiSigInput', function () {
15
+ var privateKey1 = new PrivateKey('KwF9LjRraetZuEjR8VqEq539z137LW5anYDUnVK11vM3mNMHTWb4')
16
+ var privateKey2 = new PrivateKey('L4PqnaPTCkYhAqH3YQmefjxQP6zRcF4EJbdGqR8v6adtG9XSsadY')
17
+ var privateKey3 = new PrivateKey('L4CTX79zFeksZTyyoFuPQAySfmP7fL3R41gWKTuepuN7hxuNuJwV')
18
+ var public1 = privateKey1.publicKey
19
+ var public2 = privateKey2.publicKey
20
+ var public3 = privateKey3.publicKey
21
+ var address = new Address('33zbk2aSZYdNbRsMPPt6jgy6Kq1kQreqeb')
22
+
23
+ var output = {
24
+ txId: '66e64ef8a3b384164b78453fa8c8194de9a473ba14f89485a0e433699daec140',
25
+ outputIndex: 0,
26
+ script: new Script('5221025c95ec627038e85b5688a9b3d84d28c5ebe66e8c8d697d498e20fe96e3b1ab1d2102cdddfc974d41a62f1f80081deee70592feb7d6e6cf6739d6592edbe7946720e72103c95924e02c240b5545089c69c6432447412b58be43fd671918bd184a5009834353ae'),
27
+ satoshis: 1000000
28
+ }
29
+ it('can count missing signatures', function () {
30
+ var transaction = new Transaction()
31
+ .from(output, [public1, public2, public3], 2)
32
+ .to(address, 1000000)
33
+ var input = transaction.inputs[0]
34
+
35
+ input.countSignatures().should.equal(0)
36
+
37
+ transaction.sign(privateKey1)
38
+ input.countSignatures().should.equal(1)
39
+ input.countMissingSignatures().should.equal(1)
40
+ input.isFullySigned().should.equal(false)
41
+
42
+ transaction.sign(privateKey2)
43
+ input.countSignatures().should.equal(2)
44
+ input.countMissingSignatures().should.equal(0)
45
+ input.isFullySigned().should.equal(true)
46
+ })
47
+ it('can count missing signatures, signed with key 3 and 1', function () {
48
+ var transaction = new Transaction()
49
+ .from(output, [public1, public2, public3], 2)
50
+ .to(address, 1000000)
51
+ var input = transaction.inputs[0]
52
+
53
+ input.countSignatures().should.equal(0)
54
+
55
+ transaction.sign(privateKey3)
56
+ input.countSignatures().should.equal(1)
57
+ input.countMissingSignatures().should.equal(1)
58
+ input.isFullySigned().should.equal(false)
59
+
60
+ transaction.sign(privateKey1)
61
+ input.countSignatures().should.equal(2)
62
+ input.countMissingSignatures().should.equal(0)
63
+ input.isFullySigned().should.equal(true)
64
+ })
65
+ it('returns a list of public keys with missing signatures', function () {
66
+ var transaction = new Transaction()
67
+ .from(output, [public1, public2, public3], 2)
68
+ .to(address, 1000000)
69
+ var input = transaction.inputs[0]
70
+
71
+ _.every(input.publicKeysWithoutSignature(), function (publicKeyMissing) {
72
+ var serialized = publicKeyMissing.toString()
73
+ return serialized === public1.toString() ||
74
+ serialized === public2.toString() ||
75
+ serialized === public3.toString()
76
+ }).should.equal(true)
77
+ transaction.sign(privateKey1)
78
+ _.every(input.publicKeysWithoutSignature(), function (publicKeyMissing) {
79
+ var serialized = publicKeyMissing.toString()
80
+ return serialized === public2.toString() ||
81
+ serialized === public3.toString()
82
+ }).should.equal(true)
83
+ })
84
+ it('can clear all signatures', function () {
85
+ var transaction = new Transaction()
86
+ .from(output, [public1, public2, public3], 2)
87
+ .to(address, 1000000)
88
+ .sign(privateKey1)
89
+ .sign(privateKey2)
90
+
91
+ var input = transaction.inputs[0]
92
+ input.isFullySigned().should.equal(true)
93
+ input.clearSignatures()
94
+ input.isFullySigned().should.equal(false)
95
+ })
96
+ it('can estimate how heavy is the output going to be', function () {
97
+ var transaction = new Transaction()
98
+ .from(output, [public1, public2, public3], 2)
99
+ .to(address, 1000000)
100
+ var input = transaction.inputs[0]
101
+ input._estimateSize().should.equal(188)
102
+ })
103
+ it('uses SIGHASH_ALL|FORKID by default', function () {
104
+ var transaction = new Transaction()
105
+ .from(output, [public1, public2, public3], 2)
106
+ .to(address, 1000000)
107
+ var input = transaction.inputs[0]
108
+ var sigs = input.getSignatures(transaction, privateKey1, 0)
109
+ sigs[0].sigtype.should.equal(Signature.SIGHASH_ALL | Signature.SIGHASH_FORKID)
110
+ })
111
+ it('roundtrips to/from object', function () {
112
+ var transaction = new Transaction()
113
+ .from(output, [public1, public2, public3], 2)
114
+ .to(address, 1000000)
115
+ .sign(privateKey1)
116
+ var input = transaction.inputs[0]
117
+ var roundtrip = new MultiSigInput(input.toObject())
118
+ roundtrip.toObject().should.deep.equal(input.toObject())
119
+ })
120
+ it('roundtrips to/from object when not signed', function () {
121
+ var transaction = new Transaction()
122
+ .from(output, [public1, public2, public3], 2)
123
+ .to(address, 1000000)
124
+ var input = transaction.inputs[0]
125
+ var roundtrip = new MultiSigInput(input.toObject())
126
+ roundtrip.toObject().should.deep.equal(input.toObject())
127
+ })
128
+ it('can parse list of signature buffers, from TX signed with key 1 and 2', function () {
129
+ var transaction = new Transaction('010000000140c1ae9d6933e4a08594f814ba73a4e94d19c8a83f45784b1684b3a3f84ee666000000009200473044022012bd2f15e56ab1b63d5ee23e194ed995ad4b81a21bcb8e0d913e5e791c07f7280220278bdb6b54cdc608193c869affe28dc2f700902218122770faff25c56142102b01483045022100e74e9955e042aca36f4f3ad907a0926c5b85e5d9608b0678a78a9cbc0259c7a2022053ff761e5f9a80558db7023e45c4979ac3c19a423f0184fb0596d3da308cc4b501ffffffff0140420f000000000017a91419438da7d16709643be5abd8df62ca4034a489a78700000000')
130
+
131
+ var inputObj = transaction.inputs[0].toObject()
132
+ inputObj.output = output
133
+ transaction.inputs[0] = new Transaction.Input(inputObj)
134
+
135
+ inputObj.signatures = MultiSigInput.normalizeSignatures(
136
+ transaction,
137
+ transaction.inputs[0],
138
+ 0,
139
+ transaction.inputs[0].script.chunks.slice(1).map(function (s) { return s.buf }),
140
+ [public1, public2, public3]
141
+ )
142
+
143
+ transaction.inputs[0] = new MultiSigInput(inputObj, [public1, public2, public3], 2)
144
+
145
+ transaction.inputs[0].signatures[0].publicKey.should.deep.equal(public1)
146
+ transaction.inputs[0].signatures[1].publicKey.should.deep.equal(public2)
147
+ should.equal(transaction.inputs[0].signatures[2], undefined)
148
+ transaction.inputs[0].isValidSignature(transaction, transaction.inputs[0].signatures[0]).should.equal(true)
149
+ transaction.inputs[0].isValidSignature(transaction, transaction.inputs[0].signatures[1]).should.equal(true)
150
+ })
151
+ it('can parse list of signature buffers, from TX signed with key 3 and 1', function () {
152
+ var transaction = new Transaction('010000000140c1ae9d6933e4a08594f814ba73a4e94d19c8a83f45784b1684b3a3f84ee666000000009300483045022100fc39ce4f51b2766ec8e978296e0594ea4578a3eb2543722fd4053e92bf16e6b1022030f739868397a881b019508b9c725a5c69a3652cb8928027748e93e67dfaef5501483045022100e74e9955e042aca36f4f3ad907a0926c5b85e5d9608b0678a78a9cbc0259c7a2022053ff761e5f9a80558db7023e45c4979ac3c19a423f0184fb0596d3da308cc4b501ffffffff0140420f000000000017a91419438da7d16709643be5abd8df62ca4034a489a78700000000')
153
+
154
+ var inputObj = transaction.inputs[0].toObject()
155
+ inputObj.output = output
156
+ transaction.inputs[0] = new Transaction.Input(inputObj)
157
+
158
+ inputObj.signatures = MultiSigInput.normalizeSignatures(
159
+ transaction,
160
+ transaction.inputs[0],
161
+ 0,
162
+ transaction.inputs[0].script.chunks.slice(1).map(function (s) { return s.buf }),
163
+ [public1, public2, public3]
164
+ )
165
+
166
+ transaction.inputs[0] = new MultiSigInput(inputObj, [public1, public2, public3], 2)
167
+
168
+ transaction.inputs[0].signatures[0].publicKey.should.deep.equal(public1)
169
+ should.equal(transaction.inputs[0].signatures[1], undefined)
170
+ transaction.inputs[0].signatures[2].publicKey.should.deep.equal(public3)
171
+ transaction.inputs[0].isValidSignature(transaction, transaction.inputs[0].signatures[0]).should.equal(true)
172
+ transaction.inputs[0].isValidSignature(transaction, transaction.inputs[0].signatures[2]).should.equal(true)
173
+ })
174
+ })
@@ -0,0 +1,111 @@
1
+ 'use strict'
2
+
3
+ require('chai').should()
4
+ var _ = require('../../../lib/util/_')
5
+
6
+ var bsv = require('../../..')
7
+ var Transaction = bsv.Transaction
8
+ var PrivateKey = bsv.PrivateKey
9
+ var Address = bsv.Address
10
+ var Script = bsv.Script
11
+ var Signature = bsv.crypto.Signature
12
+ var MultiSigScriptHashInput = bsv.Transaction.Input.MultiSigScriptHash
13
+
14
+ describe('MultiSigScriptHashInput', function () {
15
+ var privateKey1 = new PrivateKey('KwF9LjRraetZuEjR8VqEq539z137LW5anYDUnVK11vM3mNMHTWb4')
16
+ var privateKey2 = new PrivateKey('L4PqnaPTCkYhAqH3YQmefjxQP6zRcF4EJbdGqR8v6adtG9XSsadY')
17
+ var privateKey3 = new PrivateKey('L4CTX79zFeksZTyyoFuPQAySfmP7fL3R41gWKTuepuN7hxuNuJwV')
18
+ var public1 = privateKey1.publicKey
19
+ var public2 = privateKey2.publicKey
20
+ var public3 = privateKey3.publicKey
21
+ var address = new Address('33zbk2aSZYdNbRsMPPt6jgy6Kq1kQreqeb')
22
+
23
+ var output = {
24
+ address: '33zbk2aSZYdNbRsMPPt6jgy6Kq1kQreqeb',
25
+ txId: '66e64ef8a3b384164b78453fa8c8194de9a473ba14f89485a0e433699daec140',
26
+ outputIndex: 0,
27
+ script: new Script(address),
28
+ satoshis: 1000000
29
+ }
30
+ it('can count missing signatures', function () {
31
+ var transaction = new Transaction()
32
+ .from(output, [public1, public2, public3], 2)
33
+ .to(address, 1000000)
34
+ var input = transaction.inputs[0]
35
+
36
+ input.countSignatures().should.equal(0)
37
+
38
+ transaction.sign(privateKey1)
39
+ input.countSignatures().should.equal(1)
40
+ input.countMissingSignatures().should.equal(1)
41
+ input.isFullySigned().should.equal(false)
42
+
43
+ transaction.sign(privateKey2)
44
+ input.countSignatures().should.equal(2)
45
+ input.countMissingSignatures().should.equal(0)
46
+ input.isFullySigned().should.equal(true)
47
+ })
48
+ it('returns a list of public keys with missing signatures', function () {
49
+ var transaction = new Transaction()
50
+ .from(output, [public1, public2, public3], 2)
51
+ .to(address, 1000000)
52
+ var input = transaction.inputs[0]
53
+
54
+ _.every(input.publicKeysWithoutSignature(), function (publicKeyMissing) {
55
+ var serialized = publicKeyMissing.toString()
56
+ return serialized === public1.toString() ||
57
+ serialized === public2.toString() ||
58
+ serialized === public3.toString()
59
+ }).should.equal(true)
60
+ transaction.sign(privateKey1)
61
+ _.every(input.publicKeysWithoutSignature(), function (publicKeyMissing) {
62
+ var serialized = publicKeyMissing.toString()
63
+ return serialized === public2.toString() ||
64
+ serialized === public3.toString()
65
+ }).should.equal(true)
66
+ })
67
+ it('can clear all signatures', function () {
68
+ var transaction = new Transaction()
69
+ .from(output, [public1, public2, public3], 2)
70
+ .to(address, 1000000)
71
+ .sign(privateKey1)
72
+ .sign(privateKey2)
73
+
74
+ var input = transaction.inputs[0]
75
+ input.isFullySigned().should.equal(true)
76
+ input.clearSignatures()
77
+ input.isFullySigned().should.equal(false)
78
+ })
79
+ it('can estimate how heavy is the output going to be', function () {
80
+ var transaction = new Transaction()
81
+ .from(output, [public1, public2, public3], 2)
82
+ .to(address, 1000000)
83
+ var input = transaction.inputs[0]
84
+ input._estimateSize().should.equal(297)
85
+ })
86
+ it('uses SIGHASH_ALL|FORKID by default', function () {
87
+ var transaction = new Transaction()
88
+ .from(output, [public1, public2, public3], 2)
89
+ .to(address, 1000000)
90
+ var input = transaction.inputs[0]
91
+ var sigs = input.getSignatures(transaction, privateKey1, 0)
92
+ sigs[0].sigtype.should.equal(Signature.SIGHASH_ALL | Signature.SIGHASH_FORKID)
93
+ })
94
+ it('roundtrips to/from object', function () {
95
+ var transaction = new Transaction()
96
+ .from(output, [public1, public2, public3], 2)
97
+ .to(address, 1000000)
98
+ .sign(privateKey1)
99
+ var input = transaction.inputs[0]
100
+ var roundtrip = new MultiSigScriptHashInput(input.toObject())
101
+ roundtrip.toObject().should.deep.equal(input.toObject())
102
+ })
103
+ it('roundtrips to/from object when not signed', function () {
104
+ var transaction = new Transaction()
105
+ .from(output, [public1, public2, public3], 2)
106
+ .to(address, 1000000)
107
+ var input = transaction.inputs[0]
108
+ var roundtrip = new MultiSigScriptHashInput(input.toObject())
109
+ roundtrip.toObject().should.deep.equal(input.toObject())
110
+ })
111
+ })
@@ -0,0 +1,68 @@
1
+ 'use strict'
2
+
3
+ var bsv = require('../../..')
4
+ var Transaction = bsv.Transaction
5
+ var PrivateKey = bsv.PrivateKey
6
+
7
+ describe('PublicKeyInput', function () {
8
+ var utxo = {
9
+ txid: '7f3b688cb224ed83e12d9454145c26ac913687086a0a62f2ae0bc10934a4030f',
10
+ vout: 0,
11
+ address: 'n4McBrSkw42eYGX5YMACGpkGUJKL3jVSbo',
12
+ scriptPubKey: '2103c9594cb2ebfebcb0cfd29eacd40ba012606a197beef76f0269ed8c101e56ceddac',
13
+ amount: 50,
14
+ confirmations: 104,
15
+ spendable: true
16
+ }
17
+ var privateKey = PrivateKey.fromWIF('cQ7tSSQDEwaxg9usnnP1Aztqvm9nCQVfNWz9kU2rdocDjknF2vd6')
18
+ var address = privateKey.toAddress()
19
+ utxo.address.should.equal(address.toString())
20
+
21
+ var destKey = new PrivateKey()
22
+
23
+ it('will correctly sign a publickey out transaction', function () {
24
+ var tx = new Transaction()
25
+ tx.from(utxo)
26
+ tx.to(destKey.toAddress(), 10000)
27
+ tx.sign(privateKey)
28
+ tx.inputs[0].script.toBuffer().length.should.be.above(0)
29
+ })
30
+
31
+ it('count can count missing signatures', function () {
32
+ var tx = new Transaction()
33
+ tx.from(utxo)
34
+ tx.to(destKey.toAddress(), 10000)
35
+ var input = tx.inputs[0]
36
+ input.isFullySigned().should.equal(false)
37
+ tx.sign(privateKey)
38
+ input.isFullySigned().should.equal(true)
39
+ })
40
+
41
+ it('it\'s size can be estimated', function () {
42
+ var tx = new Transaction()
43
+ tx.from(utxo)
44
+ tx.to(destKey.toAddress(), 10000)
45
+ var input = tx.inputs[0]
46
+ input._estimateSize().should.equal(114)
47
+ })
48
+
49
+ it('it\'s signature can be removed', function () {
50
+ var tx = new Transaction()
51
+ tx.from(utxo)
52
+ tx.to(destKey.toAddress(), 10000)
53
+ var input = tx.inputs[0]
54
+ tx.sign(privateKey)
55
+ input.isFullySigned().should.equal(true)
56
+ input.clearSignatures()
57
+ input.isFullySigned().should.equal(false)
58
+ })
59
+
60
+ it('returns an empty array if private key mismatches', function () {
61
+ var tx = new Transaction()
62
+ tx.from(utxo)
63
+ tx.to(destKey.toAddress(), 10000)
64
+ var input = tx.inputs[0]
65
+ var signatures = input.getSignatures(tx, new PrivateKey(), 0)
66
+ signatures.length.should.equal(0)
67
+ })
68
+ })
@@ -0,0 +1,59 @@
1
+ 'use strict'
2
+
3
+ require('chai').should()
4
+
5
+ var bsv = require('../../..')
6
+ var Transaction = bsv.Transaction
7
+ var PrivateKey = bsv.PrivateKey
8
+ var Address = bsv.Address
9
+ var Script = bsv.Script
10
+ var Networks = bsv.Networks
11
+
12
+ describe('PublicKeyHashInput', function () {
13
+ var privateKey = new PrivateKey('KwF9LjRraetZuEjR8VqEq539z137LW5anYDUnVK11vM3mNMHTWb4')
14
+ var publicKey = privateKey.publicKey
15
+ var address = new Address(publicKey, Networks.livenet)
16
+
17
+ var output = {
18
+ address: '33zbk2aSZYdNbRsMPPt6jgy6Kq1kQreqeb',
19
+ txId: '66e64ef8a3b384164b78453fa8c8194de9a473ba14f89485a0e433699daec140',
20
+ outputIndex: 0,
21
+ script: new Script(address),
22
+ satoshis: 1000000
23
+ }
24
+ it('can count missing signatures', function () {
25
+ var transaction = new Transaction()
26
+ .from(output)
27
+ .to(address, 1000000)
28
+ var input = transaction.inputs[0]
29
+
30
+ input.isFullySigned().should.equal(false)
31
+ transaction.sign(privateKey)
32
+ input.isFullySigned().should.equal(true)
33
+ })
34
+ it('it\'s size can be estimated', function () {
35
+ var transaction = new Transaction()
36
+ .from(output)
37
+ .to(address, 1000000)
38
+ var input = transaction.inputs[0]
39
+ input._estimateSize().should.equal(148)
40
+ })
41
+ it('it\'s signature can be removed', function () {
42
+ var transaction = new Transaction()
43
+ .from(output)
44
+ .to(address, 1000000)
45
+ var input = transaction.inputs[0]
46
+
47
+ transaction.sign(privateKey)
48
+ input.clearSignatures()
49
+ input.isFullySigned().should.equal(false)
50
+ })
51
+ it('returns an empty array if private key mismatches', function () {
52
+ var transaction = new Transaction()
53
+ .from(output)
54
+ .to(address, 1000000)
55
+ var input = transaction.inputs[0]
56
+ var signatures = input.getSignatures(transaction, new PrivateKey(), 0)
57
+ signatures.length.should.equal(0)
58
+ })
59
+ })
@@ -0,0 +1,185 @@
1
+ 'use strict'
2
+
3
+ var should = require('chai').should()
4
+
5
+ var bsv = require('../..')
6
+ var BN = bsv.crypto.BN
7
+ var BufferWriter = bsv.encoding.BufferWriter
8
+ var BufferReader = bsv.encoding.BufferReader
9
+ var Output = bsv.Transaction.Output
10
+ var Script = bsv.Script
11
+
12
+ describe('Output', function () {
13
+ var output = new Output({
14
+ satoshis: 0,
15
+ script: Script.empty()
16
+ })
17
+
18
+ it('throws error with unrecognized argument', function () {
19
+ (function () {
20
+ new Output(12345) //eslint-disable-line
21
+ }.should.throw(TypeError))
22
+ })
23
+
24
+ it('can be assigned a satoshi amount in big number', function () {
25
+ var newOutput = new Output({
26
+ satoshis: new BN(100),
27
+ script: Script.empty()
28
+ })
29
+ newOutput.satoshis.should.equal(100)
30
+ })
31
+
32
+ it('can be assigned a satoshi amount with a string', function () {
33
+ var newOutput = new Output({
34
+ satoshis: '100',
35
+ script: Script.empty()
36
+ })
37
+ newOutput.satoshis.should.equal(100)
38
+ })
39
+
40
+ describe('will error if output is not a positive integer', function () {
41
+ it('-100', function () {
42
+ (function () {
43
+ new Output({ //eslint-disable-line
44
+ satoshis: -100,
45
+ script: Script.empty()
46
+ })
47
+ }.should.throw('Output satoshis is not a natural number'))
48
+ })
49
+
50
+ it('1.1', function () {
51
+ (function () {
52
+ new Output({ //eslint-disable-line
53
+ satoshis: 1.1,
54
+ script: Script.empty()
55
+ })
56
+ }.should.throw('Output satoshis is not a natural number'))
57
+ })
58
+
59
+ it('NaN', function () {
60
+ (function () {
61
+ new Output({ //eslint-disable-line
62
+ satoshis: NaN,
63
+ script: Script.empty()
64
+ })
65
+ }.should.throw('Output satoshis is not a natural number'))
66
+ })
67
+
68
+ it('Infinity', function () {
69
+ (function () {
70
+ new Output({ //eslint-disable-line
71
+ satoshis: Infinity,
72
+ script: Script.empty()
73
+ })
74
+ }.should.throw('Output satoshis is not a natural number'))
75
+ })
76
+ })
77
+
78
+ var expectEqualOutputs = function (a, b) {
79
+ a.satoshis.should.equal(b.satoshis)
80
+ a.script.toString().should.equal(b.script.toString())
81
+ }
82
+
83
+ it('deserializes correctly a simple output', function () {
84
+ var writer = new BufferWriter()
85
+ output.toBufferWriter(writer)
86
+ var deserialized = Output.fromBufferReader(
87
+ new BufferReader(writer.toBuffer())
88
+ )
89
+ expectEqualOutputs(output, deserialized)
90
+ })
91
+
92
+ it('can instantiate from an object', function () {
93
+ var out = new Output(output.toObject())
94
+ should.exist(out)
95
+ })
96
+
97
+ it('can set a script from a buffer', function () {
98
+ var newOutput = new Output(output.toObject())
99
+ newOutput.setScript(Script().add(0).toBuffer())
100
+ newOutput.inspect().should.equal('<Output (0 sats) <Script: OP_0>>')
101
+ })
102
+
103
+ it('has a inspect property', function () {
104
+ output.inspect().should.equal('<Output (0 sats) <Script: >>')
105
+ })
106
+
107
+ var output2 = new Output({
108
+ satoshis: 1100000000,
109
+ script: new Script(
110
+ 'OP_2 21 0x038282263212c609d9ea2a6e3e172de238d8c39' +
111
+ 'cabd5ac1ca10646e23fd5f51508 21 0x038282263212c609d9ea2a6e3e172de23' +
112
+ '8d8c39cabd5ac1ca10646e23fd5f51508 OP_2 OP_CHECKMULTISIG OP_EQUAL'
113
+ )
114
+ })
115
+
116
+ it('toBufferWriter', function () {
117
+ output2
118
+ .toBufferWriter()
119
+ .toBuffer()
120
+ .toString('hex')
121
+ .should.equal(
122
+ '00ab904100000000485215038282263212c609d9ea2a6e3e172de2' +
123
+ '38d8c39cabd5ac1ca10646e23fd5f5150815038282263212c609d9ea2a6e3e172d' +
124
+ 'e238d8c39cabd5ac1ca10646e23fd5f5150852ae87'
125
+ )
126
+ })
127
+
128
+ it('roundtrips to/from object', function () {
129
+ var newOutput = new Output({
130
+ satoshis: 50,
131
+ script: new Script().add(0)
132
+ })
133
+ var otherOutput = new Output(newOutput.toObject())
134
+ expectEqualOutputs(newOutput, otherOutput)
135
+ })
136
+
137
+ it('toObject will handle an invalid (null) script', function () {
138
+ // block 000000000000000b7e48f88e86ceee3e97b4df7c139f5411d14735c1b3c36791 (livenet)
139
+ // transaction index 2
140
+ // txid ebc9fa1196a59e192352d76c0f6e73167046b9d37b8302b6bb6968dfd279b767
141
+ var transaction = bsv.Transaction()
142
+ transaction.fromString(
143
+ '01000000019ac03d5ae6a875d970128ef9086cef276a1919684a6988023cc7254691d97e6d010000006b4830450221009d41dc793ba24e65f571473d40b299b6459087cea1509f0d381740b1ac863cb6022039c425906fcaf51b2b84d8092569fb3213de43abaff2180e2a799d4fcb4dd0aa012102d5ede09a8ae667d0f855ef90325e27f6ce35bbe60a1e6e87af7f5b3c652140fdffffffff080100000000000000010101000000000000000202010100000000000000014c0100000000000000034c02010100000000000000014d0100000000000000044dffff010100000000000000014e0100000000000000064effffffff0100000000'
144
+ )
145
+ var obj = transaction.toObject()
146
+ obj.outputs[2].script.should.equal('4c')
147
+ obj.outputs[4].script.should.equal('4d')
148
+ obj.outputs[6].script.should.equal('4e')
149
+ })
150
+
151
+ it('#toObject roundtrip will handle an invalid (null) script', function () {
152
+ var invalidOutputScript = Buffer.from('0100000000000000014c', 'hex')
153
+ var br = new bsv.encoding.BufferReader(invalidOutputScript)
154
+ var output = Output.fromBufferReader(br)
155
+ var output2 = new Output(output.toObject())
156
+ should.equal(output2.script, null)
157
+ should.equal(output2._scriptBuffer.toString('hex'), '4c')
158
+ })
159
+
160
+ it('inspect will work with an invalid (null) script', function () {
161
+ var invalidOutputScript = Buffer.from('0100000000000000014c', 'hex')
162
+ var br = new bsv.encoding.BufferReader(invalidOutputScript)
163
+ var output = Output.fromBufferReader(br)
164
+ output.inspect().should.equal('<Output (1 sats) 4c>')
165
+ })
166
+
167
+ it('roundtrips to/from JSON', function () {
168
+ var json = JSON.stringify(output2)
169
+ var o3 = new Output(JSON.parse(json))
170
+ JSON.stringify(o3).should.equal(json)
171
+ })
172
+
173
+ it('setScript fails with invalid input', function () {
174
+ var out = new Output(output2.toJSON())
175
+ out.setScript.bind(out, 45).should.throw('Invalid argument type: script')
176
+ })
177
+
178
+ it('sets script to null if it is an InvalidBuffer', function () {
179
+ var output = new Output({
180
+ satoshis: 1000,
181
+ script: Buffer.from('4c', 'hex')
182
+ })
183
+ should.equal(output.script, null)
184
+ })
185
+ })