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,150 @@
1
+ 'use strict'
2
+
3
+ var assert = require('assert')
4
+
5
+ var BufferWriter = function BufferWriter (obj) {
6
+ if (!(this instanceof BufferWriter)) { return new BufferWriter(obj) }
7
+ this.bufLen = 0
8
+ if (obj) { this.set(obj) } else { this.bufs = [] }
9
+ }
10
+
11
+ BufferWriter.prototype.set = function (obj) {
12
+ this.bufs = obj.bufs || this.bufs || []
13
+ this.bufLen = this.bufs.reduce(function (prev, buf) { return prev + buf.length }, 0)
14
+ return this
15
+ }
16
+
17
+ BufferWriter.prototype.toBuffer = function () {
18
+ return this.concat()
19
+ }
20
+
21
+ BufferWriter.prototype.concat = function () {
22
+ return Buffer.concat(this.bufs, this.bufLen)
23
+ }
24
+
25
+ BufferWriter.prototype.write = function (buf) {
26
+ assert(Buffer.isBuffer(buf))
27
+ this.bufs.push(buf)
28
+ this.bufLen += buf.length
29
+ return this
30
+ }
31
+
32
+ BufferWriter.prototype.writeReverse = function (buf) {
33
+ assert(Buffer.isBuffer(buf))
34
+ this.bufs.push(Buffer.from(buf).reverse())
35
+ this.bufLen += buf.length
36
+ return this
37
+ }
38
+
39
+ BufferWriter.prototype.writeUInt8 = function (n) {
40
+ var buf = Buffer.alloc(1)
41
+ buf.writeUInt8(n, 0)
42
+ this.write(buf)
43
+ return this
44
+ }
45
+
46
+ BufferWriter.prototype.writeUInt16BE = function (n) {
47
+ var buf = Buffer.alloc(2)
48
+ buf.writeUInt16BE(n, 0)
49
+ this.write(buf)
50
+ return this
51
+ }
52
+
53
+ BufferWriter.prototype.writeUInt16LE = function (n) {
54
+ var buf = Buffer.alloc(2)
55
+ buf.writeUInt16LE(n, 0)
56
+ this.write(buf)
57
+ return this
58
+ }
59
+
60
+ BufferWriter.prototype.writeUInt32BE = function (n) {
61
+ var buf = Buffer.alloc(4)
62
+ buf.writeUInt32BE(n, 0)
63
+ this.write(buf)
64
+ return this
65
+ }
66
+
67
+ BufferWriter.prototype.writeInt32LE = function (n) {
68
+ var buf = Buffer.alloc(4)
69
+ buf.writeInt32LE(n, 0)
70
+ this.write(buf)
71
+ return this
72
+ }
73
+
74
+ BufferWriter.prototype.writeUInt32LE = function (n) {
75
+ var buf = Buffer.alloc(4)
76
+ buf.writeUInt32LE(n, 0)
77
+ this.write(buf)
78
+ return this
79
+ }
80
+
81
+ BufferWriter.prototype.writeUInt64BEBN = function (bn) {
82
+ var buf = bn.toBuffer({ size: 8 })
83
+ this.write(buf)
84
+ return this
85
+ }
86
+
87
+ BufferWriter.prototype.writeUInt64LEBN = function (bn) {
88
+ var buf = bn.toBuffer({ size: 8 })
89
+ this.writeReverse(buf)
90
+ return this
91
+ }
92
+
93
+ BufferWriter.prototype.writeVarintNum = function (n) {
94
+ var buf = BufferWriter.varintBufNum(n)
95
+ this.write(buf)
96
+ return this
97
+ }
98
+
99
+ BufferWriter.prototype.writeVarintBN = function (bn) {
100
+ var buf = BufferWriter.varintBufBN(bn)
101
+ this.write(buf)
102
+ return this
103
+ }
104
+
105
+ BufferWriter.varintBufNum = function (n) {
106
+ var buf
107
+ if (n < 253) {
108
+ buf = Buffer.alloc(1)
109
+ buf.writeUInt8(n, 0)
110
+ } else if (n < 0x10000) {
111
+ buf = Buffer.alloc(1 + 2)
112
+ buf.writeUInt8(253, 0)
113
+ buf.writeUInt16LE(n, 1)
114
+ } else if (n < 0x100000000) {
115
+ buf = Buffer.alloc(1 + 4)
116
+ buf.writeUInt8(254, 0)
117
+ buf.writeUInt32LE(n, 1)
118
+ } else {
119
+ buf = Buffer.alloc(1 + 8)
120
+ buf.writeUInt8(255, 0)
121
+ buf.writeInt32LE(n & -1, 1)
122
+ buf.writeUInt32LE(Math.floor(n / 0x100000000), 5)
123
+ }
124
+ return buf
125
+ }
126
+
127
+ BufferWriter.varintBufBN = function (bn) {
128
+ var buf
129
+ var n = bn.toNumber()
130
+ if (n < 253) {
131
+ buf = Buffer.alloc(1)
132
+ buf.writeUInt8(n, 0)
133
+ } else if (n < 0x10000) {
134
+ buf = Buffer.alloc(1 + 2)
135
+ buf.writeUInt8(253, 0)
136
+ buf.writeUInt16LE(n, 1)
137
+ } else if (n < 0x100000000) {
138
+ buf = Buffer.alloc(1 + 4)
139
+ buf.writeUInt8(254, 0)
140
+ buf.writeUInt32LE(n, 1)
141
+ } else {
142
+ var bw = new BufferWriter()
143
+ bw.writeUInt8(255)
144
+ bw.writeUInt64LEBN(bn)
145
+ buf = bw.concat()
146
+ }
147
+ return buf
148
+ }
149
+
150
+ module.exports = BufferWriter
@@ -0,0 +1,71 @@
1
+ 'use strict'
2
+
3
+ var BufferWriter = require('./bufferwriter')
4
+ var BufferReader = require('./bufferreader')
5
+ var BN = require('../crypto/bn')
6
+
7
+ var Varint = function Varint (buf) {
8
+ if (!(this instanceof Varint)) { return new Varint(buf) }
9
+ if (Buffer.isBuffer(buf)) {
10
+ this.buf = buf
11
+ } else if (typeof buf === 'number') {
12
+ var num = buf
13
+ this.fromNumber(num)
14
+ } else if (buf instanceof BN) {
15
+ var bn = buf
16
+ this.fromBN(bn)
17
+ } else if (buf) {
18
+ var obj = buf
19
+ this.set(obj)
20
+ }
21
+ }
22
+
23
+ Varint.prototype.set = function (obj) {
24
+ this.buf = obj.buf || this.buf
25
+ return this
26
+ }
27
+
28
+ Varint.prototype.fromString = function (str) {
29
+ this.set({
30
+ buf: Buffer.from(str, 'hex')
31
+ })
32
+ return this
33
+ }
34
+
35
+ Varint.prototype.toString = function () {
36
+ return this.buf.toString('hex')
37
+ }
38
+
39
+ Varint.prototype.fromBuffer = function (buf) {
40
+ this.buf = buf
41
+ return this
42
+ }
43
+
44
+ Varint.prototype.fromBufferReader = function (br) {
45
+ this.buf = br.readVarintBuf()
46
+ return this
47
+ }
48
+
49
+ Varint.prototype.fromBN = function (bn) {
50
+ this.buf = BufferWriter().writeVarintBN(bn).concat()
51
+ return this
52
+ }
53
+
54
+ Varint.prototype.fromNumber = function (num) {
55
+ this.buf = BufferWriter().writeVarintNum(num).concat()
56
+ return this
57
+ }
58
+
59
+ Varint.prototype.toBuffer = function () {
60
+ return this.buf
61
+ }
62
+
63
+ Varint.prototype.toBN = function () {
64
+ return BufferReader(this.buf).readVarintBN()
65
+ }
66
+
67
+ Varint.prototype.toNumber = function () {
68
+ return BufferReader(this.buf).readVarintNum()
69
+ }
70
+
71
+ module.exports = Varint
@@ -0,0 +1,57 @@
1
+ 'use strict'
2
+
3
+ var _ = require('../util/_')
4
+
5
+ function format (message, args) {
6
+ return message
7
+ .replace('{0}', args[0])
8
+ .replace('{1}', args[1])
9
+ .replace('{2}', args[2])
10
+ }
11
+ var traverseNode = function (parent, errorDefinition) {
12
+ var NodeError = function () {
13
+ if (_.isString(errorDefinition.message)) {
14
+ this.message = format(errorDefinition.message, arguments)
15
+ } else if (_.isFunction(errorDefinition.message)) {
16
+ this.message = errorDefinition.message.apply(null, arguments)
17
+ } else {
18
+ throw new Error('Invalid error definition for ' + errorDefinition.name)
19
+ }
20
+ this.stack = this.message + '\n' + (new Error()).stack
21
+ }
22
+ NodeError.prototype = Object.create(parent.prototype)
23
+ NodeError.prototype.name = parent.prototype.name + errorDefinition.name
24
+ parent[errorDefinition.name] = NodeError
25
+ if (errorDefinition.errors) {
26
+ childDefinitions(NodeError, errorDefinition.errors)
27
+ }
28
+ return NodeError
29
+ }
30
+
31
+ var childDefinitions = function (parent, childDefinitions) {
32
+ _.each(childDefinitions, function (childDefinition) {
33
+ traverseNode(parent, childDefinition)
34
+ })
35
+ }
36
+
37
+ var traverseRoot = function (parent, errorsDefinition) {
38
+ childDefinitions(parent, errorsDefinition)
39
+ return parent
40
+ }
41
+
42
+ var bsv = {}
43
+ bsv.Error = function () {
44
+ this.message = 'Internal error'
45
+ this.stack = this.message + '\n' + (new Error()).stack
46
+ }
47
+ bsv.Error.prototype = Object.create(Error.prototype)
48
+ bsv.Error.prototype.name = 'bsv.Error'
49
+
50
+ var data = require('./spec')
51
+ traverseRoot(bsv.Error, data)
52
+
53
+ module.exports = bsv.Error
54
+
55
+ module.exports.extend = function (spec) {
56
+ return traverseNode(bsv.Error, spec)
57
+ }
@@ -0,0 +1,184 @@
1
+ 'use strict'
2
+
3
+ var docsURL = 'https://docs.moneybutton.com/'
4
+
5
+ module.exports = [{
6
+ name: 'InvalidB58Char',
7
+ message: 'Invalid Base58 character: {0} in {1}'
8
+ }, {
9
+ name: 'InvalidB58Checksum',
10
+ message: 'Invalid Base58 checksum for {0}'
11
+ }, {
12
+ name: 'InvalidNetwork',
13
+ message: 'Invalid version for network: got {0}'
14
+ }, {
15
+ name: 'InvalidState',
16
+ message: 'Invalid state: {0}'
17
+ }, {
18
+ name: 'NotImplemented',
19
+ message: 'Function {0} was not implemented yet'
20
+ }, {
21
+ name: 'InvalidNetworkArgument',
22
+ message: 'Invalid network: must be "livenet" or "testnet", got {0}'
23
+ }, {
24
+ name: 'InvalidArgument',
25
+ message: function () {
26
+ return 'Invalid Argument' + (arguments[0] ? (': ' + arguments[0]) : '') +
27
+ (arguments[1] ? (' Documentation: ' + docsURL + arguments[1]) : '')
28
+ }
29
+ }, {
30
+ name: 'AbstractMethodInvoked',
31
+ message: 'Abstract Method Invocation: {0}'
32
+ }, {
33
+ name: 'InvalidArgumentType',
34
+ message: function () {
35
+ return 'Invalid Argument for ' + arguments[2] + ', expected ' + arguments[1] + ' but got ' + typeof arguments[0]
36
+ }
37
+ }, {
38
+ name: 'Unit',
39
+ message: 'Internal Error on Unit {0}',
40
+ errors: [{
41
+ 'name': 'UnknownCode',
42
+ 'message': 'Unrecognized unit code: {0}'
43
+ }, {
44
+ 'name': 'InvalidRate',
45
+ 'message': 'Invalid exchange rate: {0}'
46
+ }]
47
+ }, {
48
+ name: 'MerkleBlock',
49
+ message: 'Internal Error on MerkleBlock {0}',
50
+ errors: [{
51
+ 'name': 'InvalidMerkleTree',
52
+ 'message': 'This MerkleBlock contain an invalid Merkle Tree'
53
+ }]
54
+ }, {
55
+ name: 'Transaction',
56
+ message: 'Internal Error on Transaction {0}',
57
+ errors: [{
58
+ name: 'Input',
59
+ message: 'Internal Error on Input {0}',
60
+ errors: [{
61
+ name: 'MissingScript',
62
+ message: 'Need a script to create an input'
63
+ }, {
64
+ name: 'UnsupportedScript',
65
+ message: 'Unsupported input script type: {0}'
66
+ }, {
67
+ name: 'MissingPreviousOutput',
68
+ message: 'No previous output information.'
69
+ }]
70
+ }, {
71
+ name: 'NeedMoreInfo',
72
+ message: '{0}'
73
+ }, {
74
+ name: 'InvalidSorting',
75
+ message: 'The sorting function provided did not return the change output as one of the array elements'
76
+ }, {
77
+ name: 'InvalidOutputAmountSum',
78
+ message: '{0}'
79
+ }, {
80
+ name: 'MissingSignatures',
81
+ message: 'Some inputs have not been fully signed'
82
+ }, {
83
+ name: 'InvalidIndex',
84
+ message: 'Invalid index: {0} is not between 0, {1}'
85
+ }, {
86
+ name: 'UnableToVerifySignature',
87
+ message: 'Unable to verify signature: {0}'
88
+ }, {
89
+ name: 'DustOutputs',
90
+ message: 'Dust amount detected in one output'
91
+ }, {
92
+ name: 'InvalidSatoshis',
93
+ message: 'Output satoshis are invalid'
94
+ }, {
95
+ name: 'FeeError',
96
+ message: 'Internal Error on Fee {0}',
97
+ errors: [{
98
+ name: 'TooSmall',
99
+ message: 'Fee is too small: {0}'
100
+ }, {
101
+ name: 'TooLarge',
102
+ message: 'Fee is too large: {0}'
103
+ }, {
104
+ name: 'Different',
105
+ message: 'Unspent value is different from specified fee: {0}'
106
+ }]
107
+ }, {
108
+ name: 'ChangeAddressMissing',
109
+ message: 'Change address is missing'
110
+ }, {
111
+ name: 'BlockHeightTooHigh',
112
+ message: 'Block Height can be at most 2^32 -1'
113
+ }, {
114
+ name: 'NLockTimeOutOfRange',
115
+ message: 'Block Height can only be between 0 and 499 999 999'
116
+ }, {
117
+ name: 'LockTimeTooEarly',
118
+ message: 'Lock Time can\'t be earlier than UNIX date 500 000 000'
119
+ }]
120
+ }, {
121
+ name: 'Script',
122
+ message: 'Internal Error on Script {0}',
123
+ errors: [{
124
+ name: 'UnrecognizedAddress',
125
+ message: 'Expected argument {0} to be an address'
126
+ }, {
127
+ name: 'CantDeriveAddress',
128
+ message: 'Can\'t derive address associated with script {0}, needs to be p2pkh in, p2pkh out, p2sh in, or p2sh out.'
129
+ }, {
130
+ name: 'InvalidBuffer',
131
+ message: 'Invalid script buffer: can\'t parse valid script from given buffer {0}'
132
+ }]
133
+ }, {
134
+ name: 'HDPrivateKey',
135
+ message: 'Internal Error on HDPrivateKey {0}',
136
+ errors: [{
137
+ name: 'InvalidDerivationArgument',
138
+ message: 'Invalid derivation argument {0}, expected string, or number and boolean'
139
+ }, {
140
+ name: 'InvalidEntropyArgument',
141
+ message: 'Invalid entropy: must be an hexa string or binary buffer, got {0}',
142
+ errors: [{
143
+ name: 'TooMuchEntropy',
144
+ message: 'Invalid entropy: more than 512 bits is non standard, got "{0}"'
145
+ }, {
146
+ name: 'NotEnoughEntropy',
147
+ message: 'Invalid entropy: at least 128 bits needed, got "{0}"'
148
+ }]
149
+ }, {
150
+ name: 'InvalidLength',
151
+ message: 'Invalid length for xprivkey string in {0}'
152
+ }, {
153
+ name: 'InvalidPath',
154
+ message: 'Invalid derivation path: {0}'
155
+ }, {
156
+ name: 'UnrecognizedArgument',
157
+ message: 'Invalid argument: creating a HDPrivateKey requires a string, buffer, json or object, got "{0}"'
158
+ }]
159
+ }, {
160
+ name: 'HDPublicKey',
161
+ message: 'Internal Error on HDPublicKey {0}',
162
+ errors: [{
163
+ name: 'ArgumentIsPrivateExtended',
164
+ message: 'Argument is an extended private key: {0}'
165
+ }, {
166
+ name: 'InvalidDerivationArgument',
167
+ message: 'Invalid derivation argument: got {0}'
168
+ }, {
169
+ name: 'InvalidLength',
170
+ message: 'Invalid length for xpubkey: got "{0}"'
171
+ }, {
172
+ name: 'InvalidPath',
173
+ message: 'Invalid derivation path, it should look like: "m/1/100", got "{0}"'
174
+ }, {
175
+ name: 'InvalidIndexCantDeriveHardened',
176
+ message: 'Invalid argument: creating a hardened path requires an HDPrivateKey'
177
+ }, {
178
+ name: 'MustSupplyArgument',
179
+ message: 'Must supply an argument to create a HDPublicKey'
180
+ }, {
181
+ name: 'UnrecognizedArgument',
182
+ message: 'Invalid argument for creation, must be string, json, buffer, or object'
183
+ }]
184
+ }]