@opcat-labs/opcat 1.0.0

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 (138) hide show
  1. package/.mocharc.yaml +3 -0
  2. package/index.d.ts +1541 -0
  3. package/index.js +74 -0
  4. package/lib/address.js +478 -0
  5. package/lib/block/block.js +277 -0
  6. package/lib/block/blockheader.js +295 -0
  7. package/lib/block/index.js +4 -0
  8. package/lib/block/merkleblock.js +323 -0
  9. package/lib/bn.js +3423 -0
  10. package/lib/crypto/bn.js +278 -0
  11. package/lib/crypto/ecdsa.js +339 -0
  12. package/lib/crypto/hash.browser.js +171 -0
  13. package/lib/crypto/hash.js +2 -0
  14. package/lib/crypto/hash.node.js +171 -0
  15. package/lib/crypto/point.js +221 -0
  16. package/lib/crypto/random.browser.js +28 -0
  17. package/lib/crypto/random.js +2 -0
  18. package/lib/crypto/random.node.js +11 -0
  19. package/lib/crypto/signature.js +325 -0
  20. package/lib/encoding/base58.js +111 -0
  21. package/lib/encoding/base58check.js +121 -0
  22. package/lib/encoding/bufferreader.js +212 -0
  23. package/lib/encoding/bufferwriter.js +140 -0
  24. package/lib/encoding/decode-asm.js +24 -0
  25. package/lib/encoding/decode-hex.js +32 -0
  26. package/lib/encoding/decode-script-chunks.js +43 -0
  27. package/lib/encoding/encode-hex.js +284 -0
  28. package/lib/encoding/is-hex.js +7 -0
  29. package/lib/encoding/varint.js +75 -0
  30. package/lib/errors/index.js +54 -0
  31. package/lib/errors/spec.js +314 -0
  32. package/lib/hash-cache.js +50 -0
  33. package/lib/hdprivatekey.js +678 -0
  34. package/lib/hdpublickey.js +525 -0
  35. package/lib/message/message.js +191 -0
  36. package/lib/mnemonic/mnemonic.js +303 -0
  37. package/lib/mnemonic/pbkdf2.browser.js +68 -0
  38. package/lib/mnemonic/pbkdf2.js +2 -0
  39. package/lib/mnemonic/pbkdf2.node.js +68 -0
  40. package/lib/mnemonic/words/chinese.js +2054 -0
  41. package/lib/mnemonic/words/english.js +2054 -0
  42. package/lib/mnemonic/words/french.js +2054 -0
  43. package/lib/mnemonic/words/index.js +8 -0
  44. package/lib/mnemonic/words/italian.js +2054 -0
  45. package/lib/mnemonic/words/japanese.js +2054 -0
  46. package/lib/mnemonic/words/spanish.js +2054 -0
  47. package/lib/networks.js +379 -0
  48. package/lib/opcode.js +255 -0
  49. package/lib/privatekey.js +374 -0
  50. package/lib/publickey.js +386 -0
  51. package/lib/script/index.js +5 -0
  52. package/lib/script/interpreter.js +1834 -0
  53. package/lib/script/script.js +1074 -0
  54. package/lib/script/stack.js +109 -0
  55. package/lib/script/write-i32-le.js +17 -0
  56. package/lib/script/write-push-data.js +35 -0
  57. package/lib/script/write-u16-le.js +12 -0
  58. package/lib/script/write-u32-le.js +16 -0
  59. package/lib/script/write-u64-le.js +24 -0
  60. package/lib/script/write-u8-le.js +8 -0
  61. package/lib/script/write-varint.js +46 -0
  62. package/lib/transaction/index.js +7 -0
  63. package/lib/transaction/input/index.js +5 -0
  64. package/lib/transaction/input/input.js +354 -0
  65. package/lib/transaction/input/multisig.js +242 -0
  66. package/lib/transaction/input/publickey.js +100 -0
  67. package/lib/transaction/input/publickeyhash.js +118 -0
  68. package/lib/transaction/output.js +231 -0
  69. package/lib/transaction/sighash.js +167 -0
  70. package/lib/transaction/signature.js +97 -0
  71. package/lib/transaction/transaction.js +1639 -0
  72. package/lib/transaction/unspentoutput.js +113 -0
  73. package/lib/util/_.js +47 -0
  74. package/lib/util/js.js +90 -0
  75. package/lib/util/preconditions.js +33 -0
  76. package/package.json +26 -0
  77. package/test/address.js +509 -0
  78. package/test/block/block.js +251 -0
  79. package/test/block/blockheader.js +275 -0
  80. package/test/block/merklebloack.js +211 -0
  81. package/test/crypto/bn.js +177 -0
  82. package/test/crypto/ecdsa.js +391 -0
  83. package/test/crypto/hash.browser.js +135 -0
  84. package/test/crypto/hash.js +136 -0
  85. package/test/crypto/point.js +224 -0
  86. package/test/crypto/random.js +32 -0
  87. package/test/crypto/signature.js +409 -0
  88. package/test/data/bip69.json +215 -0
  89. package/test/data/bitcoind/base58_keys_invalid.json +52 -0
  90. package/test/data/bitcoind/base58_keys_valid.json +335 -0
  91. package/test/data/bitcoind/blocks.json +22 -0
  92. package/test/data/bitcoind/script_tests.json +3822 -0
  93. package/test/data/bitcoind/sig_canonical.json +7 -0
  94. package/test/data/bitcoind/sig_noncanonical.json +36 -0
  95. package/test/data/bitcoind/tx_invalid.json +445 -0
  96. package/test/data/bitcoind/tx_valid.json +44 -0
  97. package/test/data/blk86756-testnet.dat +0 -0
  98. package/test/data/blk86756-testnet.js +14 -0
  99. package/test/data/blk86756-testnet.json +684 -0
  100. package/test/data/block.hex +1 -0
  101. package/test/data/ecdsa.json +230 -0
  102. package/test/data/merkleblocks.js +488 -0
  103. package/test/data/messages.json +22 -0
  104. package/test/data/sighash.json +12 -0
  105. package/test/data/tx_creation.json +95 -0
  106. package/test/encoding/base58.js +131 -0
  107. package/test/encoding/base58check.js +136 -0
  108. package/test/encoding/bufferreader.js +337 -0
  109. package/test/encoding/bufferwriter.js +172 -0
  110. package/test/encoding/varint.js +104 -0
  111. package/test/hashCache.js +67 -0
  112. package/test/hdkeys.js +445 -0
  113. package/test/hdprivatekey.js +332 -0
  114. package/test/hdpublickey.js +304 -0
  115. package/test/index.js +16 -0
  116. package/test/message/message.js +204 -0
  117. package/test/mnemonic/data/fixtures.json +300 -0
  118. package/test/mnemonic/mnemonic.js +259 -0
  119. package/test/mnemonic/mocha.opts +1 -0
  120. package/test/mnemonic/pbkdf2.test.js +59 -0
  121. package/test/networks.js +159 -0
  122. package/test/opcode.js +161 -0
  123. package/test/privatekey.js +439 -0
  124. package/test/publickey.js +554 -0
  125. package/test/script/interpreter.js +734 -0
  126. package/test/script/script.js +1437 -0
  127. package/test/transaction/deserialize.js +34 -0
  128. package/test/transaction/input/input.js +90 -0
  129. package/test/transaction/input/multisig.js +90 -0
  130. package/test/transaction/input/publickey.js +68 -0
  131. package/test/transaction/input/publickeyhash.js +51 -0
  132. package/test/transaction/output.js +185 -0
  133. package/test/transaction/sighash.js +65 -0
  134. package/test/transaction/signature.js +114 -0
  135. package/test/transaction/transaction.js +1109 -0
  136. package/test/transaction/unspentoutput.js +110 -0
  137. package/test/util/js.js +76 -0
  138. package/test/util/preconditions.js +79 -0
