@onekeyfe/kaspa-core-lib 1.0.0-alpha.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.
- package/LICENSE +32 -0
- package/README.md +59 -0
- package/index.d.ts +410 -0
- package/index.js +93 -0
- package/lib/address.js +792 -0
- package/lib/block/block.js +281 -0
- package/lib/block/blockheader.js +296 -0
- package/lib/block/index.js +4 -0
- package/lib/block/merkleblock.js +313 -0
- package/lib/crypto/bn.js +218 -0
- package/lib/crypto/ecdsa.js +296 -0
- package/lib/crypto/hash.js +85 -0
- package/lib/crypto/point.js +166 -0
- package/lib/crypto/random.js +57 -0
- package/lib/crypto/schnorr.js +287 -0
- package/lib/crypto/signature.js +441 -0
- package/lib/encoding/base58.js +70 -0
- package/lib/encoding/base58check.js +95 -0
- package/lib/encoding/bufferreader.js +202 -0
- package/lib/encoding/bufferwriter.js +155 -0
- package/lib/encoding/varint.js +72 -0
- package/lib/errors/index.js +61 -0
- package/lib/errors/spec.js +191 -0
- package/lib/hdprivatekey.js +646 -0
- package/lib/hdpublickey.js +496 -0
- package/lib/networks.js +277 -0
- package/lib/opcode.js +261 -0
- package/lib/privatekey.js +431 -0
- package/lib/publickey.js +375 -0
- package/lib/script/index.js +3 -0
- package/lib/script/interpreter.js +2107 -0
- package/lib/script/script.js +1163 -0
- package/lib/transaction/index.js +7 -0
- package/lib/transaction/input/index.js +6 -0
- package/lib/transaction/input/input.js +375 -0
- package/lib/transaction/input/multisig.js +221 -0
- package/lib/transaction/input/multisigscripthash.js +178 -0
- package/lib/transaction/input/publickey.js +91 -0
- package/lib/transaction/input/publickeyhash.js +100 -0
- package/lib/transaction/output.js +188 -0
- package/lib/transaction/sighash.js +534 -0
- package/lib/transaction/signature.js +89 -0
- package/lib/transaction/transaction.js +1384 -0
- package/lib/transaction/unspentoutput.js +109 -0
- package/lib/unit.js +238 -0
- package/lib/uri.js +223 -0
- package/lib/util/base32.js +63 -0
- package/lib/util/buffer.js +162 -0
- package/lib/util/convertBits.js +62 -0
- package/lib/util/js.js +110 -0
- package/lib/util/log.js +10 -0
- package/lib/util/preconditions.js +34 -0
- package/package.json +62 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
var $ = require('../util/preconditions');
|
|
5
|
+
var JSUtil = require('../util/js');
|
|
6
|
+
|
|
7
|
+
var Script = require('../script');
|
|
8
|
+
var Address = require('../address');
|
|
9
|
+
var Unit = require('../unit');
|
|
10
|
+
var BN = require('../crypto/bn');
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Represents an unspent output information: its script, associated amount and address,
|
|
14
|
+
* transaction id and output index.
|
|
15
|
+
*
|
|
16
|
+
* @constructor
|
|
17
|
+
* @param {object} data
|
|
18
|
+
* @param {string} data.txid the previous transaction id
|
|
19
|
+
* @param {string=} data.txId alias for `txid`
|
|
20
|
+
* @param {number} data.vout the index in the transaction
|
|
21
|
+
* @param {number=} data.outputIndex alias for `vout`
|
|
22
|
+
* @param {string|Script} data.scriptPubKey the script that must be resolved to release the funds
|
|
23
|
+
* @param {string|Script=} data.script alias for `scriptPubKey`
|
|
24
|
+
* @param {number} data.amount amount of bitcoins associated
|
|
25
|
+
* @param {string|number=} data.satoshis alias for `amount`, but expressed in satoshis (1 BTC = 1e8 satoshis)
|
|
26
|
+
* @param {string|Address=} data.address the associated address to the script, if provided
|
|
27
|
+
*/
|
|
28
|
+
function UnspentOutput(data) {
|
|
29
|
+
/* jshint maxcomplexity: 20 */
|
|
30
|
+
/* jshint maxstatements: 20 */
|
|
31
|
+
if (!(this instanceof UnspentOutput)) {
|
|
32
|
+
return new UnspentOutput(data);
|
|
33
|
+
}
|
|
34
|
+
$.checkArgument(_.isObject(data), 'Must provide an object from where to extract data');
|
|
35
|
+
var address = data.address ? new Address(data.address) : undefined;
|
|
36
|
+
var txId = data.txid ? data.txid : data.txId;
|
|
37
|
+
if (!txId || !JSUtil.isHexaString(txId) || txId.length > 64) {
|
|
38
|
+
// TODO: Use the errors library
|
|
39
|
+
throw new Error('Invalid TXID in object', data);
|
|
40
|
+
}
|
|
41
|
+
var outputIndex = _.isUndefined(data.vout) ? data.outputIndex : data.vout;
|
|
42
|
+
if (!_.isNumber(outputIndex)) {
|
|
43
|
+
throw new Error('Invalid outputIndex, received ' + outputIndex);
|
|
44
|
+
}
|
|
45
|
+
$.checkArgument(!_.isUndefined(data.scriptPubKey) || !_.isUndefined(data.script),
|
|
46
|
+
'Must provide the scriptPubKey for that output!');
|
|
47
|
+
var script = new Script(data.scriptPubKey || data.script);
|
|
48
|
+
$.checkArgument(!_.isUndefined(data.amount) || !_.isUndefined(data.satoshis),
|
|
49
|
+
'Must provide an amount for the output');
|
|
50
|
+
|
|
51
|
+
var amount;
|
|
52
|
+
if (!_.isUndefined(data.amount)) {
|
|
53
|
+
amount = BN.fromNumber(data.amount);
|
|
54
|
+
amount = amount.mul(new BN(1e8)).toString();
|
|
55
|
+
} else {
|
|
56
|
+
amount = BN.fromNumber(data.satoshis).toString();
|
|
57
|
+
}
|
|
58
|
+
$.checkArgument(!amount.includes('.'), 'Amount must be a number');
|
|
59
|
+
|
|
60
|
+
JSUtil.defineImmutable(this, {
|
|
61
|
+
address: address,
|
|
62
|
+
txId: txId,
|
|
63
|
+
outputIndex: outputIndex,
|
|
64
|
+
script: script,
|
|
65
|
+
satoshis: amount.toString()
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Provide an informative output when displaying this object in the console
|
|
71
|
+
* @returns string
|
|
72
|
+
*/
|
|
73
|
+
UnspentOutput.prototype.inspect = function() {
|
|
74
|
+
return '<UnspentOutput: ' + this.txId + ':' + this.outputIndex +
|
|
75
|
+
', satoshis: ' + this.satoshis + ', address: ' + this.address + '>';
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* String representation: just "txid:index"
|
|
80
|
+
* @returns string
|
|
81
|
+
*/
|
|
82
|
+
UnspentOutput.prototype.toString = function() {
|
|
83
|
+
return this.txId + ':' + this.outputIndex;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Deserialize an UnspentOutput from an object
|
|
88
|
+
* @param {object|string} data
|
|
89
|
+
* @return UnspentOutput
|
|
90
|
+
*/
|
|
91
|
+
UnspentOutput.fromObject = function(data) {
|
|
92
|
+
return new UnspentOutput(data);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Returns a plain object (no prototype or methods) with the associated info for this output
|
|
97
|
+
* @return {object}
|
|
98
|
+
*/
|
|
99
|
+
UnspentOutput.prototype.toObject = UnspentOutput.prototype.toJSON = function toObject() {
|
|
100
|
+
return {
|
|
101
|
+
address: this.address ? this.address.toString() : undefined,
|
|
102
|
+
txid: this.txId,
|
|
103
|
+
vout: this.outputIndex,
|
|
104
|
+
scriptPubKey: this.script.toBuffer().toString('hex'),
|
|
105
|
+
amount: Unit.fromSatoshis(this.satoshis).toBTC()
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
module.exports = UnspentOutput;
|
package/lib/unit.js
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
|
|
5
|
+
var errors = require('./errors');
|
|
6
|
+
var $ = require('./util/preconditions');
|
|
7
|
+
|
|
8
|
+
var UNITS = {
|
|
9
|
+
'BTC' : [1e8, 8],
|
|
10
|
+
'mBTC' : [1e5, 5],
|
|
11
|
+
'uBTC' : [1e2, 2],
|
|
12
|
+
'bits' : [1e2, 2],
|
|
13
|
+
'satoshis' : [1, 0]
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Utility for handling and converting bitcoins units. The supported units are
|
|
18
|
+
* BTC, mBTC, bits (also named uBTC) and satoshis. A unit instance can be created with an
|
|
19
|
+
* amount and a unit code, or alternatively using static methods like {fromBTC}.
|
|
20
|
+
* It also allows to be created from a fiat amount and the exchange rate, or
|
|
21
|
+
* alternatively using the {fromFiat} static method.
|
|
22
|
+
* You can consult for different representation of a unit instance using it's
|
|
23
|
+
* {to} method, the fixed unit methods like {toSatoshis} or alternatively using
|
|
24
|
+
* the unit accessors. It also can be converted to a fiat amount by providing the
|
|
25
|
+
* corresponding BTC/fiat exchange rate.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```javascript
|
|
29
|
+
* var sats = Unit.fromBTC(1.3).toSatoshis();
|
|
30
|
+
* var mili = Unit.fromBits(1.3).to(Unit.mBTC);
|
|
31
|
+
* var bits = Unit.fromFiat(1.3, 350).bits;
|
|
32
|
+
* var btc = new Unit(1.3, Unit.bits).BTC;
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @param {Number} amount - The amount to be represented
|
|
36
|
+
* @param {String|Number} code - The unit of the amount or the exchange rate
|
|
37
|
+
* @returns {Unit} A new instance of an Unit
|
|
38
|
+
* @constructor
|
|
39
|
+
*/
|
|
40
|
+
function Unit(amount, code) {
|
|
41
|
+
if (!(this instanceof Unit)) {
|
|
42
|
+
return new Unit(amount, code);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// convert fiat to BTC
|
|
46
|
+
if (_.isNumber(code)) {
|
|
47
|
+
if (code <= 0) {
|
|
48
|
+
throw new errors.Unit.InvalidRate(code);
|
|
49
|
+
}
|
|
50
|
+
amount = amount / code;
|
|
51
|
+
code = Unit.BTC;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
this._value = this._from(amount, code);
|
|
55
|
+
|
|
56
|
+
var self = this;
|
|
57
|
+
var defineAccesor = function(key) {
|
|
58
|
+
Object.defineProperty(self, key, {
|
|
59
|
+
get: function() { return self.to(key); },
|
|
60
|
+
enumerable: true,
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
Object.keys(UNITS).forEach(defineAccesor);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
Object.keys(UNITS).forEach(function(key) {
|
|
68
|
+
Unit[key] = key;
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Returns a Unit instance created from JSON string or object
|
|
73
|
+
*
|
|
74
|
+
* @param {String|Object} json - JSON with keys: amount and code
|
|
75
|
+
* @returns {Unit} A Unit instance
|
|
76
|
+
*/
|
|
77
|
+
Unit.fromObject = function fromObject(data){
|
|
78
|
+
$.checkArgument(_.isObject(data), 'Argument is expected to be an object');
|
|
79
|
+
return new Unit(data.amount, data.code);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Returns a Unit instance created from an amount in BTC
|
|
84
|
+
*
|
|
85
|
+
* @param {Number} amount - The amount in BTC
|
|
86
|
+
* @returns {Unit} A Unit instance
|
|
87
|
+
*/
|
|
88
|
+
Unit.fromBTC = function(amount) {
|
|
89
|
+
return new Unit(amount, Unit.BTC);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Returns a Unit instance created from an amount in mBTC
|
|
94
|
+
*
|
|
95
|
+
* @param {Number} amount - The amount in mBTC
|
|
96
|
+
* @returns {Unit} A Unit instance
|
|
97
|
+
*/
|
|
98
|
+
Unit.fromMillis = Unit.fromMilis = function(amount) {
|
|
99
|
+
return new Unit(amount, Unit.mBTC);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Returns a Unit instance created from an amount in bits
|
|
104
|
+
*
|
|
105
|
+
* @param {Number} amount - The amount in bits
|
|
106
|
+
* @returns {Unit} A Unit instance
|
|
107
|
+
*/
|
|
108
|
+
Unit.fromMicros = Unit.fromBits = function(amount) {
|
|
109
|
+
return new Unit(amount, Unit.bits);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Returns a Unit instance created from an amount in satoshis
|
|
114
|
+
*
|
|
115
|
+
* @param {Number} amount - The amount in satoshis
|
|
116
|
+
* @returns {Unit} A Unit instance
|
|
117
|
+
*/
|
|
118
|
+
Unit.fromSatoshis = function(amount) {
|
|
119
|
+
return new Unit(amount, Unit.satoshis);
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Returns a Unit instance created from a fiat amount and exchange rate.
|
|
124
|
+
*
|
|
125
|
+
* @param {Number} amount - The amount in fiat
|
|
126
|
+
* @param {Number} rate - The exchange rate BTC/fiat
|
|
127
|
+
* @returns {Unit} A Unit instance
|
|
128
|
+
*/
|
|
129
|
+
Unit.fromFiat = function(amount, rate) {
|
|
130
|
+
return new Unit(amount, rate);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
Unit.prototype._from = function(amount, code) {
|
|
134
|
+
if (!UNITS[code]) {
|
|
135
|
+
throw new errors.Unit.UnknownCode(code);
|
|
136
|
+
}
|
|
137
|
+
return parseInt((amount * UNITS[code][0]).toFixed());
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Returns the value represented in the specified unit
|
|
142
|
+
*
|
|
143
|
+
* @param {String|Number} code - The unit code or exchange rate
|
|
144
|
+
* @returns {Number} The converted value
|
|
145
|
+
*/
|
|
146
|
+
Unit.prototype.to = function(code) {
|
|
147
|
+
if (_.isNumber(code)) {
|
|
148
|
+
if (code <= 0) {
|
|
149
|
+
throw new errors.Unit.InvalidRate(code);
|
|
150
|
+
}
|
|
151
|
+
return parseFloat((this.BTC * code).toFixed(2));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (!UNITS[code]) {
|
|
155
|
+
throw new errors.Unit.UnknownCode(code);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
var value = this._value / UNITS[code][0];
|
|
159
|
+
return parseFloat(value.toFixed(UNITS[code][1]));
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Returns the value represented in BTC
|
|
164
|
+
*
|
|
165
|
+
* @returns {Number} The value converted to BTC
|
|
166
|
+
*/
|
|
167
|
+
Unit.prototype.toBTC = function() {
|
|
168
|
+
return this.to(Unit.BTC);
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Returns the value represented in mBTC
|
|
173
|
+
*
|
|
174
|
+
* @returns {Number} The value converted to mBTC
|
|
175
|
+
*/
|
|
176
|
+
Unit.prototype.toMillis = Unit.prototype.toMilis = function() {
|
|
177
|
+
return this.to(Unit.mBTC);
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Returns the value represented in bits
|
|
182
|
+
*
|
|
183
|
+
* @returns {Number} The value converted to bits
|
|
184
|
+
*/
|
|
185
|
+
Unit.prototype.toMicros = Unit.prototype.toBits = function() {
|
|
186
|
+
return this.to(Unit.bits);
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Returns the value represented in satoshis
|
|
191
|
+
*
|
|
192
|
+
* @returns {Number} The value converted to satoshis
|
|
193
|
+
*/
|
|
194
|
+
Unit.prototype.toSatoshis = function() {
|
|
195
|
+
return this.to(Unit.satoshis);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Returns the value represented in fiat
|
|
200
|
+
*
|
|
201
|
+
* @param {string} rate - The exchange rate between BTC/currency
|
|
202
|
+
* @returns {Number} The value converted to satoshis
|
|
203
|
+
*/
|
|
204
|
+
Unit.prototype.atRate = function(rate) {
|
|
205
|
+
return this.to(rate);
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Returns a the string representation of the value in satoshis
|
|
210
|
+
*
|
|
211
|
+
* @returns {string} the value in satoshis
|
|
212
|
+
*/
|
|
213
|
+
Unit.prototype.toString = function() {
|
|
214
|
+
return this.satoshis + ' satoshis';
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Returns a plain object representation of the Unit
|
|
219
|
+
*
|
|
220
|
+
* @returns {Object} An object with the keys: amount and code
|
|
221
|
+
*/
|
|
222
|
+
Unit.prototype.toObject = Unit.prototype.toJSON = function toObject() {
|
|
223
|
+
return {
|
|
224
|
+
amount: this.BTC,
|
|
225
|
+
code: Unit.BTC
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Returns a string formatted for the console
|
|
231
|
+
*
|
|
232
|
+
* @returns {string} the value in satoshis
|
|
233
|
+
*/
|
|
234
|
+
Unit.prototype.inspect = function() {
|
|
235
|
+
return '<Unit: ' + this.toString() + '>';
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
module.exports = Unit;
|
package/lib/uri.js
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
var URL = require('url');
|
|
5
|
+
|
|
6
|
+
var Address = require('./address');
|
|
7
|
+
var Networks = require('./networks');
|
|
8
|
+
var Unit = require('./unit');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Kaspacore URI
|
|
12
|
+
*
|
|
13
|
+
* Instantiate an URI from a bitcoin URI String or an Object. An URI instance
|
|
14
|
+
* can be created with a bitcoin uri string or an object. All instances of
|
|
15
|
+
* URI are valid, the static method isValid allows checking before instantiation.
|
|
16
|
+
*
|
|
17
|
+
* All standard parameters can be found as members of the class, the address
|
|
18
|
+
* is represented using an {Address} instance and the amount is represented in
|
|
19
|
+
* satoshis. Any other non-standard parameters can be found under the extra member.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```javascript
|
|
23
|
+
*
|
|
24
|
+
* var uri = new URI('bitcoin:12A1MyfXbW6RhdRAZEqofac5jCQQjwEPBu?amount=1.2');
|
|
25
|
+
* console.log(uri.address, uri.amount);
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @param {string|Object} data - A bitcoin URI string or an Object
|
|
29
|
+
* @param {Array.<string>=} knownParams - Required non-standard params
|
|
30
|
+
* @throws {TypeError} Invalid bitcoin address
|
|
31
|
+
* @throws {TypeError} Invalid amount
|
|
32
|
+
* @throws {Error} Unknown required argument
|
|
33
|
+
* @returns {URI} A new valid and frozen instance of URI
|
|
34
|
+
* @constructor
|
|
35
|
+
*/
|
|
36
|
+
var URI = function(data, knownParams) {
|
|
37
|
+
if (!(this instanceof URI)) {
|
|
38
|
+
return new URI(data, knownParams);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
this.extras = {};
|
|
42
|
+
this.knownParams = knownParams || [];
|
|
43
|
+
this.address = this.network = this.amount = this.message = null;
|
|
44
|
+
|
|
45
|
+
if (typeof(data) === 'string') {
|
|
46
|
+
var params = URI.parse(data);
|
|
47
|
+
if (params.amount) {
|
|
48
|
+
params.amount = this._parseAmount(params.amount);
|
|
49
|
+
}
|
|
50
|
+
this._fromObject(params);
|
|
51
|
+
} else if (typeof(data) === 'object') {
|
|
52
|
+
this._fromObject(data);
|
|
53
|
+
} else {
|
|
54
|
+
throw new TypeError('Unrecognized data format.');
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Instantiate a URI from a String
|
|
60
|
+
*
|
|
61
|
+
* @param {string} str - JSON string or object of the URI
|
|
62
|
+
* @returns {URI} A new instance of a URI
|
|
63
|
+
*/
|
|
64
|
+
URI.fromString = function fromString(str) {
|
|
65
|
+
if (typeof(str) !== 'string') {
|
|
66
|
+
throw new TypeError('Expected a string');
|
|
67
|
+
}
|
|
68
|
+
return new URI(str);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Instantiate a URI from an Object
|
|
73
|
+
*
|
|
74
|
+
* @param {Object} data - object of the URI
|
|
75
|
+
* @returns {URI} A new instance of a URI
|
|
76
|
+
*/
|
|
77
|
+
URI.fromObject = function fromObject(json) {
|
|
78
|
+
return new URI(json);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Check if an bitcoin URI string is valid
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```javascript
|
|
86
|
+
*
|
|
87
|
+
* var valid = URI.isValid('bitcoin:12A1MyfXbW6RhdRAZEqofac5jCQQjwEPBu');
|
|
88
|
+
* // true
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @param {string|Object} data - A bitcoin URI string or an Object
|
|
92
|
+
* @param {Array.<string>=} knownParams - Required non-standard params
|
|
93
|
+
* @returns {boolean} Result of uri validation
|
|
94
|
+
*/
|
|
95
|
+
URI.isValid = function(arg, knownParams) {
|
|
96
|
+
try {
|
|
97
|
+
new URI(arg, knownParams);
|
|
98
|
+
} catch (err) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
return true;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Convert a bitcoin URI string into a simple object.
|
|
106
|
+
*
|
|
107
|
+
* @param {string} uri - A bitcoin URI string
|
|
108
|
+
* @throws {TypeError} Invalid bitcoin URI
|
|
109
|
+
* @returns {Object} An object with the parsed params
|
|
110
|
+
*/
|
|
111
|
+
URI.parse = function(uri) {
|
|
112
|
+
var info = URL.parse(uri, true);
|
|
113
|
+
if (Networks.get( info.protocol.replace(':', '') ,'prefix')) {
|
|
114
|
+
// workaround to host insensitiveness
|
|
115
|
+
var group = /[^:]*:\/?\/?([^?]*)/.exec(uri);
|
|
116
|
+
info.query.address = group && group[1] || undefined;
|
|
117
|
+
|
|
118
|
+
return info.query;
|
|
119
|
+
} else {
|
|
120
|
+
throw new TypeError('Invalid bitcoin URI');
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
URI.Members = ['address', 'amount', 'message', 'label', 'r'];
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Internal function to load the URI instance with an object.
|
|
128
|
+
*
|
|
129
|
+
* @param {Object} obj - Object with the information
|
|
130
|
+
* @throws {TypeError} Invalid bitcoin address
|
|
131
|
+
* @throws {TypeError} Invalid amount
|
|
132
|
+
* @throws {Error} Unknown required argument
|
|
133
|
+
*/
|
|
134
|
+
URI.prototype._fromObject = function(obj) {
|
|
135
|
+
/* jshint maxcomplexity: 10 */
|
|
136
|
+
|
|
137
|
+
if (!Address.isValid(obj.address)) {
|
|
138
|
+
throw new TypeError('Invalid bitcoin address');
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
this.address = new Address(obj.address);
|
|
142
|
+
this.network = this.address.network;
|
|
143
|
+
this.amount = obj.amount;
|
|
144
|
+
|
|
145
|
+
for (var key in obj) {
|
|
146
|
+
if (key === 'address' || key === 'amount') {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (/^req-/.exec(key) && this.knownParams.indexOf(key) === -1) {
|
|
151
|
+
throw Error('Unknown required argument ' + key);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
var destination = URI.Members.indexOf(key) > -1 ? this : this.extras;
|
|
155
|
+
destination[key] = obj[key];
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Internal function to transform a BTC string amount into satoshis
|
|
161
|
+
*
|
|
162
|
+
* @param {string} amount - Amount BTC string
|
|
163
|
+
* @throws {TypeError} Invalid amount
|
|
164
|
+
* @returns {Object} Amount represented in satoshis
|
|
165
|
+
*/
|
|
166
|
+
URI.prototype._parseAmount = function(amount) {
|
|
167
|
+
amount = Number(amount);
|
|
168
|
+
if (isNaN(amount)) {
|
|
169
|
+
throw new TypeError('Invalid amount');
|
|
170
|
+
}
|
|
171
|
+
return Unit.fromBTC(amount).toSatoshis();
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
URI.prototype.toObject = URI.prototype.toJSON = function toObject() {
|
|
175
|
+
var json = {};
|
|
176
|
+
for (var i = 0; i < URI.Members.length; i++) {
|
|
177
|
+
var m = URI.Members[i];
|
|
178
|
+
if (this.hasOwnProperty(m) && typeof(this[m]) !== 'undefined') {
|
|
179
|
+
json[m] = this[m].toString();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
_.extend(json, this.extras);
|
|
183
|
+
return json;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Will return a the string representation of the URI
|
|
188
|
+
*
|
|
189
|
+
* @returns {string} Bitcoin URI string
|
|
190
|
+
*/
|
|
191
|
+
URI.prototype.toString = function() {
|
|
192
|
+
var query = {};
|
|
193
|
+
if (this.amount) {
|
|
194
|
+
query.amount = Unit.fromSatoshis(this.amount).toBTC();
|
|
195
|
+
}
|
|
196
|
+
if (this.message) {
|
|
197
|
+
query.message = this.message;
|
|
198
|
+
}
|
|
199
|
+
if (this.label) {
|
|
200
|
+
query.label = this.label;
|
|
201
|
+
}
|
|
202
|
+
if (this.r) {
|
|
203
|
+
query.r = this.r;
|
|
204
|
+
}
|
|
205
|
+
_.extend(query, this.extras);
|
|
206
|
+
|
|
207
|
+
return URL.format({
|
|
208
|
+
protocol: Networks.get(this.network ,'name').prefix + ':',
|
|
209
|
+
host: this.address.toString(true),
|
|
210
|
+
query: query
|
|
211
|
+
});
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Will return a string formatted for the console
|
|
216
|
+
*
|
|
217
|
+
* @returns {string} Bitcoin URI
|
|
218
|
+
*/
|
|
219
|
+
URI.prototype.inspect = function() {
|
|
220
|
+
return '<URI: ' + this.toString() + '>';
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
module.exports = URI;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/***
|
|
3
|
+
* https://github.com/bitcoincashjs/cashaddr
|
|
4
|
+
* Copyright (c) 2018 Matias Alejo Garcia
|
|
5
|
+
* Copyright (c) 2017 Emilio Almansi
|
|
6
|
+
* Distributed under the MIT software license, see the accompanying
|
|
7
|
+
* file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
var $ = require('./preconditions');
|
|
12
|
+
|
|
13
|
+
/***
|
|
14
|
+
* Charset containing the 32 symbols used in the base32 encoding.
|
|
15
|
+
*/
|
|
16
|
+
var CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
|
|
17
|
+
|
|
18
|
+
/***
|
|
19
|
+
* Inverted index mapping each symbol into its index within the charset.
|
|
20
|
+
*/
|
|
21
|
+
var CHARSET_INVERSE_INDEX = {
|
|
22
|
+
'q': 0, 'p': 1, 'z': 2, 'r': 3, 'y': 4, '9': 5, 'x': 6, '8': 7,
|
|
23
|
+
'g': 8, 'f': 9, '2': 10, 't': 11, 'v': 12, 'd': 13, 'w': 14, '0': 15,
|
|
24
|
+
's': 16, '3': 17, 'j': 18, 'n': 19, '5': 20, '4': 21, 'k': 22, 'h': 23,
|
|
25
|
+
'c': 24, 'e': 25, '6': 26, 'm': 27, 'u': 28, 'a': 29, '7': 30, 'l': 31,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/***
|
|
29
|
+
* Encodes the given array of 5-bit integers as a base32-encoded string.
|
|
30
|
+
*
|
|
31
|
+
* @param {Array} data Array of integers between 0 and 31 inclusive.
|
|
32
|
+
*/
|
|
33
|
+
function encode(data) {
|
|
34
|
+
$.checkArgument(data instanceof Array, 'Must be Array');
|
|
35
|
+
var base32 = '';
|
|
36
|
+
for (var i=0; i<data.length; i++) {
|
|
37
|
+
var value = data[i];
|
|
38
|
+
$.checkArgument(0 <= value && value < 32, 'value ' + value);
|
|
39
|
+
base32 += CHARSET[value];
|
|
40
|
+
}
|
|
41
|
+
return base32;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/***
|
|
45
|
+
* Decodes the given base32-encoded string into an array of 5-bit integers.
|
|
46
|
+
*
|
|
47
|
+
* @param {string} base32
|
|
48
|
+
*/
|
|
49
|
+
function decode(base32) {
|
|
50
|
+
$.checkArgument(typeof base32 === 'string', 'Must be base32-encoded string');
|
|
51
|
+
var data = [];
|
|
52
|
+
for (var i=0; i<base32.length; i++) {
|
|
53
|
+
var value = base32[i];
|
|
54
|
+
$.checkArgument(value in CHARSET_INVERSE_INDEX, 'value '+ value);
|
|
55
|
+
data.push(CHARSET_INVERSE_INDEX[value]);
|
|
56
|
+
}
|
|
57
|
+
return data;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
module.exports = {
|
|
61
|
+
encode: encode,
|
|
62
|
+
decode: decode,
|
|
63
|
+
};
|