smartledger-bsv 3.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 (76) hide show
  1. package/LICENSE +36 -0
  2. package/README.md +305 -0
  3. package/SECURITY.md +75 -0
  4. package/bsv-ecies.min.js +12 -0
  5. package/bsv-message.min.js +10 -0
  6. package/bsv-mnemonic.min.js +12 -0
  7. package/bsv.d.ts +440 -0
  8. package/bsv.min.js +37 -0
  9. package/ecies/index.js +1 -0
  10. package/index.js +101 -0
  11. package/lib/address.js +526 -0
  12. package/lib/block/block.js +277 -0
  13. package/lib/block/blockheader.js +294 -0
  14. package/lib/block/index.js +4 -0
  15. package/lib/block/merkleblock.js +316 -0
  16. package/lib/crypto/bn.js +278 -0
  17. package/lib/crypto/ecdsa.js +330 -0
  18. package/lib/crypto/elliptic-fixed.js +74 -0
  19. package/lib/crypto/hash.browser.js +171 -0
  20. package/lib/crypto/hash.js +2 -0
  21. package/lib/crypto/hash.node.js +171 -0
  22. package/lib/crypto/point.js +217 -0
  23. package/lib/crypto/random.js +37 -0
  24. package/lib/crypto/signature.js +410 -0
  25. package/lib/crypto/smartledger_verify.js +109 -0
  26. package/lib/ecies/bitcore-ecies.js +163 -0
  27. package/lib/ecies/electrum-ecies.js +175 -0
  28. package/lib/ecies/errors.js +16 -0
  29. package/lib/ecies/index.js +1 -0
  30. package/lib/encoding/base58.js +108 -0
  31. package/lib/encoding/base58check.js +112 -0
  32. package/lib/encoding/bufferreader.js +200 -0
  33. package/lib/encoding/bufferwriter.js +150 -0
  34. package/lib/encoding/varint.js +71 -0
  35. package/lib/errors/index.js +57 -0
  36. package/lib/errors/spec.js +184 -0
  37. package/lib/hdprivatekey.js +655 -0
  38. package/lib/hdpublickey.js +509 -0
  39. package/lib/message/index.js +4 -0
  40. package/lib/message/message.js +181 -0
  41. package/lib/mnemonic/errors.js +18 -0
  42. package/lib/mnemonic/index.js +4 -0
  43. package/lib/mnemonic/mnemonic.js +304 -0
  44. package/lib/mnemonic/pbkdf2.js +68 -0
  45. package/lib/mnemonic/words/chinese.js +5 -0
  46. package/lib/mnemonic/words/english.js +5 -0
  47. package/lib/mnemonic/words/french.js +5 -0
  48. package/lib/mnemonic/words/index.js +8 -0
  49. package/lib/mnemonic/words/italian.js +5 -0
  50. package/lib/mnemonic/words/japanese.js +5 -0
  51. package/lib/mnemonic/words/spanish.js +5 -0
  52. package/lib/networks.js +392 -0
  53. package/lib/opcode.js +248 -0
  54. package/lib/privatekey.js +373 -0
  55. package/lib/publickey.js +387 -0
  56. package/lib/script/index.js +3 -0
  57. package/lib/script/interpreter.js +1807 -0
  58. package/lib/script/script.js +1153 -0
  59. package/lib/transaction/index.js +7 -0
  60. package/lib/transaction/input/index.js +6 -0
  61. package/lib/transaction/input/input.js +202 -0
  62. package/lib/transaction/input/multisig.js +220 -0
  63. package/lib/transaction/input/multisigscripthash.js +189 -0
  64. package/lib/transaction/input/publickey.js +96 -0
  65. package/lib/transaction/input/publickeyhash.js +103 -0
  66. package/lib/transaction/output.js +192 -0
  67. package/lib/transaction/sighash.js +288 -0
  68. package/lib/transaction/signature.js +88 -0
  69. package/lib/transaction/transaction.js +1208 -0
  70. package/lib/transaction/unspentoutput.js +97 -0
  71. package/lib/util/_.js +44 -0
  72. package/lib/util/js.js +91 -0
  73. package/lib/util/preconditions.js +34 -0
  74. package/message/index.js +1 -0
  75. package/mnemonic/index.js +1 -0
  76. package/package.json +86 -0
