bsv2 2.0.24 → 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.
Files changed (170) hide show
  1. package/dist/bsv2.mjs +36061 -4
  2. package/package.json +35 -39
  3. package/.babelrc.test +0 -10
  4. package/.editorconfig +0 -4
  5. package/.env.defaults +0 -1
  6. package/.env.schema +0 -1
  7. package/.husky/pre-commit +0 -1
  8. package/.mocharc.yml +0 -4
  9. package/.nvmrc +0 -1
  10. package/.travis.yml +0 -7
  11. package/benchmark.js +0 -50
  12. package/bin/bsv.js +0 -11
  13. package/bin/network.js +0 -92
  14. package/build/index.html +0 -20
  15. package/dist/bsv2.bundle.js +0 -8
  16. package/dist/bsv2.cjs.js +0 -2
  17. package/dist/bsv2.cjs.js.LICENSE.txt +0 -1
  18. package/dist/bsv2.module.js +0 -2
  19. package/entry.js +0 -82
  20. package/index.js +0 -102
  21. package/karma.conf.js +0 -78
  22. package/lib/ach.js +0 -59
  23. package/lib/address.js +0 -264
  24. package/lib/aes.js +0 -57
  25. package/lib/aescbc.js +0 -38
  26. package/lib/base-58-check.js +0 -83
  27. package/lib/base-58.js +0 -59
  28. package/lib/bip-32.js +0 -472
  29. package/lib/bip-39-en-wordlist.js +0 -2054
  30. package/lib/bip-39-en.js +0 -10
  31. package/lib/bip-39-es-wordlist.js +0 -2055
  32. package/lib/bip-39-es.js +0 -10
  33. package/lib/bip-39-jp-wordlist.js +0 -2053
  34. package/lib/bip-39-jp.js +0 -10
  35. package/lib/bip-39-words.js +0 -5
  36. package/lib/bip-39.js +0 -285
  37. package/lib/block-header.js +0 -79
  38. package/lib/block.js +0 -132
  39. package/lib/bn.js +0 -467
  40. package/lib/br.js +0 -172
  41. package/lib/bsm.js +0 -127
  42. package/lib/bw.js +0 -188
  43. package/lib/cbc.js +0 -140
  44. package/lib/cmp.js +0 -37
  45. package/lib/config.js +0 -50
  46. package/lib/constants.js +0 -150
  47. package/lib/ecdsa.js +0 -495
  48. package/lib/ecies.js +0 -151
  49. package/lib/hash-cache.js +0 -41
  50. package/lib/hash.js +0 -191
  51. package/lib/interp.js +0 -2013
  52. package/lib/inv.js +0 -49
  53. package/lib/key-pair.js +0 -133
  54. package/lib/mainnet-address.js +0 -5
  55. package/lib/mainnet-bip-32.js +0 -5
  56. package/lib/mainnet-key-pair.js +0 -5
  57. package/lib/mainnet-priv-key.js +0 -5
  58. package/lib/merkle.js +0 -92
  59. package/lib/msg.js +0 -184
  60. package/lib/op-code.js +0 -398
  61. package/lib/point.js +0 -134
  62. package/lib/priv-key.js +0 -157
  63. package/lib/pub-key.js +0 -230
  64. package/lib/random.js +0 -28
  65. package/lib/script.js +0 -826
  66. package/lib/sig-operations.js +0 -120
  67. package/lib/sig.js +0 -399
  68. package/lib/struct.js +0 -389
  69. package/lib/testnet-address.js +0 -5
  70. package/lib/testnet-bip-32.js +0 -5
  71. package/lib/testnet-key-pair.js +0 -5
  72. package/lib/testnet-priv-key.js +0 -5
  73. package/lib/tx-builder.js +0 -593
  74. package/lib/tx-in.js +0 -172
  75. package/lib/tx-out-map.js +0 -57
  76. package/lib/tx-out.js +0 -73
  77. package/lib/tx-verifier.js +0 -209
  78. package/lib/tx.js +0 -430
  79. package/lib/var-int.js +0 -73
  80. package/lib/version.js +0 -92
  81. package/lib/workers-result.js +0 -65
  82. package/lib/workers.js +0 -77
  83. package/load-babel.js +0 -3
  84. package/mtest/test.html +0 -15
  85. package/mtest/test.js +0 -2
  86. package/mtest/test.mjs +0 -2
  87. package/test/ach.js +0 -172
  88. package/test/address.js +0 -464
  89. package/test/aes.js +0 -96
  90. package/test/aescbc.js +0 -65
  91. package/test/base-58-check.js +0 -128
  92. package/test/base-58.js +0 -113
  93. package/test/bip-32.js +0 -721
  94. package/test/bip-39.js +0 -282
  95. package/test/block-header.js +0 -152
  96. package/test/block.js +0 -243
  97. package/test/bn.js +0 -643
  98. package/test/br.js +0 -353
  99. package/test/bsm.js +0 -141
  100. package/test/bw.js +0 -292
  101. package/test/cbc.js +0 -335
  102. package/test/cmp.js +0 -56
  103. package/test/data/bip69.json +0 -215
  104. package/test/data/bitcoind/base58_keys_invalid.json +0 -152
  105. package/test/data/bitcoind/base58_keys_valid.json +0 -452
  106. package/test/data/bitcoind/blocks.json +0 -27
  107. package/test/data/bitcoind/script_tests.json +0 -2244
  108. package/test/data/bitcoind/sig_canonical.json +0 -7
  109. package/test/data/bitcoind/sig_noncanonical.json +0 -22
  110. package/test/data/bitcoind/tx_invalid.json +0 -177
  111. package/test/data/bitcoind/tx_valid.json +0 -224
  112. package/test/data/blk86756-testnet.dat +0 -0
  113. package/test/data/blk86756-testnet.js +0 -12
  114. package/test/data/blk86756-testnet.json +0 -684
  115. package/test/data/ecdsa.json +0 -230
  116. package/test/data/messages.json +0 -22
  117. package/test/data/sighash.json +0 -1004
  118. package/test/data/tx_creation.json +0 -85
  119. package/test/ecdsa.js +0 -748
  120. package/test/ecies.js +0 -238
  121. package/test/global.js +0 -13
  122. package/test/hash-cache.js +0 -52
  123. package/test/hash.js +0 -354
  124. package/test/index.js +0 -10
  125. package/test/interp.js +0 -512
  126. package/test/inv.js +0 -65
  127. package/test/key-pair.js +0 -243
  128. package/test/merkle.js +0 -179
  129. package/test/msg.js +0 -295
  130. package/test/op-code.js +0 -79
  131. package/test/point.js +0 -375
  132. package/test/priv-key.js +0 -264
  133. package/test/pub-key.js +0 -549
  134. package/test/random.js +0 -31
  135. package/test/script.js +0 -1340
  136. package/test/sig-operations.js +0 -149
  137. package/test/sig.js +0 -662
  138. package/test/struct.js +0 -552
  139. package/test/tx-builder.js +0 -2104
  140. package/test/tx-in.js +0 -198
  141. package/test/tx-out-map.js +0 -104
  142. package/test/tx-out.js +0 -162
  143. package/test/tx-verifier.js +0 -246
  144. package/test/tx.js +0 -539
  145. package/test/var-int.js +0 -132
  146. package/test/vectors/aes.json +0 -10392
  147. package/test/vectors/aescbc.json +0 -620
  148. package/test/vectors/bip39.json +0 -502
  149. package/test/vectors/bip69.json +0 -215
  150. package/test/vectors/bitcoin-abc/script_tests.json +0 -48
  151. package/test/vectors/bitcoin-abc/sighash.json +0 -7004
  152. package/test/vectors/bitcoin-sv/script_tests.json +0 -248
  153. package/test/vectors/bitcoind/script_invalid.json +0 -1151
  154. package/test/vectors/bitcoind/script_valid.json +0 -1462
  155. package/test/vectors/bitcoind/sighash.json +0 -3503
  156. package/test/vectors/bitcoind/tx_invalid.json +0 -883
  157. package/test/vectors/bitcoind/tx_valid.json +0 -1150
  158. package/test/vectors/coolest-tx-ever-sent.json +0 -8
  159. package/test/vectors/ecdsa.json +0 -300
  160. package/test/vectors/hash.json +0 -6133
  161. package/test/vectors/kdf.json +0 -47
  162. package/test/vectors/largesttx.json +0 -5
  163. package/test/vectors/largesttxblock.json +0 -6
  164. package/test/vectors/sig.json +0 -79
  165. package/test/vectors/sighash-single-bug.json +0 -8
  166. package/test/version.js +0 -81
  167. package/test/workers-result.js +0 -89
  168. package/test/workers.js +0 -91
  169. package/test-index.js +0 -3
  170. package/webpack.config.js +0 -115
