@smartledger/bsv 5.5.0 → 5.5.2

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 (93) hide show
  1. package/.mocharc.json +5 -0
  2. package/CHANGELOG.md +40 -0
  3. package/README.md +38 -38
  4. package/bsv-covenant.min.js +2 -2
  5. package/bsv-gdaf.min.js +2 -2
  6. package/bsv-ltp.min.js +2 -2
  7. package/bsv-smartcontract.min.js +2 -2
  8. package/bsv.bundle.js +2 -2
  9. package/bsv.min.js +2 -2
  10. package/docs/MODULE_REFERENCE_COMPLETE.md +27 -27
  11. package/docs/advanced/UTXO_MANAGER_GUIDE.md +1 -1
  12. package/docs/getting-started/INSTALLATION.md +23 -23
  13. package/docs/getting-started/QUICK_START.md +7 -7
  14. package/docs/migration/FROM_BSV_1_5_6.md +5 -5
  15. package/lib/smart_contract/covenant_helpers.js +3 -3
  16. package/lib/smart_contract/index.js +4 -5
  17. package/package.json +5 -1
  18. package/test/address.js +629 -0
  19. package/test/block/block.js +239 -0
  20. package/test/block/blockheader.js +270 -0
  21. package/test/block/merkleblock.js +207 -0
  22. package/test/cli/smoke.js +261 -0
  23. package/test/credentials-test.js +329 -0
  24. package/test/crypto/bn.js +168 -0
  25. package/test/crypto/ecdsa.js +328 -0
  26. package/test/crypto/elliptic-fixed.js +61 -0
  27. package/test/crypto/hash.browser.js +121 -0
  28. package/test/crypto/hash.js +122 -0
  29. package/test/crypto/point.js +193 -0
  30. package/test/crypto/random.js +32 -0
  31. package/test/crypto/security.js +127 -0
  32. package/test/crypto/shamir.js +169 -0
  33. package/test/crypto/signature.js +318 -0
  34. package/test/data/bip69.json +215 -0
  35. package/test/data/bitcoind/base58_keys_invalid.json +152 -0
  36. package/test/data/bitcoind/base58_keys_valid.json +452 -0
  37. package/test/data/bitcoind/blocks.json +27 -0
  38. package/test/data/bitcoind/script_tests.json +2244 -0
  39. package/test/data/bitcoind/sig_canonical.json +7 -0
  40. package/test/data/bitcoind/sig_noncanonical.json +22 -0
  41. package/test/data/bitcoind/tx_invalid.json +177 -0
  42. package/test/data/bitcoind/tx_valid.json +224 -0
  43. package/test/data/blk86756-testnet.dat +0 -0
  44. package/test/data/blk86756-testnet.js +12 -0
  45. package/test/data/blk86756-testnet.json +684 -0
  46. package/test/data/ecdsa.json +230 -0
  47. package/test/data/merkleblocks.js +486 -0
  48. package/test/data/messages.json +22 -0
  49. package/test/data/sighash.json +1004 -0
  50. package/test/data/tx_creation.json +85 -0
  51. package/test/ecies/bitcore-ecies.js +170 -0
  52. package/test/ecies/electrum-ecies.js +206 -0
  53. package/test/encoding/base58.js +131 -0
  54. package/test/encoding/base58check.js +136 -0
  55. package/test/encoding/bufferreader.js +328 -0
  56. package/test/encoding/bufferwriter.js +160 -0
  57. package/test/encoding/varint.js +104 -0
  58. package/test/gdaf/canonicalize.js +140 -0
  59. package/test/hdkeys.js +365 -0
  60. package/test/hdprivatekey.js +330 -0
  61. package/test/hdpublickey.js +293 -0
  62. package/test/index.js +16 -0
  63. package/test/ltp/right.js +198 -0
  64. package/test/message/message.js +190 -0
  65. package/test/mnemonic/data/fixtures.json +300 -0
  66. package/test/mnemonic/mnemonic.js +245 -0
  67. package/test/mnemonic/mocha.opts +1 -0
  68. package/test/mnemonic/pbkdf2.test.js +43 -0
  69. package/test/networks.js +162 -0
  70. package/test/opcode.js +161 -0
  71. package/test/privatekey.js +430 -0
  72. package/test/publickey.js +411 -0
  73. package/test/script/genesis_limits.js +80 -0
  74. package/test/script/interpreter.js +723 -0
  75. package/test/script/script.js +1256 -0
  76. package/test/script/string_ops.js +72 -0
  77. package/test/smart_contract/covenants.js +247 -0
  78. package/test/smart_contract/dsl_debugger.js +103 -0
  79. package/test/smart_contract/token_generalized.js +187 -0
  80. package/test/transaction/deserialize.js +33 -0
  81. package/test/transaction/input/input.js +92 -0
  82. package/test/transaction/input/multisig.js +174 -0
  83. package/test/transaction/input/multisigscripthash.js +111 -0
  84. package/test/transaction/input/publickey.js +68 -0
  85. package/test/transaction/input/publickeyhash.js +59 -0
  86. package/test/transaction/output.js +185 -0
  87. package/test/transaction/sighash.js +71 -0
  88. package/test/transaction/signature.js +127 -0
  89. package/test/transaction/transaction.js +1299 -0
  90. package/test/transaction/unspentoutput.js +92 -0
  91. package/test/util/js.js +76 -0
  92. package/test/util/preconditions.js +79 -0
  93. package/test/vcjwt/interop.js +126 -0