@@ -0,0 +1,110 @@
1
+ 'use strict';
2
+
3
+ var chai = require('chai');
4
+ var expect = chai.expect;
5
+ var cloneDeep = require('clone-deep');
6
+
7
+ var opcat = require('../..');
8
+ var UnspentOutput = opcat.Transaction.UnspentOutput;
9
+
10
+ describe('UnspentOutput', function () {
11
+ var sampleData1 = {
12
+ address: 'mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1',
13
+ txId: 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458',
14
+ outputIndex: 0,
15
+ script:
16
+ 'OP_DUP OP_HASH160 20 0x88d9931ea73d60eaf7e5671efc0552b912911f2a OP_EQUALVERIFY OP_CHECKSIG',
17
+ satoshis: 1020000,
18
+ };
19
+ var sampleData2 = {
20
+ txid: 'e42447187db5a29d6db161661e4bc66d61c3e499690fe5ea47f87b79ca573986',
21
+ vout: 1,
22
+ address: 'mgBCJAsvzgT2qNNeXsoECg2uPKrUsZ76up',
23
+ scriptPubKey: '76a914073b7eae2823efa349e3b9155b8a735526463a0f88ac',
24
+ amount: 0.0108,
25
+ data: ""
26
+ };
27
+
28
+ var sampleData3 = {
29
+ txid: 'e42447187db5a29d6db161661e4bc66d61c3e499690fe5ea47f87b79ca573986',
30
+ vout: 1,
31
+ address: 'mgBCJAsvzgT2qNNeXsoECg2uPKrUsZ76up',
32
+ scriptPubKey: '76a914073b7eae2823efa349e3b9155b8a735526463a0f88ac',
33
+ amount: 0.0108,
34
+ data: "68656c6c6f206f70636174"
35
+ };
36
+
37
+ it('roundtrip from raw data', function () {
38
+ expect(UnspentOutput(sampleData2).toObject()).to.deep.equal(sampleData2);
39
+ });
40
+
41
+ it('roundtrip from raw data', function () {
42
+ expect(UnspentOutput(sampleData3).toObject()).to.deep.equal(sampleData3);
43
+ });
44
+
45
+ it('can be created without "new" operand', function () {
46
+ expect(UnspentOutput(sampleData1) instanceof UnspentOutput).to.equal(true);
47
+ });
48
+
49
+ it('fails if no tx id is provided', function () {
50
+ expect(function () {
51
+ return new UnspentOutput({});
52
+ }).to.throw();
53
+ });
54
+
55
+ it('fails if vout is not a number', function () {
56
+ var sample = cloneDeep(sampleData2);
57
+ sample.vout = '1';
58
+ expect(function () {
59
+ return new UnspentOutput(sample);
60
+ }).to.throw();
61
+ });
62
+
63
+ it('displays nicely on the console', function () {
64
+ var expected =
65
+ '<UnspentOutput: a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458:0' +
66
+ ', satoshis: 1020000, address: mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1>';
67
+ expect(new UnspentOutput(sampleData1).inspect()).to.equal(expected);
68
+ });
69
+
70
+ describe('checking the constructor parameters', function () {
71
+ var notDefined = {
72
+ txId: 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458',
73
+ outputIndex: 0,
74
+ script:
75
+ 'OP_DUP OP_HASH160 20 0x88d9931ea73d60eaf7e5671efc0552b912911f2a OP_EQUALVERIFY OP_CHECKSIG',
76
+ };
77
+ var zero = {
78
+ txId: 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458',
79
+ outputIndex: 0,
80
+ script:
81
+ 'OP_DUP OP_HASH160 20 0x88d9931ea73d60eaf7e5671efc0552b912911f2a OP_EQUALVERIFY OP_CHECKSIG',
82
+ amount: 0,
83
+ };
84
+ it('fails when no amount is defined', function () {
85
+ expect(function () {
86
+ return new UnspentOutput(notDefined);
87
+ }).to.throw('Must provide an amount for the output');
88
+ });
89
+ it('does not fail when amount is zero', function () {
90
+ expect(function () {
91
+ return new UnspentOutput(zero);
92
+ }).to.not.throw();
93
+ });
94
+ });
95
+
96
+ it('toString returns txid:vout', function () {
97
+ var expected = 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458:0';
98
+ expect(new UnspentOutput(sampleData1).toString()).to.equal(expected);
99
+ });
100
+
101
+ it('to/from JSON roundtrip', function () {
102
+ var utxo = new UnspentOutput(sampleData2);
103
+ var obj = UnspentOutput.fromObject(utxo.toJSON()).toObject();
104
+ expect(obj).to.deep.equal(sampleData2);
105
+ var str = JSON.stringify(UnspentOutput.fromObject(obj));
106
+ expect(JSON.parse(str)).to.deep.equal(sampleData2);
107
+ var str2 = JSON.stringify(new UnspentOutput(JSON.parse(str)));
108
+ expect(JSON.parse(str2)).to.deep.equal(sampleData2);
109
+ });
110
+ });
@@ -0,0 +1,76 @@
1
+ 'use strict';
2
+
3
+ require('chai').should();
4
+
5
+ var opcat = require('../..');
6
+ var JSUtil = opcat.util.js;
7
+
8
+ describe('js utils', function () {
9
+ describe('isValidJSON', function () {
10
+ var hexa = '8080808080808080808080808080808080808080808080808080808080808080';
11
+ var json = '{"key": ["value", "value2"]}';
12
+ var json2 = '["value", "value2", {"key": "value"}]';
13
+
14
+ it('does not mistake an integer as valid json object', function () {
15
+ var valid = JSUtil.isValidJSON(hexa);
16
+ valid.should.equal(false);
17
+ });
18
+
19
+ it('correctly validates a json object', function () {
20
+ var valid = JSUtil.isValidJSON(json);
21
+ valid.should.equal(true);
22
+ });
23
+
24
+ it('correctly validates an array json object', function () {
25
+ var valid = JSUtil.isValidJSON(json2);
26
+ valid.should.equal(true);
27
+ });
28
+ });
29
+
30
+ describe('isNaturalNumber', function () {
31
+ it('false for float', function () {
32
+ var a = JSUtil.isNaturalNumber(0.1);
33
+ a.should.equal(false);
34
+ });
35
+
36
+ it('false for string float', function () {
37
+ var a = JSUtil.isNaturalNumber('0.1');
38
+ a.should.equal(false);
39
+ });
40
+
41
+ it('false for string integer', function () {
42
+ var a = JSUtil.isNaturalNumber('1');
43
+ a.should.equal(false);
44
+ });
45
+
46
+ it('false for negative integer', function () {
47
+ var a = JSUtil.isNaturalNumber(-1);
48
+ a.should.equal(false);
49
+ });
50
+
51
+ it('false for negative integer string', function () {
52
+ var a = JSUtil.isNaturalNumber('-1');
53
+ a.should.equal(false);
54
+ });
55
+
56
+ it('false for infinity', function () {
57
+ var a = JSUtil.isNaturalNumber(Infinity);
58
+ a.should.equal(false);
59
+ });
60
+
61
+ it('false for NaN', function () {
62
+ var a = JSUtil.isNaturalNumber(NaN);
63
+ a.should.equal(false);
64
+ });
65
+
66
+ it('true for zero', function () {
67
+ var a = JSUtil.isNaturalNumber(0);
68
+ a.should.equal(true);
69
+ });
70
+
71
+ it('true for positive integer', function () {
72
+ var a = JSUtil.isNaturalNumber(1000);
73
+ a.should.equal(true);
74
+ });
75
+ });
76
+ });
@@ -0,0 +1,79 @@
1
+ 'use strict';
2
+
3
+ var should = require('chai').should();
4
+
5
+ var opcat = require('../..');
6
+ var errors = opcat.errors;
7
+ var $ = opcat.util.preconditions;
8
+ var PrivateKey = opcat.PrivateKey;
9
+
10
+ describe('preconditions', function () {
11
+ it('can be used to assert state', function () {
12
+ (function () {
13
+ $.checkState(false, 'testing');
14
+ }).should.throw(errors.InvalidState);
15
+ });
16
+ it('throws no false negative', function () {
17
+ (function () {
18
+ $.checkState(true, 'testing');
19
+ }).should.not.throw();
20
+ });
21
+
22
+ it('can be used to check an argument', function () {
23
+ (function () {
24
+ $.checkArgument(false, 'testing');
25
+ }).should.throw(errors.InvalidArgument);
26
+
27
+ (function () {
28
+ $.checkArgument(true, 'testing');
29
+ }).should.not.throw(errors.InvalidArgument);
30
+ });
31
+
32
+ it('can be used to check an argument type', function () {
33
+ var error;
34
+ try {
35
+ $.checkArgumentType(1, 'string', 'argumentName');
36
+ } catch (e) {
37
+ error = e;
38
+ e.message.should.equal('Invalid Argument for argumentName, expected string but got number');
39
+ }
40
+ should.exist(error);
41
+ });
42
+ it('has no false negatives when used to check an argument type', function () {
43
+ (function () {
44
+ $.checkArgumentType('a String', 'string', 'argumentName');
45
+ }).should.not.throw();
46
+ });
47
+
48
+ it('can be used to check an argument type for a class', function () {
49
+ var error;
50
+ try {
51
+ $.checkArgumentType(1, PrivateKey);
52
+ } catch (e) {
53
+ error = e;
54
+ var fail = !~e.message.indexOf('Invalid Argument for (unknown name)');
55
+ fail.should.equal(false);
56
+ }
57
+ should.exist(error);
58
+ });
59
+ it('has no false negatives when checking a type for a class', function () {
60
+ (function () {
61
+ $.checkArgumentType(new PrivateKey(), PrivateKey);
62
+ }).should.not.throw();
63
+ });
64
+
65
+ it('formats correctly a message on InvalidArgument()', function () {
66
+ var error = new errors.InvalidArgument();
67
+ error.message.should.equal('Invalid Argument');
68
+ });
69
+
70
+ it('formats correctly a message on checkArgument', function () {
71
+ var error;
72
+ try {
73
+ $.checkArgument(null, 'parameter must be provided');
74
+ } catch (e) {
75
+ error = e;
76
+ }
77
+ error.message.should.equal('Invalid Argument: parameter must be provided');
78
+ });
79
+ });