package/entry.js DELETED
@@ -1,82 +0,0 @@
1
- /**
2
- * bsv
3
- * ===
4
- *
5
- * entry.js is the entry point for a the js bundlers.
6
- * Webpack and microbundlers, both start use this file as
7
- * the entry point to bundle the entire library.
8
- */
9
- import aes from 'aes'
10
- import bnjs from 'bn.js'
11
- import bs58 from 'bs58'
12
-
13
- import elliptic from 'bitcoin-elliptic'
14
- import hashjs from 'hash.js'
15
- import pbkdf2 from 'pbkdf2'
16
- import { Buffer } from 'buffer';
17
-
18
- // version string.
19
- import pkgInfo from './package.json' assert {type: 'json'};
20
-
21
- // Dependencies, subject to change.
22
- const deps = {
23
- aes,
24
- bnjs,
25
- bs58,
26
- elliptic,
27
- hashjs,
28
- pbkdf2,
29
- Buffer
30
- }
31
-
32
- export { deps }
33
- const version = pkgInfo.version
34
- export { version }
35
-
36
- // Main bitcoin library - bitcoin protocols, standards, cryptography, and
37
- // utilities.
38
- export * from './lib/address'
39
-
40
- export * from './lib/bip-32'
41
- export * from './lib/bip-39'
42
- export * from './lib/bip-39-words'
43
- export * from './lib/bn'
44
- export * from './lib/br'
45
- export * from './lib/bsm'
46
- export * from './lib/bw'
47
- export * from './lib/base-58'
48
- export * from './lib/base-58-check'
49
- export * from './lib/block'
50
- export * from './lib/block-header'
51
- export * from './lib/constants'
52
- export * from './lib/ecdsa'
53
- export * from './lib/hash'
54
- export * from './lib/interp'
55
- export * from './lib/key-pair'
56
- export * from './lib/op-code'
57
- export * from './lib/point'
58
- export * from './lib/priv-key'
59
- export * from './lib/pub-key'
60
- export * from './lib/random'
61
- export * from './lib/script'
62
- export * from './lib/sig'
63
- export * from './lib/sig-operations'
64
- export * from './lib/struct'
65
- export * from './lib/tx'
66
- export * from './lib/tx-builder'
67
- export * from './lib/tx-in'
68
- export * from './lib/tx-out'
69
- export * from './lib/tx-out-map'
70
- export * from './lib/tx-verifier'
71
- export * from './lib/var-int'
72
- export * from './lib/workers'
73
- export * from './lib/workers-result'
74
- export * from './lib/cmp'
75
-
76
- // Encryption tools. Some bitcoin standards use Aes encryption, so that's why
77
- // these are available.
78
- export * from './lib/ach'
79
- export * from './lib/aes'
80
- export * from './lib/aescbc'
81
- export * from './lib/cbc'
82
- export * from './lib/ecies'
package/index.js DELETED
@@ -1,102 +0,0 @@
1
- /**
2
- * bsv
3
- * ===
4
- *
5
- * index.js is an example of how to build a bundle with bsv. This
6
- * bundle includes the entire library, which uses the default configuration
7
- * (which is the same as Mainnet) and can be overridden. It also includes
8
- * Mainnet and Testnet configuration which are accessible even if you override
9
- * the defaults. It is not necessary to use Yours Bitcoin this way, since you
10
- * probably do not use every component, and therefore do not need to include
11
- * every component into your project. You can simply directly require the
12
- * elements of the library you need, and, if your project is browser-based,
13
- * browserify your project. For instance:
14
- * const Address = require('bsv/lib/address').
15
- */
16
- 'use strict'
17
- if (!global._babelPolyfill) {
18
- require('babel-polyfill')
19
- }
20
- require('./lib/config')
21
-
22
- const bsv = module.exports
23
-
24
- bsv.version = require('./package').version
25
-
26
- // Main bitcoin library - bitcoin protocols, standards, cryptography, and
27
- // utilities.
28
- bsv.Address = require('./lib/address')
29
- bsv.Bip32 = require('./lib/bip-32')
30
- bsv.Bip39 = require('./lib/bip-39')
31
- bsv.Bn = require('./lib/bn')
32
- bsv.Br = require('./lib/br')
33
- bsv.Bsm = require('./lib/bsm')
34
- bsv.Bw = require('./lib/bw')
35
- bsv.Base58 = require('./lib/base-58')
36
- bsv.Base58Check = require('./lib/base-58-check')
37
- bsv.Block = require('./lib/block')
38
- bsv.BlockHeader = require('./lib/block-header')
39
- bsv.Constants = require('./lib/constants')
40
- bsv.Ecdsa = require('./lib/ecdsa')
41
- bsv.Hash = require('./lib/hash')
42
- bsv.Interp = require('./lib/interp')
43
- bsv.KeyPair = require('./lib/key-pair')
44
- bsv.OpCode = require('./lib/op-code')
45
- bsv.Point = require('./lib/point')
46
- bsv.PrivKey = require('./lib/priv-key')
47
- bsv.PubKey = require('./lib/pub-key')
48
- bsv.Random = require('./lib/random')
49
- bsv.Script = require('./lib/script')
50
- bsv.Sig = require('./lib/sig')
51
- bsv.SigOperations = require('./lib/sig-operations')
52
- bsv.Struct = require('./lib/struct')
53
- bsv.Tx = require('./lib/tx')
54
- bsv.TxBuilder = require('./lib/tx-builder')
55
- bsv.TxIn = require('./lib/tx-in')
56
- bsv.TxOut = require('./lib/tx-out')
57
- bsv.TxOutMap = require('./lib/tx-out-map')
58
- bsv.TxVerifier = require('./lib/tx-verifier')
59
- bsv.VarInt = require('./lib/var-int')
60
- bsv.Workers = require('./lib/workers')
61
- bsv.WorkersResult = require('./lib/workers-result')
62
- bsv.cmp = require('./lib/cmp')
63
-
64
- // Encryption tools. Some bitcoin standards use Aes encryption, so that's why
65
- // these are available.
66
- bsv.Ach = require('./lib/ach')
67
- bsv.Aes = require('./lib/aes')
68
- bsv.Aescbc = require('./lib/aescbc')
69
- bsv.Cbc = require('./lib/cbc')
70
- bsv.Ecies = require('./lib/ecies')
71
-
72
- // Dependencies, subject to change.
73
- bsv.deps = {}
74
- bsv.deps.aes = require('aes')
75
- bsv.deps.bnjs = require('bn.js')
76
- bsv.deps.bs58 = require('bs58')
77
- bsv.deps.Buffer = Buffer
78
- bsv.deps.elliptic = require('bitcoin-elliptic')
79
- bsv.deps.hashjs = require('hash.js')
80
- bsv.deps.pbkdf2compat = require('pbkdf2-compat')
81
-
82
- // Mainnet classes for your convenience (in case default is not what you want).
83
- const Mainnet = {}
84
- Object.keys(bsv).forEach(function (key) {
85
- Mainnet[key] = bsv[key].Mainnet
86
- ? bsv[key].Mainnet
87
- : bsv[key]
88
- })
89
-
90
- // Testnet classes for your convenience (in case default is not what you want).
91
- const Testnet = {}
92
- Object.keys(bsv).forEach(function (key) {
93
- Testnet[key] = bsv[key].Testnet
94
- ? bsv[key].Testnet
95
- : bsv[key]
96
- })
97
-
98
- bsv.Mainnet = Mainnet
99
- bsv.Testnet = Testnet
100
-
101
- bsv.browser = process.browser
102
- bsv.env = process.env
package/karma.conf.js DELETED
@@ -1,78 +0,0 @@
1
- const webpack = require('webpack')
2
- // Karma configuration
3
- // Generated on Sat May 16 2020 20:12:10 GMT-0300 (Argentina Standard Time)
4
-
5
- module.exports = function (config) {
6
- config.set({
7
-
8
- // base path that will be used to resolve all patterns (eg. files, exclude)
9
- basePath: '',
10
-
11
- // frameworks to use
12
- // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
13
- frameworks: ['webpack', 'mocha'],
14
-
15
- // list of files / patterns to load in the browser
16
- files: [
17
- 'test/**/*.js'
18
- ],
19
-
20
- // list of files / patterns to exclude
21
- exclude: [
22
- ],
23
-
24
- // preprocess matching files before serving them to the browser
25
- // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
26
- preprocessors: {
27
- 'lib/**/*.js': [],
28
- 'test/**/*.js': ['webpack']
29
- },
30
-
31
- plugins: [
32
- require('karma-chrome-launcher'),
33
- require('karma-firefox-launcher'),
34
- require('karma-mocha'),
35
- require('karma-webpack')
36
- ],
37
-
38
- // test results reporter to use
39
- // possible values: 'dots', 'progress'
40
- // available reporters: https://npmjs.org/browse/keyword/karma-reporter
41
- reporters: ['progress'],
42
-
43
- // web server port
44
- port: 9876,
45
-
46
- // enable / disable colors in the output (reporters and logs)
47
- colors: true,
48
-
49
- // level of logging
50
- // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
51
- logLevel: config.LOG_INFO,
52
-
53
- // enable / disable watching file and executing tests whenever any file changes
54
- autoWatch: false,
55
-
56
- // start these browsers
57
- // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
58
- browsers: ['ChromeHeadless', 'FirefoxHeadless'],
59
-
60
- // Continuous Integration mode
61
- // if true, Karma captures browsers, runs the tests and exits
62
- singleRun: false,
63
-
64
- webpack: {
65
- mode: 'production',
66
- plugins: [
67
- new webpack.ProvidePlugin({
68
- process: 'process/browser',
69
- Buffer: ['buffer', 'Buffer']
70
- })
71
- ]
72
- },
73
-
74
- // Concurrency level
75
- // how many browser should be started simultaneous
76
- concurrency: Infinity
77
- })
78
- }
package/lib/ach.js DELETED
@@ -1,59 +0,0 @@
1
- /**
2
- * Ach (Aes+Cbc+Hmac) (experimental)
3
- * =================================
4
- *
5
- * An "encrypt-then-MAC" that uses Aes, Cbc and SHA256 Hmac. This is suitable
6
- * for general encryption of data.
7
- *
8
- * The encrypted data takes the form:
9
- * (256 bit hmac)(128 bit iv)(128+ bits Aes+Cbc encrypted message)
10
- */
11
- 'use strict'
12
-
13
- import { Aescbc } from './aescbc'
14
- import { Hash } from './hash'
15
- import { Random } from './random'
16
- import { Workers } from './workers'
17
- import { cmp } from './cmp'
18
-
19
- class Ach { }
20
-
21
- Ach.encrypt = function (messageBuf, cipherKeyBuf, ivBuf) {
22
- const encBuf = Aescbc.encrypt(messageBuf, cipherKeyBuf, ivBuf)
23
- const hmacbuf = Hash.sha256Hmac(encBuf, cipherKeyBuf)
24
- return Buffer.concat([hmacbuf, encBuf])
25
- }
26
-
27
- Ach.asyncEncrypt = async function (messageBuf, cipherKeyBuf, ivBuf) {
28
- if (!ivBuf) {
29
- ivBuf = Random.getRandomBuffer(128 / 8)
30
- }
31
- const args = [messageBuf, cipherKeyBuf, ivBuf]
32
- const workersResult = await Workers.asyncClassMethod(Ach, 'encrypt', args)
33
- return workersResult.resbuf
34
- }
35
-
36
- Ach.decrypt = function (encBuf, cipherKeyBuf) {
37
- if (encBuf.length < (256 + 128 + 128) / 8) {
38
- throw new Error(
39
- 'The encrypted data must be at least 256+128+128 bits, which is the length of the Hmac plus the iv plus the smallest encrypted data size'
40
- )
41
- }
42
- const hmacbuf = encBuf.slice(0, 256 / 8)
43
- encBuf = encBuf.slice(256 / 8, encBuf.length)
44
- const hmacbuf2 = Hash.sha256Hmac(encBuf, cipherKeyBuf)
45
- if (!cmp(hmacbuf, hmacbuf2)) {
46
- throw new Error(
47
- 'Message authentication failed - Hmacs are not equivalent'
48
- )
49
- }
50
- return Aescbc.decrypt(encBuf, cipherKeyBuf)
51
- }
52
-
53
- Ach.asyncDecrypt = async function (encBuf, cipherKeyBuf) {
54
- const args = [encBuf, cipherKeyBuf]
55
- const workersResult = await Workers.asyncClassMethod(Ach, 'decrypt', args)
56
- return workersResult.resbuf
57
- }
58
-
59
- export { Ach }
package/lib/address.js DELETED
@@ -1,264 +0,0 @@
1
- /**
2
- * Bitcoin Address
3
- * ===============
4
- *
5
- * A bitcoin address. Normal use cases:
6
- * const address = new Address().fromPubKey(pubKey)
7
- * const address = new Address().fromString(string)
8
- * const string = address.toString()
9
- * const script = address.toTxOutScript()
10
- * const isValid = Address.isValid(string)
11
- *
12
- * Can also do testnet:
13
- * const address = Address.Testnet()
14
- *
15
- * Note that an Address and an Addr are two completely different things. An
16
- * Address is what you send bitcoin to. An Addr is an ip address and port that
17
- * you connect to over the internet.
18
- */
19
- 'use strict'
20
-
21
- import { Base58Check } from './base-58-check'
22
- import { Constants } from './constants'
23
- import { Hash } from './hash'
24
- import { OpCode } from './op-code'
25
- import { PubKey } from './pub-key'
26
- import { PrivKey } from './priv-key'
27
- import { Script } from './script'
28
- import { Struct } from './struct'
29
- import { Workers } from './workers'
30
-
31
- class Address extends Struct {
32
- constructor (versionByteNum, hashBuf, constants = null) {
33
- super({ versionByteNum, hashBuf })
34
- constants = constants || Constants.Default.Address
35
- this.Constants = constants
36
- }
37
-
38
- fromBuffer (buf) {
39
- if (buf.length !== 1 + 20) {
40
- throw new Error('address buffers must be exactly 21 bytes')
41
- }
42
- if (
43
- buf[0] !== this.Constants.pubKeyHash
44
- ) {
45
- throw new Error('address: invalid versionByteNum byte')
46
- }
47
- this.versionByteNum = buf[0]
48
- this.hashBuf = buf.slice(1)
49
- return this
50
- }
51
-
52
- fromPubKeyHashBuf (hashBuf) {
53
- this.hashBuf = hashBuf
54
- this.versionByteNum = this.Constants.pubKeyHash
55
- return this
56
- }
57
-
58
- static fromPubKeyHashBuf (hashBuf) {
59
- return new this().fromPubKeyHashBuf(hashBuf)
60
- }
61
-
62
- fromPubKey (pubKey) {
63
- const hashBuf = Hash.sha256Ripemd160(pubKey.toBuffer())
64
- return this.fromPubKeyHashBuf(hashBuf)
65
- }
66
-
67
- static fromPubKey (pubKey) {
68
- return new this().fromPubKey(pubKey)
69
- }
70
-
71
- async asyncFromPubKey (pubKey) {
72
- const args = [pubKey]
73
- const workersResult = await Workers.asyncObjectMethod(
74
- this,
75
- 'fromPubKey',
76
- args
77
- )
78
- return this.fromFastBuffer(workersResult.resbuf)
79
- }
80
-
81
- static asyncFromPubKey (pubKey) {
82
- return new this().asyncFromPubKey(pubKey)
83
- }
84
-
85
- fromPrivKey (privKey) {
86
- const pubKey = new PubKey().fromPrivKey(privKey)
87
- const hashBuf = Hash.sha256Ripemd160(pubKey.toBuffer())
88
- return this.fromPubKeyHashBuf(hashBuf)
89
- }
90
-
91
- static fromPrivKey (privKey) {
92
- return new this().fromPrivKey(privKey)
93
- }
94
-
95
- async asyncFromPrivKey (privKey) {
96
- const args = [privKey]
97
- const workersResult = await Workers.asyncObjectMethod(
98
- this,
99
- 'fromPrivKey',
100
- args
101
- )
102
- return this.fromFastBuffer(workersResult.resbuf)
103
- }
104
-
105
- static asyncFromPrivKey (privKey) {
106
- return new this().fromPrivKey(privKey)
107
- }
108
-
109
- fromRandom () {
110
- const randomPrivKey = new PrivKey().fromRandom()
111
- return this.fromPrivKey(randomPrivKey)
112
- }
113
-
114
- static fromRandom () {
115
- return new this().fromRandom()
116
- }
117
-
118
- async asyncFromRandom () {
119
- const args = []
120
- const workersResult = await Workers.asyncObjectMethod(
121
- this,
122
- 'fromRandom',
123
- args
124
- )
125
- return this.fromFastBuffer(workersResult.resbuf)
126
- }
127
-
128
- static asyncFromRandom () {
129
- return new this().fromRandom()
130
- }
131
-
132
- fromString (str) {
133
- const buf = Base58Check.decode(str)
134
- return this.fromBuffer(buf)
135
- }
136
-
137
- async asyncFromString (str) {
138
- const args = [str]
139
- const workersResult = await Workers.asyncObjectMethod(
140
- this,
141
- 'fromString',
142
- args
143
- )
144
- return this.fromFastBuffer(workersResult.resbuf)
145
- }
146
-
147
- static asyncFromString (str) {
148
- return new this().asyncFromString(str)
149
- }
150
-
151
- static isValid (addrstr) {
152
- let address
153
- try {
154
- address = new Address().fromString(addrstr)
155
- } catch (e) {
156
- return false
157
- }
158
- return address.isValid()
159
- }
160
-
161
- isValid () {
162
- try {
163
- this.validate()
164
- return true
165
- } catch (e) {
166
- return false
167
- }
168
- }
169
-
170
- toTxOutScript () {
171
- const script = new Script()
172
- script.writeOpCode(OpCode.OP_DUP)
173
- script.writeOpCode(OpCode.OP_HASH160)
174
- script.writeBuffer(this.hashBuf)
175
- script.writeOpCode(OpCode.OP_EQUALVERIFY)
176
- script.writeOpCode(OpCode.OP_CHECKSIG)
177
-
178
- return script
179
- }
180
-
181
- fromTxInScript (script) {
182
- const pubKeyHashBuf = Hash.sha256Ripemd160(script.chunks[1].buf || Buffer.from('00'.repeat(32), 'hex'))
183
- return this.fromPubKeyHashBuf(pubKeyHashBuf)
184
- }
185
-
186
- static fromTxInScript (script) {
187
- return new this().fromTxInScript(script)
188
- }
189
-
190
- fromTxOutScript (script) {
191
- return this.fromPubKeyHashBuf(script.chunks[2].buf)
192
- }
193
-
194
- static fromTxOutScript (script) {
195
- return new this().fromTxOutScript(script)
196
- }
197
-
198
- toBuffer () {
199
- const versionByteBuf = Buffer.from([this.versionByteNum])
200
- const buf = Buffer.concat([versionByteBuf, this.hashBuf])
201
- return buf
202
- }
203
-
204
- toJSON () {
205
- const json = {}
206
- if (this.hashBuf) {
207
- json.hashBuf = this.hashBuf.toString('hex')
208
- }
209
- if (typeof this.versionByteNum !== 'undefined') {
210
- json.versionByteNum = this.versionByteNum
211
- }
212
- return json
213
- }
214
-
215
- fromJSON (json) {
216
- if (json.hashBuf) {
217
- this.hashBuf = Buffer.from(json.hashBuf, 'hex')
218
- }
219
- if (typeof json.versionByteNum !== 'undefined') {
220
- this.versionByteNum = json.versionByteNum
221
- }
222
- return this
223
- }
224
-
225
- toString () {
226
- return Base58Check.encode(this.toBuffer())
227
- }
228
-
229
- async asyncToString () {
230
- const args = []
231
- const workersResult = await Workers.asyncObjectMethod(
232
- this,
233
- 'toString',
234
- args
235
- )
236
- return JSON.parse(workersResult.resbuf.toString())
237
- }
238
-
239
- validate () {
240
- if (!Buffer.isBuffer(this.hashBuf) || this.hashBuf.length !== 20) {
241
- throw new Error('hashBuf must be a buffer of 20 bytes')
242
- }
243
- if (
244
- this.versionByteNum !== this.Constants.pubKeyHash
245
- ) {
246
- throw new Error('invalid versionByteNum')
247
- }
248
- return this
249
- }
250
- }
251
-
252
- Address.Mainnet = class extends Address {
253
- constructor (versionByteNum, hashBuf) {
254
- super(versionByteNum, hashBuf, Constants.Mainnet.Address)
255
- }
256
- }
257
-
258
- Address.Testnet = class extends Address {
259
- constructor (versionByteNum, hashBuf) {
260
- super(versionByteNum, hashBuf, Constants.Testnet.Address)
261
- }
262
- }
263
-
264
- export { Address }
package/lib/aes.js DELETED
@@ -1,57 +0,0 @@
1
- /**
2
- * Aes (experimental)
3
- * ==================
4
- *
5
- * Advanced Encryption Standard (Aes). This is a low-level tool for encrypting
6
- * or decrypting blocks of data. There is almost never a reason to use this -
7
- * don't use it unless you need to encrypt or decrypt individual blocks.
8
- */
9
- 'use strict'
10
-
11
- import _Aes from 'aes'
12
-
13
- class Aes { }
14
-
15
- Aes.encrypt = function (messageBuf, keyBuf) {
16
- const key = Aes.buf2Words(keyBuf)
17
- const message = Aes.buf2Words(messageBuf)
18
- const a = new _Aes(key)
19
- const enc = a.encrypt(message)
20
- const encBuf = Aes.words2Buf(enc)
21
- return encBuf
22
- }
23
-
24
- Aes.decrypt = function (encBuf, keyBuf) {
25
- const enc = Aes.buf2Words(encBuf)
26
- const key = Aes.buf2Words(keyBuf)
27
- const a = new _Aes(key)
28
- const message = a.decrypt(enc)
29
- const messageBuf = Aes.words2Buf(message)
30
- return messageBuf
31
- }
32
-
33
- Aes.buf2Words = function (buf) {
34
- if (buf.length % 4) {
35
- throw new Error('buf length must be a multiple of 4')
36
- }
37
-
38
- const words = []
39
-
40
- for (let i = 0; i < buf.length / 4; i++) {
41
- words.push(buf.readUInt32BE(i * 4))
42
- }
43
-
44
- return words
45
- }
46
-
47
- Aes.words2Buf = function (words) {
48
- const buf = Buffer.alloc(words.length * 4)
49
-
50
- for (let i = 0; i < words.length; i++) {
51
- buf.writeUInt32BE(words[i], i * 4)
52
- }
53
-
54
- return buf
55
- }
56
-
57
- export { Aes }
package/lib/aescbc.js DELETED
@@ -1,38 +0,0 @@
1
- /**
2
- * Aescbc (experimental)
3
- * =====================
4
- *
5
- * This is a convenience class for using Aes with Cbc. This is a low-level tool
6
- * that does not include authentication. You should only use this if you are
7
- * authenticating your data somehow else.
8
- */
9
- 'use strict'
10
-
11
- import { Aes } from './aes'
12
- import { Cbc } from './cbc'
13
- import { Random } from './random'
14
-
15
- class Aescbc { }
16
-
17
- Aescbc.encrypt = function (messageBuf, cipherKeyBuf, ivBuf, concatIvBuf = true) {
18
- ivBuf = ivBuf || Random.getRandomBuffer(128 / 8)
19
- const ctBuf = Cbc.encrypt(messageBuf, ivBuf, Aes, cipherKeyBuf)
20
- if (concatIvBuf) {
21
- return Buffer.concat([ivBuf, ctBuf])
22
- } else {
23
- return ctBuf
24
- }
25
- }
26
-
27
- Aescbc.decrypt = function (encBuf, cipherKeyBuf, ivBuf = false) {
28
- if (!ivBuf) {
29
- const ivBuf = encBuf.slice(0, 128 / 8)
30
- const ctBuf = encBuf.slice(128 / 8)
31
- return Cbc.decrypt(ctBuf, ivBuf, Aes, cipherKeyBuf)
32
- } else {
33
- const ctBuf = encBuf
34
- return Cbc.decrypt(ctBuf, ivBuf, Aes, cipherKeyBuf)
35
- }
36
- }
37
-
38
- export { Aescbc }