@@ -0,0 +1,97 @@
1
+ 'use strict'
2
+
3
+ var _ = require('../util/_')
4
+ var $ = require('../util/preconditions')
5
+ var JSUtil = require('../util/js')
6
+
7
+ var Script = require('../script')
8
+ var Address = require('../address')
9
+
10
+ /**
11
+ * Represents an unspent output information: its script, associated amount and address,
12
+ * transaction id and output index.
13
+ *
14
+ * @constructor
15
+ * @param {object} data
16
+ * @param {string} data.txid the previous transaction id
17
+ * @param {string=} data.txId alias for `txid`
18
+ * @param {number} data.vout the index in the transaction
19
+ * @param {number=} data.outputIndex alias for `vout`
20
+ * @param {string|Script} data.scriptPubKey the script that must be resolved to release the funds
21
+ * @param {string|Script=} data.script alias for `scriptPubKey`
22
+ * @param {number} data.amount amount of bitcoins associated
23
+ * @param {number=} data.satoshis alias for `amount`, but expressed in satoshis (1 BSV = 1e8 satoshis)
24
+ * @param {string|Address=} data.address the associated address to the script, if provided
25
+ */
26
+ function UnspentOutput (data) {
27
+ if (!(this instanceof UnspentOutput)) {
28
+ return new UnspentOutput(data)
29
+ }
30
+ $.checkArgument(_.isObject(data), 'Must provide an object from where to extract data')
31
+ var address = data.address ? new Address(data.address) : undefined
32
+ var txId = data.txid ? data.txid : data.txId
33
+ if (!txId || !JSUtil.isHexaString(txId) || txId.length > 64) {
34
+ // TODO: Use the errors library
35
+ throw new Error('Invalid TXID in object', data)
36
+ }
37
+ var outputIndex = _.isUndefined(data.vout) ? data.outputIndex : data.vout
38
+ if (!_.isNumber(outputIndex)) {
39
+ throw new Error('Invalid outputIndex, received ' + outputIndex)
40
+ }
41
+ $.checkArgument(!_.isUndefined(data.scriptPubKey) || !_.isUndefined(data.script),
42
+ 'Must provide the scriptPubKey for that output!')
43
+ var script = new Script(data.scriptPubKey || data.script)
44
+ $.checkArgument(!_.isUndefined(data.amount) || !_.isUndefined(data.satoshis),
45
+ 'Must provide an amount for the output')
46
+ var amount = !_.isUndefined(data.amount) ? Math.round(data.amount * 1e8) : data.satoshis
47
+ $.checkArgument(_.isNumber(amount), 'Amount must be a number')
48
+ JSUtil.defineImmutable(this, {
49
+ address: address,
50
+ txId: txId,
51
+ outputIndex: outputIndex,
52
+ script: script,
53
+ satoshis: amount
54
+ })
55
+ }
56
+
57
+ /**
58
+ * Provide an informative output when displaying this object in the console
59
+ * @returns string
60
+ */
61
+ UnspentOutput.prototype.inspect = function () {
62
+ return '<UnspentOutput: ' + this.txId + ':' + this.outputIndex +
63
+ ', satoshis: ' + this.satoshis + ', address: ' + this.address + '>'
64
+ }
65
+
66
+ /**
67
+ * String representation: just "txid:index"
68
+ * @returns string
69
+ */
70
+ UnspentOutput.prototype.toString = function () {
71
+ return this.txId + ':' + this.outputIndex
72
+ }
73
+
74
+ /**
75
+ * Deserialize an UnspentOutput from an object
76
+ * @param {object|string} data
77
+ * @return UnspentOutput
78
+ */
79
+ UnspentOutput.fromObject = function (data) {
80
+ return new UnspentOutput(data)
81
+ }
82
+
83
+ /**
84
+ * Returns a plain object (no prototype or methods) with the associated info for this output
85
+ * @return {object}
86
+ */
87
+ UnspentOutput.prototype.toObject = UnspentOutput.prototype.toJSON = function toObject () {
88
+ return {
89
+ address: this.address ? this.address.toString() : undefined,
90
+ txid: this.txId,
91
+ vout: this.outputIndex,
92
+ scriptPubKey: this.script.toBuffer().toString('hex'),
93
+ amount: Number.parseFloat((this.satoshis / 1e8).toFixed(8))
94
+ }
95
+ }
96
+
97
+ module.exports = UnspentOutput
package/lib/util/_.js ADDED
@@ -0,0 +1,44 @@
1
+ 'use strict'
2
+
3
+ var _ = {}
4
+
5
+ _.isArray = t => Array.isArray(t)
6
+ _.isNumber = t => typeof t === 'number'
7
+ _.isObject = t => t && typeof t === 'object'
8
+ _.isString = t => typeof t === 'string'
9
+ _.isUndefined = t => typeof t === 'undefined'
10
+ _.isFunction = t => typeof t === 'function'
11
+ _.isNull = t => t === null
12
+ _.isDate = t => t instanceof Date
13
+ _.extend = (a, b) => Object.assign(a, b)
14
+ _.noop = () => { }
15
+ _.every = (a, f) => a.every(f || (t => t))
16
+ _.map = (a, f) => Array.from(a).map(f || (t => t))
17
+ _.includes = (a, e) => a.includes(e)
18
+ _.each = (a, f) => a.forEach(f)
19
+ _.clone = o => Object.assign({}, o)
20
+ _.pick = (object, keys) => {
21
+ const obj = {}
22
+ keys.forEach(key => {
23
+ if (typeof object[key] !== 'undefined') { obj[key] = object[key] }
24
+ })
25
+ return obj
26
+ }
27
+ _.values = o => Object.values(o)
28
+ _.filter = (a, f) => a.filter(f)
29
+ _.reduce = (a, f, s) => a.reduce(f, s)
30
+ _.without = (a, n) => a.filter(t => t !== n)
31
+ _.shuffle = a => {
32
+ const result = a.slice(0)
33
+ for (let i = result.length - 1; i > 0; i--) {
34
+ const j = Math.floor(Math.random() * (i + 1));
35
+ [result[i], result[j]] = [result[j], result[i]]
36
+ }
37
+ return result
38
+ }
39
+ _.difference = (a, b) => a.filter(t => !b.includes(t))
40
+ _.findIndex = (a, f) => a.findIndex(f)
41
+ _.some = (a, f) => a.some(f)
42
+ _.range = n => [...Array(n).keys()]
43
+
44
+ module.exports = _
package/lib/util/js.js ADDED
@@ -0,0 +1,91 @@
1
+ 'use strict'
2
+
3
+ var _ = require('../util/_')
4
+ var $ = require('./preconditions')
5
+
6
+ /**
7
+ * Determines whether a string contains only hexadecimal values
8
+ *
9
+ * @name JSUtil.isHexa
10
+ * @param {string} value
11
+ * @return {boolean} true if the string is the hexa representation of a number
12
+ */
13
+ var isHexa = function isHexa (value) {
14
+ if (!_.isString(value)) {
15
+ return false
16
+ }
17
+ return /^[0-9a-fA-F]+$/.test(value)
18
+ }
19
+
20
+ /**
21
+ * @namespace JSUtil
22
+ */
23
+ module.exports = {
24
+ /**
25
+ * Test if an argument is a valid JSON object. If it is, returns a truthy
26
+ * value (the json object decoded), so no double JSON.parse call is necessary
27
+ *
28
+ * @param {string} arg
29
+ * @return {Object|boolean} false if the argument is not a JSON string.
30
+ */
31
+ isValidJSON: function isValidJSON (arg) {
32
+ var parsed
33
+ if (!_.isString(arg)) {
34
+ return false
35
+ }
36
+ try {
37
+ parsed = JSON.parse(arg)
38
+ } catch (e) {
39
+ return false
40
+ }
41
+ if (typeof (parsed) === 'object') {
42
+ return true
43
+ }
44
+ return false
45
+ },
46
+ isHexa: isHexa,
47
+ isHexaString: isHexa,
48
+
49
+ /**
50
+ * Define immutable properties on a target object
51
+ *
52
+ * @param {Object} target - An object to be extended
53
+ * @param {Object} values - An object of properties
54
+ * @return {Object} The target object
55
+ */
56
+ defineImmutable: function defineImmutable (target, values) {
57
+ Object.keys(values).forEach(function (key) {
58
+ Object.defineProperty(target, key, {
59
+ configurable: false,
60
+ enumerable: true,
61
+ value: values[key]
62
+ })
63
+ })
64
+ return target
65
+ },
66
+ /**
67
+ * Checks that a value is a natural number, a positive integer or zero.
68
+ *
69
+ * @param {*} value
70
+ * @return {Boolean}
71
+ */
72
+ isNaturalNumber: function isNaturalNumber (value) {
73
+ return typeof value === 'number' &&
74
+ isFinite(value) &&
75
+ Math.floor(value) === value &&
76
+ value >= 0
77
+ },
78
+
79
+ /**
80
+ * Transform a 4-byte integer (unsigned value) into a Buffer of length 4 (Big Endian Byte Order)
81
+ *
82
+ * @param {number} integer
83
+ * @return {Buffer}
84
+ */
85
+ integerAsBuffer: function integerAsBuffer (integer) {
86
+ $.checkArgumentType(integer, 'number', 'integer')
87
+ const buf = Buffer.allocUnsafe(4)
88
+ buf.writeUInt32BE(integer, 0)
89
+ return buf
90
+ }
91
+ }
@@ -0,0 +1,34 @@
1
+ 'use strict'
2
+
3
+ var errors = require('../errors')
4
+ var _ = require('../util/_')
5
+
6
+ module.exports = {
7
+ checkState: function (condition, message) {
8
+ if (!condition) {
9
+ throw new errors.InvalidState(message)
10
+ }
11
+ },
12
+ checkArgument: function (condition, argumentName, message, docsPath) {
13
+ if (!condition) {
14
+ throw new errors.InvalidArgument(argumentName, message, docsPath)
15
+ }
16
+ },
17
+ checkArgumentType: function (argument, type, argumentName) {
18
+ argumentName = argumentName || '(unknown name)'
19
+ if (_.isString(type)) {
20
+ if (type === 'Buffer') {
21
+ var buffer = require('buffer') // './buffer' fails on cordova & RN
22
+ if (!buffer.Buffer.isBuffer(argument)) {
23
+ throw new errors.InvalidArgumentType(argument, type, argumentName)
24
+ }
25
+ } else if (typeof argument !== type) { // eslint-disable-line
26
+ throw new errors.InvalidArgumentType(argument, type, argumentName)
27
+ }
28
+ } else {
29
+ if (!(argument instanceof type)) {
30
+ throw new errors.InvalidArgumentType(argument, type.name, argumentName)
31
+ }
32
+ }
33
+ }
34
+ }
@@ -0,0 +1 @@
1
+ module.exports = require('../lib/message')
@@ -0,0 +1 @@
1
+ module.exports = require('../lib/mnemonic')
package/package.json ADDED
@@ -0,0 +1,86 @@
1
+ {
2
+ "name": "smartledger-bsv",
3
+ "version": "3.0.0",
4
+ "description": "Security-hardened Bitcoin SV library - Complete drop-in replacement for bsv@1.5.6 with zero vulnerabilities",
5
+ "author": "SmartLedger Technology <hello@smartledger.technology> (https://smartledger.technology)",
6
+ "homepage": "https://github.com/codenlighten/smartledger-bsv#readme",
7
+ "bugs": {
8
+ "url": "https://github.com/codenlighten/smartledger-bsv/issues"
9
+ },
10
+ "main": "index.js",
11
+ "scripts": {
12
+ "lint": "standard",
13
+ "test": "standard && mocha",
14
+ "coverage": "nyc --reporter=text npm run test",
15
+ "build-bsv": "webpack index.js --config webpack.config.js",
16
+ "build-ecies": "webpack ecies/index.js --config webpack.subproject.config.js --output-library bsvEcies -o bsv-ecies.min.js",
17
+ "build-message": "webpack message/index.js --config webpack.subproject.config.js --output-library bsvMessage -o bsv-message.min.js",
18
+ "build-mnemonic": "webpack mnemonic/index.js --config webpack.subproject.config.js --output-library bsvMnemonic -o bsv-mnemonic.min.js",
19
+ "build": "npm run build-bsv && npm run build-ecies && npm run build-message && npm run build-mnemonic",
20
+ "prepublishOnly": "NODE_OPTIONS=\"--openssl-legacy-provider\" npm run build"
21
+ },
22
+ "unpkg": "bsv.min.js",
23
+ "keywords": [
24
+ "bitcoin",
25
+ "bitcoin-sv",
26
+ "bsv",
27
+ "transaction",
28
+ "address",
29
+ "cryptocurrency",
30
+ "blockchain",
31
+ "security",
32
+ "hardened",
33
+ "vulnerability-free",
34
+ "drop-in-replacement",
35
+ "ecies",
36
+ "p2p",
37
+ "payment",
38
+ "bip21",
39
+ "bip32",
40
+ "bip37",
41
+ "bip69",
42
+ "bip70",
43
+ "multisig"
44
+ ],
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "https://github.com/codenlighten/smartledger-bsv"
48
+ },
49
+ "browser": {
50
+ "request": "browser-request"
51
+ },
52
+ "dependencies": {
53
+ "aes-js": "^3.1.2",
54
+ "bn.js": "=4.11.9",
55
+ "bs58": "=4.0.1",
56
+ "clone-deep": "^4.0.1",
57
+ "elliptic": "6.6.1",
58
+ "hash.js": "^1.1.7",
59
+ "inherits": "2.0.3",
60
+ "unorm": "1.4.1"
61
+ },
62
+ "devDependencies": {
63
+ "brfs": "2.0.1",
64
+ "chai": "4.2.0",
65
+ "mocha": "^8.4.0",
66
+ "nyc": "^14.1.1",
67
+ "sinon": "7.2.3",
68
+ "standard": "12.0.1",
69
+ "webpack": "4.29.3",
70
+ "webpack-cli": "^3.3.12"
71
+ },
72
+ "license": "MIT",
73
+ "standard": {
74
+ "globals": [
75
+ "after",
76
+ "afterEach",
77
+ "before",
78
+ "beforeEach",
79
+ "describe",
80
+ "it"
81
+ ],
82
+ "ignore": [
83
+ "dist/**"
84
+ ]
85
+ }
86
+ }