@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,162 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var buffer = require('buffer');
|
|
4
|
+
var assert = require('assert');
|
|
5
|
+
|
|
6
|
+
var js = require('./js');
|
|
7
|
+
var $ = require('./preconditions');
|
|
8
|
+
|
|
9
|
+
function equals(a, b) {
|
|
10
|
+
if (a.length !== b.length) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
var length = a.length;
|
|
14
|
+
for (var i = 0; i < length; i++) {
|
|
15
|
+
if (a[i] !== b[i]) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = {
|
|
23
|
+
/**
|
|
24
|
+
* Fill a buffer with a value.
|
|
25
|
+
*
|
|
26
|
+
* @param {Buffer} buffer
|
|
27
|
+
* @param {number} value
|
|
28
|
+
* @return {Buffer}
|
|
29
|
+
*/
|
|
30
|
+
fill: function fill(buffer, value) {
|
|
31
|
+
$.checkArgumentType(buffer, 'Buffer', 'buffer');
|
|
32
|
+
$.checkArgumentType(value, 'number', 'value');
|
|
33
|
+
var length = buffer.length;
|
|
34
|
+
for (var i = 0; i < length; i++) {
|
|
35
|
+
buffer[i] = value;
|
|
36
|
+
}
|
|
37
|
+
return buffer;
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Return a copy of a buffer
|
|
42
|
+
*
|
|
43
|
+
* @param {Buffer} original
|
|
44
|
+
* @return {Buffer}
|
|
45
|
+
*/
|
|
46
|
+
copy: function(original) {
|
|
47
|
+
var buffer = Buffer.alloc(original.length);
|
|
48
|
+
original.copy(buffer);
|
|
49
|
+
return buffer;
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Returns true if the given argument is an instance of a buffer. Tests for
|
|
54
|
+
* both node's Buffer and Uint8Array
|
|
55
|
+
*
|
|
56
|
+
* @param {*} arg
|
|
57
|
+
* @return {boolean}
|
|
58
|
+
*/
|
|
59
|
+
isBuffer: function isBuffer(arg) {
|
|
60
|
+
return buffer.Buffer.isBuffer(arg) || arg instanceof Uint8Array;
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns a zero-filled byte array
|
|
65
|
+
*
|
|
66
|
+
* @param {number} bytes
|
|
67
|
+
* @return {Buffer}
|
|
68
|
+
*/
|
|
69
|
+
emptyBuffer: function emptyBuffer(bytes) {
|
|
70
|
+
$.checkArgumentType(bytes, 'number', 'bytes');
|
|
71
|
+
var result = Buffer.alloc(bytes);
|
|
72
|
+
for (var i = 0; i < bytes; i++) {
|
|
73
|
+
result.write('\0', i);
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Concatenates a buffer
|
|
80
|
+
*
|
|
81
|
+
* Shortcut for <tt>buffer.Buffer.concat</tt>
|
|
82
|
+
*/
|
|
83
|
+
concat: buffer.Buffer.concat,
|
|
84
|
+
|
|
85
|
+
equals: equals,
|
|
86
|
+
equal: equals,
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Transforms a number from 0 to 255 into a Buffer of size 1 with that value
|
|
90
|
+
*
|
|
91
|
+
* @param {number} integer
|
|
92
|
+
* @return {Buffer}
|
|
93
|
+
*/
|
|
94
|
+
integerAsSingleByteBuffer: function integerAsSingleByteBuffer(integer) {
|
|
95
|
+
$.checkArgumentType(integer, 'number', 'integer');
|
|
96
|
+
return Buffer.from([integer & 0xff]);
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Transform a 4-byte integer into a Buffer of length 4.
|
|
101
|
+
*
|
|
102
|
+
* @param {number} integer
|
|
103
|
+
* @return {Buffer}
|
|
104
|
+
*/
|
|
105
|
+
integerAsBuffer: function integerAsBuffer(integer) {
|
|
106
|
+
$.checkArgumentType(integer, 'number', 'integer');
|
|
107
|
+
var bytes = [];
|
|
108
|
+
bytes.push((integer >> 24) & 0xff);
|
|
109
|
+
bytes.push((integer >> 16) & 0xff);
|
|
110
|
+
bytes.push((integer >> 8) & 0xff);
|
|
111
|
+
bytes.push(integer & 0xff);
|
|
112
|
+
return Buffer.from(bytes);
|
|
113
|
+
},
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Transform the first 4 values of a Buffer into a number, in little endian encoding
|
|
117
|
+
*
|
|
118
|
+
* @param {Buffer} buffer
|
|
119
|
+
* @return {number}
|
|
120
|
+
*/
|
|
121
|
+
integerFromBuffer: function integerFromBuffer(buffer) {
|
|
122
|
+
$.checkArgumentType(buffer, 'Buffer', 'buffer');
|
|
123
|
+
return buffer[0] << 24 | buffer[1] << 16 | buffer[2] << 8 | buffer[3];
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Transforms the first byte of an array into a number ranging from -128 to 127
|
|
128
|
+
* @param {Buffer} buffer
|
|
129
|
+
* @return {number}
|
|
130
|
+
*/
|
|
131
|
+
integerFromSingleByteBuffer: function integerFromBuffer(buffer) {
|
|
132
|
+
$.checkArgumentType(buffer, 'Buffer', 'buffer');
|
|
133
|
+
return buffer[0];
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Transforms a buffer into a string with a number in hexa representation
|
|
138
|
+
*
|
|
139
|
+
* Shorthand for <tt>buffer.toString('hex')</tt>
|
|
140
|
+
*
|
|
141
|
+
* @param {Buffer} buffer
|
|
142
|
+
* @return {string}
|
|
143
|
+
*/
|
|
144
|
+
bufferToHex: function bufferToHex(buffer) {
|
|
145
|
+
$.checkArgumentType(buffer, 'Buffer', 'buffer');
|
|
146
|
+
return buffer.toString('hex');
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Reverse a buffer
|
|
151
|
+
* @param {Buffer} param
|
|
152
|
+
* @return {Buffer}
|
|
153
|
+
*/
|
|
154
|
+
reverse: function reverse(param) {
|
|
155
|
+
return (Buffer.from(param)).reverse();
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
module.exports.NULL_HASH = module.exports.fill(Buffer.alloc(32), 0);
|
|
162
|
+
module.exports.EMPTY_BUFFER = Buffer.alloc(0);
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Copyright (c) 2018 Matias Alejo Garcia
|
|
3
|
+
// Copyright (c) 2017 Emilio Almansi
|
|
4
|
+
// Copyright (c) 2017 Pieter Wuille
|
|
5
|
+
//
|
|
6
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
// of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
// in the Software without restriction, including without limitation the rights
|
|
9
|
+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
// copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
// furnished to do so, subject to the following conditions:
|
|
12
|
+
//
|
|
13
|
+
// The above copyright notice and this permission notice shall be included in
|
|
14
|
+
// all copies or substantial portions of the Software.
|
|
15
|
+
//
|
|
16
|
+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
22
|
+
// THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Converts an array of integers made up of `from` bits into an
|
|
26
|
+
* array of integers made up of `to` bits. The output array is
|
|
27
|
+
* zero-padded if necessary, unless strict mode is true.
|
|
28
|
+
* Original by Pieter Wuille: https://github.com/sipa/bech32.
|
|
29
|
+
*
|
|
30
|
+
* @param {Array} data Array of integers made up of `from` bits.
|
|
31
|
+
* @param {number} from Length in bits of elements in the input array.
|
|
32
|
+
* @param {number} to Length in bits of elements in the output array.
|
|
33
|
+
* @param {bool} strict Require the conversion to be completed without padding.
|
|
34
|
+
*/
|
|
35
|
+
var $ = require('./preconditions');
|
|
36
|
+
|
|
37
|
+
module.exports = function(data, from, to, strict){
|
|
38
|
+
strict = strict || false;
|
|
39
|
+
var accumulator = 0;
|
|
40
|
+
var bits = 0;
|
|
41
|
+
var result = [];
|
|
42
|
+
var mask = (1 << to) - 1;
|
|
43
|
+
for (var i=0; i<data.length; i++) {
|
|
44
|
+
var value = data[i];
|
|
45
|
+
$.checkArgument(!(value < 0 || (value >> from) !== 0), 'value ' + value);
|
|
46
|
+
|
|
47
|
+
accumulator = (accumulator << from) | value;
|
|
48
|
+
bits += from;
|
|
49
|
+
while (bits >= to) {
|
|
50
|
+
bits -= to;
|
|
51
|
+
result.push((accumulator >> bits) & mask);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (!strict) {
|
|
55
|
+
if (bits > 0) {
|
|
56
|
+
result.push((accumulator << (to - bits)) & mask);
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
$.checkState(!(bits >= from || ((accumulator << (to - bits)) & mask)), 'Conversion requires padding but strict mode was used');
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
};
|
package/lib/util/js.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('lodash');
|
|
4
|
+
var logLevels = require('./log').logLevels;
|
|
5
|
+
var BN = require('../crypto/bn');
|
|
6
|
+
|
|
7
|
+
const UINT64_MAX = new BN('18446744073709551615');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Determines whether a string contains only hexadecimal values
|
|
11
|
+
*
|
|
12
|
+
* @name JSUtil.isHexa
|
|
13
|
+
* @param {string} value
|
|
14
|
+
* @return {boolean} true if the string is the hexa representation of a number
|
|
15
|
+
*/
|
|
16
|
+
var isHexa = function isHexa(value) {
|
|
17
|
+
if (!_.isString(value)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
return /^[0-9a-fA-F]+$/.test(value);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @namespace JSUtil
|
|
25
|
+
*/
|
|
26
|
+
const JSUtil = {
|
|
27
|
+
debugLevel:0,
|
|
28
|
+
LogBufferActive: false,
|
|
29
|
+
LogBuffer(...args) {
|
|
30
|
+
if (!JSUtil.LogBufferActive || JSUtil.debugLevel < logLevels.debug)
|
|
31
|
+
return
|
|
32
|
+
args[args.length - 1] = args[args.length - 1].map(buf => buf.toString("hex"));
|
|
33
|
+
console.log(...args)
|
|
34
|
+
},
|
|
35
|
+
/**
|
|
36
|
+
* Test if an argument is a valid JSON object. If it is, returns a truthy
|
|
37
|
+
* value (the json object decoded), so no double JSON.parse call is necessary
|
|
38
|
+
*
|
|
39
|
+
* @param {string} arg
|
|
40
|
+
* @return {Object|boolean} false if the argument is not a JSON string.
|
|
41
|
+
*/
|
|
42
|
+
isValidJSON: function isValidJSON(arg) {
|
|
43
|
+
var parsed;
|
|
44
|
+
if (!_.isString(arg)) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
parsed = JSON.parse(arg);
|
|
49
|
+
} catch (e) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
if (typeof(parsed) === 'object') {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
},
|
|
57
|
+
isHexa: isHexa,
|
|
58
|
+
isHexaString: isHexa,
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Clone an array
|
|
62
|
+
*/
|
|
63
|
+
cloneArray: function(array) {
|
|
64
|
+
return [].concat(array);
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Define immutable properties on a target object
|
|
69
|
+
*
|
|
70
|
+
* @param {Object} target - An object to be extended
|
|
71
|
+
* @param {Object} values - An object of properties
|
|
72
|
+
* @return {Object} The target object
|
|
73
|
+
*/
|
|
74
|
+
defineImmutable: function defineImmutable(target, values) {
|
|
75
|
+
Object.keys(values).forEach(function(key) {
|
|
76
|
+
Object.defineProperty(target, key, {
|
|
77
|
+
configurable: false,
|
|
78
|
+
enumerable: true,
|
|
79
|
+
value: values[key]
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
return target;
|
|
83
|
+
},
|
|
84
|
+
/**
|
|
85
|
+
* Checks that a value is a natural number, a positive integer or zero.
|
|
86
|
+
*
|
|
87
|
+
* @param {*} value
|
|
88
|
+
* @return {Boolean}
|
|
89
|
+
*/
|
|
90
|
+
isNaturalNumber: function isNaturalNumber(value) {
|
|
91
|
+
if (typeof value === 'string') {
|
|
92
|
+
if (!/^\d*$/.test(value)) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if(typeof value === 'number' || typeof value === 'string'){
|
|
98
|
+
try {
|
|
99
|
+
var bnValue = BN.fromNumber(value);
|
|
100
|
+
return bnValue.lte(UINT64_MAX);
|
|
101
|
+
} catch (e) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
module.exports = JSUtil;
|
package/lib/util/log.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var errors = require('../errors');
|
|
4
|
+
var _ = require('lodash');
|
|
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) {
|
|
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
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@onekeyfe/kaspa-core-lib",
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"authors": [
|
|
6
|
+
{
|
|
7
|
+
"name": "Onekey",
|
|
8
|
+
"email": "dev@onekey.so"
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"name": "BitPay",
|
|
12
|
+
"email": "dev@bitpay.com"
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"name": "ASPECTRON Inc",
|
|
16
|
+
"email": "info@aspectron.com"
|
|
17
|
+
}
|
|
18
|
+
],
|
|
19
|
+
"browser": {
|
|
20
|
+
"request": "browser-request"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@noble/hashes": "1.1.5",
|
|
24
|
+
"@noble/secp256k1": "1.7.1",
|
|
25
|
+
"bn.js": "=4.11.8",
|
|
26
|
+
"bs58": "^4.0.1",
|
|
27
|
+
"buffer-compare": "=1.1.1",
|
|
28
|
+
"elliptic": "^6.5.3",
|
|
29
|
+
"inherits": "=2.0.1",
|
|
30
|
+
"lodash": "^4.17.20",
|
|
31
|
+
"url": "*"
|
|
32
|
+
},
|
|
33
|
+
"description": "JavaScript Kaspa library.",
|
|
34
|
+
"___devDependencies": {
|
|
35
|
+
"base-x": "=3.0.4",
|
|
36
|
+
"bitcore-build": "^8.23.1",
|
|
37
|
+
"brfs": "^2.0.1",
|
|
38
|
+
"chai": "^4.2.0",
|
|
39
|
+
"gulp": "^4.0.0",
|
|
40
|
+
"sinon": "^7.1.1"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [],
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "https://github.com/aspectron/kaspacore-lib"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"xbuild": "gulp",
|
|
50
|
+
"xcoverage": "gulp coverage",
|
|
51
|
+
"xpub": "npm run build && npm publish",
|
|
52
|
+
"xtest": "gulp test",
|
|
53
|
+
"xtest:ci": "npm run test"
|
|
54
|
+
},
|
|
55
|
+
"files": [
|
|
56
|
+
"lib",
|
|
57
|
+
"index.d.ts",
|
|
58
|
+
"index.js",
|
|
59
|
+
"LICENSE",
|
|
60
|
+
"README.md"
|
|
61
|
+
]
|
|
62
|
+
}
|