@@ -0,0 +1,85 @@
1
+ [
2
+ [
3
+ "from", [{
4
+ "address": "mszYqVnqKoQx4jcTdJXxwKAissE3Jbrrc1",
5
+ "txId": "a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458",
6
+ "outputIndex": 0,
7
+ "script": "OP_DUP OP_HASH160 20 0x88d9931ea73d60eaf7e5671efc0552b912911f2a OP_EQUALVERIFY OP_CHECKSIG",
8
+ "satoshis": 1020000
9
+ }],
10
+ "to", ["mrU9pEmAx26HcbKVrABvgL7AwA5fjNFoDc", 1010000],
11
+ "sign", ["cSBnVM4xvxarwGQuAfQFwqDg9k5tErHUHzgWsEfD4zdwUasvqRVY", 1],
12
+ "serialize", "01000000015884e5db9de218238671572340b207ee85b628074e7e467096c267266baf77a4000000006b4830450221009972100061da4a17a471ac1906c18bb5445c03da2a0be52c59aca6c58f1e342302202b7a19a22572cabb6e55c368ebdb5921541358fa969d5c76b5e0d6ad3f26a7d701210223078d2942df62c45621d209fab84ea9a7a23346201b7727b9b45a29c4e76f5effffffff0150690f00000000001976a9147821c0a3768aa9d1a37e16cf76002aef5373f1a888ac00000000"
13
+ ],
14
+ [
15
+ "from", [{
16
+ "txid": "e42447187db5a29d6db161661e4bc66d61c3e499690fe5ea47f87b79ca573986",
17
+ "vout": 1,
18
+ "address": "mgBCJAsvzgT2qNNeXsoECg2uPKrUsZ76up",
19
+ "scriptPubKey": "76a914073b7eae2823efa349e3b9155b8a735526463a0f88ac",
20
+ "amount": 0.01080000
21
+ }],
22
+ "to", ["mn9new5vPYWuVN5m3gUBujfKh1uPQvR9mf", 500000],
23
+ "to", ["mw5ctwgEaNRbxkM4JhXH3rp5AyGvTWDZCD", 570000],
24
+ "sign", ["cSQUuwwJBAg6tYQhzqqLWW115D1s5KFZDyhCF2ffrnukZxMK6rNZ", 1],
25
+ "serialize", "0100000001863957ca797bf847eae50f6999e4c3616dc64b1e6661b16d9da2b57d184724e4010000006b483045022100855691c90510edf83ab632f0a0b17f5202d2cf7071050dcf0c2778325ed403cd022014ee7a4e787da8bc088d2ece43108a7b8d7112eba89876a27bb44195a0715a910121039dd446bbc85db6917f39c0b4c295b0f8cce76d1926fa76d7b84e3f7ff1c5eec5ffffffff0220a10700000000001976a91448c819246ae5645ceecd41fbe1aa6202a0a9b5ca88ac90b20800000000001976a914aab76ba4877d696590d94ea3e02948b55294815188ac00000000"
26
+ ],
27
+ [
28
+ "from", [
29
+ [{
30
+ "txid": "a9db84566e0fc9351e86337d2828ab281b25ddc06fab798f6d4b5baef48c02b3",
31
+ "vout": 0,
32
+ "address": "mn9new5vPYWuVN5m3gUBujfKh1uPQvR9mf",
33
+ "account": "",
34
+ "scriptPubKey": "76a91448c819246ae5645ceecd41fbe1aa6202a0a9b5ca88ac",
35
+ "amount": 0.00500000,
36
+ "confirmations": 0
37
+ }, {
38
+ "txid": "a9db84566e0fc9351e86337d2828ab281b25ddc06fab798f6d4b5baef48c02b3",
39
+ "vout": 1,
40
+ "address": "mw5ctwgEaNRbxkM4JhXH3rp5AyGvTWDZCD",
41
+ "account": "",
42
+ "scriptPubKey": "76a914aab76ba4877d696590d94ea3e02948b55294815188ac",
43
+ "amount": 0.00570000,
44
+ "confirmations": 0
45
+ }]
46
+ ],
47
+ "to", ["mtymcCX5KixPjT1zxtg59qewBGWptj9etH", 1060000],
48
+ "sign", [
49
+ ["cPGbA2C54ZZ1sw4dc2ckBE1WqkdrNSbEV8Tkjhi2p1J15oErdgP2", "cSpyve5bXAuyHrNeV9MjTdFz3HLw739yUjjUAUSMe3ppf2qzj2hw"], 1
50
+ ],
51
+ "serialize", "0100000002b3028cf4ae5b4b6d8f79ab6fc0dd251b28ab28287d33861e35c90f6e5684dba9000000006a4730440220635e95e1981bbb360feaf4c232f626a0af8eb5c043a99749a21b0e37fd0048fd02201c5db8a93f1f5c268be6167da7c92f5030481acf65bef750c81ec9907aba3b170121030253c73236acf5ea9085d408220141197f6094de07426bd0d32c7a543614fdd7ffffffffb3028cf4ae5b4b6d8f79ab6fc0dd251b28ab28287d33861e35c90f6e5684dba9010000006a4730440220319a0b5ee9c67ccb7de4222234f31059354be4f239c99ca24bff30adfec8e8ec02204ad4c6dedebb20d100f9484b9ea6c5ba36712e9da155c24509e793c02e4805fd012102977a001a0a7bbfd1f8a647c7d46e13e8f6920635b328390b43b3303977101149ffffffff01a02c1000000000001976a91493abf1e9e4a20c125b93f93ee39efc16b6e4bc4688ac00000000"
52
+ ],
53
+ [
54
+ "from", [{
55
+ "address": "mgJT8iegL4f9NCgQFeFyfvnSw1Yj4M5Woi",
56
+ "txid": "f50e13cecda9a438ebd7df213a2899e42b2461a18d4630ee773d26b4f2688bdc",
57
+ "vout": 1,
58
+ "scriptPubKey": "76a914089acaba6af8b2b4fb4bed3b747ab1e4e60b496588ac",
59
+ "amount": 0.01
60
+ }],
61
+ "to", ["n3riXZowrjGnY74rx7Hdi9wCyvgyJC28zZ", 990000],
62
+ "sign", ["cPwWtDztEgRCMCU8pMQp4HgphvyadrAsYBrCjXUZuDSmnZkyoyNF", 1],
63
+ "serialize", "0100000001dc8b68f2b4263d77ee30468da161242be499283a21dfd7eb38a4a9cdce130ef5010000006a473044022012610834051e6af8594dd5d9c47123d6ce03537d321f9fe3f6ff244b23f47dd5022034901bb0d83688758a9248fdef48bd2000b55cf70111cbef8f206e72a31aaf61012103e26b47e7c0d8946954bf9dd4bc7f9e415437eb98271d05f69e78cef8fc6c9a54ffffffff01301b0f00000000001976a914f50f9826ef186074c6fe206cca6b71472ff07ba888ac00000000"
64
+ ],
65
+ [
66
+ "from", [{
67
+ "address":"38nw4sTs3fCH1YiBjYeQAX1t9eWMxpek8Z","txid":"073281b2cc94e879aaf30ea2e92947d9827b270015849d3a5b96a89ee15bfa66","vout":0,"ts":1418877950,"scriptPubKey":"a9144de752833233fe69a20064f29b2ca0f6399c8af387","amount":0.00318512,"confirmationsFromCache":false
68
+ }, ["03411e021210a933c247ea9c8dad4bbead281597d508ad84e899de13e4ce98b4ba", "03bda2c3cc11a391b36e3d695b03ab42b96fe04086b2349056108d858983e60dab", "03c16e6ecf4e02bf9db23d3247ab92db8881f0866133ffb530c3470f6d8dea7330"], 2],
69
+ "to", ["3BazTqvkvEBcWk7J4sbgRnxUw6rjYrogf9", 300299],
70
+ "sign", [["L3y1PAXfjayeB3w6NP7qfNxPbuPpQFA6hd6gj4BUKa2xSdLwoVSZ", "L3a57fiNjqfbXBdYvvGYPQ1HiDX463mwA2xFGTYVkXYdooaz9Z2u"], 1],
71
+ "serialize", "010000000166fa5be19ea8965b3a9d841500277b82d94729e9a20ef3aa79e894ccb281320700000000fc0047304402202f66fcfc0d5bff109ec12327ca37e0bc7722d08da9538f12468994799534bed602203a0b374f3a6810b0ee26028c390e14f5b6bada8fa390f4364d70d04aee240d350147304402206757f710f25937cd846fd8c56687c463a4561e9e0421fc173c78eaf2de9b9a7d02204995db6b81513b67a42b09dabab6ed4cb90901c52f6f945b768827a9390bda60014c69522103411e021210a933c247ea9c8dad4bbead281597d508ad84e899de13e4ce98b4ba2103bda2c3cc11a391b36e3d695b03ab42b96fe04086b2349056108d858983e60dab2103c16e6ecf4e02bf9db23d3247ab92db8881f0866133ffb530c3470f6d8dea733053aeffffffff010b9504000000000017a9146c8d8b04c6a1e664b1ec20ec932760760c97688e8700000000"
72
+ ],
73
+ [
74
+ "from", [{"address":"3BazTqvkvEBcWk7J4sbgRnxUw6rjYrogf9","txid":"afbf98ca4a43db8915d75184b5204fbe71d916482adfe85cb0ed3635764fc220","vout":0,"ts":1418878108,"scriptPubKey":"a9146c8d8b04c6a1e664b1ec20ec932760760c97688e87","amount":0.00318512,"confirmationsFromCache":false}, ["020483ebb834d91d494a3b649cf0e8f5c9c4fcec5f194ab94341cc99bb440007f2", "0271ebaeef1c2bf0c1a4772d1391eab03e4d96a6e9b48551ab4e4b0d2983eb452b", "03a659828aabe443e2dedabb1db5a22335c5ace5b5b7126998a288d63c99516dd8"], 2
75
+ ],
76
+ "from", [
77
+ {"address":"3BazTqvkvEBcWk7J4sbgRnxUw6rjYrogf9","txid":"dc2e197ab72f71912c39bc23a42d823a3aa8d469fe65eb591c086e60d14c64a0","vout":0,"ts":1418878014,"scriptPubKey":"a9146c8d8b04c6a1e664b1ec20ec932760760c97688e87","amount":0.00300299,"confirmationsFromCache":false}, ["020483ebb834d91d494a3b649cf0e8f5c9c4fcec5f194ab94341cc99bb440007f2", "0271ebaeef1c2bf0c1a4772d1391eab03e4d96a6e9b48551ab4e4b0d2983eb452b", "03a659828aabe443e2dedabb1db5a22335c5ace5b5b7126998a288d63c99516dd8"], 2],
78
+ "to", ["38nw4sTs3fCH1YiBjYeQAX1t9eWMxpek8Z", 150000],
79
+ "to", ["38nw4sTs3fCH1YiBjYeQAX1t9eWMxpek8Z", 160000],
80
+ "change", ["3BazTqvkvEBcWk7J4sbgRnxUw6rjYrogf9"],
81
+ "sign", ["L2U9m5My3cdyN5qX1PH4B7XstGDZFWwyukdX8gj8vsJ3fkrqArQo", 1],
82
+ "sign", ["L4jFVcDaqZCkknP5KQWjCBgiLFxKxRxywNGTucm3jC3ozByZcbZv", 1],
83
+ "serialize", "010000000220c24f763536edb05ce8df2a4816d971be4f20b58451d71589db434aca98bfaf00000000fdfd0000473044022049971af56696e7468c82fde7502487c5a71c207029e18b8525e7947ed33c8b7e022008faed03c20cfd593d3e57ee938781dd1419c2b9b9caca98d0e2928cb05484fb01483045022100d6d3cc551fbd0d97117c60f4abe1471fcf57bb2f02a41a9102ed64b45df30428022078186f084e1c05f2656f9a407d8ca2b4965270cdf3aeba6dc5d4afda51be6983014c695221020483ebb834d91d494a3b649cf0e8f5c9c4fcec5f194ab94341cc99bb440007f2210271ebaeef1c2bf0c1a4772d1391eab03e4d96a6e9b48551ab4e4b0d2983eb452b2103a659828aabe443e2dedabb1db5a22335c5ace5b5b7126998a288d63c99516dd853aeffffffffa0644cd1606e081c59eb65fe69d4a83a3a822da423bc392c91712fb77a192edc00000000fdfe0000483045022100ccadfd2f98a3a8eea787c19d7d3119d0504b051293c0e1eb420c26dc535a74b702207d5b38040994e86204604222d87278456ee4a376c340d1376d1cf86c88d0f8c801483045022100cadab93035584dc94f498898a2df329dd83e2a86e94bb96b54a629386095f59e022043a1dc8d8160495b63180163278513d9d8dfbf8faa580e58fa3ee3169a5adee5014c695221020483ebb834d91d494a3b649cf0e8f5c9c4fcec5f194ab94341cc99bb440007f2210271ebaeef1c2bf0c1a4772d1391eab03e4d96a6e9b48551ab4e4b0d2983eb452b2103a659828aabe443e2dedabb1db5a22335c5ace5b5b7126998a288d63c99516dd853aeffffffff03f04902000000000017a9144de752833233fe69a20064f29b2ca0f6399c8af387007102000000000017a9144de752833233fe69a20064f29b2ca0f6399c8af387dba403000000000017a9146c8d8b04c6a1e664b1ec20ec932760760c97688e8700000000"
84
+ ]
85
+ ]
@@ -0,0 +1,170 @@
1
+ 'use strict'
2
+
3
+ var ECIES = require('../../lib/ecies/bitcore-ecies')
4
+
5
+ var should = require('chai').should()
6
+ var bsv = require('../../')
7
+ var PrivateKey = bsv.PrivateKey
8
+
9
+ var aliceKey = new PrivateKey('L1Ejc5dAigm5XrM3mNptMEsNnHzS7s51YxU7J61ewGshZTKkbmzJ')
10
+ var bobKey = new PrivateKey('KxfxrUXSMjJQcb3JgnaaA6MqsrKQ1nBSxvhuigdKRyFiEm6BZDgG')
11
+
12
+ describe('Bitcore ECIES', function () {
13
+ it('should exist', function () {
14
+ should.exist(ECIES)
15
+ })
16
+
17
+ it('constructor', function () {
18
+ (typeof ECIES).should.equal('function')
19
+ })
20
+
21
+ it('constructs an instance', function () {
22
+ var ecies = new ECIES();
23
+ (ecies instanceof ECIES).should.equal(true)
24
+ })
25
+
26
+ it('doesnt require the "new" keyword', function () {
27
+ var ecies = ECIES();
28
+ (ecies instanceof ECIES).should.equal(true)
29
+ })
30
+
31
+ it('privateKey fails with no argument', function () {
32
+ var ecies = ECIES()
33
+ var fail = function () {
34
+ ecies.privateKey()
35
+ }
36
+ fail.should.throw('no private key provided')
37
+ })
38
+
39
+ it('chainable function', function () {
40
+ var ecies = ECIES()
41
+ .privateKey(aliceKey)
42
+ .publicKey(bobKey.publicKey);
43
+
44
+ (ecies instanceof ECIES).should.equal(true)
45
+ })
46
+
47
+ var alice = ECIES()
48
+ .privateKey(aliceKey)
49
+ .publicKey(bobKey.publicKey)
50
+
51
+ var bob = ECIES()
52
+ .privateKey(bobKey)
53
+ .publicKey(aliceKey.publicKey)
54
+
55
+ var message = 'attack at dawn'
56
+ var encrypted = '0339e504d6492b082da96e11e8f039796b06cd4855c101e2492a6f10f3e056a9e712c732611c6917ab5c57a1926973bc44a1586e94a783f81d05ce72518d9b0a80e2e13c7ff7d1306583f9cc7a48def5b37fbf2d5f294f128472a6e9c78dede5f5'
57
+ var encBuf = Buffer.from(encrypted, 'hex')
58
+
59
+ it('correctly encrypts a message', function () {
60
+ var ciphertext = alice.encrypt(message)
61
+ Buffer.isBuffer(ciphertext).should.equal(true)
62
+ ciphertext.toString('hex').should.equal(encrypted)
63
+ })
64
+
65
+ it('correctly decrypts a message', function () {
66
+ var decrypted = bob
67
+ .decrypt(encBuf)
68
+ .toString()
69
+ decrypted.should.equal(message)
70
+ })
71
+
72
+ it('retrieves senders publickey from the encypted buffer', function () {
73
+ var bob2 = ECIES().privateKey(bobKey)
74
+ var decrypted = bob2.decrypt(encBuf).toString()
75
+ bob2._publicKey.toDER().should.deep.equal(aliceKey.publicKey.toDER())
76
+ decrypted.should.equal(message)
77
+ })
78
+
79
+ it('roundtrips', function () {
80
+ var secret = 'some secret message!!!'
81
+ var encrypted = alice.encrypt(secret)
82
+ var decrypted = bob
83
+ .decrypt(encrypted)
84
+ .toString()
85
+ decrypted.should.equal(secret)
86
+ })
87
+
88
+ it('roundtrips (no public key)', function () {
89
+ alice.opts.noKey = true
90
+ bob.opts.noKey = true
91
+ var secret = 'some secret message!!!'
92
+ var encrypted = alice.encrypt(secret)
93
+ var decrypted = bob
94
+ .decrypt(encrypted)
95
+ .toString()
96
+ decrypted.should.equal(secret)
97
+ })
98
+
99
+ it('roundtrips (short tag)', function () {
100
+ alice.opts.shortTag = true
101
+ bob.opts.shortTag = true
102
+ var secret = 'some secret message!!!'
103
+ var encrypted = alice.encrypt(secret)
104
+ var decrypted = bob
105
+ .decrypt(encrypted)
106
+ .toString()
107
+ decrypted.should.equal(secret)
108
+ })
109
+
110
+ it('roundtrips (no public key & short tag)', function () {
111
+ alice.opts.noKey = true
112
+ alice.opts.shortTag = true
113
+ bob.opts.noKey = true
114
+ bob.opts.shortTag = true
115
+ var secret = 'some secret message!!!'
116
+ var encrypted = alice.encrypt(secret)
117
+ var decrypted = bob
118
+ .decrypt(encrypted)
119
+ .toString()
120
+ decrypted.should.equal(secret)
121
+ })
122
+
123
+ it('correctly fails if trying to decrypt a bad message', function () {
124
+ var encrypted = Buffer.from(encBuf)
125
+ encrypted[encrypted.length - 1] = 2;
126
+ (function () {
127
+ return bob.decrypt(encrypted)
128
+ }).should.throw('Invalid checksum')
129
+ })
130
+
131
+ it('decrypting uncompressed keys', function () {
132
+ var secret = 'test'
133
+
134
+ // test uncompressed
135
+ var alicePrivateKey = bsv.PrivateKey.fromObject({
136
+ bn: '1fa76f9c799ca3a51e2c7c901d3ba8e24f6d870beccf8df56faf30120b38f360',
137
+ compressed: false,
138
+ network: 'livenet'
139
+ })
140
+ var alicePublicKey = bsv.PublicKey.fromPrivateKey(alicePrivateKey) // alicePrivateKey.publicKey
141
+ alicePrivateKey.compressed.should.equal(false)
142
+
143
+ var cypher1 = ECIES().privateKey(alicePrivateKey).publicKey(alicePublicKey)
144
+ var encrypted = cypher1.encrypt(secret)
145
+
146
+ var cypher2 = ECIES().privateKey(alicePrivateKey).publicKey(alicePublicKey)
147
+ var decrypted = cypher2.decrypt(encrypted)
148
+ secret.should.equal(decrypted.toString())
149
+ })
150
+
151
+ it('decrypting compressed keys', function () {
152
+ var secret = 'test'
153
+
154
+ // test compressed
155
+ var alicePrivateKey = bsv.PrivateKey.fromObject({
156
+ bn: '1fa76f9c799ca3a51e2c7c901d3ba8e24f6d870beccf8df56faf30120b38f360',
157
+ compressed: true,
158
+ network: 'livenet'
159
+ })
160
+ var alicePublicKey = bsv.PublicKey.fromPrivateKey(alicePrivateKey) // alicePrivateKey.publicKey
161
+ alicePrivateKey.compressed.should.equal(true)
162
+
163
+ var cypher1 = ECIES().privateKey(alicePrivateKey).publicKey(alicePublicKey)
164
+ var encrypted = cypher1.encrypt(secret)
165
+
166
+ var cypher2 = ECIES().privateKey(alicePrivateKey).publicKey(alicePublicKey)
167
+ var decrypted = cypher2.decrypt(encrypted)
168
+ secret.should.equal(decrypted.toString())
169
+ })
170
+ })
@@ -0,0 +1,206 @@
1
+ 'use strict'
2
+
3
+ var ECIES = require('../../lib/ecies/electrum-ecies')
4
+
5
+ var should = require('chai').should()
6
+ var bsv = require('../../')
7
+ var PrivateKey = bsv.PrivateKey
8
+
9
+ var aliceKey = new PrivateKey('L1Ejc5dAigm5XrM3mNptMEsNnHzS7s51YxU7J61ewGshZTKkbmzJ')
10
+ var bobKey = new PrivateKey('KxfxrUXSMjJQcb3JgnaaA6MqsrKQ1nBSxvhuigdKRyFiEm6BZDgG')
11
+
12
+ describe('ECIES', function () {
13
+ it('constructor', function () {
14
+ (typeof ECIES).should.equal('function')
15
+ })
16
+
17
+ it('constructs an instance', function () {
18
+ var ecies = new ECIES();
19
+ (ecies instanceof ECIES).should.equal(true)
20
+ })
21
+
22
+ it('doesnt require the "new" keyword', function () {
23
+ var ecies = ECIES();
24
+ (ecies instanceof ECIES).should.equal(true)
25
+ })
26
+
27
+ it('use ephemeral privateKey if privateKey is not set', function () {
28
+ var ecies = ECIES()
29
+ var ephemeralKey = ecies._privateKey;
30
+ (ephemeralKey instanceof bsv.PrivateKey).should.equal(true)
31
+ })
32
+
33
+ it('chainable function', function () {
34
+ var ecies = ECIES()
35
+ .privateKey(aliceKey)
36
+ .publicKey(bobKey.publicKey);
37
+
38
+ (ecies instanceof ECIES).should.equal(true)
39
+ })
40
+
41
+ it('should do these test vectors correctly', function () {
42
+ let message = Buffer.from('this is my test message')
43
+
44
+ let alice = ECIES()
45
+ .privateKey(aliceKey)
46
+ .publicKey(bobKey.publicKey)
47
+ alice.decrypt(Buffer.from('QklFMQOGFyMXLo9Qv047K3BYJhmnJgt58EC8skYP/R2QU/U0yXXHOt6L3tKmrXho6yj6phfoiMkBOhUldRPnEI4fSZXbiaH4FsxKIOOvzolIFVAS0FplUmib2HnlAM1yP/iiPsU=', 'base64')).toString().should.equal(message.toString())
48
+
49
+ let bob = ECIES()
50
+ .privateKey(bobKey)
51
+ .publicKey(aliceKey.publicKey)
52
+ bob.decrypt(Buffer.from('QklFMQM55QTWSSsILaluEejwOXlrBs1IVcEB4kkqbxDz4Fap53XHOt6L3tKmrXho6yj6phfoiMkBOhUldRPnEI4fSZXbvZJHgyAzxA6SoujduvJXv+A9ri3po9veilrmc8p6dwo=', 'base64')).toString().should.equal(message.toString())
53
+ })
54
+
55
+ var alice = ECIES()
56
+ .privateKey(aliceKey)
57
+ .publicKey(bobKey.publicKey)
58
+
59
+ var bob = ECIES()
60
+ .privateKey(bobKey)
61
+ .publicKey(aliceKey.publicKey)
62
+
63
+ var aliceReloaded = ECIES()
64
+ .privateKey(aliceKey)
65
+ .publicKey(bobKey.publicKey)
66
+
67
+ var message = 'attack at dawn'
68
+ var encrypted = 'QklFMQM55QTWSSsILaluEejwOXlrBs1IVcEB4kkqbxDz4Fap56+ajq0hzmnaQJXwUMZ/DUNgEx9i2TIhCA1mpBFIfxWZy+sH6H+sqqfX3sPHsGu0ug=='
69
+ var encBuf = Buffer.from(encrypted, 'base64')
70
+
71
+ it('correctly encrypts a message', function () {
72
+ var ciphertext = alice.encrypt(message)
73
+ Buffer.isBuffer(ciphertext).should.equal(true)
74
+ ciphertext.toString('base64').should.equal(encrypted)
75
+ })
76
+
77
+ it('correctly decrypts a message', function () {
78
+ var decrypted = bob
79
+ .decrypt(encBuf)
80
+ .toString()
81
+ decrypted.should.equal(message)
82
+ })
83
+
84
+ it('correctly recovers a message', function () {
85
+ var decrypted = aliceReloaded
86
+ .decrypt(encBuf)
87
+ .toString()
88
+ decrypted.should.equal(message)
89
+ })
90
+
91
+ it('retrieves senders publickey from the encypted buffer', function () {
92
+ var bob2 = ECIES().privateKey(bobKey)
93
+ var decrypted = bob2.decrypt(encBuf).toString()
94
+ bob2._publicKey.toDER().should.deep.equal(aliceKey.publicKey.toDER())
95
+ decrypted.should.equal(message)
96
+ })
97
+
98
+ var message1 = 'This is message from first sender'
99
+ var message2 = 'This is message from second sender'
100
+
101
+ it('decrypt messages from different senders', function () {
102
+ var sender1 = ECIES().publicKey(bobKey.publicKey)
103
+ var sender2 = ECIES().publicKey(bobKey.publicKey)
104
+ var bob2 = ECIES().privateKey(bobKey)
105
+ var decrypted1 = bob2.decrypt(sender1.encrypt(message1)).toString()
106
+ var decrypted2 = bob2.decrypt(sender2.encrypt(message2)).toString()
107
+ decrypted1.should.equal(message1)
108
+ decrypted2.should.equal(message2)
109
+ })
110
+
111
+ it('roundtrips', function () {
112
+ var secret = 'some secret message!!!'
113
+ var encrypted = alice.encrypt(secret)
114
+ var decrypted = bob
115
+ .decrypt(encrypted)
116
+ .toString()
117
+ decrypted.should.equal(secret)
118
+ })
119
+
120
+ it('roundtrips (no public key)', function () {
121
+ alice.opts.noKey = true
122
+ bob.opts.noKey = true
123
+ var secret = 'some secret message!!!'
124
+ var encrypted = alice.encrypt(secret)
125
+ var decrypted = bob
126
+ .decrypt(encrypted)
127
+ .toString()
128
+ decrypted.should.equal(secret)
129
+ })
130
+
131
+ it('roundtrips (short tag)', function () {
132
+ alice.opts.shortTag = true
133
+ bob.opts.shortTag = true
134
+ var secret = 'some secret message!!!'
135
+ var encrypted = alice.encrypt(secret)
136
+ var decrypted = bob
137
+ .decrypt(encrypted)
138
+ .toString()
139
+ decrypted.should.equal(secret)
140
+ })
141
+
142
+ it('roundtrips (no public key & short tag)', function () {
143
+ alice.opts.noKey = true
144
+ alice.opts.shortTag = true
145
+ bob.opts.noKey = true
146
+ bob.opts.shortTag = true
147
+ var secret = 'some secret message!!!'
148
+ var encrypted = alice.encrypt(secret)
149
+ var decrypted = bob
150
+ .decrypt(encrypted)
151
+ .toString()
152
+ decrypted.should.equal(secret)
153
+ })
154
+
155
+ it('errors', function () {
156
+ should.exist(bsv.errors.ECIES)
157
+ })
158
+
159
+ it('correctly fails if trying to decrypt a bad message', function () {
160
+ var encrypted = Buffer.from(encBuf)
161
+ encrypted[encrypted.length - 1] = 2;
162
+ (function () {
163
+ return bob.decrypt(encrypted)
164
+ }).should.throw('Invalid checksum')
165
+ })
166
+
167
+ it('decrypting uncompressed keys', function () {
168
+ var secret = 'test'
169
+
170
+ // test uncompressed
171
+ var alicePrivateKey = bsv.PrivateKey.fromObject({
172
+ bn: '1fa76f9c799ca3a51e2c7c901d3ba8e24f6d870beccf8df56faf30120b38f360',
173
+ compressed: false,
174
+ network: 'livenet'
175
+ })
176
+ var alicePublicKey = bsv.PublicKey.fromPrivateKey(alicePrivateKey) // alicePrivateKey.publicKey
177
+ alicePrivateKey.compressed.should.equal(false)
178
+
179
+ var cypher1 = ECIES().privateKey(alicePrivateKey).publicKey(alicePublicKey)
180
+ var encrypted = cypher1.encrypt(secret)
181
+
182
+ var cypher2 = ECIES().privateKey(alicePrivateKey).publicKey(alicePublicKey)
183
+ var decrypted = cypher2.decrypt(encrypted)
184
+ secret.should.equal(decrypted.toString())
185
+ })
186
+
187
+ it('decrypting compressed keys', function () {
188
+ var secret = 'test'
189
+
190
+ // test compressed
191
+ var alicePrivateKey = bsv.PrivateKey.fromObject({
192
+ bn: '1fa76f9c799ca3a51e2c7c901d3ba8e24f6d870beccf8df56faf30120b38f360',
193
+ compressed: true,
194
+ network: 'livenet'
195
+ })
196
+ var alicePublicKey = bsv.PublicKey.fromPrivateKey(alicePrivateKey) // alicePrivateKey.publicKey
197
+ alicePrivateKey.compressed.should.equal(true)
198
+
199
+ var cypher1 = ECIES().privateKey(alicePrivateKey).publicKey(alicePublicKey)
200
+ var encrypted = cypher1.encrypt(secret)
201
+
202
+ var cypher2 = ECIES().privateKey(alicePrivateKey).publicKey(alicePublicKey)
203
+ var decrypted = cypher2.decrypt(encrypted)
204
+ secret.should.equal(decrypted.toString())
205
+ })
206
+ })
@@ -0,0 +1,131 @@
1
+ 'use strict'
2
+
3
+ var should = require('chai').should()
4
+ var bsv = require('../..')
5
+ var buffer = require('buffer')
6
+ var Base58 = bsv.encoding.Base58
7
+
8
+ describe('Base58', function () {
9
+ var buf = Buffer.from([0, 1, 2, 3, 253, 254, 255])
10
+ var enc = '1W7N4RuG'
11
+
12
+ it('should make an instance with "new"', function () {
13
+ var b58 = new Base58()
14
+ should.exist(b58)
15
+ })
16
+
17
+ it('validates characters with no false negatives', function () {
18
+ Base58.validCharacters(
19
+ '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
20
+ ).should.equal(true)
21
+ })
22
+ it('validates characters from buffer', function () {
23
+ Base58.validCharacters(
24
+ buffer.Buffer.from('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz')
25
+ ).should.equal(true)
26
+ })
27
+
28
+ it('some characters are invalid (no false positives)', function () {
29
+ Base58.validCharacters('!@#%^$&*()\\').should.equal(false)
30
+ })
31
+
32
+ it('should make an instance without "new"', function () {
33
+ var b58 = Base58()
34
+ should.exist(b58)
35
+ })
36
+
37
+ it('should allow this handy syntax', function () {
38
+ Base58(buf).toString().should.equal(enc)
39
+ Base58(enc).toBuffer().toString('hex').should.equal(buf.toString('hex'))
40
+ })
41
+
42
+ describe('#set', function () {
43
+ it('should set a blank buffer', function () {
44
+ Base58().set({
45
+ buf: buffer.Buffer.from([])
46
+ })
47
+ })
48
+ })
49
+
50
+ describe('@encode', function () {
51
+ it('should encode the buffer accurately', function () {
52
+ Base58.encode(buf).should.equal(enc)
53
+ })
54
+
55
+ it('should throw an error when the Input is not a buffer', function () {
56
+ (function () {
57
+ Base58.encode('string')
58
+ }).should.throw('Input should be a buffer')
59
+ })
60
+ })
61
+
62
+ describe('@decode', function () {
63
+ it('should decode this encoded value correctly', function () {
64
+ Base58.decode(enc).toString('hex').should.equal(buf.toString('hex'))
65
+ })
66
+
67
+ it('should throw an error when Input is not a string', function () {
68
+ (function () {
69
+ Base58.decode(5)
70
+ }).should.throw('Input should be a string')
71
+ })
72
+ })
73
+
74
+ describe('#fromBuffer', function () {
75
+ it('should not fail', function () {
76
+ should.exist(Base58().fromBuffer(buf))
77
+ })
78
+
79
+ it('should set buffer', function () {
80
+ var b58 = Base58().fromBuffer(buf)
81
+ b58.buf.toString('hex').should.equal(buf.toString('hex'))
82
+ })
83
+ })
84
+
85
+ describe('@fromBuffer', function () {
86
+ it('should set buffer', function () {
87
+ var b58 = Base58.fromBuffer(buf)
88
+ b58.toHex().should.equal(buf.toString('hex'))
89
+ })
90
+ })
91
+
92
+ describe('@fromHex', function () {
93
+ it('should set buffer', function () {
94
+ var b58 = Base58.fromHex(buf.toString('hex'))
95
+ b58.buf.toString('hex').should.equal(buf.toString('hex'))
96
+ })
97
+ })
98
+
99
+ describe('#fromString', function () {
100
+ it('should convert this known string to a buffer', function () {
101
+ Base58().fromString(enc).toBuffer().toString('hex').should.equal(buf.toString('hex'))
102
+ })
103
+ })
104
+
105
+ describe('#fromString', function () {
106
+ it('should convert this known string to a buffer', function () {
107
+ Base58.fromString(enc).toBuffer().toString('hex').should.equal(buf.toString('hex'))
108
+ })
109
+ })
110
+
111
+ describe('#toBuffer', function () {
112
+ it('should return the buffer', function () {
113
+ var b58 = Base58(buf)
114
+ b58.buf.toString('hex').should.equal(buf.toString('hex'))
115
+ })
116
+ })
117
+
118
+ describe('#toHex', function () {
119
+ it('should return the hex', function () {
120
+ var b58 = Base58(buf)
121
+ b58.toHex().should.equal(buf.toString('hex'))
122
+ })
123
+ })
124
+
125
+ describe('#toString', function () {
126
+ it('should return the buffer', function () {
127
+ var b58 = Base58(buf)
128
+ b58.toString().should.equal(enc)
129
+ })
130
+ })
131
+ })