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,171 @@
1
+ 'use strict'
2
+
3
+ var crypto = require('crypto')
4
+ var $ = require('../util/preconditions')
5
+
6
+ var Hash = module.exports
7
+
8
+ /**
9
+ * A SHA or SHA1 hash, which is always 160 bits or 20 bytes long.
10
+ *
11
+ * See:
12
+ * https://en.wikipedia.org/wiki/SHA-1
13
+ *
14
+ * @param {Buffer} buf Data, a.k.a. pre-image, which can be any size.
15
+ * @returns {Buffer} The hash in the form of a buffer.
16
+ */
17
+ Hash.sha1 = function (buf) {
18
+ $.checkArgument(Buffer.isBuffer(buf))
19
+ return crypto.createHash('sha1').update(buf).digest()
20
+ }
21
+
22
+ Hash.sha1.blocksize = 512
23
+
24
+ /**
25
+ * A SHA256 hash, which is always 256 bits or 32 bytes long.
26
+ *
27
+ * See:
28
+ * https://www.movable-type.co.uk/scripts/sha256.html
29
+ *
30
+ * @param {Buffer} buf Data, a.k.a. pre-image, which can be any size.
31
+ * @returns {Buffer} The hash in the form of a buffer.
32
+ */
33
+ Hash.sha256 = function (buf) {
34
+ $.checkArgument(Buffer.isBuffer(buf))
35
+ return crypto.createHash('sha256').update(buf).digest()
36
+ }
37
+
38
+ Hash.sha256.blocksize = 512
39
+
40
+ /**
41
+ * A double SHA256 hash, which is always 256 bits or 32 bytes bytes long. This
42
+ * hash function is commonly used inside Bitcoin, particularly for the hash of a
43
+ * block and the hash of a transaction.
44
+ *
45
+ * See:
46
+ * https://www.movable-type.co.uk/scripts/sha256.html
47
+ *
48
+ * @param {Buffer} buf Data, a.k.a. pre-image, which can be any size.
49
+ * @returns {Buffer} The hash in the form of a buffer.
50
+ */
51
+ Hash.sha256sha256 = function (buf) {
52
+ $.checkArgument(Buffer.isBuffer(buf))
53
+ return Hash.sha256(Hash.sha256(buf))
54
+ }
55
+
56
+ /**
57
+ * A RIPEMD160 hash, which is always 160 bits or 20 bytes long.
58
+ *
59
+ * See:
60
+ * https://en.wikipedia.org/wiki/RIPEMD
61
+ *
62
+ * @param {Buffer} buf Data, a.k.a. pre-image, which can be any size.
63
+ * @returns {Buffer} The hash in the form of a buffer.
64
+ */
65
+ Hash.ripemd160 = function (buf) {
66
+ $.checkArgument(Buffer.isBuffer(buf))
67
+ return crypto.createHash('ripemd160').update(buf).digest()
68
+ }
69
+ /**
70
+ * A RIPEMD160 hash of a SHA256 hash, which is always 160 bits or 20 bytes long.
71
+ * This value is commonly used inside Bitcoin, particularly for Bitcoin
72
+ * addresses.
73
+ *
74
+ * See:
75
+ * https://en.wikipedia.org/wiki/RIPEMD
76
+ *
77
+ * @param {Buffer} buf Data, a.k.a. pre-image, which can be any size.
78
+ * @returns {Buffer} The hash in the form of a buffer.
79
+ */
80
+ Hash.sha256ripemd160 = function (buf) {
81
+ $.checkArgument(Buffer.isBuffer(buf))
82
+ return Hash.ripemd160(Hash.sha256(buf))
83
+ }
84
+
85
+ /**
86
+ * A SHA512 hash, which is always 512 bits or 64 bytes long.
87
+ *
88
+ * See:
89
+ * https://en.wikipedia.org/wiki/SHA-2
90
+ *
91
+ * @param {Buffer} buf Data, a.k.a. pre-image, which can be any size.
92
+ * @returns {Buffer} The hash in the form of a buffer.
93
+ */
94
+ Hash.sha512 = function (buf) {
95
+ $.checkArgument(Buffer.isBuffer(buf))
96
+ return crypto.createHash('sha512').update(buf).digest()
97
+ }
98
+
99
+ Hash.sha512.blocksize = 1024
100
+
101
+ /**
102
+ * A way to do HMAC using any underlying hash function. If you ever find that
103
+ * you want to hash two pieces of data together, you should use HMAC instead of
104
+ * just using a hash function. Rather than doing hash(data1 + data2) you should
105
+ * do HMAC(data1, data2). Actually, rather than use HMAC directly, we recommend
106
+ * you use either sha256hmac or sha515hmac provided below.
107
+ *
108
+ * See:
109
+ * https://en.wikipedia.org/wiki/Length_extension_attack
110
+ * https://blog.skullsecurity.org/2012/everything-you-need-to-know-about-hash-length-extension-attacks
111
+ *
112
+ * @param {function} hashf Which hash function to use.
113
+ * @param {Buffer} data Data, which can be any size.
114
+ * @param {Buffer} key Key, which can be any size.
115
+ * @returns {Buffer} The HMAC in the form of a buffer.
116
+ */
117
+ Hash.hmac = function (hashf, data, key) {
118
+ // http://en.wikipedia.org/wiki/Hash-based_message_authentication_code
119
+ // http://tools.ietf.org/html/rfc4868#section-2
120
+ $.checkArgument(Buffer.isBuffer(data))
121
+ $.checkArgument(Buffer.isBuffer(key))
122
+ $.checkArgument(hashf.blocksize)
123
+
124
+ var blocksize = hashf.blocksize / 8
125
+
126
+ if (key.length > blocksize) {
127
+ key = hashf(key)
128
+ } else if (key < blocksize) {
129
+ var fill = Buffer.alloc(blocksize)
130
+ fill.fill(0)
131
+ key.copy(fill)
132
+ key = fill
133
+ }
134
+
135
+ var oKey = Buffer.alloc(blocksize)
136
+ oKey.fill(0x5c)
137
+
138
+ var iKey = Buffer.alloc(blocksize)
139
+ iKey.fill(0x36)
140
+
141
+ var oKeyPad = Buffer.alloc(blocksize)
142
+ var iKeyPad = Buffer.alloc(blocksize)
143
+ for (var i = 0; i < blocksize; i++) {
144
+ oKeyPad[i] = oKey[i] ^ key[i]
145
+ iKeyPad[i] = iKey[i] ^ key[i]
146
+ }
147
+
148
+ return hashf(Buffer.concat([oKeyPad, hashf(Buffer.concat([iKeyPad, data]))]))
149
+ }
150
+
151
+ /**
152
+ * A SHA256 HMAC.
153
+ *
154
+ * @param {Buffer} data Data, which can be any size.
155
+ * @param {Buffer} key Key, which can be any size.
156
+ * @returns {Buffer} The HMAC in the form of a buffer.
157
+ */
158
+ Hash.sha256hmac = function (data, key) {
159
+ return Hash.hmac(Hash.sha256, data, key)
160
+ }
161
+
162
+ /**
163
+ * A SHA512 HMAC.
164
+ *
165
+ * @param {Buffer} data Data, which can be any size.
166
+ * @param {Buffer} key Key, which can be any size.
167
+ * @returns {Buffer} The HMAC in the form of a buffer.
168
+ */
169
+ Hash.sha512hmac = function (data, key) {
170
+ return Hash.hmac(Hash.sha512, data, key)
171
+ }
@@ -0,0 +1,217 @@
1
+ 'use strict'
2
+
3
+ var BN = require('./bn')
4
+
5
+ var EC = require('elliptic').ec
6
+ var ec = new EC('secp256k1')
7
+ var ecPoint = ec.curve.point.bind(ec.curve)
8
+ var ecPointFromX = ec.curve.pointFromX.bind(ec.curve)
9
+
10
+ /**
11
+ * Instantiate a valid secp256k1 Point from the X and Y coordinates. This class
12
+ * is just an extension of the secp256k1 code from the library "elliptic" by
13
+ * Fedor Indutny. It includes a few extra features that are useful in Bitcoin.
14
+ *
15
+ * @param {BN|String} x - The X coordinate
16
+ * @param {BN|String} y - The Y coordinate
17
+ * @link https://github.com/indutny/elliptic
18
+ * @augments elliptic.curve.point
19
+ * @throws {Error} A validation error if exists
20
+ * @returns {Point} An instance of Point
21
+ * @constructor
22
+ */
23
+ var Point = function Point (x, y, isRed) {
24
+ try {
25
+ var point = ecPoint(x, y, isRed)
26
+ } catch (e) {
27
+ throw new Error('Invalid Point')
28
+ }
29
+ point.validate()
30
+ return point
31
+ }
32
+
33
+ Point.prototype = Object.getPrototypeOf(ec.curve.point())
34
+
35
+ /**
36
+ *
37
+ * Instantiate a valid secp256k1 Point from only the X coordinate. This is
38
+ * useful to rederive a full point from the compressed form of a point.
39
+ *
40
+ * @param {boolean} odd - If the Y coordinate is odd
41
+ * @param {BN|String} x - The X coordinate
42
+ * @throws {Error} A validation error if exists
43
+ * @returns {Point} An instance of Point
44
+ */
45
+ Point.fromX = function fromX (odd, x) {
46
+ try {
47
+ var point = ecPointFromX(x, odd)
48
+ } catch (e) {
49
+ throw new Error('Invalid X')
50
+ }
51
+ point.validate()
52
+ return point
53
+ }
54
+
55
+ /**
56
+ *
57
+ * Will return a secp256k1 ECDSA base point.
58
+ *
59
+ * @link https://en.bitcoin.it/wiki/Secp256k1
60
+ * @returns {Point} An instance of the base point.
61
+ */
62
+ Point.getG = function getG () {
63
+ return ec.curve.g
64
+ }
65
+
66
+ /**
67
+ *
68
+ * Will return the max of range of valid private keys as governed by the
69
+ * secp256k1 ECDSA standard.
70
+ *
71
+ * @link https://en.bitcoin.it/wiki/Private_key#Range_of_valid_ECDSA_private_keys
72
+ * @returns {BN} A BN instance of the number of points on the curve
73
+ */
74
+ Point.getN = function getN () {
75
+ return new BN(ec.curve.n.toArray())
76
+ }
77
+
78
+ if (!Point.prototype._getX) { Point.prototype._getX = Point.prototype.getX }
79
+
80
+ /**
81
+ * Will return the X coordinate of the Point.
82
+ *
83
+ * @returns {BN} A BN instance of the X coordinate
84
+ */
85
+ Point.prototype.getX = function getX () {
86
+ return new BN(this._getX().toArray())
87
+ }
88
+
89
+ if (!Point.prototype._getY) { Point.prototype._getY = Point.prototype.getY }
90
+
91
+ /**
92
+ * Will return the Y coordinate of the Point.
93
+ *
94
+ * @returns {BN} A BN instance of the Y coordinate
95
+ */
96
+ Point.prototype.getY = function getY () {
97
+ return new BN(this._getY().toArray())
98
+ }
99
+
100
+ /**
101
+ * Will determine if the point is valid.
102
+ *
103
+ * @link https://www.iacr.org/archive/pkc2003/25670211/25670211.pdf
104
+ * @throws {Error} A validation error if exists
105
+ * @returns {Point} An instance of the same Point
106
+ */
107
+ Point.prototype.validate = function validate () {
108
+ if (this.isInfinity()) {
109
+ throw new Error('Point cannot be equal to Infinity')
110
+ }
111
+
112
+ var p2
113
+ try {
114
+ p2 = ecPointFromX(this.getX(), this.getY().isOdd())
115
+ } catch (e) {
116
+ throw new Error('Point does not lie on the curve')
117
+ }
118
+
119
+ if (p2.y.cmp(this.y) !== 0) {
120
+ throw new Error('Invalid y value for curve.')
121
+ }
122
+
123
+ // todo: needs test case
124
+ if (!(this.mul(Point.getN()).isInfinity())) {
125
+ throw new Error('Point times N must be infinity')
126
+ }
127
+
128
+ return this
129
+ }
130
+
131
+ /**
132
+ * A "compressed" format point is the X part of the (X, Y) point plus an extra
133
+ * bit (which takes an entire byte) to indicate whether the Y value is odd or
134
+ * not. Storing points this way takes a bit less space, but requires a bit more
135
+ * computation to rederive the full point.
136
+ *
137
+ * @param {Point} point An instance of Point.
138
+ * @returns {Buffer} A compressed point in the form of a buffer.
139
+ */
140
+ Point.pointToCompressed = function pointToCompressed (point) {
141
+ var xbuf = point.getX().toBuffer({ size: 32 })
142
+ var ybuf = point.getY().toBuffer({ size: 32 })
143
+
144
+ var prefix
145
+ var odd = ybuf[ybuf.length - 1] % 2
146
+ if (odd) {
147
+ prefix = Buffer.from([0x03])
148
+ } else {
149
+ prefix = Buffer.from([0x02])
150
+ }
151
+ return Buffer.concat([prefix, xbuf])
152
+ }
153
+
154
+ /**
155
+ * Converts a compressed buffer into a point.
156
+ *
157
+ * @param {Buffer} buf A compressed point.
158
+ * @returns {Point} A Point.
159
+ */
160
+ Point.pointFromCompressed = function (buf) {
161
+ if (buf.length !== 33) {
162
+ throw new Error('invalid buffer length')
163
+ }
164
+ let prefix = buf[0]
165
+ let odd
166
+ if (prefix === 0x03) {
167
+ odd = true
168
+ } else if (prefix === 0x02) {
169
+ odd = false
170
+ } else {
171
+ throw new Error('invalid value of compressed prefix')
172
+ }
173
+
174
+ let xbuf = buf.slice(1, 33)
175
+ let x = BN.fromBuffer(xbuf)
176
+ return Point.fromX(odd, x)
177
+ }
178
+
179
+ /**
180
+ * Convert point to a compressed buffer.
181
+ *
182
+ * @returns {Buffer} A compressed point.
183
+ */
184
+ Point.prototype.toBuffer = function () {
185
+ return Point.pointToCompressed(this)
186
+ }
187
+
188
+ /**
189
+ * Convert point to a compressed hex string.
190
+ *
191
+ * @returns {string} A compressed point as a hex string.
192
+ */
193
+ Point.prototype.toHex = function () {
194
+ return this.toBuffer().toString('hex')
195
+ }
196
+
197
+ /**
198
+ * Converts a compressed buffer into a point.
199
+ *
200
+ * @param {Buffer} buf A compressed point.
201
+ * @returns {Point} A Point.
202
+ */
203
+ Point.fromBuffer = function (buf) {
204
+ return Point.pointFromCompressed(buf)
205
+ }
206
+
207
+ /**
208
+ * Converts a compressed buffer into a point.
209
+ *
210
+ * @param {Buffer} hex A compressed point as a hex string.
211
+ * @returns {Point} A Point.
212
+ */
213
+ Point.fromHex = function (hex) {
214
+ return Point.fromBuffer(Buffer.from(hex, 'hex'))
215
+ }
216
+
217
+ module.exports = Point
@@ -0,0 +1,37 @@
1
+ 'use strict'
2
+
3
+ function Random () {
4
+ }
5
+
6
+ /* secure random bytes that sometimes throws an error due to lack of entropy */
7
+ Random.getRandomBuffer = function (size) {
8
+ if (process.browser) { return Random.getRandomBufferBrowser(size) } else { return Random.getRandomBufferNode(size) }
9
+ }
10
+
11
+ Random.getRandomBufferNode = function (size) {
12
+ var crypto = require('crypto')
13
+ return crypto.randomBytes(size)
14
+ }
15
+
16
+ Random.getRandomBufferBrowser = function (size) {
17
+ if (!window.crypto && !window.msCrypto) {
18
+ throw new Error('window.crypto not available')
19
+ }
20
+ var crypto
21
+
22
+ if (window.crypto && window.crypto.getRandomValues) {
23
+ crypto = window.crypto
24
+ } else if (window.msCrypto && window.msCrypto.getRandomValues) { // internet explorer
25
+ crypto = window.msCrypto
26
+ } else {
27
+ throw new Error('window.crypto.getRandomValues not available')
28
+ }
29
+
30
+ var bbuf = new Uint8Array(size)
31
+ crypto.getRandomValues(bbuf)
32
+ var buf = Buffer.from(bbuf)
33
+
34
+ return buf
35
+ }
36
+
37
+ module.exports = Random