bsv2 2.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc.test +10 -0
- package/.editorconfig +4 -0
- package/.env.defaults +1 -0
- package/.env.schema +1 -0
- package/.mocharc.yml +4 -0
- package/.nvmrc +1 -0
- package/.travis.yml +7 -0
- package/LICENSE +39 -0
- package/README.md +53 -0
- package/benchmark.js +50 -0
- package/bin/bsv.js +11 -0
- package/bin/network.js +92 -0
- package/build/bsv.js +3 -0
- package/build/index.html +20 -0
- package/dist/bsv.bundle.js +3 -0
- package/dist/bsv.bundle.js.LICENSE.txt +10 -0
- package/dist/bsv.bundle.js.map +1 -0
- package/dist/bsv.cjs.js +3 -0
- package/dist/bsv.cjs.js.LICENSE.txt +1 -0
- package/dist/bsv.cjs.js.map +1 -0
- package/dist/bsv.min.js +2 -0
- package/dist/bsv.module.js +3 -0
- package/dist/bsv.module.js.LICENSE.txt +1 -0
- package/dist/bsv.module.js.map +1 -0
- package/doc/assets/anchor.js +350 -0
- package/doc/assets/bass-addons.css +12 -0
- package/doc/assets/bass.css +544 -0
- package/doc/assets/fonts/EOT/SourceCodePro-Bold.eot +0 -0
- package/doc/assets/fonts/EOT/SourceCodePro-Regular.eot +0 -0
- package/doc/assets/fonts/LICENSE.txt +93 -0
- package/doc/assets/fonts/OTF/SourceCodePro-Bold.otf +0 -0
- package/doc/assets/fonts/OTF/SourceCodePro-Regular.otf +0 -0
- package/doc/assets/fonts/TTF/SourceCodePro-Bold.ttf +0 -0
- package/doc/assets/fonts/TTF/SourceCodePro-Regular.ttf +0 -0
- package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Bold.otf.woff +0 -0
- package/doc/assets/fonts/WOFF/OTF/SourceCodePro-Regular.otf.woff +0 -0
- package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Bold.ttf.woff +0 -0
- package/doc/assets/fonts/WOFF/TTF/SourceCodePro-Regular.ttf.woff +0 -0
- package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Bold.otf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/OTF/SourceCodePro-Regular.otf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Bold.ttf.woff2 +0 -0
- package/doc/assets/fonts/WOFF2/TTF/SourceCodePro-Regular.ttf.woff2 +0 -0
- package/doc/assets/fonts/source-code-pro.css +23 -0
- package/doc/assets/github.css +123 -0
- package/doc/assets/site.js +168 -0
- package/doc/assets/split.css +15 -0
- package/doc/assets/split.js +784 -0
- package/doc/assets/style.css +147 -0
- package/doc/index.html +9387 -0
- package/docs/block.html +36 -0
- package/docs/ecies.html +81 -0
- package/docs/index.html +84 -0
- package/docs/networks.html +43 -0
- package/docs/script.html +101 -0
- package/docs/transaction.html +150 -0
- package/docs/unspentoutput.html +32 -0
- package/entry.js +82 -0
- package/index.js +102 -0
- package/jsdoc.json +5 -0
- package/karma.conf.js +78 -0
- package/lib/ach.js +59 -0
- package/lib/address.js +264 -0
- package/lib/aes.js +57 -0
- package/lib/aescbc.js +38 -0
- package/lib/base-58-check.js +83 -0
- package/lib/base-58.js +59 -0
- package/lib/bip-32.js +472 -0
- package/lib/bip-39-en-wordlist.js +2054 -0
- package/lib/bip-39-en.js +10 -0
- package/lib/bip-39-es-wordlist.js +2055 -0
- package/lib/bip-39-es.js +10 -0
- package/lib/bip-39-jp-wordlist.js +2053 -0
- package/lib/bip-39-jp.js +10 -0
- package/lib/bip-39-words.js +5 -0
- package/lib/bip-39.js +275 -0
- package/lib/block-header.js +79 -0
- package/lib/block.js +132 -0
- package/lib/bn.js +467 -0
- package/lib/br.js +172 -0
- package/lib/bsm.js +127 -0
- package/lib/bw.js +188 -0
- package/lib/cbc.js +140 -0
- package/lib/cmp.js +37 -0
- package/lib/config.js +50 -0
- package/lib/constants.js +150 -0
- package/lib/ecdsa.js +495 -0
- package/lib/ecies.js +151 -0
- package/lib/hash-cache.js +41 -0
- package/lib/hash.js +191 -0
- package/lib/interp.js +1853 -0
- package/lib/inv.js +49 -0
- package/lib/key-pair.js +133 -0
- package/lib/mainnet-address.js +5 -0
- package/lib/mainnet-bip-32.js +5 -0
- package/lib/mainnet-key-pair.js +5 -0
- package/lib/mainnet-priv-key.js +5 -0
- package/lib/merkle.js +92 -0
- package/lib/msg.js +184 -0
- package/lib/op-code.js +398 -0
- package/lib/point.js +134 -0
- package/lib/priv-key.js +157 -0
- package/lib/pub-key.js +230 -0
- package/lib/rabin.js +300 -0
- package/lib/random.js +28 -0
- package/lib/script.js +826 -0
- package/lib/sig-operations.js +120 -0
- package/lib/sig.js +399 -0
- package/lib/struct.js +389 -0
- package/lib/testnet-address.js +5 -0
- package/lib/testnet-bip-32.js +5 -0
- package/lib/testnet-key-pair.js +5 -0
- package/lib/testnet-priv-key.js +5 -0
- package/lib/tx-builder.js +593 -0
- package/lib/tx-in.js +172 -0
- package/lib/tx-out-map.js +57 -0
- package/lib/tx-out.js +73 -0
- package/lib/tx-verifier.js +209 -0
- package/lib/tx.js +429 -0
- package/lib/var-int.js +73 -0
- package/lib/version.js +92 -0
- package/lib/workers-result.js +65 -0
- package/lib/workers.js +77 -0
- package/load-babel.js +3 -0
- package/mkdocs.yml +37 -0
- package/package.json +138 -0
- package/test/ach.js +172 -0
- package/test/address.js +464 -0
- package/test/aes.js +96 -0
- package/test/aescbc.js +65 -0
- package/test/base-58-check.js +128 -0
- package/test/base-58.js +113 -0
- package/test/bip-32.js +721 -0
- package/test/bip-39.js +282 -0
- package/test/block-header.js +152 -0
- package/test/block.js +243 -0
- package/test/bn.js +643 -0
- package/test/br.js +353 -0
- package/test/bsm.js +141 -0
- package/test/bw.js +292 -0
- package/test/cbc.js +335 -0
- package/test/cmp.js +56 -0
- package/test/data/bip69.json +215 -0
- package/test/data/bitcoind/base58_keys_invalid.json +152 -0
- package/test/data/bitcoind/base58_keys_valid.json +452 -0
- package/test/data/bitcoind/blocks.json +27 -0
- package/test/data/bitcoind/script_tests.json +2244 -0
- package/test/data/bitcoind/sig_canonical.json +7 -0
- package/test/data/bitcoind/sig_noncanonical.json +22 -0
- package/test/data/bitcoind/tx_invalid.json +177 -0
- package/test/data/bitcoind/tx_valid.json +224 -0
- package/test/data/blk86756-testnet.dat +0 -0
- package/test/data/blk86756-testnet.js +12 -0
- package/test/data/blk86756-testnet.json +684 -0
- package/test/data/ecdsa.json +230 -0
- package/test/data/messages.json +22 -0
- package/test/data/sighash.json +1004 -0
- package/test/data/tx_creation.json +85 -0
- package/test/ecdsa.js +748 -0
- package/test/ecies.js +238 -0
- package/test/global.js +13 -0
- package/test/hash-cache.js +52 -0
- package/test/hash.js +354 -0
- package/test/index.js +10 -0
- package/test/interp.js +512 -0
- package/test/inv.js +65 -0
- package/test/key-pair.js +243 -0
- package/test/merkle.js +179 -0
- package/test/msg.js +295 -0
- package/test/op-code.js +79 -0
- package/test/point.js +375 -0
- package/test/priv-key.js +264 -0
- package/test/pub-key.js +549 -0
- package/test/random.js +31 -0
- package/test/script.js +1340 -0
- package/test/sig-operations.js +149 -0
- package/test/sig.js +662 -0
- package/test/struct.js +552 -0
- package/test/tx-builder.js +2104 -0
- package/test/tx-in.js +198 -0
- package/test/tx-out-map.js +104 -0
- package/test/tx-out.js +162 -0
- package/test/tx-verifier.js +246 -0
- package/test/tx.js +539 -0
- package/test/var-int.js +132 -0
- package/test/vectors/aes.json +10392 -0
- package/test/vectors/aescbc.json +620 -0
- package/test/vectors/bip39.json +502 -0
- package/test/vectors/bip69.json +215 -0
- package/test/vectors/bitcoin-abc/script_tests.json +48 -0
- package/test/vectors/bitcoin-abc/sighash.json +7004 -0
- package/test/vectors/bitcoin-sv/script_tests.json +248 -0
- package/test/vectors/bitcoind/script_invalid.json +1151 -0
- package/test/vectors/bitcoind/script_valid.json +1462 -0
- package/test/vectors/bitcoind/sighash.json +3503 -0
- package/test/vectors/bitcoind/tx_invalid.json +883 -0
- package/test/vectors/bitcoind/tx_valid.json +1150 -0
- package/test/vectors/coolest-tx-ever-sent.json +8 -0
- package/test/vectors/ecdsa.json +300 -0
- package/test/vectors/hash.json +6133 -0
- package/test/vectors/kdf.json +47 -0
- package/test/vectors/largesttx.json +5 -0
- package/test/vectors/largesttxblock.json +6 -0
- package/test/vectors/sig.json +79 -0
- package/test/vectors/sighash-single-bug.json +8 -0
- package/test/version.js +81 -0
- package/test/workers-result.js +89 -0
- package/test/workers.js +91 -0
- package/test-index.js +3 -0
- package/test1.html +88 -0
- package/webpack.config.js +60 -0
- package/webpack.config.js_entry +57 -0
- package/yarn-error.log +6199 -0
package/index.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
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/jsdoc.json
ADDED
package/karma.conf.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
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 }
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base58 Check Encoding
|
|
3
|
+
* =====================
|
|
4
|
+
*
|
|
5
|
+
* Base58 check encoding. The usual way to use it is
|
|
6
|
+
* new Base58Check(buf).toString() or new Base58Check(str).toBuffer().
|
|
7
|
+
*/
|
|
8
|
+
'use strict'
|
|
9
|
+
|
|
10
|
+
import { Base58 } from './base-58'
|
|
11
|
+
import { cmp } from './cmp'
|
|
12
|
+
import { Hash } from './hash'
|
|
13
|
+
import { Struct } from './struct'
|
|
14
|
+
|
|
15
|
+
class Base58Check extends Struct {
|
|
16
|
+
constructor (buf) {
|
|
17
|
+
super({ buf })
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
fromHex (hex) {
|
|
21
|
+
return this.fromBuffer(Buffer.from(hex, 'hex'))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
toHex () {
|
|
25
|
+
return this.toBuffer().toString('hex')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static decode (s) {
|
|
29
|
+
if (typeof s !== 'string') {
|
|
30
|
+
throw new Error('Input must be a string')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const buf = Base58.decode(s)
|
|
34
|
+
|
|
35
|
+
if (buf.length < 4) {
|
|
36
|
+
throw new Error('Input string too short')
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const data = buf.slice(0, -4)
|
|
40
|
+
const csum = buf.slice(-4)
|
|
41
|
+
|
|
42
|
+
const hash = Hash.sha256Sha256(data)
|
|
43
|
+
const hash4 = hash.slice(0, 4)
|
|
44
|
+
|
|
45
|
+
if (!cmp(csum, hash4)) {
|
|
46
|
+
throw new Error('Checksum mismatch')
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return data
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
static encode (buf) {
|
|
53
|
+
if (!Buffer.isBuffer(buf)) {
|
|
54
|
+
throw new Error('Input must be a buffer')
|
|
55
|
+
}
|
|
56
|
+
const checkedBuf = Buffer.alloc(buf.length + 4)
|
|
57
|
+
const hash = Hash.sha256Sha256(buf)
|
|
58
|
+
buf.copy(checkedBuf)
|
|
59
|
+
hash.copy(checkedBuf, buf.length)
|
|
60
|
+
return Base58.encode(checkedBuf)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
fromBuffer (buf) {
|
|
64
|
+
this.buf = buf
|
|
65
|
+
return this
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
fromString (str) {
|
|
69
|
+
const buf = Base58Check.decode(str)
|
|
70
|
+
this.buf = buf
|
|
71
|
+
return this
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
toBuffer () {
|
|
75
|
+
return this.buf
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
toString () {
|
|
79
|
+
return Base58Check.encode(this.buf)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { Base58Check }
|